Don't rely on .bazelrc for signaling sanitizer builds

PiperOrigin-RevId: 582436549
pull/14754/head
Mike Kruskal 1 year ago committed by Copybara-Service
parent c3bae392a6
commit ab2451f6c2
  1. 4
      .bazelrc
  2. 4
      ci/common.bazelrc
  3. 6
      src/google/protobuf/arena_unittest.cc
  4. 2
      src/google/protobuf/generated_message_tctable_lite_test.cc
  5. 2
      src/google/protobuf/map_field.h
  6. 23
      src/google/protobuf/port_def.inc
  7. 10
      src/google/protobuf/repeated_field_unittest.cc

@ -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

@ -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

@ -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
}

@ -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

@ -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) {

@ -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
@ -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

@ -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<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

Loading…
Cancel
Save