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 -mllvm "${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/asan/lit.cfg =================================================================== --- compiler-rt/test/asan/lit.cfg +++ compiler-rt/test/asan/lit.cfg @@ -65,8 +65,8 @@ # FIXME: Review the set of required flags and check if it can be reduced. target_cflags = [get_required_attr(config, "target_cflags")] + extra_link_flags target_cxxflags = config.cxx_mode_flags + target_cflags -clang_asan_static_cflags = (["-fsanitize=address", - "-mno-omit-leaf-frame-pointer", +clang_asan_static_cflags = (config.sanitize_address_cflags + + ["-mno-omit-leaf-frame-pointer", "-fno-omit-frame-pointer", "-fno-optimize-sibling-calls"] + config.debug_info_flags + target_cflags) Index: compiler-rt/test/lit.common.cfg =================================================================== --- compiler-rt/test/lit.common.cfg +++ compiler-rt/test/lit.common.cfg @@ -51,6 +51,12 @@ # Add compiler ID to the list of available features. config.available_features.add(compiler_id) +# Set cflags required for -fsanitize=address. +config.sanitize_address_cflags = ["-fsanitize=address"] +if config.asan_shadow_scale != '': + config.sanitize_address_cflags += ["-mllvm", "-asan-mapping-scale", + "-mllvm", 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@) Index: compiler-rt/test/lsan/lit.common.cfg =================================================================== --- compiler-rt/test/lsan/lit.common.cfg +++ compiler-rt/test/lsan/lit.common.cfg @@ -26,7 +26,7 @@ lsan_cflags = ["-fsanitize=leak"] elif lsan_lit_test_mode == "AddressSanitizer": config.name = "LeakSanitizer-AddressSanitizer" - lsan_cflags = ["-fsanitize=address"] + lsan_cflags = config.sanitize_address_cflags config.available_features.add('asan') else: lit_config.fatal("Unknown LSan test mode: %r" % lsan_lit_test_mode) Index: compiler-rt/test/sanitizer_common/lit.common.cfg =================================================================== --- compiler-rt/test/sanitizer_common/lit.common.cfg +++ compiler-rt/test/sanitizer_common/lit.common.cfg @@ -7,7 +7,7 @@ default_tool_options = [] if config.tool_name == "asan": - tool_cflags = ["-fsanitize=address"] + tool_cflags = config.sanitize_address_cflags tool_options = "ASAN_OPTIONS" elif config.tool_name == "tsan": tool_cflags = ["-fsanitize=thread"] Index: compiler-rt/test/ubsan/lit.common.cfg =================================================================== --- compiler-rt/test/ubsan/lit.common.cfg +++ compiler-rt/test/ubsan/lit.common.cfg @@ -28,7 +28,7 @@ elif ubsan_lit_test_mode == "AddressSanitizer": config.name = 'UBSan-ASan-' + config.target_arch config.available_features.add("ubsan-asan") - clang_ubsan_cflags = ["-fsanitize=address"] + clang_ubsan_cflags = config.sanitize_address_cflags default_ubsan_opts += ['detect_leaks=0'] elif ubsan_lit_test_mode == "MemorySanitizer": config.name = 'UBSan-MSan-' + config.target_arch