All checks were successful
docs / build-and-deploy (push) Successful in 3s
The security-sensitive counterpart to the fast, non-cryptographic Hash.* library: standard, test-vector-backed hashing for signed saves and message integrity, kept in its own namespace so nobody reaches for the wrong tool. Crypto.sha256(s) SHA-256 -> 64-char lowercase hex Crypto.hmac_sha256(key, msg) HMAC-SHA256 -> 64-char hex Crypto.verify_hmac(key, msg, mac) recompute + constant-time compare -> bool Crypto.hex(s) lowercase hex of a string's bytes Crypto.ct_equal(a, b) constant-time string equality The primitives are implemented from scratch in plain integer LLVM IR (FIPS 180-4 / RFC 2104): no libc crypto, no data-dependent branches in the compression rounds, so a given input hashes to the same 32 bytes on every platform and run. Digests are returned as hex strings, not raw bytes, because a `str` is null-terminated and a raw digest can contain a NUL. MAC checks use a non-short-circuiting compare so timing does not leak how much of a forged tag was correct. Emitted on demand via g_uses_cryptort, mirroring the emit_hash prelude gate. Scoped to the deterministic, known-answer-testable core; OS-backed random_bytes (the one piece that can't be validated by test vectors) is left for a follow-up. Tested against published SHA-256 vectors (empty/"abc"/fox + 55/56/64-byte multi-block padding) and HMAC-SHA256 vectors; wired into the self-host suite as `crypto`. Docs: a new Crypto section with honest "what this protects / does not" guidance, one page per method, all fences checked and in the inventory. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
129 lines
5.2 KiB
Text
129 lines
5.2 KiB
Text
# emit_decl.ludic — functions, main, and the whole-program driver. A function's
|
|
# body is built into a scratch buffer so entry-block allocas can be spliced in
|
|
# ahead of it (see emit_alloca). Returns go through a %retval slot + `ret` label.
|
|
|
|
function emit_params_sig(d: Node) -> void {
|
|
var i = 0
|
|
while i < len(d.kids) {
|
|
if i > 0 { emit(", ") }
|
|
emit(llty(d.kids[i].ty)); emit(" %arg_"); emit(d.kids[i].s)
|
|
i = i + 1
|
|
}
|
|
}
|
|
|
|
function emit_fn(d: Node) -> void {
|
|
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0
|
|
ret_ty = d.ty
|
|
let fbody = buf_new()
|
|
falloc = buf_new()
|
|
let saved = code
|
|
code = fbody
|
|
let rl = llty(ret_ty)
|
|
if not (rl == "void") { buf_puts(falloc, " %retval = alloca "); buf_puts(falloc, rl); buf_puts(falloc, "\n") }
|
|
# params: store each incoming argument into a stack slot
|
|
var i = 0
|
|
while i < len(d.kids) {
|
|
let p = d.kids[i]
|
|
let slot = emit_alloca(llty(p.ty))
|
|
emit(" store "); emit(llty(p.ty)); emit(" %arg_"); emit(p.s); emit(", ptr "); emit(slot); emit("\n")
|
|
loc_push(p.s, slot, p.ty)
|
|
i = i + 1
|
|
}
|
|
emit_block(d.a)
|
|
if not g_term { emit(" br label %ret\n") }
|
|
emit("ret:\n")
|
|
if (rl == "void") { emit(" ret void\n") }
|
|
else { let r = emit_bind(`load {rl}, ptr %retval`); emit(" ret "); emit(rl); emit(" "); emit(r); emit("\n") }
|
|
code = saved
|
|
emit("define "); emit(rl); emit(" @fn_"); emit(d.s); emit("("); emit_params_sig(d); emit(") {\nentry:\n")
|
|
emit(buf_str(falloc))
|
|
emit(buf_str(fbody))
|
|
emit("}\n\n")
|
|
}
|
|
|
|
function emit_main(d: Node) -> void {
|
|
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0
|
|
ret_ty = "int"
|
|
let fbody = buf_new()
|
|
falloc = buf_new()
|
|
let saved = code
|
|
code = fbody
|
|
buf_puts(falloc, " %retval = alloca i32\n")
|
|
emit(" store i32 %argc, ptr @L_argc\n")
|
|
emit(" store ptr %argv, ptr @L_argv\n")
|
|
emit(" store i32 0, ptr %retval\n")
|
|
emit_block(d.a)
|
|
if not g_term { emit(" br label %ret\n") }
|
|
emit("ret:\n")
|
|
let r = emit_bind("load i32, ptr %retval")
|
|
emit(" ret i32 "); emit(r); emit("\n")
|
|
code = saved
|
|
emit("define i32 @main(i32 %argc, ptr %argv) {\nentry:\n")
|
|
emit(buf_str(falloc))
|
|
emit(buf_str(fbody))
|
|
emit("}\n")
|
|
}
|
|
|
|
function emit_program() -> void {
|
|
head = buf_new()
|
|
code = buf_new()
|
|
g_uses_str = false
|
|
g_uses_intstr = false
|
|
g_uses_strslice = false
|
|
g_uses_loopback = false
|
|
loc_name = new []pointer; loc_reg = new []pointer; loc_ty = new []pointer; loc_mut = new []int
|
|
brk_lbl = new []pointer; cnt_lbl = new []pointer
|
|
self_stk = new []pointer
|
|
mach_stk = new []Node
|
|
emit_header()
|
|
emit_extern_decls()
|
|
if has_ecs() { emit_ecs_storage() }
|
|
var i = 0
|
|
while i < len(prog) { if prog[i].kind == N_FN { emit_fn(prog[i]) }; i = i + 1 }
|
|
if len(g_events) > 0 { emit_event_fns() } # EV0: @ev_<E> event-dispatch functions
|
|
if has_ecs() and len(g_events) > 0 { emit_world_table() } # EV2: the mod reflection ABI
|
|
if has_ecs() { emit_ecs_allocator(); emit_snapshot() }
|
|
if has_ecs() { emit_net() } # N2/N3: @Sync serializers + @Owned storage (gated internally)
|
|
if has_ui() { emit_ui_build() }
|
|
if has_systems() and has_entry() { # N5: game owns its loop via `entry`
|
|
emit_game_defs() # system fns, hooks, tick helpers
|
|
i = 0
|
|
while i < len(prog) { if prog[i].kind == N_MAIN { emit_main(prog[i]) }; i = i + 1 }
|
|
}
|
|
else { if has_systems() { emit_game_main() } # the auto frame loop
|
|
else {
|
|
i = 0
|
|
while i < len(prog) { if prog[i].kind == N_MAIN { emit_main(prog[i]) }; i = i + 1 }
|
|
} }
|
|
if g_uses_loopback { emit_loopback() } # built-in transport, after all net_send/net_poll uses are seen
|
|
if g_uses_str { emit_str_prelude() } # @fn_str_eq / @fn_str_concat, after all uses are seen
|
|
if g_uses_intstr { emit_int_str() } # @fn_int_str, for string(int) in interpolation
|
|
if g_uses_longstr { emit_long_str() } # @fn_long_str, for string(long) / long interpolation
|
|
if g_uses_strslice { emit_str_slice() } # @fn_str_slice, for s[a..b]
|
|
if g_uses_mathrt { emit_math_prelude() } # @fn_fx_sqrt / @fn_fx_sin + the sine table
|
|
if g_uses_textrt { emit_text_prelude() } # @fn_str_upper/lower/trim/repeat/pad builders
|
|
if g_uses_textrt2 { emit_text2_prelude() } # @fn_str_replace/join/split builders
|
|
if g_uses_hashrt { emit_hash_prelude() } # @fn_hash_fnv1a / @fn_hash_crc32 byte hashers
|
|
if g_uses_cryptort { emit_crypto_prelude() } # @fn_sha256_hex / @fn_hmac_sha256_hex + constant-time compare
|
|
if g_uses_datert { emit_datetime_prelude() } # @fn_days_from_civil / @fn_civil_from_days conversions
|
|
}
|
|
|
|
# Flush the emitted IR. With a null path it goes to stdout (the pipe the shell
|
|
# drivers read); with a path it is written to that file so ludicc can hand it to
|
|
# clang itself.
|
|
function ir_flush(path: pointer) -> bool {
|
|
let h = buf_str(head)
|
|
let c = buf_str(code)
|
|
if (path == null) { # raw IR to stdout (no trailing newline)
|
|
let out = file_stdout()
|
|
file_write(out, h, len(h))
|
|
file_write(out, c, len(c))
|
|
return true
|
|
}
|
|
let f = file_open(path, "wb")
|
|
if (f == null) { return false }
|
|
file_write(f, h, len(h))
|
|
file_write(f, c, len(c))
|
|
file_close(f)
|
|
return true
|
|
}
|