| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | import arena |
| 5 | |
| 6 | struct ValuesForward400Observation { |
| 7 | text string |
| 8 | steps int |
| 9 | error string |
| 10 | effects string |
| 11 | } |
| 12 | |
| 13 | // Execute unchanged bytecode one instruction at a time as an independent |
| 14 | // reference. In particular, do not infer step costs from the proposed plan. |
| 15 | fn values_forward400_observe(source string, single bool, limit int) !ValuesForward400Observation { |
| 16 | mut vm := new_with_options(Options{max_steps: limit, max_frames: 0})! |
| 17 | invocation := vm.next_context() |
| 18 | function := vm.new_source_program(reader.read_all(source)!, invocation, unsafe { nil }, '', false)! |
| 19 | original := function.instructions.clone() |
| 20 | mut machine := Machine{function: function, context: invocation, logical_context: invocation} |
| 21 | entry := vm.enter_program(mut machine)! |
| 22 | defer { vm.leave_entry(entry) } |
| 23 | mut message := '' |
| 24 | mut value := unspecified() |
| 25 | if single { |
| 26 | for !machine.finished { |
| 27 | assert machine.steps < 100000 |
| 28 | ins := machine.function.instructions[machine.ip] |
| 29 | machine.ip++ |
| 30 | machine.origin = ins.origin |
| 31 | machine.charge(limit) or { message = machine.located(err).msg(); break } |
| 32 | machine.execute(mut vm, ins) or { message = machine.located(err).msg(); break } |
| 33 | } |
| 34 | if message == '' { value = machine.stack[0] } |
| 35 | } else { |
| 36 | value = machine.run(mut vm) or { message = err.msg(); unspecified() } |
| 37 | } |
| 38 | assert function.instructions == original |
| 39 | effect := vm.lookup('effects') or { unspecified() } |
| 40 | return ValuesForward400Observation{ |
| 41 | text: if message == '' { write(value)! } else { '' } |
| 42 | steps: machine.steps |
| 43 | error: message |
| 44 | effects: write(effect)! |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn values_forward400_compare(source string, expected string) ! { |
| 49 | fast := values_forward400_observe(source, false, 0)! |
| 50 | plain := values_forward400_observe(source, true, 0)! |
| 51 | assert fast.error == '' && fast.text == expected, source |
| 52 | assert fast == plain, source |
| 53 | } |
| 54 | |
| 55 | fn test_values_forward400_results_aliases_mutation_and_live_bindings() { |
| 56 | for source,expected in { |
| 57 | '(list (call-with-values (lambda()(values)) list) (call-with-values (lambda()(values 42)) list) (call-with-values (lambda()(values 20 22)) +))': '(() (42) 42)' |
| 58 | '(define pack values)(define bridge call-with-values)(bridge (lambda()(pack 20 21 22)) (lambda x x))': '(20 21 22)' |
| 59 | '(define v(vector 1))(call-with-values (lambda()(values v v)) (lambda(a b)(vector-set! a 0 42)(list(eq? a b)(vector-ref b 0))))': '(#t 42)' |
| 60 | '(let((values(lambda(a b)(list a b)))) (call-with-values (lambda()(values 20 22)) list))': '((20 22))' |
| 61 | '(define p values)(set! values (lambda(a b)99))(call-with-values (lambda()(p 20 22)) +)': '42' |
| 62 | '(define effects 0)(call-with-values (lambda()(set! effects 1)(values 20 22)) (lambda(a b)(set! effects(+ effects 1))(+ a b effects)))': '44' |
| 63 | } { values_forward400_compare(source,expected)! } |
| 64 | } |
| 65 | |
| 66 | fn test_values_forward400_preserves_each_budget_error_and_effect_boundary() { |
| 67 | for source in [ |
| 68 | '(define effects 0)(call-with-values (lambda()(set! effects 1)(values 20 22)) (lambda(a b)(set! effects 2)(+ a b)))', |
| 69 | '(define effects 0)(call-with-values (lambda()(set! effects 1)(values 20 22)) (lambda(a)(set! effects 2)a))', |
| 70 | '(define effects 0)(call-with-values (lambda()(set! effects 1)(values 20 22)) #f)', |
| 71 | ] { |
| 72 | full := values_forward400_observe(source,false,0)! |
| 73 | assert full.steps < 160 |
| 74 | for limit in 0 .. full.steps + 2 { |
| 75 | fast := values_forward400_observe(source,false,limit)! |
| 76 | plain := values_forward400_observe(source,true,limit)! |
| 77 | assert fast == plain, '${limit}: ${source}: ${fast} != ${plain}' |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn test_values_forward400_growth_continuations_and_nested_tail_consumers() { |
| 83 | args := []string{len: 300, init: index.str()}.join(' ') |
| 84 | values_forward400_compare('(call-with-values (lambda()(values ${args})) (lambda x(length x)))','300')! |
| 85 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 20000})! |
| 86 | vm.eval('(define saved #f)(define kept #f) |
| 87 | (define (producer)(let((x(call/cc(lambda(k)(set! saved k)20))))(values x 22))) |
| 88 | (define (consumer a b)(set! kept(lambda()a))(+ a b))')! |
| 89 | assert vm.eval('(call-with-values producer consumer)')!.whole() == 42 |
| 90 | for n in [30,40] { |
| 91 | gc_collect() |
| 92 | assert vm.call(vm.lookup('saved')!,[integer(n)])!.whole() == n + 22 |
| 93 | assert vm.call(vm.lookup('kept')!,[])!.whole() == n |
| 94 | } |
| 95 | vm.eval('(define (nested n) (if (= n 0) (values 20 22) |
| 96 | (call-with-values (lambda()(nested(- n 1))) values)))')! |
| 97 | assert vm.eval('(call-with-values (lambda()(nested 4096)) +)')!.whole() == 42 |
| 98 | assert vm.active.len == 0 |
| 99 | } |
| 100 | |
| 101 | fn test_values_forward400_repeated_packet_allocation_is_bounded() { |
| 102 | mut region := arena.new(chunk_size: 1048576) |
| 103 | region.push() |
| 104 | defer { region.pop(); region.free() } |
| 105 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 16})! |
| 106 | vm.eval('(define (producer)(values 20 22))(define (consumer a b)(+ a b)) |
| 107 | (define (loop n acc)(if (= n 0) acc |
| 108 | (loop(- n 1)(+ acc(call-with-values producer consumer)))))')! |
| 109 | procedure := vm.lookup('loop')! |
| 110 | mut args := [integer(100),integer(0)] |
| 111 | assert vm.call(procedure,args)!.whole() == 4200 |
| 112 | mut retained := []int{cap: 2} |
| 113 | for count in [100,10000] { |
| 114 | container_store_validated(mut args,0,integer(count)) |
| 115 | before := region.used() |
| 116 | result := vm.call(procedure,args)! |
| 117 | used := region.used() - before |
| 118 | assert result.whole() == i64(count) * 42 |
| 119 | retained << used |
| 120 | println('VALUES400_ALLOCATION ${count} ${used}') |
| 121 | } |
| 122 | assert retained[1] <= retained[0] + 512, retained.str() |
| 123 | } |
| 124 | |
| 125 | fn test_values_forward400_handoff_preserves_registers_roots_and_private_buffers() { |
| 126 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 127 | producer := vm.eval('(lambda()(values 20 22))')! |
| 128 | consumer := vm.lookup('+')! |
| 129 | bridge_env := &Environment{slots: [producer,consumer]} |
| 130 | mut machine := Machine{function: producer.procedure_pointer().function, |
| 131 | env: &Environment{slots: [integer(99)]},base: 2,steps: 9, |
| 132 | stack: [integer(9),consumer,integer(88),vm.lookup('values')!,integer(20),integer(22)], |
| 133 | frames: [CallFrame{function: &call_values_bridge_function,env: bridge_env,ip: 3,base: 1}]} |
| 134 | frames := machine.frames.clone() |
| 135 | stack := machine.stack.clone() |
| 136 | frame_data := machine.frames.data |
| 137 | stack_data := machine.stack.data |
| 138 | mut region := arena.new(chunk_size: 4096) |
| 139 | region.push() |
| 140 | before := region.used() |
| 141 | accepted := machine.forward_values_to_consumer(vm,3,2) |
| 142 | used := region.used() - before |
| 143 | region.pop() |
| 144 | region.free() |
| 145 | assert accepted && used == 0 |
| 146 | assert machine.steps == 10 && machine.ip == 4 && machine.base == 1 |
| 147 | assert machine.origin == reader.no_node && !machine.finished |
| 148 | assert voidptr(machine.function) == voidptr(&call_values_bridge_function) |
| 149 | assert voidptr(machine.env) == voidptr(bridge_env) |
| 150 | assert machine.stack.data == stack_data && machine.stack.len == 4 |
| 151 | assert machine.stack[0].whole() == 9 && machine.stack[1].pointer() == consumer.pointer() |
| 152 | assert machine.stack[2].whole() == 20 && machine.stack[3].whole() == 22 |
| 153 | for i in 4 .. 6 { assert unsafe { (&Value(stack_data))[i] }.tag == .unspecified } |
| 154 | assert machine.frames.data == frame_data && machine.frames.len == 0 |
| 155 | dead := unsafe { (&CallFrame(frame_data))[0] } |
| 156 | assert isnil(dead.function) && isnil(dead.env) |
| 157 | assert machine.cached == 1 && machine.spares[1].slots[0].tag == .unspecified |
| 158 | assert frames.len == 1 && frames[0].ip == 3 && frames[0].base == 1 |
| 159 | assert stack.len == 6 && stack[2].whole() == 88 && stack[4].whole() == 20 |
| 160 | } |
| 161 | |
| 162 | fn test_values_forward400_rejection_preserves_every_machine_register_and_buffer() { |
| 163 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 164 | producer := vm.eval('(lambda()(values 20 22))')! |
| 165 | consumer := vm.lookup('+')! |
| 166 | for reason in 0 .. 6 { |
| 167 | vm.options = Options{...vm.options,max_steps: if reason == 0 { 1 } else { 0 }} |
| 168 | mut machine := Machine{function: producer.procedure_pointer().function, |
| 169 | env: &Environment{slots: [integer(99)]},base: if reason == 1 { 0 } else { 1 }, |
| 170 | steps: 9,ip: 7,stack: [consumer,vm.lookup('values')!,integer(20),integer(22)], |
| 171 | frames: if reason == 2 { []CallFrame{} } else { |
| 172 | [CallFrame{function: if reason == 3 { producer.procedure_pointer().function } |
| 173 | else { &call_values_bridge_function }, |
| 174 | ip: if reason == 4 { 4 } else { 3 },base: 0}] |
| 175 | }} |
| 176 | function := machine.function |
| 177 | env := machine.env |
| 178 | stack := machine.stack.clone() |
| 179 | frames := machine.frames.clone() |
| 180 | stack_data := machine.stack.data |
| 181 | frame_data := machine.frames.data |
| 182 | assert !machine.forward_values_to_consumer(vm,1,if reason == 5 { 1 } else { 2 }) |
| 183 | assert machine.steps == 9 && machine.ip == 7 && machine.cached == 0 && !machine.finished |
| 184 | assert machine.base == if reason == 1 { 0 } else { 1 } |
| 185 | assert voidptr(machine.function) == voidptr(function) && voidptr(machine.env) == voidptr(env) |
| 186 | assert machine.stack.data == stack_data && machine.frames.data == frame_data |
| 187 | assert machine.stack == stack && machine.frames.len == frames.len |
| 188 | if frames.len > 0 { |
| 189 | assert machine.frames[0].ip == frames[0].ip |
| 190 | assert voidptr(machine.frames[0].function) == voidptr(frames[0].function) |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | fn test_values_forward400_bridge_shares_only_immutable_protocol_across_vms() { |
| 196 | mut a := new_with_options(Options{max_steps: 0})! |
| 197 | mut b := new_with_options(Options{max_steps: 0})! |
| 198 | pa := a.lookup('call-with-values')!.procedure_pointer() |
| 199 | pb := b.lookup('call-with-values')!.procedure_pointer() |
| 200 | assert voidptr(pa.function) == voidptr(&call_values_bridge_function) |
| 201 | assert voidptr(pb.function) == voidptr(&call_values_bridge_function) |
| 202 | assert voidptr(pa.owner) != voidptr(pb.owner) |
| 203 | a.eval('(define first #f)(call-with-values (lambda()(values 20 22)) (lambda(a b)(set! first(lambda()a))(+ a b)))')! |
| 204 | b.eval('(define second #f)(call-with-values (lambda()(values 30 12)) (lambda(a b)(set! second(lambda()b))(+ a b)))')! |
| 205 | gc_collect() |
| 206 | assert a.eval('(first)')!.whole() == 20 && b.eval('(second)')!.whole() == 12 |
| 207 | foreign := a.lookup('call-with-values')! |
| 208 | if _ := b.call(foreign,[]) { assert false } |
| 209 | assert a.eval('(call-with-values (lambda()(values 1 2)) +)')!.whole() == 3 |
| 210 | assert b.eval('(call-with-values (lambda()(values 3 4)) +)')!.whole() == 7 |
| 211 | } |
| 212 | |
| 213 | fn test_budget_values401_handoff_requires_two_remaining_charges() { |
| 214 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 215 | producer := vm.eval('(lambda()(values 20 22))')! |
| 216 | consumer := vm.lookup('+')! |
| 217 | for limit in [0,8,9,10,11,12] { |
| 218 | vm.options = Options{...vm.options,max_steps: limit} |
| 219 | bridge_env := &Environment{slots: [producer,consumer]} |
| 220 | mut machine := Machine{function: producer.procedure_pointer().function, |
| 221 | env: &Environment{slots: [integer(99)]},base: 1,ip: 7,steps: 9, |
| 222 | stack: [consumer,vm.lookup('values')!,integer(20),integer(22)], |
| 223 | frames: [CallFrame{function: &call_values_bridge_function,env: bridge_env,ip: 3,base: 0}]} |
| 224 | stack := machine.stack.clone() |
| 225 | frame_data := machine.frames.data |
| 226 | stack_data := machine.stack.data |
| 227 | env := machine.env |
| 228 | accepted := machine.forward_values_to_consumer(vm,1,2) |
| 229 | assert accepted == (limit == 0 || limit >= 11), limit.str() |
| 230 | if accepted { |
| 231 | assert machine.steps == 10 && machine.ip == 4 && machine.base == 0 |
| 232 | assert machine.frames.len == 0 && machine.stack.len == 3 |
| 233 | assert machine.stack[0].pointer() == consumer.pointer() |
| 234 | assert machine.stack[1].whole() == 20 && machine.stack[2].whole() == 22 |
| 235 | machine.apply(mut vm,2,true)! |
| 236 | assert machine.steps == 11 && machine.finished |
| 237 | assert machine.stack.len == 1 && machine.stack[0].whole() == 42 |
| 238 | } else { |
| 239 | assert machine.steps == 9 && machine.ip == 7 && machine.base == 1 |
| 240 | assert machine.frames.len == 1 && machine.stack == stack |
| 241 | assert voidptr(machine.env) == voidptr(env) && machine.cached == 0 |
| 242 | assert machine.frames.data == frame_data && machine.stack.data == stack_data |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | fn test_budget_values401_repeated_packet_allocation_is_bounded() { |
| 248 | mut region := arena.new(chunk_size: 1048576) |
| 249 | region.push() |
| 250 | defer { region.pop(); region.free() } |
| 251 | mut vm := new_with_options(Options{max_frames: 16})! |
| 252 | assert vm.options.max_steps == 1000000 |
| 253 | vm.eval('(define (producer)(values 20 22))(define (consumer a b)(+ a b)) |
| 254 | (define (loop n acc)(if (= n 0) acc |
| 255 | (loop(- n 1)(+ acc(call-with-values producer consumer)))))')! |
| 256 | procedure := vm.lookup('loop')! |
| 257 | mut args := [integer(100),integer(0)] |
| 258 | assert vm.call(procedure,args)!.whole() == 4200 |
| 259 | mut retained := []int{cap: 2} |
| 260 | for count in [100,10000] { |
| 261 | container_store_validated(mut args,0,integer(count)) |
| 262 | before := region.used() |
| 263 | result := vm.call(procedure,args)! |
| 264 | used := region.used() - before |
| 265 | assert result.whole() == i64(count) * 42 |
| 266 | retained << used |
| 267 | println('BUDGET401_ALLOCATION ${count} ${used}') |
| 268 | } |
| 269 | assert retained[1] <= retained[0] + 512, retained.str() |
| 270 | } |
| 271 | |