verify: Check slice element alignment

826d60ec47acfc0cb63509c46d28fd11f62e5f510a45296467ff779bd5fc6453
Require slice views and destinations to preserve element alignment so
verified code cannot obtain misaligned pointers.

Assisted-by: Codex:gpt-6
Alexis Sellier committed ago 1 parent 73067c84
lib/std/lang/il/verify.rad +22 -7
650 650
    if not layout::arrayView(t, p.layout, o, e, count) {
651 651
        throw fail(v, Rule::View);
652 652
    }
653 653
}
654 654
655 -
/// The slice slot that a pointer addresses, whose elements must be of layout `e`, or a plain
656 -
/// layout of the same size when `e` is plain.
655 +
/// The slice slot that a pointer addresses. A plain view must have the same
656 +
/// element size as the slot.
657 657
fn sliceSlot(v: &Verifier, p: PtrState, e: u32) -> il::PtrType throws (Error) {
658 658
    let t = &v.ctx.table;
659 659
    let o = try extent(v, p, 0, 0);
660 660
    let case il::SlotKind::Slice(slot) = layout::slotAt(t, p.layout, o) else {
661 661
        throw fail(v, Rule::Slot);
667 667
        throw fail(v, Rule::View);
668 668
    }
669 669
    return slot;
670 670
}
671 671
672 +
/// Check that a slice provides the alignment required by its element view.
673 +
fn sliceSource(v: &Verifier, src: PtrState, e: u32) -> il::PtrType throws (Error) {
674 +
    let slot = try sliceSlot(v, src, e);
675 +
    if layout::alignment(&v.ctx.table, slot.layout) < layout::alignment(&v.ctx.table, e) {
676 +
        throw fail(v, Rule::View);
677 +
    }
678 +
    return slot;
679 +
}
680 +
672 681
/// A slice slot that the slice instructions write.
673 682
record SliceDest: Copy {
674 683
    /// Type of the slot.
675 684
    slot: il::PtrType,
676 685
    /// Whether the slot is in an object of the current frame.
677 686
    local: bool,
678 687
}
679 688
680 -
/// Check the destination of a slice instruction: a writable slice slot of elements `e`.
689 +
/// Check a writable slice slot. The element view must provide the alignment
690 +
/// required by the slot.
681 691
fn sliceDest(v: &Verifier, dst: il::Reg, e: u32) -> SliceDest throws (Error) {
682 692
    let d = try writable(v, dst);
683 -
    return SliceDest { slot: try sliceSlot(v, d, e), local: d.local };
693 +
    let slot = try sliceSlot(v, d, e);
694 +
    if layout::alignment(&v.ctx.table, e) < layout::alignment(&v.ctx.table, slot.layout) {
695 +
        throw fail(v, Rule::View);
696 +
    }
697 +
    return SliceDest { slot, local: d.local };
684 698
}
685 699
686 700
/// Check that a slice slot can receive a slice over memory of class `own` and
687 701
/// mutability `mutable`. A slot that does not own its pointee keeps it only in the frame.
688 702
fn sliceFrom(v: &Verifier, d: SliceDest, own: bool, mutable: bool) throws (Error) {
872 886
            }));
873 887
        }
874 888
        case il::Instr::SliceElem { dst, slice, index, layout: e } => {
875 889
            try operand(v, index);
876 890
            let p = try accessible(v, slice);
877 -
            let slot = try sliceSlot(v, p, e);
891 +
            let slot = try sliceSource(v, p, e);
878 892
            try define(v, dst, RegState::Ptr(PtrState {
879 893
                layout: e,
880 894
                offset: 0,
881 895
                own: slot.own,
882 896
                mutable: slot.mutable,
903 917
        }
904 918
        case il::Instr::SliceSub { dst, src, layout: e, start, end } => {
905 919
            let d = try sliceDest(v, dst, e);
906 920
            try operand(v, start);
907 921
            try operand(v, end);
908 -
            let from = try sliceSlot(v, try accessible(v, src), e);
922 +
            let from = try sliceSource(v, try accessible(v, src), e);
909 923
            try sliceFrom(v, d, from.own, from.mutable);
910 924
        }
911 925
        case il::Instr::LoadPtr { dst, src, offset, nullable } => {
912 926
            let p = try accessible(v, src);
913 927
            let o = try extent(v, p, offset as i64, 0);
1008 1022
            }
1009 1023
            try define(v, dst, RegState::Data);
1010 1024
        }
1011 1025
        case il::Instr::PageSlice { err, slot, handle, write, layout: e } => {
1012 1026
            try operand(v, handle);
1013 -
            let to = try sliceSlot(v, try writable(v, slot), e);
1027 +
            let dest = try sliceDest(v, slot, e);
1028 +
            let to = dest.slot;
1014 1029
            // Page memory is shared and reachable by the kernel, so only plain elements live
1015 1030
            // in it, and the slice must own it with the rights of the access.
1016 1031
            if not to.own {
1017 1032
                throw fail(v, Rule::Class);
1018 1033
            }
lib/std/lang/il/verify/tests.rad +77 -0
1068 1068
        &[reserve(1, SLICEC), sliceElem(2, 1, reg(0), P1), store(imm(1), 2, 0)],
1069 1069
        verify::Rule::Mutability
1070 1070
    );
1071 1071
}
1072 1072
1073 +
/// Slice views preserve the alignment of their elements.
1074 +
@test unsafe fn sliceElementAlignment() throws (testing::TestError) {
1075 +
    let mut weakRead = newBuilder();
1076 +
    let weak = addLayout(&mut weakRead, 16, 8, &[sliceSlot(0, B8, true, true)]);
1077 +
    try checkIn(
1078 +
        &mut weakRead,
1079 +
        &[oneData()],
1080 +
        il::ValType::Void,
1081 +
        &[reserve(1, weak), sliceElem(2, 1, reg(0), P8), retVoid()],
1082 +
        verify::Rule::View
1083 +
    );
1084 +
1085 +
    let mut strongRead = newBuilder();
1086 +
    let strong = addLayout(&mut strongRead, 16, 8, &[sliceSlot(0, P8, true, true)]);
1087 +
    try checkIn(
1088 +
        &mut strongRead,
1089 +
        &[oneData()],
1090 +
        il::ValType::Void,
1091 +
        &[reserve(1, strong), sliceElem(2, 1, reg(0), B8), retVoid()],
1092 +
        nil
1093 +
    );
1094 +
}
1095 +
1096 +
/// A slice write cannot weaken the alignment promised by its destination slot.
1097 +
@test unsafe fn sliceDestinationAlignment() throws (testing::TestError) {
1098 +
    let mut strongWrite = newBuilder();
1099 +
    let dest = addLayout(&mut strongWrite, 16, 8, &[sliceSlot(0, P8, true, true)]);
1100 +
    try checkIn(
1101 +
        &mut strongWrite,
1102 +
        &[param(0, ptrType(B8, true, true, false))],
1103 +
        il::ValType::Void,
1104 +
        &[reserve(1, dest), sliceNew(1, reg(0), B8, 1), retVoid()],
1105 +
        verify::Rule::View
1106 +
    );
1107 +
1108 +
    let mut page = newBuilder();
1109 +
    let pageDest = addLayout(&mut page, 16, 8, &[sliceSlot(0, P8, true, true)]);
1110 +
    try checkIn(
1111 +
        &mut page,
1112 +
        &[oneData()],
1113 +
        il::ValType::Void,
1114 +
        &[reserve(1, pageDest), pageSlice(2, 1, true, B8), retVoid()],
1115 +
        verify::Rule::View
1116 +
    );
1117 +
1118 +
    let mut aligned = newBuilder();
1119 +
    let alignedDest = addLayout(&mut aligned, 16, 8, &[sliceSlot(0, P8, true, true)]);
1120 +
    try checkIn(
1121 +
        &mut aligned,
1122 +
        &[param(0, ptrType(P8, true, true, false))],
1123 +
        il::ValType::Void,
1124 +
        &[reserve(1, alignedDest), sliceNew(1, reg(0), P8, 1), retVoid()],
1125 +
        nil
1126 +
    );
1127 +
1128 +
    let mut strongSource = newBuilder();
1129 +
    let weakDest = addLayout(&mut strongSource, 16, 8, &[sliceSlot(0, B8, true, true)]);
1130 +
    try checkIn(
1131 +
        &mut strongSource,
1132 +
        &[param(0, ptrType(P8, true, true, false))],
1133 +
        il::ValType::Void,
1134 +
        &[reserve(1, weakDest), sliceNew(1, reg(0), P8, 1), retVoid()],
1135 +
        nil
1136 +
    );
1137 +
1138 +
    let mut weakSlice = newBuilder();
1139 +
    let weakSlot = addLayout(&mut weakSlice, 16, 8, &[sliceSlot(0, B8, true, true)]);
1140 +
    let strongSlot = addLayout(&mut weakSlice, 16, 8, &[sliceSlot(0, P8, true, true)]);
1141 +
    try checkIn(
1142 +
        &mut weakSlice,
1143 +
        &[],
1144 +
        il::ValType::Void,
1145 +
        &[reserve(0, weakSlot), reserve(1, strongSlot), sliceSub(1, 0, P8), retVoid()],
1146 +
        verify::Rule::View
1147 +
    );
1148 +
}
1149 +
1073 1150
/// A sub-slice has the rights of the slice that it is taken from.
1074 1151
@test unsafe fn subSlice() throws (testing::TestError) {
1075 1152
    try check(&[], &[reserve(0, SLICE), reserve(1, SLICE), sliceSub(1, 0, P1)], nil);
1076 1153
    try check(
1077 1154
        &[], &[reserve(0, SLICEC), reserve(1, SLICE), sliceSub(1, 0, P1)], verify::Rule::Mutability