From ab2451f6c23c1189fccd17a2f6e233bc82e9cfe1 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 14 Nov 2023 14:11:58 -0800 Subject: [PATCH] Don't rely on .bazelrc for signaling sanitizer builds PiperOrigin-RevId: 582436549 --- .bazelrc | 4 ---- ci/common.bazelrc | 4 ---- src/google/protobuf/arena_unittest.cc | 6 ++--- .../generated_message_tctable_lite_test.cc | 2 +- src/google/protobuf/map_field.h | 2 +- src/google/protobuf/port_def.inc | 23 +++++++++++-------- .../protobuf/repeated_field_unittest.cc | 10 ++++---- 7 files changed, 24 insertions(+), 27 deletions(-) diff --git a/.bazelrc b/.bazelrc index eba2a89fb0a03..4eea8fb85cb76 100644 --- a/.bazelrc +++ b/.bazelrc @@ -7,7 +7,6 @@ build:opt --compilation_mode=opt build:san-common --config=dbg --strip=never --copt=-O0 --copt=-fno-omit-frame-pointer build:asan --config=san-common --copt=-fsanitize=address --linkopt=-fsanitize=address -build:asan --copt=-DADDRESS_SANITIZER=1 # ASAN hits ODR violations with shared linkage due to rules_proto. build:asan --dynamic_mode=off @@ -15,14 +14,11 @@ build:msan --config=san-common --copt=-fsanitize=memory --linkopt=-fsanitize=mem build:msan --copt=-fsanitize-memory-track-origins build:msan --copt=-fsanitize-memory-use-after-dtor build:msan --action_env=MSAN_OPTIONS=poison_in_dtor=1 -build:msan --copt=-DMEMORY_SANITIZER=1 build:tsan --config=san-common --copt=-fsanitize=thread --linkopt=-fsanitize=thread -build:tsan --copt=-DTHREAD_SANITIZER=1 build:ubsan --config=san-common --copt=-fsanitize=undefined --linkopt=-fsanitize=undefined build:ubsan --action_env=UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 -build:ubsan --copt=-DUNDEFINED_SANITIZER=1 # Workaround for the fact that Bazel links with $CC, not $CXX # https://github.com/bazelbuild/bazel/issues/11122#issuecomment-613746748 build:ubsan --copt=-fno-sanitize=function --copt=-fno-sanitize=vptr diff --git a/ci/common.bazelrc b/ci/common.bazelrc index 623a9280ea061..12445ba717762 100644 --- a/ci/common.bazelrc +++ b/ci/common.bazelrc @@ -5,7 +5,6 @@ build:opt --compilation_mode=opt build:san-common --config=dbg --strip=never --copt=-O0 --copt=-fno-omit-frame-pointer build:asan --config=san-common --copt=-fsanitize=address --linkopt=-fsanitize=address -build:asan --copt=-DADDRESS_SANITIZER=1 # ASAN hits ODR violations with shared linkage due to rules_proto. build:asan --dynamic_mode=off @@ -13,7 +12,6 @@ build:msan --config=san-common --copt=-fsanitize=memory --linkopt=-fsanitize=mem build:msan --copt=-fsanitize-memory-track-origins build:msan --copt=-fsanitize-memory-use-after-dtor build:msan --action_env=MSAN_OPTIONS=poison_in_dtor=1 -build:msan --copt=-DMEMORY_SANITIZER=1 # Use our instrumented LLVM libc++ in Kokoro. build:docker-msan --config=msan @@ -23,11 +21,9 @@ build:docker-msan --cxxopt=-stdlib=libc++ --linkopt=-stdlib=libc++ build:tsan --config=san-common --copt=-fsanitize=thread --linkopt=-fsanitize=thread -build:tsan --copt=-DTHREAD_SANITIZER=1 build:ubsan --config=san-common --copt=-fsanitize=undefined --linkopt=-fsanitize=undefined build:ubsan --action_env=UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 -build:ubsan --copt=-DUNDEFINED_SANITIZER=1 # Workaround for the fact that Bazel links with $CC, not $CXX # https://github.com/bazelbuild/bazel/issues/11122#issuecomment-613746748 build:ubsan --copt=-fno-sanitize=function --copt=-fno-sanitize=vptr diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc index 673e3e669534e..d0387bcff97d0 100644 --- a/src/google/protobuf/arena_unittest.cc +++ b/src/google/protobuf/arena_unittest.cc @@ -1571,7 +1571,7 @@ TEST(ArenaTest, SpaceReuseForArraysSizeChecks) { } TEST(ArenaTest, SpaceReusePoisonsAndUnpoisonsMemory) { -#ifdef ADDRESS_SANITIZER +#ifdef PROTOBUF_ASAN char buf[1024]{}; constexpr int kSize = 32; { @@ -1605,9 +1605,9 @@ TEST(ArenaTest, SpaceReusePoisonsAndUnpoisonsMemory) { ASSERT_FALSE(__asan_address_is_poisoned(&c)); } -#else // ADDRESS_SANITIZER +#else // PROTOBUF_ASAN GTEST_SKIP(); -#endif // ADDRESS_SANITIZER +#endif // PROTOBUF_ASAN } diff --git a/src/google/protobuf/generated_message_tctable_lite_test.cc b/src/google/protobuf/generated_message_tctable_lite_test.cc index 3b9449dfcd051..136fea52a80fc 100644 --- a/src/google/protobuf/generated_message_tctable_lite_test.cc +++ b/src/google/protobuf/generated_message_tctable_lite_test.cc @@ -861,7 +861,7 @@ TEST(GeneratedMessageTctableLiteTest, PackedEnumSmallRange) { // This test checks that the parser doesn't overflow an int32 when computing the // array's new length. TEST(GeneratedMessageTctableLiteTest, PackedEnumSmallRangeLargeSize) { -#ifdef ABSL_HAVE_MEMORY_SANITIZER +#ifdef PROTOBUF_MSAN // This test attempts to allocate 8GB of memory, which OOMs MSAN. return; #endif diff --git a/src/google/protobuf/map_field.h b/src/google/protobuf/map_field.h index 8388f8a2e5208..b9d03c806fcbc 100644 --- a/src/google/protobuf/map_field.h +++ b/src/google/protobuf/map_field.h @@ -436,7 +436,7 @@ class PROTOBUF_EXPORT MapFieldBase : public MapFieldBaseForParse { // thread calls either ConstAccess() or MutableAccess(), on the same // MapFieldBase-derived object, and there is no synchronization going // on between them, tsan will alert. -#if defined(__SANITIZE_THREAD__) || defined(THREAD_SANITIZER) +#if defined(PROTOBUF_TSAN) void ConstAccess() const { ABSL_CHECK_EQ(seq1_, seq2_); } void MutableAccess() { if (seq1_ & 1) { diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 1d68bd271e58a..d1bf975a16b7e 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -73,15 +73,6 @@ #define PROTOBUF_has_warning_DEFINED_ #endif -#ifdef ADDRESS_SANITIZER -#include -#define PROTOBUF_POISON_MEMORY_REGION(p, n) ASAN_POISON_MEMORY_REGION(p, n) -#define PROTOBUF_UNPOISON_MEMORY_REGION(p, n) ASAN_UNPOISON_MEMORY_REGION(p, n) -#else // ADDRESS_SANITIZER -#define PROTOBUF_POISON_MEMORY_REGION(p, n) -#define PROTOBUF_UNPOISON_MEMORY_REGION(p, n) -#endif // ADDRESS_SANITIZER - // Portable PROTOBUF_BUILTIN_BSWAPxx definitions // Code must check for availability, e.g.: `defined(PROTOBUF_BUILTIN_BSWAP32)` #ifdef PROTOBUF_BUILTIN_BSWAP16 @@ -692,8 +683,22 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3), # define PROTOBUF_TSAN 1 # endif # endif +# ifdef __SANITIZE_MEMORY__ +# if __SANITIZE_MEMORY__ +# define PROTOBUF_ASAN 1 +# endif +# endif #endif +#ifdef PROTOBUF_ASAN +#include +#define PROTOBUF_POISON_MEMORY_REGION(p, n) ASAN_POISON_MEMORY_REGION(p, n) +#define PROTOBUF_UNPOISON_MEMORY_REGION(p, n) ASAN_UNPOISON_MEMORY_REGION(p, n) +#else // PROTOBUF_ASAN +#define PROTOBUF_POISON_MEMORY_REGION(p, n) +#define PROTOBUF_UNPOISON_MEMORY_REGION(p, n) +#endif // PROTOBUF_ASAN + #ifdef PROTOBUF_TSAN_READ #error PROTOBUF_TSAN_READ was previously defined #endif diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index 814f0c8d7999c..655bdf3494c50 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -450,7 +450,7 @@ TEST(RepeatedField, ReserveLarge) { } TEST(RepeatedField, ReserveHuge) { -#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || defined(ABSL_HAVE_MEMORY_SANITIZER) +#if defined(PROTOBUF_ASAN) || defined(PROTOBUF_MSAN) GTEST_SKIP() << "Disabled because sanitizer is active"; #endif // Largest value that does not clamp to the large limit: @@ -1172,17 +1172,17 @@ TEST(RepeatedField, HardenAgainstBadTruncate) { } } -#if defined(GTEST_HAS_DEATH_TEST) && (defined(ABSL_HAVE_ADDRESS_SANITIZER) || \ - defined(ABSL_HAVE_MEMORY_SANITIZER)) +#if defined(GTEST_HAS_DEATH_TEST) && \ + (defined(PROTOBUF_ASAN) || defined(PROTOBUF_MSAN)) // This function verifies that the code dies under ASAN or MSAN trying to both // read and write the reserved element directly beyond the last element. void VerifyDeathOnWriteAndReadAccessBeyondEnd(RepeatedField& field) { auto* end = field.Mutable(field.size() - 1) + 1; -#if defined(ABSL_HAVE_ADDRESS_SANITIZER) +#if defined(PROTOBUF_ASAN) EXPECT_DEATH(*end = 1, "container-overflow"); EXPECT_DEATH(EXPECT_NE(*end, 1), "container-overflow"); -#elif defined(ABSL_HAVE_MEMORY_SANITIZER) +#elif defined(PROTOBUF_MSAN) EXPECT_DEATH(EXPECT_NE(*end, 1), "use-of-uninitialized-value"); #endif