vile / scheme / bounded_vm_oom_test.c.v
156 lines · 146 sloc · 5.53 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5#flag -I @VMODROOT/scheme/testdata/bounded_oom
6#include "deny_large.h"
7
8fn C.vile_bounded_set_deny(int)
9fn C.vile_bounded_fail_large_on(int)
10fn C.vile_bounded_large_attempts() int
11
12fn test_denied_program_activation_preserves_the_enclosing_entry_and_saved_closure() {
13 $if gcboehm ? { return }
14 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })!
15 defer { instance.close() or { panic(err) } }
16 instance.eval('(define (saved) 42)')!
17 setup := fn (mut vm VM) !Value {
18 vm.register('denied-activation', fn (mut vm VM, args []Value) !Value {
19 check_arity('denied-activation', args.len, 0, 0)!
20 function := &Function{
21 code: unsafe { nil }
22 slot_count: 2 * 1024 * 1024
23 constants: [integer(7)]
24 instructions: [bridge_instruction(.constant, 0, 0), bridge_instruction(.ret, 0, 0)]
25 }
26 before := vm.active.len
27 C.vile_bounded_set_deny(1)
28 defer { C.vile_bounded_set_deny(0) }
29 if _ := vm.run_program(function, vm.next_context()) {
30 panic('oversized activation unexpectedly succeeded')
31 } else {
32 assert err is ResourceLimitRequest
33 if err is ResourceLimitRequest { assert err.kind == .vm_allocation }
34 }
35 C.vile_bounded_set_deny(0)
36 assert vm.active.len == before
37 return vm.call(vm.lookup('saved')!, [])
38 })!
39 return unspecified()
40 }
41 instance.run(setup)!
42 assert write(instance.eval('(list (denied-activation) (saved))')!)! == '(42 42)'
43 assert instance.eval('(saved)')!.as_integer()! == 42
44}
45
46fn test_denied_tracked_binding_preserves_names_values_and_syntax_generations() {
47 $if gcboehm ? { return }
48 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 160 * 1024 * 1024 })!
49 defer { instance.close() or { panic(err) } }
50 key := 'q'.repeat(17 * 1024 * 1024)
51 work := fn [key] (mut vm VM) !Value {
52 env := vm.new_environment()!
53 vm.define_in(env, 'old', integer(42))!
54 vm.eval('(define-syntax marker (syntax-rules () ((_) 42)))')!
55 space := vm.environment_space(env)!
56 assert vm.owner.syntax_tracking
57 epoch := vm.binding_epoch
58 generation := vm.owner.syntax_generation
59 count := vm.globals.len
60 identity := space.syntax_identity
61 // The location's spelling and its map key precede the ordinary binding
62 // key. Denying the third reservation exercises rollback of both tables.
63 for attempt in 1 .. 4 {
64 C.vile_bounded_fail_large_on(attempt)
65 if _ := vm.bind_global_in(space, key, integer(7)) {
66 panic('oversized binding unexpectedly succeeded')
67 } else {
68 assert err is ResourceLimitRequest
69 if err is ResourceLimitRequest { assert err.kind == .vm_allocation }
70 }
71 assert C.vile_bounded_large_attempts() == attempt
72 C.vile_bounded_fail_large_on(0)
73 assert key !in space.bindings && key !in space.syntax_locations
74 assert vm.globals.len == count && vm.binding_epoch == epoch
75 assert vm.owner.syntax_generation == generation && space.syntax_identity == identity
76 }
77 vm.define_in(env, 'next', integer(1))!
78 location := syntax_global_location(vm.owner, space, 'next') or { return error('missing binding location') }
79 assert location.namespace == space.syntax_identity && location.slot == space.bindings['next']
80 return vm.lookup_in(env, 'old')
81 }
82 defer { C.vile_bounded_fail_large_on(0) }
83 assert instance.run(work)!.as_integer()! == 42
84 assert instance.eval('(marker)')!.as_integer()! == 42
85}
86
87fn attempt_library_entry(mut m Machine, library &Library, mut result BoundedResult) {
88 defer {
89 if msg := recover() {
90 result.exhausted = arena.allocation_failure() or { panic(msg) }
91 }
92 }
93 m.enter_library_context(library)
94}
95
96fn test_denied_library_context_allocation_keeps_activation_unpublished() {
97 mut region := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 })
98 defer { region.free() }
99 mut machine := Machine{}
100 library := &Library{ space: unsafe { nil } }
101 mut result := BoundedResult{}
102 region.push()
103 attempt_library_entry(mut machine, library, mut result)
104 region.pop()
105 reason := result.exhausted or { panic('expected the library allocation to be denied') }
106 assert reason == .chunk_limit
107 assert isnil(machine.library_context)
108 assert library.status == .registered
109 assert library.active == 0
110}
111
112fn allocate_while_libc_refuses_big_chunks(mut _vm VM) !Value {
113 C.vile_bounded_set_deny(1)
114 defer { C.vile_bounded_set_deny(0) }
115 return owned_bytevector([]u8{len: 16 * 1024 * 1024})
116}
117
118fn test_physical_chunk_failure_is_typed_and_rolls_back_the_chunk_reservation() {
119 $if gcboehm ? {
120 return
121 }
122 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_total_bytes: 128 * 1024 * 1024 })!
123 defer { instance.close() or { panic(err) } }
124 before := instance.chunk_usage() or { panic('expected budget') }
125 if _ := instance.run(allocate_while_libc_refuses_big_chunks) {
126 assert false
127 } else {
128 assert err is ResourceLimitRequest
129 if err is ResourceLimitRequest {
130 assert err.kind == .vm_allocation
131 }
132 }
133 after := instance.chunk_usage() or { panic('expected budget') }
134 assert after == before
135 assert !arena.active()
136}
137
138fn test_physical_failure_before_publishing_a_vector_preserves_the_existing_vm() {
139 $if gcboehm ? {
140 return
141 }
142 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_total_bytes: 128 * 1024 * 1024 })!
143 defer { instance.close() or { panic(err) } }
144 instance.eval('(define previous 42)')!
145 C.vile_bounded_set_deny(1)
146 defer { C.vile_bounded_set_deny(0) }
147 if _ := instance.eval('(define previous (make-bytevector 20000000))') {
148 assert false
149 } else {
150 assert err is ResourceLimitRequest
151 if err is ResourceLimitRequest {
152 assert err.kind == .vm_allocation
153 }
154 }
155 assert (instance.eval('previous')!).as_integer()! == 42
156}
157