module scheme import arena #flag -I @VMODROOT/scheme/testdata/bounded_oom #include "deny_large.h" fn C.vile_bounded_set_deny(int) fn C.vile_bounded_fail_large_on(int) fn C.vile_bounded_large_attempts() int fn test_denied_program_activation_preserves_the_enclosing_entry_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)')! setup := fn (mut vm VM) !Value { vm.register('denied-activation', fn (mut vm VM, args []Value) !Value { check_arity('denied-activation', args.len, 0, 0)! function := &Function{ code: unsafe { nil } slot_count: 2 * 1024 * 1024 constants: [integer(7)] instructions: [bridge_instruction(.constant, 0, 0), bridge_instruction(.ret, 0, 0)] } before := vm.active.len C.vile_bounded_set_deny(1) defer { C.vile_bounded_set_deny(0) } if _ := vm.run_program(function, vm.next_context()) { panic('oversized 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() } instance.run(setup)! assert write(instance.eval('(list (denied-activation) (saved))')!)! == '(42 42)' assert instance.eval('(saved)')!.as_integer()! == 42 } fn test_denied_tracked_binding_preserves_names_values_and_syntax_generations() { $if gcboehm ? { return } mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 160 * 1024 * 1024 })! defer { instance.close() or { panic(err) } } key := 'q'.repeat(17 * 1024 * 1024) work := fn [key] (mut vm VM) !Value { env := vm.new_environment()! vm.define_in(env, 'old', integer(42))! vm.eval('(define-syntax marker (syntax-rules () ((_) 42)))')! space := vm.environment_space(env)! assert vm.owner.syntax_tracking epoch := vm.binding_epoch generation := vm.owner.syntax_generation count := vm.globals.len identity := space.syntax_identity // The location's spelling and its map key precede the ordinary binding // key. Denying the third reservation exercises rollback of both tables. for attempt in 1 .. 4 { C.vile_bounded_fail_large_on(attempt) if _ := vm.bind_global_in(space, key, integer(7)) { panic('oversized binding unexpectedly succeeded') } else { assert err is ResourceLimitRequest if err is ResourceLimitRequest { assert err.kind == .vm_allocation } } assert C.vile_bounded_large_attempts() == attempt C.vile_bounded_fail_large_on(0) assert key !in space.bindings && key !in space.syntax_locations assert vm.globals.len == count && vm.binding_epoch == epoch assert vm.owner.syntax_generation == generation && space.syntax_identity == identity } vm.define_in(env, 'next', integer(1))! location := syntax_global_location(vm.owner, space, 'next') or { return error('missing binding location') } assert location.namespace == space.syntax_identity && location.slot == space.bindings['next'] return vm.lookup_in(env, 'old') } defer { C.vile_bounded_fail_large_on(0) } assert instance.run(work)!.as_integer()! == 42 assert instance.eval('(marker)')!.as_integer()! == 42 } fn attempt_library_entry(mut m Machine, library &Library, mut result BoundedResult) { defer { if msg := recover() { result.exhausted = arena.allocation_failure() or { panic(msg) } } } m.enter_library_context(library) } fn test_denied_library_context_allocation_keeps_activation_unpublished() { mut region := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 }) defer { region.free() } mut machine := Machine{} library := &Library{ space: unsafe { nil } } mut result := BoundedResult{} region.push() attempt_library_entry(mut machine, library, mut result) region.pop() reason := result.exhausted or { panic('expected the library allocation to be denied') } assert reason == .chunk_limit assert isnil(machine.library_context) assert library.status == .registered assert library.active == 0 } fn allocate_while_libc_refuses_big_chunks(mut _vm VM) !Value { C.vile_bounded_set_deny(1) defer { C.vile_bounded_set_deny(0) } return owned_bytevector([]u8{len: 16 * 1024 * 1024}) } fn test_physical_chunk_failure_is_typed_and_rolls_back_the_chunk_reservation() { $if gcboehm ? { return } mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_total_bytes: 128 * 1024 * 1024 })! defer { instance.close() or { panic(err) } } before := instance.chunk_usage() or { panic('expected budget') } if _ := instance.run(allocate_while_libc_refuses_big_chunks) { assert false } else { assert err is ResourceLimitRequest if err is ResourceLimitRequest { assert err.kind == .vm_allocation } } after := instance.chunk_usage() or { panic('expected budget') } assert after == before assert !arena.active() } fn test_physical_failure_before_publishing_a_vector_preserves_the_existing_vm() { $if gcboehm ? { return } mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_total_bytes: 128 * 1024 * 1024 })! defer { instance.close() or { panic(err) } } instance.eval('(define previous 42)')! C.vile_bounded_set_deny(1) defer { C.vile_bounded_set_deny(0) } if _ := instance.eval('(define previous (make-bytevector 20000000))') { assert false } else { assert err is ResourceLimitRequest if err is ResourceLimitRequest { assert err.kind == .vm_allocation } } assert (instance.eval('previous')!).as_integer()! == 42 }