From 103d0692ea7f75288e6ba421ea149016c248fd2b Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 30 May 2017 11:05:28 -0700 Subject: [PATCH] Avoid unsupported attributes when not building with UBSAN Summary: yiwu-arbug see individual commits. Closes https://github.com/facebook/rocksdb/pull/2318 Differential Revision: D5141520 Pulled By: yiwu-arbug fbshipit-source-id: 7987c92ab4461eef36afce5a133d3a0ee0c96300 --- CMakeLists.txt | 3 +-- Makefile | 8 ++++---- db/db_test_util.h | 3 --- db/fault_injection_test.cc | 8 +++++--- util/coding.h | 14 ++++++++++++++ util/hash.cc | 9 +++++---- utilities/col_buf_encoder.cc | 8 +++++--- 7 files changed, 34 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dec306881c..4cf55534ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,7 +195,6 @@ endif() option(WITH_ASAN "build with ASAN" OFF) if(WITH_ASAN) - add_definitions(-DROCKSDB_TSAN_RUN) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address") @@ -207,7 +206,6 @@ endif() option(WITH_TSAN "build with TSAN" OFF) if(WITH_TSAN) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread -pie") - add_definitions(-DROCKSDB_TSAN_RUN) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fPIC") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fPIC") if(WITH_JEMALLOC) @@ -217,6 +215,7 @@ endif() option(WITH_UBSAN "build with UBSAN" OFF) if(WITH_UBSAN) + add_definitions(-DROCKSDB_UBSAN_RUN) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined") diff --git a/Makefile b/Makefile index b0108ca23c..1b97ce0365 100644 --- a/Makefile +++ b/Makefile @@ -193,8 +193,8 @@ endif ifdef COMPILE_WITH_TSAN DISABLE_JEMALLOC=1 EXEC_LDFLAGS += -fsanitize=thread - PLATFORM_CCFLAGS += -fsanitize=thread -fPIC -DROCKSDB_TSAN_RUN - PLATFORM_CXXFLAGS += -fsanitize=thread -fPIC -DROCKSDB_TSAN_RUN + PLATFORM_CCFLAGS += -fsanitize=thread -fPIC + PLATFORM_CXXFLAGS += -fsanitize=thread -fPIC # Turn off -pg when enabling TSAN testing, because that induces # a link failure. TODO: find the root cause PROFILING_FLAGS = @@ -211,8 +211,8 @@ endif ifdef COMPILE_WITH_UBSAN DISABLE_JEMALLOC=1 EXEC_LDFLAGS += -fsanitize=undefined - PLATFORM_CCFLAGS += -fsanitize=undefined - PLATFORM_CXXFLAGS += -fsanitize=undefined + PLATFORM_CCFLAGS += -fsanitize=undefined -DROCKSDB_UBSAN_RUN + PLATFORM_CXXFLAGS += -fsanitize=undefined -DROCKSDB_UBSAN_RUN endif ifndef DISABLE_JEMALLOC diff --git a/db/db_test_util.h b/db/db_test_util.h index f729f50c99..384b19243a 100644 --- a/db/db_test_util.h +++ b/db/db_test_util.h @@ -49,10 +49,7 @@ #include "util/mutexlock.h" #include "util/string_util.h" -// SyncPoint is not supported in Released Windows Mode. -#if !(defined NDEBUG) || !defined(OS_WIN) #include "util/sync_point.h" -#endif // !(defined NDEBUG) || !defined(OS_WIN) #include "util/testharness.h" #include "util/testutil.h" #include "utilities/merge_operators.h" diff --git a/db/fault_injection_test.cc b/db/fault_injection_test.cc index 7fb708aa56..98b9b9b974 100644 --- a/db/fault_injection_test.cc +++ b/db/fault_injection_test.cc @@ -230,10 +230,12 @@ class FaultInjectionTest : public testing::Test, return Status::OK(); } -#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 9) -__attribute__((__no_sanitize__("undefined"))) -#elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) +#ifdef ROCKSDB_UBSAN_RUN +#if defined(__clang__) +__attribute__((__no_sanitize__("shift"), no_sanitize("signed-integer-overflow"))) +#elif defined(__GNUC__) __attribute__((__no_sanitize_undefined__)) +#endif #endif // Return the ith key Slice Key(int i, std::string* storage) const { diff --git a/util/coding.h b/util/coding.h index 2cc6f30e7b..be33dba8d3 100644 --- a/util/coding.h +++ b/util/coding.h @@ -354,6 +354,13 @@ inline Slice GetSliceUntil(Slice* slice, char delimiter) { } template +#ifdef ROCKSDB_UBSAN_RUN +#if defined(__clang__) +__attribute__((__no_sanitize__("alignment"))) +#elif defined(__GNUC__) +__attribute__((__no_sanitize_undefined__)) +#endif +#endif inline void PutUnaligned(T *memory, const T &value) { #if defined(PLATFORM_UNALIGNED_ACCESS_NOT_ALLOWED) char *nonAlignedMemory = reinterpret_cast(memory); @@ -364,6 +371,13 @@ inline void PutUnaligned(T *memory, const T &value) { } template +#ifdef ROCKSDB_UBSAN_RUN +#if defined(__clang__) +__attribute__((__no_sanitize__("alignment"))) +#elif defined(__GNUC__) +__attribute__((__no_sanitize_undefined__)) +#endif +#endif inline void GetUnaligned(const T *memory, T *value) { #if defined(PLATFORM_UNALIGNED_ACCESS_NOT_ALLOWED) char *nonAlignedMemory = reinterpret_cast(value); diff --git a/util/hash.cc b/util/hash.cc index 02b59952de..5af5c5a1d9 100644 --- a/util/hash.cc +++ b/util/hash.cc @@ -15,12 +15,13 @@ namespace rocksdb { -// This function may intentionally do a left shift on a -ve number -#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 9) -__attribute__((__no_sanitize__("undefined"))) -#elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) +#ifdef ROCKSDB_UBSAN_RUN +#if defined(__clang__) +__attribute__((__no_sanitize__("shift"))) +#elif defined(__GNUC__) __attribute__((__no_sanitize_undefined__)) #endif +#endif uint32_t Hash(const char* data, size_t n, uint32_t seed) { // Similar to murmur hash const uint32_t m = 0xc6a4a793; diff --git a/utilities/col_buf_encoder.cc b/utilities/col_buf_encoder.cc index 5335498c6b..4e5f7e1305 100644 --- a/utilities/col_buf_encoder.cc +++ b/utilities/col_buf_encoder.cc @@ -46,11 +46,13 @@ ColBufEncoder *ColBufEncoder::NewColBufEncoder( return nullptr; } -#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 9) -__attribute__((__no_sanitize__("undefined"))) -#elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) +#ifdef ROCKSDB_UBSAN_RUN +#if defined(__clang__) +__attribute__((__no_sanitize__("shift"))) +#elif defined(__GNUC__) __attribute__((__no_sanitize_undefined__)) #endif +#endif size_t FixedLengthColBufEncoder::Append(const char *buf) { if (nullable_) { if (buf == nullptr) {