Part IV · Compiler Architecture, Backend Internals, Safety Proofs & ADRs
Chapter 17
Safety Classification & ORO Memory Model Proof
16-byte Value and SlabHdr layouts, mechanical DAG cycle-freedom proof, 12-bucket slab recycling, and formal trust boundaries
This specification details Veko's One Reference Only (ORO) + Copy-on-Write (CoW) memory architecture across all three execution backends (oro_vm.vk, codegen.vk / vvm, and native_emit.vk / arm64_emit.vk), mechanical proof of cycle freedom, deterministic .secret zeroization and sink firewalls (including .ui clipboard/window and .ffi), vvm sandbox confinement, opt-in .ffi isolation with zero-W^X static trampolines, and atomic slab recycling across Windows (x86_64), Linux (x86_64/arm64), and macOS (arm64).
1. 16-Byte Value & SlabArena Architecture
Every Veko runtime value—whether inside oro_vm.vk frames, codegen.vk Wasm linear memory, or native_emit.vk / arm64_emit.vk registers and OS-allocated slabs (VirtualAlloc on win-x64, mmap on linux-x64, linux-arm64, and macos-arm64)—occupies an aligned 16-byte cell (4 cells per 64-byte CPU cache line):
16-Byte Value Cell, 14-Byte Inline SSO & 12-Bucket Slab Arena
Exact binary representation shared across 64-bit VKO VM, Wasm 3.0 linear memory, x86_64, and AArch64.
| Component & Module | Architectural Role |
|---|---|
16-Byte Value Wordruntime.vk + native_rt.vk | Uniform 128-bit representation holding unboxed scalars, inline SSO bytes, or a slab pointer. |
14-Byte Inline SSOruntime.vk + native_rt.vk | Stores short identifiers, map keys, HTTP tokens, and nonces directly inside bytes 2..15. |
Acyclic Closure Cellvko_emit.vk + native_emit.vk | Pairs a compiled function table index with an immutable snapshot of captured outer values. |
16-Byte SlabHdrnative_rt.vk + arm64_rt_core.vk | Prefixes every heap-allocated bytes, str_heap, block, map, and struct payload. |
12-Bucket Free-Listsnative_rt.vk + runtime.vk | Recycles freed slabs immediately into per-thread size-class singly linked lists. |
Volatile .secret Scrubtypes.vk + native_rt.vk | Zeroes secret key material deterministically before returning memory to any free-list. |
Byte-Accurate Value Cell Layout
| Offset | Size | Field | Role |
|---|---|---|---|
0 | 1 B | tag | Discriminant (uninit, bool, i64, f64, dur, size, word, enum, str_sso, str_heap, bytes, block, map, closure, native_fn, err) |
1 | 1 B | flags | Wasm: flag_owned (0x01), flag_secret (0x02), flag_const (0x04), flag_mut_ref (0x08), flag_struct (0x10), flag_map_hashed (0x20 on map_val with >16 pairs; on str_sso, upper nibble stores length 0..14). Native: FLAG-OWNED=1, FLAG-MUT-REF=2, FLAG-SECRET=4, FLAG-CONST=8, FLAG-STRUCT=16, FLAG-MAP-HASHED=32 (.rdata constant slabs carry rc = -1). |
2..3 | 2 B | aux | Word binding kind, native arity, canonical struct schema symbol ID, or str_sso bytes 0..1 |
4..7 | 4 B | meta | 32-bit SlabId (Wasm), interned symbol ID, closure fn_idx, or str_sso bytes 2..5 |
8..15 | 8 B | data | Unboxed i64/f64, 64-bit native slab pointer, str_sso bytes 6..13, or closure env_slab |
2. Why flag_owned Eliminates Borrow Reference-Counting
Traditional reference-counted languages increment and decrement heap counters on every function call. Veko stores ownership directly on the 16-byte Value cell:
- Read-Only Borrow: Passing
bufto a plain parameter copies the 16-byteValueinto the callee frame withflag_owned = 0. When the callee exits, scope cleanup inspects(v.flags & flag_owned) != 0, skips borrowed values, and performs zero heap memory touches. - Ownership Move: Assigning
y = xor returning a value transfersflag_owned = 1to the destination and immediately releases any previous slab owned byy. - Explicit
mutLoan: Marking both signature and call site withmut(including disjoint field borrowsmut state.tx_buf) setsflag_mut_reftargeting the caller's slot so in-place mutations (bytes/push,ascii/upper!) operate directly on the caller's buffer and update the slot if a CoW split reallocates.
3. Mechanical Proof That Reference Cycles Cannot Exist
Appending a container to itself (push mut a a) cannot form a reference cycle under Copy-on-Write ensure_unique:
- Evaluating the second argument
acreates a temporary snapshot, bumpinghdr(a).rcfrom1to2. pushinvokesensure_unique(mut a)before mutating.- Because
hdr(a).rc == 2(shared),ensure_uniqueallocates a fresh slaba'(rc = 1), copies existing elements intoa', decrements olda'srcback to1, and appends oldainside newa'. - Invariant: Only a slab with
rc == 1can ever be mutated; an older slab can never be mutated to point to a newer or ancestor slab. Every heap pointer points strictly backward in allocation time, guaranteeing a Directed Acyclic Graph (DAG) and 100% deterministic scope-exit deallocation.
4. Deterministic .secret Zeroization & End-to-End Sink Firewalls
- No Inline SSO for Secrets:
os/getenv/secret,ui/clipboard-get/secret,bytes/with-secret, and interpolated backtick templates containing secret expressions always allocate heap slabs carryingflag_secretso full capacity is zero-wiped (0x00) insiderelease([16 .. 16 + cap)for raw slabs,cap * 16for value slabs) before the slab returns to any free-list or OS page pool. - Automatic Redaction:
print,println,mold, andlog/*redact.secretvalues to<secret>. - Static & Runtime Sink Firewalls: Both
veko check(types.vk) and recursive runtime inspection block.secretvalues, struct fields, and containers from crossing:- Disk & stdio (
os/write-file,os/pwrite,os/stderr,os/exec), - Wire serialization & networking (
json/encode,net/*,http/*), - Concurrency channels & tasks (
spawn,chan/send), - Desktop UI & OS clipboard (
ui/present,ui/clipboard-set), and - Foreign C-ABI boundaries (
ffi/open,ffi/sym,ffi/call).
- Disk & stdio (
5. Cross-Module Isolation, Shared-Nothing Concurrency & Sandbox Confinement
- Controlled Module Exports:
pubandexport [...]permit functions, macros,struct/sealed structschemas, andinterfacecontracts—rejectingpub mutstate so mutable slabs never alias across module boundaries. - Stage 1
--strict-typesStatic Soundness (types.vk): Enforces fallibleT|errexpression tracking across I/O, network, map, and parsing APIs (requiring/orrecovery orif err? xbranch narrowing before typedTuse), exhaustivematcharm verification over booleans and closed enums, homogeneousblock[T]literals, strictmutlocal reassignment, and identicalerrsemantics for missingos/getenvkeys across VM, Wasm, and Native. - Shared-Nothing
spawnWorkers: Eachspawn [ ... ]worker runs with privateThreadState(R12on x86_64,X19on AArch64) or an isolatedvvmHostSandboxstore, updating shared refcounts atomically (lock xadd/ldaddal) while prohibiting sharedmutborrows and closures across threads. vvmWebAssembly 3.0 Confinement: Enforces bounded linear memory (--mem-pages), deterministic instruction fuel (--fuel), confined WASI filesystem roots (--allow-fs=<dir>), SSRF loopback/RFC1918 guards (--no-private-net), and unconditional denial of.ffi.
6. Atomic 12-Bucket Slab Recycling & Unwind Safety
- 16-Byte Native Slab Header:
[+0 rc:i32, +4 cap:i32, +8 len:i32, +12 flags:i32]packsSLAB-FLAG-VAL = 1,FLAG-SECRET = 4,FLAG-STRUCT = 16, 16-bittype_sym << 8, andbucket_tag << 24(1..12for32 B..65536 B;0for>64 KiBdirect OS pages). - Unwind-Safe
/or&deferIsolation: During/orerror unwinding, outer error cells andRT-TRY-TOPindices remain isolated across nested LIFOdeferexecutions, and division/modulo by zero (OP-DIV,OP-MOD) raise catchable/orerrors rather than OS hardware traps. - Formal Safety Pillars:
| Safety Pillar | Enforced Mechanism | Guarantee |
|---|---|---|
| Spatial Safety | Bounds-checked bytes, str, block, wire intrinsics, and O(1) struct slot access across VM, Wasm, and Native. | Eliminates out-of-bounds reads/writes and raw pointer arithmetic in safe .vk code. |
| Temporal Safety | Single-owner flag_owned moves, CoW rc == 1 mutation invariant, and disjoint lexical mut loans. | Eliminates use-after-free, double-free, and cyclic leaks deterministically at scope exit. |
| Concurrency Safety | Compile-time spawn capture checks + runtime conc_safe transfer validation + atomic rc. | Eliminates unsynchronized shared-memory data races. |
| Confidentiality & Supply Chain | flag_secret heap zeroization + I/O sink firewalls + Ed25519/ML-DSA-65 'VKP1' & veko.lock Merkle verification. | Prevents secret exfiltration and tampered package injection. |
Explicit Trust Boundary & Opt-In .ffi Isolation
Safe .vk guarantees hold modulo Veko's self-hosted compiler/runtime TCB and underlying OS kernel syscalls. Foreign C/Rust library interop (lib/ffi.vk) is denied by default, forbidden on --wasm, implemented strictly via static AOT C-ABI trampolines (zero RWX JIT pages), and gated behind explicit allow-caps = [.ffi] or --allow-ffi.