| 1 | // Started by the packaged BusyBox init through /etc/inittab on a diskless guest. |
| 2 | module main |
| 3 | |
| 4 | import os |
| 5 | |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/reboot.h> |
| 9 | #include <malloc.h> |
| 10 | |
| 11 | fn C.open(&char, int, ...) int |
| 12 | fn C.dup2(int, int) int |
| 13 | fn C.close(int) int |
| 14 | fn C.reboot(int) int |
| 15 | fn C.malloc_trim(uint) int |
| 16 | fn C.malloc_usable_size(voidptr) uint |
| 17 | |
| 18 | fn run(argv []string) !string { |
| 19 | result := os.exec(argv) |
| 20 | if result.exit_code != 0 { |
| 21 | return error('${argv} exited ${result.exit_code}: ${result.output}') |
| 22 | } |
| 23 | return result.output.trim_space() |
| 24 | } |
| 25 | |
| 26 | fn check(argv []string, expected string) ! { |
| 27 | actual := run(argv)! |
| 28 | if actual != expected { |
| 29 | return error('${argv}: expected ${expected}, got ${actual}') |
| 30 | } |
| 31 | println('${argv} -> ${actual}') |
| 32 | } |
| 33 | |
| 34 | fn verify() ! { |
| 35 | for path in ['/sbin/init', '/bin/sh', '/bin/cat'] { |
| 36 | if !os.is_link(path) || os.real_path(path) != '/bin/busybox' { |
| 37 | return error('${path} must resolve to /bin/busybox') |
| 38 | } |
| 39 | } |
| 40 | check(['/bin/busybox', 'uname', '-m'], 'x86_64')! |
| 41 | check(['/bin/sh', '-c', r'echo $((6 * 7))'], '42')! |
| 42 | check(['/bin/busybox', 'ash', '-c', r'echo $((3 * 7))'], '21')! |
| 43 | check(['/bin/cat', '/etc/inittab'], '::sysinit:/busybox-probe')! |
| 44 | println(run(['/bin/busybox', 'sha256sum', '/bin/busybox'])!) |
| 45 | $if packaged_musl ? { |
| 46 | listing := run(['/lib/ld-musl-x86_64.so.1', '--list', '/busybox-probe'])! |
| 47 | if !listing.contains('libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1') { |
| 48 | return error('unexpected dynamic dependencies: ${listing}') |
| 49 | } |
| 50 | println(listing) |
| 51 | println(run(['/bin/busybox', 'sha256sum', '/lib/ld-musl-x86_64.so.1'])!) |
| 52 | // A freed 256 KiB mapping falls in a retained bucket in the Vinix allocator. |
| 53 | C.malloc_trim(0) |
| 54 | buffer := unsafe { C.malloc(256 * 1024) } |
| 55 | if buffer == unsafe { nil } { |
| 56 | return error('malloc failed to provide 256 KiB') |
| 57 | } |
| 58 | capacity := C.malloc_usable_size(buffer) |
| 59 | unsafe { C.free(buffer) } |
| 60 | if capacity < 256 * 1024 { |
| 61 | return error('malloc supplied a truncated allocation') |
| 62 | } |
| 63 | if C.malloc_trim(0) == 0 { |
| 64 | return error('malloc_trim did not release a mapping after free') |
| 65 | } |
| 66 | println('Packaged musl released a mapping with malloc_trim.') |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn main() { |
| 71 | if os.getppid() != 1 || run(['/bin/busybox', 'uname', '-s']) or { '' } != 'Vinix' { |
| 72 | eprintln('requires a Vinix guest with the probe started directly by init') |
| 73 | exit(2) |
| 74 | } |
| 75 | fd := C.open(c'/dev/com1', C.O_WRONLY) |
| 76 | if fd < 0 || C.dup2(fd, 1) < 0 || C.dup2(fd, 2) < 0 { |
| 77 | exit(2) |
| 78 | } |
| 79 | if fd > 2 { |
| 80 | C.close(fd) |
| 81 | } |
| 82 | verify() or { |
| 83 | eprintln('VPAC BUSYBOX GUEST FAIL ${err}') |
| 84 | C.reboot(C.RB_POWER_OFF) |
| 85 | exit(1) |
| 86 | } |
| 87 | println('VPAC BUSYBOX GUEST PASS') |
| 88 | C.reboot(C.RB_POWER_OFF) |
| 89 | } |
| 90 | |