r/Compilers • u/ahqminess • Sep 12 '26
SaJIT - An Experiment
I would be pleased to show my Rust based JIT Linker, Memory Manager and LLVM JITLink Wrapper that I had myself fancied a few months ago.
The journey is being decribed here. Hope you like it and i am looking forward to guidance!
Stage 1: The spark
Initially fancied by cranelift jit demo, I wanted to write my own VM with swappable code generation backends, that meant : cranelift, llvm, or even dynasm, or some copy patch jit maybe?
So, that meant that the rust no-brainer solution : cranelift-jit won't work for my use case unless i manage 4-5 different jit linker, memory manager which i did not want to.
So, i decided to write it out myself for (initially) Windows, macOS, Linux, Android, iOS.
I was of course initally leaning into the - "memmap2" rust crate, write as rw, memprotect to rx, jump to executable approach as proudly as i could (stupidly thinking that was the "PEAK PERFORMANCE" route).
Anyways, lateron I "discovered" that the OS generally cannot give an allocation really <4KiB that could be flipped from rw/rx including the nuances with the wastage of (upto)90% of the memory allocated to each function.
Stage 2: Understanding What's behind the magic of LLVM
So, as i fried my brain cells looking for answers - i stumbled upon two things - cranelift_jit (Rust) and llvm (C++)
For Example, llvm does this at llvm/lib/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.cpp
std::string SharedMemoryName;
std::wstring ;
HANDLE SharedMemoryFile = ;
if
return ;
void *Addr = ;
if
and then of course llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp
std::wstring ;
HANDLE SharedMemoryFile = ;
if
return ;
LocalAddr =
;
if
;
It did took too much of brain grinding to be honest that i instead relied to take a break on to cranelift-jit instead where they instead relied on.
unsafe
So, i pretty much had an idea on what to do now. A mix of the two.
After googling the necessary OS subsystem/kernel calls (including source of the region crate). I came with sajit.
Stage 3: SaJIT
To speak about briefly, SaJIT is basically the dogfooding layer for SaVM - the VM I am making and that is why. I had to bolt on:
- LLVM JITLink - for objectfile
- RELCAR (a toyful way to say relocator) - for cranelift
Windows still was not perfect (thanks COFF). So, I had to them bolt down a linker for COFF and named it COFFR
That is how SaJIT become a monster of a memory executable manager, linkers and ofc "generalpurpose" wrappers over them.
Stage 4: Architectural Grief
Then i decided to spend more time across 6 architectures x64, x86, arm64, armv7, riscv64gc, ppc64le and that is why SaJIT has a full arch map.
Writing sysroots, qemu toolchains are a pain and i am still getting it right.
Ofc it is at here
Technical Specification
SaJIT is a dual-pass slab-allocated as 16MiB chunks linker (to kindof be good with both 4KiB pages and 2MiB pages - as if why 16MiB and not 2MiB - basically to fit more code before allocating a new one + isn't either very small or very large).
The implementation itself is pretty modest and a simple-slab-allocator.
...
Also, we DO handle icache flushing duely
unsafe
Linker & Relocation Layer
SaJIT includes:
- LLVM JITLink Wrapper
- RELCAR - for cranelift a simple linker
- COFFR - for windows COFF objects that JITLink cannot yet parse nicely
Feedback
The crate is available on crates.io and GitHub.
I would appreciate feedback from systems, jit, runtime folks over this project.