Skip to content

waze-on-cluster: apply the CarPlay patch in memory; carplay stays stock on disk - #233

Draft
robertnisipeanu wants to merge 1 commit into
mainfrom
inmemory-patch
Draft

robertnisipeanu wants to merge 1 commit into
mainfrom
inmemory-patch

Conversation

@robertnisipeanu

Copy link
Copy Markdown
Owner

waze-on-cluster applies CarPlay's cluster-map patch in CarPlay's memory at each start. /usr/bin/carplay stays as it ships, and nothing on / is remounted or written to patch, unpatch or recover CarPlay. Android Auto's patch is unchanged, still written to aapd and aapd.conf; the same mechanism can take it later.

Draft: this waits for a car test before it can merge or release.

Design

Where and when the edits apply. libwocpatch.so is preloaded into carplay.service. Its constructor runs after the shared libraries' own constructors and before carplay's static initialisers and main, so none of the edited code has run yet. One of the edits is a static initialiser's lea. It runs in every armed start, the starts patchd makes with SIGKILL+start included.

How sites are found and verified. There is one pattern description: the existing locator in carplay_locate.js. carplay_patch.js plan runs as root on the stock file on disk, with every check an apply's first edit runs (head unit, coding, locator, proven build or --unproven). It builds the edited image with the same code apply uses (edited(), shared by both). It writes a plan with one line per edit region: virtual address, shipped bytes, bytes to write. Regions the edit leaves stock are listed too. The plan is then laid back over the file (overlay) and kept only if that reproduces the edited image exactly. The C library locates nothing. It only verifies:

  • the main program's GNU build-id from its own PT_NOTE; any other program, such as a child inheriting LD_PRELOAD, is left alone;
  • the plan is whole: version, every line, the closing count, and only line kinds it knows;
  • every region lies in the file-backed part of one PT_LOAD, outside PT_GNU_RELRO, and reads exactly its stock bytes.

All or nothing. All sites are verified before any write. Each page is made writable; code pages stay executable for that moment, because libdlt's constructor has already started two threads, though neither runs carplay's code. Then the library writes, restores each segment's protection and reads every region back. Any failure puts the stock bytes back and restores the protections. Only a page that cannot get its protection back ends carplay (abort), which boot recovery counts as a crash. The result goes to /run/woc-preload/carplay.result and the journal: {pid, applied, edits, why}, in a file the boot step created for carplay's user. The library never creates files.

Arming. The drop-in written at install has EnvironmentFile=-/run/woc-preload/carplay.env. Without that file carplay starts stock. Boot recovery's boot step (and the watcher, and patchd after a change) decides per boot from the record and the crash counts, then writes the plan copy, the empty result file and, last, the env file. LD_PRELOAD is read from the unit (/usr/lib/libschedconfig.so.1), with this version's library appended: an EnvironmentFile value replaces Environment=, so BMW's preload is kept explicitly. The record (/var/opt/waze-on-cluster/inmem-carplay.json) arms only while carplay's inode, size, mtime and the plan's SHA-256 match. A firmware update or any other write of the file arms nothing.

Crash accounting. An armed start is counted exactly like a patched start today: thresholds of 1 on trial or unproven, 2 when proven, unknown boots counted in pairs, and the shutdown mark plus the recmgr/crashhandler restart records. One rule is stricter. For an armed start, a recorded crash restart counts even after the 60 s healthy mark, because a reset that beats the shutdown mark leaves only that record. At the threshold the restore removes the env file and turns the record off (by: "recovery"). No file on / is written and there is no remount, and the next start, this boot or the next, is stock. A trial becomes proven after a healthy run in which the running carplay's memory (/proc/<pid>/mem, read-only) holds the plan and the library's result names its PID. The boot-recovery state is written through a per-process temporary file, since the boot step, the watcher and patchd all write it.

Restarts to get the map.

Starting point activate Head-unit restarts
Fresh install on stock carplay plans and arms; pending 1 (the deploy hooks never restart CarPlay)
Earlier release's patch on disk in one step: checks the plan on our verified backup, checks the file differs from it only in our regions, checks arming can work, reverts the file from the backup, drops the backup, plans, arms 0 when the running carplay holds the plan's bytes (the earlier patch runs them), else 1
CarPlay's Patch or Remove patch button where patchd may restart CarPlay (recmgr's entry and the drop-in loaded), SIGKILL+start 0

