vile / freestanding_test.v
166 lines · 158 sloc · 6.93 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module main
2
3import os
4import time
5import encoding.binary as endian
6
7struct FreestandingCliCase {
8 args []string
9 output string
10 status int
11}
12
13fn require_standalone_binary(path string) ! {
14 $if macos && amd64 {
15 bytes := os.read_bytes(path)!
16 assert bytes.len >= 32
17 assert endian.little_endian_u32_at(bytes, 0) == 0xfeedfacf
18 assert endian.little_endian_u32_at(bytes, 12) == 2, 'expected a Mach-O executable'
19 assert endian.little_endian_u32_at(bytes, 24) & 4 == 0, 'a standalone executable must not require dyld'
20 count := endian.little_endian_u32_at(bytes, 16)
21 commands_size := endian.little_endian_u32_at(bytes, 20)
22 assert u64(commands_size) <= u64(bytes.len - 32)
23 mut offset := 32
24 mut entries := 0
25 for _ in 0 .. count {
26 assert offset + 8 <= 32 + commands_size
27 kind := endian.little_endian_u32_at(bytes, offset)
28 size := endian.little_endian_u32_at(bytes, offset + 4)
29 assert size >= 8 && u64(size) <= u64(32 + commands_size - offset)
30 // All dylib loading commands, including weak/reexport/upward variants.
31 assert kind !in [u32(0xc), 0xe, 0x80000018, 0x8000001f, 0x20, 0x80000023], 'a freestanding Mach-O must not load libraries or an interpreter'
32 if kind == 5 { entries++ } // LC_UNIXTHREAD supplies the raw entry point.
33 if kind == 0xb { // LC_DYSYMTAB declares the undefined-symbol range.
34 assert size >= 80
35 assert endian.little_endian_u32_at(bytes, offset + 28) == 0, 'a freestanding Mach-O must not import runtime symbols'
36 }
37 offset += int(size)
38 }
39 assert offset == 32 + commands_size && entries == 1
40 return
41 }
42
43 $if !linux {
44 return
45 }
46 bytes := os.read_bytes(path)!
47 assert bytes.len >= 64 && bytes[..4] == [u8(0x7f), `E`, `L`, `F`]
48 assert bytes[4] == 2 && bytes[5] == 1
49 start := int(endian.little_endian_u64_at(bytes, 32))
50 stride := int(endian.little_endian_u16_at(bytes, 54))
51 count := int(endian.little_endian_u16_at(bytes, 56))
52 for i in 0 .. count {
53 entry := start + stride * i
54 assert entry >= 0 && entry + 56 <= bytes.len
55 kind := endian.little_endian_u32_at(bytes, entry)
56 assert kind != 3, 'a freestanding Linux binary must not need an ELF interpreter'
57 if kind == 2 {
58 offset := int(endian.little_endian_u64_at(bytes, entry + 8))
59 size := int(endian.little_endian_u64_at(bytes, entry + 32))
60 assert offset >= 0 && size >= 0 && offset + size <= bytes.len
61 for j := 0; j + 16 <= size; j += 16 {
62 assert endian.little_endian_u64_at(bytes, offset + j) != 1, 'a freestanding Linux binary must not load shared libraries'
63 }
64 }
65 }
66}
67
68// All executed fixtures have bounded output. A runtime regression must fail the
69// integration test rather than leave a blocked standalone process behind.
70fn run_freestanding_program(command []string) os.Result {
71 mut child := os.new_process(command[0])
72 child.set_args(command[1..])
73 child.set_redirect_stdio_merged()
74 child.run()
75 defer { child.close() }
76 deadline := time.sys_mono_now() + u64(10 * time.second)
77 for child.is_alive() && time.sys_mono_now() < deadline {
78 time.sleep(5 * time.millisecond)
79 }
80 timed_out := child.is_alive()
81 if timed_out { child.signal_kill() }
82 child.wait()
83 message := child.stdout_slurp()
84 assert !timed_out, 'standalone program timed out: ${command}: ${message}'
85 return os.Result{ exit_code: child.code, output: message }
86}
87
88fn test_freestanding_cli_and_embedded_runtime() ! {
89 $if (!linux && !macos) || (!amd64 && !arm64) {
90 return
91 }
92 root := @VMODROOT
93 work := os.join_path(os.vtmp_dir(), 'vile_freestanding_${os.getpid()}')
94 os.mkdir_all(work)!
95 defer { os.rmdir_all(work) or {} }
96 for production in [false, true] {
97 mut flags := [@VEXE, '-new-compiler', '-no-retry-compilation', '-cc', 'clang', '-freestanding',
98 '-gc', 'none']
99 source_path := os.getenv('VILE_TEST_VPATH')
100 if source_path != '' { flags << ['-path', source_path] }
101 if production { flags << '-prod' }
102 binary := os.join_path(work, 'vile-${production}')
103 mut cli_command := flags.clone()
104 cli_command << ['-o', binary, os.join_path(root, 'main.v')]
105 cli := os.exec(cli_command)
106 assert cli.exit_code == 0, cli.output
107 require_standalone_binary(binary)!
108 for case in [
109 FreestandingCliCase{ args: ['-s', '-e', '(+ 20 22)'], output: '42' },
110 FreestandingCliCase{ args: ['-v', '-e', '"hello " + "λ"'], output: '"hello λ"' },
111 FreestandingCliCase{
112 args: ['-s', '-e',
113 '(import (scheme base)) (list (bytevector-u8-ref #u8(1 255) 1) (vector-ref #(20 22) 1))']
114 output: '(255 22)'
115 },
116 FreestandingCliCase{ args: ['-s', '-e', '(exit 7)'], status: 7 },
117 FreestandingCliCase{ args: ['-s', '-e', '(emergency-exit 9)'], status: 9 },
118 ] {
119 mut run_command := [binary]
120 run_command << case.args
121 result := run_freestanding_program(run_command)
122 assert result.exit_code == case.status, result.output
123 assert result.output.trim_space() == case.output, result.output
124 }
125 program := os.join_path(work, 'with spaces.vile')
126 os.write_file(program, 'display(20+22)\nnewline()')!
127 file_run := run_freestanding_program([binary, program])
128 assert file_run.exit_code == 0 && file_run.output == '42\n', file_run.output
129 // Resolve a directory symlink before `..`, then resolve includes from
130 // that canonical source directory. The lexical destination is a decoy.
131 tree := os.join_path(work, 'paths-${production}')
132 os.mkdir_all(os.join_path(tree, 'real', 'inner'))!
133 os.symlink('real/inner', os.join_path(tree, 'link'))!
134 os.write_file(os.join_path(tree, 'entry.scm'), '(display 17) (newline)')!
135 os.write_file(os.join_path(tree, 'real', 'datum.scm'), '(define value 41)')!
136 os.write_file(os.join_path(tree, 'real', 'entry.scm'),
137 '(include "datum.scm") (display (+ value 1)) (newline)')!
138 linked := run_freestanding_program([binary, '-s', os.join_path(tree, 'link') + '/../entry.scm'])
139 assert linked.exit_code == 0 && linked.output == '42\n', linked.output
140 broken := run_freestanding_program([binary, '-s', '-e', '(car 3)'])
141 assert broken.exit_code == 1 && broken.output.contains('pair'), broken.output
142
143 host := os.join_path(work, 'host-${production}')
144 mut host_command := flags.clone()
145 host_command << ['-o', host, os.join_path(root, 'tests', 'freestanding_host', 'main.v')]
146 built := os.exec(host_command)
147 assert built.exit_code == 0, built.output
148 require_standalone_binary(host)!
149 ran := run_freestanding_program([host, root])
150 assert ran.exit_code == 0, ran.output
151 assert ran.output == 'freestanding embedding passed\n', ran.output
152
153 // The existing context host uses standard V channels/timers and a native
154 // cancellation thread outside its VM arena.
155 context_host := os.join_path(work, 'context-${production}')
156 mut context_command := flags.clone()
157 context_command << ['-o', context_host,
158 os.join_path(root, 'examples', 'context_host', 'main.v')]
159 context_built := os.exec(context_command)
160 assert context_built.exit_code == 0, context_built.output
161 require_standalone_binary(context_host)!
162 canceled := run_freestanding_program([context_host])
163 assert canceled.exit_code == 0, canceled.output
164 assert canceled.output.contains('deadline exceeded'), canceled.output
165 }
166}
167