From 0db9865045f63656842d8e5534bdbf9f04327b9f Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sun, 8 Nov 2026 19:26:26 -0500 Subject: [PATCH] fix: validate the CASP pair on a mismatch in the LSE emulator --- docs/arm-v8.0-steam-client.md | 29 ++++++++++++++++--- tools/linuxfs/lse-emu/README.md | 7 +++-- tools/linuxfs/lse-emu/lse_emu.c | 16 ++++++---- .../overlay/usr/local/bin/droiddeck-session | 2 +- tools/tests/lse_emu_test.c | 3 ++ tools/tests/test_lse_emu.py | 15 +++++++--- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/docs/arm-v8.0-steam-client.md b/docs/arm-v8.0-steam-client.md index b4240f4bf..8188ff725 100644 --- a/docs/arm-v8.0-steam-client.md +++ b/docs/arm-v8.0-steam-client.md @@ -77,13 +77,34 @@ refused, every site logs `mprotect-failed`, and execution stays on the slower tr - without the library the program dies with SIGILL - trapping alone runs every form and keeps the threaded counters exact -- patching runs every form and patches every site, single threaded +- patching runs every form twice, so the second run goes through the generated stubs, both + single threaded and with four threads +- condition flags survive a patched `ldumax`/`ldsmaxb`, and `caspal` returns the current pair + on a mismatch - without `LSE_EMU=1`, and on `-cpu neoverse-n1`, the library stays out of the way - a SIGILL handler the program installs itself still runs for an unrelated `udf` -Threaded patching segfaults under qemu-user, writing to the stub arena, which is mapped and -writable. qemu write-protects pages it has translated code from, and that races when other -threads run code on them. It needs checking on real hardware. +The patch tests cannot run on an Apple Silicon Mac in an amd64 Docker container: qemu-aarch64 +there segfaults whenever code is written to an executable page after code on it has run, even +in a ten line program without the library. CI runs them on x86 hardware. + +## Fixes over upstream + +The vendored copy carries two fixes found in review, both in the patched stubs and the +emulation path: + +- `LDSMAX`/`LDSMIN`/`LDUMAX`/`LDUMIN` stubs used `cmp`/`csel` and overwrote NZCV. They now + save and restore it. +- `CASP` with a 64-bit pair returned the `ldxp` result on a mismatch without a store. A 128-bit + `ldxp` is only single-copy atomic once a `stxp` succeeds, so a concurrent update could give + one old and one new half. Both the trap path and the stub now store the loaded pair back on a + mismatch and retry if that store fails, as libatomic does. + +Patching also replaces an LSE instruction with a `b` while other threads may be running it. +The architecture only promises old-or-new behavior for such changes between a few instruction +types, and `LDADD` is not one of them. Upstream accepts that and reports it working on a +Cortex-A57. If it ever shows up as random crashes on a device, running with `LSE_EMU_PATCH=0` +keeps every site on the trap path at the cost of speed. ## Testing on a device diff --git a/tools/linuxfs/lse-emu/README.md b/tools/linuxfs/lse-emu/README.md index 90db0de80..a4c18f502 100644 --- a/tools/linuxfs/lse-emu/README.md +++ b/tools/linuxfs/lse-emu/README.md @@ -1,9 +1,10 @@ # LSE-Emu `lse_emu.c` is a copy of [SildurFX/LSE-Emu](https://github.com/SildurFX/LSE-Emu) at commit -`8bc9548ac6fd24a8d506b266b17ba8d0e71a9b01` (GPL-3.0-or-later) with one fix: the patched stub -for `LDSMAX`/`LDSMIN`/`LDUMAX`/`LDUMIN` saves and restores NZCV around its `cmp`/`csel`, since -the real instructions leave the flags alone. Carry that fix forward until upstream has it. +`8bc9548ac6fd24a8d506b266b17ba8d0e71a9b01` (GPL-3.0-or-later) with two fixes, listed in +`docs/arm-v8.0-steam-client.md` under "Fixes over upstream": NZCV kept across the min/max +stubs, and a store-back on `CASP` mismatches so the returned pair is atomic. Carry them forward +until upstream has them. It is an `LD_PRELOAD` library that emulates the ARMv8.1 LSE atomic instructions (and `LDAPR`) on ARMv8.0 CPUs. A SIGILL handler performs each trapped instruction with an LL/SC loop. With diff --git a/tools/linuxfs/lse-emu/lse_emu.c b/tools/linuxfs/lse-emu/lse_emu.c index e891a137a..d1a8f515e 100644 --- a/tools/linuxfs/lse-emu/lse_emu.c +++ b/tools/linuxfs/lse-emu/lse_emu.c @@ -90,7 +90,10 @@ static void casp128(void *p, uint64_t *o0, uint64_t *o1, " cbnz %2, 2f\n" " stlxp %w2, %9, %10, [%6]\n" " cbnz %w2, 1b\n" - "2: clrex\n" + " b 3f\n" + "2: stlxp %w2, %0, %1, [%6]\n" // a 128-bit ldxp is only single-copy atomic once a stxp succeeds + " cbnz %w2, 1b\n" + "3:\n" : "=&r"(a), "=&r"(b), "=&r"(t), "=&r"(s) : "0"(0), "1"(0), "r"(p), "r"(e0), "r"(e1), "r"(d0), "r"(d1) : "memory", "cc"); @@ -180,7 +183,7 @@ static uint32_t alu(unsigned op, unsigned d, unsigned n, unsigned m) { return base[op] | (m << 16) | (n << 5) | d; // add, bic, eor, orr } -// CASP{A}{L} Xs,Xs+1,Xt,Xt+1,[Xn] (64-bit pair): ldxp/ldaxp + compare + stxp/stlxp. Four scratch registers, no flag changes, 15 words. +// CASP{A}{L} Xs,Xs+1,Xt,Xt+1,[Xn] (64-bit pair): ldxp/ldaxp + compare + stxp/stlxp. Four scratch registers, no flag changes, 16 words. static int gen_casp(uint32_t *w, uintptr_t stub, uintptr_t ret, unsigned sz, unsigned rs, unsigned rt, unsigned rn, unsigned acq, unsigned rel) { // sz 1: 64-bit pair (LDXP x), sz 0: 32-bit pair (LDXP w, 8-byte aligned) @@ -198,10 +201,11 @@ static int gen_casp(uint32_t *w, uintptr_t stub, uintptr_t ret, unsigned sz, W(EOR | (rs << 16) | (t0 << 5) | t2); // eor t2,t0,rs W(EOR | ((rs + 1) << 16) | (t1 << 5) | t3); // eor t3,t1,rs+1 W(ORR | (t3 << 16) | (t2 << 5) | t2); // orr t2,t2,t3 - W(0xB5000000u | (3u << 5) | t2); // cbnz t2, clrex (+3) - W(STXP | (t2 << 16) | (rel << 15) | ((rt + 1) << 10) | (rn << 5) | rt); // stxp/stlxp w(t2),rt,rt+1,[rn] - W(0x35000000u | (((unsigned)(loop - n) & 0x7FFFF) << 5) | t2); // cbnz w(t2), loop - W(0xD5033F5Fu); // clrex + W(0xB4000000u | (3u << 5) | t2); // cbz t2, match (+3) + W(STXP | (t2 << 16) | (rel << 15) | (t1 << 10) | (rn << 5) | t0); // mismatch: store the loaded pair back, which validates it + W(0x14000002u); // b check (+2) + W(STXP | (t2 << 16) | (rel << 15) | ((rt + 1) << 10) | (rn << 5) | rt); // match: stxp/stlxp w(t2),rt,rt+1,[rn] + W(0x35000000u | (((unsigned)(loop - n) & 0x7FFFF) << 5) | t2); // check: cbnz w(t2), loop W(MOVX(rs, t0)); W(MOVX(rs + 1, t1)); W(0xA9410000u | (t3 << 10) | (31u << 5) | t2); // ldp t2,t3,[sp,#16] diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index c68d740aa..b09e9a964 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -12,7 +12,7 @@ if [ "${BL_RAISE_NOFILE:-0}" = 1 ] && [ -z "${BL_INSIDE:-}" ]; then fi export HOME=${HOME:-/root} if grep -qs '/liblse_emu\.so$' /etc/ld.so.preload; then - export LSE_EMU=1 LSE_EMU_PATCH=1 + export LSE_EMU=1 LSE_EMU_PATCH=${LSE_EMU_PATCH:-1} [ -n "${BL_DEBUG_DIR:-}" ] && export LSE_EMU_LOG=${LSE_EMU_LOG:-$BL_DEBUG_DIR/lse_emu.log} fi mode=${1:-desktop} diff --git a/tools/tests/lse_emu_test.c b/tools/tests/lse_emu_test.c index 402dc1fca..abcf931ae 100644 --- a/tools/tests/lse_emu_test.c +++ b/tools/tests/lse_emu_test.c @@ -99,6 +99,9 @@ static __attribute__((noinline)) void single_forms(void) { register uint64_t e0 __asm__("x4") = 1, e1 __asm__("x5") = 2, d0 __asm__("x6") = 3, d1 __asm__("x7") = 4; __asm__ volatile("caspal x4, x5, x6, x7, [%4]" : "+r"(e0), "+r"(e1) : "r"(d0), "r"(d1), "r"(pair) : "memory"); check(e0 == 1 && e1 == 2 && pair[0] == 3 && pair[1] == 4, "caspal"); + register uint64_t m0 __asm__("x4") = 8, m1 __asm__("x5") = 9, n0 __asm__("x6") = 5, n1 __asm__("x7") = 6; + __asm__ volatile("caspal x4, x5, x6, x7, [%4]" : "+r"(m0), "+r"(m1) : "r"(n0), "r"(n1), "r"(pair) : "memory"); + check(m0 == 3 && m1 == 4 && pair[0] == 3 && pair[1] == 4, "caspal returns the current pair on a mismatch"); uint64_t missed = casal64(&v, 99, 1); check(missed == 7 && v == 7, "casal leaves memory alone on a mismatch"); diff --git a/tools/tests/test_lse_emu.py b/tools/tests/test_lse_emu.py index c35f3c776..97cded498 100644 --- a/tools/tests/test_lse_emu.py +++ b/tools/tests/test_lse_emu.py @@ -1,3 +1,4 @@ +import os from pathlib import Path import shutil import subprocess @@ -11,6 +12,8 @@ SYSROOT = Path("/usr/aarch64-linux-gnu") def toolchain(): if not (shutil.which("aarch64-linux-gnu-gcc") and shutil.which("qemu-aarch64") and SYSROOT.is_dir()): + if os.environ.get("CI"): + raise AssertionError("CI must install gcc-aarch64-linux-gnu, libc6-dev-arm64-cross and qemu-user") raise unittest.SkipTest("needs aarch64-linux-gnu-gcc, libc6-dev-arm64-cross and qemu-user") @@ -46,15 +49,19 @@ class LseEmuTest(unittest.TestCase): self.assertIn("lse ok", result.stdout) self.assertRegex(result.stderr, r"traps=[1-9]\d* patched=0 ") - def test_patching_runs_every_form_on_an_armv8_0_cpu(self): - # qemu-user write-protects translated code pages and races when other threads run them while the - # stub arena is written, so threaded patching is only meaningful on hardware. + def patched(self, threads): result = self.run_on("cortex-a57", f"LD_PRELOAD={self.library}", "LSE_EMU=1", "LSE_EMU_PATCH=1", - "LSE_EMU_STATS=1", threads=1) + "LSE_EMU_STATS=1", threads=threads) self.assertEqual(result.returncode, 0, result.stdout + result.stderr) self.assertIn("lse ok", result.stdout) self.assertRegex(result.stderr, r"patched=[1-9]\d* failed=0") + def test_patching_runs_every_form_on_an_armv8_0_cpu(self): + self.patched(threads=1) + + def test_patching_stays_atomic_across_threads(self): + self.patched(threads=4) + def test_it_stays_off_without_the_switch(self): result = self.run_on("cortex-a57", f"LD_PRELOAD={self.library}") self.assertEqual(result.returncode, -4, result.stdout + result.stderr)