vile / scheme / bounded_compilation_oom_test.c.v
194 lines · 181 sloc · 7.39 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import reader
5
6#flag -I @VMODROOT/scheme/testdata/bounded_oom
7#include "deny_large.h"
8
9fn C.vile_bounded_set_deny(int)
10
11fn compilation_denial_document(storage arena.Arena, staged bool) !reader.Document {
12 storage.push()
13 defer { storage.pop() }
14 vector := '#(' + '42 '.repeat(1100000) + ')'
15 if !staged { return reader.read_all(vector) }
16 return reader.read_all("(begin
17 (define protected '#0=(42 . #0#))
18 (define (saved-graph) protected)
19 (define-syntax pick (lambda (stx) (syntax-case stx () ((_ x) #'x))))
20 (set! protected " + vector + '))')
21}
22
23fn test_denied_compiler_literal_preserves_enclosing_entry_and_saved_closure() {
24 $if gcboehm ? { return }
25 // Prepare valid input before injection so denial reaches literal compilation,
26 // rather than the parser or the host-call argument bridge.
27 mut input_owner := arena.new()
28 defer { input_owner.free() }
29 document := compilation_denial_document(input_owner, false)!
30 assert document.arena.node(document.forms[0])!.count == 1100000
31 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })!
32 defer { instance.close() or { panic(err) } }
33 instance.eval('(define (saved) 42)')!
34 setup := fn [document] (mut vm VM) !Value {
35 vm.register('denied-compile', fn [document] (mut vm VM, args []Value) !Value {
36 check_arity('denied-compile', args.len, 0, 0)!
37 before := vm.active.len
38 C.vile_bounded_set_deny(1)
39 defer { C.vile_bounded_set_deny(0) }
40 if _ := vm.eval_document(document, unsafe { nil }, '') {
41 panic('oversized compiler literal unexpectedly succeeded')
42 } else {
43 assert err is ResourceLimitRequest
44 if err is ResourceLimitRequest { assert err.kind == .vm_allocation }
45 }
46 C.vile_bounded_set_deny(0)
47 assert vm.active.len == before
48 return vm.call(vm.lookup('saved')!, [])
49 })!
50 return unspecified()
51 }
52 instance.run(setup)!
53 assert write(instance.eval('(list (denied-compile) (saved))')!)! == '(42 42)'
54 assert instance.eval('(saved)')!.as_integer()! == 42
55}
56
57fn test_interrupted_transformer_native_callback_still_disables_bounded_vm() {
58 $if gcboehm ? { return }
59 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })!
60 defer { instance.close() or { panic(err) } }
61 instance.run(fn (mut vm VM) !Value {
62 vm.register('explode-compiler', fn (mut _vm VM, _args []Value) !Value {
63 return owned_bytevector([]u8{len: 20 * 1024 * 1024})
64 })!
65 return unspecified()
66 })!
67 instance.eval('(define (saved) 42)
68 (define-syntax broken (lambda (stx) (explode-compiler)))')!
69 C.vile_bounded_set_deny(1)
70 defer { C.vile_bounded_set_deny(0) }
71 if _ := instance.eval('(broken)') {
72 panic('native transformer allocation unexpectedly succeeded')
73 } else {
74 assert err is ResourceLimitRequest
75 if err is ResourceLimitRequest { assert err.kind == .vm_allocation }
76 }
77 C.vile_bounded_set_deny(0)
78 assert instance.files.interrupted != none
79 assert instance.vm.active.len == 0
80 if _ := instance.eval('(saved)') { panic('interrupted callback was allowed to resume') }
81}
82
83fn test_denied_compiler_literal_quota_preserves_bindings_in_both_frontends() {
84 mut input_owner := arena.new()
85 defer { input_owner.free() }
86 document := compilation_denial_document(input_owner, true)!
87 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 8 * 1024 * 1024 })!
88 defer { instance.close() or { panic(err) } }
89 instance.eval('(define old 42) (define (saved) old)')!
90 setup := fn [document] (mut vm VM) !Value {
91 vm.register('denied-compile', fn [document] (mut vm VM, _args []Value) !Value {
92 if _ := vm.eval_document(document, unsafe { nil }, '') {
93 panic('compiler literal exceeded its hard chunk limit')
94 } else {
95 assert err is ResourceLimitRequest
96 if err is ResourceLimitRequest { assert err.kind == .vm_chunk_bytes }
97 }
98 return vm.call(vm.lookup('saved')!, [])
99 })!
100 return unspecified()
101 }
102 instance.run(setup)!
103 assert write(instance.eval('(list (denied-compile) (saved))')!)! == '(42 42)'
104 assert write(instance.eval_alt('list(denied-compile(), saved())')!)! == '(42 42)'
105 assert instance.eval('old')!.as_integer()! == 42
106 assert write(instance.eval('(list (eq? protected (saved-graph))
107 (eq? protected (cdr protected)) (pick (car protected)))')!)! == '(#t #t 42)'
108 assert write(instance.eval_alt('list(eq?(protected, saved-graph()),
109 eq?(protected, cdr(protected)), pick(car(protected)))')!)! == '(#t #t 42)'
110}
111
112struct CompilationHostPanic {
113mut:
114 message string
115}
116
117fn intercept_compiler_host_panic(mut instance BoundedVM, mut result CompilationHostPanic) {
118 defer { if message := recover() { result.message = message } }
119 instance.eval('(broken)') or { panic(err) }
120}
121
122fn test_compiler_recovery_preserves_ordinary_host_panics() {
123 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 8 * 1024 * 1024 })!
124 defer { instance.close() or { panic(err) } }
125 instance.run(fn (mut vm VM) !Value {
126 vm.register('host-compiler-panic', fn (mut _vm VM, _args []Value) !Value {
127 panic('ordinary compiler host panic')
128 })!
129 return unspecified()
130 })!
131 instance.eval('(define-syntax broken (lambda (stx) (host-compiler-panic)))')!
132 mut result := CompilationHostPanic{}
133 intercept_compiler_host_panic(mut instance, mut result)
134 assert result.message == 'ordinary compiler host panic'
135 assert instance.vm.active.len == 0
136 assert instance.state == .failed
137}
138
139fn driver_denial_document(storage arena.Arena) !reader.Document {
140 storage.push()
141 defer { storage.pop() }
142 return reader.read_all('42 '.repeat(1100000))
143}
144
145fn test_denied_source_driver_construction_preserves_the_enclosing_entry() {
146 $if gcboehm ? { return }
147 mut input_owner := arena.new()
148 defer { input_owner.free() }
149 document := driver_denial_document(input_owner)!
150 assert document.forms.len == 1100000
151 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })!
152 defer { instance.close() or { panic(err) } }
153 instance.eval('(define (saved) 42)')!
154 instance.run(fn [document] (mut vm VM) !Value {
155 vm.register('denied-driver', fn [document] (mut vm VM, _args []Value) !Value {
156 before := vm.active.len
157 C.vile_bounded_set_deny(1)
158 defer { C.vile_bounded_set_deny(0) }
159 if _ := vm.eval_document(document, unsafe { nil }, '') {
160 panic('oversized source driver unexpectedly succeeded')
161 } else {
162 assert err is ResourceLimitRequest
163 if err is ResourceLimitRequest { assert err.kind == .vm_allocation }
164 }
165 C.vile_bounded_set_deny(0)
166 assert vm.active.len == before
167 return vm.call(vm.lookup('saved')!, [])
168 })!
169 return unspecified()
170 })!
171 assert write(instance.eval('(list (denied-driver) (saved))')!)! == '(42 42)'
172}
173
174fn test_denied_source_driver_quota_leaves_earlier_bindings_usable() {
175 mut input_owner := arena.new()
176 defer { input_owner.free() }
177 document := driver_denial_document(input_owner)!
178 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 8 * 1024 * 1024 })!
179 defer { instance.close() or { panic(err) } }
180 instance.eval('(define (saved) 42)')!
181 instance.run(fn [document] (mut vm VM) !Value {
182 vm.register('denied-driver', fn [document] (mut vm VM, _args []Value) !Value {
183 if _ := vm.eval_document(document, unsafe { nil }, '') {
184 panic('source driver exceeded its hard chunk limit')
185 } else {
186 assert err is ResourceLimitRequest
187 if err is ResourceLimitRequest { assert err.kind == .vm_chunk_bytes }
188 }
189 return vm.call(vm.lookup('saved')!, [])
190 })!
191 return unspecified()
192 })!
193 assert write(instance.eval('(list (denied-driver) (saved))')!)! == '(42 42)'
194}
195