wolfpld / tracy

Frame profiler
https://tracy.nereid.pl/
Other
10.25k stars 692 forks source link

Fix crash on Android due to wrong TLS model #925

Closed TroutZhang closed 1 week ago

TroutZhang commented 2 weeks ago

For android ndk, it is always a dynamic .so to let java load. So "initial-exec" would either not work, or crash for ndk 26+ with minSdk 29+.

check here: https://github.com/android/ndk/wiki/Changelog-r26#changes

Issue 1679: Clang will now automatically enable ELF TLS for minSdkVersion 29 or higher.

TroutZhang commented 2 weeks ago

Here is the PR for upstream: https://github.com/mjansson/rpmalloc/pull/347

TroutZhang commented 2 weeks ago

Fixes https://github.com/wolfpld/tracy/issues/779

wolfpld commented 2 weeks ago

How does this interact with previous Android SDKs, where the change was not needed?

TroutZhang commented 2 weeks ago

My colleague uses tracy for profiling on Android, and we recently upgraded ndk from 25c to 27c, today when enabling tracy, the build will crash, hence the fix.

TroutZhang commented 2 weeks ago

How does this interact with previous Android SDKs, where the change was not needed?

It seems previously the emulated TLS doesn't give error at all.

wolfpld commented 2 weeks ago

Ah, it shouldn't matter, there are ifdefs against the SDK and NDK version.

TroutZhang commented 2 weeks ago

My local fix is like the below:

#if defined(__ANDROID__) && __ANDROID_API__ >= 29 && defined(__NDK_MAJOR__) && __NDK_MAJOR__ >= 26
    #define TLS_MODEL __attribute__((tls_model("local-dynamic")))
#else
    #define TLS_MODEL
#endif
TroutZhang commented 2 weeks ago

Ah, it shouldn't matter, there are ifdefs against the SDK and NDK version.

Yes, it should be empty, but since the code works just as is, so I didn't change much.

TroutZhang commented 2 weeks ago

Basically, only one of our games, that has minSdk 29, triggered this. Others are fine when ndk 25c => 27c.

So, only ndk 26+ and minSdk 29+ will enable hardware ELF TLS, hence the crash in dlopen from Java's System.loadLibrary