Repository navigation
Fix JIT silently dropping unsupported opcodes (port/proto filters wrong on Apple Silicon) - #10
Merged
Merged
Conversation
…ng on Apple Silicon) Both JIT backends had opcode paths that fabricated a result instead of declining to compile. The arm64 compiler implements only 8 opcodes and every other opcode hit a single default that emitted a truncated 16-bit MOVZ of -1 (garbage return, not (uint64_t)-1); the x86-64 default emitted mov RAX,0 (silent DROP). These are reachable from ordinary filters -- (= proto 6) compiles to LD8, (= dst-port 443) and >=/<= (JGE/JLE) hit the defaults -- so such filters silently matched zero or garbage under the default JIT, worst on Apple Silicon where every non-trivial program is affected. Make both backends bail to the interpreter on any uncompilable opcode, generalizing the merged x86-64 stack-overflow precedent: free partial state and return NULL from the compile function so vfm_load_program leaves vm->cold.jit_code NULL and vfm_execute runs the correct bounds-checked interpreter. Cleanup per backend: arm64 uses free_scheduler (discard buffered instructions, not flush) + pthread_jit_write_protect_np(1) under #ifdef __APPLE__ (re-enables the W^X protection the bail would otherwise skip) + munmap(code,4096); x86-64 uses free(jit.labels)+munmap and returns NULL directly (not goto done, which would double-free and run a stray mprotect). Also convert the x86-64 VFM_IPV6_EXT case from the fragile runtime -1 convention to the same compile-time bail. Fix a latent test bug (test/jit_test.c used magic byte 0x16 = VFM_JLT as a stand-in for RET, which is 0x17). Add two regression tests: the JIT declines an unsupported opcode, and a dst-port filter matches nonzero under the default build (asserting on arm64 that the JIT declined so the interpreter produced the match). Suite is now 16. The adaptive multicore compilers (vfm_jit_compile_*_adaptive) share the same silent-wrong-opcode shape but are deliberately NOT changed here: a naive NULL-bail there is a use-after-free in adaptive_jit_recompile (it frees and nulls shared->jit_code before recompiling while cores still reference the prior result), and they are unreachable via vfm_load_program, so this fix causes no regression. Tracked as a separate follow-up. Verified 16/16 on macOS/arm64 (JIT entitlement active, real bail path exercised) and Docker linux/amd64 (x86-64 JIT).
MonkeyIsNull
added a commit
that referenced
this pull request
Oct 8, 2026
…orrect JIT ABI, safe recompile
The multicore adaptive JIT was unfinished and unsafe: the worker never executed JIT (an int result=1 placeholder), vfm_multicore_execute_batch deadlocked (per-batch create-then-join on workers that never terminate), the adaptive emitters used a divergent/wrong ABI and emitted silent nops for unimplemented opcodes, and adaptive_jit_recompile freed the live JIT page before recompiling (use-after-free if a recompile declined). Investigation also showed the single-core ARM64 JIT had never actually been executed by any test (jit_test skipped execution and only checked the compile pointer; test_vfm only asserted interpreter fallback), and its execution was in fact broken: the compiled code treated X0 as a VM-state struct while callers pass X0=packet, PUSH truncated immediates to 16 bits, and stack addressing used fixed offsets that only worked for a single PUSH.
Single-core first, oracle-validated: define one canonical JIT ABI on both arches -- uint64_t fn(const uint8_t *packet, uint16_t len, uint64_t *stack64, vfm_u128_t *stack128) -- removing all dependence on struct layout so a single read-execute page is valid across threads. Rewrite the ARM64 emitters to index-based stack addressing and full 64-bit immediates (movz/movk), retarget the packet-reading emitters to X0, and gate each emitter against the interpreter oracle, demoting any that cannot be proven (trusted ARM64 set: PUSH/ADD/RET). Unify vfm_execute so the JIT and interpreter return senses agree.
Multicore: back each core with its own interpreter-only vfm_state_t that borrows the shared compiled page (single owner, freed exactly once); the worker runs vfm_execute per packet (JIT when the page is present, per-core interpreter fallback otherwise). Replace the deadlocking per-batch create/join with a persistent worker pool plus a generation/completed_count barrier under a pool mutex. Recompile uses compile-into-temp + swap-on-success, never freeing the live page on failure so cores stay valid. The multicore path JITs only the oracle-validated {PUSH, ADD, RET} set on both arches, declining everything else to the interpreter.
Blocking use-after-free fixed: the single-core JIT cache teardown (vfm_destroy and the vfm_load_program reload path) nulled jit_cache_entry before testing it, so the free guard always held and vfm_jit_free munmap'd a cache-owned page, leaving the cache bucket with a dangling pointer that a later same-hash lookup would execute. The new ABI made single-core JIT execution reachable for the first time, exposing this. Capture cache ownership before releasing and free only non-cache-owned pages; also munmap a freshly compiled page when vfm_jit_cache_store fails (leak). Added a permanent ASan regression (jit_test cache-teardown case + a test-jit-asan target).
Tests: jit_test now EXECUTES the single-core JIT and asserts JIT == interpreter oracle (PUSH/ADD/RET, >0xFFFF immediates, LD16 decline, cache-teardown UAF). New multicore suite: 126 correctness cases (6 programs x {1,2,8} cores x batch sizes incl. remainders/empty) vs the oracle, recompile swap, declined-recompile UAF regression, per-instance counters, a 500-batch no-deadlock test (timeout-guarded so a hang fails), and program lifetime. ASan and TSan build targets added.
Known follow-up, NOT in scope here: the single-core x86-64 LD8 emitter is itself wrong (MOV r8 + a 64-bit self-move leaves the high bits garbage; it needs MOVZX), the x86 analogue of the ARM64 bug fixed in PR #10. The multicore path already declines it to the interpreter, but single-core x86 JIT consumers remain affected. Tracked separately.
Verified on macOS/arm64 (16/16 + JIT-vs-oracle execution + 126 multicore cases + recompile + UAF regression + 500-batch no-deadlock + ASan clean + TSan clean) and Docker linux/amd64 (16/16 + multicore matches the oracle incl. proto_tcp + recompile + no-deadlock + ASan clean; TSan is not runnable under QEMU so it was run natively on macOS).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (silent wrong answer)
Both JIT backends had opcode paths that fabricated a result instead of declining to compile:
src/jit_arm64.c) implements only 8 opcodes; every other opcode hit a singledefaultthat emitted a truncated 16-bitMOVZof -1 — not even a clean(uint64_t)-1, just garbage in X0.src/jit_x86_64.c)defaultemittedmov RAX,0— a silent DROP.These are reachable from ordinary filters:
(= proto 6)->LD8,(= dst-port 443)and>=/<=(JGE/JLE) hit the defaults. So such filters silently matched zero/garbage under the default JIT, worst on Apple Silicon where essentially every non-trivial program is affected. Found while benchmarking PacketVelocity. The interpreter was always correct; only the JIT lied.Fix (correct-first: bail to the interpreter)
Generalizes the merged x86-64 stack-overflow precedent (#9): on any uncompilable opcode, free partial state and
return NULLsovfm_load_programleavesvm->cold.jit_code == NULLandvfm_executeruns the bounds-checked interpreter.free_scheduler(discard buffered instructions, notflush) +pthread_jit_write_protect_np(1)under#ifdef __APPLE__(re-enables the W^X protection the bail would otherwise skip — thread-global, mandatory) +munmap(code, 4096).free(jit.labels)+munmap, returningNULLdirectly (notgoto done, which would double-free and run a straymprotect). Also converts theVFM_IPV6_EXTcase from the fragile runtime -1 convention to the same compile-time bail.No half-emitted code can execute: both backends unmap the page and return NULL; nothing is stored in the VM or JIT cache.
Tests
test/jit_test.cused magic byte0x16(=VFM_JLT) as a stand-in forRET(0x17).dst-port 443filter matches nonzero under the default build — on arm64 it additionally initializes the JIT cache and assertsjit_code == NULL, proving the interpreter (not a stub) produced the match.Deliberately out of scope
The adaptive multicore compilers share the same shape but are not changed here: a naive NULL-bail there is a use-after-free in
adaptive_jit_recompile(it frees+nullsshared->jit_codebefore recompiling while cores still reference the prior result), and they're unreachable viavfm_load_program. Tracked as a follow-up.Verification
make clean && make && make test-> 16/16.make clean && make && make test-> 16/16.Produced via a plan -> 5-lens committee -> synthesize -> implement -> review -> fix cycle.