Lines Matching defs:layout
19 /// The layout of a type which might be dynamically-sized.
21 /// `DstLayout` describes the layout of sized types, slice types, and "slice
28 /// the layout of real Rust types or instances of types.
180 /// For a completed layout, this value is a multiple of `DstLayout::align`.
196 /// For a completed layout, `size_align` is at least `DstLayout::align`.
199 /// a nested DST's own layout. For example, a `repr(packed(2))` wrapper
241 /// whole number of elements, even for a valid Rust layout. For example, a
417 /// Computes the size described by this layout for `elems` trailing slice
436 // `size_rounding_align_and_phase`. The normalized layout formula,
479 // `usize::MAX + 1`. This sum need not fit: a raw layout need not
488 // `usize`, so its residue is the exact size specified by the layout.
518 // For either layout, interpret its normalized size formula over the
558 // giving the following identity for each layout:
593 /// `elems`, the returned layout describes:
601 /// method. The returned layout preserves the physical slice offset
842 /// \[1\] Per <https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers>:
872 /// Assumes that this layout lacks static shallow padding.
946 /// Unsafe code may assume that `DstLayout` is the correct layout for `T`.
1022 /// Unsafe code may assume that `DstLayout` is the correct layout for `[T]`.
1071 /// This method cannot be used to match the layout of a record with the
1078 /// resulting `DstLayout` will correctly encode the layout of that struct.
1084 /// `repr(packed)`, and inner-type layout rules. The Reference prohibits the
1085 /// direct form, and Rust's continued acceptance and layout behavior are
1088 /// layout.
1116 let mut layout = DstLayout::new_zst(repr_align);
1123 layout = layout.extend(field, repr_packed);
1127 layout = layout.pad_to_align();
1129 // SAFETY: `layout` accurately describes the layout of a `repr(C)`
1131 // the given `fields`. The `layout` is constructed using a sequence of
1141 layout
1144 /// Like `Layout::extend`, this creates a layout that describes a record
1145 /// whose layout consists of `self` followed by `next` that includes the
1148 /// In order to match the layout of a `#[repr(C)]` struct, this method
1150 /// padding, call `DstLayout::pad_to_align` after extending the layout for
1155 /// This method cannot be used to match the layout of a record with the
1161 /// fields whose layout are `self`, and those fields are immediately
1162 /// followed by a field whose layout is `field`, then unsafe code may rely
1163 /// on `self.extend(field, repr_packed)` producing a layout that correctly
1210 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1222 // If the layout is already a DST, we panic; DSTs cannot be extended
1231 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
1239 // if the layout components can correspond to a leading layout
1252 // If the trailing field is sized, the resulting layout
1254 // preceding layout, the size of the new field, and the
1258 // panic) if the layout components can correspond to a
1259 // leading layout fragment of a valid Rust type, but may
1276 // will the resulting layout. The offset of the trailing
1282 // panic) if the layout components can correspond to a
1283 // leading layout fragment of a valid Rust type, but may
1324 /// Like `Layout::pad_to_align`, this routine rounds the size of this layout
1328 /// In order to match the layout of a `#[repr(C)]` struct, this method
1331 /// This method cannot be used to match the layout of a record with the
1337 /// whose layout are `self` followed only by zero or more bytes of trailing
1339 /// `self.pad_to_align()` producing a layout that correctly
1340 /// encapsulates the layout of that type.
1379 // input layout describes:
1442 /// Produces `false` when this layout is marked as having no static shallow
1546 /// Given a completed type layout, produces metadata and a split index for
1574 /// When `self` accurately describes a completed type layout, including
1588 /// These guarantees require the completed-layout premise; `DstLayout`
1589 /// also permits unfinished layout fragments and arbitrary field values.
1707 // Under the documented completed-layout premise, all valid sizes
1820 // layout premises that it consumes.
2035 Some(layout) => layout,
2293 let layout = crate::trailing_slice_layout::<Target>();
2294 let trailing_size = elems.checked_mul(layout.elem_size)?;
2298 let (alignment, phase) = layout.size_rounding_align_and_phase.components();
2303 let object_size = layout.size_base.checked_add(rounded)?;
2304 let trailing_end = layout.offset.checked_add(trailing_size).unwrap();
2307 let padding = layout.padding_for_elems(elems);
2362 // [1] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#size-and-alignment
2363 // [2] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#the-alignment-modifiers
2376 // A slice shares its array section's layout [3]. Array size is
2379 // [3] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#slice-layout
2380 // [4] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#array-layout
2386 // the layout of the fields themselves" [5]. Thus packing a nested
2389 // [5] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#representations
2398 // [6] https://doc.rust-lang.org/1.93.0/reference/type-layout.html#reprc-structs
2428 /// `DstLayout` cannot construct the layout. This includes static size
2437 let mut layout = DstLayout {
2454 layout = DstLayout::for_repr_c_struct(Some(align), Some(packed), &[prefix, layout]);
2456 move |elems: usize| elems.size_for_metadata(layout)
2519 "unrepresentable test layout: {:?}, {:?}",
2724 let layout = TrailingSliceLayout {
2730 assert_eq!(layout.size_offset(), offset);
2733 let unrounded = offset + elems * layout.elem_size;
2736 assert_eq!(layout.size_for_elems(elems), Some(old_size));
2748 let checked_size = |layout: TrailingSliceLayout, trailing_bytes: usize| {
2749 let (alignment, phase) = layout.size_rounding_align_and_phase.components();
2752 layout.size_base.checked_add(rounded)
2759 let mut layout = TrailingSliceLayout {
2766 match layout.max_trailing_bytes(available) {
2768 assert!(checked_size(layout, max_bytes).unwrap() <= available);
2770 assert!(checked_size(layout, next_bytes)
2776 assert!(checked_size(layout, 0)
2784 layout.elem_size = elem_size;
2785 let max_elems = layout
2799 .and_then(|bytes| checked_size(layout, bytes));
2805 layout.offset = offset;
2806 assert_eq!(layout.size_for_elems(elems), expected);
2857 let layout = TrailingSliceLayout {
2873 layout.padding_for_elems(elems),
2884 let layout = DstLayout::for_slice::<u32>();
2885 match layout.size_info {
2892 assert_eq!(layout.align.get(), 4);
2899 // This macro constructs a layout corresponding to a `u8` and extends
2967 // layout.
3030 // For all valid alignments `align`, construct a one-byte layout aligned
3032 // resulting layout is equal to `align`.
3034 let layout = DstLayout {
3041 layout.pad_to_align(),
3100 let layout = DstLayout {
3114 layout.pad_to_align(),
3264 fn layout<S: Into<SizeInfo>>(s: S, align: usize) -> DstLayout {
3282 /// layout(_, _).validate(_, _, _), Ok(Some((_, _)))
3324 layout($size:tt, $align:tt)
3346 layout(size_info, align).validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
3356 "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?})" ,size_info, align, addr, bytes_len, cast_type
3400 layout(((1..8) | ((1..8), (1..8))), _).validate([0], [0], _),
3404 layout(((2..8) | ((2..8), (2..8))), _).validate([0], [1], Prefix),
3408 layout(((2..8) | ((2..8), (2..8))), _).validate([0x1000_0000 - 1], [1], Suffix),
3413 test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
3414 test!(layout(_, [2]).validate(ODDS, _, Prefix), Ok(Err(MetadataCastError::Alignment)));
3417 test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
3418 test!(layout(_, [2]).validate(EVENS, ODDS, Suffix), Ok(Err(MetadataCastError::Alignment)));
3436 test!(layout((_, [0]), _).validate(_, _, _), Err(Some(msgs::TRAILING) | None),);
3439 test!(layout(_, _).validate([usize::MAX], (1..100), _), Err(Some(msgs::OVERFLOW) | None));
3440 test!(layout(_, _).validate((1..100), [usize::MAX], _), Err(Some(msgs::OVERFLOW) | None));
3442 layout(_, _).validate(
3454 (layout, addr, bytes_len, cast_type): (DstLayout, usize, usize, CastType),
3457 layout.validate_cast_and_convert_metadata(addr, bytes_len, cast_type)
3459 let (size_info, align) = (layout.size_info, layout.align);
3461 "layout({:?}, {}).validate_cast_and_convert_metadata({}, {}, {:?}) => ({}, {})",
3469 let sized = matches!(layout.size_info, SizeInfo::Sized { .. });
3472 let resulting_size = match layout.size_info {
3511 let min_size = match layout.size_info {
3531 let misaligned = (base + addr) % layout.align != 0;
3545 .map(|(size_info, align)| layout(size_info, align));
3590 // built-in APIs to confirm that Rust makes decisions about type layout
3614 let layout = {
3676 // understanding of how Rust determines the layout of repr(C)
3697 let (got_elems, got_split_at) = layout
3945 //! Arithmetic models and input generators for the layout proofs.
3950 //! domains: `Some` supplies an expected layout, while `None` excludes an
3965 /// Here, `size_align` and `size_phase` come from `layout.size_rounding_align_and_phase`.
3972 layout: TrailingSliceLayout<ElementSize>,
3976 layout.size_rounding_align_and_phase.components().1.checked_add(trailing_size)?;
3978 util::padding_needed_for(without_padding, layout.size_rounding_align_and_phase.align());
3980 layout.size_base.checked_add(rounded)
4017 /// Trailing padding for the enclosing layout is left to `padded_layout`.
4055 /// A sized layout rounds its size up to a multiple of `layout.align`. A DST
4060 /// layout.align)
4067 /// cleared only when a sized layout gains padding; DST padding is accounted
4070 /// Returns `None` if `layout.align` is not a power of two or a checked
4074 pub(super) fn padded_layout(layout: DstLayout) -> Option<DstLayout> {
4075 if !layout.align.is_power_of_two() {
4078 let (size_info, no_static_padding) = match layout.size_info {
4080 let rounded = round_up(size, layout.align)?;
4085 let (size_base, size_rounding_align_and_phase) = if inner_align >= layout.align {
4087 round_up(trailing.size_base, layout.align)?,
4092 let new_phase = fixed & (layout.align.get() - 1);
4093 (fixed - new_phase, RoundingAlignAndPhase::new(layout.align, new_phase))
4107 statically_shallow_unpadded: layout.statically_shallow_unpadded && no_static_padding,
4108 ..layout
4112 /// Models a complete layout for `DstLayout::for_repr_c_struct` by composing
4117 /// alignment; the final layout is padded to its resulting alignment. Only
4132 let mut layout = DstLayout {
4138 layout = extended_layout(layout, field, packed)?;
4140 padded_layout(layout)
4144 // These generators retain the narrower domains of the Rust-layout proofs.
4148 /// callers must impose any constraints relating it to a layout's size.
4163 /// standalone Rust-layout proofs.
4170 /// with this layout exists.
4208 /// constraints involving the enclosing layout's alignment.
4270 let layout = TrailingSliceLayout {
4277 let (size_align, size_phase) = layout.size_rounding_align_and_phase.components();
4283 let trailing_bytes = elems.wrapping_mul(layout.elem_size);
4286 let object_size = layout.size_base.wrapping_add(rounded);
4287 let trailing_end = layout.offset.wrapping_add(trailing_bytes);
4288 assert_eq!(layout.padding_for_elems(elems), object_size.wrapping_sub(trailing_end));
4332 let layout = TrailingSliceLayout {
4339 assert_eq!(layout.size_offset(), offset);
4347 assert_eq!(layout.size_for_elems(trailing_size), old_size);
4348 assert_eq!(slice_dst_size_for_trailing_bytes(layout, trailing_size), old_size);
4371 let layout: TrailingSliceLayout = any_bounded_trailing_layout();
4376 let size_offset = layout.size_offset();
4378 let advanced = layout.advance(bytes, elem_size);
4386 assert_eq!(advanced.offset, layout.offset);
4391 layout.size_rounding_align_and_phase.align()
4395 assert_eq!(advanced.size_base & size_align_mask, layout.size_base & size_align_mask);
4397 let size_align = layout.size_rounding_align_and_phase.align();
4399 let size_prefix = layout.size_base & (size_align.get() - 1);
4586 // branch. A zero-sized layout makes every aligned case succeed and
4588 let layout = DstLayout {
4593 match layout.validate_cast_and_convert_metadata(addr, bytes_len, cast_type) {
4686 let layout: DstLayout = any_valid_dst_layout();
4688 let SizeInfo::SliceDst(size_info) = layout.size_info else {
4718 if !layout.requires_dynamic_padding() {
4748 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
4759 // [1] Per https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers:
4805 // If the trailing field is sized, the resulting layout will
4807 // layout, the size of the new field, and the size of
4823 // the basic invariants of a valid Rust layout. At
4895 // the basic invariants of a valid Rust layout. At
4901 panic!("The extension of a layout with a DST must result in a DST.")
4926 layout: DstLayout,
4931 assert_eq!(padded.align, layout.align);
4932 assert_eq!(padded.statically_shallow_unpadded, layout.statically_shallow_unpadded);
4935 panic!("The padding of a DST layout must result in a DST layout.")
4961 let layout: DstLayout = any_valid_dst_layout();
4962 let SizeInfo::Sized { size: unpadded_size } = layout.size_info else {
4967 let padded = layout.pad_to_align();
4968 assert_eq!(padded.align, layout.align);
4970 panic!("The padding of a sized layout must result in a sized layout.")
4973 // If the layout is sized, it will remain sized after padding is added.
4976 let padding = padding_needed_for(unpadded_size, layout.align);
4980 layout.statically_shallow_unpadded && padding == 0
4985 let layout_analog = Layout::from_size_align(unpadded_size, layout.align.get()).unwrap();
4987 assert_eq!(padded_analog.align(), layout.align.get());
4996 let layout: DstLayout = any_valid_dst_layout();
4997 let SizeInfo::SliceDst(trailing) = layout.size_info else {
5001 kani::assume(trailing.size_rounding_align_and_phase.align() >= layout.align);
5003 // `any_valid_dst_layout` requires the zero-metadata layout to be valid.
5008 let min_padding = padding_needed_for(min_size, layout.align);
5012 let padding = padding_needed_for(trailing.size_base, layout.align);
5015 let padded = layout.pad_to_align();
5022 prove_dst_layout_pad_to_align_dst_invariants(layout, trailing, padded);
5080 let layout: DstLayout = any_valid_dst_layout();
5081 let SizeInfo::SliceDst(trailing) = layout.size_info else {
5086 kani::assume(size_align < layout.align);
5088 // `any_valid_dst_layout` requires the zero-metadata layout to be valid.
5093 let min_padding = padding_needed_for(min_size, layout.align);
5107 let expected_phase = t & (layout.align.get() - 1);
5111 let padded = layout.pad_to_align();
5116 RoundingAlignAndPhase::new(layout.align, expected_phase)
5118 prove_dst_layout_pad_to_align_dst_invariants(layout, trailing, padded);