| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | // BoundedVM owns an arena for its VM, including initial bindings, parsing, |
| 7 | // compilation, evaluation, native reentry and nested scratch arenas. |
| 8 | // Use one handle per thread or synchronize host access. Values and ordinary |
| 9 | // errors returned by this handle remain valid until close(). |
| 10 | @[nocopy] |
| 11 | pub struct BoundedVM { |
| 12 | mut: |
| 13 | region arena.Arena |
| 14 | vm &VM |
| 15 | files &BoundedFiles = unsafe { nil } |
| 16 | state BoundedState |
| 17 | } |
| 18 | |
| 19 | enum BoundedState { |
| 20 | idle |
| 21 | running |
| 22 | failed |
| 23 | closed |
| 24 | } |
| 25 | |
| 26 | const vm_chunk_failure = ResourceLimitRequest{ kind: .vm_chunk_bytes, requested: 0 } |
| 27 | const vm_backing_failure = ResourceLimitRequest{ kind: .vm_allocation, requested: 0 } |
| 28 | |
| 29 | enum BoundedOperation { |
| 30 | initialize |
| 31 | eval_source |
| 32 | call_procedure |
| 33 | work |
| 34 | } |
| 35 | |
| 36 | struct BoundedInput { |
| 37 | mode BoundedOperation |
| 38 | options Options |
| 39 | source string |
| 40 | syntax SourceSyntax |
| 41 | procedure Value |
| 42 | args []Value |
| 43 | work EvaluationFn = unsafe { nil } |
| 44 | } |
| 45 | |
| 46 | struct BoundedResult { |
| 47 | mut: |
| 48 | value Value |
| 49 | error ?IError |
| 50 | exhausted ?arena.AllocationFailure |
| 51 | } |
| 52 | |
| 53 | // new_bounded includes VM initialization in the hard backing-chunk budget. |
| 54 | // Zero limits reject the first chunk. Close the returned handle to release |
| 55 | // its files and all VM allocations. |
| 56 | pub fn new_bounded(options Options, limits arena.ChunkLimits) !BoundedVM { |
| 57 | validate_vm_options(options)! |
| 58 | region := arena.try_new(limits: limits) or { return &vm_backing_failure } |
| 59 | mut instance := BoundedVM{ region: region, vm: unsafe { nil } } |
| 60 | instance.run_input(BoundedInput{ mode: .initialize, options: options }) or { |
| 61 | instance.close() or {} |
| 62 | return err |
| 63 | } |
| 64 | return instance |
| 65 | } |
| 66 | |
| 67 | // chunk_usage reports live, peak live and cumulative backing chunk bytes. |
| 68 | pub fn (instance &BoundedVM) chunk_usage() ?arena.ChunkUsage { |
| 69 | if instance.state == .closed { return none } |
| 70 | return instance.region.chunk_usage() |
| 71 | } |
| 72 | |
| 73 | // eval evaluates source in this VM. Parsing and supported aggregate operations |
| 74 | // can return ResourceLimitRequest before publication and keep the VM reusable. |
| 75 | // An allocator failure elsewhere quarantines it and releases owned descriptors. |
| 76 | pub fn (mut instance BoundedVM) eval(source string) !Value { |
| 77 | return instance.run_input(BoundedInput{ mode: .eval_source, source: source }) |
| 78 | } |
| 79 | |
| 80 | // eval_alt reads Vile syntax using the same VM, bindings and allocation limits. |
| 81 | pub fn (mut instance BoundedVM) eval_alt(source string) !Value { |
| 82 | return instance.run_input(BoundedInput{ mode: .eval_source, source: source, syntax: .vile }) |
| 83 | } |
| 84 | |
| 85 | // call invokes a published procedure in the same allocation domain. |
| 86 | pub fn (mut instance BoundedVM) call(procedure Value, args []Value) !Value { |
| 87 | return instance.run_input(BoundedInput{ mode: .call_procedure, procedure: procedure, args: args }) |
| 88 | } |
| 89 | |
| 90 | // run executes host code with all VM host APIs available under the same limit. |
| 91 | // Reenter through the callback's VM. Host code explicitly selecting another |
| 92 | // allocator, allocating through C, or starting threads manages those resources. |
| 93 | pub fn (mut instance BoundedVM) run(work EvaluationFn) !Value { |
| 94 | return instance.run_input(BoundedInput{ mode: .work, work: work }) |
| 95 | } |
| 96 | |
| 97 | fn (mut instance BoundedVM) run_input(input BoundedInput) !Value { |
| 98 | if instance.state != .idle { return failure('bounded VM is busy, exhausted, or closed') } |
| 99 | instance.state = .running |
| 100 | // A host panic can also interrupt publication. Its original panic continues |
| 101 | // across the entry, while the owner still releases descriptors and is closable. |
| 102 | defer { |
| 103 | if instance.state == .running { |
| 104 | instance.state = .failed |
| 105 | if !isnil(instance.files) { instance.files.abort() } |
| 106 | } |
| 107 | } |
| 108 | mut result := BoundedResult{} |
| 109 | instance.run_selected(input, mut result) |
| 110 | if !isnil(instance.files) && result.exhausted == none { |
| 111 | result.exhausted = instance.files.interrupted |
| 112 | } |
| 113 | if kind := result.exhausted { |
| 114 | instance.state = .failed |
| 115 | if !isnil(instance.files) { instance.files.abort() } |
| 116 | if kind == .chunk_limit { return &vm_chunk_failure } |
| 117 | return &vm_backing_failure |
| 118 | } |
| 119 | if !isnil(instance.vm) && !isnil(instance.vm.goops) |
| 120 | && instance.vm.goops.bootstrap_failed && !instance.vm.goops.starting { |
| 121 | instance.state = .failed |
| 122 | if !isnil(instance.files) { instance.files.abort() } |
| 123 | if err := result.error { return err } |
| 124 | return &goops_bootstrap_interrupted |
| 125 | } |
| 126 | instance.state = .idle |
| 127 | if err := result.error { return err } |
| 128 | return result.value |
| 129 | } |
| 130 | |
| 131 | fn (mut instance BoundedVM) run_selected(input BoundedInput, mut result BoundedResult) { |
| 132 | instance.region.push() |
| 133 | defer { instance.region.pop() } |
| 134 | defer { |
| 135 | if message := recover() { |
| 136 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 137 | } |
| 138 | } |
| 139 | match input.mode { |
| 140 | .initialize { |
| 141 | instance.files = &BoundedFiles{} |
| 142 | instance.vm = make_vm(input.options, instance.files) |
| 143 | } |
| 144 | .eval_source { |
| 145 | mut state := SourceReadState{} |
| 146 | result.value = instance.vm.eval_with_read_state(input.source, input.syntax, mut state) or { |
| 147 | result.error = err |
| 148 | return |
| 149 | } |
| 150 | } |
| 151 | .call_procedure { |
| 152 | result.value = instance.vm.call(input.procedure, input.args) or { |
| 153 | result.error = err |
| 154 | return |
| 155 | } |
| 156 | } |
| 157 | .work { |
| 158 | result.value = input.work(mut instance.vm) or { |
| 159 | result.error = err |
| 160 | return |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // close flushes and closes remaining owned files and releases the arena. After |
| 167 | // exhaustion its descriptors have already been closed, without invoking host |
| 168 | // callbacks or attempting to allocate for buffered output. |
| 169 | pub fn (mut instance BoundedVM) close() ! { |
| 170 | if instance.state == .closed { return } |
| 171 | if instance.state == .running { return failure('bounded VM is running') } |
| 172 | instance.state = .closed |
| 173 | defer { instance.region.free() } |
| 174 | if !isnil(instance.files) { instance.files.close()! } |
| 175 | } |
| 176 | |
| 177 | // These links are allocated before opening a descriptor. Cleanup can release a |
| 178 | // partially constructed port even if publication was interrupted by its allocator. |
| 179 | enum MachineBuffer { |
| 180 | stack |
| 181 | frames |
| 182 | bridge_sites |
| 183 | } |
| 184 | |
| 185 | struct BoundedFiles { |
| 186 | argument_list fn ([]Value) !Value = bounded_argument_list |
| 187 | reserve_buffer fn (mut Machine, MachineBuffer, int) ! = bounded_reserve_buffer |
| 188 | activate fn (mut Machine, &Function, &Environment, bool) !&Environment = bounded_activation |
| 189 | next_method fn (&VM, &Environment, &Function, Value, []Value) !Value = bounded_goops_next |
| 190 | build_program fn (&VM, reader.Document, u64, &Namespace, string, bool) !&Function = bounded_build_program |
| 191 | compile_form fn (mut VM, mut Machine, reader.NodeId) !&Function = bounded_compile_form |
| 192 | compile_value fn (mut VM, Value, Value, string) !&Function = bounded_compile_value |
| 193 | enter_program fn (mut VM, mut Machine) !ActiveEntry = bounded_enter_program |
| 194 | enter_call fn (mut VM, mut Machine, Value, []Value) !ActiveEntry = bounded_enter_call |
| 195 | read_source fn (&VM, SourceInput) !reader.Document = read_bounded_source |
| 196 | binding_name fn (string, reader.ReadOptions) !string = bounded_binding_name |
| 197 | publish_binding fn (mut VM, &Namespace, string, Value) !int = bounded_publish_binding |
| 198 | apply_primitive fn (Primitive, []Value, string) !Value = bounded_primitive |
| 199 | mut: |
| 200 | head &BoundedFileLink = unsafe { nil } |
| 201 | interrupted ?arena.AllocationFailure |
| 202 | } |
| 203 | |
| 204 | struct BoundedFileLink { |
| 205 | mut: |
| 206 | files &BoundedFiles |
| 207 | stream &PortFileStream |
| 208 | prev &BoundedFileLink = unsafe { nil } |
| 209 | next &BoundedFileLink = unsafe { nil } |
| 210 | } |
| 211 | |
| 212 | fn (mut files BoundedFiles) track(mut stream PortFileStream) { |
| 213 | link := &BoundedFileLink{ files: &files, stream: &stream, next: files.head } |
| 214 | if !isnil(files.head) { files.head.prev = link } |
| 215 | stream.bounded_link = link |
| 216 | files.head = link |
| 217 | } |
| 218 | |
| 219 | fn (mut link BoundedFileLink) unlink() { |
| 220 | if isnil(link.prev) { link.files.head = link.next } else { link.prev.next = link.next } |
| 221 | if !isnil(link.next) { link.next.prev = link.prev } |
| 222 | link.stream.bounded_link = unsafe { nil } |
| 223 | } |
| 224 | |
| 225 | fn (mut files BoundedFiles) abort() { |
| 226 | for !isnil(files.head) { |
| 227 | mut stream := files.head.stream |
| 228 | if stream.file.is_opened { stream.close_resource() or {} } |
| 229 | stream.release_file_resource() |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | fn (mut files BoundedFiles) close() ! { |
| 234 | mut first := ?IError(none) |
| 235 | for !isnil(files.head) { |
| 236 | mut stream := files.head.stream |
| 237 | stream.close() or { if first == none { first = err } } |
| 238 | } |
| 239 | if err := first { return err } |
| 240 | } |
| 241 | |