Skip to content

Conversation

jserv
Copy link
Contributor

@jserv jserv commented Oct 5, 2025

ThreadSanitizer (TSAN) can now detect race conditions across the entire multi-threaded JIT pipeline with full 4GB address space emulation. This enables testing of the tier-2 LLVM compilation thread while maintaining production memory layout.

Memory Layout (TSAN-compatible):

  • Main memory: MAP_FIXED at 0x7d0000000000 (4GB)
  • JIT buffer: MAP_FIXED at 0x7d1000000000
  • Both allocations within TSAN app range (0x7cf-0x7ff trillion)
  • Prevents conflicts with TSAN shadow memory (0x02a-0x7ce trillion)

ASLR Mitigation:

  • Added setarch -R wrapper for TSAN test execution
  • Disables ASLR to prevent random allocations in shadow memory
  • Only affects test runs, not production builds

SDL Conflict Resolution:

  • SDL (uninstrumented system library) creates threads TSAN cannot track
  • Disabled SDL when TSAN enabled to focus on built-in race detection
  • Production builds still fully support SDL

Summary by cubic

Enables ThreadSanitizer across the multi-threaded JIT pipeline with FULL4G memory emulation, including tier-2 compilation thread support. Uses fixed mappings and ASLR-disabled test runs to avoid TSAN shadow conflicts while keeping the production layout.

  • New Features

    • TSAN build mode with -fsanitize=thread; tests run via setarch -R; SDL and LTO disabled only when TSAN is enabled.
    • Fixed x86-64 mappings for FULL4G memory (0x7d0000000000) and JIT buffer (0x7d1000000000) using MAP_FIXED within TSAN’s app range.
    • TSAN runtime defaults added to reduce noise and keep tests running after reports.
  • Bug Fixes

    • Correct race handling in T2C: atomic load/store for hot2, compiled, n_invoke; acquire/release semantics around func/hot2.
    • Background compiler uses a condition variable and cache key lookup to avoid busy waits and use-after-free; signals on enqueue and shutdown with SATP verification.

ThreadSanitizer (TSAN) can now detect race conditions across the entire
multi-threaded JIT pipeline with full 4GB address space emulation. This
enables testing of the tier-2 LLVM compilation thread while maintaining
production memory layout.

Memory Layout (TSAN-compatible):
- Main memory: MAP_FIXED at 0x7d0000000000 (4GB)
- JIT buffer: MAP_FIXED at 0x7d1000000000
- Both allocations within TSAN app range (0x7cf-0x7ff trillion)
- Prevents conflicts with TSAN shadow memory (0x02a-0x7ce trillion)

ASLR Mitigation:
- Added setarch -R wrapper for TSAN test execution
- Disables ASLR to prevent random allocations in shadow memory
- Only affects test runs, not production builds

SDL Conflict Resolution:
- SDL (uninstrumented system library) creates threads TSAN cannot track
- Disabled SDL when TSAN enabled to focus on built-in race detection
- Production builds still fully support SDL
Copy link
Contributor Author

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Benchmark suite Current: 2497867 Previous: 2149996 Ratio
Dhrystone 1328 Average DMIPS over 10 runs 1344 Average DMIPS over 10 runs 1.01
Coremark 961.712 Average iterations/sec over 10 runs 962.342 Average iterations/sec over 10 runs 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 8 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="src/jit.c">

<violation number="1" location="src/jit.c:2350">
When TSAN is enabled on macOS x86_64, this mmap call loses the MAP_JIT flag that the standard path uses, so hardened macOS failures return MAP_FAILED and the JIT never initializes. Please keep MAP_JIT on macOS even in the TSAN path.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

* 0x7ffffffff000) and prevents overlap with main memory or TSAN shadow.
*/
void *jit_addr = (void *) 0x7d1000000000UL;
state->buf = mmap(jit_addr, size, PROT_READ | PROT_WRITE | PROT_EXEC,
Copy link

@cubic-dev-ai cubic-dev-ai bot Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When TSAN is enabled on macOS x86_64, this mmap call loses the MAP_JIT flag that the standard path uses, so hardened macOS failures return MAP_FAILED and the JIT never initializes. Please keep MAP_JIT on macOS even in the TSAN path.

Prompt for AI agents
Address the following comment on src/jit.c at line 2350:

<comment>When TSAN is enabled on macOS x86_64, this mmap call loses the MAP_JIT flag that the standard path uses, so hardened macOS failures return MAP_FAILED and the JIT never initializes. Please keep MAP_JIT on macOS even in the TSAN path.</comment>

<file context>
@@ -2336,6 +2336,25 @@ struct jit_state *jit_state_init(size_t size)
+     * 0x7ffffffff000) and prevents overlap with main memory or TSAN shadow.
+     */
+    void *jit_addr = (void *) 0x7d1000000000UL;
+    state-&gt;buf = mmap(jit_addr, size, PROT_READ | PROT_WRITE | PROT_EXEC,
+                      MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+    if (state-&gt;buf == MAP_FAILED) {
</file context>

✅ Addressed in f915bc2

@jserv jserv changed the title Enable TSAN with FULL4G and T2C support Enable ThreadSanitizer across the entire multi-threaded JIT pipeline Oct 5, 2025
@jserv jserv force-pushed the fix-regressions branch 6 times, most recently from 558e438 to 132db90 Compare October 8, 2025 10:34
This commit adds ThreadSanitizer (TSAN) support for ARM64/Apple Silicon
and fixes critical JIT instruction cache coherency issues.

ARM64 TSAN Support:
- Extended TSAN-compatible memory allocation to ARM64 architecture
- Main memory allocated at fixed address 0x150000000000 (21TB)
- JIT buffer allocated at 0x151000000000 with MAP_JIT for Apple Silicon
- Both allocations avoid TSAN shadow memory and enable race detection
- Note: Requires ASLR disabled on macOS (SIP restrictions may apply)

JIT Cache Coherency Fixes:
1. Fixed pthread_jit_write_protect_np() ordering in update_branch_imm
2. Added sys_icache_invalidate() in update_branch_imm
3. Added cache invalidation in resolve_jumps() for x86_64

Fix JIT regalloc conflicts in memory load

After reset_reg() clears the register allocator state, load instructions
(lb/lh/lw/lbu/lhu) could reallocate the same host register for both the
address and destination, causing data corruption. This commit uses
map_vm_reg_reserved() to prevent reusing the address register.
This commit introduces a comprehensive JIT debugging infrastructure to
catch register allocation conflicts and cache coherency issues before
they cause subtle runtime failures in production.
jserv added 3 commits October 8, 2025 22:20
User-space emulation tests were failing because ENABLE_ELF_LOADER
defaulted to 0, preventing ELF file loading. The fix automatically
enables ELF_LOADER when SYSTEM=0, as user-space mode always requires
it to load test ELF files.
User-space emulation tests were failing because ENABLE_ELF_LOADER
defaulted to 0, preventing ELF file loading. The fix automatically
enables ELF_LOADER when SYSTEM=0, except for architecture tests which
have their own binary loading mechanism.
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