From aa662f61deca10ba49cd117909d632dd2125898b Mon Sep 17 00:00:00 2001 From: Vy Nguyen Date: Sat, 17 Oct 2020 01:57:02 -0400 Subject: [PATCH] Disable emulated-tls for compiler-rt+tests on Android if ELF_TLS is presence. This is necessary for enabling LSAN on Android (D89251) because: - LSAN will have false negatives if run with emulated-tls. - Bionic ELF-TLS is not compatible with Gold (hence the need for LLD) Differential Revision: https://reviews.llvm.org/D89615 --- compiler-rt/CMakeLists.txt | 6 ++++++ compiler-rt/lib/lsan/CMakeLists.txt | 4 +++- compiler-rt/test/lit.common.cfg.py | 5 ++++- compiler-rt/test/lit.common.configured.in | 1 + .../TestCases/Linux/use_tls_test.cpp | 21 +++++++++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Linux/use_tls_test.cpp diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt index afdaeb56413cf..422ea58c1fc77 100644 --- a/compiler-rt/CMakeLists.txt +++ b/compiler-rt/CMakeLists.txt @@ -121,6 +121,8 @@ if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*hf$") endif() if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*android.*") set(ANDROID 1) + string(REGEX MATCH "-target(=| +)[^ ]+android([0-9]+)" ANDROID_API_LEVEL "${CMAKE_C_FLAGS}") + set(ANDROID_API_LEVEL ${CMAKE_MATCH_2}) endif() pythonize_bool(ANDROID) @@ -275,6 +277,10 @@ if(NOT COMPILER_RT_HAS_FUNC_SYMBOL) endif() # Provide some common commmandline flags for Sanitizer runtimes. +if(${ANDROID_API_LEVEL} GREATER_EQUAL 28) + list(APPEND SANITIZER_COMMON_CFLAGS -fno-emulated-tls) + list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -fno-emulated-tls) +endif() if(NOT WIN32) append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC SANITIZER_COMMON_CFLAGS) endif() diff --git a/compiler-rt/lib/lsan/CMakeLists.txt b/compiler-rt/lib/lsan/CMakeLists.txt index ff8d38d848490..0a07e67ccf28d 100644 --- a/compiler-rt/lib/lsan/CMakeLists.txt +++ b/compiler-rt/lib/lsan/CMakeLists.txt @@ -1,6 +1,7 @@ include_directories(..) set(LSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS}) +set(LSAN_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS}) append_rtti_flag(OFF LSAN_CFLAGS) set(LSAN_COMMON_SOURCES @@ -61,7 +62,7 @@ if(COMPILER_RT_HAS_LSAN) RTSanitizerCommonCoverage RTSanitizerCommonSymbolizer CFLAGS ${LSAN_CFLAGS} - LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS} + LINK_FLAGS ${LSAN_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS} LINK_LIBS ${LSAN_LINK_LIBS} PARENT_TARGET lsan) else() @@ -78,6 +79,7 @@ if(COMPILER_RT_HAS_LSAN) $ ADDITIONAL_HEADERS ${LSAN_HEADERS} CFLAGS ${LSAN_CFLAGS} + LINK_FLAGS ${LSAN_LINK_FLAGS} PARENT_TARGET lsan) endforeach() endif() diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py index dc616f914c6d8..6c913c89ee5c2 100644 --- a/compiler-rt/test/lit.common.cfg.py +++ b/compiler-rt/test/lit.common.cfg.py @@ -355,6 +355,7 @@ def get_macos_aligned_version(macos_vers): try: android_api_level_str = subprocess.check_output([adb, "shell", "getprop", "ro.build.version.sdk"], env=env).rstrip() + android_api_codename = subprocess.check_output([adb, "shell", "getprop", "ro.build.version.codename"], env=env).rstrip().decode("utf-8") except (subprocess.CalledProcessError, OSError): lit_config.fatal("Failed to read ro.build.version.sdk (using '%s' as adb)" % adb) try: @@ -365,6 +366,8 @@ def get_macos_aligned_version(macos_vers): config.available_features.add('android-26') if android_api_level >= 28: config.available_features.add('android-28') + if android_api_level >= 31 or android_api_codename == 'S': + config.available_features.add('android-thread-properties-api') # Prepare the device. android_tmpdir = '/data/local/tmp/Output' @@ -385,7 +388,7 @@ def get_macos_aligned_version(macos_vers): env={'LANG': 'C'}) sout, _ = ldd_ver_cmd.communicate() ver_line = sout.splitlines()[0] - if ver_line.startswith(b"ldd "): + if not config.android and ver_line.startswith(b"ldd "): from distutils.version import LooseVersion ver = LooseVersion(ver_line.split()[-1].decode()) # 2.27 introduced some incompatibilities diff --git a/compiler-rt/test/lit.common.configured.in b/compiler-rt/test/lit.common.configured.in index 4721b4b8beef1..626c847f25ba5 100644 --- a/compiler-rt/test/lit.common.configured.in +++ b/compiler-rt/test/lit.common.configured.in @@ -40,6 +40,7 @@ set_default("use_thinlto", False) set_default("use_lto", config.use_thinlto) set_default("use_newpm", False) set_default("android", @ANDROID_PYBOOL@) +set_default("android_ndk_version", @ANDROID_NDK_VERSION@) set_default("android_serial", "@ANDROID_SERIAL_FOR_TESTING@") set_default("android_files_to_push", []) set_default("have_rpc_xdr_h", @HAVE_RPC_XDR_H@) diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/use_tls_test.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/use_tls_test.cpp new file mode 100644 index 0000000000000..e69baa4095a6e --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/use_tls_test.cpp @@ -0,0 +1,21 @@ +// Test that executable with ELF-TLS will link/run successfully on aarch64 +// RUN: %clangxx -fno-emulated-tls %s -o %t +// RUN: %run %t 2>&1 +// REQUIRES: android-28 + +#include +#include + +__thread void *tls_var; +int var; + +void set_var() { + var = 123; + tls_var = &var; +} +int main() { + set_var(); + fprintf(stderr, "Test alloc: %p\n", tls_var); + fflush(stderr); + return 0; +}