nx_clock_driver_boundary_ar.nx source
↩ module page · 111 lines · 5090 B
1// TEST ONLY: strict fixture persistence boundary, not a segment-store implementation.
2import "nx_syscalls.nx"
3const DFI_MODE: i64 = 384
4const DFI_REFUSED: i64 = 90
5const DFI_EAGAIN: i64 = 0 - 11
6const DFI_DECIMAL_DIGITS: i64 = 19
7const DFI_DECIMAL_BASE: i64 = 10
8const DFI_I64_MAX: i64 = 9223372036854775807
9func dfi_len(p: *u8) -> i64 { var n: i64=0; while p[n] != (0 as u8) { n=n+1 }; return n }
10func dfi_eq(a: *u8,b: *u8) -> i64 { var i: i64=0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 }; i=i+1 }; return (b[i] == (0 as u8)) as i64 }
11func dfi_mark(p: *u8) -> i64 {
12 let fd: i64=sys_openat_append(p,DFI_MODE)
13 if fd<0 { sys_exit(DFI_REFUSED) }
14 if sys_write(fd,"x" as *u8,1) != 1 { sys_close(fd); sys_exit(DFI_REFUSED) }
15 if sys_close(fd) != 0 { sys_exit(DFI_REFUSED) }
16 return 0
17}
18func dfi_guard() -> i64 {
19 let n: *i64=sys_mmap(8) as *i64
20 let b: *u8=sys_read_file("fixture-root.guard" as *u8,n)
21 let wanted: *u8="NISHI-CLOCK-RESERVE-FIXTURE\n" as *u8
22 if (b as i64)==0 { return 0 }
23 if n[0] != dfi_len(wanted) { return 0 }
24 var i: i64=0; while i<n[0] { if b[i] != wanted[i] { return 0 }; i=i+1 }
25 return 1
26}
27func dfi_require() -> i64 { if dfi_guard()!=1 { sys_exit(DFI_REFUSED) }; return 0 }
28func dfi_preflight(argc: i64,argv: *i64) -> i64 {
29 if argc != 4 { return 0 }
30 if dfi_eq(argv[1] as *u8,"clock_jobs.tsv" as *u8)!=1 { return 0 }
31 if dfi_eq(argv[2] as *u8,"clock_tick.txt" as *u8)!=1 { return 0 }
32 if dfi_eq(argv[3] as *u8,"isolated-driver" as *u8)!=1 { return 0 }
33 if dfi_guard()!=1 { return 0 }
34 let budget: i64=dfi_positive_config("driver.handshake-ms" as *u8)
35 let poll: i64=dfi_positive_config("driver.handshake-poll-ms" as *u8)
36 if budget<=0 { return 0 }; if poll<=0 { return 0 }; if poll>budget { return 0 }
37 let start: i64=sys_now_ms()
38 while sys_now_ms()-start<budget {
39 let n: *i64=sys_mmap(8) as *i64
40 let b: *u8=sys_read_file("witness.watchdog-ready" as *u8,n)
41 if (b as i64)!=0 { if n[0]==1 { if b[0]==(120 as u8) { return 1 } } }
42 sys_sleep_ms(poll)
43 }
44 sts_werr("DRIVER-FIXTURE REFUSED: no confirmed watchdog; scheduler never entered\n" as *u8)
45 return 0
46}
47func dfi_positive_config(path: *u8) -> i64 {
48 let n: *i64=sys_mmap(8) as *i64; let b: *u8=sys_read_file(path,n)
49 if (b as i64)==0 { return 0 }; if n[0]<=0 { return 0 }; if n[0]>DFI_DECIMAL_DIGITS { return 0 }
50 var i: i64=0; var value: i64=0
51 while i<n[0] {
52 let c: i64=b[i] as i64
53 if c<48 { return 0 }; if c>57 { return 0 }
54 let digit: i64=c-48
55 if value>(DFI_I64_MAX-digit)/DFI_DECIMAL_BASE { return 0 }
56 value=value*DFI_DECIMAL_BASE+digit; i=i+1
57 }
58 return value
59}
60func dfi_watchdog_denied() -> i64 {
61 dfi_require()
62 let n: *i64=sys_mmap(8) as *i64; let b: *u8=sys_read_file("fixture.watchdog-denied" as *u8,n)
63 if (b as i64)==0 { return 0 }
64 if n[0]!=1 { sys_exit(DFI_REFUSED) }
65 if b[0]!=(120 as u8) { sys_exit(DFI_REFUSED) }
66 return 1
67}
68func dfi_windows() -> i64 {
69 dfi_require()
70 let n: *i64=sys_mmap(8) as *i64
71 let b: *u8=sys_read_file("driver.windows" as *u8,n)
72 if (b as i64)==0 { sys_exit(DFI_REFUSED) }
73 if n[0]!=1 { sys_exit(DFI_REFUSED) }
74 // Structural fixture shape: two actual main-loop iterations for the positive control.
75 if b[0]!=(50 as u8) { sys_exit(DFI_REFUSED) }
76 return 2
77}
78func sts_werr(s: *u8) -> i64 { return sys_write(2,s,dfi_len(s)) }
79func sts_load(prefix: *u8,out: *u8,cap: i64) -> i64 {
80 dfi_require()
81 var path: *u8=0 as *u8
82 if dfi_eq(prefix,"knowledge/store/clocksched-" as *u8)==1 {
83 path="fixture.schedule.tsv" as *u8; dfi_mark("witness.schedule-load" as *u8)
84 } else {
85 if dfi_eq(prefix,"knowledge/store/clockjobs-" as *u8)!=1 { sys_exit(DFI_REFUSED) }
86 path="fixture.desired.tsv" as *u8; dfi_mark("witness.desired-load" as *u8)
87 }
88 let n: *i64=sys_mmap(8) as *i64; let b: *u8=sys_read_file(path,n)
89 if (b as i64)==0 { sys_exit(DFI_REFUSED) }
90 if n[0]>=cap { sys_exit(DFI_REFUSED) }
91 var i: i64=0; while i<n[0] { out[i]=b[i]; i=i+1 }
92 out[i]=0 as u8
93 return n[0]
94}
95func sts_seed(prefix: *u8,buf: *u8,n: i64) -> i64 {
96 dfi_require()
97 if dfi_eq(prefix,"knowledge/store/clocksched-" as *u8)!=1 { sys_exit(DFI_REFUSED) }
98 if n<=0 { sys_exit(DFI_REFUSED) }
99 dfi_mark("witness.schedule-seed" as *u8)
100 let fd: i64=sys_openat_wr("fixture.schedule.tsv" as *u8,DFI_MODE)
101 if fd<0 { sys_exit(DFI_REFUSED) }
102 var w: i64=0
103 while w<n { let got: i64=sys_write(fd,(buf as i64+w) as *u8,n-w); if got<=0 { sys_close(fd); sys_exit(DFI_REFUSED) }; w=w+got }
104 if sys_close(fd)!=0 { sys_exit(DFI_REFUSED) }
105 let lp: *i64=sys_mmap(8) as *i64; let b: *u8=sys_read_file("fixture.schedule.tsv" as *u8,lp)
106 if (b as i64)==0 { sys_exit(DFI_REFUSED) }; if lp[0]!=n { sys_exit(DFI_REFUSED) }
107 var i: i64=0; var rows: i64=0
108 while i<n { if b[i]!=buf[i] { sys_exit(DFI_REFUSED) }; if b[i]==(10 as u8) { rows=rows+1 }; i=i+1 }
109 dfi_mark("witness.schedule-committed" as *u8)
110 return rows
111}