// ptrarr_index_test — indexing through a pointer-to-array (`p[i]`, p: *[N]T) // must stride by size(T), the pointee array's ELEMENT, never by the whole-array // byte size, migrated from test/wcc/949_ptrarr_index_run.c (#61). Three sub- // classes of one root: // A. value-route scaling — wwstage's elemsizeofc fed a `*[N]T` pointee into // the "outer stride = whole sub-array" rule (only correct for [N][M]T), so // every read/write/compound through `p[i]` strode N*size(T) (OOB for i>=1) // and a var-idx write emitted an N*8 aggregate copy → caller-frame smash // (exactly lib/hash/siphash round()'s corruption). cstage was correct; // wwstage aligned UP via the idxeffti/idxelemtn choke-point. // B. `&p[i]` addr-of — BOTH stages identically wrong (byte-id-BLIND): the // TK_AMP arm read bu->sub->size without the ptr peel, so &p[3]-&a[0] // returned 3*N*size(T). Only a RUNTIME row pins this class. // C. `(*p)[i]` explicit deref+index — BOTH stages SEGV'd identically: the // deref materialized an 8B scalar load and indexed off that VALUE. Fixed // at the deref choke-point (an ARRAY pointee is its own address, #270-1a). // // cs==ww is byte-identical, and cstage is runtime-correct, so byte-id (the gate) // + these behavioral @tests (cstage-runtime) together pin BOTH stages. The // matrix covers elem widths {1,2,4,8}B, const+var indices, param/local/cast // bases, read/write/compound routes, neighbor-corruption guards, the &p[i] // pointer difference (B), nested *[2][3], *[3]str header elements, the siphash // round() mix-in-place shape, and the (*p)[i] deref family (C). package ptrarr_index_test; fn pa_rd64c(p: *[4]u64) u64 = { return p[1]; }; fn pa_rd8v(p: *[4]u8, i: i32) u8 = { return p[i]; }; fn pa_rd16(p: *[4]u16) u16 = { return p[3]; }; fn pa_rd32(p: *[4]u32) u32 = { return p[2]; }; fn pa_rd32s(p: *[4]i32) i32 = { return p[1]; }; fn pa_wr64c(p: *[4]u64) void = { p[1] = 7u64; }; fn pa_wr64v(p: *[4]u64, i: i32, x: u64) void = { p[i] = x; }; fn pa_wr8(p: *[4]u8) void = { p[1] = 9u8; }; fn pa_add5(p: *[4]u64) void = { p[1] += 5u64; }; fn pa_addat(p: *[4]u32, i: i32) void = { p[i] += 7u32; }; fn pa_rd2d(p: *[2][3]u32, i: i32, j: i32) u32 = { return p[i][j]; }; fn pa_lenof(p: *[3]str, i: i32) i32 = { return p[i].len; }; fn pa_mix(v: *[4]u64) void = { v[0] += v[1]; v[2] += v[3]; v[1] += v[0]; v[3] += v[2]; }; fn pa_deref_rd(p: *[4]u64) u64 = { return (*p)[1]; }; @test fn rd_u64_param_const() void = { let a: [4]u64 = [100u64, 101u64, 102u64, 103u64]; assert(pa_rd64c(&a) == 101u64); }; @test fn rd_u8_param_var() void = { let a: [4]u8 = [10u8, 20u8, 30u8, 40u8]; assert(pa_rd8v(&a, 2) == 30u8); }; @test fn rd_u16_param() void = { let a: [4]u16 = [11u16, 22u16, 33u16, 44u16]; assert(pa_rd16(&a) == 44u16); }; @test fn rd_u32_param() void = { let a: [4]u32 = [11u32, 22u32, 33u32, 44u32]; assert(pa_rd32(&a) == 33u32); }; @test fn rd_i32_signed() void = { // signed-narrow elem behind the ptr — pins the MOVSXD the idxeffti'd // elemissigned picks (an undrilled ptr-tinfo read classified ARRAY as // unsigned). let a: [4]i32 = [7, -5, 9, 1]; assert(pa_rd32s(&a) == -5i32); }; @test fn wr_u64_param_const() void = { let a: [4]u64 = [1u64, 2u64, 3u64, 4u64]; pa_wr64c(&a); assert(a[0] == 1u64); assert(a[1] == 7u64); assert(a[2] == 3u64); assert(a[3] == 4u64); }; @test fn wr_u64_varidx_paramrhs() void = { // the corruption proof: pre-fix wwstage emitted a 32B aggregate copy // sourced at &x to base+32*i → frame smash. Neighbor guards assert no // byte outside a[1] moved. let a: [4]u64 = [10u64, 11u64, 12u64, 13u64]; let i: i32 = 1; pa_wr64v(&a, i, 77u64); assert(a[0] == 10u64); assert(a[1] == 77u64); assert(a[2] == 12u64); assert(a[3] == 13u64); }; @test fn wr_u8_neighbors() void = { let a: [4]u8 = [1u8, 2u8, 3u8, 4u8]; pa_wr8(&a); assert(a[0] == 1u8); assert(a[1] == 9u8); assert(a[2] == 3u8); assert(a[3] == 4u8); }; @test fn compound_u64() void = { let a: [4]u64 = [100u64, 101u64, 102u64, 103u64]; pa_add5(&a); assert(a[0] == 100u64); assert(a[1] == 106u64); assert(a[2] == 102u64); }; @test fn compound_u32_varidx() void = { let a: [4]u32 = [10u32, 20u32, 30u32, 40u32]; pa_addat(&a, 2); assert(a[1] == 20u32); assert(a[2] == 37u32); assert(a[3] == 40u32); }; // A: local-ptr base, no call boundary. @test fn rd_localptr() void = { let a: [4]u64 = [100u64, 101u64, 102u64, 103u64]; let p: *[4]u64 = &a; assert(p[2] == 102u64); }; @test fn rd_castbase() void = { let big: [4]u64 = [5u64, 6u64, 7u64, 8u64]; let p: *[4]u64 = (&big): *[4]u64; assert(p[1] == 6u64); }; // A: nested *[2][3]u32 — inner stride 4, outer 12; the pointee's OWN nesting // must coexist with the ptr carve-out. Read, write, neighbor guards. @test fn nested_2d() void = { let a: [2][3]u32; a[0][0] = 0u32; a[0][1] = 1u32; a[0][2] = 2u32; a[1][0] = 10u32; a[1][1] = 11u32; a[1][2] = 12u32; assert(pa_rd2d(&a, 1, 2) == 12u32); assert(pa_rd2d(&a, 0, 1) == 1u32); let p: *[2][3]u32 = &a; p[1][0] = 99u32; assert(a[1][0] == 99u32); assert(a[1][1] == 11u32); assert(a[0][2] == 2u32); }; // A: *[3]str — 3-word header elements; pins the read-side str-header gate on // the idx_eff'd element. @test fn rd_str_elem() void = { let a: [3]str; a[0] = "x"; a[1] = "yy"; a[2] = "zzz"; assert(pa_lenof(&a, 2) == 3); assert(pa_lenof(&a, 0) == 1); let p: *[3]str = &a; assert(p[1].len == 2); }; // B: &p[i] pointer difference — both stages emitted whole-array stride (96) // byte-IDENTICALLY pre-fix; only this runtime row sees the class. 3*8 = 24. @test fn amp_diff_u64() void = { let a: [4]u64 = [1u64, 2u64, 3u64, 4u64]; let p: *[4]u64 = &a; let d: u64 = (&p[3]): u64 - (&a[0]): u64; assert(d == 24u64); }; // B: &p[i] difference at a narrow width. 3*2 = 6. @test fn amp_diff_u16() void = { let a: [4]u16 = [1u16, 2u16, 3u16, 4u16]; let p: *[4]u16 = &a; let d: u64 = (&p[3]): u64 - (&a[0]): u64; assert(d == 6u64); }; // A: the live consumer's shape — siphash round() mutates all four lanes through // the param ptr, each read feeding a later write. @test fn mix_inplace_round() void = { let v: [4]u64 = [1u64, 2u64, 3u64, 4u64]; pa_mix(&v); assert(v[0] == 3u64); assert(v[1] == 5u64); assert(v[2] == 7u64); assert(v[3] == 11u64); }; @test fn deref_rd_u64() void = { let a: [4]u64 = [100u64, 101u64, 102u64, 103u64]; let p: *[4]u64 = &a; assert((*p)[1] == 101u64); }; @test fn deref_rd_u32() void = { // pins the N_UN-base esz arm (the 8B default would mis-stride). let a: [4]u32 = [11u32, 22u32, 33u32, 44u32]; let p: *[4]u32 = &a; assert((*p)[2] == 33u32); }; @test fn deref_rd_param() void = { let a: [4]u64 = [100u64, 101u64, 102u64, 103u64]; assert(pa_deref_rd(&a) == 101u64); }; @test fn deref_wr_u64() void = { let a: [4]u64 = [1u64, 2u64, 3u64, 4u64]; let p: *[4]u64 = &a; (*p)[1] = 7u64; assert(a[0] == 1u64); assert(a[1] == 7u64); assert(a[2] == 3u64); }; @test fn deref_wr_u8() void = { let a: [4]u8 = [1u8, 2u8, 3u8, 4u8]; let p: *[4]u8 = &a; (*p)[1] = 9u8; assert(a[0] == 1u8); assert(a[1] == 9u8); assert(a[2] == 3u8); }; @test fn deref_compound_u64() void = { let a: [4]u64 = [100u64, 101u64, 102u64, 103u64]; let p: *[4]u64 = &a; (*p)[1] += 5u64; assert(a[1] == 106u64); };