nx_pe_writer_test.nx source
↩ module page · 69 lines · 2753 B
1// nx_pe_writer_test.nx -- substrate emits a Windows PE, writes to disk.
2//
3// Substrate side: build the PE bytes in memory, flush to
4// /mnt/c/Users/elder/nishi-core/_offc/nx_pe_exit42.exe so the bash
5// driver can launch it on Windows + check the exit code.
6
7import "nx_syscalls.nx"
8import "nx_hal.nx"
9import "nx_pe_writer.nx"
10
11func main() -> i64 {
12 // ----- Allocate 1536 bytes (PE_FILE_SIZE) zero-init -----
13 let buf: *u8 = nx_hal_alloc_pages(PE_FILE_SIZE)
14 if (buf as i64) == 0 { return 1 }
15
16 // ----- Emit PE bytes -----
17 let rc: i64 = nx_pe_emit_exit42(buf)
18 if rc != NX_PE_OK { return 2 }
19
20 // ----- Spot-checks on emitted layout (substrate verifies its own work) -----
21 // MZ magic
22 if buf[0] != 0x4D { return 10 } // 'M'
23 if buf[1] != 0x5A { return 11 } // 'Z'
24 // e_lfanew == 0x80
25 if buf[0x3C] != 0x80 { return 12 }
26 // PE signature "PE\0\0"
27 if buf[0x80] != 0x50 { return 13 } // 'P'
28 if buf[0x81] != 0x45 { return 14 } // 'E'
29 if buf[0x82] != 0x00 { return 15 }
30 if buf[0x83] != 0x00 { return 16 }
31 // Machine = AMD64 (0x8664 LE -> 64 86)
32 if buf[0x84] != 0x64 { return 17 }
33 if buf[0x85] != 0x86 { return 18 }
34 // Optional Header Magic = PE32+ (0x020B LE -> 0B 02)
35 if buf[0x98] != 0x0B { return 19 }
36 if buf[0x99] != 0x02 { return 20 }
37 // .text bytes: 48 83 EC 28 B9 2A 00 00 00 FF 15 29 10 00 00 CC
38 if buf[0x200] != 0x48 { return 30 }
39 if buf[0x201] != 0x83 { return 31 }
40 if buf[0x202] != 0xEC { return 32 }
41 if buf[0x203] != 0x28 { return 33 }
42 if buf[0x204] != 0xB9 { return 34 }
43 if buf[0x205] != 0x2A { return 35 } // exit code 42
44 if buf[0x209] != 0xFF { return 36 }
45 if buf[0x20A] != 0x15 { return 37 }
46 if buf[0x20B] != 0x29 { return 38 } // disp32 low byte
47 if buf[0x20C] != 0x10 { return 39 } // disp32 high
48 if buf[0x20F] != 0xCC { return 40 } // int3 safety
49 // .idata "kernel32.dll" at file 0x456
50 if buf[0x456] != 0x6B { return 50 } // 'k'
51 if buf[0x457] != 0x65 { return 51 } // 'e'
52 if buf[0x458] != 0x72 { return 52 } // 'r'
53 if buf[0x459] != 0x6E { return 53 } // 'n'
54 if buf[0x462] != 0x00 { return 54 } // null term
55 // "ExitProcess" at file 0x44A
56 if buf[0x44A] != 0x45 { return 60 } // 'E'
57 if buf[0x44B] != 0x78 { return 61 } // 'x'
58
59 // ----- Flush to disk -----
60 let out_path: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_pe_exit42.exe" as *u8
61 let wrc: i64 = nx_pe_write_to_file(out_path, buf, PE_FILE_SIZE)
62 if wrc != NX_PE_OK { return 70 }
63
64 // Confirm with a stdout marker.
65 let msg: *u8 = "[substrate] PE32+ written to nx_pe_exit42.exe (1536 bytes)\n" as *u8
66 sys_write(1, msg, 59)
67
68 return 0
69}