verify: Accept protected array prefixes

5ac4b96ffa9d333851f22f9ec5af1ac06fb074c624c075ff6ba26293183e2bc0
A protected slice view required the exact backing array count, which
rejected a bounded prefix. Accept a matching element layout when the
backing array contains at least the requested count.

Cover full arrays, valid prefixes and views beyond the backing array.

Assisted-by: Codex:gpt-6
Alexis Sellier committed ago 1 parent d3b089b9
lib/std/lang/il/verify/layout.rad +2 -2
709 709
        and (have.mutable or not want.mutable);
710 710
}
711 711
712 712
/// Whether a pointer to layout `l` at byte `o` can be used as a pointer to `count` consecutive
713 713
/// objects of layout `e`. A plain `e` needs a plain region that is inside `l`. Any other
714 -
/// `e` needs an array node of `e` with that count, or a single object that is a view.
714 +
/// `e` needs an array node of `e` with at least that count, or a single object that is a view.
715 715
export fn arrayView(t: &Table, l: u32, o: u64, e: u32, count: u32) -> bool {
716 716
    let total = size(t, e) * count as u64;
717 717
    if o + total > size(t, l) {
718 718
        return false;
719 719
    }
728 728
        let slot = covering(t.layouts, at.layout, at.offset) else {
729 729
            return false;
730 730
        };
731 731
        if slot.offset as u64 == at.offset {
732 732
            if let case il::SlotKind::Array { layout, count: n } = slot.kind {
733 -
                if layout == e and n == count {
733 +
                if layout == e and n >= count {
734 734
                    return true;
735 735
                }
736 736
            }
737 737
        }
738 738
        let next = enter(t.layouts, slot, at.offset) else {
lib/std/lang/il/verify/tests.rad +13 -1
1130 1130
    try check(
1131 1131
        &[oneData()], &[reserve(1, P16), elem(2, 1, reg(0), P4, 4), store(imm(1), 2, 0)], nil
1132 1132
    );
1133 1133
    try check(&[], &[reserve(0, P16), elem(1, 0, imm(0), P4, 5)], verify::Rule::Bounds);
1134 1134
    try check(&[], &[reserve(0, CELLS), elem(1, 0, imm(1), CELL, 3), loadPtr(2, 1, 0)], nil);
1135 -
    try check(&[], &[reserve(0, CELLS), elem(1, 0, imm(1), CELL, 2)], verify::Rule::View);
1135 +
    try check(&[], &[reserve(0, CELLS), elem(1, 0, imm(1), CELL, 2)], nil);
1136 1136
    try check(&[], &[reserve(0, CELLS), elem(1, 0, imm(1), CELL, 4)], verify::Rule::Bounds);
1137 1137
    try check(&[], &[reserve(0, P16), elem(1, 0, imm(0), CELL, 2)], verify::Rule::View);
1138 1138
    try checkBody(
1139 1139
        &[],
1140 1140
        il::ValType::Void,
5356 5356
            sliceCopy(4, 3, LOCAL), retVoid(),
5357 5357
        ]
5358 5358
    );
5359 5359
    try rejected(&mut b, verify::Rule::Return);
5360 5360
}
5361 +
5362 +
/// A protected slice can use a prefix of its backing array.
5363 +
@test unsafe fn sliceArrayPrefixView() throws (testing::TestError) {
5364 +
    let params = [param(0, ptrType(CELLS, true, true, false))];
5365 +
    try check(&params[..], &[reserve(1, SLICECELL), sliceNew(1, reg(0), CELL, 3)], nil);
5366 +
    try check(
5367 +
        &params[..],
5368 +
        &[reserve(1, SLICECELL), sliceNew(1, reg(0), CELL, 4)],
5369 +
        verify::Rule::Bounds
5370 +
    );
5371 +
    try check(&params[..], &[reserve(1, SLICECELL), sliceNew(1, reg(0), CELL, 2)], nil);
5372 +
}