diff --git a/native/waze-on-cluster/README.md b/native/waze-on-cluster/README.md index b6b16f72..a08e130d 100644 --- a/native/waze-on-cluster/README.md +++ b/native/waze-on-cluster/README.md @@ -74,7 +74,10 @@ restores the original binary before CarPlay (or aapd) starts: after one failed start for a patch not yet proven on that car, after two for a proven one. CarPlay comes back without the map, its Status reads `Removed — head unit kept restarting`, and its Patch button applies the patch again -(PATCH.md "Boot recovery"). +(PATCH.md "Boot recovery"). Android Auto's patch is removed at the first +crash of the patched aapd, and aapd is started again after every end, so +Android Auto comes back without the cluster map; the patch stays off until +its Patch button applies it or an update brings a newer patch. To break a loop by hand: keep the phone from connecting (unplug it, or turn off its Wi-Fi and Bluetooth), then over SSH run @@ -266,6 +269,7 @@ for experiments and for undoing the patches without the app: |---|---| | `status`, `aa-status` | what is on disk | | `probe` | where the edit sites are in a carplay binary and whether it is patchable, without writing | +| `aa-probe []` | the same for aapd (`/usr/bin/aapd` by default) | | `verify` | whether carplay's patch is complete | | `revert [--force]` | restores the original carplay; `--force` over another build (a firmware update) | | `aa-revert [--force]` | restores the original aapd and aapd.conf | diff --git a/native/waze-on-cluster/patch/PATCH.md b/native/waze-on-cluster/patch/PATCH.md index 4ad9f13a..9062fd57 100644 --- a/native/waze-on-cluster/patch/PATCH.md +++ b/native/waze-on-cluster/patch/PATCH.md @@ -800,7 +800,18 @@ start), a site a dynamic relocation covers refused. A build where a site is not found, is found more than once or fails a check is refused with the reason; `patchd` reports it as `not patchable: ` (`Status: Not supported on this car` in the app's Android Auto section). Each check has a -name in `aapd_locate.js` (`CHECKS`). +name in `aapd_locate.js` (`CHECKS`), and `aapd_locate_test.js` shows for +each one a changed build that it alone refuses. The checks tie the sites to +each other, not to a build: the gate's kept result must be the flag that +turns the cluster display code on, the self-start's fields must be those +the session start and end set, and its opening test must read the object +its function was called with. No list of builds decides it: a build of +another version patches when its code has the same shape. Before writing, +`apply` locates its own edited copy again, and writes nothing unless every +site reads patched at the same offsets. `aapd_patch.js probe []` +prints each site's offset, its bytes on disk and both forms, or why it is +not found, the backup's and the running copy's SHA-256 and the patch +record; Save logs includes it (`aapd-probe.txt`). **Proven builds**, listed by build-id in `aapd_patch.js`: the first edit needs the whole file to be the shipped one. Another build counts as @@ -847,7 +858,7 @@ In the proven build: |---|---|---|---| | gate | `0x998bd` | `83bb1c020000 01` (`cmp dword [rbx+0x21c],1`) | `…02` | | selfstart call | `0x1d43eb` | `e800f8ffff` (`call setNaviStreamStatus`) | `e840fbffff` (`call requestStream`) | -| selfstart guard | `0x1d3f5d` | `0f848d000000` (`je 0x1d3ff0`) | `e98e00000090` (`jmp 0x1d3ff0 ; nop`) | +| selfstart guard | `0x1d3f53` | `837b3802 740a 837b4402 0f848d000000` (`cmp dword [rbx+0x38],2 ; je L1 ; cmp dword [rbx+0x44],2 ; je 0x1d3ff0`) | `807b4100 740a 837b3802 0f858d000000` (`cmp byte [rbx+0x41],0 ; je L1 ; cmp dword [rbx+0x38],2 ; jne 0x1d3ff0`) | | screen | `0x15b440` | `660fefc0 ebe1 662e0f1f840000000000` | `83f802 740b 660fefc0 ebdc 0f1f440000` | - **gate.** `Application::secondVideoEnabled()` returns `featureFlag && @@ -858,10 +869,27 @@ In the proven build: then waits for a `ProjectedModeRequestStreamCommand` the car never sends, as CarPlay waits for NavController. `onNewNavigationStatus()`'s shared tail calls `requestStream(this)` instead of `setNaviStreamStatus(this, - status)` (the extra argument is unused), and `requestStream()` no longer - waits on the `[rbx+0x44] == 2` a `StreamCommandChanged` event would set; - its own check that it is not streaming already (`[rbx+0x38] != 2`) stays, - and it still calls `setNaviStreamStatus(STREAMING)`. The two go together. + status)` (the extra argument is unused), on every status but rerouting + and active while streaming. `requestStream()` sets the stream status to + STREAMING and calls the cluster display's + `SetVideoFocus(PROJECTED, unsolicited)`, whose error + `throwOnCinemoError` throws and nothing catches: aapd aborts. Cinemo + refuses that call until the phone has set the cluster display up + (`Unable to SetVideoFocus() without active player or correct + configuration!`), as before a session, before the phone's display is + ready, after the session ends or with a phone that offers no cluster + display. Stock, the call can come only from the car's request, to which + aapd subscribes in `onAlternateVideoReady()`. So the edit replaces + requestStream's opening test, not streaming (`[rbx+0x38] != 2`) and the + focus mode native (`[rbx+0x44] == 2`), with: the cluster display ready + (`[rbx+0x41]`, which `onAlternateVideoReady()` sets and `onEndSession()` + clears) and not streaming. Before that, it logs `Ignoring event. Already + streaming...` and returns; the next navigation status after the display + is ready starts the stream. The focus mode test goes, so a stream the + phone's main-display focus change marked ERROR starts again at the next + status. The two go together. Releases up to 0.10.10 made the focus test + a `jmp` and kept no readiness test; the locator reads that guard as + `outdated`, and an install replaces it with this one. - **screen.** `loadKombiDisplayDefinition()` turns the class into the screen size it looks up in aapd.conf (`0x15b400`: 0 → 12.3, 1 → 10.25, else 0.0); with 0.0 it skips aapd.conf and sends an 800x480 stream with the UI @@ -891,12 +919,21 @@ In the proven build: its result as a byte of its object. No other code compares the field with 1 or 2. - *selfstart*: `requestStream()`, which `onStreamCommandChanged()` calls, - opens with `cmp [this+A],2 ; je +10 ; cmp [this+B],2 ; je Ldo` (or the - patched `jmp Ldo ; nop`); before `Ldo` it logs `Ignoring event. Already - streaming...`, after it `Request Stream Command event received`, calls - `setNaviStreamStatus(STREAMING)` (2) and sets `B` to 1. + copies `rdi` (its `this`) into the register its opening test reads and + opens with `cmp [this+A],2 ; je +10 ; cmp [this+B],2 ; je Ldo`, or the + patched `cmp byte [this+F],0 ; je +10 ; cmp [this+A],2 ; jne Ldo`, or + the outdated `... ; jmp Ldo ; nop`; before `Ldo` it logs `Ignoring + event. Already streaming...`, after it `Request Stream Command event + received`, calls `setNaviStreamStatus(STREAMING)` (2) and sets `B` to 1. + `onAlternateVideoReady()` sets one byte field `F` to 1 and calls + `subscribeEvents()`; `onEndSession()` sets `F` to 0, `A` to 0 and `B` + to 2; the patched test must read that `F`, and in the patched form `B` is + the field set to 1 after the call that `onEndSession()` sets to 2. `setNaviStreamStatus` copies its argument's value (`[rsi+X]`) into `A`; where the build has its name string, it is the function loading it. + The gate's kept result (a byte of the Application) is tested by + `initAlternateDisplays()`, which builds the cluster display, and before + every branch into `onNewNavigationStatus()` (within 0x40 bytes of it). `onNewNavigationStatus()` calls `setNaviStreamStatus` (stock) or `requestStream` (patched) exactly once: that call. On every path to it, `rsi` holds `rsp+B`, its value followed through the function by @@ -988,7 +1025,7 @@ needs 2 x 7.4 MB free on `/` (the backup and the temporary copy) plus 1 MiB, read with `df`, or it refuses. `revert` needs no locator. aapd is restored over a file of the backup's -build (the same size and build-id, or at most 68 bytes apart without one): +build (the same size and build-id, or at most 88 bytes apart without one): the edits' bytes only when the locator reads it and the pristine backup as the same build at the same sites, else the whole backup. aapd.conf is restored whole over a file that differs from the backup only in @@ -1127,6 +1164,32 @@ starts aapd again as after a crash. The shutdown record writes such an end as `stopped`, so the boot step does not count it either. The watcher follows aapd only on a unit that has it. +Android Auto differs in what follows an end, so that it is never left down +and a failing patch is not tried again: + +- **A crash of the patched aapd restores the original at once**, the + first one, proven build or not: the watcher, where the head unit stays + up, then starts the original; the boot step, where the crash restarted + the head unit (the watcher saw it, or the shutdown record says + `failed`), before aapd starts. A failed start with no crash of aapd + recorded counts against the thresholds below, as for CarPlay. A crash of + the run that was up when a change waiting for the next start + (`aa-pending`) replaced its file is not counted: the start runs the file + on disk, and a crash of that run counts. +- **aapd is started again after every end** the watcher sees where recmgr + loaded its entry this boot: a crash, a SIGKILL `patchd` did not send, and + a clean exit of a run that started (`ExecMainStartTimestampMonotonic` + above 0), 2 s, 5 s, 15 s and 30 s after the first four ends in a row, + then 60 s after each one, with no limit. A run active 60 s makes the + next start 2 s again. A start that fails is tried again the same way. +- **The patch stays off** while the patch record names the restore: an + install leaves it off, unless its patch revision (`aapd_patch.js` + `PATCH_REV`, kept in the patch record as `rev`, 1 when there is none) is + newer than the removed patch's; Android Auto's Patch button applies it + again. `patchd` reports the restore as `recovered` with + `recoveredWhy`: `crash` (the patched aapd crashed) or `boot` (failed + head-unit starts). + **What is recorded.** - The patch record, `.woc-patch.json` (`patch_record.js`), beside @@ -1135,7 +1198,8 @@ follows aapd only on a unit that has it. with nothing to write when the file has changed since the record was written, or there is none), by the watcher when a patch on trial ran healthy (below), by the restore below with `by: "recovery"`, - `at` and `fails` after failed starts, or `by: "refused"` and `at` on a + `at`, `fails`, `why` (`crash` or `boot`) and `rev` (the removed patch's + revision) after failed starts or a crash, or `by: "refused"` and `at` on a head unit that refuses our parts, and removed with the backup. It describes the target while its inode, size and modification time are the recorded ones. For aapd `proven` is whether the build is its one known build. For carplay it is true only for a @@ -1390,7 +1454,7 @@ the next start. Where recmgr loaded its `carplay` entry this boot and the head unit stays up, for a patch at `fails` plus this start's crashes at the threshold the watcher restores the original, then starts CarPlay with `systemctl start carplay.service`, 5 s, 15 s and 60 s after a crash, at -most three times per head-unit start, only while CarPlay is down, the +most three times per head-unit start (aapd: after every end, above), only while CarPlay is down, the system is not stopping and `patchd` does not hold the remount lock. The run it starts is probed at the next tick. The watcher also runs the boot step's logic at its own start, so a start whose boot step did not run or finish is diff --git a/native/waze-on-cluster/patch/aapd_locate.js b/native/waze-on-cluster/patch/aapd_locate.js index 73cae72c..248066c8 100644 --- a/native/waze-on-cluster/patch/aapd_locate.js +++ b/native/waze-on-cluster/patch/aapd_locate.js @@ -40,6 +40,10 @@ var NAMES = { /^void (?:\w+::)*AlternateDisplays::onStreamCommandChanged\(const (?:\w+::)*ProjectedModeStreamCommand&\)$/, 'self-start: onStreamCommandChanged'], received: ['Request Stream Command event received', /^Request Stream Command event received$/, 'self-start: request log'], already: ['Ignoring event. Already streaming', /^Ignoring event\. Already streaming\.\.\.$/, 'self-start: already-streaming log'], + ready: ['AlternateDisplays::onAlternateVideoReady()', /^void (?:\w+::)*AlternateDisplays::onAlternateVideoReady\(\)$/, 'self-start: onAlternateVideoReady'], + endSession: ['AlternateDisplays::onEndSession()', /^void (?:\w+::)*AlternateDisplays::onEndSession\(\)$/, 'self-start: onEndSession'], + subscribe: ['AlternateDisplays::subscribeEvents()', /^void (?:\w+::)*AlternateDisplays::subscribeEvents\(\)$/, 'self-start: subscribeEvents'], + initAlt: ['Application::initAlternateDisplays()', /^void (?:\w+::)*Application::initAlternateDisplays\(\)$/, 'gate: initAlternateDisplays'], /* not in every build: checked whenever a string names it */ ss: ['AlternateDisplays::setNaviStreamStatus(', /AlternateDisplays::setNaviStreamStatus\(/, 'self-start: setNaviStreamStatus'], }; @@ -52,8 +56,8 @@ var CHECKS = [ 'conv-copy', 'conv-switch-once', 'conv-mapping', 'getter-shared', 'getter-once', 'helper-arg', 'helper-once', 'size-1', 'size-0', 'class0-shape', 'room', 'fall-through', - 'gate-field', 'gate-flag', 'gate-once', 'gate-kept', 'gate-unique', - 'near', 'rs-called', 'guard-once', 'guard-form', 'already-log', 'request-log', 'request-log-once', + 'gate-field', 'gate-flag', 'gate-once', 'gate-kept', 'gate-unique', 'gate-gates-alt', 'onnav-gated', + 'near', 'rs-called', 'guard-once', 'guard-form', 'guard-this', 'ready-flag', 'ready-subscribe', 'end-resets', 'guard-ready', 'already-log', 'request-log', 'request-log-once', 'ss-call-once', 'ss-field', 'ss-field-once', 'streaming', 'mark', 'ss-name', 'onnav-once', 'arg-pointer', 'arg-frame', 'status-unavailable', 'status-available', 'reloc', @@ -293,18 +297,71 @@ function locateGate(im, str, refs, field) { if (c && c.field === field && (c.imm === 1 || c.imm === 2)) return fail('gate: variant compared outside secondVideoEnabled'); } var body = hex(buf, g.at, 6); - return { ok: true, offset: g.at, len: 7, state: buf[g.at + 6] === 1 ? 'original' : 'patched', stock: body + '01', patched: body + '02' }; + return { ok: true, offset: g.at, len: 7, state: buf[g.at + 6] === 1 ? 'original' : 'patched', stock: body + '01', patched: body + '02', + flag: on('gate-kept') ? buf.readUInt32LE(st + 2) : null }; +} + +/* `cmp byte [reg+disp32],0` (80 /7) with disp32 `flag` in [from, to). */ +function flagTests(buf, from, to, flag) { + return E.scan(buf, from, to, [0x80, null, flag & 0xff, (flag >>> 8) & 0xff, (flag >>> 16) & 0xff, flag >>> 24, 0]) + .filter(function (q) { return (buf[q + 1] & 0xf8) === 0xb8 && (buf[q + 1] & 7) !== 4; }); +} + +/* The gate's kept result is the flag that turns the AlternateDisplays code + * on: initAlternateDisplays tests it before it builds the cluster display, + * and every branch into onNewNavigationStatus (the navigation-status + * callback's) is taken only after a test of it. Why not, or null. */ +function gateGatesAlt(im, str, refs, flag, onNav) { + var buf = im.buf, init = entryFunc(im, refs, str, 'initAlt'); + if (init.err) return init.err; + if (on('gate-gates-alt') && !flagTests(buf, init.off, init.end, flag).length) return 'gate: initAlternateDisplays does not test the gate\'s result'; + if (!on('onnav-gated')) return null; + var into = []; + for (var p = im.exec.off; p + 5 <= im.exec.end; p++) { + if ((buf[p] === 0xe8 || buf[p] === 0xe9) && im.rel(p + 1, p + 5) === onNav) into.push(p); + } + if (!into.length) return 'self-start: nothing branches into onNewNavigationStatus'; + var open = into.filter(function (p) { return !flagTests(buf, Math.max(im.exec.off, p - 0x40), p, flag).length; }); + return open.length ? 'self-start: onNewNavigationStatus reached without a test of the gate\'s result' : null; } /* ---- Edit: selfstart (onNewNavigationStatus's call, requestStream's guard) */ -/* cmp dword [reg+A],2 ; je over the guard ; cmp dword [reg+B],2 */ +/* requestStream's opening test at `p`, 16 bytes, R its object register, L1 + * the end of the 16 bytes: + * cmp dword [R+A],2 ; je L1 ; cmp dword [R+B],2 ; je Ldo (stock) + * cmp byte [R+F],0 ; je L1 ; cmp dword [R+A],2 ; jne Ldo (patched) + * cmp dword [R+A],2 ; je L1 ; cmp dword [R+B],2 ; jmp Ldo ; nop (outdated: + * the patch of releases up to 0.10.10) + * A is the stream status, B the video focus mode, F the alternate display's + * ready flag. { form (original | patched | outdated | null: the opening + * test with another jump), r, a, b (null when patched), f (patched only), + * g (the jump's offset), ldo } or null. */ function guardAt(buf, p) { - var r = buf[p + 1]; - if (buf[p] !== 0x83 || (r & 0xf8) !== 0x78 || (r & 7) === 4 || buf[p + 3] !== 2 || buf[p + 4] !== 0x74 || buf[p + 5] !== 10 || - buf[p + 6] !== 0x83 || buf[p + 7] !== r || buf[p + 9] !== 2 || buf[p + 2] === buf[p + 8]) return null; - return { a: buf[p + 2], b: buf[p + 8], g: p + 10, r: r & 7 }; + var r = buf[p + 1], g = p + 10; + if ((r & 0xf8) !== 0x78 || (r & 7) === 4 || buf[p + 4] !== 0x74 || buf[p + 5] !== 10 || buf[p + 6] !== 0x83 || buf[p + 7] !== r || buf[p + 9] !== 2) return null; + var out = { p: p, r: r & 7, g: g, form: null, ldo: null }; + if (buf[p] === 0x80 && buf[p + 3] === 0) { + if (buf[g] !== 0x0f || buf[g + 1] !== 0x85) return null; + out.form = 'patched'; out.f = buf[p + 2]; out.a = buf[p + 8]; out.b = null; out.ldo = rel32(buf, g + 2); + return out; + } + if (buf[p] !== 0x83 || buf[p + 3] !== 2 || buf[p + 2] === buf[p + 8]) return null; + out.a = buf[p + 2]; out.b = buf[p + 8]; + if (buf[g] === 0xe9 && buf[g + 5] === 0x90) { out.form = 'outdated'; out.ldo = rel32(buf, g + 1); } + else if (buf[g] === 0x0f && (buf[g + 1] === 0x84 || !on('guard-form'))) { out.form = 'original'; out.ldo = rel32(buf, g + 2); } + return out; +} + +/* `mov byte [reg+d8],v` (c6 /0) in fn: the d8 of each. */ +function byteStores(buf, fn, v) { + return E.scan(buf, fn.off, fn.end, [0xc6, null, null, v]).filter(function (q) { return (buf[q + 1] & 0xf8) === 0x40 && (buf[q + 1] & 7) !== 4; }).map(function (q) { return buf[q + 2]; }); +} +/* `mov dword [reg+d8],v` (c7 /0) in [from, to): the d8 of each. */ +function dwordStores(buf, from, to, v) { + return E.scan(buf, from, to, [0xc7, null, null, v, 0, 0, 0]).filter(function (q) { return (buf[q + 1] & 0xf8) === 0x40 && (buf[q + 1] & 7) !== 4; }).map(function (q) { return buf[q + 2]; }); } +function h8(n) { return (n & 0xff).toString(16).replace(/^(.)$/, '0$1'); } /* A status argument built on the stack: its vtable stored at [rsp+B] * (`mov [rsp+B],rax`) and its value at [rsp+B+X], X where @@ -343,10 +400,8 @@ function statusStores(buf, from, to, v, d) { return E.scan(buf, from, to, [0xc7, 0x44, 0x24, d === undefined ? null : d, v, 0, 0, 0]); } -/* requestStream (called by onStreamCommandChanged) opens with - * cmp [this+A],2 ; je L1 ; cmp [this+B],2 ; je Ldo (stock) - * cmp [this+A],2 ; je L1 ; cmp [this+B],2 ; jmp Ldo ; nop (patched) - * L1 logging that it is streaming already, Ldo logging the request and +/* requestStream (called by onStreamCommandChanged) opens with one of + * guardAt's forms, L1 logging that it is streaming already, Ldo logging the request and * calling setNaviStreamStatus(STREAMING), then setting B to 1. * setNaviStreamStatus (named so where the build has its name string) copies * the status argument's value into A. onNewNavigationStatus calls it @@ -363,12 +418,25 @@ function locateSelfstart(im, str, refs) { for (var p = rs.off; p + 16 <= Math.min(rs.end, rs.off + ENTRY); p++) { var x = guardAt(buf, p); if (x) gs.push(x); } var gd = one(gs, 'guard-once'); if (!gd) return fail(count(gs, 'self-start: requestStream guard')); - var g = gd.g, ldo, gState; - if (buf[g] === 0xe9 && buf[g + 5] === 0x90) { ldo = rel32(buf, g + 1); gState = 'patched'; } - else if (buf[g] === 0x0f && (buf[g + 1] === 0x84 || !on('guard-form'))) { ldo = rel32(buf, g + 2); gState = 'original'; } - else return fail('self-start: requestStream guard is neither stock nor patched'); + var g = gd.g, ldo = gd.ldo, gState = gd.form; + if (!gState) return fail('self-start: requestStream guard is neither stock nor patched'); + /* R is requestStream's this: copied from rdi before the test. */ + if (on('guard-this') && !E.scan(buf, rs.off, gd.p, [0x48, null, null]).some(function (q) { + return (buf[q + 1] === 0x89 && buf[q + 2] === (0xf8 | gd.r)) || (buf[q + 1] === 0x8b && buf[q + 2] === (0xc7 | gd.r << 3)); + })) return fail('self-start: requestStream tests another object than its own'); + /* The alternate display's ready flag F: onAlternateVideoReady sets it + * and subscribes to the car's stream commands, the only way stock + * requestStream is reached; onEndSession clears it with the stream + * status and puts the focus mode back to native (2). The patched test + * reads that flag, so requestStream goes ahead only while the phone's + * cluster display is set up. */ + var ready = entryFunc(im, refs, str, 'ready'), endS = entryFunc(im, refs, str, 'endSession'), sub = entryFunc(im, refs, str, 'subscribe'); + if (ready.err || endS.err || sub.err) return fail(ready.err || endS.err || sub.err); + var fs1 = byteStores(buf, ready, 1), F = one(fs1, 'ready-flag'); + if (F === undefined) return fail(count(fs1, 'self-start: onAlternateVideoReady\'s ready flag')); + if (on('ready-subscribe') && !im.calls(ready.off, ready.end).some(function (c) { return c.to === sub.off; })) return fail('self-start: onAlternateVideoReady does not subscribe to the stream commands'); function loads(s, from, to) { return refs[s.off].some(function (q) { return q >= from && q < to; }); } - if (on('already-log') && !loads(str.already, g + 6, ldo)) return fail('self-start: requestStream does not log streaming already'); + if (on('already-log') && !loads(str.already, gd.p + 16, ldo)) return fail('self-start: requestStream does not log streaming already'); var recvs = on('request-log') ? refs[str.received.off].filter(function (q) { return q >= ldo && q < rs.end; }) : [ldo]; var recv = one(recvs, 'request-log-once'); if (recv === undefined) return fail(recvs.length ? 'self-start: requestStream logs the request ' + recvs.length + ' times' : 'self-start: requestStream does not log the request'); @@ -392,8 +460,16 @@ function locateSelfstart(im, str, refs) { return fail('self-start: requestStream does not pass the STREAMING status it builds'); if (on('arg-frame') && sv && !sameFrame(im, rs, [sv.v.q, sv.s], ssCall.at)) return fail('self-start: requestStream builds STREAMING in another frame than the call\'s'); - if (on('mark') && !E.scan(buf, ssCall.at + 5, Math.min(rs.end, ssCall.at + 0x30), [0xc7, 0x40 | gd.r, gd.b, 0x01, 0x00, 0x00, 0x00]).length) - return fail('self-start: requestStream does not mark the request'); + /* After the call requestStream sets the focus mode B to projected (1). + * The patched form does not name B: there it is the one field set so + * that onEndSession sets to 2. */ + var marked = E.scan(buf, ssCall.at + 5, Math.min(rs.end, ssCall.at + 0x30), [0xc7, 0x40 | gd.r, null, 0x01, 0x00, 0x00, 0x00]).map(function (q) { return buf[q + 2]; }); + var endTwo = dwordStores(buf, endS.off, endS.end, 2); + var B = gd.b !== null ? gd.b : one(marked.filter(function (d) { return endTwo.indexOf(d) >= 0; }), 'mark'); + if (on('mark') && (B === undefined || marked.indexOf(B) < 0)) return fail('self-start: requestStream does not mark the request'); + if (on('end-resets') && (byteStores(buf, endS, 0).indexOf(F) < 0 || dwordStores(buf, endS.off, endS.end, 0).indexOf(gd.a) < 0 || endTwo.indexOf(B) < 0)) + return fail('self-start: onEndSession does not reset the ready flag, the stream status and the focus mode requestStream tests'); + if (on('guard-ready') && gState === 'patched' && gd.f !== F) return fail('self-start: requestStream tests another flag than the ready flag'); if (on('ss-name') && !/not found$/.test(str.ss.err || '')) { var named = entryFunc(im, refs, str, 'ss'); if (named.err || named.off !== ss) return fail(named.err || 'self-start: requestStream calls another function than setNaviStreamStatus'); @@ -421,8 +497,10 @@ function locateSelfstart(im, str, refs) { call: { ok: true, offset: at, len: 5, state: site.to === ss ? 'original' : 'patched', stock: 'e8' + E.hex32(im.va(ss) - next), patched: 'e8' + E.hex32(im.va(rs.off) - next), onNav: onNav.off, requestStream: rs.off, setNaviStreamStatus: ss }, - guard: { ok: true, offset: g, len: 6, state: gState, - stock: '0f84' + E.hex32(im.va(ldo) - im.va(g + 6)), patched: 'e9' + E.hex32(im.va(ldo) - im.va(g + 5)) + '90' }, + guard: { ok: true, offset: gd.p, len: 16, state: gState, + stock: '83' + h8(0x78 | gd.r) + h8(gd.a) + '02740a83' + h8(0x78 | gd.r) + h8(B === undefined ? 0 : B) + '020f84' + E.hex32(im.va(ldo) - im.va(gd.p + 16)), + patched: '80' + h8(0x78 | gd.r) + h8(F) + '00740a83' + h8(0x78 | gd.r) + h8(gd.a) + '020f85' + E.hex32(im.va(ldo) - im.va(gd.p + 16)), + readyFlag: F }, }; } @@ -461,6 +539,8 @@ function locate(buf, opts) { var ssr = locateSelfstart(im, str, refs); out.call = ssr.ok === false ? ssr : ssr.call; out.guard = ssr.ok === false ? ssr : ssr.guard; + var gg = out.gate.ok && out.call.ok && out.gate.flag !== null ? gateGatesAlt(im, str, refs, out.gate.flag, out.call.onNav) : null; + if (gg) out.gate = out.call = out.guard = fail(gg); } var hit = on('reloc') ? E.relocated(im, EDITS.filter(function (e) { return out[e].ok; }).map(function (e) { return [e, out[e].offset, out[e].len]; })) : { all: null, edits: [] }; diff --git a/native/waze-on-cluster/patch/aapd_patch.js b/native/waze-on-cluster/patch/aapd_patch.js index 908472f6..e88cfc90 100644 --- a/native/waze-on-cluster/patch/aapd_patch.js +++ b/native/waze-on-cluster/patch/aapd_patch.js @@ -15,9 +15,13 @@ * apply refuses unless the coding is the proven 7. * selfstart onNewNavigationStatus()'s call of setNaviStreamStatus * retargeted to requestStream() (same `this`), and - * requestStream()'s `je` on a StreamCommandChanged field that is - * never set made a `jmp`: aapd starts the cluster stream itself - * once navigation is active. + * requestStream()'s opening test, `not streaming && focus mode + * native`, made `cluster display ready && not streaming`: aapd + * starts the cluster stream itself on the phone's navigation + * status, once the phone has set up its cluster display. Its + * SetVideoFocus() throws on a Cinemo error, and nothing catches + * that, so the request must wait for the display as the car's + * own request does. * screen the KombiVariant -> screen-size helper's fall-through: class 2 * reads 12.3 as class 0 does, so aapd uses aapd.conf's 12.3 * geometry instead of its built-in 800x480 default. @@ -32,6 +36,7 @@ * unit that refuses our parts (head_unit.js); revert works on any. * * aapd_patch.js status [--target ] [--conf ] [--coding ] + * aapd_patch.js probe [] [--target ] * aapd_patch.js apply [--dpi ] [--card shown|hidden] [--target ...] [--conf ...] [--coding ...] [--unproven] [--yes] * aapd_patch.js revert [--target ...] [--conf ...] [--yes] [--force] [--drop-backup] * @@ -61,16 +66,22 @@ var PROVEN = { '2dfb9881706bff435b05f621486e8eb8b6c55984': { version: 'aapd 22.39.3+gitrAUTOINC+d5a1db18c4-r0', sha256: '6880f54c3abba40c1368f2bf254c77bd4a169323c0d4cab8f9662a7c912dcf89', - stock: [[0x998bd, '83bb1c02000001'], [0x1d43eb, 'e800f8ffff'], [0x1d3f5d, '0f848d000000'], [0x15b440, '660fefc0ebe1662e0f1f840000000000']], + stock: [[0x998bd, '83bb1c02000001'], [0x1d43eb, 'e800f8ffff'], [0x1d3f53, '837b3802740a837b44020f848d000000'], [0x15b440, '660fefc0ebe1662e0f1f840000000000']], }, }; /* The locator's edits and their names on the status lines. */ var EDITS = [['gate', 'gate'], ['call', 'selfstart call'], ['guard', 'selfstart guard'], ['screen', 'screen']]; /* The most bytes aapd may differ in from its backup and still be its build: - * the edits' bytes (gate 7, call 5, guard 6, screen at most 16), twice + * the edits' bytes (gate 7, call 5, guard 16, screen at most 16), twice * over. */ -var SAME_BUILD_MAX_DIFF = 2 * (7 + 5 + 6 + 16); +var SAME_BUILD_MAX_DIFF = 2 * (7 + 5 + 16 + 16); +/* The patch's revision, kept in the patch record. Boot recovery copies it + * into its record when it removes a patch, and an install applies again a + * patch boot recovery removed only when this revision is newer than the + * removed one (patchd.js plan). 2: requestStream goes ahead only while the + * phone's cluster display is set up. */ +var PATCH_REV = 2; /* /dev/shm/rawcodingdata, the coding image CodingShmApp publishes: a 0x2c * byte header (flags at 0x28, 0x09 = valid coding) and the blocks. aapd's @@ -190,7 +201,7 @@ function restorePlan(b, cur, isBin) { return { out: out, other: other }; } -function oursIn(c) { return EDITS.some(function (e) { return c.loc[e[0]].ok && c.loc[e[0]].state === 'patched'; }); } +function oursIn(c) { return EDITS.some(function (e) { return c.loc[e[0]].ok && (c.loc[e[0]].state === 'patched' || c.loc[e[0]].state === 'outdated'); }); } /* Each file's undo as it stands, aapd then aapd.conf: { file, cur, b (its * backup), p (the restore plan), floor (the bits aapd.service's user needs, @@ -334,6 +345,38 @@ function cmdStatus(t) { (fs.existsSync(backupPath(t.conf)) ? ', ' + backupPath(t.conf) : '')); } +/* What the locator reads at each site of the file (aapd by default), for + * Save logs: its build, every edit's offset, bytes on disk, stock and + * patched forms and state, or why it is not found; and the files beside it + * (the backup, the running copy patchd keeps linked, the patch record). It + * never writes. */ +function cmdProbe(t, file) { + var f = file || t.target, buf = readOr(f); + if (!buf) die('cannot read ' + f); + var c = classify(buf); + line('file', f); + line('size', buf.length); + line('sha256', c.sha256); + line('build-id', c.loc.buildId || 'none'); + line('version', c.loc.version || 'unknown'); + line('proven', PROVEN[c.loc.buildId] ? 'yes' : 'no'); + line('patch-rev', String(PATCH_REV)); + line('patchable', c.bad.length ? 'no (' + c.bad.join('; ') + ')' : 'yes'); + EDITS.forEach(function (e) { + var s = c.loc[e[0]], k = e[1].replace(' ', '-'); + if (!s || !s.ok) { line(k, 'not found (' + (s ? s.reason : 'not located') + ')'); return; } + line(k, s.state + ' at 0x' + s.offset.toString(16) + ', ' + s.len + ' bytes: ' + buf.toString('hex', s.offset, s.offset + s.len) + + ' (stock ' + (s.stock || '?') + ', patched ' + s.patched + ')' + (s.readyFlag !== undefined ? ', ready flag +0x' + s.readyFlag.toString(16) : '')); + }); + [[backupPath(t.target), 'backup'], [t.target + '.woc-running', 'running']].forEach(function (x) { + var b = readOr(x[0]); + line(x[1], b ? x[0] + ' sha256 ' + sha256(b) + ', build-id ' + (buildId(b) || 'none') : 'none'); + }); + var rec = null; + try { rec = fs.readFileSync(record.recordPath(t.target), 'utf8').trim(); } catch (e) {} + line('record', rec || 'none'); +} + function cmdApply(t, dpi, card, yes, unproven) { var hu = headUnit.refused(); if (hu) die('refusing: ' + hu); @@ -371,7 +414,7 @@ function cmdApply(t, dpi, card, yes, unproven) { die('refusing: the edited aapd does not verify; no change made'); var outConf = Buffer.from(conf.transform(origText.toString('utf8'), dpi, card), 'utf8'); var binChange = !outBin.equals(buf), confChange = !outConf.equals(text); - var rec = { patched: true, proven: !!PROVEN[c.loc.buildId] }; + var rec = { patched: true, proven: !!PROVEN[c.loc.buildId], rev: PATCH_REV }; if (!binChange && !confChange) { repairModes([[t.target, fileMode.EXEC], [t.conf, fileMode.READ]], yes); process.stdout.write('nothing to do; already in the requested state\n'); record.ensure(t.target, rec); return; @@ -441,11 +484,12 @@ function main() { } var t = { target: a.target || '/usr/bin/aapd', conf: a.conf || '/etc/aapd.conf', coding: a.coding || '/dev/shm/rawcodingdata' }; if (a._[0] === 'status') { cmdStatus(t); line('end', 'complete'); } + else if (a._[0] === 'probe') cmdProbe(t, a._[1]); else if (a._[0] === 'apply') cmdApply(t, a.dpi === undefined ? conf.DEFAULT_DPI : Number(a.dpi), a.card || conf.DEFAULT_CARD, a.yes, a.unproven); else if (a._[0] === 'revert') cmdRevert(t, a.yes, a.force, a['drop-backup']); - else { process.stderr.write('usage: aapd_patch.js status|apply|revert [--target ] [--conf ] [--coding ] [--dpi ] [--card shown|hidden] [--unproven] [--yes] [--force] [--drop-backup]\n'); process.exit(2); } + else { process.stderr.write('usage: aapd_patch.js status|probe []|apply|revert [--target ] [--conf ] [--coding ] [--dpi ] [--card shown|hidden] [--unproven] [--yes] [--force] [--drop-backup]\n'); process.exit(2); } } -module.exports = { CODING: CODING, PROVEN: PROVEN, trusted: trusted, binOther: binOther, readCoding: readCoding, codingRefusal: codingRefusal }; +module.exports = { CODING: CODING, PROVEN: PROVEN, PATCH_REV: PATCH_REV, trusted: trusted, binOther: binOther, readCoding: readCoding, codingRefusal: codingRefusal }; if (require.main === module) main(); diff --git a/native/waze-on-cluster/patch/journal_keep.js b/native/waze-on-cluster/patch/journal_keep.js index b5d91922..afcf332d 100644 --- a/native/waze-on-cluster/patch/journal_keep.js +++ b/native/waze-on-cluster/patch/journal_keep.js @@ -18,7 +18,7 @@ * the file system's own commit makes them durable within seconds. * A file that would pass FILE_MAX becomes journal.log.1, the older * ones move up and journal.log. is dropped. The tools' lines - * (waze-on-cluster, cluster-render) and the others (carplay, recmgr, + * (waze-on-cluster, cluster-render) and the others (carplay, aapd, recmgr, * crashhandler, systemd, the kernel) each have RATE / 2 bytes a minute, with a * minute's worth in reserve, so a flood of one kind never pushes out the * other; what comes faster is dropped and counted in one line per flush. @@ -83,8 +83,8 @@ var CLK_TCK = 100; var UNITS = ['waze-on-cluster.service', 'waze-on-cluster-patchd.service', 'waze-on-cluster-boot.service', 'waze-on-cluster-recovery.service', 'waze-on-cluster-capture.service', 'waze-on-cluster-journal.service', - 'waze-on-cluster-logs.service', 'waze-on-cluster-logs-fw.service', 'cluster-render.service', 'carplay.service', 'recmgr.service', - 'crashhandler.service']; + 'waze-on-cluster-logs.service', 'waze-on-cluster-logs-fw.service', 'waze-on-cluster-aa-boot.service', 'cluster-render.service', 'carplay.service', + 'aapd.service', 'recmgr.service', 'crashhandler.service']; /* units with no process of their own: only systemd's lines about them */ var QUIET = ['waze-on-cluster.socket', 'waze-on-cluster-logs.socket', 'cluster-render.socket', 'waze-on-cluster-journal.timer']; @@ -102,7 +102,7 @@ function journalArgs(follow) { /* Lines of the units that explain nothing, by message. */ var NOISE = [ - /^\[\w+\] (Intercepting function|Blocked function|Notice in function: findThreadByName)/, // carplay's libschedconfig + /^\[\w+\] (Intercepting function|Blocked function|Notice in function: findThreadByName)/, // carplay's and aapd's libschedconfig /^Coding base: Shm object data is available/, /^\s*ipcLib:/, // the crash handler's IPC /^DLT ~(FIFO .* cannot be opened|Logging \(re-\)enabled|Buffer: )/, diff --git a/native/waze-on-cluster/patch/logs_extra.js b/native/waze-on-cluster/patch/logs_extra.js index a05288cf..b5627ba4 100644 --- a/native/waze-on-cluster/patch/logs_extra.js +++ b/native/waze-on-cluster/patch/logs_extra.js @@ -47,6 +47,10 @@ * the crash handler and the kernel keep of crashes * (DUMPS, two levels deep, at most ENTRIES each), * never their contents + * aapd-crashes.txt of the crash handler's records of aapd's newest + * CONTEXTS crashes, where it crashed: signal, + * kernel stack, executable mappings, crash ids and + * summary (aapdCrashesText) * * node logs_extra.js patchd * patchd-logs.json: patchd's carplay, marker and AVB lines, its DLT @@ -102,6 +106,8 @@ var INCIDENTS = 2000; var SCRUB_MS = Number(process.env.WOC_SCRUB_MS) || 60000; var SCRUB_FILE_MAX = 8 * 1024 * 1024; var CODING = '/dev/shm/rawcodingdata'; +var CONTEXTS = 3; +var CONTEXT_MAX = 4 * 1024 * 1024; var DUMPS = [SIN + '/crashhandler/coredumps', SIN + '/crashhandler/gpu_hangs', SIN + '/gcd', SIN + '/dlt-cdh', '/var/opt/kerneldump']; function r(p) { return ROOT + p; } @@ -264,12 +270,45 @@ function dumpsText() { return out.join('\n') + '\n'; } +/* The crash handler's record of aapd's newest CONTEXTS crashes + * (dlt-cdh/context.