Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/ndk/bionic_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3838,6 +3838,22 @@ __thread __attribute__((tls_model("initial-exec"))) WrapEnv t_wrap_env;
std::int64_t g_wrap_disp =
0; // &t_wrap_env - glibc %fs base; constant across threads

// A thread's t_wrap_env and t_bionic_fs, addressed from its glibc thread
// pointer as a value. A function that swaps the thread pointer must reach them
// this way and not by name. The compiler takes the thread pointer for constant
// across a function, so it may compute `&t_wrap_env` on either side of a swap:
// at -O2 GCC computes it in w_GetEnv after the bionic thread pointer is back,
// and the env handed to the app library is an address under bionic's TCB.
// Both displacements are recorded before any app library runs.
inline WrapEnv* wrap_env_at(std::uintptr_t glibc_fs) {
return reinterpret_cast<WrapEnv*>(glibc_fs +
static_cast<std::uintptr_t>(g_wrap_disp));
}
inline std::uintptr_t* bionic_fs_at(std::uintptr_t glibc_fs) {
return reinterpret_cast<std::uintptr_t*>(
glibc_fs + static_cast<std::uintptr_t>(g_tls_disp));
}

// Entered under the CALLING thread's bionic %fs (from its native code); restore
// that same thread's glibc %fs for the real call, then put its bionic one back.
// Never a constant pair: restoring the bootstrapping thread's glibc %fs here
Expand All @@ -3847,8 +3863,8 @@ std::int64_t g_wrap_disp =
// as before.
struct ToGlibc {
std::uintptr_t bionic_fs;
ToGlibc() : bionic_fs(ndk_get_fs()) {
const std::uintptr_t glibc_fs = glibc_fs_for(bionic_fs);
std::uintptr_t glibc_fs;
ToGlibc() : bionic_fs(ndk_get_fs()), glibc_fs(glibc_fs_for(bionic_fs)) {
if (glibc_fs == 0)
acl_ndk_unattached(); // [[noreturn]]: no pairing => whose TLS is this?
ndk_set_fs(glibc_fs);
Expand Down Expand Up @@ -4868,14 +4884,13 @@ jint JNICALL w_GetEnv(JavaVM* vm, void** out, jint ver) {
// `.real`, and the first wrapper it calls (e.g. DeleteLocalRef)
// dereferences null. YouTube's libelements JNI_OnLoad hit exactly this.
// Mirrors AttachCurrentThread.
t_wrap_env.functions = &g_env_tbl;
t_wrap_env.real = tmp;
// &t_wrap_env is a glibc TLS access (%fs-relative), so it is only
// meaningful while ToGlibc holds this thread's glibc %fs. Taking it outside
// this scope would resolve the same displacement against BIONIC's TCB and
// hand the caller garbage — a JNIEnv that segfaults on first use. Any glibc
// TLS read under bionic %fs is silently wrong; keep them in here.
wrapped = reinterpret_cast<JNIEnv*>(&t_wrap_env);
// Not `&t_wrap_env`: that is a thread-pointer-relative address, and the
// compiler may compute it after this scope, against BIONIC's TCB, which
// hands the caller a JNIEnv that segfaults on first use (wrap_env_at).
WrapEnv* const we = wrap_env_at(g.glibc_fs);
we->functions = &g_env_tbl;
we->real = tmp;
wrapped = reinterpret_cast<JNIEnv*>(we);
}
if (out != nullptr)
*out = wrapped;
Expand Down Expand Up @@ -4921,9 +4936,10 @@ jint JNICALL w_AttachCurrentThread(JavaVM* vm, void** p_env, void* args) {
w->real, reinterpret_cast<void**>(&tmp), args);
if (rc != JNI_OK)
return rc;
t_wrap_env.functions = &g_env_tbl;
t_wrap_env.real = tmp;
wrapped = reinterpret_cast<JNIEnv*>(&t_wrap_env);
WrapEnv* const we = wrap_env_at(g.glibc_fs); // not &t_wrap_env
we->functions = &g_env_tbl;
we->real = tmp;
wrapped = reinterpret_cast<JNIEnv*>(we);
}
if (p_env != nullptr)
*p_env = wrapped;
Expand Down Expand Up @@ -4952,8 +4968,9 @@ jint JNICALL w_DetachCurrentThread(JavaVM* vm) {
// run that native on this thread's TCB: the wrong one, and by then
// unpaired, so its first call back up aborts as unattached. Its wrapped
// env is this thread's too.
t_bionic_fs = 0;
t_wrap_env.real = nullptr;
// By address from the donor's thread pointer, not by name (wrap_env_at).
*bionic_fs_at(glibc) = 0;
wrap_env_at(glibc)->real = nullptr;
ndk_set_fs(cur);
// Reclaim this thread's pairing + donor slot so a later bionic thread can
// reuse them (Chromium's ThreadPool churns more threads over a session than
Expand Down