| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import context |
| 5 | |
| 6 | fn nested_usage_bloat() { _ := []u8{len: 2 * 1024 * 1024} } |
| 7 | |
| 8 | fn test_nested_arena_limits_check_all_ancestors_and_restore_parent_pointer() { |
| 9 | for inner_smaller in [false, true] { |
| 10 | work := fn [inner_smaller] (mut vm VM) !Value { |
| 11 | parent := vm.system.evaluation |
| 12 | inner := parent.limit + if inner_smaller { -256 * 1024 } else { 512 * 1024 } |
| 13 | if _ := vm.with_arena_usage_limit(parent.region, inner, fn (mut vm VM) !Value { |
| 14 | nested_usage_bloat() |
| 15 | return vm.eval('42') |
| 16 | }) { |
| 17 | assert false |
| 18 | } else { |
| 19 | // Both are over: the innermost node wins, even with a larger limit. |
| 20 | assert err is ArenaUsageLimitRequest |
| 21 | if err is ArenaUsageLimitRequest { |
| 22 | assert err.limit == inner |
| 23 | } |
| 24 | } |
| 25 | assert voidptr(vm.system.evaluation) == voidptr(parent) |
| 26 | return vm.eval('42') |
| 27 | } |
| 28 | mut region := arena.new() |
| 29 | region.push() |
| 30 | mut vm := new() |
| 31 | outer := i64(region.used()) + if inner_smaller { 512 * 1024 } else { 32768 } |
| 32 | if _ := vm.with_arena_usage_limit(®ion, outer, work) { |
| 33 | assert false |
| 34 | } else { |
| 35 | assert err is ArenaUsageLimitRequest |
| 36 | if err is ArenaUsageLimitRequest { |
| 37 | assert err.limit == outer |
| 38 | } |
| 39 | } |
| 40 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 41 | region.pop() |
| 42 | region.free() |
| 43 | } |
| 44 | // Only the smaller parent is exceeded, so a larger child cannot bypass it. |
| 45 | mut region := arena.new() |
| 46 | defer { region.free() } |
| 47 | region.push() |
| 48 | defer { region.pop() } |
| 49 | mut vm := new() |
| 50 | limit := i64(region.used()) + 32768 |
| 51 | if _ := vm.with_arena_usage_limit(®ion, limit, fn (mut vm VM) !Value { |
| 52 | return vm.with_arena_usage_limit(vm.system.evaluation.region, i64(vm.system.evaluation.region.used()) + 4 * 1024 * 1024, fn (mut vm VM) !Value { |
| 53 | nested_usage_bloat() |
| 54 | return vm.eval('42') |
| 55 | }) |
| 56 | }) { |
| 57 | assert false |
| 58 | } else { |
| 59 | assert err is ArenaUsageLimitRequest |
| 60 | if err is ArenaUsageLimitRequest { |
| 61 | assert err.limit == limit |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn test_nested_private_arena_allocations_are_excluded_and_parent_need_not_be_current_at_poll() { |
| 67 | mut outer := arena.new() |
| 68 | defer { outer.free() } |
| 69 | outer.push() |
| 70 | defer { outer.pop() } |
| 71 | mut vm := new() |
| 72 | limit := i64(outer.used()) + 65536 |
| 73 | result := vm.with_arena_usage_limit(&outer, limit, fn (mut vm VM) !Value { |
| 74 | parent := vm.system.evaluation |
| 75 | mut child := arena.new() |
| 76 | child.push() |
| 77 | child_limit := i64(child.used()) + 4 * 1024 * 1024 |
| 78 | value := vm.with_arena_usage_limit(&child, child_limit, fn (mut vm VM) !Value { |
| 79 | nested_usage_bloat() |
| 80 | vm.check_entry_control()! |
| 81 | return integer(42) |
| 82 | }) or { panic(err) } |
| 83 | assert voidptr(vm.system.evaluation) == voidptr(parent) |
| 84 | assert child.used() > 1024 * 1024 && parent.region.used() < parent.limit |
| 85 | child.pop() |
| 86 | child.free() |
| 87 | return value |
| 88 | }) or { panic(err) } |
| 89 | assert result.whole() == 42 && isnil(vm.system.evaluation) |
| 90 | } |
| 91 | |
| 92 | fn test_mixed_cancellation_and_usage_controls_use_innermost_first_precedence() { |
| 93 | for usage_inner in [false, true] { |
| 94 | mut background := context.background() |
| 95 | mut ctx, cancel := context.with_cancel(mut background) |
| 96 | defer { cancel() } |
| 97 | mut region := arena.new() |
| 98 | region_ref := ®ion |
| 99 | inner_work := fn [cancel] (mut vm VM) !Value { |
| 100 | nested_usage_bloat() |
| 101 | cancel() |
| 102 | return vm.eval('42') |
| 103 | } |
| 104 | usage_work := fn [inner_work, region_ref] (mut vm VM) !Value { |
| 105 | return vm.with_arena_usage_limit(region_ref, i64(region_ref.used()) + 65536, inner_work) |
| 106 | } |
| 107 | context_work := fn [mut ctx, inner_work] (mut vm VM) !Value { |
| 108 | return vm.with_context(mut ctx, inner_work) |
| 109 | } |
| 110 | region.push() |
| 111 | mut vm := new() |
| 112 | if usage_inner { |
| 113 | if _ := vm.with_context(mut ctx, usage_work) { |
| 114 | assert false |
| 115 | } else { |
| 116 | assert err is ArenaUsageLimitRequest |
| 117 | } |
| 118 | } else { |
| 119 | if _ := vm.with_arena_usage_limit(®ion, i64(region.used()) + 65536, context_work) { |
| 120 | assert false |
| 121 | } else { |
| 122 | assert err is CancellationRequest |
| 123 | } |
| 124 | } |
| 125 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 126 | assert vm.eval('42') or { panic(err) }.whole() == 42 |
| 127 | region.pop() |
| 128 | region.free() |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fn test_disabled_usage_scope_preserves_ancestor_checks_and_allocates_no_node() { |
| 133 | mut region := arena.new() |
| 134 | defer { region.free() } |
| 135 | region.push() |
| 136 | defer { region.pop() } |
| 137 | mut vm := new() |
| 138 | limit := i64(region.used()) + 65536 |
| 139 | if _ := vm.with_arena_usage_limit(®ion, limit, fn (mut vm VM) !Value { |
| 140 | parent := vm.system.evaluation |
| 141 | nested_usage_bloat() |
| 142 | return vm.with_arena_usage_limit(parent.region, 0, fn (mut vm VM) !Value { |
| 143 | vm.define('disabled-work-ran', integer(1))! |
| 144 | return integer(1) |
| 145 | }) |
| 146 | }) { |
| 147 | assert false |
| 148 | } else { |
| 149 | assert err is ArenaUsageLimitRequest |
| 150 | } |
| 151 | if _ := vm.lookup('disabled-work-ran') { |
| 152 | assert false |
| 153 | } else { |
| 154 | assert err is EvalError |
| 155 | } |
| 156 | assert isnil(vm.system.evaluation) |
| 157 | } |
| 158 | |
| 159 | fn test_saved_continuations_observe_current_usage_scope_not_expired_one() { |
| 160 | mut region := arena.new() |
| 161 | defer { region.free() } |
| 162 | region.push() |
| 163 | defer { region.pop() } |
| 164 | mut vm := new() |
| 165 | vm.with_arena_usage_limit(®ion, i64(region.used()) + 65536, fn (mut vm VM) !Value { |
| 166 | return vm.eval('(define usage-saved #f) (+ 40 (call/cc (lambda (k) (set! usage-saved k) 2)))') |
| 167 | }) or { panic(err) } |
| 168 | assert vm.call(vm.lookup('usage-saved') or { panic(err) }, [integer(3)]) or { panic(err) }.whole() == 43 |
| 169 | if _ := vm.with_arena_usage_limit(®ion, i64(region.used()) + 65536, fn (mut vm VM) !Value { |
| 170 | nested_usage_bloat() |
| 171 | return vm.call(vm.lookup('usage-saved')!, [integer(4)]) |
| 172 | }) { |
| 173 | assert false |
| 174 | } else { |
| 175 | assert err is ArenaUsageLimitRequest |
| 176 | } |
| 177 | assert vm.call(vm.lookup('usage-saved') or { panic(err) }, [integer(4)]) or { panic(err) }.whole() == 44 |
| 178 | } |
| 179 | |
| 180 | fn test_disabled_usage_scope_postcheck_and_canceled_parent_still_apply() { |
| 181 | mut background := context.background() |
| 182 | mut ctx, cancel := context.with_cancel(mut background) |
| 183 | defer { cancel() } |
| 184 | mut region := arena.new() |
| 185 | defer { region.free() } |
| 186 | region_ref := ®ion |
| 187 | cancel_work := fn [cancel, region_ref] (mut vm VM) !Value { |
| 188 | cancel() |
| 189 | return vm.with_arena_usage_limit(region_ref, 0, fn (mut _vm VM) !Value { |
| 190 | return failure('disabled work ignored canceled parent') |
| 191 | }) |
| 192 | } |
| 193 | region.push() |
| 194 | defer { region.pop() } |
| 195 | mut vm := new() |
| 196 | if _ := vm.with_arena_usage_limit(®ion, i64(region.used()) + 65536, fn (mut vm VM) !Value { |
| 197 | parent := vm.system.evaluation |
| 198 | return vm.with_arena_usage_limit(parent.region, 0, fn (mut vm VM) !Value { |
| 199 | assert vm.system.evaluation.kind == .arena_usage && isnil(vm.system.evaluation.previous) |
| 200 | nested_usage_bloat() |
| 201 | return integer(42) |
| 202 | }) |
| 203 | }) { |
| 204 | assert false |
| 205 | } else { |
| 206 | assert err is ArenaUsageLimitRequest |
| 207 | } |
| 208 | if _ := vm.with_context(mut ctx, cancel_work) { |
| 209 | assert false |
| 210 | } else { |
| 211 | assert err is CancellationRequest |
| 212 | } |
| 213 | assert isnil(vm.system.evaluation) |
| 214 | } |
| 215 | |
| 216 | struct UsageHostFailure { |
| 217 | Error |
| 218 | token int |
| 219 | } |
| 220 | |
| 221 | fn (failure &UsageHostFailure) msg() string { return 'original usage host failure' } |
| 222 | |
| 223 | fn test_nested_usage_scope_preserves_original_error_exit_and_exact_parent_restoration() { |
| 224 | for original in [IError(UsageHostFailure{ token: 73 }), IError(ExitRequest{ status: 7 })] { |
| 225 | inner_work := fn [original] (mut _vm VM) !Value { |
| 226 | nested_usage_bloat() |
| 227 | return original |
| 228 | } |
| 229 | outer_work := fn [inner_work] (mut vm VM) !Value { |
| 230 | parent := vm.system.evaluation |
| 231 | if _ := vm.with_arena_usage_limit(parent.region, i64(parent.region.used()) + 128 * 1024, inner_work) { |
| 232 | assert false |
| 233 | } else { |
| 234 | assert voidptr(vm.system.evaluation) == voidptr(parent) |
| 235 | return err |
| 236 | } |
| 237 | return integer(0) |
| 238 | } |
| 239 | mut region := arena.new() |
| 240 | region.push() |
| 241 | mut vm := new() |
| 242 | if _ := vm.with_arena_usage_limit(®ion, i64(region.used()) + 65536, outer_work) { |
| 243 | assert false |
| 244 | } else { |
| 245 | if original is ExitRequest { |
| 246 | assert err is ExitRequest |
| 247 | if err is ExitRequest { |
| 248 | assert err.status == 7 |
| 249 | } |
| 250 | } else { |
| 251 | assert err is UsageHostFailure |
| 252 | if err is UsageHostFailure { |
| 253 | assert err.token == 73 |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 258 | region.pop() |
| 259 | region.free() |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | fn test_child_arena_limit_can_be_caught_before_free_without_canceling_live_parent() { |
| 264 | mut outer := arena.new() |
| 265 | defer { outer.free() } |
| 266 | outer.push() |
| 267 | defer { outer.pop() } |
| 268 | mut vm := new() |
| 269 | value := vm.with_arena_usage_limit(&outer, i64(outer.used()) + 65536, fn (mut vm VM) !Value { |
| 270 | parent := vm.system.evaluation |
| 271 | mut child := arena.new() |
| 272 | child.push() |
| 273 | if _ := vm.with_arena_usage_limit(&child, i64(child.used()) + 32768, fn (mut _vm VM) !Value { |
| 274 | nested_usage_bloat() |
| 275 | return integer(1) |
| 276 | }) { |
| 277 | assert false |
| 278 | } else { |
| 279 | assert err is ArenaUsageLimitRequest |
| 280 | } |
| 281 | assert voidptr(vm.system.evaluation) == voidptr(parent) |
| 282 | child.pop() |
| 283 | child.free() |
| 284 | return vm.eval('42') |
| 285 | }) or { panic(err) } |
| 286 | assert value.whole() == 42 && isnil(vm.system.evaluation) |
| 287 | } |
| 288 | |