vile / scheme / bounded_activation_oom_test.c.v
106 lines · 99 sloc · 4.12 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)
9
10fn install_large_activation(mut vm VM) !Value {
11 function := &Function{
12 code: &Code{}
13 slot_count: 2 * 1024 * 1024
14 constants: [integer(7)]
15 instructions: [bridge_instruction(.constant, 0, 0), bridge_instruction(.ret, 0, 0)]
16 }
17 vm.define('huge', procedure_value(&Procedure{ owner: vm.owner, mode: .closure, function: function }))!
18 return unspecified()
19}
20
21fn test_denied_closure_activation_preserves_the_native_caller_and_saved_closure() {
22 $if gcboehm ? { return }
23 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })!
24 defer { instance.close() or { panic(err) } }
25 instance.eval('(define (saved) 42)')!
26 instance.run(install_large_activation)!
27 instance.run(fn (mut vm VM) !Value {
28 vm.register('denied-activation', fn (mut vm VM, _args []Value) !Value {
29 procedure := vm.lookup('huge')!
30 before := vm.active.len
31 C.vile_bounded_set_deny(1)
32 defer { C.vile_bounded_set_deny(0) }
33 if _ := vm.call(procedure, []) {
34 panic('oversized closure activation unexpectedly succeeded')
35 } else {
36 assert err is ResourceLimitRequest
37 if err is ResourceLimitRequest { assert err.kind == .vm_allocation }
38 }
39 C.vile_bounded_set_deny(0)
40 assert vm.active.len == before
41 return vm.call(vm.lookup('saved')!, [])
42 })!
43 return unspecified()
44 })!
45 assert write(instance.eval('(list (denied-activation) (saved))')!)! == '(42 42)'
46}
47
48fn test_denied_closure_activation_quota_preserves_captured_mutation_in_both_frontends() {
49 mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 8 * 1024 * 1024 })!
50 defer { instance.close() or { panic(err) } }
51 instance.eval('(define cell (let ((n 41))
52 (cons (lambda () (set! n (+ n 1)) n) (lambda () n))))
53 (define (saved) ((cdr cell)))')!
54 instance.run(install_large_activation)!
55 instance.run(fn (mut vm VM) !Value {
56 vm.register('denied-activation', fn (mut vm VM, _args []Value) !Value {
57 if _ := vm.call(vm.lookup('huge')!, []) {
58 panic('closure activation exceeded its hard chunk limit')
59 } else {
60 assert err is ResourceLimitRequest
61 if err is ResourceLimitRequest { assert err.kind == .vm_chunk_bytes }
62 }
63 return vm.call(vm.lookup('saved')!, [])
64 })!
65 return unspecified()
66 })!
67 assert write(instance.eval('(list (denied-activation) (saved))')!)! == '(41 41)'
68 assert write(instance.eval_alt('list(denied-activation(), saved())')!)! == '(41 41)'
69 assert instance.eval('((car cell))')!.as_integer()! == 42
70 assert instance.eval('(saved)')!.as_integer()! == 42
71}
72
73fn test_denied_call_frame_growth_preserves_the_callers_code_and_environment() {
74 $if gcboehm ? { return }
75 mut instance := new_bounded(Options{ max_frames: 0, max_steps: 0 }, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })!
76 defer { instance.close() or { panic(err) } }
77 instance.eval('(define (saved) 42)')!
78 assert instance.run(fn (mut vm VM) !Value {
79 caller := &Function{ code: &Code{} }
80 environment := &Environment{ slots: [integer(41)], escaped: true }
81 callee := &Function{ code: &Code{}, constants: [integer(7)], instructions: [bridge_instruction(.constant, 0, 0), bridge_instruction(.ret, 0, 0)] }
82 procedure := procedure_value(&Procedure{ owner: vm.owner, mode: .closure, function: callee })
83 mut machine := Machine{
84 function: caller
85 env: environment
86 stack: [procedure]
87 frames: []CallFrame{len: 750000, init: CallFrame{ function: caller, env: environment }}
88 }
89 before := machine.frames.len
90 C.vile_bounded_set_deny(1)
91 defer { C.vile_bounded_set_deny(0) }
92 if _ := machine.apply(mut vm, 0, false) {
93 panic('oversized frame growth unexpectedly succeeded')
94 } else {
95 assert err is ResourceLimitRequest
96 if err is ResourceLimitRequest { assert err.kind == .vm_allocation }
97 }
98 C.vile_bounded_set_deny(0)
99 assert machine.frames.len == before
100 assert voidptr(machine.function) == voidptr(caller)
101 assert voidptr(machine.env) == voidptr(environment)
102 assert environment.slots[0].as_integer()! == 41
103 assert vm.owner.bounded_files.interrupted == none
104 return vm.call(vm.lookup('saved')!, [])
105 })!.as_integer()! == 42
106}
107