ASan: build unit tests with -fsanitize-address-zero-base-shadow on Linux and Android
authorAlexey Samsonov <samsonov@google.com>
Mon, 21 Jan 2013 10:51:18 +0000 (10:51 +0000)
committerAlexey Samsonov <samsonov@google.com>
Mon, 21 Jan 2013 10:51:18 +0000 (10:51 +0000)
llvm-svn: 173021

compiler-rt/lib/asan/asan_intercepted_functions.h
compiler-rt/lib/asan/asan_mapping.h
compiler-rt/lib/asan/tests/CMakeLists.txt
compiler-rt/lib/asan/tests/asan_noinst_test.cc

index 7f978c1..d4e8a8d 100644 (file)
@@ -255,6 +255,6 @@ DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_source_set_cancel_handler,
 #  endif  // MAC_INTERPOSE_FUNCTIONS
 # endif  // __APPLE__
 }  // extern "C"
-#endif // defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
+#endif  // defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
 
 #endif  // ASAN_INTERCEPTED_FUNCTIONS_H
index 5e30670..8decdf3 100644 (file)
@@ -111,6 +111,10 @@ static inline bool AddrIsInShadow(uptr a) {
 }
 
 static inline bool AddrIsInShadowGap(uptr a) {
+  // In zero-based shadow mode we treat addresses near zero as addresses
+  // in shadow gap as well.
+  if (ASAN_FLEXIBLE_MAPPING_AND_OFFSET)
+    return a <= kShadowGapEnd;
   return a >= kShadowGapBeg && a <= kShadowGapEnd;
 }
 
index 272950b..132304a 100644 (file)
@@ -15,6 +15,13 @@ include(CompilerRTCompile)
 include_directories(..)
 include_directories(../..)
 
+# Use zero-based shadow on Linux and Android.
+if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
+  set(ASAN_TESTS_USE_ZERO_BASE_SHADOW TRUE)
+else()
+  set(ASAN_TESTS_USE_ZERO_BASE_SHADOW FALSE)
+endif()
+
 set(ASAN_UNITTEST_HEADERS
   asan_mac_test.h
   asan_test_config.h
@@ -32,39 +39,38 @@ set(ASAN_UNITTEST_COMMON_CFLAGS
   -O2
 )
 
+if(ASAN_TESTS_USE_ZERO_BASE_SHADOW)
+  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -fPIE)
+endif()
 if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
   list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -Wno-variadic-macros)
 endif()
 
 # Use -D instead of definitions to please custom compile command.
+list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
+  -DASAN_FLEXIBLE_MAPPING_AND_OFFSET=1
+  -DASAN_HAS_BLACKLIST=1
+  -DASAN_HAS_EXCEPTIONS=1
+  -DASAN_UAR=0)
 if(ANDROID)
   list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
     -DASAN_LOW_MEMORY=1
-    -DASAN_HAS_BLACKLIST=1
-    -DASAN_HAS_EXCEPTIONS=1
-    -DASAN_NEEDS_SEGV=0
-    -DASAN_UAR=0
-    -fPIE
-  )
+    -DASAN_NEEDS_SEGV=0)
 else()
   list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
-    -DASAN_HAS_BLACKLIST=1
-    -DASAN_HAS_EXCEPTIONS=1
-    -DASAN_NEEDS_SEGV=1
-    -DASAN_UAR=0
-  )
+    -DASAN_LOW_MEMORY=0
+    -DASAN_NEEDS_SEGV=1)
 endif()
 
 set(ASAN_LINK_FLAGS)
-if(ANDROID)
-  # On Android, we link with ASan runtime manually
+if(ASAN_TESTS_USE_ZERO_BASE_SHADOW)
   list(APPEND ASAN_LINK_FLAGS -pie)
-else()
-  # On other platforms, we depend on Clang driver behavior,
-  # passing -fsanitize=address flag.
+endif()
+# On Android, we link with ASan runtime manually. On other platforms we depend
+# on Clang driver behavior, passing -fsanitize=address flag.
+if(NOT ANDROID)
   list(APPEND ASAN_LINK_FLAGS -fsanitize=address)
 endif()
-
 # Unit tests on Mac depend on Foundation.
 if(APPLE)
   list(APPEND ASAN_LINK_FLAGS -framework Foundation)
@@ -84,6 +90,10 @@ set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
   -mllvm -asan-mapping-offset-log=-1  # default will be used
   -mllvm -asan-use-after-return=0
 )
+if(ASAN_TESTS_USE_ZERO_BASE_SHADOW)
+  list(APPEND ASAN_UNITTEST_INSTRUMENTED_CFLAGS
+    -fsanitize-address-zero-base-shadow)
+endif()
 
 # Compile source for the given architecture, using compiler
 # options in ${ARGN}, and add it to the object list.
index 576312b..36dd3b2 100644 (file)
@@ -330,7 +330,8 @@ TEST(AddressSanitizer, MemsetWildAddressTest) {
   // Prevent inlining of memset().
   volatile memset_p libc_memset = (memset_p)memset;
   EXPECT_DEATH(libc_memset((void*)(kLowShadowBeg + 200), 0, 100),
-               "unknown-crash.*low shadow");
+               ASAN_FLEXIBLE_MAPPING_AND_OFFSET ? "unknown-crash.*shadow gap"
+                                                : "unknown-crash.*low shadow");
   EXPECT_DEATH(libc_memset((void*)(kShadowGapBeg + 200), 0, 100),
                "unknown-crash.*shadow gap");
   EXPECT_DEATH(libc_memset((void*)(kHighShadowBeg + 200), 0, 100),