| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn static_cleanup_require(ok bool, message string) { |
| 6 | if !ok { panic(message) } |
| 7 | } |
| 8 | |
| 9 | fn static_cleanup_no_growth(storage &arena.Arena, before int, site int) { |
| 10 | after := storage.used() |
| 11 | if after != before { panic('cleanup site ${site} allocated ${after - before} bytes') } |
| 12 | } |
| 13 | |
| 14 | @[heap] |
| 15 | struct StaticCleanupFailure { |
| 16 | identity int |
| 17 | } |
| 18 | |
| 19 | fn (_ &StaticCleanupFailure) msg() string { return 'prepared host flush failure' } |
| 20 | |
| 21 | fn (problem &StaticCleanupFailure) code() int { return problem.identity } |
| 22 | |
| 23 | fn static_cleanup_same_error(err IError, problem &StaticCleanupFailure) { |
| 24 | if err is StaticCleanupFailure { |
| 25 | static_cleanup_require(voidptr(err) == voidptr(problem), 'cleanup replaced the prepared error') |
| 26 | static_cleanup_require(err.code() == problem.identity, 'cleanup changed the error code') |
| 27 | } else { |
| 28 | panic('cleanup lost the prepared host error type') |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Fixed storage and a prepared error keep the close allocation oracle about |
| 33 | // cleanup, rather than allocations made by a growing sink or error factory. |
| 34 | @[heap] |
| 35 | struct StaticCleanupHost { |
| 36 | input string |
| 37 | problem &StaticCleanupFailure |
| 38 | mut: |
| 39 | offset int |
| 40 | reads int |
| 41 | bytes [64]u8 |
| 42 | used int |
| 43 | writes int |
| 44 | write_limit int |
| 45 | fail_write_at int |
| 46 | flushes int |
| 47 | fail_flush bool |
| 48 | } |
| 49 | |
| 50 | fn (mut host StaticCleanupHost) read(mut destination []u8) !int { |
| 51 | host.reads++ |
| 52 | count := int_min(destination.len, host.input.len - host.offset) |
| 53 | for i in 0 .. count { destination[i] = host.input[host.offset + i] } |
| 54 | host.offset += count |
| 55 | return count |
| 56 | } |
| 57 | |
| 58 | fn (mut host StaticCleanupHost) write(bytes []u8) !int { |
| 59 | host.writes++ |
| 60 | if host.writes == host.fail_write_at { return host.problem } |
| 61 | count := if host.write_limit > 0 { int_min(host.write_limit, bytes.len) } else { bytes.len } |
| 62 | if count > host.bytes.len - host.used { panic('test sink capacity exceeded') } |
| 63 | for i in 0 .. count { host.bytes[host.used + i] = bytes[i] } |
| 64 | host.used += count |
| 65 | return count |
| 66 | } |
| 67 | |
| 68 | fn (mut host StaticCleanupHost) flush() ! { |
| 69 | host.flushes++ |
| 70 | if host.fail_flush { return host.problem } |
| 71 | } |
| 72 | |
| 73 | fn static_cleanup_bytes(host &StaticCleanupHost, expected string) { |
| 74 | static_cleanup_require(host.used == expected.len, 'wrong number of committed output bytes') |
| 75 | for i in 0 .. expected.len { |
| 76 | static_cleanup_require(host.bytes[i] == expected[i], 'output prefix was lost or duplicated') |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn static_cleanup_put(mut vm VM, port Value, text string) ! { |
| 81 | port_put_string(mut vm, [port, string_value(text)])! |
| 82 | } |
| 83 | |
| 84 | fn static_cleanup_detached(group &CombinedTranscodedPort, port Value) { |
| 85 | static_cleanup_require(port.input_pointer().closed && port.output_pointer().closed, 'facade stayed open') |
| 86 | static_cleanup_require(!port.input_pointer().busy && !port.output_pointer().busy, 'facade stayed busy') |
| 87 | static_cleanup_require(port.input_pointer().source is ClosedPort, 'facade retained its reader') |
| 88 | static_cleanup_require(port.output_pointer().sink is ClosedPort, 'facade retained its writer') |
| 89 | static_cleanup_require(group.input.raw.source is ClosedPort, 'raw input retained the host') |
| 90 | static_cleanup_require(group.output.raw.sink is ClosedPort, 'raw output retained the host') |
| 91 | static_cleanup_require(group.input.raw.buffer.len == 0, 'raw input buffer survived final close') |
| 92 | static_cleanup_require(group.output.pending.len == 0, 'pending suffix survived final close') |
| 93 | } |
| 94 | |
| 95 | fn test_static_cleanup_successful_one_way_transcoders_do_not_allocate() { |
| 96 | mut storage := arena.new() |
| 97 | storage.push() |
| 98 | defer { |
| 99 | storage.pop() |
| 100 | storage.free() |
| 101 | } |
| 102 | mut vm := new() |
| 103 | problem := &StaticCleanupFailure{ identity: 101 } |
| 104 | mut host := &StaticCleanupHost{ input: 'λR', problem: problem } |
| 105 | binary_input := vm.input_port(host, binary: true, read_size: 1) or { panic(err) } |
| 106 | input := vm.transcoded_port(binary_input, transcoder_value(.utf8, .none, .raise)) or { panic(err) } |
| 107 | static_cleanup_require((vm.peek_char(input) or { panic(err) }).whole() == `λ`, 'input did not decode Unicode') |
| 108 | source := input.input_pointer().source |
| 109 | if source is TranscodedInput { |
| 110 | before := storage.used() |
| 111 | vm.close_input_port(input) or { panic(err) } |
| 112 | static_cleanup_no_growth(&storage, before, 1) |
| 113 | static_cleanup_require(source.raw.closed && source.raw.buffer.len == 0, 'raw input storage was not released') |
| 114 | static_cleanup_require(source.raw.source is ClosedPort, 'raw input retained the host reader') |
| 115 | } else { |
| 116 | panic('missing one-way input adapter') |
| 117 | } |
| 118 | static_cleanup_require(input.input_pointer().buffer.len == 0, 'facade input retained its buffer') |
| 119 | static_cleanup_require(host.reads > 0 && host.flushes == 0, 'input close touched output') |
| 120 | binary_output := vm.output_port(host, binary: true) or { panic(err) } |
| 121 | output := vm.transcoded_port(binary_output, transcoder_value(.utf8, .none, .raise)) or { panic(err) } |
| 122 | static_cleanup_put(mut vm, output, 'alphaλ') or { panic(err) } |
| 123 | sink := output.output_pointer().sink |
| 124 | if sink is TranscodedOutput { |
| 125 | before := storage.used() |
| 126 | vm.close_output_port(output) or { panic(err) } |
| 127 | static_cleanup_no_growth(&storage, before, 2) |
| 128 | static_cleanup_require(sink.raw.closed && sink.pending.len == 0, 'raw output storage was not released') |
| 129 | static_cleanup_require(sink.raw.sink is ClosedPort, 'raw output retained the host writer') |
| 130 | } else { |
| 131 | panic('missing one-way output adapter') |
| 132 | } |
| 133 | static_cleanup_bytes(host, 'alphaλ') |
| 134 | static_cleanup_require(host.flushes == 1, 'successful close did not flush exactly once') |
| 135 | before_repeat := storage.used() |
| 136 | vm.close_input_port(input) or { panic(err) } |
| 137 | vm.close_output_port(output) or { panic(err) } |
| 138 | static_cleanup_no_growth(&storage, before_repeat, 3) |
| 139 | static_cleanup_require(host.flushes == 1, 'repeated close flushed again') |
| 140 | } |
| 141 | |
| 142 | fn test_static_cleanup_one_way_flush_error_detaches_without_restarting_close() { |
| 143 | mut storage := arena.new() |
| 144 | storage.push() |
| 145 | defer { |
| 146 | storage.pop() |
| 147 | storage.free() |
| 148 | } |
| 149 | mut vm := new() |
| 150 | problem := &StaticCleanupFailure{ identity: 202 } |
| 151 | mut host := &StaticCleanupHost{ problem: problem, fail_flush: true } |
| 152 | binary := vm.output_port(host, binary: true) or { panic(err) } |
| 153 | output := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } |
| 154 | static_cleanup_put(mut vm, output, 'saved') or { panic(err) } |
| 155 | sink := output.output_pointer().sink |
| 156 | if sink is TranscodedOutput { |
| 157 | before := storage.used() |
| 158 | if _ := vm.close_output_port(output) { |
| 159 | panic('host flush failure was discarded') |
| 160 | } else { |
| 161 | static_cleanup_same_error(err, problem) |
| 162 | } |
| 163 | static_cleanup_no_growth(&storage, before, 4) |
| 164 | static_cleanup_require(sink.raw.sink is ClosedPort, 'failed one-way close retained its host writer') |
| 165 | static_cleanup_require(sink.pending.len == 0, 'failed one-way close retained pending bytes') |
| 166 | } else { |
| 167 | panic('missing one-way output adapter') |
| 168 | } |
| 169 | static_cleanup_require(output.output_pointer().closed && !output.output_pointer().busy, 'failed close stayed open or busy') |
| 170 | static_cleanup_require(output.output_pointer().sink is ClosedPort, 'failed close retained its adapter') |
| 171 | before_repeat := storage.used() |
| 172 | vm.close_output_port(output) or { panic(err) } |
| 173 | static_cleanup_no_growth(&storage, before_repeat, 5) |
| 174 | static_cleanup_require(host.flushes == 1, 'failed close was restarted') |
| 175 | static_cleanup_bytes(host, 'saved') |
| 176 | } |
| 177 | |
| 178 | fn test_static_cleanup_combined_finalizer_releases_prepared_storage_without_allocating() { |
| 179 | mut storage := arena.new() |
| 180 | storage.push() |
| 181 | defer { |
| 182 | storage.pop() |
| 183 | storage.free() |
| 184 | } |
| 185 | mut vm := new() |
| 186 | problem := &StaticCleanupFailure{ identity: 505 } |
| 187 | mut host := &StaticCleanupHost{ input: 'λRX', problem: problem } |
| 188 | binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } |
| 189 | port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } |
| 190 | mut group := combined_transcoded_resource(port) |
| 191 | static_cleanup_put(mut vm, port, 'sentλ') or { panic(err) } |
| 192 | static_cleanup_require((vm.peek_char(port) or { panic(err) }).whole() == `λ`, 'input did not prepare a live buffer') |
| 193 | // Public duplex dispatch currently allocates a temporary interface view. |
| 194 | // Sampling the captured finalizer pins the cleanup boundary independently. |
| 195 | before := storage.used() |
| 196 | group.close_all() or { panic(err) } |
| 197 | static_cleanup_no_growth(&storage, before, 13) |
| 198 | static_cleanup_detached(group, port) |
| 199 | static_cleanup_bytes(host, 'sentλ') |
| 200 | static_cleanup_require(host.flushes == 1, 'finalizer did not flush exactly once') |
| 201 | before_repeat := storage.used() |
| 202 | vm.close_port(port) or { panic(err) } |
| 203 | static_cleanup_no_growth(&storage, before_repeat, 14) |
| 204 | static_cleanup_require(host.flushes == 1, 'public repeated close restarted finalization') |
| 205 | // Closing borrowed capabilities must leave the underlying host usable. |
| 206 | host.write([u8(`!`)]) or { panic(err) } |
| 207 | static_cleanup_bytes(host, 'sentλ!') |
| 208 | } |
| 209 | |
| 210 | fn test_static_cleanup_combined_flush_error_retains_raw_sink_until_final_retry() { |
| 211 | mut storage := arena.new() |
| 212 | storage.push() |
| 213 | defer { |
| 214 | storage.pop() |
| 215 | storage.free() |
| 216 | } |
| 217 | mut vm := new() |
| 218 | problem := &StaticCleanupFailure{ identity: 303 } |
| 219 | mut host := &StaticCleanupHost{ input: 'RX', problem: problem, fail_flush: true } |
| 220 | binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } |
| 221 | port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } |
| 222 | group := combined_transcoded_resource(port) |
| 223 | static_cleanup_put(mut vm, port, 'hello') or { panic(err) } |
| 224 | static_cleanup_require((vm.peek_char(port) or { panic(err) }).whole() == `R`, 'input did not warm') |
| 225 | if _ := vm.close_output_port(port) { |
| 226 | panic('combined flush failure was discarded') |
| 227 | } else { |
| 228 | static_cleanup_same_error(err, problem) |
| 229 | } |
| 230 | static_cleanup_require(port.output_pointer().closed && !port.input_pointer().closed, 'failed half-close closed the wrong facets') |
| 231 | static_cleanup_require(port.output_pointer().sink is ClosedPort, 'closed TX facade retained its adapter') |
| 232 | static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the retry capability') |
| 233 | static_cleanup_require(group.output.pending.len == 0, 'flush error reintroduced acknowledged bytes') |
| 234 | static_cleanup_require(host.flushes == 1 && host.writes == 1, 'unexpected host activity on half-close') |
| 235 | static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `R`, 'RX stopped working after failed TX close') |
| 236 | static_cleanup_require(host.flushes == 1, 'RX retried TX flush') |
| 237 | host.fail_flush = false |
| 238 | vm.close_input_port(port) or { panic(err) } |
| 239 | static_cleanup_detached(group, port) |
| 240 | static_cleanup_require(host.flushes == 2 && host.writes == 1, 'final cleanup did not retry only the failed flush') |
| 241 | static_cleanup_bytes(host, 'hello') |
| 242 | before_repeat := storage.used() |
| 243 | vm.close_port(port) or { panic(err) } |
| 244 | static_cleanup_no_growth(&storage, before_repeat, 8) |
| 245 | static_cleanup_require(host.flushes == 2, 'final cleanup restarted after close') |
| 246 | } |
| 247 | |
| 248 | fn test_static_cleanup_combined_pending_suffix_is_drained_once_without_close_allocations() { |
| 249 | mut storage := arena.new() |
| 250 | storage.push() |
| 251 | defer { |
| 252 | storage.pop() |
| 253 | storage.free() |
| 254 | } |
| 255 | mut vm := new() |
| 256 | problem := &StaticCleanupFailure{ identity: 404 } |
| 257 | mut host := &StaticCleanupHost{ input: 'λRX', problem: problem, write_limit: 2, fail_write_at: 2 } |
| 258 | binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } |
| 259 | port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } |
| 260 | mut group := combined_transcoded_resource(port) |
| 261 | if _ := static_cleanup_put(mut vm, port, 'abcde') { |
| 262 | panic('prepared partial write did not fail') |
| 263 | } else { |
| 264 | static_cleanup_same_error(err, problem) |
| 265 | } |
| 266 | static_cleanup_bytes(host, 'ab') |
| 267 | static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'unsent suffix was not retained') |
| 268 | host.fail_write_at = 3 |
| 269 | if _ := vm.close_output_port(port) { |
| 270 | panic('pending-suffix flush did not fail') |
| 271 | } else { |
| 272 | static_cleanup_same_error(err, problem) |
| 273 | } |
| 274 | static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the pending writer') |
| 275 | static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'failed half-close lost its suffix') |
| 276 | static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `λ`, 'independent RX stopped after TX error') |
| 277 | static_cleanup_require(host.writes == 3, 'RX retried pending TX') |
| 278 | host.fail_write_at = 0 |
| 279 | before_final := storage.used() |
| 280 | // This is the finalizer called by the public final-half close. Resolve its |
| 281 | // resource before sampling, excluding the separate interface-view lookup. |
| 282 | group.close_all() or { panic(err) } |
| 283 | static_cleanup_no_growth(&storage, before_final, 10) |
| 284 | static_cleanup_detached(group, port) |
| 285 | static_cleanup_bytes(host, 'abcde') |
| 286 | static_cleanup_require(host.writes == 5 && host.flushes == 1, 'pending suffix was not drained exactly once') |
| 287 | } |
| 288 | |
| 289 | fn test_static_cleanup_independent_arenas_keep_closed_stubs_and_output_snapshots_valid() { |
| 290 | mut short := arena.new() |
| 291 | short.push() |
| 292 | mut first := new() |
| 293 | first_input := first.open_input_string('short') or { panic(err) } |
| 294 | first_output := first.open_output_string() or { panic(err) } |
| 295 | first.close_input_port(first_input) or { panic(err) } |
| 296 | first.close_output_port(first_output) or { panic(err) } |
| 297 | short.pop() |
| 298 | mut survivor := arena.new() |
| 299 | survivor.push() |
| 300 | mut vm := new() |
| 301 | input := vm.open_input_string('survivor') or { panic(err) } |
| 302 | output := vm.open_output_string() or { panic(err) } |
| 303 | static_cleanup_put(mut vm, output, 'alphaλ') or { panic(err) } |
| 304 | snapshot := vm.get_output_string(output) or { panic(err) } |
| 305 | static_cleanup_put(mut vm, output, '/tail') or { panic(err) } |
| 306 | before := survivor.used() |
| 307 | vm.close_input_port(input) or { panic(err) } |
| 308 | vm.close_output_port(output) or { panic(err) } |
| 309 | static_cleanup_no_growth(&survivor, before, 11) |
| 310 | complete := vm.get_output_string(output) or { panic(err) } |
| 311 | survivor.pop() |
| 312 | // No value or interface from short is inspected after this free. The second |
| 313 | // VM and its own port/snapshot storage all remain alive in survivor. |
| 314 | short.free() |
| 315 | survivor.push() |
| 316 | static_cleanup_require((snapshot.as_string() or { panic(err) }) == 'alphaλ', 'earlier snapshot changed after append/close') |
| 317 | static_cleanup_require((complete.as_string() or { panic(err) }) == 'alphaλ/tail', 'completed snapshot lost output') |
| 318 | vm.define('retained-snapshot', snapshot) or { panic(err) } |
| 319 | vm.eval('(string-set! retained-snapshot 0 #\\X)') or { panic(err) } |
| 320 | static_cleanup_require((snapshot.as_string() or { panic(err) }) == 'Xlphaλ', 'snapshot mutation did not apply') |
| 321 | static_cleanup_require((complete.as_string() or { panic(err) }) == 'alphaλ/tail', 'independent snapshot was aliased') |
| 322 | latest := vm.get_output_string(output) or { panic(err) } |
| 323 | static_cleanup_require((latest.as_string() or { panic(err) }) == 'alphaλ/tail', 'snapshot mutation changed the closed collector') |
| 324 | mut reader := input.input_pointer().source |
| 325 | mut writer := output.output_pointer().sink |
| 326 | mut bytes := [u8(0)] |
| 327 | if _ := reader.read(mut bytes) { |
| 328 | panic('survivor closed reader succeeded') |
| 329 | } else { |
| 330 | static_cleanup_require(err.msg() == 'input port is closed', 'survivor reader lost its stub') |
| 331 | } |
| 332 | if _ := writer.write(bytes) { |
| 333 | panic('survivor closed writer succeeded') |
| 334 | } else { |
| 335 | static_cleanup_require(err.msg() == 'output port is closed', 'survivor writer lost its stub') |
| 336 | } |
| 337 | before_repeat := survivor.used() |
| 338 | vm.close_input_port(input) or { panic(err) } |
| 339 | vm.close_output_port(output) or { panic(err) } |
| 340 | static_cleanup_no_growth(&survivor, before_repeat, 12) |
| 341 | survivor.pop() |
| 342 | survivor.free() |
| 343 | } |
| 344 | |