Index: compiler-rt/CMakeLists.txt =================================================================== --- compiler-rt/CMakeLists.txt +++ compiler-rt/CMakeLists.txt @@ -43,6 +43,25 @@ option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF) mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT) +set(COMPILER_RT_ASAN_SHADOW_SCALE "" + CACHE STRING "Override the shadow scale to be used in ASan runtime") + +if (NOT COMPILER_RT_ASAN_SHADOW_SCALE STREQUAL "") + # Check that the shadow scale value is valid. + if (NOT (COMPILER_RT_ASAN_SHADOW_SCALE GREATER -1 AND + COMPILER_RT_ASAN_SHADOW_SCALE LESS 8)) + message(FATAL_ERROR " + Invalid ASan Shadow Scale '${COMPILER_RT_ASAN_SHADOW_SCALE}'.") + endif() + + set(COMPILER_RT_ASAN_SHADOW_SCALE_LLVM_FLAG + -mllvm -asan-mapping-scale=${COMPILER_RT_ASAN_SHADOW_SCALE}) + set(COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION + ASAN_SHADOW_SCALE=${COMPILER_RT_ASAN_SHADOW_SCALE}) + set(COMPILER_RT_ASAN_SHADOW_SCALE_FLAG + -D${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION}) +endif() + set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOLEAN "Build for a bare-metal target.") Index: compiler-rt/lib/asan/CMakeLists.txt =================================================================== --- compiler-rt/lib/asan/CMakeLists.txt +++ compiler-rt/lib/asan/CMakeLists.txt @@ -39,6 +39,7 @@ include_directories(..) set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS}) +set(ASAN_COMMON_DEFINITIONS ${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION}) append_rtti_flag(OFF ASAN_CFLAGS) Index: compiler-rt/lib/asan/asan_mapping.h =================================================================== --- compiler-rt/lib/asan/asan_mapping.h +++ compiler-rt/lib/asan/asan_mapping.h @@ -131,7 +131,11 @@ // || `[0x30000000, 0x35ffffff]` || LowShadow || // || `[0x00000000, 0x2fffffff]` || LowMem || +#if defined(ASAN_SHADOW_SCALE) +static const u64 kDefaultShadowScale = ASAN_SHADOW_SCALE; +#else static const u64 kDefaultShadowScale = 3; +#endif static const u64 kDefaultShadowSentinel = ~(uptr)0; static const u64 kDefaultShadowOffset32 = 1ULL << 29; // 0x20000000 static const u64 kDefaultShadowOffset64 = 1ULL << 44; Index: compiler-rt/lib/asan/tests/CMakeLists.txt =================================================================== --- compiler-rt/lib/asan/tests/CMakeLists.txt +++ compiler-rt/lib/asan/tests/CMakeLists.txt @@ -23,6 +23,7 @@ set(ASAN_UNITTEST_COMMON_CFLAGS ${COMPILER_RT_UNITTEST_CFLAGS} ${COMPILER_RT_GTEST_CFLAGS} + ${COMPILER_RT_ASAN_SHADOW_SCALE_LLVM_FLAG} -I${COMPILER_RT_SOURCE_DIR}/include -I${COMPILER_RT_SOURCE_DIR}/lib -I${COMPILER_RT_SOURCE_DIR}/lib/asan @@ -52,6 +53,7 @@ # Use -D instead of definitions to please custom compile command. list(APPEND ASAN_UNITTEST_COMMON_CFLAGS + ${COMPILER_RT_ASAN_SHADOW_SCALE_FLAG} -DASAN_HAS_BLACKLIST=1 -DASAN_HAS_EXCEPTIONS=1 -DASAN_UAR=0) Index: compiler-rt/test/lit.common.cfg =================================================================== --- compiler-rt/test/lit.common.cfg +++ compiler-rt/test/lit.common.cfg @@ -51,6 +51,10 @@ # Add compiler ID to the list of available features. config.available_features.add(compiler_id) +# If needed, add cflag for shadow scale. +if config.asan_shadow_scale != '': + config.target_cflags += " -mllvm -asan-mapping-scale=" + config.asan_shadow_scale + # BFD linker in 64-bit android toolchains fails to find libm.so, which is a # transitive shared library dependency (via asan runtime). if config.android: Index: compiler-rt/test/lit.common.configured.in =================================================================== --- compiler-rt/test/lit.common.configured.in +++ compiler-rt/test/lit.common.configured.in @@ -25,6 +25,7 @@ set_default("compiler_rt_debug", @COMPILER_RT_DEBUG_PYBOOL@) set_default("compiler_rt_libdir", "@COMPILER_RT_LIBRARY_OUTPUT_DIR@") set_default("emulator", "@COMPILER_RT_EMULATOR@") +set_default("asan_shadow_scale", "@COMPILER_RT_ASAN_SHADOW_SCALE@") set_default("ios", False) set_default("iossim", False) set_default("sanitizer_can_use_cxxabi", @SANITIZER_CAN_USE_CXXABI_PYBOOL@)