Merge "[MTE] fix longjmp handling" into main
diff --git a/docs/elf-tls.md b/docs/elf-tls.md
index d408b3f..6b03841 100644
--- a/docs/elf-tls.md
+++ b/docs/elf-tls.md
@@ -1,9 +1,4 @@
-# Android ELF TLS (Draft)
-
-Internal links:
- * [go/android-elf-tls](http://go/android-elf-tls)
- * [One-pager](https://docs.google.com/document/d/1leyPTnwSs24P2LGiqnU6HetnN5YnDlZkihigi6qdf_M)
- * Tracking bugs: http://b/110100012, http://b/78026329
+# Android ELF TLS
[TOC]
diff --git a/docs/status.md b/docs/status.md
index bc8ab6a..e0364a8 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -125,6 +125,7 @@
* `getloadavg` (BSD/GNU extension in <stdlib.h>)
New libc behavior in Q (API level 29):
+ * Support for [ELF TLS](elf-tls.md).
* Whole printf family now supports the GNU `%m` extension, rather than a
special-case hack in `syslog`.
* `popen` now always uses `O_CLOEXEC`, not just with the `e` extension.
diff --git a/libc/Android.bp b/libc/Android.bp
index 018b7c0..5063364 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -128,7 +128,7 @@
android_x86: {
pack_relocations: false,
ldflags: ["-Wl,--hash-style=both"],
- }
+ },
},
}
@@ -1585,14 +1585,6 @@
// Do not pack libc.so relocations; see http://b/20645321 for details.
pack_relocations: false,
- // WARNING: The only libraries libc.so should depend on are libdl.so and ld-android.so!
- // If you add other libraries, make sure to add -Wl,--exclude-libs=libgcc.a to the
- // LOCAL_LDFLAGS for those libraries. This ensures that symbols that are pulled into
- // those new libraries from libgcc.a are not declared external; if that were the case,
- // then libc would not pull those symbols from libgcc.a as it should, instead relying
- // on the external symbols from the dependent libraries. That would create a "cloaked"
- // dependency on libgcc.a in libc though the libraries, which is not what you wanted!
-
shared_libs: [
"ld-android",
"libdl",
diff --git a/libc/bionic/heap_tagging.cpp b/libc/bionic/heap_tagging.cpp
index 6741be3..3d92404 100644
--- a/libc/bionic/heap_tagging.cpp
+++ b/libc/bionic/heap_tagging.cpp
@@ -53,8 +53,6 @@
heap_tagging_level = __libc_shared_globals()->initial_heap_tagging_level;
#endif
- __libc_memtag_stack_abi = __libc_shared_globals()->initial_memtag_stack_abi;
-
__libc_globals.mutate([](libc_globals* globals) {
switch (heap_tagging_level) {
case M_HEAP_TAGGING_LEVEL_TBI:
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index 939e4e1..c82c52e 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -58,7 +58,6 @@
__LIBC_HIDDEN__ constinit WriteProtected<libc_globals> __libc_globals;
__LIBC_HIDDEN__ constinit _Atomic(bool) __libc_memtag_stack;
-__LIBC_HIDDEN__ constinit bool __libc_memtag_stack_abi;
// Not public, but well-known in the BSDs.
__BIONIC_WEAK_VARIABLE_FOR_NATIVE_BRIDGE
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index ac97376..3da0a92 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -289,7 +289,11 @@
// We can't short-circuit the environment override, as `stack` is still inherited from the
// binary's settings.
- get_environment_memtag_setting(&level);
+ if (get_environment_memtag_setting(&level)) {
+ if (level == M_HEAP_TAGGING_LEVEL_NONE || level == M_HEAP_TAGGING_LEVEL_TBI) {
+ *stack = false;
+ }
+ }
return level;
}
@@ -325,14 +329,13 @@
bool memtag_stack = false;
HeapTaggingLevel level =
__get_tagging_level(memtag_dynamic_entries, phdr_start, phdr_ct, load_bias, &memtag_stack);
- // initial_memtag_stack is used by the linker (in linker.cpp) to communicate than any library
- // linked by this executable enables memtag-stack.
- // memtag_stack is also set for static executables if they request memtag stack via the note,
- // in which case it will differ from initial_memtag_stack.
- if (__libc_shared_globals()->initial_memtag_stack || memtag_stack) {
+ // This is used by the linker (in linker.cpp) to communicate than any library linked by this
+ // executable enables memtag-stack.
+ if (__libc_shared_globals()->initial_memtag_stack) {
+ if (!memtag_stack) {
+ async_safe_format_log(ANDROID_LOG_INFO, "libc", "enabling PROT_MTE as requested by linker");
+ }
memtag_stack = true;
- __libc_shared_globals()->initial_memtag_stack_abi = true;
- __get_bionic_tcb()->tls_slot(TLS_SLOT_STACK_MTE) = __allocate_stack_mte_ringbuffer(0, nullptr);
}
if (int64_t timed_upgrade = __get_memtag_upgrade_secs()) {
if (level == M_HEAP_TAGGING_LEVEL_ASYNC) {
diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp
index 2f4d206..a1d6909 100644
--- a/libc/bionic/locale.cpp
+++ b/libc/bionic/locale.cpp
@@ -35,17 +35,8 @@
#include <time.h>
#include <wchar.h>
-#include "platform/bionic/macros.h"
-
-#if defined(__BIONIC_BUILD_FOR_ANDROID_SUPPORT)
-#define USE_TLS_SLOT 0
-#else
-#define USE_TLS_SLOT 1
-#endif
-
-#if USE_TLS_SLOT
#include "bionic/pthread_internal.h"
-#endif
+#include "platform/bionic/macros.h"
// We only support two locales, the "C" locale (also known as "POSIX"),
// and the "C.UTF-8" locale (also known as "en_US.UTF-8").
@@ -82,10 +73,6 @@
return get_locale_mb_cur_max(uselocale(nullptr));
}
-#if !USE_TLS_SLOT
-static thread_local locale_t g_current_locale;
-#endif
-
static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT;
static lconv g_locale;
@@ -180,11 +167,7 @@
}
static locale_t* get_current_locale_ptr() {
-#if USE_TLS_SLOT
return &__get_bionic_tls().locale;
-#else
- return &g_current_locale;
-#endif
}
locale_t uselocale(locale_t new_locale) {
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index a8d09eb..5bd4f16 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -65,7 +65,6 @@
}
void __init_bionic_tls_ptrs(bionic_tcb* tcb, bionic_tls* tls) {
- tcb->thread()->bionic_tcb = tcb;
tcb->thread()->bionic_tls = tls;
tcb->tls_slot(TLS_SLOT_BIONIC_TLS) = tls;
}
@@ -444,14 +443,6 @@
ScopedReadLock locker(&g_thread_creation_lock);
-// This has to be done under g_thread_creation_lock or g_thread_list_lock to avoid racing with
-// __pthread_internal_remap_stack_with_mte.
-#ifdef __aarch64__
- if (__libc_memtag_stack_abi) {
- tcb->tls_slot(TLS_SLOT_STACK_MTE) = __allocate_stack_mte_ringbuffer(0, thread);
- }
-#endif
-
sigset64_t block_all_mask;
sigfillset64(&block_all_mask);
__rt_sigprocmask(SIG_SETMASK, &block_all_mask, &thread->start_mask, sizeof(thread->start_mask));
diff --git a/libc/bionic/pthread_internal.cpp b/libc/bionic/pthread_internal.cpp
index e8a8ba2..8b9573f 100644
--- a/libc/bionic/pthread_internal.cpp
+++ b/libc/bionic/pthread_internal.cpp
@@ -33,12 +33,10 @@
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
-#include <sys/prctl.h>
#include <async_safe/log.h>
#include <bionic/reserved_signals.h>
-#include "bionic/tls_defines.h"
#include "private/ErrnoRestorer.h"
#include "private/ScopedRWLock.h"
#include "private/bionic_futex.h"
@@ -73,21 +71,8 @@
g_thread_list = thread->next;
}
}
-// N.B. that this is NOT the pagesize, but 4096. This is hardcoded in the codegen.
-// See
-// https://github.com/search?q=repo%3Allvm/llvm-project%20AArch64StackTagging%3A%3AinsertBaseTaggedPointer&type=code
-constexpr size_t kStackMteRingbufferSizeMultiplier = 4096;
static void __pthread_internal_free(pthread_internal_t* thread) {
-#ifdef __aarch64__
- if (void* stack_mte_tls = thread->bionic_tcb->tls_slot(TLS_SLOT_STACK_MTE)) {
- size_t size =
- kStackMteRingbufferSizeMultiplier * (reinterpret_cast<uintptr_t>(stack_mte_tls) >> 56ULL);
- void* ptr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(stack_mte_tls) &
- ((1ULL << 56ULL) - 1ULL));
- munmap(ptr, size);
- }
-#endif
if (thread->mmap_size != 0) {
// Free mapped space, including thread stack and pthread_internal_t.
munmap(thread->mmap_base, thread->mmap_size);
@@ -191,70 +176,12 @@
async_safe_fatal("stack not found in /proc/self/maps");
}
-__LIBC_HIDDEN__ void* __allocate_stack_mte_ringbuffer(size_t n, pthread_internal_t* thread) {
- if (n > 7) async_safe_fatal("error: invalid mte stack ring buffer size");
- // Allocation needs to be aligned to 2*size to make the fancy code-gen work.
- // So we allocate 3*size - pagesz bytes, which will always contain size bytes
- // aligned to 2*size, and unmap the unneeded part.
- // See
- // https://github.com/search?q=repo%3Allvm/llvm-project%20AArch64StackTagging%3A%3AinsertBaseTaggedPointer&type=code
- //
- // In the worst case, we get an allocation that is one page past the properly
- // aligned address, in which case we have to unmap the previous
- // 2*size - pagesz bytes. In that case, we still have size properly aligned
- // bytes left.
- size_t size = (1 << n) * kStackMteRingbufferSizeMultiplier;
- size_t pgsize = page_size();
-
- size_t alloc_size = __BIONIC_ALIGN(3 * size - pgsize, pgsize);
- void* allocation_ptr =
- mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- if (allocation_ptr == MAP_FAILED)
- async_safe_fatal("error: failed to allocate stack mte ring buffer");
- uintptr_t allocation = reinterpret_cast<uintptr_t>(allocation_ptr);
-
- size_t alignment = 2 * size;
- uintptr_t aligned_allocation = __BIONIC_ALIGN(allocation, alignment);
- if (allocation != aligned_allocation) {
- munmap(reinterpret_cast<void*>(allocation), aligned_allocation - allocation);
- }
- if (aligned_allocation + size != allocation + alloc_size) {
- munmap(reinterpret_cast<void*>(aligned_allocation + size),
- (allocation + alloc_size) - (aligned_allocation + size));
- }
-
- const char* name;
- if (thread == nullptr) {
- name = "stack_mte_ring:main";
- } else {
- // The kernel doesn't copy the name string, but this variable will last at least as long as the
- // mapped area. We unmap the ring buffer before unmapping the rest of the thread storage.
- auto& name_buffer = thread->stack_mte_ringbuffer_vma_name_buffer;
- static_assert(arraysize(name_buffer) >= arraysize("stack_mte_ring:") + 11 + 1);
- async_safe_format_buffer(name_buffer, arraysize(name_buffer), "stack_mte_ring:%d", thread->tid);
- name = name_buffer;
- }
- prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, reinterpret_cast<void*>(aligned_allocation), size, name);
-
- // We store the size in the top byte of the pointer (which is ignored)
- return reinterpret_cast<void*>(aligned_allocation | ((1ULL << n) << 56ULL));
-}
-
bool __pthread_internal_remap_stack_with_mte() {
#if defined(__aarch64__)
- ScopedWriteLock creation_locker(&g_thread_creation_lock);
- ScopedReadLock list_locker(&g_thread_list_lock);
- // If process already uses memtag-stack ABI, we don't need to do anything.
- if (__libc_memtag_stack_abi) return false;
- __libc_memtag_stack_abi = true;
-
- for (pthread_internal_t* t = g_thread_list; t != nullptr; t = t->next) {
- if (t->terminating) continue;
- t->bionic_tcb->tls_slot(TLS_SLOT_STACK_MTE) =
- __allocate_stack_mte_ringbuffer(0, t->is_main() ? nullptr : t);
- }
+ // If process doesn't have MTE enabled, we don't need to do anything.
if (!atomic_load(&__libc_globals->memtag)) return false;
- if (atomic_exchange(&__libc_memtag_stack, true)) return false;
+ bool prev = atomic_exchange(&__libc_memtag_stack, true);
+ if (prev) return false;
uintptr_t lo, hi;
__find_main_stack_limits(&lo, &hi);
@@ -262,6 +189,8 @@
PROT_READ | PROT_WRITE | PROT_MTE | PROT_GROWSDOWN)) {
async_safe_fatal("error: failed to set PROT_MTE on main thread");
}
+ ScopedWriteLock creation_locker(&g_thread_creation_lock);
+ ScopedReadLock list_locker(&g_thread_list_lock);
for (pthread_internal_t* t = g_thread_list; t != nullptr; t = t->next) {
if (t->terminating || t->is_main()) continue;
if (mprotect(t->mmap_base_unguarded, t->mmap_size_unguarded,
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index b270a06..b0e9461 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -178,10 +178,6 @@
bionic_tls* bionic_tls;
int errno_value;
-
- bionic_tcb* bionic_tcb;
- char stack_mte_ringbuffer_vma_name_buffer[32];
-
bool is_main() { return start_routine == nullptr; }
};
@@ -213,7 +209,6 @@
__LIBC_HIDDEN__ void __pthread_internal_remove(pthread_internal_t* thread);
__LIBC_HIDDEN__ void __pthread_internal_remove_and_free(pthread_internal_t* thread);
__LIBC_HIDDEN__ void __find_main_stack_limits(uintptr_t* low, uintptr_t* high);
-__LIBC_HIDDEN__ void* __allocate_stack_mte_ringbuffer(size_t n, pthread_internal_t* thread);
static inline __always_inline bionic_tcb* __get_bionic_tcb() {
return reinterpret_cast<bionic_tcb*>(&__get_tls()[MIN_TLS_SLOT]);
diff --git a/libc/include/math.h b/libc/include/math.h
index fc6c228..343ab98 100644
--- a/libc/include/math.h
+++ b/libc/include/math.h
@@ -68,10 +68,7 @@
#define isnormal(x) __builtin_isnormal(x)
-#define signbit(x) \
- ((sizeof(x) == sizeof(float)) ? __builtin_signbitf(x) \
- : (sizeof(x) == sizeof(double)) ? __builtin_signbit(x) \
- : __builtin_signbitl(x))
+#define signbit(x) __builtin_signbit(x)
double acos(double __x);
float acosf(float __x);
@@ -308,20 +305,6 @@
#define islessgreater(x, y) __builtin_islessgreater((x), (y))
#define isunordered(x, y) __builtin_isunordered((x), (y))
-/*
- * https://code.google.com/p/android/issues/detail?id=271629
- * To be fully compliant with C++, we need to not define these (C doesn't
- * specify them either). Exposing these means that isinf and isnan will have a
- * return type of int in C++ rather than bool like they're supposed to be.
- *
- * GNU libstdc++ 4.9 isn't able to handle a standard compliant C library. Its
- * <cmath> will `#undef isnan` from math.h and only adds the function overloads
- * to the std namespace, making it impossible to use both <cmath> (which gets
- * included by a lot of other standard headers) and ::isnan.
- */
-int (isinf)(double __x) __attribute_const__;
-int (isnan)(double __x) __attribute_const__;
-
/* POSIX extensions. */
extern int signgam;
@@ -362,6 +345,7 @@
double scalb(double __x, double __exponent);
double drem(double __x, double __y);
int finite(double __x) __attribute_const__;
+int isinff(float __x) __attribute_const__;
int isnanf(float __x) __attribute_const__;
double gamma_r(double __x, int* _Nonnull __sign);
double lgamma_r(double __x, int* _Nonnull __sign);
@@ -402,6 +386,8 @@
#define M_2_SQRTPIl 1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
#define M_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
#define M_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
+int isinfl(long double __x) __attribute_const__;
+int isnanl(long double __x) __attribute_const__;
#endif
__END_DECLS
diff --git a/libc/platform/bionic/tls_defines.h b/libc/platform/bionic/tls_defines.h
index 06c6617..8fe8701 100644
--- a/libc/platform/bionic/tls_defines.h
+++ b/libc/platform/bionic/tls_defines.h
@@ -85,8 +85,7 @@
// [1] "Addenda to, and Errata in, the ABI for the ARM Architecture". Section 3.
// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0045e/IHI0045E_ABI_addenda.pdf
-#define MIN_TLS_SLOT (-3) // update this value when reserving a slot
-#define TLS_SLOT_STACK_MTE (-3)
+#define MIN_TLS_SLOT (-2) // update this value when reserving a slot
#define TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE (-2)
#define TLS_SLOT_BIONIC_TLS (-1)
#define TLS_SLOT_DTV 0
diff --git a/libc/private/bionic_globals.h b/libc/private/bionic_globals.h
index a1bebda..0949056 100644
--- a/libc/private/bionic_globals.h
+++ b/libc/private/bionic_globals.h
@@ -76,23 +76,10 @@
};
__LIBC_HIDDEN__ extern WriteProtected<libc_globals> __libc_globals;
-// These cannot be in __libc_globals, because we cannot access the
+// This cannot be in __libc_globals, because we cannot access the
// WriteProtected in a thread-safe way.
// See b/328256432.
-//
-// __libc_memtag_stack says whether stack MTE is enabled on the process, i.e.
-// whether the stack pages are mapped with PROT_MTE. This is always false if
-// MTE is disabled for the process (i.e. libc_globals.memtag is false).
__LIBC_HIDDEN__ extern _Atomic(bool) __libc_memtag_stack;
-// __libc_memtag_stack_abi says whether the process contains any code that was
-// compiled with memtag-stack. This is true even if the process does not have
-// MTE enabled (e.g. because it was overridden using MEMTAG_OPTIONS, or because
-// MTE is disabled for the device).
-// Code compiled with memtag-stack needs a stack history buffer in
-// TLS_SLOT_STACK_MTE, because the codegen will emit an unconditional
-// (to keep the code branchless) write to it.
-// Protected by g_heap_creation_lock.
-__LIBC_HIDDEN__ extern bool __libc_memtag_stack_abi;
struct abort_msg_t;
struct crash_detail_page_t;
@@ -146,9 +133,7 @@
size_t scudo_stack_depot_size = 0;
HeapTaggingLevel initial_heap_tagging_level = M_HEAP_TAGGING_LEVEL_NONE;
- // See comments for __libc_memtag_stack / __libc_memtag_stack_abi above.
bool initial_memtag_stack = false;
- bool initial_memtag_stack_abi = false;
int64_t heap_tagging_upgrade_timer_sec = 0;
void (*memtag_stack_dlopen_callback)() = nullptr;
diff --git a/libdl/Android.bp b/libdl/Android.bp
index f53d2d1..1bbd902 100644
--- a/libdl/Android.bp
+++ b/libdl/Android.bp
@@ -65,24 +65,6 @@
"bug_24465209_workaround",
],
- // NOTE: --exclude-libs=libgcc.a makes sure that any symbols libdl.so pulls from
- // libgcc.a are made static to libdl.so. This in turn ensures that libraries that
- // a) pull symbols from libgcc.a and b) depend on libdl.so will not rely on libdl.so
- // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
- // we use this property to make sure libc.so has its own copy of the code from
- // libgcc.a it uses.
- //
- // DO NOT REMOVE --exclude-libs!
- ldflags: [
- "-Wl,--exclude-libs=libgcc.a",
- "-Wl,--exclude-libs=libgcc_stripped.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-riscv64-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
- ],
-
arch: {
arm: {
version_script: ":libdl.arm.map",
@@ -94,13 +76,9 @@
version_script: ":libdl.riscv64.map",
},
x86: {
- // Exclude libgcc_eh.a for the same reasons as above
- ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
version_script: ":libdl.x86.map",
},
x86_64: {
- // Exclude libgcc_eh.a for the same reasons as above
- ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
version_script: ":libdl.x86_64.map",
},
},
@@ -159,37 +137,6 @@
recovery_available: true,
native_bridge_supported: true,
- // NOTE: --exclude-libs=libgcc.a makes sure that any symbols libdl.so pulls from
- // libgcc.a are made static to libdl.so. This in turn ensures that libraries that
- // a) pull symbols from libgcc.a and b) depend on libdl.so will not rely on libdl.so
- // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
- // we use this property to make sure libc.so has its own copy of the code from
- // libgcc.a it uses.
- //
- // DO NOT REMOVE --exclude-libs!
-
- ldflags: [
- "-Wl,--exclude-libs=libgcc.a",
- "-Wl,--exclude-libs=libgcc_stripped.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-riscv64-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
- ],
-
- // for x86, exclude libgcc_eh.a for the same reasons as above
- arch: {
- x86: {
- ldflags: [
- "-Wl,--exclude-libs=libgcc_eh.a",
- ],
- },
- x86_64: {
- ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
- },
- },
-
srcs: ["libdl_android.cpp"],
version_script: "libdl_android.map.txt",
diff --git a/libm/Android.bp b/libm/Android.bp
index 09d8535..00d90a0 100644
--- a/libm/Android.bp
+++ b/libm/Android.bp
@@ -199,11 +199,6 @@
"upstream-netbsd/lib/libm/complex/ctanhl.c",
"upstream-netbsd/lib/libm/complex/ctanl.c",
- // TODO: this comes from from upstream's libc, not libm, but it's an
- // implementation detail that should have hidden visibility, so it needs
- // to be in whatever library the math code is in.
- "digittoint.c",
-
// Functionality not in the BSDs.
"significandl.c",
"fake_long_double.c",
diff --git a/libm/digittoint.c b/libm/digittoint.c
deleted file mode 100644
index 1824788..0000000
--- a/libm/digittoint.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/*-
- * Copyright (c) 2007 David Schultz
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * $FreeBSD$
- */
-
-#include <sys/cdefs.h>
-
-/* digittoint is in the FreeBSD C library, but implemented in terms of locale stuff. */
-__LIBC_HIDDEN__ int digittoint(char ch) {
- int d = ch - '0';
- if ((unsigned) d < 10) {
- return d;
- }
- d = ch - 'a';
- if ((unsigned) d < 6) {
- return d + 10;
- }
- d = ch - 'A';
- if ((unsigned) d < 6) {
- return d + 10;
- }
- return -1;
-}
diff --git a/libm/freebsd-compat.h b/libm/freebsd-compat.h
index 7accc55..a3c7cd4 100644
--- a/libm/freebsd-compat.h
+++ b/libm/freebsd-compat.h
@@ -27,18 +27,12 @@
#define __strong_reference(sym,aliassym) \
extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
-#define __warn_references(sym,msg) /* ignored */
-
-// digittoint is in BSD's <ctype.h>, but not ours, so we have a secret
-// implementation in libm. We reuse parts of libm in the NDK's
-// libandroid_support, where it's a static library, so we want all our
-// "hidden" functions start with a double underscore --- being HIDDEN
-// in the ELF sense is not sufficient.
-#define digittoint __libm_digittoint
-int digittoint(char ch);
-
-// Similarly rename _scan_nan.
-#define _scan_nan __libm_scan_nan
+// digittoint is in BSD's <ctype.h> but not ours.
+#include <ctype.h>
+static inline int digittoint(char ch) {
+ if (!isxdigit(ch)) return -1;
+ return isdigit(ch) ? (ch - '0') : (_tolower(ch) - 'a');
+}
// FreeBSD exports these in <math.h> but we don't.
double cospi(double);
diff --git a/linker/Android.bp b/linker/Android.bp
index ce5eff9..da57f7a 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -1,14 +1,3 @@
-// ========================================================
-// linker_wrapper - Linux Bionic (on the host)
-// ========================================================
-
-// This is used for bionic on (host) Linux to bootstrap our linker embedded into
-// a binary.
-//
-// Host bionic binaries do not have a PT_INTERP section, instead this gets
-// embedded as the entry point, and the linker is embedded as ELF sections in
-// each binary. There's a linker script that sets all of that up (generated by
-// extract_linker), and defines the extern symbols used in this file.
package {
default_team: "trendy_team_native_tools_libraries",
default_applicable_licenses: ["bionic_linker_license"],
@@ -25,6 +14,17 @@
],
}
+// ========================================================
+// linker_wrapper - Linux Bionic (on the host)
+// ========================================================
+
+// This is used for bionic on (host) Linux to bootstrap our linker embedded into
+// a binary.
+//
+// Host bionic binaries do not have a PT_INTERP section, instead this gets
+// embedded as the entry point, and the linker is embedded as ELF sections in
+// each binary. There's a linker script that sets all of that up (generated by
+// extract_linker), and defines the extern symbols used in this file.
cc_object {
name: "linker_wrapper",
host_supported: true,
@@ -327,8 +327,10 @@
},
},
+ static_executable: true,
+
// -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
- // static_executable. This dynamic linker is actually a shared object linked with static
+ // static_executable. The dynamic linker is actually a shared object linked with static
// libraries.
ldflags: [
"-shared",
@@ -344,51 +346,37 @@
"-Wl,--pack-dyn-relocs=relr",
],
- // we are going to link libc++_static manually because
- // when stl is not set to "none" build system adds libdl
- // to the list of static libraries which needs to be
- // avoided in the case of building loader.
+ // We link libc++_static manually because otherwise the build system will
+ // automatically add libdl to the list of static libraries.
stl: "none",
- // we don't want crtbegin.o (because we have begin.o), so unset it
- // just for this module
+ // We don't want crtbegin.o (because we have our own arch/*/begin.o),
+ // so unset it just for this module.
nocrt: true,
- static_executable: true,
-
// Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
// looking up symbols in the linker by mistake.
prefix_symbols: "__dl_",
sanitize: {
hwaddress: false,
- memtag_stack: false,
},
static_libs: [
"liblinker_main",
"liblinker_malloc",
- // Use a version of libc++ built without exceptions, because accessing EH globals uses
- // ELF TLS, which is not supported in the loader.
+ // We use a version of libc++ built without exceptions,
+ // because accessing EH globals uses ELF TLS,
+ // which is not supported in the loader.
"libc++_static_noexcept",
+
"libc_nomalloc",
"libc_dynamic_dispatch",
"libm",
"libunwind",
],
- // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
- // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
- // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
- // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
- // non-relative dynamic relocation in the linker binary, which complicates linker startup.
- //
- // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
- // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
- // linker stops linking against libgcc.a's arm32 unwinder.
- whole_static_libs: ["libc_unwind_static"],
-
system_shared_libs: [],
// Opt out of native_coverage when opting out of system_shared_libs
@@ -475,35 +463,6 @@
}
cc_library {
- // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
- // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
- // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
- // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
- // we use this property to make sure libc.so has its own copy of the code from
- // libgcc.a it uses.
- //
- // DO NOT REMOVE --exclude-libs!
-
- ldflags: [
- "-Wl,--exclude-libs=libgcc.a",
- "-Wl,--exclude-libs=libgcc_stripped.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-riscv64-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
- "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
- ],
-
- // for x86, exclude libgcc_eh.a for the same reasons as above
- arch: {
- x86: {
- ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
- },
- x86_64: {
- ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
- },
- },
-
srcs: ["ld_android.cpp"],
cflags: [
"-Wall",
diff --git a/tests/libs/testbinary_is_stack_mte.cpp b/tests/libs/testbinary_is_stack_mte.cpp
index 0cdc466..d8074d5 100644
--- a/tests/libs/testbinary_is_stack_mte.cpp
+++ b/tests/libs/testbinary_is_stack_mte.cpp
@@ -36,9 +36,7 @@
#if defined(__BIONIC__) && defined(__aarch64__)
extern "C" int main(int, char**) {
- void* mte_tls_ptr = mte_tls();
- *reinterpret_cast<uintptr_t*>(mte_tls_ptr) = 1;
- int ret = is_stack_mte_on() && mte_tls_ptr != nullptr ? 0 : 1;
+ int ret = is_stack_mte_on() ? 0 : 1;
printf("RAN\n");
return ret;
}
diff --git a/tests/libs/testbinary_is_stack_mte_after_dlopen.cpp b/tests/libs/testbinary_is_stack_mte_after_dlopen.cpp
index 35af8f4..937ac4c 100644
--- a/tests/libs/testbinary_is_stack_mte_after_dlopen.cpp
+++ b/tests/libs/testbinary_is_stack_mte_after_dlopen.cpp
@@ -96,7 +96,6 @@
State state = kInit;
bool is_early_thread_mte_on = false;
- void* early_thread_mte_tls = nullptr;
std::thread early_th([&] {
{
std::lock_guard lk(m);
@@ -108,8 +107,6 @@
cv.wait(lk, [&] { return state == kStackRemapped; });
}
is_early_thread_mte_on = is_stack_mte_on();
- early_thread_mte_tls = mte_tls();
- *reinterpret_cast<uintptr_t*>(early_thread_mte_tls) = 1;
});
{
std::unique_lock lk(m);
@@ -123,7 +120,6 @@
cv.notify_one();
CHECK(handle != nullptr);
CHECK(is_stack_mte_on());
- CHECK(mte_tls() != nullptr);
bool new_stack_page_mte_on = false;
uintptr_t low;
@@ -133,18 +129,11 @@
CHECK(new_stack_page_mte_on);
bool is_late_thread_mte_on = false;
- void* late_thread_mte_tls = nullptr;
- std::thread late_th([&] {
- is_late_thread_mte_on = is_stack_mte_on();
- late_thread_mte_tls = mte_tls();
- *reinterpret_cast<uintptr_t*>(late_thread_mte_tls) = 1;
- });
+ std::thread late_th([&] { is_late_thread_mte_on = is_stack_mte_on(); });
late_th.join();
early_th.join();
CHECK(is_late_thread_mte_on);
CHECK(is_early_thread_mte_on);
- CHECK(late_thread_mte_tls != nullptr);
- CHECK(early_thread_mte_tls != nullptr);
printf("RAN\n");
return 0;
}
diff --git a/tests/math_test.cpp b/tests/math_test.cpp
index 493f3af..273ef97 100644
--- a/tests/math_test.cpp
+++ b/tests/math_test.cpp
@@ -17,36 +17,37 @@
#define _GNU_SOURCE 1
#include <math.h>
-// This include (and the associated definition of __test_capture_signbit)
-// must be placed before any files that include <cmath> (gtest.h in this case).
+// <math.h> is required to define type-generic macros: fpclassify, signbit,
+// isfinite, isinf, isnan, isnormal, isgreater, isgreaterequal, isless,
+// islessequal, islessgreater, and isunordered.
//
-// <math.h> is required to define generic macros signbit, isfinite and
-// several other such functions.
+// <cmath> is required to #undef these macros and make equivalent sets of
+// _overloaded_ functions available in namespace std. So the isnan() macro,
+// for example, is replaced by std::isnan(float), std::isnan(double),
+// and std::isnan(long double).
//
-// <cmath> is required to undef declarations of these macros in the global
-// namespace and make equivalent functions available in namespace std. Our
-// stlport implementation does this only for signbit, isfinite, isinf and
-// isnan.
-//
-// NOTE: We don't write our test using std::signbit because we want to be
-// sure that we're testing the bionic version of signbit. The C++ libraries
-// are free to reimplement signbit or delegate to compiler builtins if they
-// please.
+// We're trying to test the bionic macros rather than whatever libc++'s
+// implementation happens to be, so we #include <math.h> and "capture" the
+// macros in our own _template_ functions in the global namespace before
+// we #include any files that include <cmath>, such as <gtest.h>.
-namespace {
-template<typename T> inline int test_capture_signbit(const T in) {
- return signbit(in);
-}
-template<typename T> inline int test_capture_isfinite(const T in) {
- return isfinite(in);
-}
-template<typename T> inline int test_capture_isnan(const T in) {
- return isnan(in);
-}
-template<typename T> inline int test_capture_isinf(const T in) {
- return isinf(in);
-}
-}
+#define capture_generic_macro(capture_function_name, generic_macro_name) \
+ template <typename T> inline int capture_function_name(const T in) { \
+ return generic_macro_name(in); \
+ }
+
+capture_generic_macro(test_capture_fpclassify, fpclassify)
+capture_generic_macro(test_capture_signbit, signbit)
+capture_generic_macro(test_capture_isfinite, isfinite)
+capture_generic_macro(test_capture_isinf, isinf)
+capture_generic_macro(test_capture_isnan, isnan)
+capture_generic_macro(test_capture_isnormal, isnormal)
+capture_generic_macro(test_capture_isgreater, isgreater)
+capture_generic_macro(test_capture_isgreaterequal, isgreaterequal)
+capture_generic_macro(test_capture_isless, isless)
+capture_generic_macro(test_capture_islessequal, islessequal)
+capture_generic_macro(test_capture_islessgreater, islessgreater)
+capture_generic_macro(test_capture_isunordered, isunordered)
#include "math_data_test.h"
@@ -60,6 +61,22 @@
#include <android-base/scopeguard.h>
+// Now we've included all the headers we need, we can redefine the generic
+// function-like macros to point to the bionic <math.h> versions we captured
+// earlier.
+#define fpclassify test_capture_fpclassify
+#define signbit test_capture_signbit
+#define isfinite test_capture_isfinite
+#define isinf test_capture_isinf
+#define isnan test_capture_isnan
+#define isnormal test_capture_isnormal
+#define isgreater test_capture_isgreater
+#define isgreaterequal test_capture_isgreaterequal
+#define isless test_capture_isless
+#define islessequal test_capture_islessequal
+#define islessgreater test_capture_islessgreater
+#define isunordered test_capture_isunordered
+
static float float_subnormal() {
union {
float f;
@@ -124,36 +141,36 @@
}
TEST(math_h, isfinite) {
- ASSERT_TRUE(test_capture_isfinite(123.0f));
- ASSERT_TRUE(test_capture_isfinite(123.0));
- ASSERT_TRUE(test_capture_isfinite(123.0L));
- ASSERT_FALSE(test_capture_isfinite(HUGE_VALF));
- ASSERT_FALSE(test_capture_isfinite(-HUGE_VALF));
- ASSERT_FALSE(test_capture_isfinite(HUGE_VAL));
- ASSERT_FALSE(test_capture_isfinite(-HUGE_VAL));
- ASSERT_FALSE(test_capture_isfinite(HUGE_VALL));
- ASSERT_FALSE(test_capture_isfinite(-HUGE_VALL));
+ ASSERT_TRUE(isfinite(123.0f));
+ ASSERT_TRUE(isfinite(123.0));
+ ASSERT_TRUE(isfinite(123.0L));
+ ASSERT_FALSE(isfinite(HUGE_VALF));
+ ASSERT_FALSE(isfinite(-HUGE_VALF));
+ ASSERT_FALSE(isfinite(HUGE_VAL));
+ ASSERT_FALSE(isfinite(-HUGE_VAL));
+ ASSERT_FALSE(isfinite(HUGE_VALL));
+ ASSERT_FALSE(isfinite(-HUGE_VALL));
}
TEST(math_h, isinf) {
- ASSERT_FALSE(test_capture_isinf(123.0f));
- ASSERT_FALSE(test_capture_isinf(123.0));
- ASSERT_FALSE(test_capture_isinf(123.0L));
- ASSERT_TRUE(test_capture_isinf(HUGE_VALF));
- ASSERT_TRUE(test_capture_isinf(-HUGE_VALF));
- ASSERT_TRUE(test_capture_isinf(HUGE_VAL));
- ASSERT_TRUE(test_capture_isinf(-HUGE_VAL));
- ASSERT_TRUE(test_capture_isinf(HUGE_VALL));
- ASSERT_TRUE(test_capture_isinf(-HUGE_VALL));
+ ASSERT_FALSE(isinf(123.0f));
+ ASSERT_FALSE(isinf(123.0));
+ ASSERT_FALSE(isinf(123.0L));
+ ASSERT_TRUE(isinf(HUGE_VALF));
+ ASSERT_TRUE(isinf(-HUGE_VALF));
+ ASSERT_TRUE(isinf(HUGE_VAL));
+ ASSERT_TRUE(isinf(-HUGE_VAL));
+ ASSERT_TRUE(isinf(HUGE_VALL));
+ ASSERT_TRUE(isinf(-HUGE_VALL));
}
TEST(math_h, isnan) {
- ASSERT_FALSE(test_capture_isnan(123.0f));
- ASSERT_FALSE(test_capture_isnan(123.0));
- ASSERT_FALSE(test_capture_isnan(123.0L));
- ASSERT_TRUE(test_capture_isnan(nanf("")));
- ASSERT_TRUE(test_capture_isnan(nan("")));
- ASSERT_TRUE(test_capture_isnan(nanl("")));
+ ASSERT_FALSE(isnan(123.0f));
+ ASSERT_FALSE(isnan(123.0));
+ ASSERT_FALSE(isnan(123.0L));
+ ASSERT_TRUE(isnan(nanf("")));
+ ASSERT_TRUE(isnan(nan("")));
+ ASSERT_TRUE(isnan(nanl("")));
}
TEST(math_h, isnormal) {
@@ -167,17 +184,17 @@
// TODO: isgreater, isgreaterequals, isless, islessequal, islessgreater, isunordered
TEST(math_h, signbit) {
- ASSERT_EQ(0, test_capture_signbit(0.0f));
- ASSERT_EQ(0, test_capture_signbit(0.0));
- ASSERT_EQ(0, test_capture_signbit(0.0L));
+ ASSERT_EQ(0, signbit(0.0f));
+ ASSERT_EQ(0, signbit(0.0));
+ ASSERT_EQ(0, signbit(0.0L));
- ASSERT_EQ(0, test_capture_signbit(1.0f));
- ASSERT_EQ(0, test_capture_signbit(1.0));
- ASSERT_EQ(0, test_capture_signbit(1.0L));
+ ASSERT_EQ(0, signbit(1.0f));
+ ASSERT_EQ(0, signbit(1.0));
+ ASSERT_EQ(0, signbit(1.0L));
- ASSERT_NE(0, test_capture_signbit(-1.0f));
- ASSERT_NE(0, test_capture_signbit(-1.0));
- ASSERT_NE(0, test_capture_signbit(-1.0L));
+ ASSERT_NE(0, signbit(-1.0f));
+ ASSERT_NE(0, signbit(-1.0));
+ ASSERT_NE(0, signbit(-1.0L));
}
// Historical BSD cruft that isn't exposed in <math.h> any more.
@@ -309,9 +326,7 @@
// Historical BSD cruft that isn't exposed in <math.h> any more.
extern "C" int __isinf(double);
extern "C" int __isinff(float);
-extern "C" int isinff(float);
extern "C" int __isinfl(long double);
-extern "C" int isinfl(long double);
TEST(math_h, __isinf) {
#if defined(ANDROID_HOST_MUSL)
@@ -367,9 +382,7 @@
// Historical BSD cruft that isn't exposed in <math.h> any more.
extern "C" int __isnan(double);
extern "C" int __isnanf(float);
-extern "C" int isnanf(float);
extern "C" int __isnanl(long double);
-extern "C" int isnanl(long double);
TEST(math_h, __isnan) {
#if defined(ANDROID_HOST_MUSL)
diff --git a/tests/mte_utils.h b/tests/mte_utils.h
index 020faec..6e8385c 100644
--- a/tests/mte_utils.h
+++ b/tests/mte_utils.h
@@ -40,10 +40,4 @@
return p == p_cpy;
}
-static void* mte_tls() {
- void** dst;
- __asm__("mrs %0, TPIDR_EL0" : "=r"(dst) :);
- return dst[-3];
-}
-
#endif
diff --git a/tests/struct_layout_test.cpp b/tests/struct_layout_test.cpp
index 1f04344..0123ed9 100644
--- a/tests/struct_layout_test.cpp
+++ b/tests/struct_layout_test.cpp
@@ -30,7 +30,7 @@
#define CHECK_OFFSET(name, field, offset) \
check_offset(#name, #field, offsetof(name, field), offset);
#ifdef __LP64__
- CHECK_SIZE(pthread_internal_t, 816);
+ CHECK_SIZE(pthread_internal_t, 776);
CHECK_OFFSET(pthread_internal_t, next, 0);
CHECK_OFFSET(pthread_internal_t, prev, 8);
CHECK_OFFSET(pthread_internal_t, tid, 16);
@@ -55,8 +55,6 @@
CHECK_OFFSET(pthread_internal_t, dlerror_buffer, 248);
CHECK_OFFSET(pthread_internal_t, bionic_tls, 760);
CHECK_OFFSET(pthread_internal_t, errno_value, 768);
- CHECK_OFFSET(pthread_internal_t, bionic_tcb, 776);
- CHECK_OFFSET(pthread_internal_t, stack_mte_ringbuffer_vma_name_buffer, 784);
CHECK_SIZE(bionic_tls, 12200);
CHECK_OFFSET(bionic_tls, key_data, 0);
CHECK_OFFSET(bionic_tls, locale, 2080);
@@ -74,7 +72,7 @@
CHECK_OFFSET(bionic_tls, bionic_systrace_disabled, 12193);
CHECK_OFFSET(bionic_tls, padding, 12194);
#else
- CHECK_SIZE(pthread_internal_t, 704);
+ CHECK_SIZE(pthread_internal_t, 668);
CHECK_OFFSET(pthread_internal_t, next, 0);
CHECK_OFFSET(pthread_internal_t, prev, 4);
CHECK_OFFSET(pthread_internal_t, tid, 8);
@@ -99,8 +97,6 @@
CHECK_OFFSET(pthread_internal_t, dlerror_buffer, 148);
CHECK_OFFSET(pthread_internal_t, bionic_tls, 660);
CHECK_OFFSET(pthread_internal_t, errno_value, 664);
- CHECK_OFFSET(pthread_internal_t, bionic_tcb, 668);
- CHECK_OFFSET(pthread_internal_t, stack_mte_ringbuffer_vma_name_buffer, 672);
CHECK_SIZE(bionic_tls, 11080);
CHECK_OFFSET(bionic_tls, key_data, 0);
CHECK_OFFSET(bionic_tls, locale, 1040);