Skip to content

Commit a2291a5

Browse files
committed
Enable LSAN for Android
Make use of the newly added thread-properties API (available since 31). Differential Revision: https://reviews.llvm.org/D85927
1 parent 62e2ac6 commit a2291a5

29 files changed

+187
-53
lines changed

compiler-rt/cmake/config-ix.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ else()
681681
endif()
682682

683683
if (COMPILER_RT_HAS_SANITIZER_COMMON AND LSAN_SUPPORTED_ARCH AND
684-
OS_NAME MATCHES "Darwin|Linux|NetBSD|Fuchsia")
684+
OS_NAME MATCHES "Android|Darwin|Linux|NetBSD|Fuchsia")
685685
set(COMPILER_RT_HAS_LSAN TRUE)
686686
else()
687687
set(COMPILER_RT_HAS_LSAN FALSE)

compiler-rt/lib/asan/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ append_rtti_flag(OFF ASAN_CFLAGS)
8686
set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
8787

8888
if(ANDROID)
89+
list(APPEND ASAN_CFLAGS -fno-emulated-tls)
90+
list(APPEND ASAN_DYNAMIC_LINK_FLAGS -fuse-ld=lld)
8991
# Put most Sanitizer shared libraries in the global group. For more details, see
9092
# android-changes-for-ndk-developers.md#changes-to-library-search-order
9193
if (COMPILER_RT_HAS_Z_GLOBAL)
@@ -232,7 +234,9 @@ else()
232234
-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
233235
# The Solaris 11.4 linker supports a subset of GNU ld version scripts,
234236
# but requires a special option to enable it.
235-
if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
237+
# This is used/compatible with ANDROID because we force `lld` on ANDROID (line 90).
238+
# Therefore we don't want to add it for ANDROID.
239+
if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT AND NOT ANDROID)
236240
list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
237241
endif()
238242
set_property(SOURCE

compiler-rt/lib/asan/tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ if(APPLE)
9191
endif()
9292
if(ANDROID)
9393
list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS -pie)
94+
list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS -fuse-ld=lld)
9495
endif()
9596

9697
set(ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS
@@ -288,6 +289,7 @@ if(ANDROID)
288289
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
289290
$<TARGET_OBJECTS:RTSanitizerCommonCoverage.${arch}>
290291
$<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>
292+
$<TARGET_OBJECTS:RTLSanCommon.${arch}>
291293
$<TARGET_OBJECTS:RTUbsan.${arch}>
292294
$<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
293295
${COMPILER_RT_GTEST_SOURCE}

compiler-rt/lib/lsan/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include_directories(..)
22

33
set(LSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
4+
set(LSAN_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
45
append_rtti_flag(OFF LSAN_CFLAGS)
56

67
set(LSAN_COMMON_SOURCES
@@ -33,6 +34,11 @@ set(LSAN_HEADERS
3334

3435
set(LSAN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
3536

37+
if(ANDROID)
38+
list(APPEND LSAN_CFLAGS -fno-emulated-tls)
39+
list(APPEND LSAN_LINK_FLAGS -fuse-ld=lld)
40+
endif()
41+
3642
add_compiler_rt_object_libraries(RTLSanCommon
3743
OS ${SANITIZER_COMMON_SUPPORTED_OS}
3844
ARCHS ${LSAN_COMMON_SUPPORTED_ARCH}
@@ -61,7 +67,7 @@ if(COMPILER_RT_HAS_LSAN)
6167
RTSanitizerCommonCoverage
6268
RTSanitizerCommonSymbolizer
6369
CFLAGS ${LSAN_CFLAGS}
64-
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS}
70+
LINK_FLAGS ${LSAN_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS}
6571
LINK_LIBS ${LSAN_LINK_LIBS}
6672
PARENT_TARGET lsan)
6773
else()
@@ -78,6 +84,7 @@ if(COMPILER_RT_HAS_LSAN)
7884
$<TARGET_OBJECTS:RTLSanCommon.${arch}>
7985
ADDITIONAL_HEADERS ${LSAN_HEADERS}
8086
CFLAGS ${LSAN_CFLAGS}
87+
LINK_FLAGS ${LSAN_LINK_FLAGS}
8188
PARENT_TARGET lsan)
8289
endforeach()
8390
endif()

compiler-rt/lib/lsan/lsan_common.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ static const char kSuppressionLeak[] = "leak";
7171
static const char *kSuppressionTypes[] = { kSuppressionLeak };
7272
static const char kStdSuppressions[] =
7373
#if SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
74-
// For more details refer to the SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
75-
// definition.
76-
"leak:*pthread_exit*\n"
74+
// For more details refer to the SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
75+
// definition.
76+
"leak:*pthread_exit*\n"
7777
#endif // SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
7878
#if SANITIZER_MAC
79-
// For Darwin and os_log/os_trace: https://reviews.llvm.org/D35173
80-
"leak:*_os_trace*\n"
79+
// For Darwin and os_log/os_trace: https://reviews.llvm.org/D35173
80+
"leak:*_os_trace*\n"
8181
#endif
82-
// TLS leak in some glibc versions, described in
83-
// https://sourceware.org/bugzilla/show_bug.cgi?id=12650.
84-
"leak:*tls_get_addr*\n";
82+
// TLS leak in some glibc versions, described in
83+
// https://sourceware.org/bugzilla/show_bug.cgi?id=12650.
84+
"leak:*tls_get_addr*\n";
8585

8686
void InitializeSuppressions() {
8787
CHECK_EQ(nullptr, suppression_ctx);
@@ -294,6 +294,22 @@ static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
294294
kReachable);
295295
}
296296
}
297+
#if SANITIZER_ANDROID
298+
if (HAS_ANDROID_THREAD_PROPERTIES_API) {
299+
auto *cb = +[](void *dtls_begin, void *dtls_end, uptr /*dso_idd*/,
300+
void *arg) -> void {
301+
ScanRangeForPointers(reinterpret_cast<uptr>(dtls_begin),
302+
reinterpret_cast<uptr>(dtls_end),
303+
reinterpret_cast<Frontier *>(arg), "DTLS",
304+
kReachable);
305+
};
306+
307+
// FIXME: There might be a race-condition here (and in Bionic) if the
308+
// thread is suspended in the middle of updating its DTLS. IOWs, we
309+
// could scan already freed memory. (probably fine for now)
310+
__libc_iterate_dynamic_tls(os_id, cb, frontier);
311+
}
312+
#else
297313
if (dtls && !DTLSInDestruction(dtls)) {
298314
for (uptr j = 0; j < dtls->dtv_size; ++j) {
299315
uptr dtls_beg = dtls->dtv[j].beg;
@@ -309,6 +325,7 @@ static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
309325
// this and continue.
310326
LOG_THREADS("Thread %d has DTLS under destruction.\n", os_id);
311327
}
328+
#endif
312329
}
313330
}
314331
}
@@ -575,8 +592,16 @@ static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads,
575592
}
576593

