| 1 | module scheme |
| 2 | |
| 3 | #include "native_bytes_test.h" |
| 4 | fn C.vile_native_test_fault_invoke(voidptr, &C.VileNativeValue, &C.VileNativeHostApi, &C.VileNativeValue) |
| 5 | fn C.vile_native_bytes_map(i64) voidptr |
| 6 | fn C.vile_native_bytes_unmap(voidptr, i64) |
| 7 | |
| 8 | fn test_native_bytevector_result_preserves_identity_and_commits_once() { |
| 9 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 10 | vm.eval('(define (work b)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) b) (define (pass b)b)')! |
| 11 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 12 | b := bytevector([u8(41)]) |
| 13 | assert vm.call(vm.lookup('work')!,[b])!.pointer() == b.pointer() |
| 14 | assert b.bytevector_items()! == [u8(42)] |
| 15 | literal := vm.eval('#u8(7 8)')! |
| 16 | returned := vm.call(vm.lookup('pass')!,[literal])! |
| 17 | assert returned.pointer() == literal.pointer() && returned.read_only |
| 18 | hits,deopts := vm.native_execution_stats() |
| 19 | assert hits == 2 && deopts == 0 |
| 20 | return unspecified() |
| 21 | })! |
| 22 | } |
| 23 | |
| 24 | fn test_native_bytevector_repeated_writes_share_first_touch_entries() { |
| 25 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 26 | vm.eval('(define (fill b n limit size)(if (= n limit) n |
| 27 | (begin (bytevector-u8-set! b (modulo n size) (modulo n 256)) (fill b (+ n 1) limit size))))')! |
| 28 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 29 | b := bytevector([]u8{len: 518}) |
| 30 | assert vm.call(vm.lookup('fill')!,[b,integer(0),integer(4099),integer(518)])!.whole() == 4099 |
| 31 | for i, value in b.bytevector_items()! { |
| 32 | last := i + ((4098 - i) / 518) * 518 |
| 33 | assert value == u8(last % 256) |
| 34 | } |
| 35 | hits,deopts := vm.native_execution_stats() |
| 36 | assert hits == 1 && deopts == 0 |
| 37 | return unspecified() |
| 38 | })! |
| 39 | } |
| 40 | |
| 41 | fn test_native_bytevector_journal_boundary_and_exhaustion_replay_once() { |
| 42 | for size in [1024,1025] { |
| 43 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 44 | vm.eval('(define (fill b i)(if (= i (bytevector-length b)) 0 |
| 45 | (begin (bytevector-u8-set! b i (+ (bytevector-u8-ref b i) 1)) (fill b (+ i 1)))))')! |
| 46 | vm.with_native_execution(fn [size] (mut vm VM) !Value { |
| 47 | b := bytevector([]u8{len: size}) |
| 48 | assert vm.call(vm.lookup('fill')!,[b,integer(0)])!.whole() == 0 |
| 49 | for value in b.bytevector_items()! { assert value == 1 } |
| 50 | hits,deopts := vm.native_execution_stats() |
| 51 | if size == 1024 { assert hits == 1 && deopts == 0 } else { assert deopts == 1 } |
| 52 | return unspecified() |
| 53 | })! |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn test_native_bytevector_and_vector_effects_rollback_with_scalar_captures() { |
| 58 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 59 | vm.eval('(define cells (let ((count 0)) (list |
| 60 | (lambda(b v)(set! count (+ count 1)) |
| 61 | (bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) |
| 62 | (vector-set! v 0 (+ (vector-ref v 0) 1)) (+ 9223372036854775807 1)) |
| 63 | (lambda()count)))) (define work (car cells)) (define get (cadr cells))')! |
| 64 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 65 | b := bytevector([u8(1)]);v := vector([integer(0)]) |
| 66 | assert vm.call(vm.lookup('work')!,[b,v])!.tag == .big_integer |
| 67 | assert b.bytevector_items()! == [u8(2)] |
| 68 | assert v.vector_pointer().items[0].whole() == 1 |
| 69 | assert vm.call(vm.lookup('get')!,[])!.whole() == 1 |
| 70 | _,deopts := vm.native_execution_stats() |
| 71 | assert deopts == 1 |
| 72 | return unspecified() |
| 73 | })! |
| 74 | } |
| 75 | |
| 76 | fn test_native_bytevector_globals_captures_and_nested_vector_handles_refresh() { |
| 77 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 78 | vm.eval('(define global (bytevector 4)) (define (read-global)(bytevector-u8-ref global 0)) |
| 79 | (define (make b)(lambda(n)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) n)) (bytevector-u8-ref b 0))) |
| 80 | (define a (make (bytevector 1))) (define b (make (bytevector 100))) |
| 81 | (define (nested v)(let ((b (vector-ref v 0))) |
| 82 | (bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 2)) b))')! |
| 83 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 84 | assert vm.call(vm.lookup('read-global')!,[])!.whole() == 4 |
| 85 | assert vm.call(vm.lookup('a')!,[integer(7)])!.whole() == 8 |
| 86 | assert vm.call(vm.lookup('b')!,[integer(10)])!.whole() == 110 |
| 87 | vm.eval('(set! global (bytevector 42)) (set! a (make (bytevector 20)))')! |
| 88 | $if gcboehm ? { gc_collect() } |
| 89 | assert vm.call(vm.lookup('read-global')!,[])!.whole() == 42 |
| 90 | assert vm.call(vm.lookup('a')!,[integer(2)])!.whole() == 22 |
| 91 | bytes := bytevector([u8(10)]) |
| 92 | v := vector([bytes]) |
| 93 | assert vm.call(vm.lookup('nested')!,[v])!.pointer() == bytes.pointer() |
| 94 | assert bytes.bytevector_items()! == [u8(12)] |
| 95 | _,deopts := vm.native_execution_stats() |
| 96 | assert deopts == 0 |
| 97 | return unspecified() |
| 98 | })! |
| 99 | } |
| 100 | |
| 101 | fn test_native_bytevector_literals_keep_readonly_flags_and_errors() { |
| 102 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 103 | vm.eval('(define literal #u8(4 5)) (define (read)(+ (bytevector-length literal) (bytevector-u8-ref literal 1))) |
| 104 | (define (write b v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (bytevector-u8-set! b 0 42))')! |
| 105 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 106 | assert vm.call(vm.lookup('read')!,[])!.whole() == 7 |
| 107 | literal := vm.lookup('literal')! |
| 108 | v := vector([integer(0)]) |
| 109 | if _ := vm.call(vm.lookup('write')!,[literal,v]) { return error('modified bytevector literal') } |
| 110 | assert literal.bytevector_items()! == [u8(4),5] |
| 111 | assert v.vector_pointer().items[0].whole() == 1 |
| 112 | _,deopts := vm.native_execution_stats() |
| 113 | assert deopts == 1 |
| 114 | return unspecified() |
| 115 | })! |
| 116 | } |
| 117 | |
| 118 | struct NativeByteErrorCase { |
| 119 | index Value |
| 120 | value Value |
| 121 | } |
| 122 | |
| 123 | fn native_bytes_test_error(mut vm VM, work Value, args []Value) string { |
| 124 | if _ := vm.call(work,args) { return 'unexpectedly accepted' } |
| 125 | else { return err.msg() } |
| 126 | } |
| 127 | |
| 128 | fn test_native_bytevector_bad_types_ranges_and_bytes_preserve_vm_errors() { |
| 129 | for bad in [ |
| 130 | NativeByteErrorCase{integer(-1),integer(0)},NativeByteErrorCase{integer(2),integer(0)}, |
| 131 | NativeByteErrorCase{real(0.0),integer(0)},NativeByteErrorCase{integer(0),integer(-1)}, |
| 132 | NativeByteErrorCase{integer(0),integer(256)},NativeByteErrorCase{integer(0),real(1.0)}, |
| 133 | NativeByteErrorCase{integer(0),boolean(false)},NativeByteErrorCase{integer(0),list([integer(1)])} |
| 134 | ] { |
| 135 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 136 | vm.eval('(define (work b i x)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (bytevector-u8-set! b i x))')! |
| 137 | work := vm.lookup('work')! |
| 138 | expected := native_bytes_test_error(mut vm,work,[bytevector([u8(0)]),bad.index,bad.value]) |
| 139 | assert expected != 'unexpectedly accepted' |
| 140 | vm.with_native_execution(fn [bad,expected] (mut vm VM) !Value { |
| 141 | b := bytevector([u8(0)]) |
| 142 | actual := native_bytes_test_error(mut vm,vm.lookup('work')!,[b,bad.index,bad.value]) |
| 143 | assert actual == expected |
| 144 | assert b.bytevector_items()! == [u8(1)] |
| 145 | return unspecified() |
| 146 | })! |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fn test_native_bytevector_handles_stay_distinct_from_vectors_and_imported_pairs() { |
| 151 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 152 | vm.eval('(define (work xs b)(+ (car xs) (bytevector-u8-ref b 0))) |
| 153 | (define (as-vector x)(vector-ref x 0)) (define (as-bytes x)(bytevector-u8-ref x 0))')! |
| 154 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 155 | assert vm.call(vm.lookup('work')!,[list([integer(20)]),bytevector([u8(22)])])!.whole() == 42 |
| 156 | if _ := vm.call(vm.lookup('as-vector')!,[bytevector([u8(1)])]) { return error('bytevector used as vector') } |
| 157 | if _ := vm.call(vm.lookup('as-bytes')!,[vector([integer(1)])]) { return error('vector used as bytevector') } |
| 158 | return unspecified() |
| 159 | })! |
| 160 | } |
| 161 | |
| 162 | fn test_native_bytevector_primitive_rebinding_invalidates_cached_guards() { |
| 163 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 164 | vm.eval('(define (work b)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (bytevector-u8-ref b 0))')! |
| 165 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 166 | b := bytevector([u8(0)]) |
| 167 | assert vm.call(vm.lookup('work')!,[b])!.whole() == 1 |
| 168 | vm.eval('(set! bytevector-u8-set! (lambda(b i x)42))')! |
| 169 | assert vm.call(vm.lookup('work')!,[b])!.whole() == 1 |
| 170 | vm.eval('(set! bytevector-u8-ref (lambda(b i)99))')! |
| 171 | assert vm.call(vm.lookup('work')!,[b])!.whole() == 99 |
| 172 | assert b.bytevector_items()! == [u8(1)] |
| 173 | return unspecified() |
| 174 | })! |
| 175 | } |
| 176 | |
| 177 | fn native_bytes_gc_fault(args &C.VileNativeValue, api &C.VileNativeHostApi, journal &C.VileNativeVectorJournal, out &C.VileNativeValue) { |
| 178 | mut values := [3]C.VileNativeValue{} |
| 179 | values[0] = unsafe { args[0] };values[1] = C.VileNativeValue{x: 0,k: 1} |
| 180 | api.vector(api.context,journal,0,&values[0],2,out) |
| 181 | values[0] = unsafe { *out };values[2] = C.VileNativeValue{x: 77,k: 1} |
| 182 | api.vector(api.context,journal,5,&values[0],3,out) |
| 183 | values[0] = unsafe { args[0] };values[2] = C.VileNativeValue{x: 0,k: 1} |
| 184 | api.vector(api.context,journal,2,&values[0],3,out) |
| 185 | $if gcboehm ? { gc_collect() } |
| 186 | unsafe { *out = C.VileNativeValue{k: 6} } |
| 187 | } |
| 188 | |
| 189 | fn native_bytes_gc_container() Value { return vector([bytevector([u8(33)])]) } |
| 190 | |
| 191 | fn test_native_bytevector_journal_keeps_owners_alive_across_collection() { |
| 192 | v := native_bytes_gc_container() |
| 193 | mut scope := NativeScope{} |
| 194 | scope.begin_vector_transaction() |
| 195 | api := C.VileNativeHostApi{context: voidptr(&scope),vector: native_vector_io,finish: native_vector_finish} |
| 196 | scalar := native_scalar(v) |
| 197 | assert scalar.k != 0 |
| 198 | args := [C.VileNativeValue{x: scalar.x,k: scalar.k}] |
| 199 | mut out := C.VileNativeValue{} |
| 200 | unsafe { C.vile_native_test_fault_invoke(voidptr(native_bytes_gc_fault),&args[0],&api,&out) } |
| 201 | assert out.k == 6 |
| 202 | assert v.vector_pointer().items[0].bytevector_items()! == [u8(33)] |
| 203 | } |
| 204 | |
| 205 | fn test_native_bytevector_callbacks_preserve_large_offsets_without_large_resident_buffers() { |
| 206 | length := i64(2147483648) + 8192 |
| 207 | raw := C.vile_native_bytes_map(length) |
| 208 | assert raw != unsafe { nil } |
| 209 | defer { C.vile_native_bytes_unmap(raw,length) } |
| 210 | mut storage := []u8{} |
| 211 | unsafe { storage.data = raw;storage.len = int(length);storage.cap = int(length);storage.flags.clear(.managed) } |
| 212 | bytes := owned_bytevector(storage) |
| 213 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 214 | vm.eval('(define (work b i)(bytevector-u8-set! b i 42) (bytevector-u8-ref b i)) |
| 215 | (define (fail b i)(bytevector-u8-set! b i (+ (bytevector-u8-ref b i) 1)) (+ 9223372036854775807 1))')! |
| 216 | vm.with_native_execution(fn [bytes] (mut vm VM) !Value { |
| 217 | for i in [i64(2147483647),2147483648,2147483664] { |
| 218 | assert vm.call(vm.lookup('work')!,[bytes,integer(i)])!.whole() == 42 |
| 219 | assert unsafe { (&u8(bytes.bytevector_pointer().bytes.data))[i] } == 42 |
| 220 | } |
| 221 | assert vm.call(vm.lookup('fail')!,[bytes,integer(2147483664)])!.tag == .big_integer |
| 222 | assert unsafe { (&u8(bytes.bytevector_pointer().bytes.data))[i64(2147483664)] } == 43 |
| 223 | return unspecified() |
| 224 | })! |
| 225 | } |
| 226 | |
| 227 | fn test_native_bytevector_operations_work_with_vile_frontend() { |
| 228 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 229 | vm.eval_alt('work := fn(b) { bytevector-u8-set!(b,0,42)\n bytevector-u8-ref(b,0) }')! |
| 230 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 231 | assert vm.call(vm.lookup('work')!,[bytevector([u8(0)])])!.whole() == 42 |
| 232 | hits,deopts := vm.native_execution_stats() |
| 233 | assert hits == 1 && deopts == 0 |
| 234 | return unspecified() |
| 235 | })! |
| 236 | } |
| 237 | |
| 238 | fn test_native_bytevector_loops_obey_vm_instruction_budgets() { |
| 239 | mut vm := new_with_options(Options{max_steps: 200,max_frames: 0})! |
| 240 | vm.eval('(define (fill b n)(bytevector-u8-set! b 0 (modulo n 256)) (fill b (+ n 1)))')! |
| 241 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 242 | if _ := vm.call(vm.lookup('fill')!,[bytevector([u8(0)]),integer(0)]) { return error('bytevector loop bypassed its budget') } |
| 243 | hits,_ := vm.native_execution_stats() |
| 244 | assert hits == 0 |
| 245 | return unspecified() |
| 246 | })! |
| 247 | } |
| 248 | |
| 249 | fn test_native_bytevector_and_vector_writes_share_the_transaction_budget() { |
| 250 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 251 | vm.eval('(define (fill b i)(if (= i (bytevector-length b)) 0 |
| 252 | (begin (bytevector-u8-set! b i (+ (bytevector-u8-ref b i) 1)) (fill b (+ i 1))))) |
| 253 | (define (work b v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (fill b 0))')! |
| 254 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 255 | b := bytevector([]u8{len: 1024});v := vector([integer(0)]) |
| 256 | assert vm.call(vm.lookup('work')!,[b,v])!.whole() == 0 |
| 257 | for value in b.bytevector_items()! { assert value == 1 } |
| 258 | assert v.vector_pointer().items[0].whole() == 1 |
| 259 | _,deopts := vm.native_execution_stats() |
| 260 | assert deopts == 1 |
| 261 | return unspecified() |
| 262 | })! |
| 263 | } |
| 264 | |
| 265 | fn test_native_bytevector_publication_keeps_flags_identity_and_replay_effects() { |
| 266 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 267 | vm.eval('(define (store b v)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (vector-set! v 0 b) b) |
| 268 | (define (pass b v)(vector-set! v 0 b) b) |
| 269 | (define cells (let ((held #f))(list |
| 270 | (lambda(b v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (set! held b) 42) |
| 271 | (lambda()held)))) (define capture (car cells)) (define get (cadr cells))')! |
| 272 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 273 | b := bytevector([u8(1)]);v := vector([integer(0)]) |
| 274 | assert vm.call(vm.lookup('store')!,[b,v])!.pointer() == b.pointer() |
| 275 | assert b.bytevector_items()! == [u8(2)] |
| 276 | assert v.vector_pointer().items[0].pointer() == b.pointer() |
| 277 | literal := vm.eval('#u8(7)')! |
| 278 | returned := vm.call(vm.lookup('pass')!,[literal,v])! |
| 279 | assert returned.pointer() == literal.pointer() && returned.read_only |
| 280 | assert v.vector_pointer().items[0].read_only |
| 281 | counter := vector([integer(0)]) |
| 282 | assert vm.call(vm.lookup('capture')!,[b,counter])!.whole() == 42 |
| 283 | assert counter.vector_pointer().items[0].whole() == 1 |
| 284 | assert vm.call(vm.lookup('get')!,[])!.pointer() == b.pointer() |
| 285 | _,deopts := vm.native_execution_stats() |
| 286 | assert deopts == 1 |
| 287 | return unspecified() |
| 288 | })! |
| 289 | } |
| 290 | |