From 048fa5a8f18d222d51f928da4098c2c826b88d62 Mon Sep 17 00:00:00 2001 From: Xinhao Yuan Date: Fri, 10 Jan 2025 12:09:33 -0800 Subject: [PATCH] Enable FuzzTest on macOS (experimental) with documentation. PiperOrigin-RevId: 714132065 --- doc/quickstart-bazel.md | 7 ++++++- .../fuzz_tests_for_functional_testing.cc | 2 +- fuzztest/internal/coverage.cc | 17 ++++++++++++----- fuzztest/internal/runtime.cc | 6 +++--- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/doc/quickstart-bazel.md b/doc/quickstart-bazel.md index 7f73f8ae..0977ba21 100644 --- a/doc/quickstart-bazel.md +++ b/doc/quickstart-bazel.md @@ -10,7 +10,7 @@ more extensive showcase of the FuzzTest framework, consider doing the To use FuzzTest, you'll need: -* A Linux-based operating system +* A Linux-based operating system or macOS (experimental) * [Clang](https://clang.llvm.org/) * [Bazel](https://bazel.build/) @@ -52,6 +52,8 @@ Next, create a [Bazel configuration file](https://bazel.build/run/bazelrc) named `.bazelrc` to configure the build flags: ``` +common --enable_platform_specific_config + # 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 @@ -63,6 +65,9 @@ build --cxxopt=-std=c++17 # Show everything when running tests. test --test_output=streamed +build:macos --macos_minimum_os=10.15 +build:macos --no@fuzztest//fuzztest:use_riegeli + # To create this file, please run: # # bazel run @fuzztest//bazel:setup_configs > fuzztest.bazelrc diff --git a/e2e_tests/testdata/fuzz_tests_for_functional_testing.cc b/e2e_tests/testdata/fuzz_tests_for_functional_testing.cc index eeec09ae..fc88e680 100644 --- a/e2e_tests/testdata/fuzz_tests_for_functional_testing.cc +++ b/e2e_tests/testdata/fuzz_tests_for_functional_testing.cc @@ -552,7 +552,7 @@ FUZZ_TEST(MySuite, FailsWhenOneofFieldDoesntHaveOneofValue) .WithDomains(Arbitrary() .WithOneofAlwaysSet("oneof_field") .WithFieldUnset("oneof_u32") - .WithInt64Field("oneof_i64", fuzztest::Just(1l))); + .WithInt64Field("oneof_i64", fuzztest::Just(int64_t{1}))); void FailsIfProtobufEnumEqualsLabel4(TestProtobuf::Enum e) { if (e == TestProtobuf::Enum::TestProtobuf_Enum_Label4) { diff --git a/fuzztest/internal/coverage.cc b/fuzztest/internal/coverage.cc index 25a6623f..d4d50d18 100644 --- a/fuzztest/internal/coverage.cc +++ b/fuzztest/internal/coverage.cc @@ -172,9 +172,16 @@ void ExecutionCoverage::UpdateMaxStack(uintptr_t PC) { } } -// Coverage only available in Clang, but only for Linux. -// iOS and Windows and Android might not have what we need. -#if defined(__clang__) && defined(__linux__) && !defined(__ANDROID__) +// Coverage only available in Clang, but only for Linux and macOS. +// Windows and Android might not have what we need. +#if /* Supported compilers */ \ + defined(__clang__) && \ + (/* Supported platforms */ \ + (defined(__linux__) && !defined(__ANDROID__)) || defined(__APPLE__)) +#define FUZZTEST_COVERAGE_IS_AVAILABLE +#endif + +#ifdef FUZZTEST_COVERAGE_IS_AVAILABLE namespace { // Use clang's vector extensions. This way it will implement with whatever the // platform supports. @@ -315,7 +322,7 @@ bool CorpusCoverage::Update(ExecutionCoverage* execution_coverage) { return new_coverage || execution_coverage->NewCoverageFound(); } -#else // __clang__ && __linux__ +#else // FUZZTEST_COVERAGE_IS_AVAILABLE // On other compilers we just need it to build, but we know we don't have any // instrumentation. @@ -326,7 +333,7 @@ bool CorpusCoverage::Update(ExecutionCoverage* execution_coverage) { return false; } -#endif // __clang__ && __linux__ +#endif // FUZZTEST_COVERAGE_IS_AVAILABLE } // namespace fuzztest::internal diff --git a/fuzztest/internal/runtime.cc b/fuzztest/internal/runtime.cc index ecdf0200..a19be1a9 100644 --- a/fuzztest/internal/runtime.cc +++ b/fuzztest/internal/runtime.cc @@ -403,7 +403,7 @@ void Runtime::OnTestIterationEnd() { watchdog_spinlock_.clear(); } -#if defined(__linux__) +#if defined(__linux__) || defined(__APPLE__) struct OldSignalHandler { int signum; @@ -554,14 +554,14 @@ void Runtime::PrintReportOnDefaultSink() const { PrintReport(&signal_out_sink); } -#else // __linux__ +#else // __linux__ || __APPLE__ // TODO(sbenzaquen): We should still install signal handlers in other systems. void InstallSignalHandlers(FILE* out) {} void Runtime::PrintFinalStatsOnDefaultSink() const {} void Runtime::PrintReportOnDefaultSink() const {} -#endif // __linux__ +#endif // __linux__ || __APPLE__ using corpus_type = GenericDomainCorpusType;