module scheme import os import time import crypto.sha256 fn native_cache_test_root() string { return os.join_path(os.temp_dir(), 'vile-native-cache-test.${os.getpid()}.${time.sys_mono_now()}') } fn native_cache_restore_env(key string, value string) { if value == '' { os.unsetenv(key) } else { os.setenv(key, value, true) } } fn native_cache_test_headers() map[string]string { mut previous := map[string]string{} for key in ['CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 'OBJC_INCLUDE_PATH', 'SDKROOT'] { previous[key] = os.getenv(key).clone() os.unsetenv(key) } return previous } fn native_cache_restore_headers(previous map[string]string) { for key, value in previous { native_cache_restore_env(key, value) } } fn native_cache_probe(source string, result i64, cache_hits u64, compilations u64) ! { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval(source)! vm.with_native_execution(fn [result, cache_hits, compilations] (mut vm VM) !Value { value := vm.call(vm.lookup('f')!, [integer(5)])! assert value.whole() == result hits, builds := vm.native_compilation_stats() assert hits == cache_hits && builds == compilations return value })! assert isnil(vm.native_execution_scope) } fn test_native_disk_cache_reuses_code_across_vms_without_capturing_environments() { headers := native_cache_test_headers() defer { native_cache_restore_headers(headers) } root := native_cache_test_root() old := os.getenv('VILE_NATIVE_CACHE').clone() os.setenv('VILE_NATIVE_CACHE', root, true) defer { native_cache_restore_env('VILE_NATIVE_CACHE', old); os.rmdir_all(root) or {} } native_cache_probe('(define (make n)(lambda(x)(+ x n))) (define f(make 7))', 12, 0, 1)! native_cache_probe('(define (make n)(lambda(x)(+ x n))) (define f(make 19))', 24, 1, 0)! assert os.ls(root)!.len == 1 // A different body must select another compiled image. native_cache_probe('(define(f x)(* x 3))', 15, 0, 1)! assert os.ls(root)!.len == 2 } fn test_native_disk_cache_corruption_and_missing_symbol_rebuild_safely() { headers := native_cache_test_headers() defer { native_cache_restore_headers(headers) } root := native_cache_test_root() old := os.getenv('VILE_NATIVE_CACHE').clone() os.setenv('VILE_NATIVE_CACHE', root, true) defer { native_cache_restore_env('VILE_NATIVE_CACHE', old); os.rmdir_all(root) or {} } source := '(define(f x)(+ x 7))' native_cache_probe(source, 12, 0, 1)! key := os.ls(root)![0] entry := os.join_path(root, key) current := os.join_path(entry, 'current') manifest := os.read_file(current)!.split_into_lines() bad := os.join_path(entry, manifest[1] + '.dylib') os.chmod(bad, 0o600)! os.write_file(bad, 'corrupt')! native_cache_probe(source, 12, 0, 1)! native_cache_probe(source, 12, 1, 0)! // A valid library with the wrong entry point passes the file hash but must // be closed and replaced before it can become a NativePlan. file := os.join_path(root, 'other.c') library := os.join_path(root, 'other.dylib') os.write_file(file, 'int unrelated(void){return 7;}')! build := os.exec(['clang', '-dynamiclib', file, '-o', library]) assert build.exit_code == 0, build.output bytes := os.read_bytes(library)! digest := sha256.sum256(bytes).hex() os.write_file_array(os.join_path(entry, digest + '.dylib'), bytes)! os.write_file(current, key + '\n' + digest + '\n')! native_cache_probe(source, 12, 0, 1)! assert os.ls(entry)!.all(!it.starts_with('.publish.')) } fn test_native_disk_cache_off_unusable_root_and_limited_vm_keep_fallback_rules() { root := native_cache_test_root() old := os.getenv('VILE_NATIVE_CACHE').clone() defer { native_cache_restore_env('VILE_NATIVE_CACHE', old); os.rm(root) or {} } os.setenv('VILE_NATIVE_CACHE', 'off', true) for _ in 0 .. 2 { native_cache_probe('(define(f x)(+ x 7))', 12, 0, 1)! } os.write_file(root, 'not a cache directory')! os.setenv('VILE_NATIVE_CACHE', root, true) native_cache_probe('(define(f x)(+ x 7))', 12, 0, 1)! os.rm(root)! mut vm := new_with_options(Options{max_steps: 40})! vm.eval('(define(f x)(+ x 7))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('f')!, [integer(5)])!.whole() == 12 assert isnil(vm.native_execution_scope) return unspecified() })! assert !os.exists(root) } fn test_native_cache_identity_includes_build_flags_and_compiler() { code := 'same generated code' plain := native_cache_key(code, 'clang-a', ['-O3']) assert plain == native_cache_key(code, 'clang-a', ['-O3']) assert plain != native_cache_key(code, 'clang-a', ['-O3', '-fsanitize=address,undefined']) assert plain != native_cache_key(code, 'clang-b', ['-O3']) assert plain != native_cache_key(code + ' changed', 'clang-a', ['-O3']) assert native_cache_key('c', 'a', ['b']) != native_cache_key('bc', 'a', []) } fn test_native_disk_cache_custom_header_search_does_not_reuse_opaque_dependencies() { root := native_cache_test_root() headers := root + '-headers' old_cache := os.getenv('VILE_NATIVE_CACHE').clone() old_headers := os.getenv('C_INCLUDE_PATH').clone() os.mkdir_all(headers, mode: 0o700)! os.setenv('VILE_NATIVE_CACHE', root, true) os.setenv('C_INCLUDE_PATH', headers, true) defer { native_cache_restore_env('VILE_NATIVE_CACHE', old_cache) native_cache_restore_env('C_INCLUDE_PATH', old_headers) os.rmdir_all(headers) or {} os.rmdir_all(root) or {} } for _ in 0 .. 2 { native_cache_probe('(define(f x)(+ x 7))', 12, 0, 1)! } assert !os.exists(root) }