Skip to content

Fix Apple's library crashing the app on some phones, and keep the keyboard off the server field (1.1.1) - #233

Merged
wanderdev0 merged 6 commits into
mainfrom
fix/survive-apples-library-crashing-on-load
Sep 17, 2026
Merged

wanderdev0 merged 6 commits into
mainfrom
fix/survive-apples-library-crashing-on-load

Conversation

@wanderdev0

@wanderdev0 wanderdev0 commented Sep 17, 2026 •

Copy link
Copy Markdown
Owner

Fixes #232.

What the crash log shows

From @PBue's adb logcat -b crash on a Redmi Note 11 Pro+ 5G (pissarropro), Android 13:

signal 7 (SIGBUS), code 1 (BUS_ADRALN)
  #00-02  …/files/anisette/lib/arm64-v8a/libstoreservicescore.so
  #03     linker64 (soinfo::call_constructors)
  #04-06  linker64 do_dlopen <- dlopen
  #07     libopentagviewer.so (NativeAdi_open)
  …       AdiLibrary.open <- LocalAnisette.load <- ensureReady <- AnisetteStatus.of
          <- AppleLoginActivity.getAnisetteSetupStatus

Apple's library dies inside dlopen, in its own static initialisers, before this app calls a single ADI function.

The cause: our makeWorkQueue stub

mediaplatform::WorkQueue::makeWorkQueue returns a std::shared_ptr by value. That comes back through a buffer the caller provides: x8 on arm64, a hidden first argument on x86_64. Our generated stub is long f(void) { return 0; }, which never writes that buffer. The static constructor for URLBagRequest::_queue (init_array[47]) then reads the leftover stack as {object, control} and increments the reference count at control+8:

