From 519f0e66c35865a9ef291bf0c5ffc4dc7ac331fa Mon Sep 17 00:00:00 2001 From: apk-dev Date: Sat, 19 Sep 2026 00:40:02 -0400 Subject: [PATCH] fix(android): link the SDK against the dynamic libc, not static bionic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `openWire` segfaults at `dlopen` on every ABI, so the app dies the moment the passphrase is submitted and the onion never publishes. The tombstone: signal 11 (SIGSEGV), fault addr 0x0 #00 libonionwire_sdk.so (getauxval+21) #01 libonionwire_sdk.so (__cxa_atexit+604) #02 libonionwire_sdk.so (_GLOBAL__sub_I_superscalar.cpp+891) #06 libjnidispatch.so (Java_com_sun_jna_Native_open+68) #42 uniffi.onionwire_sdk.Onionwire_sdkKt.openWire+0 `_GLOBAL__sub_I_superscalar.cpp` is RandomX's, and RandomX arrived with the `cuprate-daemon` feature (#113). It calls `getauxval` from a static initializer — and `libonionwire_sdk.so` had linked bionic **statically**, so that `getauxval` read an auxv vector only the dynamic linker ever fills in. The first SDK call is the first `dlopen`, which is the first time anything touches the library: the crash lands exactly on "user typed the password". The static libc came in with the same commit that added the C++ link flags. `linkFlags()` passes `-L/sysroot/usr/lib/` for the libc++ archives; that directory also holds the static `libc.a`, while the dynamic `libc.so` stub lives in the per-API subdirectory that cargo-ndk's linker wrapper normally supplies — and that wrapper is bypassed on purpose here, so the per-API directory was no longer on the search path at all and `-lc` resolved to `libc.a`. Both directories are now passed, the per-API one first, and `verifyDynamicLibc` fails the build if `libc.so` ever leaves DT_NEEDED again. before (v0.9.0 arm64): NEEDED liblog.so only, local getauxval/__cxa_atexit after: NEEDED libstdc++.so, libdl, liblog, libm, libc.so, no local libc Also: the keep-alive notice is re-announced with the locator once the node publishes. It is anchored at the Open tap with an empty onion (so the shade does not claim a reachable node while Tor bootstraps), and only the POST_NOTIFICATIONS callback ever replaced it — on a device that already granted notifications there is no callback at all, so the notice claimed "OnionWire is starting" forever. --- .../siriusdevops/onionwire/WireViewModel.kt | 8 ++- android/sdk/build.gradle.kts | 61 ++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt index 81a1e92..04097ed 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt @@ -367,7 +367,13 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { * the system refuses anyway, [NodeHost] records why and the roster says so. */ private suspend fun startKeepAlive(w: Wire) { - if (NodeHost.keepAlive.value is KeepAliveStatus.On) return + // Announced even when the keep-alive is already `On`: it was anchored at + // the Open tap with an empty locator, so the notice says "OnionWire is + // starting". The POST_NOTIFICATIONS callback happens to re-announce on a + // fresh install, but on a device that already granted notifications + // there is no callback at all — the notice would then claim bootstrap + // forever. Same notification id, so this replaces it with the address + // the user can actually hand out. KeepAliveService.start(getApplication(), w.onion()) } diff --git a/android/sdk/build.gradle.kts b/android/sdk/build.gradle.kts index ccc4989..87a0aa8 100644 --- a/android/sdk/build.gradle.kts +++ b/android/sdk/build.gradle.kts @@ -118,7 +118,28 @@ val ndkClangLibDir: File = ndkDir.resolve("toolchains/llvm/prebuilt/$ndkHostTag/ */ fun linkFlags(target: AndroidTarget): String { val sysrootLib = ndkDir.resolve("toolchains/llvm/prebuilt/$ndkHostTag/sysroot/usr/lib/${target.sysrootLib}") + // The *dynamic* libc/libm/libdl stubs live in a per-API subdirectory; the + // arch directory above holds the static `libc.a` and the libc++ archives. + // Both must be on the search path, and this one must come first: `-lc` + // resolves to whichever the linker finds first, and bionic linked + // statically into a shared object is a landmine — its `getauxval` reads an + // auxv vector only the dynamic linker fills in, so the first C++ static + // initializer that asks for it (RandomX's, from `cuprate-daemon`) null-derefs + // inside `dlopen` and takes the process with it. + val sysrootStubLib = sysrootLib.resolve(onionwireApiLevel.toString()) + require(sysrootStubLib.resolve("libc.so").isFile) { + "no dynamic libc stub at $sysrootStubLib/libc.so — NDK $onionwireNdkVersion layout changed? " + + "Linking the arch dir alone would pull in the static libc.a." + } return listOf( + // Hygiene on a hand-written link line: lld defaults to --no-as-needed, + // which records a DT_NEEDED for every `-l` mentioned even when nothing + // is taken from it. (`libstdc++.so` survives this anyway — randomx-rs's + // cmake half asks for it and it satisfies symbols the static libc++ + // archives were already scanned for. That shim is present on every + // Android release to date; the device run in the PR loads it.) + "-C", "link-arg=-Wl,--as-needed", + "-C", "link-arg=-L$sysrootStubLib", "-C", "link-arg=-L$sysrootLib", "-C", "link-arg=-L$ndkClangLibDir", "-C", "link-arg=-lclang_rt.builtins-${target.builtins}", @@ -294,6 +315,42 @@ tasks.withType().configureEach { dependsOn(uniffiBindgen) } -tasks.matching { it.name == "preBuild" }.configureEach { - dependsOn(cargoBuildAndroid, uniffiBindgen) +/** + * Fails the build if the SDK's shared object did not link bionic dynamically. + * + * `linkFlags()` spells the NDK search path out by hand, and `-L` order decides + * whether `-lc` resolves to the per-API `libc.so` stub or to the arch + * directory's `libc.a`. Static bionic inside a `.so` leaves `getauxval` reading + * an auxv vector only the dynamic linker fills in, so the first C++ static + * initializer that asks for CPU features null-derefs inside `dlopen` — a crash + * with no Java stack, at the first SDK call, on every ABI. Reading the built + * ELF is the only check that catches it before a device does. + */ +val verifyDynamicLibc by tasks.registering { + group = "verification" + description = "Asserts libonionwire_sdk.so links libc.so dynamically (no static bionic)." + dependsOn(cargoBuildAndroid) + val libsDir = jniLibsDir + val abis = onionwireAbis + val readelf = ndkDir.resolve("toolchains/llvm/prebuilt/$ndkHostTag/bin/llvm-readelf") + doLast { + abis.forEach { abi -> + val so = libsDir.get().asFile.resolve(abi).resolve("libonionwire_sdk.so") + check(so.isFile) { "no $so to verify — did cargoBuildAndroid run for $abi?" } + val proc = ProcessBuilder(readelf.absolutePath, "-d", so.absolutePath) + .redirectErrorStream(true) + .start() + val dump = proc.inputStream.bufferedReader().use { it.readText() } + check(proc.waitFor() == 0) { "$readelf failed on $so:\n$dump" } + check(dump.lineSequence().any { it.contains("(NEEDED)") && it.contains("libc.so") }) { + "$so has no libc.so in DT_NEEDED — it linked the static libc.a. " + + "That is the `-L` order in linkFlags() regressing; a device crashes " + + "with a null deref in getauxval the first time the SDK is dlopen'd.\n$dump" + } + } + } +} + +tasks.matching { it.name == "preBuild" }.configureEach { + dependsOn(cargoBuildAndroid, uniffiBindgen, verifyDynamicLibc) } -- 2.52.0