module scheme import arena #flag -I @VMODROOT/scheme/testdata/bounded_oom #include "deny_large.h" fn C.vile_bounded_set_deny(int) fn install_large_activation(mut vm VM) !Value { function := &Function{ code: &Code{} slot_count: 2 * 1024 * 1024 constants: [integer(7)] instructions: [bridge_instruction(.constant, 0, 0), bridge_instruction(.ret, 0, 0)] } vm.define('huge', procedure_value(&Procedure{ owner: vm.owner, mode: .closure, function: function }))! return unspecified() } fn test_denied_closure_activation_preserves_the_native_caller_and_saved_closure() { $if gcboehm ? { return } mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })! defer { instance.close() or { panic(err) } } instance.eval('(define (saved) 42)')! instance.run(install_large_activation)! instance.run(fn (mut vm VM) !Value { vm.register('denied-activation', fn (mut vm VM, _args []Value) !Value { procedure := vm.lookup('huge')! before := vm.active.len C.vile_bounded_set_deny(1) defer { C.vile_bounded_set_deny(0) } if _ := vm.call(procedure, []) { panic('oversized closure activation unexpectedly succeeded') } else { assert err is ResourceLimitRequest if err is ResourceLimitRequest { assert err.kind == .vm_allocation } } C.vile_bounded_set_deny(0) assert vm.active.len == before return vm.call(vm.lookup('saved')!, []) })! return unspecified() })! assert write(instance.eval('(list (denied-activation) (saved))')!)! == '(42 42)' } fn test_denied_closure_activation_quota_preserves_captured_mutation_in_both_frontends() { mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 8 * 1024 * 1024 })! defer { instance.close() or { panic(err) } } instance.eval('(define cell (let ((n 41)) (cons (lambda () (set! n (+ n 1)) n) (lambda () n)))) (define (saved) ((cdr cell)))')! instance.run(install_large_activation)! instance.run(fn (mut vm VM) !Value { vm.register('denied-activation', fn (mut vm VM, _args []Value) !Value { if _ := vm.call(vm.lookup('huge')!, []) { panic('closure activation exceeded its hard chunk limit') } else { assert err is ResourceLimitRequest if err is ResourceLimitRequest { assert err.kind == .vm_chunk_bytes } } return vm.call(vm.lookup('saved')!, []) })! return unspecified() })! assert write(instance.eval('(list (denied-activation) (saved))')!)! == '(41 41)' assert write(instance.eval_alt('list(denied-activation(), saved())')!)! == '(41 41)' assert instance.eval('((car cell))')!.as_integer()! == 42 assert instance.eval('(saved)')!.as_integer()! == 42 } fn test_denied_call_frame_growth_preserves_the_callers_code_and_environment() { $if gcboehm ? { return } mut instance := new_bounded(Options{ max_frames: 0, max_steps: 0 }, arena.ChunkLimits{ max_live_bytes: 128 * 1024 * 1024 })! defer { instance.close() or { panic(err) } } instance.eval('(define (saved) 42)')! assert instance.run(fn (mut vm VM) !Value { caller := &Function{ code: &Code{} } environment := &Environment{ slots: [integer(41)], escaped: true } callee := &Function{ code: &Code{}, constants: [integer(7)], instructions: [bridge_instruction(.constant, 0, 0), bridge_instruction(.ret, 0, 0)] } procedure := procedure_value(&Procedure{ owner: vm.owner, mode: .closure, function: callee }) mut machine := Machine{ function: caller env: environment stack: [procedure] frames: []CallFrame{len: 750000, init: CallFrame{ function: caller, env: environment }} } before := machine.frames.len C.vile_bounded_set_deny(1) defer { C.vile_bounded_set_deny(0) } if _ := machine.apply(mut vm, 0, false) { panic('oversized frame growth unexpectedly succeeded') } else { assert err is ResourceLimitRequest if err is ResourceLimitRequest { assert err.kind == .vm_allocation } } C.vile_bounded_set_deny(0) assert machine.frames.len == before assert voidptr(machine.function) == voidptr(caller) assert voidptr(machine.env) == voidptr(environment) assert environment.slots[0].as_integer()! == 41 assert vm.owner.bounded_files.interrupted == none return vm.call(vm.lookup('saved')!, []) })!.as_integer()! == 42 }