code wiki / _hdl_build / nx_doctor_build.nx

nx_doctor_build.nx source

↩ module page · 58 lines · 2801 B

1// nx_doctor_build.nx -- the DOCTOR's build-heal (RACI: the FIX belongs to the Doctor, not the 2// Conductor). When the Engineer detects a broken/miscompiled build, the Doctor's remedy is to 3// REGENERATE it: the known-good compiler is non-deterministic, so a fresh compile usually emits 4// a correct build -- regenerating the artifact IS the heal. doc_rebuild produces a fresh 5// <src> -> <sout>(.s) -> <eout>(.elf); returns 0 on a clean build, <0 if it could not produce 6// one. The Doctor only fixes (regenerates); the ENGINEER re-verifies (runs + canary), the 7// CONDUCTOR orchestrates, the WARDEN/COUNCIL govern. Depends only on syscalls -- decoupled. 8// license_tier: ORIGINAL 9 10import "nx_syscalls.nx" 11 12func doc_rebuild(compiler: *u8, src: *u8, sout: *u8, eout: *u8) -> i64 { 13 // 1. regenerate the assembly: fork <compiler> <src> with stdout -> sout 14 let argv: *i64 = sys_mmap(16) as *i64 15 argv[0] = compiler as i64; argv[1] = src as i64; argv[2] = 0 16 let envp: *i64 = sys_mmap(16) as *i64 17 envp[0] = ("PATH=/usr/bin:/bin" as *u8) as i64; envp[1] = 0 18 let pid: i64 = sys_fork() 19 if pid < 0 { return 0 - 1 } 20 if pid == 0 { 21 let fd: i64 = sys_openat_wr(sout, 0x1a4) 22 if fd >= 0 { sys_dup3(fd, 1, 0); sys_close(fd) } 23 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 24 if dn >= 0 { sys_dup3(dn, 2, 0); sys_close(dn) } 25 sys_execve(compiler, argv, envp) 26 sys_exit(127) 27 } 28 let st: *i64 = sys_mmap(16) as *i64 29 st[0] = 0; sys_wait4(pid, st, 0) 30 if (st[0] & 0x7f) != 0 { return 0 - 1 } 31 if ((st[0] >> 8) & 0xff) != 0 { return 0 - 1 } 32 let rfd: i64 = sys_openat_rd(sout) // the .s must be non-empty (a real heal) 33 if rfd < 0 { return 0 - 1 } 34 let buf: *u8 = sys_mmap(8) 35 let n: i64 = sys_read(rfd, buf, 1) 36 sys_close(rfd) 37 if n <= 0 { return 0 - 1 } 38 39 // 2. link the fresh build: fork gcc -nostdlib -no-pie -static <sout> -o <eout> 40 let g: *i64 = sys_mmap(16 * 8) as *i64 41 g[0] = ("/usr/bin/gcc" as *u8) as i64; g[1] = ("-nostdlib" as *u8) as i64; g[2] = ("-no-pie" as *u8) as i64 42 g[3] = ("-static" as *u8) as i64; g[4] = sout as i64; g[5] = ("-o" as *u8) as i64; g[6] = eout as i64; g[7] = 0 43 let ge: *i64 = sys_mmap(16) as *i64 44 ge[0] = ("PATH=/usr/bin:/bin:/usr/local/bin" as *u8) as i64; ge[1] = 0 45 let p2: i64 = sys_fork() 46 if p2 < 0 { return 0 - 1 } 47 if p2 == 0 { 48 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 49 if dn >= 0 { sys_dup3(dn, 2, 0); sys_close(dn) } 50 sys_execve("/usr/bin/gcc" as *u8, g, ge) 51 sys_exit(127) 52 } 53 let st2: *i64 = sys_mmap(16) as *i64 54 st2[0] = 0; sys_wait4(p2, st2, 0) 55 if (st2[0] & 0x7f) != 0 { return 0 - 1 } 56 if ((st2[0] >> 8) & 0xff) != 0 { return 0 - 1 } 57 return 0 58}