| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | enum DirectActivationCase { |
| 6 | scope |
| 7 | syntax |
| 8 | promise |
| 9 | port |
| 10 | } |
| 11 | |
| 12 | fn deny_direct_activation(mut vm VM, mut machine Machine, kind DirectActivationCase, function &Function, state &CustomPortOperation) ! { |
| 13 | mut denied := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 }) |
| 14 | denied.push() |
| 15 | defer { |
| 16 | denied.pop() |
| 17 | denied.free() |
| 18 | } |
| 19 | match kind { |
| 20 | .scope { machine.call_scope(mut vm, 0, false)! } |
| 21 | .syntax { machine.call_syntax_scope(mut vm, false)! } |
| 22 | .promise { machine.enter_force(mut vm)! } |
| 23 | .port { machine.call_custom_port_bridge(mut vm, function, state, 0, false)! } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn check_direct_activation_denial(mut vm VM, kind DirectActivationCase) !Value { |
| 28 | caller := &Function{ |
| 29 | code: &Code{} |
| 30 | constants: [integer(41)] |
| 31 | instructions: [bridge_instruction(.constant, 0, 0), bridge_instruction(.ret, 0, 0)] |
| 32 | } |
| 33 | mut parent := &Environment{ slots: [integer(41)], escaped: true } |
| 34 | mut environment := parent |
| 35 | mut function := unsafe { &Function(nil) } |
| 36 | mut state := unsafe { &CustomPortOperation(nil) } |
| 37 | mut value := unspecified() |
| 38 | mut stack := []Value{cap: 2} |
| 39 | match kind { |
| 40 | .scope { |
| 41 | procedure := vm.eval('(let ((n 41)) (lambda () n))')!.procedure_pointer() |
| 42 | function = procedure.function |
| 43 | parent = procedure.env |
| 44 | environment = parent |
| 45 | assert function.capture_depth > 0 |
| 46 | stack << payload_value(.scope_function, function) |
| 47 | } |
| 48 | .syntax { |
| 49 | function = vm.eval('(lambda (argument) (+ argument 1))')!.procedure_pointer().function |
| 50 | assert function.slot_count > 0 |
| 51 | stack << payload_value(.scope_function, function) |
| 52 | stack << integer(7) |
| 53 | } |
| 54 | .promise { |
| 55 | value = vm.eval('(let ((n 41)) (delay (+ n 1)))')! |
| 56 | promise := promise_pointer(value) |
| 57 | function = promise.function |
| 58 | parent = promise.env |
| 59 | environment = &Environment{ slots: [value], escaped: true } |
| 60 | assert function.capture_depth > 0 |
| 61 | } |
| 62 | .port { |
| 63 | value = vm.eval('(define calls 0) |
| 64 | (make-custom-binary-input-port "activation" |
| 65 | (lambda (bytes start count) |
| 66 | (set! calls (+ calls 1)) |
| 67 | (bytevector-u8-set! bytes start 42) 1) #f #f #f)')! |
| 68 | state = vm.prepare_custom_port_operation(.get_u8, value, [value])! |
| 69 | procedure := vm.lookup('get-u8')! |
| 70 | function = procedure.procedure_pointer().function |
| 71 | stack << procedure |
| 72 | stack << value |
| 73 | } |
| 74 | } |
| 75 | mut machine := Machine{ |
| 76 | function: caller |
| 77 | env: environment |
| 78 | stack: stack |
| 79 | frames: []CallFrame{cap: 1} |
| 80 | } |
| 81 | before := vm.active.len |
| 82 | mut rejected := ?IError(none) |
| 83 | deny_direct_activation(mut vm, mut machine, kind, function, state) or { rejected = err } |
| 84 | err := rejected or { return error('direct activation ignored the zero chunk budget') } |
| 85 | assert err is ResourceLimitRequest |
| 86 | if err is ResourceLimitRequest { assert err.kind == .vm_chunk_bytes } |
| 87 | assert vm.active.len == before |
| 88 | assert vm.owner.bounded_files.interrupted == none |
| 89 | assert voidptr(machine.function) == voidptr(caller) |
| 90 | assert voidptr(machine.env) == voidptr(environment) |
| 91 | assert machine.frames.len == 0 |
| 92 | assert parent.slots[0].as_integer()! == 41 |
| 93 | match kind { |
| 94 | .scope { assert machine.stack.len == 0 } |
| 95 | .syntax { assert machine.stack.len == 2 } |
| 96 | .promise { |
| 97 | promise := promise_pointer(value) |
| 98 | assert !promise.ready && !promise.forwarded |
| 99 | assert voidptr(promise.function) == voidptr(function) |
| 100 | assert vm.call(vm.lookup('force')!, [value])!.as_integer()! == 42 |
| 101 | assert promise.ready && promise.result.as_integer()! == 42 |
| 102 | } |
| 103 | .port { |
| 104 | assert !value.input_pointer().busy && !value.input_pointer().closed |
| 105 | assert isnil(state.resource.active) |
| 106 | assert vm.lookup('calls')!.as_integer()! == 0 |
| 107 | assert vm.call(vm.lookup('get-u8')!, [value])!.as_integer()! == 42 |
| 108 | assert vm.lookup('calls')!.as_integer()! == 1 |
| 109 | vm.close_port(value)! |
| 110 | } |
| 111 | } |
| 112 | return vm.call(vm.lookup('saved')!, []) |
| 113 | } |
| 114 | |
| 115 | fn direct_activation_denial_case(kind DirectActivationCase) ! { |
| 116 | mut instance := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 8 * 1024 * 1024 })! |
| 117 | defer { instance.close() or { panic(err) } } |
| 118 | instance.eval('(define (saved) 42)')! |
| 119 | assert instance.run(fn [kind] (mut vm VM) !Value { |
| 120 | return check_direct_activation_denial(mut vm, kind) |
| 121 | })!.as_integer()! == 42 |
| 122 | assert instance.eval('(saved)')!.as_integer()! == 42 |
| 123 | assert instance.eval_alt('saved()')!.as_integer()! == 42 |
| 124 | } |
| 125 | |
| 126 | fn test_denied_immediate_scope_activation_preserves_its_captured_parent() { |
| 127 | direct_activation_denial_case(.scope)! |
| 128 | } |
| 129 | |
| 130 | fn test_denied_syntax_scope_activation_preserves_its_argument_packet() { |
| 131 | direct_activation_denial_case(.syntax)! |
| 132 | } |
| 133 | |
| 134 | fn test_denied_promise_activation_leaves_the_thunk_retryable() { |
| 135 | direct_activation_denial_case(.promise)! |
| 136 | } |
| 137 | |
| 138 | fn test_denied_custom_port_activation_does_not_enter_the_lease_or_callback() { |
| 139 | direct_activation_denial_case(.port)! |
| 140 | } |
| 141 | |