code wiki / (root) / nx_actlog_writer_gate.nx

nx_actlog_writer_gate.nx source

↩ module page · 133 lines · 7685 B

1// Exercise the actual shared journal writer on one exclusive caller-owned /tmp file. 2// license_tier: ORIGINAL; expect_exit: 0 3import "nx_tools_api.nx" 4import "nx_gatekit_lib.nx" 5import "nx_gate_verdict.nx" 6const AW_OLD_ROW_CAP: i64 = 768 // verified former writer allocation; regression fixture dimension 7const AW_FILE_MODE: i64 = 0x180 8const AW_I64_MIN: i64 = 0-9223372036854775807-1 9const AW_DIGIT_GUARD: i64 = 35 10 11func aw_number(v: i64, expected: *u8) -> i64 { 12 let buf: *u8 = sys_mmap(TA_LOG_I64_WIDTH+2) 13 if (buf as i64) <= 0 { return 0 } 14 var i: i64 = 0 15 while i < TA_LOG_I64_WIDTH+2 { buf[i] = AW_DIGIT_GUARD as u8; i = i+1 } 16 let end: i64 = ta_catn(buf, 1, v) 17 let n: i64 = gk_len(expected) 18 var ok: i64 = 0 19 if end == n+1 { 20 if buf[0] == (AW_DIGIT_GUARD as u8) { 21 if buf[end] == (AW_DIGIT_GUARD as u8) { ok = aw_same(((buf as i64)+1) as *u8, n, expected, n) } 22 } 23 } 24 sys_munmap(buf, TA_LOG_I64_WIDTH+2) 25 return ok 26} 27 28func aw_same(a: *u8, n: i64, b: *u8, bn: i64) -> i64 { 29 if n != bn { return 0 } 30 var i: i64 = 0 31 while i < n { if a[i] != b[i] { return 0 } i = i+1 } 32 return 1 33} 34func aw_path(path: *u8) -> i64 { 35 let root: *u8 = "/tmp/" as *u8 36 let r: i64 = gk_len(root) 37 let n: i64 = gk_len(path) 38 if n <= r { return 0 } 39 if n >= SYS_PATH_MAX { return 0 } 40 var i: i64 = 0 41 while i < r { if path[i] != root[i] { return 0 } i = i+1 } 42 if path[r] == (46 as u8) { return 0 } 43 while i < n { 44 let c: i64 = path[i] as i64 45 if c < 32 { return 0 } 46 if c == 47 { return 0 } 47 if c == 92 { return 0 } 48 i = i+1 49 } 50 return 1 51} 52func main(argc: i64, argv: *i64) -> i64 { 53 if argc != 2 { gv_puts("usage: actlog-writer-gate </tmp/fresh-file>\n" as *u8); return 3 } 54 let path: *u8 = argv[1] as *u8 55 if aw_path(path) == 0 { gv_puts("REFUSE unsafe fixture path\n" as *u8); return 3 } 56 let ctr: *i64 = gv_ctr(); gv_head("Existing actlog writer: full rows, original shape, honest I/O results" as *u8) 57 let fd: i64 = sys_openat_exclusive(path, AW_FILE_MODE) 58 gv_check("exclusive-caller-fixture-created" as *u8, fd >= 0, ctr) 59 if fd < 0 { return gv_verdict("NX-ACTLOG-WRITER" as *u8, ctr, "existing fixture refused; no production journal written" as *u8) } 60 sys_close(fd) 61 gv_check("signed-minimum-exact-digits-offset-and-canaries" as *u8, aw_number(AW_I64_MIN, "-9223372036854775808" as *u8), ctr) 62 gv_check("signed-maximum-exact-digits-offset-and-canaries" as *u8, aw_number(TA_LOG_I64_MAX, "9223372036854775807" as *u8), ctr) 63 gv_check("zero-exact-digits-offset-and-canaries" as *u8, aw_number(0, "0" as *u8), ctr) 64 gv_check("negative-digit-order-and-canaries" as *u8, aw_number(0-123456789, "-123456789" as *u8), ctr) 65 gv_check("capacity-add-exact-maximum" as *u8, ta_log_size_add(TA_LOG_I64_MAX-1, 1) == TA_LOG_I64_MAX, ctr) 66 gv_check("capacity-add-overflow-refused" as *u8, ta_log_size_add(TA_LOG_I64_MAX, 1) < 0, ctr) 67 gv_check("capacity-add-negative-propagates-refusal" as *u8, ta_log_size_add(0-1, 1) < 0, ctr) 68 let sizes: *i64 = sys_mmap(3*8) as *i64 69 let extra: *u8 = sys_mmap(AW_OLD_ROW_CAP+2) 70 var allocated: i64 = 0 71 if (sizes as i64) > 0 { if (extra as i64) > 0 { allocated = 1 } } 72 gv_check("fixture-buffers-allocated" as *u8, allocated, ctr) 73 if allocated == 0 { return gv_verdict("NX-ACTLOG-WRITER" as *u8, ctr, "fixture allocation failed" as *u8) } 74 let first_rc: i64 = ta_actlog_x_to(path, "fixture" as *u8, 7, "sync" as *u8, "ok" as *u8, 0-7, 13, 23, " job=17 reply_ms=5" as *u8) 75 gv_check("legacy-row-appends-successfully" as *u8, first_rc == 0, ctr) 76 let first: *u8 = sys_read_file(path, sizes) 77 var shape: i64 = 0 78 if (first as i64) > 0 { if sizes[0] > 0 { 79 var tab: i64 = 0 80 while tab < sizes[0] { if first[tab] == (9 as u8) { break } tab = tab+1 } 81 let tail: *u8 = "\tmcp\tfixture\tcall\tok\ttools/call lane=sync exit=-7 bytes=13 dur_ms=23 job=17 reply_ms=5\n" as *u8 82 if tab < sizes[0] { shape = aw_same(((first as i64)+tab) as *u8, sizes[0]-tab, tail, gk_len(tail)) } 83 } } 84 gv_check("existing-positional-fields-and-duration-meaning-unchanged" as *u8, shape, ctr) 85 let signed_rc: i64 = ta_actlog_x_to(path, "fixture" as *u8, 7, "sync" as *u8, "ok" as *u8, AW_I64_MIN, TA_LOG_I64_MAX, AW_I64_MIN, "" as *u8) 86 gv_check("signed-extreme-journal-row-appends" as *u8, signed_rc == 0, ctr) 87 let signed_sizes: *i64 = sys_mmap(8) as *i64 88 var signed_ok: i64 = 0 89 if (signed_sizes as i64) > 0 { 90 let signed_data: *u8 = sys_read_file(path, signed_sizes) 91 if (signed_data as i64) > 0 { 92 var tab: i64 = sizes[0] 93 while tab < signed_sizes[0] { if signed_data[tab] == (9 as u8) { break } tab = tab+1 } 94 let expected: *u8 = "\tmcp\tfixture\tcall\tok\ttools/call lane=sync exit=-9223372036854775808 bytes=9223372036854775807 dur_ms=-9223372036854775808\n" as *u8 95 if tab < signed_sizes[0] { signed_ok = aw_same(((signed_data as i64)+tab) as *u8, signed_sizes[0]-tab, expected, gk_len(expected)) } 96 sys_free_file(signed_data, signed_sizes[0]) 97 } 98 sys_munmap(signed_sizes as *u8, 8) 99 } 100 gv_check("signed-extreme-journal-row-exact-fields" as *u8, signed_ok, ctr) 101 var i: i64 = 0 102 while i <= AW_OLD_ROW_CAP { extra[i] = 120 as u8; i = i+1 } 103 extra[AW_OLD_ROW_CAP+1] = 0 as u8 104 let long_rc: i64 = ta_actlog_x_to(path, "fixture" as *u8, 7, "sync" as *u8, "ok" as *u8, 0, 13, 23, extra) 105 gv_check("tail-beyond-old-reserve-appends-completely" as *u8, long_rc == 0, ctr) 106 let second: *u8 = sys_read_file(path, ((sizes as i64)+8) as *i64) 107 var preserved: i64 = 0 108 if (first as i64) > 0 { if (second as i64) > 0 { if sizes[1] >= sizes[0] { preserved = aw_same(first, sizes[0], second, sizes[0]) } } } 109 gv_check("history-prefix-byte-identical" as *u8, preserved, ctr) 110 var tail_ok: i64 = 0 111 if (second as i64) > 0 { if sizes[1] > AW_OLD_ROW_CAP+2 { 112 let at: i64 = sizes[1]-(AW_OLD_ROW_CAP+1)-1 113 tail_ok = aw_same(((second as i64)+at) as *u8, AW_OLD_ROW_CAP+1, extra, AW_OLD_ROW_CAP+1) 114 if second[sizes[1]-1] != (10 as u8) { tail_ok = 0 } 115 } } 116 gv_check("long-row-tail-and-final-newline-retained" as *u8, tail_ok, ctr) 117 let rd: i64 = sys_openat_rd(path) 118 gv_check("readonly-fixture-descriptor-opened" as *u8, rd >= 0, ctr) 119 if rd >= 0 { 120 gv_check("actual-write-failure-never-returns-success" as *u8, ta_journal_write_fd(rd, "not-written" as *u8, 11) < 0, ctr) 121 sys_close(rd) 122 } 123 gv_check("actual-open-failure-never-returns-success" as *u8, ta_journal_append("/tmp" as *u8, "not-written" as *u8, 11) < 0, ctr) 124 gv_check("negative-row-length-refused" as *u8, ta_journal_append(path, "x" as *u8, 0-1) < 0, ctr) 125 gv_check("overflow-capacity-refused-before-reading-tool-span" as *u8, ta_actlog_x_to(path, "x" as *u8, TA_LOG_I64_MAX, "sync" as *u8, "ok" as *u8, 0, 0, 0, "" as *u8) < 0, ctr) 126 let final: *u8 = sys_read_file(path, ((sizes as i64)+16) as *i64) 127 var unchanged: i64 = 0 128 if (second as i64) > 0 { if (final as i64) > 0 { unchanged = aw_same(second, sizes[1], final, sizes[2]) } } 129 gv_check("all-refused-writes-leave-history-unchanged" as *u8, unchanged, ctr) 130 gv_puts("retained_fixture=" as *u8); gv_puts(path); gv_puts(" bytes=" as *u8); gv_num(sizes[2]); gv_puts("\n" as *u8) 131 sys_free_file(first, sizes[0]); sys_free_file(second, sizes[1]); sys_free_file(final, sizes[2]) 132 return gv_verdict("NX-ACTLOG-WRITER" as *u8, ctr, "caller fixture only; timing integration and live service require separate verification" as *u8) 133}