577594
static bool CheckForLeaks() {
595+
#if SANITIZER_ANDROID
596+
// Presence of the ThreadProperties API implies the presence of
597+
// TLS support, which is required for calling __lsan_is_turned_off().
598+
// Therefore, this check must preceed that.
599+
if (!HAS_ANDROID_THREAD_PROPERTIES_API)
600+
return false;
601+
#endif
602+
578603
if (&__lsan_is_turned_off && __lsan_is_turned_off())
579-
return false;
604+
return false;
580605
EnsureMainThreadIDIsCorrect();
581606
CheckForLeaksParam param;
582607
LockStuffAndStopTheWorld(CheckForLeaksCallback, &param);

compiler-rt/lib/lsan/lsan_common.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@
2929
// To enable LeakSanitizer on a new architecture, one needs to implement the
3030
// internal_clone function as well as (probably) adjust the TLS machinery for
3131
// the new architecture inside the sanitizer library.
32-
#if (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC) && \
33-
(SANITIZER_WORDSIZE == 64) && \
32+
#if (SANITIZER_LINUX || SANITIZER_MAC) && (SANITIZER_WORDSIZE == 64) && \
3433
(defined(__x86_64__) || defined(__mips64) || defined(__aarch64__) || \
3534
defined(__powerpc64__) || defined(__s390x__))
3635
#define CAN_SANITIZE_LEAKS 1
37-
#elif defined(__i386__) && \
38-
(SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC)
36+
#elif defined(__i386__) && (SANITIZER_LINUX || SANITIZER_MAC)
3937
#define CAN_SANITIZE_LEAKS 1
40-
#elif defined(__arm__) && \
41-
SANITIZER_LINUX && !SANITIZER_ANDROID
38+
#elif defined(__arm__) && SANITIZER_LINUX
4239
#define CAN_SANITIZE_LEAKS 1
4340
#elif SANITIZER_NETBSD || SANITIZER_FUCHSIA
4441
#define CAN_SANITIZE_LEAKS 1

compiler-rt/lib/lsan/lsan_common_linux.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,28 @@ static bool IsLinker(const LoadedModule& module) {
4141

4242
__attribute__((tls_model("initial-exec")))
4343
THREADLOCAL int disable_counter;
44-
bool DisabledInThisThread() { return disable_counter > 0; }
45-
void DisableInThisThread() { disable_counter++; }
44+
bool DisabledInThisThread() {
45+
#if SANITIZER_ANDROID
46+
// LSAN is only enabled with Android-S and up.
47+
if (!HAS_ANDROID_THREAD_PROPERTIES_API)
48+
return true;
49+
#endif
50+
return disable_counter > 0;
51+
}
52+
void DisableInThisThread() {
53+
#if SANITIZER_ANDROID
54+
// LSAN is only enabled with Android-S and up.
55+
if (!HAS_ANDROID_THREAD_PROPERTIES_API)
56+
return;
57+
#endif
58+
disable_counter++;
59+
}
4660
void EnableInThisThread() {
61+
#if SANITIZER_ANDROID
62+
// LSAN is only enabled with Android-S and up.
63+
if (!HAS_ANDROID_THREAD_PROPERTIES_API)
64+
return;
65+
#endif
4766
if (disable_counter == 0) {
4867
DisableCounterUnderflow();
4968
}
@@ -95,7 +114,15 @@ static int ProcessGlobalRegionsCallback(struct dl_phdr_info *info, size_t size,
95114

96115
// Scans global variables for heap pointers.
97116
void ProcessGlobalRegions(Frontier *frontier) {
98-
if (!flags()->use_globals) return;
117+
if (!flags()->use_globals) {
118+
#if SANITIZER_ANDROID
119+
// There are known malloc'ed global variables from libc[++] on Android.
120+
// If use_globals is turnt off, we could see leaks.
121+
// Issue a warning in case users turn it off by accident.
122+
Report("use_globals=0 on Android could lead to false reports.");
123+
#endif
124+
return;
125+
}
99126
dl_iterate_phdr(ProcessGlobalRegionsCallback, frontier);
100127
}
101128

compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@
104104
//
105105
// FIXME: do we have anything like this on Mac?
106106
#ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
107-
#if ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_OPENBSD || \
108-
SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC)
107+
#if (SANITIZER_LINUX || SANITIZER_OPENBSD || SANITIZER_FUCHSIA || \
108+
SANITIZER_NETBSD) && \
109+
!defined(PIC)
109110
#define SANITIZER_CAN_USE_PREINIT_ARRAY 1
110111
// Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.
111112
// FIXME: Check for those conditions.

compiler-rt/lib/sanitizer_common/sanitizer_linux.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ ALWAYS_INLINE uptr *get_android_tls_ptr() {
154154
return reinterpret_cast<uptr *>(&__get_tls()[TLS_SLOT_SANITIZER]);
155155
}
156156

157+
// Bionic provides this API since 31.
158+
extern "C" SANITIZER_WEAK_ATTRIBUTE void __libc_get_static_tls_bounds(void **,
159+
void **);
160+
extern "C" SANITIZER_WEAK_ATTRIBUTE void __libc_iterate_dynamic_tls(
161+
pid_t, void (*cb)(void *, void *, uptr, void *), void *);
162+
163+
#define HAS_ANDROID_THREAD_PROPERTIES_API (&__libc_iterate_dynamic_tls != 0)
164+
165+
#else
166+
#define HAS_ANDROID_THREAD_PROPERTIES_API (0)
157167
#endif // SANITIZER_ANDROID
158168

159169
} // namespace __sanitizer

compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,19 @@ int GetSizeFromHdr(struct dl_phdr_info *info, size_t size, void *data) {
460460

461461
#if !SANITIZER_GO
462462
static void GetTls(uptr *addr, uptr *size) {
463-
#if SANITIZER_LINUX && !SANITIZER_ANDROID
463+
#if SANITIZER_ANDROID
464+
if (HAS_ANDROID_THREAD_PROPERTIES_API) {
465+
void *start_addr;
466+
void *end_addr;
467+
__libc_get_static_tls_bounds(&start_addr, &end_addr);
468+
*addr = reinterpret_cast<uptr>(start_addr);
469+
*size =
470+
reinterpret_cast<uptr>(end_addr) - reinterpret_cast<uptr>(start_addr);
471+
} else {
472+
*addr = 0;
473+
*size = 0;
474+
}
475+
#elif SANITIZER_LINUX && !SANITIZER_ANDROID
464476
#if defined(__x86_64__) || defined(__i386__) || defined(__s390__)
465477
*addr = ThreadSelf();
466478
*size = GetTlsSize();
@@ -504,9 +516,6 @@ static void GetTls(uptr *addr, uptr *size) {
504516
#elif SANITIZER_OPENBSD
505517
*addr = 0;
506518
*size = 0;
507-
#elif SANITIZER_ANDROID
508-
*addr = 0;
509-
*size = 0;
510519
#elif SANITIZER_SOLARIS
511520
// FIXME
512521
*addr = 0;

0 commit comments

Comments
 (0)