fix: use current_thread tokio runtime — avoid pthread_create SEGV on Android

Multi-thread tokio runtime crashes with SEGV_ACCERR in __init_tcb
during pthread_create on Android (static bionic stubs from CRT).
Switch to current_thread runtime which runs network I/O on the
calling thread without spawning additional OS threads.

Also: clean up build.rs — use only libc++_shared.so (dynamic),
remove getauxval_fix.c hack, remove static c++/c++abi linking.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claude
2026-04-05 09:27:46 +00:00
parent 9098e28a1f
commit 9d9ce4706d
7 changed files with 35 additions and 55 deletions

View File

@@ -1,9 +1,5 @@
[target.aarch64-linux-android] [target.aarch64-linux-android]
linker = "aarch64-linux-android26-clang" linker = "aarch64-linux-android26-clang"
rustflags = [
"-C", "target-feature=-outline-atomics",
"-C", "link-arg=-Wl,-z,lazy",
]
[target.armv7-linux-androideabi] [target.armv7-linux-androideabi]
linker = "armv7a-linux-androideabi26-clang" linker = "armv7a-linux-androideabi26-clang"

View File

@@ -4,12 +4,6 @@ fn main() {
let target = std::env::var("TARGET").unwrap_or_default(); let target = std::env::var("TARGET").unwrap_or_default();
if target.contains("android") { if target.contains("android") {
// Compile a getauxval override FIRST so it takes precedence over the
// broken static stub from compiler-rt/CRT that crashes in shared libs.
cc::Build::new()
.file("cpp/getauxval_fix.c")
.compile("getauxval_fix");
let oboe_dir = fetch_oboe(); let oboe_dir = fetch_oboe();
match oboe_dir { match oboe_dir {
Some(oboe_path) => { Some(oboe_path) => {
@@ -19,6 +13,8 @@ fn main() {
build build
.cpp(true) .cpp(true)
.std("c++17") .std("c++17")
// Use shared libc++ — avoids pulling in static libc stubs
// that crash in shared libraries (getauxval, pthread_create, etc.)
.cpp_link_stdlib(Some("c++_shared")) .cpp_link_stdlib(Some("c++_shared"))
.include("cpp") .include("cpp")
.include(oboe_path.join("include")) .include(oboe_path.join("include"))
@@ -44,15 +40,15 @@ fn main() {
} }
} }
// Use libc++_shared.so (dynamic) instead of static linking. // Dynamic C++ runtime — libc++_shared.so must be in jniLibs alongside
// libwzp_android.so. We copy it there from the NDK sysroot.
// //
// Static libc++ pulls in getauxval.o from libc.a, which has a // WHY NOT STATIC: libc++_static.a + libc++abi.a transitively pull in
// stub that reads from __libc_auxv (only initialized for executables, // object files from libc.a (static libc) which contain broken stubs for
// not shared libs). This causes SIGSEGV when ring/cpufeatures calls // getauxval, __init_tcb, pthread_create, etc. These stubs only work in
// getauxval at load time. Using the shared library avoids this. // statically-linked executables. In shared libraries loaded by dlopen(),
// // they SIGSEGV because the static libc init hasn't run.
// libc++_shared.so must be bundled in the APK alongside libwzp_android.so. // Google's official recommendation: use libc++_shared.so for native libs.
// build.rs copies it to jniLibs/ automatically.
if let Ok(ndk) = std::env::var("ANDROID_NDK_HOME") { if let Ok(ndk) = std::env::var("ANDROID_NDK_HOME") {
let arch = if target.contains("aarch64") { let arch = if target.contains("aarch64") {
"aarch64-linux-android" "aarch64-linux-android"
@@ -63,23 +59,32 @@ fn main() {
} else { } else {
"aarch64-linux-android" "aarch64-linux-android"
}; };
let lib_dir = format!("{ndk}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/{arch}"); let lib_dir = format!(
"{ndk}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/{arch}"
);
println!("cargo:rustc-link-search=native={lib_dir}"); println!("cargo:rustc-link-search=native={lib_dir}");
// Copy libc++_shared.so next to libwzp_android.so // Copy libc++_shared.so to the jniLibs directory
let shared_so = format!("{lib_dir}/libc++_shared.so"); let shared_so = format!("{lib_dir}/libc++_shared.so");
let shared_path = std::path::Path::new(&shared_so); if std::path::Path::new(&shared_so).exists() {
if shared_path.exists() { let jni_abi = if target.contains("aarch64") {
// Output to the jniLibs directory (one level up from OUT_DIR) "arm64-v8a"
let out_dir = std::env::var("OUT_DIR").unwrap(); } else if target.contains("armv7") {
// Also copy to a known location that Gradle cargoNdkBuild uses "armeabi-v7a"
let jni_dir = format!("{}/../../../jniLibs/arm64-v8a", out_dir); } else {
let _ = std::fs::create_dir_all(&jni_dir); "arm64-v8a"
let _ = std::fs::copy(shared_path, format!("{jni_dir}/libc++_shared.so")); };
println!("cargo:warning=Copied libc++_shared.so to jniLibs"); // Try to copy to the Gradle jniLibs directory
let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
let jni_dir = format!(
"{manifest}/../../android/app/src/main/jniLibs/{jni_abi}"
);
if let Ok(_) = std::fs::create_dir_all(&jni_dir) {
let _ = std::fs::copy(&shared_so, format!("{jni_dir}/libc++_shared.so"));
println!("cargo:warning=Copied libc++_shared.so to {jni_dir}");
}
} }
} }
println!("cargo:rustc-link-lib=c++_shared");
// Oboe needs liblog and libOpenSLES from Android // Oboe needs liblog and libOpenSLES from Android
println!("cargo:rustc-link-lib=log"); println!("cargo:rustc-link-lib=log");
@@ -111,7 +116,7 @@ fn add_cpp_files_recursive(build: &mut cc::Build, dir: &std::path::Path) {
} }
} }
/// Try to find or fetch Oboe headers. /// Try to find or fetch Oboe headers + source.
fn fetch_oboe() -> Option<PathBuf> { fn fetch_oboe() -> Option<PathBuf> {
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let oboe_dir = out_dir.join("oboe"); let oboe_dir = out_dir.join("oboe");

View File

@@ -1,21 +0,0 @@
// Override the broken static getauxval from compiler-rt/CRT.
// The static version reads from __libc_auxv which may be NULL in shared libs.
// This version calls the real bionic getauxval via dlsym.
#ifdef __ANDROID__
#include <dlfcn.h>
#include <stdint.h>
typedef unsigned long (*getauxval_fn)(unsigned long);
unsigned long getauxval(unsigned long type) {
static getauxval_fn real_getauxval = (getauxval_fn)0;
if (!real_getauxval) {
real_getauxval = (getauxval_fn)dlsym((void*)-1 /* RTLD_DEFAULT */, "getauxval");
if (!real_getauxval) {
// Fallback: return 0 (no features detected)
return 0;
}
}
return real_getauxval(type);
}
#endif

View File

@@ -106,9 +106,9 @@ impl WzpEngine {
}; };
} }
// Create tokio runtime // Create tokio runtime — use current_thread to avoid pthread_create
let runtime = tokio::runtime::Builder::new_multi_thread() // issues on Android (SEGV_ACCERR in __init_tcb with multi_thread).
.worker_threads(2) let runtime = tokio::runtime::Builder::new_current_thread()
.thread_name("wzp-net") .thread_name("wzp-net")
.enable_all() .enable_all()
.build()?; .build()?;

BIN
qr-download.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 696 B

Binary file not shown.