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) }