[compiler-rt] aa662f6 - Disable emulated-tls for compiler-rt+tests on Android if ELF_TLS is presence.

Vy Nguyen via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 4 06:51:19 PST 2020


Author: Vy Nguyen
Date: 2020-11-04T09:49:45-05:00
New Revision: aa662f61deca10ba49cd117909d632dd2125898b

URL: https://github.com/llvm/llvm-project/commit/aa662f61deca10ba49cd117909d632dd2125898b
DIFF: https://github.com/llvm/llvm-project/commit/aa662f61deca10ba49cd117909d632dd2125898b.diff

LOG: 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

Added: 
    compiler-rt/test/sanitizer_common/TestCases/Linux/use_tls_test.cpp

Modified: 
    compiler-rt/CMakeLists.txt
    compiler-rt/lib/lsan/CMakeLists.txt
    compiler-rt/test/lit.common.cfg.py
    compiler-rt/test/lit.common.configured.in

Removed: 
    


################################################################################
diff  --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index afdaeb56413c..422ea58c1fc7 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 ff8d38d84849..0a07e67ccf28 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)
                 $<TARGET_OBJECTS:RTLSanCommon.${arch}>
         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 dc616f914c6d..6c913c89ee5c 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 4721b4b8beef..626c847f25ba 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 000000000000..e69baa4095a6
--- /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 <stdio.h>
+#include <stdlib.h>
+
+__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;
+}


        


More information about the llvm-commits mailing list