Merge "Add another link to the benchmark docs." into main
diff --git a/libc/Android.bp b/libc/Android.bp
index 048cd73..7e5e972 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -791,6 +791,7 @@
"bionic/abort.cpp",
"bionic/accept.cpp",
"bionic/access.cpp",
+ "bionic/android_crash_detail.cpp",
"bionic/android_set_abort_message.cpp",
"bionic/android_unsafe_frame_pointer_chase.cpp",
"bionic/arpa_inet.cpp",
@@ -857,7 +858,6 @@
"bionic/iconv.cpp",
"bionic/icu_wrappers.cpp",
"bionic/ifaddrs.cpp",
- "bionic/initgroups.c",
"bionic/inotify_init.cpp",
"bionic/ioctl.cpp",
"bionic/isatty.cpp",
diff --git a/libc/bionic/android_crash_detail.cpp b/libc/bionic/android_crash_detail.cpp
new file mode 100644
index 0000000..30ce505
--- /dev/null
+++ b/libc/bionic/android_crash_detail.cpp
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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 THE COPYRIGHT HOLDERS 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 THE
+ * COPYRIGHT OWNER 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.
+ */
+
+#include <android/crash_detail.h>
+
+#include <async_safe/log.h>
+#include <bionic/crash_detail_internal.h>
+
+#include <bits/stdatomic.h>
+#include <pthread.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+
+#include "private/ScopedPthreadMutexLocker.h"
+#include "private/bionic_defs.h"
+#include "private/bionic_globals.h"
+
+static _Atomic(crash_detail_t*) free_head = nullptr;
+
+__BIONIC_WEAK_FOR_NATIVE_BRIDGE
+crash_detail_t* android_crash_detail_register(const void* name, size_t name_size, const void* data,
+ size_t data_size) {
+ auto populate_crash_detail = [&](crash_detail_t* result) {
+ result->name = reinterpret_cast<const char*>(name);
+ result->name_size = name_size;
+ result->data = reinterpret_cast<const char*>(data);
+ result->data_size = data_size;
+ };
+ // This is a atomic fast-path for RAII use-cases where the app keeps creating and deleting
+ // crash details for short periods of time to capture detailed scopes.
+ if (crash_detail_t* head = atomic_load(&free_head)) {
+ while (head != nullptr && !atomic_compare_exchange_strong(&free_head, &head, head->prev_free)) {
+ // intentionally left blank.
+ }
+ if (head) {
+ head->prev_free = nullptr;
+ populate_crash_detail(head);
+ return head;
+ }
+ }
+ ScopedPthreadMutexLocker locker(&__libc_shared_globals()->crash_detail_page_lock);
+ struct crash_detail_page_t* prev = nullptr;
+ struct crash_detail_page_t* page = __libc_shared_globals()->crash_detail_page;
+ if (page != nullptr && page->used == kNumCrashDetails) {
+ prev = page;
+ page = nullptr;
+ }
+ if (page == nullptr) {
+ size_t size = sizeof(crash_detail_page_t);
+ void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
+ if (map == MAP_FAILED) {
+ async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to allocate crash_detail_page: %m");
+ return nullptr;
+ }
+ prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, size, "crash details");
+ page = reinterpret_cast<struct crash_detail_page_t*>(map);
+ page->prev = prev;
+ __libc_shared_globals()->crash_detail_page = page;
+ }
+ crash_detail_t* result = &page->crash_details[page->used];
+ populate_crash_detail(result);
+ page->used++;
+ return result;
+}
+
+__BIONIC_WEAK_FOR_NATIVE_BRIDGE
+void android_crash_detail_unregister(crash_detail_t* crash_detail) {
+ if (crash_detail) {
+ if (crash_detail->prev_free) {
+ // removing already removed would mess up the free-list by creating a circle.
+ return;
+ }
+ crash_detail->data = nullptr;
+ crash_detail->name = nullptr;
+ crash_detail_t* prev = atomic_load(&free_head);
+ do {
+ crash_detail->prev_free = prev;
+ } while (!atomic_compare_exchange_strong(&free_head, &prev, crash_detail));
+ }
+}
+
+__BIONIC_WEAK_FOR_NATIVE_BRIDGE
+void android_crash_detail_replace_data(crash_detail_t* crash_detail, const void* data,
+ size_t data_size) {
+ crash_detail->data = reinterpret_cast<const char*>(data);
+ crash_detail->data_size = data_size;
+}
+
+__BIONIC_WEAK_FOR_NATIVE_BRIDGE
+void android_crash_detail_replace_name(crash_detail_t* crash_detail, const void* name,
+ size_t name_size) {
+ crash_detail->name = reinterpret_cast<const char*>(name);
+ crash_detail->name_size = name_size;
+}
diff --git a/libc/bionic/android_set_abort_message.cpp b/libc/bionic/android_set_abort_message.cpp
index 53d7576..05adf3e 100644
--- a/libc/bionic/android_set_abort_message.cpp
+++ b/libc/bionic/android_set_abort_message.cpp
@@ -29,7 +29,6 @@
#include <android/set_abort_message.h>
#include <async_safe/log.h>
-#include <bionic/set_abort_message_internal.h>
#include <bits/stdatomic.h>
#include <pthread.h>
@@ -60,8 +59,6 @@
"The in-memory layout of magic_abort_msg_t is not consistent with what automated "
"tools expect.");
-static _Atomic(crash_detail_t*) free_head = nullptr;
-
[[clang::optnone]]
static void fill_abort_message_magic(magic_abort_msg_t* new_magic_abort_message) {
// 128-bit magic for the abort message. Chosen by fair dice roll.
@@ -103,65 +100,3 @@
strcpy(new_magic_abort_message->msg.msg, msg);
__libc_shared_globals()->abort_msg = &new_magic_abort_message->msg;
}
-
-__BIONIC_WEAK_FOR_NATIVE_BRIDGE
-crash_detail_t* android_register_crash_detail(const void* name, size_t name_size, const void* data,
- size_t data_size) {
- auto populate_crash_detail = [&](crash_detail_t* result) {
- result->name = reinterpret_cast<const char*>(name);
- result->name_size = name_size;
- result->data = reinterpret_cast<const char*>(data);
- result->data_size = data_size;
- };
- // This is a atomic fast-path for RAII use-cases where the app keeps creating and deleting
- // crash details for short periods of time to capture detailed scopes.
- if (crash_detail_t* head = atomic_load(&free_head)) {
- while (head != nullptr && !atomic_compare_exchange_strong(&free_head, &head, head->prev_free)) {
- // intentionally left blank.
- }
- if (head) {
- head->prev_free = nullptr;
- populate_crash_detail(head);
- return head;
- }
- }
- ScopedPthreadMutexLocker locker(&__libc_shared_globals()->crash_detail_page_lock);
- struct crash_detail_page_t* prev = nullptr;
- struct crash_detail_page_t* page = __libc_shared_globals()->crash_detail_page;
- if (page != nullptr && page->used == kNumCrashDetails) {
- prev = page;
- page = nullptr;
- }
- if (page == nullptr) {
- size_t size = sizeof(crash_detail_page_t);
- void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
- if (map == MAP_FAILED) {
- async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to allocate crash_detail_page: %m");
- return nullptr;
- }
- prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, size, "crash details");
- page = reinterpret_cast<struct crash_detail_page_t*>(map);
- page->prev = prev;
- __libc_shared_globals()->crash_detail_page = page;
- }
- crash_detail_t* result = &page->crash_details[page->used];
- populate_crash_detail(result);
- page->used++;
- return result;
-}
-
-__BIONIC_WEAK_FOR_NATIVE_BRIDGE
-void android_unregister_crash_detail(crash_detail_t* crash_detail) {
- if (crash_detail) {
- if (crash_detail->prev_free) {
- // removing already removed would mess up the free-list by creating a circle.
- return;
- }
- crash_detail->data = nullptr;
- crash_detail->name = nullptr;
- crash_detail_t* prev = atomic_load(&free_head);
- do {
- crash_detail->prev_free = prev;
- } while (!atomic_compare_exchange_strong(&free_head, &prev, crash_detail));
- }
-}
diff --git a/libc/bionic/grp_pwd.cpp b/libc/bionic/grp_pwd.cpp
index 600693c..82ee7ba 100644
--- a/libc/bionic/grp_pwd.cpp
+++ b/libc/bionic/grp_pwd.cpp
@@ -609,6 +609,8 @@
}
// All users are in just one group, the one passed in.
+// In practice, id(1) will show you in a lot more groups, because adbd
+// adds you to a lot of supplementary groups when dropping privileges.
int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
if (*ngroups < 1) {
*ngroups = 1;
@@ -618,6 +620,12 @@
return (*ngroups = 1);
}
+// See getgrouplist() to understand why we don't call it.
+int initgroups(const char* /*user*/, gid_t group) {
+ gid_t groups[] = {group};
+ return setgroups(1, groups);
+}
+
char* getlogin() { // NOLINT: implementing bad function.
passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
return pw ? pw->pw_name : nullptr;
diff --git a/libc/bionic/initgroups.c b/libc/bionic/initgroups.c
deleted file mode 100644
index dea6d96..0000000
--- a/libc/bionic/initgroups.c
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * 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 THE COPYRIGHT HOLDERS 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 THE
- * COPYRIGHT OWNER 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.
- */
-#include <grp.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-#define INIT_GROUPS 2
-
-int
-initgroups (const char *user, gid_t group)
-{
- gid_t groups0[ INIT_GROUPS ];
- gid_t* groups = groups0;
- int ret = -1;
- int numgroups = INIT_GROUPS;
-
- if (getgrouplist(user, group, groups, &numgroups) < 0) {
- groups = malloc(numgroups*sizeof(groups[0]));
- if (groups == NULL)
- return -1;
- if (getgrouplist(user,group,groups,&numgroups) < 0) {
- goto EXIT;
- }
- }
-
- ret = setgroups(numgroups, groups);
-
-EXIT:
- if (groups != groups0)
- free(groups);
-
- return ret;
-}
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index 295484b..1180a51 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -61,6 +61,7 @@
};
void memtag_stack_dlopen_callback() {
+ async_safe_format_log(ANDROID_LOG_INFO, "libc", "remapping stacks as PROT_MTE");
__pthread_internal_remap_stack_with_mte();
}
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index 00faa5b..f091ff8 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -305,6 +305,14 @@
bool memtag_stack = false;
HeapTaggingLevel level =
__get_tagging_level(memtag_dynamic_entries, phdr_start, phdr_ct, load_bias, &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;
+ }
char* env = getenv("BIONIC_MEMTAG_UPGRADE_SECS");
static const char kAppProcessName[] = "app_process64";
const char* progname = __libc_shared_globals()->init_progname;
@@ -373,6 +381,8 @@
}
// We did not enable MTE, so we do not need to arm the upgrade timer.
__libc_shared_globals()->heap_tagging_upgrade_timer_sec = 0;
+ // We also didn't enable memtag_stack.
+ __libc_shared_globals()->initial_memtag_stack = false;
}
#else // __aarch64__
void __libc_init_mte(const memtag_dynamic_entries_t*, const void*, size_t, uintptr_t, void*) {}
diff --git a/libc/include/android/crash_detail.h b/libc/include/android/crash_detail.h
new file mode 100644
index 0000000..1fd082f
--- /dev/null
+++ b/libc/include/android/crash_detail.h
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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 THE COPYRIGHT HOLDERS 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 THE
+ * COPYRIGHT OWNER 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.
+ */
+
+#pragma once
+
+/**
+ * @file android/crash_detail.h
+ * @brief Attach extra information to android crashes.
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+typedef struct crash_detail_t crash_detail_t;
+
+/**
+ * Register a new buffer to get logged into tombstones for crashes.
+ *
+ * It will be added to both the tombstone proto in the crash_detail field, and
+ * in the tombstone text format.
+ *
+ * Tombstone proto definition:
+ * https://cs.android.com/android/platform/superproject/main/+/main:system/core/debuggerd/proto/tombstone.proto
+ *
+ * The lifetime of name and data has to be valid until the program crashes, or until
+ * android_crash_detail_unregister is called.
+ *
+ * Example usage:
+ * const char* stageName = "garbage_collection";
+ * crash_detail_t* cd = android_crash_detail_register("stage", stageName, strlen(stageName));
+ * do_garbage_collection();
+ * android_crash_detail_unregister(cd);
+ *
+ * If this example crashes in do_garbage_collection, a line will show up in the textual representation of the tombstone:
+ * Extra crash detail: stage: 'garbage_collection'
+ *
+ * Introduced in API 35.
+ *
+ * \param name identifying name for this extra data.
+ * this should generally be a human-readable debug string, but we are treating
+ * it as arbitrary bytes because it could be corrupted by the crash.
+ * \param name_size number of bytes of the buffer pointed to by name
+ * \param data a buffer containing the extra detail bytes, if null the crash detail
+ * is disabled until android_crash_detail_replace_data replaces it with
+ * a non-null pointer.
+ * \param data_size number of bytes of the buffer pointed to by data
+ *
+ * \return a handle to the extra crash detail.
+ */
+crash_detail_t* _Nullable android_crash_detail_register(
+ const void* _Nonnull name, size_t name_size, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
+
+/**
+ * Unregister crash detail from being logged into tombstones.
+ *
+ * After this function returns, the lifetime of the objects crash_detail was
+ * constructed from no longer needs to be valid.
+ *
+ * Introduced in API 35.
+ *
+ * \param crash_detail the crash_detail that should be removed.
+ */
+void android_crash_detail_unregister(crash_detail_t* _Nonnull crash_detail) __INTRODUCED_IN(35);
+
+/**
+ * Replace data of crash detail.
+ *
+ * This is more efficient than using android_crash_detail_unregister followed by
+ * android_crash_detail_register. If you very frequently need to swap out the data,
+ * you can hold onto the crash_detail.
+ *
+ * Introduced in API 35.
+ *
+ * \param data the new buffer containing the extra detail bytes, or null to disable until
+ * android_crash_detail_replace_data is called again with non-null data.
+ * \param data_size the number of bytes of the buffer pointed to by data.
+ */
+void android_crash_detail_replace_data(crash_detail_t* _Nonnull crash_detail, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
+
+/**
+ * Replace name of crash detail.
+ *
+ * This is more efficient than using android_crash_detail_unregister followed by
+ * android_crash_detail_register. If you very frequently need to swap out the name,
+ * you can hold onto the crash_detail.
+ *
+ * Introduced in API 35.
+ *
+ * \param name identifying name for this extra data.
+ * \param name_size number of bytes of the buffer pointed to by name
+ */
+void android_crash_detail_replace_name(crash_detail_t* _Nonnull crash_detail, const void* _Nonnull name, size_t name_size) __INTRODUCED_IN(35);
+
+__END_DECLS
diff --git a/libc/include/android/set_abort_message.h b/libc/include/android/set_abort_message.h
index e92c6ec..2525242 100644
--- a/libc/include/android/set_abort_message.h
+++ b/libc/include/android/set_abort_message.h
@@ -30,7 +30,7 @@
/**
* @file android/set_abort_message.h
- * @brief Attach extra information to android crashes.
+ * @brief The android_set_abort_message() function.
*/
#include <stddef.h>
@@ -51,51 +51,4 @@
*/
void android_set_abort_message(const char* _Nullable __msg);
-/**
- * Register a new buffer to get logged into tombstones for crashes.
- *
- * It will be added to both the tombstone proto in the crash_detail field, and
- * in the tombstone text format.
- *
- * Tombstone proto definition:
- * https://cs.android.com/android/platform/superproject/main/+/main:system/core/debuggerd/proto/tombstone.proto
- *
- * The lifetime of name and data has to be valid until the program crashes, or until
- * android_unregister_crash_detail is called.
- *
- * Example usage:
- * const char* stageName = "garbage_collection";
- * crash_detail_t* cd = android_register_crash_detail("stage", stageName, strlen(stageName));
- * do_garbage_collection();
- * android_unregister_crash_detail(cd);
- *
- * If this example crashes in do_garbage_collection, a line will show up in the textual representation of the tombstone:
- * Extra crash detail: stage: 'garbage_collection'
- *
- * Introduced in API 35.
- *
- * \param name identifying name for this extra data.
- * this should generally be a human-readable debug string, but we are treating
- * it as arbitrary bytes because it could be corrupted by the crash.
- * \param name_size number of bytes of the buffer pointed to by name
- * \param data a buffer containing the extra detail bytes
- * \param data_size number of bytes of the buffer pointed to by data
- *
- * \return a handle to the extra crash detail for use with android_unregister_crash_detail.
- */
-crash_detail_t* _Nullable android_register_crash_detail(
- const void* _Nonnull name, size_t name_size, const void* _Nonnull data, size_t data_size) __INTRODUCED_IN(35);
-
-/**
- * Unregister crash detail from being logged into tombstones.
- *
- * After this function returns, the lifetime of the objects crash_detail was
- * constructed from no longer needs to be valid.
- *
- * Introduced in API 35.
- *
- * \param crash_detail the crash_detail that should be removed.
- */
-void android_unregister_crash_detail(crash_detail_t* _Nonnull crash_detail) __INTRODUCED_IN(35);
-
__END_DECLS
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index ecdb25c..5e9763b 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1586,8 +1586,10 @@
LIBC_V { # introduced=VanillaIceCream
global:
- android_register_crash_detail;
- android_unregister_crash_detail;
+ android_crash_detail_register;
+ android_crash_detail_unregister;
+ android_crash_detail_replace_name;
+ android_crash_detail_replace_data;
epoll_pwait2;
epoll_pwait2_64;
localtime_rz;
diff --git a/libc/platform/bionic/set_abort_message_internal.h b/libc/platform/bionic/crash_detail_internal.h
similarity index 97%
rename from libc/platform/bionic/set_abort_message_internal.h
rename to libc/platform/bionic/crash_detail_internal.h
index 4dff3ac..d8508a5 100644
--- a/libc/platform/bionic/set_abort_message_internal.h
+++ b/libc/platform/bionic/crash_detail_internal.h
@@ -28,7 +28,7 @@
#pragma once
-#include <android/set_abort_message.h>
+#include <android/crash_detail.h>
#include <stddef.h>
#include <sys/cdefs.h>
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 724f821..b0caedd 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1695,13 +1695,31 @@
}
// Step 3: pre-link all DT_NEEDED libraries in breadth first order.
+ bool any_memtag_stack = false;
for (auto&& task : load_tasks) {
soinfo* si = task->get_soinfo();
if (!si->is_linked() && !si->prelink_image()) {
return false;
}
+ // si->memtag_stack() needs to be called after si->prelink_image() which populates
+ // the dynamic section.
+ if (si->has_min_version(7) && si->memtag_stack()) {
+ any_memtag_stack = true;
+ LD_LOG(kLogDlopen,
+ "... load_library requesting stack MTE for: realpath=\"%s\", soname=\"%s\"",
+ si->get_realpath(), si->get_soname());
+ }
register_soinfo_tls(si);
}
+ if (any_memtag_stack) {
+ if (auto* cb = __libc_shared_globals()->memtag_stack_dlopen_callback) {
+ cb();
+ } else {
+ // find_library is used by the initial linking step, so we communicate that we
+ // want memtag_stack enabled to __libc_init_mte.
+ __libc_shared_globals()->initial_memtag_stack = true;
+ }
+ }
// Step 4: Construct the global group. DF_1_GLOBAL bit is force set for LD_PRELOADed libs because
// they must be added to the global group. Note: The DF_1_GLOBAL bit for a library is normally set
@@ -2213,14 +2231,6 @@
loading_trace.End();
if (si != nullptr) {
- if (si->has_min_version(7) && si->memtag_stack()) {
- LD_LOG(kLogDlopen, "... dlopen enabling MTE for: realpath=\"%s\", soname=\"%s\"",
- si->get_realpath(), si->get_soname());
- if (auto* cb = __libc_shared_globals()->memtag_stack_dlopen_callback) {
- cb();
- }
- }
-
void* handle = si->to_handle();
LD_LOG(kLogDlopen,
"... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 5f5eba4..d6592af 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -404,9 +404,6 @@
strerror(errno));
}
}
-
- __libc_init_mte(somain->memtag_dynamic_entries(), somain->phdr, somain->phnum, somain->load_bias,
- args.argv);
#endif
// Register the main executable and the linker upfront to have
@@ -496,6 +493,12 @@
}
si->increment_ref_count();
}
+#if defined(__aarch64__)
+ // This has to happen after the find_libraries, which will have collected any possible
+ // libraries that request memtag_stack in the dynamic section.
+ __libc_init_mte(somain->memtag_dynamic_entries(), somain->phdr, somain->phnum, somain->load_bias,
+ args.argv);
+#endif
linker_finalize_static_tls();
__libc_init_main_thread_final();
diff --git a/tests/Android.bp b/tests/Android.bp
index 0ba91ea..78c2c10 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -1127,7 +1127,12 @@
shared_libs: [
"libbase",
],
- data_libs: ["libtest_simple_memtag_stack"],
+ data_libs: ["libtest_simple_memtag_stack", "libtest_depends_on_simple_memtag_stack"],
+ data_bins: [
+ "testbinary_depends_on_simple_memtag_stack",
+ "testbinary_depends_on_depends_on_simple_memtag_stack",
+ "testbinary_is_stack_mte_after_dlopen"
+ ],
header_libs: ["bionic_libc_platform_headers"],
test_suites: ["device-tests"],
}
diff --git a/tests/grp_pwd_test.cpp b/tests/grp_pwd_test.cpp
index d3acf03..16b8d5a 100644
--- a/tests/grp_pwd_test.cpp
+++ b/tests/grp_pwd_test.cpp
@@ -851,6 +851,11 @@
#endif
}
+TEST(grp, initgroups) {
+ if (getuid() != 0) GTEST_SKIP() << "test requires root";
+ ASSERT_EQ(0, initgroups("root", 0));
+}
+
#if defined(__BIONIC__)
static void TestAidNamePrefix(const std::string& file_path) {
std::string file_contents;
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index 68efbd9..06ee132 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -234,7 +234,7 @@
}
// -----------------------------------------------------------------------------
-// Library used by memtag_stack_dlopen_test tests
+// Libraries and binaries used by memtag_stack_dlopen_test tests
// -----------------------------------------------------------------------------
cc_test_library {
name: "libtest_simple_memtag_stack",
@@ -244,6 +244,50 @@
srcs: ["dlopen_testlib_simple.cpp"],
}
+cc_test_library {
+ name: "libtest_depends_on_simple_memtag_stack",
+ sanitize: {
+ memtag_stack: false,
+ },
+ shared_libs: [
+ "libtest_simple_memtag_stack",
+ ],
+ srcs: ["dlopen_testlib_depends_on_simple.cpp"],
+}
+
+cc_binary {
+ name: "testbinary_is_stack_mte_after_dlopen",
+ sanitize: {
+ memtag_stack: false,
+ memtag_heap: true,
+ },
+ srcs: ["testbinary_is_stack_mte_after_dlopen.cpp"],
+}
+
+cc_binary {
+ name: "testbinary_depends_on_simple_memtag_stack",
+ sanitize: {
+ memtag_stack: false,
+ memtag_heap: true,
+ },
+ shared_libs: [
+ "libtest_simple_memtag_stack",
+ ],
+ srcs: ["testbinary_is_stack_mte.cpp"],
+}
+
+cc_binary {
+ name: "testbinary_depends_on_depends_on_simple_memtag_stack",
+ sanitize: {
+ memtag_stack: false,
+ memtag_heap: true,
+ },
+ shared_libs: [
+ "libtest_depends_on_simple_memtag_stack",
+ ],
+ srcs: ["testbinary_is_stack_mte.cpp"],
+}
+
// -----------------------------------------------------------------------------
// Libraries used by hwasan_test
// -----------------------------------------------------------------------------
diff --git a/tests/libs/libs_utils.h b/tests/libs/CHECK.h
similarity index 71%
rename from tests/libs/libs_utils.h
rename to tests/libs/CHECK.h
index 7dae241..2575d5b 100644
--- a/tests/libs/libs_utils.h
+++ b/tests/libs/CHECK.h
@@ -14,14 +14,13 @@
* limitations under the License.
*/
-#ifndef LIBS_UTILS_H
-#define LIBS_UTILS_H
+#pragma once
+
+// Tests proper can use libbase, but libraries for testing dlopen()
+// should probably avoid dependencies other than ones we're specifically
+// trying to test.
#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
#define CHECK(e) \
- ((e) ? static_cast<void>(0) : __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #e))
-
-#endif // LIBS_UTILS_H
+ ((e) ? static_cast<void>(0) : __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #e))
diff --git a/tests/libs/cfi_test_helper.cpp b/tests/libs/cfi_test_helper.cpp
index c1a7b6d..71cdc89 100644
--- a/tests/libs/cfi_test_helper.cpp
+++ b/tests/libs/cfi_test_helper.cpp
@@ -14,11 +14,10 @@
* limitations under the License.
*/
-#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
-#include "libs_utils.h"
+#include "CHECK.h"
// This library is built for all targets, including host tests, so __cfi_slowpath may not be
// present. But it is only used in the bionic loader tests.
diff --git a/tests/libs/cfi_test_helper2.cpp b/tests/libs/cfi_test_helper2.cpp
index 11a6036..d7cd495 100644
--- a/tests/libs/cfi_test_helper2.cpp
+++ b/tests/libs/cfi_test_helper2.cpp
@@ -14,10 +14,9 @@
* limitations under the License.
*/
-#include <assert.h>
#include <dlfcn.h>
-#include "libs_utils.h"
+#include "CHECK.h"
int main(void) {
void* handle;
diff --git a/tests/libs/dlopen_b.cpp b/tests/libs/dlopen_b.cpp
index 092c96c..5b36242 100644
--- a/tests/libs/dlopen_b.cpp
+++ b/tests/libs/dlopen_b.cpp
@@ -1,8 +1,9 @@
#include <dlfcn.h>
-extern "C" void *dlopen_b() {
- // Work around for http://b/20049306, which isn't going to be fixed.
- static int defeat_sibling_call_optimization = 0;
+// Work around for http://b/20049306, which isn't going to be fixed.
+int defeat_sibling_call_optimization = 0;
+
+extern "C" void* dlopen_b() {
// This is supposed to succeed because this library has DT_RUNPATH
// for libtest_dt_runpath_x.so which should be taken into account
// by dlopen.
diff --git a/tests/libs/libs_utils.h b/tests/libs/dlopen_testlib_depends_on_simple.cpp
similarity index 68%
copy from tests/libs/libs_utils.h
copy to tests/libs/dlopen_testlib_depends_on_simple.cpp
index 7dae241..9e130d4 100644
--- a/tests/libs/libs_utils.h
+++ b/tests/libs/dlopen_testlib_depends_on_simple.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2017 The Android Open Source Project
+ * Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,14 +14,11 @@
* limitations under the License.
*/
-#ifndef LIBS_UTILS_H
-#define LIBS_UTILS_H
-
-#include <assert.h>
-#include <stdio.h>
+#include <stdint.h>
#include <stdlib.h>
-#define CHECK(e) \
- ((e) ? static_cast<void>(0) : __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #e))
+extern "C" bool dlopen_testlib_simple_func();
-#endif // LIBS_UTILS_H
+extern "C" bool dlopen_testlib_call_simple_func() {
+ return dlopen_testlib_simple_func();
+}
diff --git a/tests/libs/preinit_getauxval_test_helper.cpp b/tests/libs/preinit_getauxval_test_helper.cpp
index 2a79b97..53d4cc9 100644
--- a/tests/libs/preinit_getauxval_test_helper.cpp
+++ b/tests/libs/preinit_getauxval_test_helper.cpp
@@ -19,7 +19,7 @@
#include <unistd.h>
#include <sys/auxv.h>
-#include "libs_utils.h"
+#include "CHECK.h"
static unsigned long g_AT_RANDOM;
static unsigned long g_AT_PAGESZ;
diff --git a/tests/libs/preinit_syscall_test_helper.cpp b/tests/libs/preinit_syscall_test_helper.cpp
index 9b6b6df..3ca8131 100644
--- a/tests/libs/preinit_syscall_test_helper.cpp
+++ b/tests/libs/preinit_syscall_test_helper.cpp
@@ -19,7 +19,7 @@
#include <unistd.h>
#include <sys/auxv.h>
-#include "libs_utils.h"
+#include "CHECK.h"
static ssize_t g_result;
static int g_errno;
diff --git a/tests/libs/stack_tagging_helper.cpp b/tests/libs/stack_tagging_helper.cpp
index 7396dd0..e7e26af 100644
--- a/tests/libs/stack_tagging_helper.cpp
+++ b/tests/libs/stack_tagging_helper.cpp
@@ -28,7 +28,7 @@
#include <bionic/malloc.h>
-#include "libs_utils.h"
+#include "CHECK.h"
#if defined(__aarch64__)
diff --git a/tests/libs/libs_utils.h b/tests/libs/testbinary_is_stack_mte.cpp
similarity index 62%
copy from tests/libs/libs_utils.h
copy to tests/libs/testbinary_is_stack_mte.cpp
index 7dae241..8dde83c 100644
--- a/tests/libs/libs_utils.h
+++ b/tests/libs/testbinary_is_stack_mte.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2017 The Android Open Source Project
+ * Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,14 +14,25 @@
* limitations under the License.
*/
-#ifndef LIBS_UTILS_H
-#define LIBS_UTILS_H
-
-#include <assert.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
-#define CHECK(e) \
- ((e) ? static_cast<void>(0) : __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #e))
+#include "../mte_utils.h"
+#include "CHECK.h"
-#endif // LIBS_UTILS_H
+#if defined(__BIONIC__) && defined(__aarch64__)
+
+extern "C" int main(int, char**) {
+ int ret = is_stack_mte_on() ? 0 : 1;
+ printf("RAN\n");
+ return ret;
+}
+
+#else
+
+extern "C" int main(int, char**) {
+ printf("RAN\n");
+ return 1;
+}
+#endif
diff --git a/tests/libs/testbinary_is_stack_mte_after_dlopen.cpp b/tests/libs/testbinary_is_stack_mte_after_dlopen.cpp
new file mode 100644
index 0000000..c5e6868
--- /dev/null
+++ b/tests/libs/testbinary_is_stack_mte_after_dlopen.cpp
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <condition_variable>
+#include <thread>
+
+#include <dlfcn.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "../mte_utils.h"
+#include "CHECK.h"
+
+#if defined(__BIONIC__) && defined(__aarch64__)
+
+enum State { kInit, kThreadStarted, kStackRemapped };
+
+// We can't use pthread_getattr_np because that uses the rlimit rather than the actual mapping
+// bounds.
+static void find_main_stack_limits(uintptr_t* low, uintptr_t* high) {
+ uintptr_t startstack = reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
+
+ // Hunt for the region that contains that address.
+ FILE* fp = fopen("/proc/self/maps", "re");
+ if (fp == nullptr) {
+ abort();
+ }
+ char line[BUFSIZ];
+ while (fgets(line, sizeof(line), fp) != nullptr) {
+ uintptr_t lo, hi;
+ if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) {
+ if (lo <= startstack && startstack <= hi) {
+ *low = lo;
+ *high = hi;
+ fclose(fp);
+ return;
+ }
+ }
+ }
+ abort();
+}
+
+template <typename Fn>
+unsigned int fault_new_stack_page(uintptr_t low, Fn f) {
+ uintptr_t new_low;
+ uintptr_t new_high;
+ volatile char buf[4096];
+ buf[4095] = 1;
+ find_main_stack_limits(&new_low, &new_high);
+ if (new_low < low) {
+ f();
+ return new_high;
+ }
+ // Useless, but should defeat TCO.
+ return new_low + fault_new_stack_page(low, f);
+}
+extern "C" int main(int argc, char** argv) {
+ if (argc < 2) {
+ return 1;
+ }
+ const char* path = argv[1];
+ CHECK(access(path, F_OK) == 0); // Verify test setup.
+ CHECK(!is_stack_mte_on());
+ std::mutex m;
+ std::condition_variable cv;
+ State state = kInit;
+
+ bool is_early_thread_mte_on = false;
+ std::thread early_th([&] {
+ {
+ std::lock_guard lk(m);
+ state = kThreadStarted;
+ }
+ cv.notify_one();
+ {
+ std::unique_lock lk(m);
+ cv.wait(lk, [&] { return state == kStackRemapped; });
+ }
+ is_early_thread_mte_on = is_stack_mte_on();
+ });
+ {
+ std::unique_lock lk(m);
+ cv.wait(lk, [&] { return state == kThreadStarted; });
+ }
+ void* handle = dlopen(path, RTLD_NOW);
+ {
+ std::lock_guard lk(m);
+ state = kStackRemapped;
+ }
+ cv.notify_one();
+ CHECK(handle != nullptr);
+ CHECK(is_stack_mte_on());
+
+ bool new_stack_page_mte_on = false;
+ uintptr_t low;
+ uintptr_t high;
+ find_main_stack_limits(&low, &high);
+ fault_new_stack_page(low, [&] { new_stack_page_mte_on = is_stack_mte_on(); });
+ CHECK(new_stack_page_mte_on);
+
+ bool is_late_thread_mte_on = false;
+ 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);
+ printf("RAN\n");
+ return 0;
+}
+
+#else
+extern "C" int main(int, char**) {
+ return 1;
+}
+#endif
diff --git a/tests/memtag_stack_dlopen_test.cpp b/tests/memtag_stack_dlopen_test.cpp
index 308af1e..68ddb81 100644
--- a/tests/memtag_stack_dlopen_test.cpp
+++ b/tests/memtag_stack_dlopen_test.cpp
@@ -35,113 +35,82 @@
#include <android-base/silent_death_test.h>
#include <android-base/test_utils.h>
+#include "mte_utils.h"
#include "utils.h"
+TEST(MemtagStackDlopenTest, DependentBinaryGetsMemtagStack) {
#if defined(__BIONIC__) && defined(__aarch64__)
-__attribute__((target("mte"))) bool is_stack_mte_on() {
- alignas(16) int x = 0;
- void* p = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(&x) + (1UL << 57));
- void* p_cpy = p;
- __builtin_arm_stg(p);
- p = __builtin_arm_ldg(p);
- __builtin_arm_stg(&x);
- return p == p_cpy;
-}
+ if (!running_with_mte()) GTEST_SKIP() << "Test requires MTE.";
+ if (is_stack_mte_on())
+ GTEST_SKIP() << "Stack MTE needs to be off for this test. Are you running fullmte?";
-// We can't use pthread_getattr_np because that uses the rlimit rather than the actual mapping
-// bounds.
-static void find_main_stack_limits(uintptr_t* low, uintptr_t* high) {
- uintptr_t startstack = reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
-
- // Hunt for the region that contains that address.
- FILE* fp = fopen("/proc/self/maps", "re");
- if (fp == nullptr) {
- abort();
- }
- char line[BUFSIZ];
- while (fgets(line, sizeof(line), fp) != nullptr) {
- uintptr_t lo, hi;
- if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) {
- if (lo <= startstack && startstack <= hi) {
- *low = lo;
- *high = hi;
- fclose(fp);
- return;
- }
- }
- }
- abort();
-}
-
-template <typename Fn>
-unsigned int fault_new_stack_page(uintptr_t low, Fn f) {
- uintptr_t new_low;
- uintptr_t new_high;
- volatile char buf[4096];
- buf[4095] = 1;
- find_main_stack_limits(&new_low, &new_high);
- if (new_low < low) {
- f();
- return new_high;
- }
- // Useless, but should defeat TCO.
- return new_low + fault_new_stack_page(low, f);
-}
-
+ std::string path =
+ android::base::GetExecutableDirectory() + "/testbinary_depends_on_simple_memtag_stack";
+ ExecTestHelper eth;
+ std::string ld_library_path = "LD_LIBRARY_PATH=" + android::base::GetExecutableDirectory();
+ eth.SetArgs({path.c_str(), nullptr});
+ eth.SetEnv({ld_library_path.c_str(), nullptr});
+ eth.Run([&]() { execve(path.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "RAN");
+#else
+ GTEST_SKIP() << "requires bionic arm64";
#endif
+}
-enum State { kInit, kThreadStarted, kStackRemapped };
+TEST(MemtagStackDlopenTest, DependentBinaryGetsMemtagStack2) {
+#if defined(__BIONIC__) && defined(__aarch64__)
+ if (!running_with_mte()) GTEST_SKIP() << "Test requires MTE.";
+ if (is_stack_mte_on())
+ GTEST_SKIP() << "Stack MTE needs to be off for this test. Are you running fullmte?";
+
+ std::string path = android::base::GetExecutableDirectory() +
+ "/testbinary_depends_on_depends_on_simple_memtag_stack";
+ ExecTestHelper eth;
+ std::string ld_library_path = "LD_LIBRARY_PATH=" + android::base::GetExecutableDirectory();
+ eth.SetArgs({path.c_str(), nullptr});
+ eth.SetEnv({ld_library_path.c_str(), nullptr});
+ eth.Run([&]() { execve(path.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "RAN");
+#else
+ GTEST_SKIP() << "requires bionic arm64";
+#endif
+}
TEST(MemtagStackDlopenTest, DlopenRemapsStack) {
#if defined(__BIONIC__) && defined(__aarch64__)
+ // If this test is failing, look at crash logcat for why the test binary died.
if (!running_with_mte()) GTEST_SKIP() << "Test requires MTE.";
+ if (is_stack_mte_on())
+ GTEST_SKIP() << "Stack MTE needs to be off for this test. Are you running fullmte?";
- std::string path = android::base::GetExecutableDirectory() + "/libtest_simple_memtag_stack.so";
- ASSERT_EQ(0, access(path.c_str(), F_OK)); // Verify test setup.
- EXPECT_FALSE(is_stack_mte_on());
- std::mutex m;
- std::condition_variable cv;
- State state = kInit;
+ std::string path =
+ android::base::GetExecutableDirectory() + "/testbinary_is_stack_mte_after_dlopen";
+ std::string lib_path =
+ android::base::GetExecutableDirectory() + "/libtest_simple_memtag_stack.so";
+ ExecTestHelper eth;
+ std::string ld_library_path = "LD_LIBRARY_PATH=" + android::base::GetExecutableDirectory();
+ eth.SetArgs({path.c_str(), lib_path.c_str(), nullptr});
+ eth.SetEnv({ld_library_path.c_str(), nullptr});
+ eth.Run([&]() { execve(path.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "RAN");
+#else
+ GTEST_SKIP() << "requires bionic arm64";
+#endif
+}
- bool is_early_thread_mte_on = false;
- std::thread early_th([&] {
- {
- std::lock_guard lk(m);
- state = kThreadStarted;
- }
- cv.notify_one();
- {
- std::unique_lock lk(m);
- cv.wait(lk, [&] { return state == kStackRemapped; });
- }
- is_early_thread_mte_on = is_stack_mte_on();
- });
- {
- std::unique_lock lk(m);
- cv.wait(lk, [&] { return state == kThreadStarted; });
- }
- void* handle = dlopen(path.c_str(), RTLD_NOW);
- {
- std::lock_guard lk(m);
- state = kStackRemapped;
- }
- cv.notify_one();
- ASSERT_NE(handle, nullptr);
- EXPECT_TRUE(is_stack_mte_on());
+TEST(MemtagStackDlopenTest, DlopenRemapsStack2) {
+#if defined(__BIONIC__) && defined(__aarch64__)
+ // If this test is failing, look at crash logcat for why the test binary died.
+ if (!running_with_mte()) GTEST_SKIP() << "Test requires MTE.";
+ if (is_stack_mte_on())
+ GTEST_SKIP() << "Stack MTE needs to be off for this test. Are you running fullmte?";
- bool new_stack_page_mte_on = false;
- uintptr_t low;
- uintptr_t high;
- find_main_stack_limits(&low, &high);
- fault_new_stack_page(low, [&] { new_stack_page_mte_on = is_stack_mte_on(); });
- EXPECT_TRUE(new_stack_page_mte_on);
-
- bool is_late_thread_mte_on = false;
- std::thread late_th([&] { is_late_thread_mte_on = is_stack_mte_on(); });
- late_th.join();
- early_th.join();
- EXPECT_TRUE(is_early_thread_mte_on);
- EXPECT_TRUE(is_late_thread_mte_on);
+ std::string path =
+ android::base::GetExecutableDirectory() + "/testbinary_is_stack_mte_after_dlopen";
+ std::string lib_path =
+ android::base::GetExecutableDirectory() + "/libtest_depends_on_simple_memtag_stack.so";
+ ExecTestHelper eth;
+ std::string ld_library_path = "LD_LIBRARY_PATH=" + android::base::GetExecutableDirectory();
+ eth.SetArgs({path.c_str(), lib_path.c_str(), nullptr});
+ eth.SetEnv({ld_library_path.c_str(), nullptr});
+ eth.Run([&]() { execve(path.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "RAN");
#else
GTEST_SKIP() << "requires bionic arm64";
#endif
diff --git a/tests/mte_utils.h b/tests/mte_utils.h
new file mode 100644
index 0000000..0f18442
--- /dev/null
+++ b/tests/mte_utils.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#if defined(__BIONIC__) && defined(__aarch64__)
+
+__attribute__((target("mte"))) static bool is_stack_mte_on() {
+ alignas(16) int x = 0;
+ void* p = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(&x) + (1UL << 57));
+ void* p_cpy = p;
+ __builtin_arm_stg(p);
+ p = __builtin_arm_ldg(p);
+ __builtin_arm_stg(&x);
+ return p == p_cpy;
+}
+
+#endif