From 076f23376bff6e7241b5f30ae2a5696a2f393382 Mon Sep 17 00:00:00 2001 From: levkropp <51499221+levkropp@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:40:41 -0500 Subject: [PATCH 1/3] Build NVS key storage cleanly under GCC 14 GCC 14.2 at -O3 reports -Werror=stringop-truncation at nvs_alloc's strncpy of the key, which stops a default build. The warning is a false positive: the caller's key is at most 15 characters and the field holds 16. snprintf states the same bounded, terminated copy without tripping it. Co-Authored-By: Claude Opus 5.5 --- src/rom_stubs.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/rom_stubs.c b/src/rom_stubs.c index d3ef7a3..9805ba9 100644 --- a/src/rom_stubs.c +++ b/src/rom_stubs.c @@ -2059,10 +2059,8 @@ static struct nvs_kv_entry *nvs_alloc(esp32_rom_stubs_t *s, const char *ns, for (int i = 0; i < 64; i++) { if (!s->nvs_kv[i].used) { s->nvs_kv[i].used = 1; - strncpy(s->nvs_kv[i].ns, ns, sizeof(s->nvs_kv[i].ns) - 1); - s->nvs_kv[i].ns[sizeof(s->nvs_kv[i].ns) - 1] = '\0'; - strncpy(s->nvs_kv[i].key, key, sizeof(s->nvs_kv[i].key) - 1); - s->nvs_kv[i].key[sizeof(s->nvs_kv[i].key) - 1] = '\0'; + snprintf(s->nvs_kv[i].ns, sizeof(s->nvs_kv[i].ns), "%s", ns); + snprintf(s->nvs_kv[i].key, sizeof(s->nvs_kv[i].key), "%s", key); return &s->nvs_kv[i]; } } From 390914885e7612da228e1be9f4e9c31d970b335a Mon Sep 17 00:00:00 2001 From: levkropp <51499221+levkropp@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:40:41 -0500 Subject: [PATCH 2/3] Invalidate the icache after JIT writes on Linux ARM64 ARM64 instruction caches are not coherent with data writes, but only the Apple path invalidated after emitting a block or patching a chain site. On Linux ARM64 the CPU could execute stale bytes, and the unit suite died with SIGILL at the first test that ran compiled code. Route both mutation paths through one helper that uses __builtin___clear_cache off Apple. Co-Authored-By: Claude Opus 5.5 --- src/jit.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/jit.c b/src/jit.c index 38c1212..985d5b4 100644 --- a/src/jit.c +++ b/src/jit.c @@ -23,11 +23,21 @@ # define JIT_NEEDS_WX 1 #endif -/* ===== W^X + icache management (Apple Silicon) ===== - * macOS on ARM64 enforces W^X on JIT pages: write mode and execute mode - * are toggled per-thread via pthread_jit_write_protect_np(), and the - * icache must be invalidated after any code mutation (block emission, - * chain patching). On x86 these are no-ops. */ +/* ===== W^X + icache management ===== + * ARM64 instruction caches are not coherent with data writes, so the icache + * must be invalidated after any code mutation (block emission, chain + * patching) on every ARM64 OS. macOS also enforces W^X on JIT pages: write + * mode and execute mode are toggled per-thread via + * pthread_jit_write_protect_np(). On x86 these are no-ops. */ +static inline void jit_icache_invalidate(void *start, size_t len) { +#if defined(JIT_NEEDS_WX) + sys_icache_invalidate(start, len); +#elif defined(JIT_ARCH_ARM64) + __builtin___clear_cache((char *)start, (char *)start + len); +#else + (void)start; (void)len; +#endif +} static inline void jit_wx_write_begin(void) { #if defined(JIT_NEEDS_WX) pthread_jit_write_protect_np(0); @@ -36,10 +46,8 @@ static inline void jit_wx_write_begin(void) { static inline void jit_wx_write_end(void *start, size_t len) { #if defined(JIT_NEEDS_WX) pthread_jit_write_protect_np(1); - sys_icache_invalidate(start, len); -#else - (void)start; (void)len; #endif + jit_icache_invalidate(start, len); } /* ===== CPU struct field offsets (computed from xtensa_cpu_t layout) ===== */ @@ -3582,9 +3590,7 @@ static void jit_patch_chain_site(uint8_t *jmp_site, uint8_t *target) { int64_t byte_delta = (int64_t)(target - jmp_site); uint32_t insn = arm64_encode_b((int32_t)(byte_delta / 4)); memcpy(jmp_site, &insn, 4); -# if defined(JIT_NEEDS_WX) - sys_icache_invalidate(jmp_site, 4); -# endif + jit_icache_invalidate(jmp_site, 4); #else int32_t rel = (int32_t)(target - (jmp_site + 5)); memcpy(jmp_site + 1, &rel, 4); From ab9c4003345a32cfe86db483a60f15f6b0282df9 Mon Sep 17 00:00:00 2001 From: levkropp <51499221+levkropp@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:40:41 -0500 Subject: [PATCH 3/3] Run the unit suite natively on Linux ARM64 in CI CI only cross-compiled the arm64 JIT backend, so code that built but crashed at runtime went unnoticed. Add a job on an ARM64 runner with gcc-14, which also covers warnings the default gcc does not raise. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 42 ++++++++++++++++++++++++++++++++++------ docs/testing.md | 5 +++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 691ec6d..24c9b94 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -95,12 +95,42 @@ jobs: FLEXE_CLI_BINARY: build-asan/xtensa-emu run: python3 tests/test_cli_trace.py - # The JIT has two backends and only one of them is exercised by a host - # build. A native aarch64 runner would be better, but a cross-compile at - # least catches the common failure: adding an emitter helper on one side - # and calling it from shared code. openssl-dependent sources are skipped - # because the cross toolchain has no aarch64 openssl headers; none of them - # contain backend-specific code. + # The JIT has two backends and an x86-64 host build only runs one of them. + # This job executes the arm64 backend on Linux, where the cache maintenance + # differs from macOS. gcc-14 also warns about things the default gcc does not. + unit-aarch64: + name: unit tests (aarch64, gcc-14) + runs-on: ubuntu-24.04-arm + steps: + - uses: actions/checkout@v4 + - name: Install build dependencies + run: | + sudo apt-get update + sudo apt-get install -y cmake ninja-build ccache libssl-dev zlib1g-dev gcc-14 + - name: Restore compiler cache + uses: actions/cache@v4 + with: + path: ~/.cache/ccache + key: ccache-v1-unit-aarch64-${{ runner.os }}-${{ hashFiles('.github/workflows/ci.yml', 'CMakeLists.txt', 'src/**', 'tests/**') }} + restore-keys: | + ccache-v1-unit-aarch64-${{ runner.os }}- + - name: Configure + run: | + cmake -S . -B build -G Ninja \ + -DCMAKE_C_COMPILER=gcc-14 \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DNATIVE_ARCH=OFF -DFLEXE_LTO=OFF + - name: Build + run: cmake --build build --target xtensa-emu xtensa-tests flexe-agon-vdp-test -j"$(nproc)" + - name: Unit tests + run: ./scripts/run-unit-tests.sh + - name: CLI trace regressions + run: python3 tests/test_cli_trace.py + + # Compiles on an x86-64 host, which catches the common failure quickly: + # adding an emitter helper on one side and calling it from shared code. + # openssl-dependent sources are skipped because the cross toolchain has no + # aarch64 openssl headers; none of them contain backend-specific code. cross-aarch64: name: aarch64 cross-compile runs-on: ubuntu-latest diff --git a/docs/testing.md b/docs/testing.md index 7bac66a..0505cc6 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -513,7 +513,8 @@ Use `-T` to write a verbose emulator trace, then narrow it with ./build/trace-filter -p trace.log # panic/abort paths ``` -CI builds with GCC and Clang, runs ASan+UBSan, cross-compiles backend-relevant -sources for AArch64, and executes the compiled-firmware gate set. Correctness +CI builds with GCC and Clang, runs ASan+UBSan, runs the unit suite natively on +Linux AArch64 with GCC 14, cross-compiles backend-relevant sources for AArch64, +and executes the compiled-firmware gate set. Correctness jobs disable expensive whole-program linking; normal optimized emulator builds retain LTO by default.