Skip to content

Commit

Permalink
Set up FuzzTest and use Clang.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 698895223
  • Loading branch information
smolkaj authored and copybara-github committed Nov 21, 2024
1 parent 1b66277 commit 5c9761a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
14 changes: 14 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Use C++ 20.
build --cxxopt=-std=c++20
build --host_cxxopt=-std=c++20

# Force the use of Clang for all builds. FuzzTest relies on Clang for sanitizer
# coverage (https://clang.llvm.org/docs/SanitizerCoverage.html).
build --action_env=CC=clang
build --action_env=CXX=clang++

# Show everything when running tests.
test --test_output=streamed

# To create this file, please run:
#
# bazel run @com_google_fuzztest//bazel:setup_configs > fuzztest.bazelrc
#
try-import %workspace%/fuzztest.bazelrc
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ jobs:
uses: actions/cache/restore@v4
with:
path: "~/.cache/bazel"
key: bazel-${{ hashFiles('MODULE.bazel', 'WORKSPACE.bazel', '.bazelrc') }}-${{ github.ref_name }}
key: bazel-${{ hashFiles('*.bazel', '*.bazelrc') }}-${{ github.ref_name }}
restore-keys: |
bazel-${{hashFiles('MODULE.bazel', 'WORKSPACE.bazel', '.bazelrc') }}
bazel-${{hashFiles('*.bazel', '*.bazelrc') }}
bazel-
- name: Save start time
Expand Down Expand Up @@ -66,4 +66,4 @@ jobs:
if: github.ref_name == 'main' || env.duration > 300
with:
path: "~/.cache/bazel"
key: bazel-${{ hashFiles('MODULE.bazel', 'WORKSPACE.bazel', '.bazelrc') }}-${{ github.ref_name }}-${{ github.run_id }}
key: bazel-${{ hashFiles('*.bazel', '*.bazelrc') }}-${{ github.ref_name }}-${{ github.run_id }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Don't track lock files.
*.lock

# Don't track Bazel output folders.
bazel-*
7 changes: 5 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "protobuf", version = "28.2", repo_name = "com_google_protobuf")
bazel_dep(name = "abseil-cpp", version = "20240116.2", repo_name = "com_google_absl")
bazel_dep(name = "googletest", version = "1.15.2", repo_name = "com_google_googletest")

# Dev Depdencies.
bazel_dep(name = "rules_license", version = "1.0.0", dev_dependency = True)
bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True, repo_name = "com_google_googletest")
bazel_dep(name = "fuzztest", version = "20241028.0", dev_dependency = True, repo_name = "com_google_fuzztest")
44 changes: 44 additions & 0 deletions fuzztest.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
### DO NOT EDIT. Generated file.
#
# To regenerate, run the following from your project's workspace:
#
# bazel run @com_google_fuzztest//bazel:setup_configs > fuzztest.bazelrc
#
# And don't forget to add the following to your project's .bazelrc:
#
# try-import %workspace%/fuzztest.bazelrc
### Common options.
#
# Do not use directly.

# Standard define for \"ifdef-ing\" any fuzz test specific code.
build:fuzztest-common --copt=-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION

# In fuzz tests, we want to catch assertion violations even in optimized builds.
build:fuzztest-common --copt=-UNDEBUG

# Enable libc++ assertions.
# See https://libcxx.llvm.org/UsingLibcxx.html#enabling-the-safe-libc-mode
build:fuzztest-common --copt=-D_LIBCPP_ENABLE_ASSERTIONS=1
### ASan (Address Sanitizer) build configuration.
#
# Use with: --config=asan

build:asan --linkopt=-fsanitize=address
build:asan --copt=-fsanitize=address
### FuzzTest build configuration.
#
# Use with: --config=fuzztest
#
# Note that this configuration includes the ASan configuration.

build:fuzztest --config=asan
build:fuzztest --config=fuzztest-common

# Link statically.
build:fuzztest --dynamic_mode=off

# We rely on the following flag instead of the compiler provided
# __has_feature(address_sanitizer) to know that we have an ASAN build even in
# the uninstrumented runtime.
build:fuzztest --copt=-DADDRESS_SANITIZER
1 change: 1 addition & 0 deletions netkat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cc_test(
deps = [
":netkat_cc_proto",
"@com_google_absl//absl/log",
"@com_google_fuzztest//fuzztest",
"@com_google_googletest//:gtest_main",
],
)
8 changes: 8 additions & 0 deletions netkat/netkat_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@
// -----------------------------------------------------------------------------

#include "absl/log/log.h"
#include "fuzztest/fuzztest.h"
#include "gtest/gtest.h"
#include "netkat/netkat.pb.h"

namespace netkat {
namespace {

// Sanity fuzz test to show that the FuzzTest library works.
void DummmyFuzzTest(PredicateProto pred, PolicyProto pol) {
LOG_EVERY_N_SEC(INFO, 1) << "pred = " << pred;
LOG_EVERY_N_SEC(INFO, 1) << "pol = " << pol;
}
FUZZ_TEST(NetkatProtoTest, DummmyFuzzTest);

// Ensures that the protobuf C++ compiler does not add underscores to the
// generated code for sub messages and oneof fields of `PredicateProto`.
//
Expand Down

0 comments on commit 5c9761a

Please sign in to comment.