folly: #define UNDEFINED_SANITIZER in ubsan mode
authorLucian Grijincu <lucian@fb.com>
Tue, 2 Feb 2016 01:00:24 +0000 (17:00 -0800)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Tue, 2 Feb 2016 01:20:26 +0000 (17:20 -0800)
Summary:
Undefined Sanitizer doesn't define any macro to detect that it's active.

The build system should provide `-DUNDEFINED_SANITIZER` when building
with -fsanitize=undefined or any of the other flags from
http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html

FWIW: Chrome defines the same preprocessor symbol:
https://chromium.googlesource.com/chromium/src/+/ac18f489dca8c902e4dfaa1a28d716b7914121d0%5E%21/build/common.gypi

Reviewed By: andrewjcg

Differential Revision: D2885167

fb-gh-sync-id: e1129c0863bfde5d032c32e7d5cea7c43d82009f

folly/CPortability.h
folly/test/MemcpyTest.cpp

index e5099d238b9de3b77edcb1d5c0babc9df4165bbd..c348b3611d6c53b6b99e4b61114bf11dc52daebd 100644 (file)
 # define FOLLY_DISABLE_ADDRESS_SANITIZER
 #endif
 
-#endif
+
+/**
+ * ASAN/MSAN/TSAN define pre-processor symbols:
+ * ADDRESS_SANITIZER/MEMORY_SANITIZER/THREAD_SANITIZER.
+ *
+ * UBSAN doesn't define anything and makes it hard to
+ * conditionally compile.
+ *
+ * The build system should define UNDEFINED_SANITIZER=1 when UBSAN is
+ * used as folly whitelists some functions.
+ */
+#if UNDEFINED_SANITIZER
+# define UBSAN_DISABLE(x) __attribute__((no_sanitize(x)))
+#else
+# define UBSAN_DISABLE(x)
+#endif // UNDEFINED_SANITIZER
+
+#endif // CPORTABILITY_H
index badd344718fceb5dbc86fb4d18b9e9cbd4d8e96f..73b64ca10fa7dea554d5e06caa1761758b797ca9 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <folly/Portability.h>
+
 #include <gtest/gtest.h>
 
 namespace {
@@ -30,7 +32,7 @@ void init() {
 }
 }
 
-TEST(memcpy, zero_len) {
+TEST(memcpy, zero_len) UBSAN_DISABLE("nonnull-attribute") {
   // If length is 0, we shouldn't touch any memory.  So this should
   // not crash.
   char* srcNull = nullptr;