Skip to content

Load Apple's ADI library once per process, instead of once per caller - #137

Merged
wanderdev0 merged 1 commit into
mainfrom
fix/load-apples-adi-once
Aug 22, 2026
Merged

wanderdev0 merged 1 commit into
mainfrom
fix/load-apples-adi-once

Conversation

@wanderdev0

Copy link
Copy Markdown
Owner

Three whole-suite aborts today, each naming different tests, all the same native crash. The
logcat artefact added in #130 is what made it findable.

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), tid RxCachedThreadS
  libstoreservicescore.so
  <- NativeAdi.callWithPath <- AdiLibrary.initialise <- LocalAnisette.load
  <- LocalAnisette.ensureReady <- AnisetteStatus.of

The guard that guarded nothing

public synchronized boolean ensureReady() {
    if (this.adi != null) {
        return true;
    }

That reads like "load it once". It is not, because of one line elsewhere:

anisetteFactory = LocalAnisette::new;   // AppDependencies

Every caller gets a fresh instance — so its adi field is always null, and its lock is
always uncontended. The guard never fired and the synchronized excluded nothing, while the
thing being guarded — a shared object loaded into the process, and ADI's own internal state — is
per-process.

Two at once is the ordinary case, not a corner

It is what an expired session does:

  1. MapsActivity starts restoring and asks for Anisette on an Rx thread.
  2. The restore fails, so the app sends the user to the login screen.
  3. AppleLoginActivity calls AnisetteStatus.of(AppDependencies.anisette(...)) — while the
    first is still working.

Both then unpack the same files to the same path before loading them:

2419  AdiLibraryFetcher: libstoreservicescore.so: 2.39 MB unpacked
2570  AdiLibraryFetcher: libstoreservicescore.so: 2.39 MB unpacked

The fix

Apple's library is held in a process-wide LoadedOnce, and the lock covers the download and
unpack
as well as the initialise — the race starts at the file writes, not at ADI.

Two things surfaced while writing it:

  • The identity is cached with the library. loadOrCreateIdentity creates one when there is
    none, so two threads on a fresh install could mint two different identities and register this
    app twice on somebody's Apple account. Rule 11 is emphatic that there is one identity;
    nothing had noticed this route to breaking it.
  • A failed load must not be cached. One flat network moment cannot mean Anisette never loads
    again until restart — and because the fallback is a remote server, nothing would look broken
    enough to investigate.

Making it testable, which inline it was not

Exercising this in place means downloading Apple's libraries and running their native code, which
the instrumented suite deliberately avoids. Left inline, the one property that matters would have
been verified by reading it.

So the rule is a small class, LoadedOnce, with a JVM test that releases twenty threads at
once
and asserts the supplier ran exactly once — plus that a failure is retried rather than
remembered, and that forget() works, which a future ADIProvisioningErase will need.

Verified it catches the real bug: dropping synchronized from get() — literally the shape
LocalAnisette had — turns exactly that test red and nothing else.

Evidence the app is fixed, not just the tests

The iCloud PR removed the trigger from four test classes by faking Anisette. That was a
workaround and was described as one.

This branch has none of those fakes. The classes that were aborting runs still load Apple's
real library here — and the suite completes: 569 tests, no failures, no abort, where three
runs earlier today stopped at 335, 374 and 489.

Not proof that every abort had this cause, but it is the only one anybody has a stack trace for,
and the trigger is an ordinary user state rather than a test artefact.

Closes #135


🤖 Written by Claude Code

Three whole-suite aborts today, each naming different tests. All the same
native crash:

    signal 11 (SIGSEGV) in tid RxCachedThreadS
      libstoreservicescore.so <- NativeAdi.callWithPath <- AdiLibrary.initialise
      <- LocalAnisette.load <- ensureReady <- AnisetteStatus.of

`ensureReady()` is `synchronized` and guards with `this.adi != null`, which
looks like it covers this. It does not: `AppDependencies.anisette` is
`LocalAnisette::new`, so every caller gets a fresh instance with its own lock
and its own null field. The guard never fired and the lock excluded nothing.

Two threads reaching it at once is the ordinary expired-session path - the map
restores on one, the restore fails, the login screen it redirects to asks on
another - and the log shows both unpacking the same file before dlopening it:

    2419  AdiLibraryFetcher: libstoreservicescore.so: 2.39 MB unpacked
    2570  AdiLibraryFetcher: libstoreservicescore.so: 2.39 MB unpacked

Now held in a process-wide `LoadedOnce`, covering the download and the unpack
as well as the initialise, because the race starts at the file writes.

Two things that came out of writing it:

- **The identity is cached with the library.** `loadOrCreateIdentity` *creates*
  one when there is none, so two threads on a fresh install could mint two and
  register this app twice on somebody's Apple account. Rule 11 says there is one
  identity; nothing had noticed this route to breaking that.
- **A failed load is not cached.** One flat network moment must not mean
  Anisette never loads again until restart - and since the fallback is a remote
  server, nothing would look broken enough to investigate.

**Testable, which it would not have been inline.** Exercising this in place
means downloading Apple's libraries and running their native code, which the
suite deliberately avoids - so the rule is extracted to `LoadedOnce` and tested
with twenty threads released at once. Verified it catches the real bug: dropping
`synchronized` from `get()`, which is the shape LocalAnisette had, turns exactly
that test red.

Evidence the app fix works, not just the tests: this branch has none of the
test-side Anisette fakes, so the classes that were aborting runs still load the
real library here. 569 tests, no failures, no abort.

Closes #135

Co-Authored-By: Claude Opus 5 <[email protected]>
@wanderdev0
wanderdev0 force-pushed the fix/load-apples-adi-once branch from 40bc23d to f71d4c6 Compare August 22, 2026 15:34
@wanderdev0
wanderdev0 merged commit e12fda1 into main Aug 22, 2026
3 checks passed
saamdotexe pushed a commit to saamdotexe/OpenTagViewer that referenced this pull request Oct 22, 2026
…di-once

Load Apple's ADI library once per process, instead of once per caller

This branch was previously deployed

1 inactive deployment
Android Build — f71d4c61 Deployed Aug 22, 2026 by wanderdev0 via Instrumented tests (emulator) #149
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.

Apple's ADI library is re-initialised on every Anisette request, and can segfault

1 participant