vile / scheme / unicode_scratch_failure_test.c.v
52 lines · 45 sloc · 1.45 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5#flag -I @VMODROOT/scheme/testdata/unicode_scratch
6#include "allocations.h"
7
8fn C.unicode_scratch_begin()
9fn C.unicode_scratch_end() int
10fn C.unicode_scratch_first() voidptr
11
12fn attempt_unicode_scratch(words bool, mut result BoundedResult) {
13 defer {
14 if message := recover() {
15 result.exhausted = arena.allocation_failure() or { panic(message) }
16 }
17 }
18 if words {
19 mut storage, _ := unicode_temp_words(1)
20 storage.free()
21 } else {
22 mut storage, _ := unicode_temp_bytes(1)
23 storage.free()
24 }
25}
26
27fn check_unicode_scratch_failure(words bool) {
28 $if gcboehm ? { return }
29 mut owner := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 })
30 defer { owner.free() }
31 mut result := BoundedResult{}
32 owner.push()
33 C.unicode_scratch_begin()
34 attempt_unicode_scratch(words, mut result)
35 outstanding := C.unicode_scratch_end()
36 restored := owner.is_current()
37 owner.pop()
38 // The old helper leaves one unpublished arena control block. Release it
39 // after measuring the leak so a failed baseline test keeps its owner valid.
40 if outstanding == 1 { unsafe { v_arena_free(C.unicode_scratch_first()) } }
41 assert restored
42 assert (result.exhausted or { panic('expected scratch denial') }) == .chunk_limit
43 assert outstanding == 0
44}
45
46fn test_denied_unicode_byte_scratch_releases_its_unpublished_arena() {
47 check_unicode_scratch_failure(false)
48}
49
50fn test_denied_unicode_word_scratch_releases_its_unpublished_arena() {
51 check_unicode_scratch_failure(true)
52}
53