code wiki / _hdl_build / nx_exclprobe.nx
nx_exclprobe.nx source
↩ module page · 64 lines · 3352 B
1// nx_exclprobe.nx -- DOES O_CREAT|O_EXCL VIA __syscall ACTUALLY WORK HERE? (2026-08-07)
2//
3// WHY THIS EXISTS. The async job lane assigns ids by probing whether `_jobs/job_<id>.done` EXISTS, but a
4// RUNNING job has no `.done`, so two tools/call in the same second get the IDENTICAL id and the loser's
5// receipt is overwritten (observed live: two nx_debt add both returned id=1786110674).
6// The correct fix is an ATOMIC reservation, and `dl_lock` in nx_dispatch_lease already does exactly that
7// with __syscall(SYS_OPENAT, AT_FDCWD, path, 193, 420, 0, 0). But banked memory warns that LITERAL
8// __syscall numbers are rv64->x86 TRANSLATED, which would make 257 mean something else entirely -- and I
9// am not shipping that into the daemon that serves /mcp on a guess after two outages today.
10// ★ VERIFY A PRIMITIVE IN A THROWAWAY BEFORE PUTTING IT IN THE THING EVERYTHING ELSE DEPENDS ON.
11// This organ answers it decisively and touches nothing but /tmp.
12// nx_exclprobe -> JSON {first_create, second_create, verdict}
13// EXPECTED IF THE PRIMITIVE IS SOUND: first >= 0 (we created it), second < 0 (EEXIST refused us).
14// If BOTH succeed, O_EXCL is not being honoured and the whole approach is unusable.
15// If BOTH fail, the syscall number is being translated and 257 is not openat here.
16// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
17import "nx_syscalls.nx"
18
19const XP_SYS_OPENAT: i64 = 257
20const XP_AT_FDCWD: i64 = 0 - 100
21const XP_OEXCL: i64 = 193 // O_CREAT(0x40)|O_EXCL(0x80)|O_WRONLY(0x1)
22const XP_MODE: i64 = 420 // 0644
23const XP_PATH: *u8 = "/tmp/nx_exclprobe.claim" as *u8
24
25func xw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
26func xn(v: i64) -> i64 {
27 let b: *u8 = sys_mmap(32)
28 var x: i64 = v
29 var neg: i64 = 0
30 if x < 0 { neg = 1; x = 0 - x }
31 var i: i64 = 31
32 if x == 0 { b[i] = 48 as u8; i = i - 1 }
33 while x > 0 { b[i] = ((48 + (x - ((x / 10) * 10))) as u8); x = x / 10; i = i - 1 }
34 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
35 sys_write(1, ((b as i64) + i + 1) as *u8, 31 - i)
36 return 0
37}
38
39func main() -> i64 {
40 sys_unlinkat(XP_PATH)
41 let a: i64 = __syscall(XP_SYS_OPENAT, XP_AT_FDCWD, XP_PATH as i64, XP_OEXCL, XP_MODE, 0, 0)
42 if a >= 0 { sys_close(a) }
43 let b: i64 = __syscall(XP_SYS_OPENAT, XP_AT_FDCWD, XP_PATH as i64, XP_OEXCL, XP_MODE, 0, 0)
44 if b >= 0 { sys_close(b) }
45 // control: the file must now EXIST and be readable, proving the first call really created it
46 let r: i64 = sys_openat_rd(XP_PATH)
47 var exists: i64 = 0
48 if r >= 0 { exists = 1; sys_close(r) }
49 sys_unlinkat(XP_PATH)
50 xw("{\"organ\":\"nx_exclprobe\",\"first_create\":" as *u8); xn(a)
51 xw(",\"second_create\":" as *u8); xn(b)
52 xw(",\"file_existed_after_first\":" as *u8); xn(exists)
53 xw(",\"verdict\":\"" as *u8)
54 var ok: i64 = 0
55 if a >= 0 { if b < 0 { if exists == 1 { ok = 1 } } }
56 if ok == 1 { xw("SOUND -- O_EXCL refuses the second creator; atomic reservation is usable" as *u8) } else {
57 if a < 0 { xw("UNUSABLE -- even the FIRST create failed; the syscall number is not openat here" as *u8) } else {
58 xw("UNSAFE -- both creates succeeded; O_EXCL is NOT being honoured" as *u8)
59 }
60 }
61 xw("\"}\n" as *u8)
62 sys_exit(0)
63 return 0
64}