vile / scheme / native_cache_d_vile_native_test.v
135 lines · 124 sloc · 5.33 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import os
4import time
5import crypto.sha256
6
7fn native_cache_test_root() string {
8 return os.join_path(os.temp_dir(), 'vile-native-cache-test.${os.getpid()}.${time.sys_mono_now()}')
9}
10
11fn native_cache_restore_env(key string, value string) {
12 if value == '' { os.unsetenv(key) } else { os.setenv(key, value, true) }
13}
14
15fn native_cache_test_headers() map[string]string {
16 mut previous := map[string]string{}
17 for key in ['CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 'OBJC_INCLUDE_PATH', 'SDKROOT'] {
18 previous[key] = os.getenv(key).clone()
19 os.unsetenv(key)
20 }
21 return previous
22}
23
24fn native_cache_restore_headers(previous map[string]string) {
25 for key, value in previous { native_cache_restore_env(key, value) }
26}
27
28fn native_cache_probe(source string, result i64, cache_hits u64, compilations u64) ! {
29 mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})!
30 vm.eval(source)!
31 vm.with_native_execution(fn [result, cache_hits, compilations] (mut vm VM) !Value {
32 value := vm.call(vm.lookup('f')!, [integer(5)])!
33 assert value.whole() == result
34 hits, builds := vm.native_compilation_stats()
35 assert hits == cache_hits && builds == compilations
36 return value
37 })!
38 assert isnil(vm.native_execution_scope)
39}
40
41fn test_native_disk_cache_reuses_code_across_vms_without_capturing_environments() {
42 headers := native_cache_test_headers()
43 defer { native_cache_restore_headers(headers) }
44 root := native_cache_test_root()
45 old := os.getenv('VILE_NATIVE_CACHE').clone()
46 os.setenv('VILE_NATIVE_CACHE', root, true)
47 defer { native_cache_restore_env('VILE_NATIVE_CACHE', old); os.rmdir_all(root) or {} }
48 native_cache_probe('(define (make n)(lambda(x)(+ x n))) (define f(make 7))', 12, 0, 1)!
49 native_cache_probe('(define (make n)(lambda(x)(+ x n))) (define f(make 19))', 24, 1, 0)!
50 assert os.ls(root)!.len == 1
51 // A different body must select another compiled image.
52 native_cache_probe('(define(f x)(* x 3))', 15, 0, 1)!
53 assert os.ls(root)!.len == 2
54}
55
56fn test_native_disk_cache_corruption_and_missing_symbol_rebuild_safely() {
57 headers := native_cache_test_headers()
58 defer { native_cache_restore_headers(headers) }
59 root := native_cache_test_root()
60 old := os.getenv('VILE_NATIVE_CACHE').clone()
61 os.setenv('VILE_NATIVE_CACHE', root, true)
62 defer { native_cache_restore_env('VILE_NATIVE_CACHE', old); os.rmdir_all(root) or {} }
63 source := '(define(f x)(+ x 7))'
64 native_cache_probe(source, 12, 0, 1)!
65 key := os.ls(root)![0]
66 entry := os.join_path(root, key)
67 current := os.join_path(entry, 'current')
68 manifest := os.read_file(current)!.split_into_lines()
69 bad := os.join_path(entry, manifest[1] + '.dylib')
70 os.chmod(bad, 0o600)!
71 os.write_file(bad, 'corrupt')!
72 native_cache_probe(source, 12, 0, 1)!
73 native_cache_probe(source, 12, 1, 0)!
74 // A valid library with the wrong entry point passes the file hash but must
75 // be closed and replaced before it can become a NativePlan.
76 file := os.join_path(root, 'other.c')
77 library := os.join_path(root, 'other.dylib')
78 os.write_file(file, 'int unrelated(void){return 7;}')!
79 build := os.exec(['clang', '-dynamiclib', file, '-o', library])
80 assert build.exit_code == 0, build.output
81 bytes := os.read_bytes(library)!
82 digest := sha256.sum256(bytes).hex()
83 os.write_file_array(os.join_path(entry, digest + '.dylib'), bytes)!
84 os.write_file(current, key + '\n' + digest + '\n')!
85 native_cache_probe(source, 12, 0, 1)!
86 assert os.ls(entry)!.all(!it.starts_with('.publish.'))
87}
88
89fn test_native_disk_cache_off_unusable_root_and_limited_vm_keep_fallback_rules() {
90 root := native_cache_test_root()
91 old := os.getenv('VILE_NATIVE_CACHE').clone()
92 defer { native_cache_restore_env('VILE_NATIVE_CACHE', old); os.rm(root) or {} }
93 os.setenv('VILE_NATIVE_CACHE', 'off', true)
94 for _ in 0 .. 2 { native_cache_probe('(define(f x)(+ x 7))', 12, 0, 1)! }
95 os.write_file(root, 'not a cache directory')!
96 os.setenv('VILE_NATIVE_CACHE', root, true)
97 native_cache_probe('(define(f x)(+ x 7))', 12, 0, 1)!
98 os.rm(root)!
99 mut vm := new_with_options(Options{max_steps: 40})!
100 vm.eval('(define(f x)(+ x 7))')!
101 vm.with_native_execution(fn (mut vm VM) !Value {
102 assert vm.call(vm.lookup('f')!, [integer(5)])!.whole() == 12
103 assert isnil(vm.native_execution_scope)
104 return unspecified()
105 })!
106 assert !os.exists(root)
107}
108
109fn test_native_cache_identity_includes_build_flags_and_compiler() {
110 code := 'same generated code'
111 plain := native_cache_key(code, 'clang-a', ['-O3'])
112 assert plain == native_cache_key(code, 'clang-a', ['-O3'])
113 assert plain != native_cache_key(code, 'clang-a', ['-O3', '-fsanitize=address,undefined'])
114 assert plain != native_cache_key(code, 'clang-b', ['-O3'])
115 assert plain != native_cache_key(code + ' changed', 'clang-a', ['-O3'])
116 assert native_cache_key('c', 'a', ['b']) != native_cache_key('bc', 'a', [])
117}
118
119fn test_native_disk_cache_custom_header_search_does_not_reuse_opaque_dependencies() {
120 root := native_cache_test_root()
121 headers := root + '-headers'
122 old_cache := os.getenv('VILE_NATIVE_CACHE').clone()
123 old_headers := os.getenv('C_INCLUDE_PATH').clone()
124 os.mkdir_all(headers, mode: 0o700)!
125 os.setenv('VILE_NATIVE_CACHE', root, true)
126 os.setenv('C_INCLUDE_PATH', headers, true)
127 defer {
128 native_cache_restore_env('VILE_NATIVE_CACHE', old_cache)
129 native_cache_restore_env('C_INCLUDE_PATH', old_headers)
130 os.rmdir_all(headers) or {}
131 os.rmdir_all(root) or {}
132 }
133 for _ in 0 .. 2 { native_cache_probe('(define(f x)(+ x 7))', 12, 0, 1)! }
134 assert !os.exists(root)
135}
136