fix(android): link the SDK against the dynamic libc, not static bionic
All checks were successful
android-ci / android (pull_request) Successful in 30m59s
All checks were successful
android-ci / android (pull_request) Successful in 30m59s
`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<ndk>/sysroot/usr/lib/<triple>` 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.
This commit is contained in:
parent
14f9284463
commit
519f0e66c3
2 changed files with 65 additions and 2 deletions
|
|
@ -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<KotlinCompile>().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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue