Skip to content

ndk: address thread-locals by value under a thread-pointer swap - #412

Merged
jwinarske merged 1 commit into
mainfrom
ndk/tls-address-under-swap
Dec 3, 2026
Merged

jwinarske merged 1 commit into
mainfrom
ndk/tls-address-under-swap

Conversation

@jwinarske

Copy link
Copy Markdown
Contributor

Summary

a4l built at -O2 with GCC crashed in any WebView app: SIGSEGV in libwebviewchromium's JNI_OnLoad, on the first use of the JNIEnv that GetEnv returned.

  • w_GetEnv took &t_wrap_env inside the ToGlibc scope. The compiler takes the thread pointer for constant across a function, so at -O2 GCC computed the address after bionic's thread pointer was back, and the env lay under bionic's TCB.
  • w_AttachCurrentThread had the same pattern; w_DetachCurrentThread wrote two thread-locals between two swaps.
  • wrap_env_at() and bionic_fs_at() now reach those thread-locals from the glibc thread pointer as a value, through the displacements already recorded for the trampolines.

The default debug build does not show the fault, and CI builds only that type, so nothing in CI would catch a regression here.

Test status

Check Result
x86, GCC 16, -O2, plain build type, one WebView app, main SIGSEGV in JNI_OnLoad, no frames
Same, with this change 3 x JNI_OnLoad ok, 647 frames in 25 s
arm64 (clang release build), i.MX 8M Plus, same app Loads and draws
x86 suite on the -O2 build 129 pass, 5 skip, 1 fail
clang-format clean

The five skips look for the build under build/. The failure is idle-gate, once in the full run; it passes twice alone.

a4l built at -O2 with GCC crashed in any WebView app: SIGSEGV in
libwebviewchromium's JNI_OnLoad, on the first use of the JNIEnv that
GetEnv returned.

w_GetEnv took &t_wrap_env inside the ToGlibc scope. The compiler takes
the thread pointer for constant across a function, so at -O2 GCC
computed the address after ~ToGlibc had put bionic's thread pointer
back, and the env it returned lay under bionic's TCB.
w_AttachCurrentThread had the same pattern, and w_DetachCurrentThread
wrote t_bionic_fs and t_wrap_env.real between two swaps. The default
debug build does not show it.

wrap_env_at() and bionic_fs_at() reach the two thread-locals from the
glibc thread pointer as a value, through the displacements already
recorded for the trampolines. ToGlibc keeps that pointer, and the three
wrappers use it.

Tested:
- x86, GCC 16, -O2 -D_FORTIFY_SOURCE=2 -fstack-protector-strong, plain
  build type, one WebView app: main crashes in JNI_OnLoad; with this
  it loads (3 x "JNI_OnLoad ok") and draws 647 frames in 25 s.
- arm64 (clang release build) on an i.MX 8M Plus: the same app loads
  and draws.
- x86 suite on the -O2 build: 129 pass, 5 skip (they look for the
  build under build/), idle-gate failed once in the full run and
  passes twice alone.
- clang-format clean.

Signed-off-by: Joel Winarske <joel.winarske@linux.com>
@jwinarske
jwinarske merged commit 83beb79 into main Dec 3, 2026
11 checks passed
@jwinarske
jwinarske deleted the ndk/tls-address-under-swap branch December 3, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant