| 1 | module scheme |
| 2 | |
| 3 | $if linux { |
| 4 | #include <sys/mman.h> |
| 5 | } |
| 6 | |
| 7 | // Test-only foreign backing. All stored Value handles are immediate; no GC |
| 8 | // payload is hidden in unscanned storage. Only a handful of pages are touched. |
| 9 | // Linux MAP_NORESERVE avoids commit-accounting the unused virtual pages. |
| 10 | fn sparse_container[T](length int) []T { |
| 11 | $if linux && amd64 { |
| 12 | bytes := length * int(sizeof(T)) |
| 13 | raw := unsafe { C.mmap(nil, uint(bytes), i32(3), i32(0x4022), i32(-1), 0) } |
| 14 | assert raw != voidptr(-1) |
| 15 | mut items := []T{} |
| 16 | unsafe { |
| 17 | items.data = raw |
| 18 | items.len = length |
| 19 | items.cap = length |
| 20 | items.flags = ArrayFlags(0) |
| 21 | } |
| 22 | return items |
| 23 | } $else { |
| 24 | panic('sparse container tests require Linux/amd64') |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn release_sparse_container[T](items []T) { |
| 29 | $if linux && amd64 { |
| 30 | assert unsafe { C.munmap(items.data, uint(items.len * int(sizeof(T)))) } == 0 |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn test_vector_high_indices_ranges_overlap_and_exact_end_are_full_width() { |
| 35 | $if linux && amd64 { |
| 36 | high := int(2147483648) + 17 |
| 37 | items := sparse_container[Value](high + 8192) |
| 38 | defer { release_sparse_container(items) } |
| 39 | value := payload_value(.vector, &Vector{ items: items }) |
| 40 | for at in [int(0), 2147483644, 2147483647, high] { |
| 41 | _ := data_call(.vector_set, [value, integer(at), integer(42)], 'vector-set!') or { panic(err) } |
| 42 | assert primitive_call(.vector_ref, [value, integer(at)], 'vector-ref') or { panic(err) }.whole() == 42 |
| 43 | _ := data_call(.vector_fill, [value, integer(9), integer(at), integer(at + 4)], 'vector-fill!') or { panic(err) } |
| 44 | for i in 0 .. 4 { |
| 45 | assert unsafe { (&Value(items.data))[at + i] }.whole() == 9 |
| 46 | } |
| 47 | for i in 0 .. 4 { |
| 48 | _ := data_call(.vector_set, [value, integer(at + i), integer(i + 1)], 'vector-set!') or { panic(err) } |
| 49 | } |
| 50 | _ := sequence_call(.vector_copy_into, [value, integer(at + 1), value, integer(at), |
| 51 | integer(at + 4)], 'vector-copy!') or { panic(err) } |
| 52 | assert copy_values(items, at, at + 5).map(it.whole()) == [i64(1), 1, 2, 3, 4] |
| 53 | _ := sequence_call(.vector_copy_into, [value, integer(at), value, integer(at + 1), |
| 54 | integer(at + 5)], 'vector-copy!') or { panic(err) } |
| 55 | copy := sequence_call(.vector_copy, [value, integer(at), integer(at + 4)], 'vector-copy') or { panic(err) } |
| 56 | assert copy.vector_items() or { panic(err) }.map(it.whole()) == [i64(1), 2, 3, 4] |
| 57 | } |
| 58 | // Explicit max_i32 end must remain an exact end, not array.slice's sentinel. |
| 59 | assert copy_values(items, 2147483644, 2147483647).len == 3 |
| 60 | before := copy_values(items, high, high + 5) |
| 61 | for args in [[value, integer(-1), integer(0)], [value, integer(items.len), integer(0)]] { |
| 62 | if _ := data_call(.vector_set, args, 'vector-set!') { |
| 63 | assert false |
| 64 | } |
| 65 | } |
| 66 | if _ := data_call(.vector_fill, [value, integer(0), integer(high), integer(items.len + 1)], 'vector-fill!') { |
| 67 | assert false |
| 68 | } |
| 69 | if _ := sequence_call(.vector_copy_into, [value, integer(items.len - 1), value, integer(high), |
| 70 | integer(high + 4)], 'vector-copy!') { |
| 71 | assert false |
| 72 | } |
| 73 | if _ := data_call(.vector_set, [literal_value(value), integer(high), integer(0)], 'vector-set!') { |
| 74 | assert false |
| 75 | } |
| 76 | assert copy_values(items, high, high + 5) == before |
| 77 | for i in 0 .. 3 { |
| 78 | _ := data_call(.vector_set, [value, integer(high + i), |
| 79 | character(rune(`a` + i)) or { panic(err) }], 'vector-set!') or { panic(err) } |
| 80 | } |
| 81 | text := sequence_call(.vector_to_string, [value, integer(high), integer(high + 3)], 'vector->string') or { panic(err) } |
| 82 | assert text.text() == 'abc' |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | fn test_substring_max_i32_boundary_and_existing_string_pointer_stores() { |
| 87 | $if linux && amd64 { |
| 88 | length := int(2147483648) + 8192 |
| 89 | bytes := sparse_container[u8](length) |
| 90 | defer { release_sparse_container(bytes) } |
| 91 | edge := int(2147483647) |
| 92 | unsafe { vmemcpy(&u8(bytes.data) + edge - 3, c'abcXYZ', 6) } |
| 93 | // Cached ASCII character count proves byte offset == character index; |
| 94 | // this test never walks billions of Unicode scalar values. |
| 95 | text := payload_value(.string, &StringData{ length: length, value: unsafe { tos(bytes.data, length) } }) |
| 96 | part := data_call(.substring, [text, integer(edge - 3), integer(edge)], 'substring') or { panic(err) } |
| 97 | assert part.text() == 'abc' |
| 98 | _ := data_call(.string_set, [text, integer(edge), character(`q`) or { panic(err) }], 'string-set!') or { panic(err) } |
| 99 | _ := data_call(.string_fill, [text, character(`z`) or { panic(err) }, integer(edge + 1), |
| 100 | integer(edge + 3)], 'string-fill!') or { panic(err) } |
| 101 | assert text_view(text.text(), edge, edge + 3) == 'qzz' |
| 102 | _ := sequence_call(.string_copy_into, [text, integer(edge), text, integer(edge - 3), |
| 103 | integer(edge)], 'string-copy!') or { panic(err) } |
| 104 | assert text_view(text.text(), edge - 3, edge + 3) == 'abcabc' |
| 105 | converted := sequence_call(.string_to_vector, [text, integer(edge - 3), integer(edge)], 'string->vector') or { panic(err) } |
| 106 | assert converted.vector_items() or { panic(err) }.map(it.whole()) == [ |
| 107 | i64(`a`), |
| 108 | `b`, |
| 109 | `c`, |
| 110 | ] |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn test_indexed_map_and_sort_high_result_slots_use_full_width() { |
| 115 | $if linux && amd64 { |
| 116 | high := int(2147483648) + 5 |
| 117 | items := sparse_container[Value](high + 8) |
| 118 | defer { release_sparse_container(items) } |
| 119 | mut state := &IndexedIteration{ collect: true, length: high + 8, index: high + 1, values: items } |
| 120 | mut machine := Machine{ |
| 121 | stack: [integer(42)] |
| 122 | env: &Environment{ |
| 123 | slots: [ |
| 124 | unspecified(), |
| 125 | unspecified(), |
| 126 | unspecified(), |
| 127 | payload_value(.indexed_iteration, state), |
| 128 | ] |
| 129 | } |
| 130 | } |
| 131 | machine.collect_indexed_iteration() or { panic(err) } |
| 132 | assert unsafe { (&Value(items.data))[high] }.whole() == 42 |
| 133 | mut sort := &SortState{ output: items, written: high } |
| 134 | sort.append_sorted(integer(99)) |
| 135 | assert unsafe { (&Value(items.data))[high] }.whole() == 99 |
| 136 | assert sort.written == high + 1 |
| 137 | chars := sparse_container[rune](high + 8) |
| 138 | defer { release_sparse_container(chars) } |
| 139 | state = &IndexedIteration{ strings: true, collect: true, length: high + 8, index: high + 1, chars: chars } |
| 140 | machine.env.slots[3] = payload_value(.indexed_iteration, state) |
| 141 | machine.push(character(`λ`) or { panic(err) }) |
| 142 | machine.collect_indexed_iteration() or { panic(err) } |
| 143 | assert unsafe { (&rune(chars.data))[high] } == `λ` |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn test_machine_sparse_stack_frame_and_bridge_cleanup_indices_are_full_width() { |
| 148 | $if linux && amd64 { |
| 149 | high := int(2147483648) + 7 |
| 150 | items := sparse_container[Value](high + 2) |
| 151 | defer { release_sparse_container(items) } |
| 152 | mut machine := Machine{ stack: items } |
| 153 | mut target := unsafe { &Value(items.data) } |
| 154 | unsafe { |
| 155 | target[high] = integer(41) |
| 156 | target[high + 1] = integer(42) |
| 157 | } |
| 158 | mut vm := VM{ owner: &Owner{} } |
| 159 | machine.execute(mut vm, Instruction{ op: .swap }) or { panic(err) } |
| 160 | assert unsafe { (&Value(items.data))[high] }.whole() == 42 |
| 161 | assert unsafe { (&Value(items.data))[high + 1] }.whole() == 41 |
| 162 | machine.truncate(high) |
| 163 | assert machine.stack.len == high |
| 164 | assert unsafe { (&Value(items.data))[high] }.tag == .unspecified |
| 165 | assert unsafe { (&Value(items.data))[high + 1] }.tag == .unspecified |
| 166 | frames := sparse_container[CallFrame](high + 1) |
| 167 | defer { release_sparse_container(frames) } |
| 168 | mut target_frames := unsafe { &CallFrame(frames.data) } |
| 169 | unsafe { target_frames[high] = CallFrame{ ip: 42, base: 7 } } |
| 170 | machine.stack = [integer(0)] |
| 171 | machine.base = 0 |
| 172 | machine.frames = frames |
| 173 | machine.finish_call(integer(99)) |
| 174 | assert machine.ip == 42 && machine.base == 7 && machine.frames.len == high |
| 175 | assert unsafe { (&CallFrame(frames.data))[high] }.ip == 0 |
| 176 | bridges := sparse_container[BridgeSite](high + 1) |
| 177 | defer { release_sparse_container(bridges) } |
| 178 | mut target_bridges := unsafe { &BridgeSite(bridges.data) } |
| 179 | unsafe { target_bridges[high] = BridgeSite{ depth: 2 } } |
| 180 | machine.bridge_sites = bridges |
| 181 | machine.release_bridge_sites(1) |
| 182 | assert machine.bridge_sites.len == high |
| 183 | assert unsafe { (&BridgeSite(bridges.data))[high] }.depth == 0 |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | fn test_global_slots_and_instruction_cache_positions_are_distinct_width_domains() { |
| 188 | $if linux && amd64 { |
| 189 | high := int(2147483648) + 19 |
| 190 | items := sparse_container[Value](high + 1) |
| 191 | defer { release_sparse_container(items) } |
| 192 | owner := &Owner{ |
| 193 | syntax_globals: { |
| 194 | 'wide': high |
| 195 | } |
| 196 | } |
| 197 | mut vm := VM{ owner: owner, globals: items, bindings: &owner.syntax_globals } |
| 198 | assert vm.bind_global('wide', integer(41)) == high |
| 199 | assert vm.lookup('wide') or { panic(err) }.whole() == 41 |
| 200 | space := &Namespace{ |
| 201 | writable: true |
| 202 | bindings: { |
| 203 | 'wide': high |
| 204 | } |
| 205 | } |
| 206 | assert vm.bind_global_in(space, 'wide', integer(42)) or { panic(err) } == high |
| 207 | instructions := sparse_container[Instruction](high + 1) |
| 208 | defer { release_sparse_container(instructions) } |
| 209 | mut function := &Function{ code: unsafe { nil }, instructions: instructions, global_slots: [high] } |
| 210 | mut machine := Machine{ function: function, ip: high + 1, stack: [integer(43)] } |
| 211 | machine.execute_extended(mut vm, Instruction{ op: .assign_resolved_global }) or { panic(err) } |
| 212 | assert vm.lookup('wide') or { panic(err) }.whole() == 43 |
| 213 | machine.load_global_reference(mut vm, 0, Instruction{ op: .global_reference }) or { panic(err) } |
| 214 | assert unsafe { (&Instruction(instructions.data))[high] }.op == .global |
| 215 | machine.stack = [integer(44)] |
| 216 | machine.assign_global_reference(mut vm, 0, Instruction{ op: .assign_global_reference }) or { panic(err) } |
| 217 | assert vm.lookup('wide') or { panic(err) }.whole() == 44 |
| 218 | assert unsafe { (&Instruction(instructions.data))[high] }.op == .assign_resolved_global |
| 219 | mut builder := Builder{ code: unsafe { nil }, function: function } |
| 220 | builder.patch(high, 42) |
| 221 | assert unsafe { (&Instruction(instructions.data))[high] }.a == 42 |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | fn test_hashtable_sparse_free_slot_clear_reuse_and_field_update_are_full_width() { |
| 226 | $if linux && amd64 { |
| 227 | high := int(2147483648) + 11 |
| 228 | entries := sparse_container[HashtableEntry](high + 1) |
| 229 | defer { release_sparse_container(entries) } |
| 230 | owner := &Owner{} |
| 231 | mut table := &Hashtable{ |
| 232 | owner: owner |
| 233 | mutable: true |
| 234 | entries: entries |
| 235 | buckets: { |
| 236 | u64(123): high |
| 237 | } |
| 238 | size: 1 |
| 239 | } |
| 240 | mut target_entries := unsafe { &HashtableEntry(entries.data) } |
| 241 | unsafe { target_entries[high] = HashtableEntry{ key: integer(1), value: integer(41), hash: 123, live: true } } |
| 242 | table.store(123, integer(1), integer(42), high) |
| 243 | assert unsafe { (&HashtableEntry(entries.data))[high] }.value.whole() == 42 |
| 244 | table.remove(123, high, -1) |
| 245 | assert table.free == high && table.size == 0 |
| 246 | assert unsafe { (&HashtableEntry(entries.data))[high] }.key.tag == .unspecified |
| 247 | table.store(123, integer(2), integer(43), -1) |
| 248 | assert table.free == -1 && table.size == 1 && table.buckets[123] == high |
| 249 | assert unsafe { (&HashtableEntry(entries.data))[high] }.key.whole() == 2 |
| 250 | assert unsafe { (&HashtableEntry(entries.data))[high] }.value.whole() == 43 |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | fn test_equal_object_pair_set_probes_high_slots_without_pointer_narrowing() { |
| 255 | $if linux && amd64 { |
| 256 | length := int(1) << 33 |
| 257 | keys := sparse_container[ObjectPair](length) |
| 258 | defer { release_sparse_container(keys) } |
| 259 | high := int(2147483648) + 23 |
| 260 | // Both identity tokens refer to this foreign mapping, never to GC |
| 261 | // allocations. Hashing/comparing them never dereferences either token. |
| 262 | a := u64(keys.data) |
| 263 | mask := u64(length - 1) |
| 264 | low := (((a >> 3) * u64(0x9e3779b185ebca87)) ^ u64(high)) * u64(839798700976720815) & mask |
| 265 | mut b := (a & ~((u64(1) << 36) - 1)) | (low << 3) |
| 266 | if b < a { b += u64(1) << 36 } |
| 267 | assert b >= a && b < a + u64(length * int(sizeof(ObjectPair))) |
| 268 | assert int(pair_hash(voidptr(a), voidptr(b)) & mask) == high |
| 269 | mut seen := ComparedPairs{ keys: keys } |
| 270 | assert seen.insert(voidptr(a), voidptr(b)) |
| 271 | assert !seen.insert(voidptr(a), voidptr(b)) |
| 272 | assert unsafe { (&ObjectPair(keys.data))[high] }.a == voidptr(a) |
| 273 | assert unsafe { (&ObjectPair(keys.data))[high] }.b == voidptr(b) |
| 274 | } |
| 275 | } |
| 276 | |