vinix-ports / tests / busybox / guest.v
92 lines · 85 sloc · 2.71 KB · 0a0ab0a6c21d20a10f1d7195371f339b763a5c79
Raw
1// Started by the packaged BusyBox init through /etc/inittab on a diskless guest.
2module main
3
4import os
5
6#include <fcntl.h>
7#include <unistd.h>
8#include <sys/reboot.h>
9#include <malloc.h>
10
11fn C.open(&char, int, ...) int
12fn C.dup2(int, int) int
13fn C.close(int) int
14fn C.reboot(int) int
15fn C.malloc_trim(uint) int
16fn C.malloc_usable_size(voidptr) uint
17
18fn 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
26fn 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
34fn 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 machine := $if arm64 { 'aarch64' } $else { 'x86_64' }
41 check(['/bin/busybox', 'uname', '-m'], machine)!
42 check(['/bin/uname', '-o'], 'Vinix')!
43 check(['/bin/sh', '-c', r'echo $((6 * 7))'], '42')!
44 check(['/bin/busybox', 'ash', '-c', r'echo $((3 * 7))'], '21')!
45 check(['/bin/cat', '/etc/inittab'], '::sysinit:/busybox-probe')!
46 println(run(['/bin/busybox', 'sha256sum', '/bin/busybox'])!)
47 $if packaged_musl ? {
48 loader := '/lib/ld-musl-${machine}.so.1'
49 listing := run([loader, '--list', '/busybox-probe'])!
50 if !listing.contains('libc.musl-${machine}.so.1 => ${loader}') {
51 return error('unexpected dynamic dependencies: ${listing}')
52 }
53 println(listing)
54 println(run(['/bin/busybox', 'sha256sum', loader])!)
55 // A freed 256 KiB mapping falls in a retained bucket in the Vinix allocator.
56 C.malloc_trim(0)
57 buffer := unsafe { C.malloc(256 * 1024) }
58 if buffer == unsafe { nil } {
59 return error('malloc failed to provide 256 KiB')
60 }
61 capacity := C.malloc_usable_size(buffer)
62 unsafe { C.free(buffer) }
63 if capacity < 256 * 1024 {
64 return error('malloc supplied a truncated allocation')
65 }
66 if C.malloc_trim(0) == 0 {
67 return error('malloc_trim did not release a mapping after free')
68 }
69 println('Packaged musl released a mapping with malloc_trim.')
70 }
71}
72
73fn main() {
74 if os.getppid() != 1 || run(['/bin/busybox', 'uname', '-s']) or { '' } != 'Vinix' {
75 eprintln('requires a Vinix guest with the probe started directly by init')
76 exit(2)
77 }
78 fd := $if arm64 { C.open(c'/dev/console', C.O_WRONLY) } $else { C.open(c'/dev/com1', C.O_WRONLY) }
79 if fd < 0 || C.dup2(fd, 1) < 0 || C.dup2(fd, 2) < 0 {
80 exit(2)
81 }
82 if fd > 2 {
83 C.close(fd)
84 }
85 verify() or {
86 eprintln('VPAC BUSYBOX GUEST FAIL ${err}')
87 C.reboot(C.RB_POWER_OFF)
88 exit(1)
89 }
90 println('VPAC BUSYBOX GUEST PASS')
91 C.reboot(C.RB_POWER_OFF)
92}
93