Skip to content

Commit

Permalink
Don't rely on .bazelrc for signaling sanitizer builds
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 582436549
  • Loading branch information
mkruskal-google authored and Copybara-Service committed Nov 14, 2023
1 parent c3bae39 commit ab2451f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
4 changes: 0 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ 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

build:msan --config=san-common --copt=-fsanitize=memory --linkopt=-fsanitize=memory
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
Expand Down
4 changes: 0 additions & 4 deletions ci/common.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ 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

build:msan --config=san-common --copt=-fsanitize=memory --linkopt=-fsanitize=memory
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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/arena_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ TEST(ArenaTest, SpaceReuseForArraysSizeChecks) {
}

TEST(ArenaTest, SpaceReusePoisonsAndUnpoisonsMemory) {
#ifdef ADDRESS_SANITIZER
#ifdef PROTOBUF_ASAN
char buf[1024]{};
constexpr int kSize = 32;
{
Expand Down Expand Up @@ -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
}


Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/generated_message_tctable_lite_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/map_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 14 additions & 9 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@
#define PROTOBUF_has_warning_DEFINED_
#endif

#ifdef ADDRESS_SANITIZER
#include <sanitizer/asan_interface.h>
#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
Expand Down Expand Up @@ -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 <sanitizer/asan_interface.h>
#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
Expand Down
10 changes: 5 additions & 5 deletions src/google/protobuf/repeated_field_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<int64_t>& 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

Expand Down

0 comments on commit ab2451f

Please sign in to comment.