15b2a4  add x8, sp, #0x18        ; buffer for the returned shared_ptr
15b2b0  bl  makeWorkQueue@plt    ; our stub: never writes it
15b2c0  ldp x9, x8, [sp, #0x18]  ; reads it anyway
15b2cc  cbz x8, …                ; zero on most phones: skipped
15b2d0  add x1, x8, #0x8
15b2d8  bl  __aarch64_ldadd8     ; ldxr x0, [x1] faults, x1 = 0x…903 = the fault addr

On most phones the leftover is zero and nothing happens. On these two it is a live pointer that is not 8-aligned, and the exclusive load takes an alignment fault (SIGBUS/BUS_ADRALN). Every register in the tombstone matches this path. x86_64 has the same bug and was only lucky.

The fix hand-writes the stub in stubs/libmediaplatform_handwritten.cpp. It returns a two-pointer struct with a non-trivial destructor, which forces the same indirect return on both ABIs, and the generator skips any symbol that file defines. libmediaplatform.expected, docs/anisette-native-android.md and AGENTS.md rule 4 all called this call harmless because provisioning succeeded. They are corrected.

The guard and the separate-process load below stay as defence against the next native crash nobody has found yet.

Why the existing fallback could not help

ensureReady catches every exception and falls back to a remote Anisette server. A signal is not an exception. The process died inside the call, the catch never ran, and because the sign-in screen checks Anisette as soon as it opens, it happened on every launch. Affected users could not reach Settings to choose a server, and 1.0.5 can no longer sign in at all — so they had nothing.

Surviving a native crash anyway

NativeLoadGuard writes a record immediately before the native calls and removes it after they return or throw. A record still present at the next launch means the load never returned, so that launch uses a server. Affected users meet the crash once, then the app works — landing on the server-choice step that existed before local Anisette.

The details that decide whether it works:

  • Checked inside the once-per-process load lock. Two threads loading at once is ordinary (Apple's ADI library is re-initialised on every Anisette request, and can segfault #135); checked outside, the second would read the first one's in-progress record as a crash.
  • The window is exactly AdiLibrary.open + initialise — native, milliseconds. Provisioning stays outside: it waits on the network, and a user closing the app then would switch local Anisette off for nothing.
  • The store uses commit(), not apply(). apply() queues the write and the queue dies with the process — it would pass every test and never record a crash.
  • The record names app version, ABI and library build. Only the same attempt counts, so an update retries and a fix reaches the devices it was for.
  • An exception clears the record before rethrowing — the process survived and the existing fallback handles it.

Loading it somewhere else first

On its own the guard only learns from a crash that already happened, so an affected phone would still crash once, including everyone updating from 1.1.0.

AppleLibraryProbeService runs in its own process (android:process=":adiprobe") and loads and initialises the library before the app does. If that process dies, the binder reports it, the answer is recorded, and the app uses a remote server. Nobody sees a crash. If it survives, the app loads the library itself, still under NativeLoadGuard. The answer is kept per app version, ABI and library build, so this runs once per update.

  • Both processes call LocalAnisette.openAndInitialise, so the test load and the real load make the same calls into Apple's code.
  • No reply within 20s means the library is not loaded this launch and nothing is recorded. If the service cannot be started at all, the app loads under the guard, as before.
  • The probe unbinds immediately on every path. A bound service whose process died is restarted by the system, and a second crash within a minute gets a "keeps stopping" dialog.
  • OpenAirTagApplication extends Application instead of PyApplication (which only called Python.start) and returns early in the probe process, so Python is not started twice.

A phone the library crashes on sees a sentence saying so above the server field, plus a Report this phone button. It opens the existing report screen with model, Android version, ABI and library build filled in.

Keyboard covering the Anisette server field

Targeting SDK 35 makes windows edge to edge on Android 15, and adjustResize no longer resizes them. The sign-in screen now takes the IME inset as a bottom margin on its ScrollView, which shrinks it and scrolls the focused field into view.

Tests

JVM, compiled and run locally with javac and JUnit 4.13.2:

  • SurvivingAppleLibraryCrashingOnLoadTest: 9/9. Mutations checked by hand: writing the record after the load, and a check that consumes the record.
  • TryingApplesLibraryElsewhereFirstTest: 7/7. Mutations checked by hand: not recording a death, loading on no answer, recording when the service could not start. Each is caught by exactly one test.

Native, run locally on this Mac for arm64 and x86_64 (Rosetta), against a caller declared with the real std::shared_ptr return type and a buffer pre-filled with 0xA5:

  • Old generated stub: buffer left as 0xa5a5… on both ABIs.
  • Hand-written stub: empty shared_ptr on both.
  • Hand-written stub without its destructor: 0xa5a5… on both, which shows the destructor is what does the work.
  • The first version of the stub put the struct in an anonymous namespace. That gave the function internal linkage, and the harness caught it failing to link. On Android it would have made the library fail to load everywhere.

Instrumented, CI's word only:

  • MakeWorkQueueStubTest: the same garbage-buffer call, made from adi.cpp through dlsym on the device. It needs no Apple library, so it runs in the default suite on x86_64.
  • ApplesLibraryInASeparateProcessTest: the probe process kills itself with a real SIGBUS, the app sees DIED and keeps running, and a second probe afterwards answers normally. It also checks that a load which throws counts as survived, not as a crash. There is only one crash in the whole suite, to avoid the "keeps stopping" dialog.
  • AnisetteStatusTest, LoginAnisetteFallbackLayoutTest: the new state, the sentence and button, and a report that names the phone.
  • WindowPaddingUtilTest: margin in, margin out, and a focused EditText ending up above a 600px keyboard.

Not verified: no test loads Apple's real library on arm64, and nobody affected has run a build with the stub fix yet. CI's emulator is x86_64 and the real-library tests are opt-in (AGENTS.md rule 4). The keyboard fix has not been seen on a real Android 15 phone.

58c04fa bumps the app to 1.1.1 (versionCode 5).

🤖 Generated with Claude Code

wanderdev0 and others added 2 commits September 17, 2026 16:44
Issue #232. On a Pixel 5 and a Redmi Note 12 Pro, 1.1.0 exits the moment it
opens. The crash log names it exactly: SIGBUS (BUS_ADRALN) inside Apple's
libstoreservicescore.so, in its own static initialisers, under
linker64 call_constructors <- dlopen <- NativeAdi.open - before this app has
called a single ADI function. It is the pinned arm64 binary, size and
SHA-256 checked, byte for byte the one that works on other phones.

LocalAnisette.ensureReady catches every exception and falls back to a remote
Anisette server, which is the right design and could not help: a signal is
not an exception. The process died inside the call, the catch never ran, and
because the sign-in screen checks Anisette as soon as it opens, it happened
again on every launch. There was no way to reach Settings and pick a server,
and 1.0.5 can no longer sign in at all, so affected users had nothing.

NativeLoadGuard writes a record immediately before the native calls and
removes it after they return or throw. A record still present at the next
launch means the load never returned, so that launch uses a server instead.
The user meets the crash once rather than on every launch.

Details that decide whether it works:

- The check runs inside the once-per-process load lock. Two threads loading
  at once is ordinary (#135); checked outside it, the second would read the
  first one's in-progress record as a crash.
- The window is exactly AdiLibrary.open and initialise - native, and
  milliseconds. Provisioning is outside it, since it waits on the network and
  a user closing the app then would switch local Anisette off for nothing.
- The store commits synchronously. apply() queues the write, and the queue
  dies with the process, so it would pass every test and never record a crash.
- The record names the app version, ABI and library build, and only a record
  for the same attempt counts. An update retries, so a fix reaches the devices
  it was for instead of them being written off permanently.
- An exception clears the record before rethrowing. The process survived and
  the existing fallback handles it.

Nine JVM tests, compiled and run locally with javac and JUnit rather than
left to CI. Two mutations checked by hand: writing the record after the load
fails the test that asserts it exists while Apple's code runs, and making the
check consume the record fails the one asserting it stays off on later
launches. LocalAnisette itself needs the Android SDK and is not compiled here.

AGENTS.md rule 4 records why a native crash bypasses the fallback, and why CI
as it stands cannot see this class - x86_64 downloads Apple's x86_64 build,
and the tests that load the real library are opt-in.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Hotfix for #232: Apple's ADI library crashing the app on launch on some
devices. versionCode moves with versionName so installs can upgrade.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
wanderdev0 and others added 2 commits September 17, 2026 17:06
Targeting SDK 35 makes windows edge to edge on Android 15, and an
edge-to-edge window is not resized when the keyboard opens, so
adjustResize did nothing and the Anisette server field was drawn
underneath the keyboard while somebody typed into it.

The sign-in screen now takes the keyboard's inset as a bottom margin on
its ScrollView. A margin rather than padding, because a ScrollView only
scrolls the focused field into view when it gets shorter.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The guard only learns from a crash that already happened, so an affected
phone still crashed once - including everyone updating from 1.1.0, who
has no record of the crash yet.

AppleLibraryProbeService, in its own :adiprobe process, now loads and
initialises Apple's library before the app does. If that process dies,
the binder reports it, the app records it and uses a remote Anisette
server, and nobody sees a crash. If it survives, the app loads the
library itself, still under NativeLoadGuard. The answer is kept per app
version, ABI and library build, so the extra process runs once.

A phone the library crashes on is told so on the sign-in screen and
offered a "Report this phone" button, which opens the existing report
screen with the model, Android version, ABI and library build filled in.

OpenAirTagApplication no longer extends PyApplication and returns early
in the probe process, so Python is not started from two processes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@wanderdev0 wanderdev0 changed the title Stop Apple's library crashing the app on every launch (1.1.1) Stop Apple's library crashing the app, and keep the keyboard off the server field (1.1.1) Sep 17, 2026
mediaplatform::WorkQueue::makeWorkQueue returns a std::shared_ptr by
value, which comes back through a buffer the caller provides (x8 on
arm64, a hidden first argument on x86_64). Our generated stub returned 0
in a register and never wrote it. The static constructor for
URLBagRequest::_queue in libstoreservicescore.so then read the leftover
stack as a control block and incremented its reference count through it.
Zero on most phones, so the increment was skipped; a live misaligned
pointer on a Pixel 5 and a Redmi Note 11 Pro+ 5G, where dlopen died with
SIGBUS BUS_ADRALN in __aarch64_ldadd8.

The stub is now written by hand in C++, returning a two-pointer struct
with a non-trivial destructor, which forces the same indirect return on
both ABIs. The generator skips any symbol the hand-written file defines.

MakeWorkQueueStubTest calls it the way Apple's code does, with the real
std::shared_ptr return type, into a garbage-filled buffer, so it fails on
any ABI without Apple's library. Checked on this Mac for arm64 and
x86_64: the old stub leaves the garbage, the new one returns empty, and
the same struct without its destructor leaves the garbage too.

Found by disassembling the pinned arm64 libstoreservicescore.so against
the crash log from #232.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@wanderdev0 wanderdev0 changed the title Stop Apple's library crashing the app, and keep the keyboard off the server field (1.1.1) Fix Apple's library crashing the app on some phones, and keep the keyboard off the server field (1.1.1) Sep 17, 2026
kill() delivers SIGBUS to whichever thread the process picks - on the
emulator, the main one - and the system writes a crash report before the
process is gone. The hook returned from kill(), so the loading thread went
on, failed its empty load and replied "survived" 11ms after the fatal
signal, before the death (seen in the logcat of the failing CI run). A
real crash is in the loading thread and can send nothing, so the hook now
never returns: it sleeps, and kills the process outright if it is somehow
still alive after ten seconds.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@wanderdev0
wanderdev0 merged commit 460a56f into main Sep 17, 2026
4 checks passed
@wanderdev0
wanderdev0 deleted the fix/survive-apples-library-crashing-on-load branch September 17, 2026 18:55

This branch was previously deployed

1 inactive deployment
Android Build — 1315ad66 Deployed Sep 17, 2026 by wanderdev0 via build #299
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App: App crashes immediately when opened.

1 participant