During a migration, every later start of that boot is armed, a crash restart included, so the map is never off between the two patches. Where the file holds another change besides our patch, or a patch that would start on trial while boot recovery counts a failure, or arming cannot work here, the earlier patch stays in the file under this version's boot recovery. An earlier patch whose bytes equal the plan's, and which its record says ran healthy on this car coded 7, keeps its proven state; anything else starts on trial.

Status. patchd reads the plan's overlay for res, safe, mapurl and the mode, and checks the running carplay's memory at every plan region. The app sees Patched while that memory holds the plan, Patched after restart while it does not, the conflict not applied in memory: <why> when the library refused for that PID, and Removed after restart after Remove patch while the patch still runs. The existing conflicts (untested build, coding, another launcher) are unchanged.

Rollback. deactivate removes the env file, record and plan, and reverts any earlier on-disk patch. A release installed afterwards finds a stock carplay with no backup and patches the file as it always did. Older drop-ins have no EnvironmentFile, so an env file left in /run is never read. Coming back, this version never arms its plan over an older release's file patch, because the record no longer describes that file; its disk-patch boot recovery handles it. rollback_test.js part 6 runs every release from 0.5.0 on over the in-memory layout: crash loop, Remove patch and activate, then this version's recovery and removal.

Room to grow (not built here)

  • Hooks. The plan is versioned by line kind, and the library refuses kinds it does not know, so an older library never half-applies a newer plan. A hook kind would carry the site's stock bytes and the planner's relocated instructions for a trampoline. The C side would keep verifying, not decoding, under the same all-or-nothing write and the same crash accounting.
  • Calling carplay's own functions, and a control channel. A hook can capture carplay's objects, for example AlternateDisplays and its Cinemo interface, so that code in the library can call them on carplay's own event thread. A socket for that would be served from inside carplay, accept only root peers (SO_PEERCRED), and be reached only through patchd.
  • Runtime layouts. The geometry is already data in the plan, so a different layout is a different plan with its own trial or proven state, chosen per start.

Not done

  • Android Auto in memory: aapd.conf would need a per-unit bind mount or an open interposition.
  • No real-car run yet: a test kit runs one CarPlay start with the patch in memory from /run, then installs this build.

AI: Claude Code, Claude Opus 5.5

Tested:

  • make test-preload (new): libwocpatch.so against a test PIE, built with the unit's gcc 7.5 / glibc 2.27 toolchain in Docker. It covers .rodata and .text edits, another program's build-id, one site not stock, a cut-short plan, an unknown line kind, a count mismatch, an address outside the program, no plan, no env, a thread from another library's constructor, a crash build aborting after it applies, the result file never created, and nothing exported.
  • make test host suites with carplay 22.28.3 (CARPLAY_BIN):
    • new inmem_test.js: plan and overlay, proven carry-over rules, arm and disarm, the memory check, boot recovery counting armed starts, the stricter tally, a firmware update;
    • patchd_patch_test, patchd_activate_test (migration cases included) and patchd_reboot_test, updated for the in-memory behaviour;
    • recovery_test, and rollback_test with part 6 over every release;
    • the rest unchanged and passing.
  • On a test unit running the firmware's carplay 22.28.3:
    • the library applied the plan in the real carplay, which then offers the cluster display;
    • migration from 0.10.10's proven on-disk patch took one install step, needed no restart, and the next start was patched in memory;
    • a fresh install was pending one restart;
    • Patch and Remove patch needed no restart;
    • a crashing test build of the library was counted, disarmed at the threshold with no file written, and the next start was stock with no loop;
    • downgrading to 0.10.10 left the file patch on disk, and an uninstall left it stock.
  • Not tested: a real car or a phone session.

Human review: no

Screenshots: n/a

🤖 Generated with Claude Code

…disk

libwocpatch.so, preloaded into carplay.service next to BMW's
libschedconfig, applies the six edits to carplay's memory before its own
code runs. carplay_patch.js plan makes the plan from the stock file with
the locator and an apply's checks; the library verifies the build-id and
every site's shipped bytes and writes all edits or none.

Boot recovery arms each start through /run/woc-preload/carplay.env (the
drop-in's EnvironmentFile=-) from a record that describes the file as it
is, counts armed starts with the existing thresholds and records (a crash
restart counts even after a healthy run), and at the threshold stops
arming: no write of / and no remount. An earlier release's file patch is
reverted from its verified backup and the plan armed in one install step.
Android Auto's patch stays in the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant