Skip to content

Commit

Permalink
Disable emulated-tls for compiler-rt+tests on Android if ELF_TLS is p…
Browse files Browse the repository at this point in the history
…resence.

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
  • Loading branch information
oontvoo committed Nov 4, 2020
1 parent d56cd42 commit aa662f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions compiler-rt/CMakeLists.txt
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion 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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion compiler-rt/test/lit.common.cfg.py
Expand Up @@ -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:
Expand All @@ -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'
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/lit.common.configured.in
Expand Up @@ -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@)
Expand Down
21 changes: 21 additions & 0 deletions 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;
}

0 comments on commit aa662f6

Please sign in to comment.