Skip to content

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

Closed

Description

@wanderdev0

Apple's ADI library is initialised once per LocalAnisette instance, and a new instance is
built every time anything asks for Anisette. The library itself is process-global, so the second
and every subsequent initialisation re-enters an ADI that is already initialised — and it can
segfault.

Caught in CI, which aborted a run at 335 of 564 tests. The logcat artefact has the stack:

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x234546
pid: 1889, tid: 1917, name: RxCachedThreadS

#00 pc 0000000000234546  <unknown>
#01 pc 00000000001c5f0a  .../anisette/lib/x86_64/libstoreservicescore.so
#02 pc 00000000001c5cc5  .../anisette/lib/x86_64/libstoreservicescore.so
#03 pc 00000000001c4bad  .../anisette/lib/x86_64/libstoreservicescore.so
#04 pc 00000000001d23a6  .../anisette/lib/x86_64/libstoreservicescore.so
#05 pc 00000000000263d6  libopentagviewer.so (Java_..._NativeAdi_callWithPath+86)
#12 AdiLibrary.withPath
#18 AdiLibrary.initialise
#24 LocalAnisette.load
#30 LocalAnisette.ensureReady
#36 AnisetteStatus.of

NativeAdi.callWithPath is ADILoadLibraryWithPath or ADISetProvisioningPath.

The guard is per-instance; what it guards is per-process

// LocalAnisette
public synchronized boolean ensureReady() {
    if (this.adi != null) {
        return true;          // <- only ever true for an instance that already loaded
    }
    ...
    load(libraryDir);
}
// AppDependencies
anisetteFactory = LocalAnisette::new;   // <- a fresh instance every call

Neither AdiLibrary.open nor AdiLibrary.initialise caches anything. open re-dlopens (benign
— refcounted, same handle) and re-resolves the symbols; initialise then calls
ADILoadLibraryWithPath, ADISetProvisioningPath and ADISetAndroidID unconditionally.

So every caller re-initialises Apple's library in a process where it is already initialised.

Why this is not only a test problem

AppDependencies.anisette(...) is called on the way to restoring a session — so once per
MapsActivity creation. That activity is recreated by AppCompat on:

  • a theme change
  • a language change
  • a map provider change
  • the system-colours toggle
  • the Apple-devices toggle

Somebody flicking dark mode on and off a few times therefore re-initialises Apple's native
library a few times, in one process, from a background thread.

Not observed in the app — only under test, where ~560 activity creations happen in one
process and the odds compound. That is inference from the mechanism rather than a reproduction,
and worth saying plainly. But the mechanism is not in doubt, and the consequence in the app would
be a hard native crash with no Java stack, which is about the worst failure this app could have.

Impact under test, which is measurable

A native crash kills the instrumentation process. The run does not fail — it stops, and every
remaining test is reported as never having run. That is the intermittent whole-suite abort this
repo has been living with: it presents as two arbitrary test names plus Process crashed, where
the names are whichever tests were in flight and have nothing to do with the cause.

Worked around in the test suite already, by having the map fixture use FakeAnisetteSource the
way the login-flow tests always have. That removes the trigger from the suite; it does not fix
the app.

Fix direction

Hold the initialised AdiLibrary once per process rather than once per LocalAnisette, so a
second caller reuses it instead of re-initialising.

Care needed, because this is rule 4 territory and a mistake here surfaces as authentication
failing hours from the cause:

  • A second caller could legitimately want different state. The provisioning path and the
    hardware profile come from stored identity and are stable in practice, but "in practice" is not
    the same as enforced. If they differ, reusing silently is its own bug — that case should be
    loud rather than papered over.
  • ADIProvisioningErase genuinely needs to re-initialise. Whatever caches has to have a way
    to be invalidated, or resetting Anisette stops working.
  • Thread safety has to be process-wide too, not per instance, since the whole point is that
    two threads can now reach it.

Worth a test that asks for Anisette twice and asserts the library is initialised once — the
current behaviour would fail it, which is the point.

Related

  • #133 — the discarded-AppleAccount
    leak. Different cause; I guessed at that one first and was wrong.
  • AGENTS.md rule 4, on the Anisette constraints this sits inside.

🤖 Written by Claude Code

Activity

  1. wanderdev0 commented on Aug 22, 2026

    @wanderdev0
    OwnerAuthor

    The mechanism is worse than described above: it is not only repeated initialisation, it is
    concurrent initialisation, including two threads writing the same files.

    Caught in another aborted run. TheMapDrawsWhatIsStoredTest stores a session that cannot be
    restored — the ordinary expired-session state — and the logcat shows two threads unpacking and
    loading Apple's libraries at the same time:

    2083 2419  AdiLibraryFetcher: libc++_shared.so: 1.05 MB unpacked
    2083 2570  AdiLibraryFetcher: libc++_shared.so: 1.05 MB unpacked
    2083 2419  AdiLibraryFetcher: libstoreservicescore.so: 2.39 MB unpacked
    2083 2570  AdiLibraryFetcher: libstoreservicescore.so: 2.39 MB unpacked
    2083 2419  adi: dlopen(.../libstoreservicescore.so) ok
    2083 2419  AdiLibrary: ADI initialised, persisting to .../anisette/provisioning
    

    Two threads, same paths, same process.

    How two arrive at once

    It is the sequence this app takes whenever a session has gone bad:

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

    ensureReady() is synchronized, which looks like it covers this and does not: the lock is on
    the instance, and these are two instances. There is no mutual exclusion at all.

    Why that is worse than repeated init

    Repeated sequential initialisation re-enters an already-initialised ADI, which is bad. Concurrent
    initialisation adds two more failure modes:

    • Two threads writing the same .so files. AdiLibraryFetcher unpacks to a fixed path. A
      reader can dlopen a file another thread is part-way through writing, and a half-written
      shared object is not a shared object.
    • Two threads calling ADILoadLibraryWithPath / ADISetProvisioningPath on one native
      library
      that has no reason to be thread-safe and is not ours to make so.

    What this means for the fix

    Whatever holds the initialised AdiLibrary has to be process-wide and locked process-wide —
    the point is precisely that two threads reach it. A per-instance synchronized is what created
    this. The download and unpack need covering too, not just the initialise: the race starts before
    ADI is ever called.

    Reachability in the app

    The three-step sequence above is not contrived — it is what an expired session does today, and
    the session-recovery work in #134 makes that path more travelled rather than less. Still only
    observed under test, but the trigger is an ordinary user state rather than 560 activity
    creations in one process.


    🤖 Written by Claude Code

  2. added a commit that references this issue on Aug 22, 2026
    f71d4c6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions