|
| 1 | +use rustc_middle::mir::interpret::{Allocation, ConstAllocation}; |
| 2 | +use rustc_middle::mir::Mutability; |
| 3 | +use rustc_middle::ty::layout::LayoutCx; |
| 4 | +use rustc_middle::ty::{ParamEnv, ParamEnvAnd}; |
| 5 | +use rustc_middle::ty::{Ty, TyCtxt}; |
| 6 | +use rustc_target::abi::{ |
| 7 | + Abi, Align, Endian, FieldsShape, HasDataLayout, Scalar, Size, WrappingRange, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug, Clone, Copy)] |
| 11 | +struct Invariant { |
| 12 | + offset: Size, |
| 13 | + size: Size, |
| 14 | + start: u128, |
| 15 | + end: u128, |
| 16 | +} |
| 17 | + |
| 18 | +fn add_invariants<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, invs: &mut Vec<Invariant>, offset: Size) { |
| 19 | + let x = tcx.layout_of(ParamEnvAnd { param_env: ParamEnv::reveal_all(), value: ty }); |
| 20 | + |
| 21 | + if let Ok(layout) = x { |
| 22 | + if let Abi::Scalar(Scalar::Initialized { value, valid_range }) = layout.layout.abi() { |
| 23 | + let size = value.size(&tcx); |
| 24 | + let WrappingRange { start, end } = valid_range; |
| 25 | + invs.push(Invariant { offset, size, start, end }) |
| 26 | + } |
| 27 | + |
| 28 | + let param_env = ParamEnv::reveal_all(); |
| 29 | + let unwrap = LayoutCx { tcx, param_env }; |
| 30 | + |
| 31 | + match layout.layout.fields() { |
| 32 | + FieldsShape::Primitive => {} |
| 33 | + FieldsShape::Union(_) => {} |
| 34 | + FieldsShape::Array { stride, count } => { |
| 35 | + // TODO: should we just bail if we're making a Too Large type? |
| 36 | + // (Like [bool; 1_000_000]) |
| 37 | + for idx in 0..*count { |
| 38 | + let off = offset + *stride * idx; |
| 39 | + let f = layout.field(&unwrap, idx as usize); |
| 40 | + add_invariants(tcx, f.ty, invs, off); |
| 41 | + } |
| 42 | + } |
| 43 | + FieldsShape::Arbitrary { offsets, .. } => { |
| 44 | + for (idx, &field_offset) in offsets.iter().enumerate() { |
| 45 | + let f = layout.field(&unwrap, idx); |
| 46 | + if f.ty == ty { |
| 47 | + // Some types contain themselves as fields, such as |
| 48 | + // &mut [T] |
| 49 | + // Easy solution is to just not recurse then. |
| 50 | + } else { |
| 51 | + add_invariants(tcx, f.ty, invs, offset + field_offset); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn extend_encoded_int(to: &mut Vec<u8>, endian: Endian, ptr_size: PointerSize, value: Size) { |
| 60 | + match (endian, ptr_size) { |
| 61 | + (Endian::Little, PointerSize::Bits16) => to.extend((value.bytes() as u16).to_le_bytes()), |
| 62 | + (Endian::Little, PointerSize::Bits32) => to.extend((value.bytes() as u32).to_le_bytes()), |
| 63 | + (Endian::Little, PointerSize::Bits64) => to.extend((value.bytes()).to_le_bytes()), |
| 64 | + (Endian::Big, PointerSize::Bits16) => to.extend((value.bytes() as u16).to_be_bytes()), |
| 65 | + (Endian::Big, PointerSize::Bits32) => to.extend((value.bytes() as u32).to_be_bytes()), |
| 66 | + (Endian::Big, PointerSize::Bits64) => to.extend((value.bytes()).to_be_bytes()), |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Clone, Copy)] |
| 71 | +enum PointerSize { |
| 72 | + Bits16, |
| 73 | + Bits32, |
| 74 | + Bits64, |
| 75 | +} |
| 76 | + |
| 77 | +/// Directly returns a `ConstAllocation` containing a list of validity invariants of the given type. |
| 78 | +pub(crate) fn alloc_validity_invariants_of<'tcx>( |
| 79 | + tcx: TyCtxt<'tcx>, |
| 80 | + ty: Ty<'tcx>, |
| 81 | +) -> ConstAllocation<'tcx> { |
| 82 | + let mut invs: Vec<Invariant> = Vec::new(); |
| 83 | + |
| 84 | + let layout = tcx.data_layout(); |
| 85 | + |
| 86 | + let ptr_size = match layout.pointer_size.bits() { |
| 87 | + 16 => PointerSize::Bits16, |
| 88 | + 32 => PointerSize::Bits32, |
| 89 | + 64 => PointerSize::Bits64, |
| 90 | + _ => { |
| 91 | + // Not sure if this can happen, but just return an empty slice? |
| 92 | + let alloc = |
| 93 | + Allocation::from_bytes(Vec::new(), Align::from_bytes(8).unwrap(), Mutability::Not); |
| 94 | + return tcx.intern_const_alloc(alloc); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + add_invariants(tcx, ty, &mut invs, Size::ZERO); |
| 99 | + |
| 100 | + let encode_range = match layout.endian { |
| 101 | + Endian::Little => |r: u128| r.to_le_bytes(), |
| 102 | + Endian::Big => |r: u128| r.to_be_bytes(), |
| 103 | + }; |
| 104 | + |
| 105 | + let mut encoded = Vec::new(); |
| 106 | + |
| 107 | + // TODO: this needs to match the layout of `Invariant` in core/src/intrinsics.rs |
| 108 | + // how do we ensure that? |
| 109 | + for inv in invs { |
| 110 | + extend_encoded_int(&mut encoded, layout.endian, ptr_size, inv.offset); |
| 111 | + extend_encoded_int(&mut encoded, layout.endian, ptr_size, inv.size); |
| 112 | + encoded.extend(encode_range(inv.start)); |
| 113 | + encoded.extend(encode_range(inv.end)); |
| 114 | + } |
| 115 | + |
| 116 | + // TODO: The alignment here should be calculated from the struct definition, I guess? |
| 117 | + let alloc = Allocation::from_bytes(encoded, Align::from_bytes(8).unwrap(), Mutability::Not); |
| 118 | + tcx.intern_const_alloc(alloc) |
| 119 | +} |
0 commit comments