Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/struct_to_bytes.rs

raw
1 // Copyright 2019 The Fuchsia Authors
2 //
3 // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 // This file may not be copied, modified, or distributed except according to
7 // those terms.
8
9 // See comment in `include.rs` for why we disable the prelude.
10 #![no_implicit_prelude]
11 #![allow(warnings)]
12
13 include!("include.rs");
14
15 // A struct can derive `IntoBytes` if all of its fields implement `IntoBytes`
16 // and its representation lets the derive prove that the struct introduces no
17 // uninitialized outer padding. The cases below are grouped by representation
18 // and by the proof strategy used by the derive.
19
20 //
21 // Non-generic `repr(C)`
22 //
23
24 #[derive(imp::IntoBytes)]
25 #[zerocopy(crate = "zerocopy_renamed")]
26 #[repr(C)]
27 struct CZst;
28
29 util_assert_impl_all!(CZst: imp::IntoBytes);
30
31 #[derive(imp::IntoBytes)]
32 #[zerocopy(crate = "zerocopy_renamed")]
33 #[repr(C, align(8))]
34 struct ReprCAlignedZst;
35
36 util_assert_impl_all!(ReprCAlignedZst: imp::IntoBytes);
37
38 #[derive(imp::IntoBytes, Copy, Clone)]
39 #[zerocopy(crate = "zerocopy_renamed")]
40 #[repr(C, align(8))]
41 struct ReprCAligned8([u8; 8]);
42
43 util_assert_impl_all!(ReprCAligned8: imp::IntoBytes);
44
45 #[derive(imp::IntoBytes)]
46 #[zerocopy(crate = "zerocopy_renamed")]
47 #[repr(C)]
48 struct C {
49 a: u8,
50 b: u8,
51 c: util::AU16,
52 }
53
54 util_assert_impl_all!(C: imp::IntoBytes);
55
56 #[derive(imp::IntoBytes)]
57 #[zerocopy(crate = "zerocopy_renamed")]
58 #[repr(C)]
59 struct ReprCSyntacticDst {
60 a: u8,
61 b: u8,
62 c: [util::AU16],
63 }
64
65 util_assert_impl_all!(ReprCSyntacticDst: imp::IntoBytes);
66
67 #[derive(imp::IntoBytes)]
68 #[zerocopy(crate = "zerocopy_renamed")]
69 #[repr(C, align(2))]
70 struct ReprCSyntacticDstAligned {
71 a: [[u8; 2]],
72 }
73
74 util_assert_impl_all!(ReprCSyntacticDstAligned: imp::IntoBytes);
75
76 //
77 // Generic `repr(C)`
78 //
79
80 #[derive(imp::IntoBytes)]
81 #[zerocopy(crate = "zerocopy_renamed")]
82 #[repr(C)]
83 struct ReprCGenericOneField<T: ?imp::Sized> {
84 t: T,
85 }
86
87 // Even though `ReprCGenericOneField` has generic type arguments, its one-field
88 // layout never requires an `Unaligned` bound. Sized, array, and slice
89 // instantiations are asserted in `assert_repr_c_homogeneous` below.
90
91 // Given `T: Sized + IntoBytes`, a plain `repr(C)` struct whose fields are all
92 // `T`, `[T; N]`, or a final `[T]` introduces no outer padding. `T` and all of
93 // its arrays and slices have the same alignment. The size of `T`, every array,
94 // and every slice value is a multiple of that alignment; bytes within each `T`
95 // are covered by `T`'s `IntoBytes` implementation.
96 //
97 // The following six declarations exhaustively enumerate the direct two-field
98 // shape combinations in this grammar. A slice may only be the final field, so
99 // the first field is either `T` or `[T; N]`, while the second is `T`, `[T; M]`,
100 // or `[T]`. The one-field case is covered by `ReprCGenericOneField` above.
101
102 #[derive(imp::IntoBytes)]
103 #[zerocopy(crate = "zerocopy_renamed")]
104 #[repr(C)]
105 struct ReprCHomogeneousTThenT<T>(T, T);
106
107 #[derive(imp::IntoBytes)]
108 #[zerocopy(crate = "zerocopy_renamed")]
109 #[repr(C)]
110 struct ReprCHomogeneousTThenArray<T, const N: usize>(T, [T; N]);
111
112 #[derive(imp::IntoBytes)]
113 #[zerocopy(crate = "zerocopy_renamed")]
114 #[repr(C)]
115 struct ReprCHomogeneousArrayThenT<T, const N: usize>([T; N], T);
116
117 #[derive(imp::IntoBytes)]
118 #[zerocopy(crate = "zerocopy_renamed")]
119 #[repr(C)]
120 struct ReprCHomogeneousArrayThenArray<T, const N: usize, const M: usize>([T; N], [T; M]);
121
122 #[derive(imp::IntoBytes)]
123 #[zerocopy(crate = "zerocopy_renamed")]
124 #[repr(C)]
125 struct ReprCHomogeneousTThenSlice<T>(T, [T]);
126
127 #[derive(imp::IntoBytes)]
128 #[zerocopy(crate = "zerocopy_renamed")]
129 #[repr(C)]
130 struct ReprCHomogeneousArrayThenSlice<T, const N: usize>([T; N], [T]);
131
132 // Exercise longer named-field structs containing every allowed sized field
133 // shape, both with and without a trailing slice. Together with the two-field
134 // matrix above, these ensure that the derive handles repeated transitions and
135 // both of its sized/DST paths rather than special-casing a particular arity.
136 // The literal-length field also ensures that recognition does not depend on
137 // the array length being a const parameter.
138 #[derive(imp::IntoBytes)]
139 #[zerocopy(crate = "zerocopy_renamed")]
140 #[repr(C)]
141 struct ReprCHomogeneousMixedSized<T, const N: usize, const M: usize> {
142 literal: [T; 0],
143 array0: [T; N],
144 t0: T,
145 array1: [T; M],
146 t1: T,
147 }
148
149 #[derive(imp::IntoBytes)]
150 #[zerocopy(crate = "zerocopy_renamed")]
151 #[repr(C)]
152 struct ReprCHomogeneousMixedDst<T, const N: usize, const M: usize> {
153 t0: T,
154 array0: [T; N],
155 t1: T,
156 array1: [T; M],
157 tail: [T],
158 }
159
160 // `align(1)` cannot increase a type's alignment, so it preserves the same
161 // no-padding argument as an unmodified `repr(C)`. Test both the sized and DST
162 // paths.
163 #[derive(imp::IntoBytes)]
164 #[zerocopy(crate = "zerocopy_renamed")]
165 #[repr(C, align(1))]
166 struct ReprCHomogeneousAlign1Sized<T, const N: usize>(T, [T; N], T);
167
168 #[derive(imp::IntoBytes)]
169 #[zerocopy(crate = "zerocopy_renamed")]
170 #[repr(C, align(1))]
171 struct ReprCHomogeneousAlign1Dst<T, const N: usize>(T, [T; N], [T]);
172
173 // `packed(2)` caps every field's alignment at the same value. Since every
174 // field's size is still a multiple of that capped alignment, it likewise
175 // preserves the no-padding argument. A packed DST may only contain elements
176 // which do not need drop, so the DST declaration additionally requires
177 // `Copy`.
178 #[derive(imp::IntoBytes)]
179 #[zerocopy(crate = "zerocopy_renamed")]
180 #[repr(C, packed(2))]
181 struct ReprCHomogeneousPacked2Sized<T, const N: usize>(T, [T; N], T);
182
183 #[derive(imp::IntoBytes)]
184 #[zerocopy(crate = "zerocopy_renamed")]
185 #[repr(C, packed(2))]
186 struct ReprCHomogeneousPacked2Dst<T: imp::Copy, const N: usize>(T, [T; N], [T]);
187
188 // This generic assertion is stronger than checking a finite set of concrete
189 // element types and array lengths: its body only assumes `T: IntoBytes`, and
190 // is type-checked for every `N` and `M`.
191 fn assert_repr_c_homogeneous<T: imp::IntoBytes, const N: usize, const M: usize>() {
192 fn assert_into_bytes<T: ?imp::Sized + imp::IntoBytes>() {}
193
194 assert_into_bytes::<ReprCGenericOneField<T>>();
195 assert_into_bytes::<ReprCGenericOneField<[T; N]>>();
196 assert_into_bytes::<ReprCGenericOneField<[T]>>();
197 assert_into_bytes::<ReprCHomogeneousTThenT<T>>();
198 assert_into_bytes::<ReprCHomogeneousTThenArray<T, N>>();
199 assert_into_bytes::<ReprCHomogeneousArrayThenT<T, N>>();
200 assert_into_bytes::<ReprCHomogeneousArrayThenArray<T, N, M>>();
201 assert_into_bytes::<ReprCHomogeneousTThenSlice<T>>();
202 assert_into_bytes::<ReprCHomogeneousArrayThenSlice<T, N>>();
203 assert_into_bytes::<ReprCHomogeneousMixedSized<T, N, M>>();
204 assert_into_bytes::<ReprCHomogeneousMixedDst<T, N, M>>();
205 assert_into_bytes::<ReprCHomogeneousAlign1Sized<T, N>>();
206 assert_into_bytes::<ReprCHomogeneousAlign1Dst<T, N>>();
207 assert_into_bytes::<ReprCHomogeneousPacked2Sized<T, N>>();
208 }
209
210 fn assert_repr_c_homogeneous_packed_dst<T: imp::Copy + imp::IntoBytes, const N: usize>() {
211 fn assert_into_bytes<T: ?imp::Sized + imp::IntoBytes>() {}
212
213 assert_into_bytes::<ReprCHomogeneousPacked2Dst<T, N>>();
214 }
215
216 // `AU16` is guaranteed to have alignment 2 and not implement `Unaligned`, so
217 // these assertions distinguish the homogeneous case from the existing
218 // `Unaligned` fallback. The witnesses cover every field shape, zero and
219 // nonzero arrays in both positions, and both the sized and DST paths.
220 util_assert_impl_all!(ReprCHomogeneousTThenT<util::AU16>: imp::IntoBytes);
221 util_assert_impl_all!(ReprCHomogeneousTThenArray<util::AU16, 0>: imp::IntoBytes);
222 util_assert_impl_all!(ReprCHomogeneousArrayThenT<util::AU16, 3>: imp::IntoBytes);
223 util_assert_impl_all!(ReprCHomogeneousArrayThenArray<util::AU16, 0, 3>: imp::IntoBytes);
224 util_assert_impl_all!(ReprCHomogeneousTThenSlice<util::AU16>: imp::IntoBytes);
225 util_assert_impl_all!(ReprCHomogeneousArrayThenSlice<util::AU16, 0>: imp::IntoBytes);
226 util_assert_impl_all!(ReprCHomogeneousArrayThenSlice<util::AU16, 3>: imp::IntoBytes);
227 util_assert_impl_all!(ReprCHomogeneousMixedSized<util::AU16, 0, 3>: imp::IntoBytes);
228 util_assert_impl_all!(ReprCHomogeneousMixedDst<util::AU16, 0, 3>: imp::IntoBytes);
229 util_assert_impl_all!(ReprCHomogeneousAlign1Sized<util::AU16, 0>: imp::IntoBytes);
230 util_assert_impl_all!(ReprCHomogeneousAlign1Dst<util::AU16, 3>: imp::IntoBytes);
231 // This exercises the case in which `packed(2)` actually lowers the element
232 // alignment rather than merely leaving it unchanged.
233 util_assert_impl_all!(ReprCHomogeneousPacked2Sized<ReprCAligned8, 0>: imp::IntoBytes);
234 util_assert_impl_all!(ReprCHomogeneousPacked2Dst<ReprCAligned8, 3>: imp::IntoBytes);
235
236 // Zero-sized element types still carry alignment. In particular, a derive
237 // must not assume that an aligned field occupies any bytes.
238 util_assert_impl_all!(ReprCHomogeneousArrayThenSlice<ReprCAlignedZst, 0>: imp::IntoBytes);
239
240 // `IntoBytes` does not imply `Immutable`; interior-mutable element types are
241 // therefore part of the supported set too.
242 util_assert_impl_all!(
243 ReprCHomogeneousArrayThenSlice<imp::UnsafeCell<util::AU16>, 3>: imp::IntoBytes
244 );
245
246 // A zero-length array does not eliminate the `T: IntoBytes` requirement: the
247 // trailing slice can still contain elements. `MaybeUninit<u8>` implements
248 // `Unaligned` but not `IntoBytes`, isolating the bound under test.
249 util_assert_not_impl_any!(
250 ReprCHomogeneousArrayThenSlice<imp::MaybeUninit<u8>, 0>: imp::IntoBytes
251 );
252
253 // Generic `repr(C)` structs outside of the homogeneous family fall back to
254 // requiring every field type to be `Unaligned`.
255 #[derive(imp::IntoBytes)]
256 #[zerocopy(crate = "zerocopy_renamed")]
257 #[repr(C)]
258 struct ReprCIndependentFields<T, U: ?imp::Sized> {
259 t: T,
260 u: U,
261 }
262
263 util_assert_impl_all!(ReprCIndependentFields<u8, [u8; 2]>: imp::IntoBytes);
264 util_assert_impl_all!(ReprCIndependentFields<u8, [[u8; 2]]>: imp::IntoBytes);
265 util_assert_not_impl_any!(ReprCIndependentFields<u8, util::AU16>: imp::IntoBytes);
266 util_assert_not_impl_any!(ReprCIndependentFields<u8, [util::AU16]>: imp::IntoBytes);
267
268 // The common element type is essential. Here, the zero-length `AU16` array
269 // gives the struct alignment 2, while an odd-length `[u8]` tail requires a
270 // trailing padding byte.
271 #[derive(imp::IntoBytes)]
272 #[zerocopy(crate = "zerocopy_renamed")]
273 #[repr(C)]
274 struct ReprCHeterogeneousArrayThenSlice<T, U, const N: usize>([T; N], [U]);
275
276 util_assert_not_impl_any!(
277 ReprCHeterogeneousArrayThenSlice<util::AU16, u8, 0>: imp::IntoBytes
278 );
279
280 //
281 // `repr(transparent)`
282 //
283
284 #[derive(imp::IntoBytes)]
285 #[zerocopy(crate = "zerocopy_renamed")]
286 #[repr(transparent)]
287 struct Transparent {
288 a: u8,
289 b: (),
290 }
291
292 util_assert_impl_all!(Transparent: imp::IntoBytes);
293
294 #[derive(imp::IntoBytes)]
295 #[zerocopy(crate = "zerocopy_renamed")]
296 #[repr(transparent)]
297 struct TransparentGeneric<T: ?imp::Sized> {
298 a: (),
299 b: T,
300 }
301
302 util_assert_impl_all!(TransparentGeneric<u64>: imp::IntoBytes);
303 util_assert_impl_all!(TransparentGeneric<[u64]>: imp::IntoBytes);
304
305 #[derive(imp::IntoBytes)]
306 #[zerocopy(crate = "zerocopy_renamed")]
307 #[repr(transparent)]
308 struct Unsized {
309 a: [u8],
310 }
311
312 util_assert_impl_all!(Unsized: imp::IntoBytes);
313
314 // Deriving `IntoBytes` should work if the struct has bounded parameters.
315
316 #[derive(imp::IntoBytes)]
317 #[zerocopy(crate = "zerocopy_renamed")]
318 #[repr(transparent)]
319 struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::IntoBytes, const N: usize>(
320 [T; N],
321 imp::PhantomData<&'a &'b ()>,
322 )
323 where
324 'a: 'b,
325 'b: 'a,
326 T: 'a + 'b + imp::IntoBytes;
327
328 util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::IntoBytes);
329
330 //
331 // Packed representations
332 //
333
334 #[derive(imp::IntoBytes)]
335 #[zerocopy(crate = "zerocopy_renamed")]
336 #[repr(C, packed)]
337 struct CZstPacked;
338
339 util_assert_impl_all!(CZstPacked: imp::IntoBytes);
340
341 #[derive(imp::IntoBytes)]
342 #[zerocopy(crate = "zerocopy_renamed")]
343 #[repr(C, packed)]
344 struct CPacked {
345 a: u8,
346 // NOTE: The `u16` type is not guaranteed to have alignment 2, although it
347 // does on many platforms. However, to fix this would require a custom type
348 // with a `#[repr(align(2))]` attribute, and `#[repr(packed)]` types are not
349 // allowed to transitively contain `#[repr(align(...))]` types. Thus, we
350 // have no choice but to use `u16` here. Luckily, these tests run in CI on
351 // platforms on which `u16` has alignment 2, so this isn't that big of a
352 // deal.
353 b: u16,
354 }
355
356 util_assert_impl_all!(CPacked: imp::IntoBytes);
357
358 #[derive(imp::IntoBytes)]
359 #[zerocopy(crate = "zerocopy_renamed")]
360 #[repr(C, packed(2))]
361 // The same caveats as for CPacked apply - we're assuming u64 is at least
362 // 4-byte aligned by default. Without packed(2), this should fail, as there
363 // would be padding between a/b assuming u64 is 4+ byte aligned.
364 struct CPacked2 {
365 a: u16,
366 b: u64,
367 }
368
369 util_assert_impl_all!(CPacked2: imp::IntoBytes);
370
371 #[derive(imp::IntoBytes)]
372 #[zerocopy(crate = "zerocopy_renamed")]
373 #[repr(C, packed)]
374 struct CPackedGeneric<T, U: ?imp::Sized> {
375 t: T,
376 // Unsized types stored in `repr(packed)` structs must not be dropped
377 // because dropping them in-place might be unsound depending on the
378 // alignment of the outer struct. Sized types can be dropped by first being
379 // moved to an aligned stack variable, but this isn't possible with unsized
380 // types.
381 u: imp::ManuallyDrop<U>,
382 }
383
384 util_assert_impl_all!(CPackedGeneric<u8, util::AU16>: imp::IntoBytes);
385 util_assert_impl_all!(CPackedGeneric<u8, [util::AU16]>: imp::IntoBytes);
386
387 #[derive(imp::IntoBytes)]
388 #[zerocopy(crate = "zerocopy_renamed")]
389 #[repr(packed)]
390 struct PackedGeneric<T, U: ?imp::Sized> {
391 t: T,
392 // Unsized types stored in `repr(packed)` structs must not be dropped
393 // because dropping them in-place might be unsound depending on the
394 // alignment of the outer struct. Sized types can be dropped by first being
395 // moved to an aligned stack variable, but this isn't possible with unsized
396 // types.
397 u: imp::ManuallyDrop<U>,
398 }
399
400 util_assert_impl_all!(PackedGeneric<u8, util::AU16>: imp::IntoBytes);
401 util_assert_impl_all!(PackedGeneric<u8, [util::AU16]>: imp::IntoBytes);
402
403 // Test for the failure reported in #1182.
404
405 #[derive(imp::IntoBytes)]
406 #[zerocopy(crate = "zerocopy_renamed")]
407 #[repr(C, packed)]
408 pub struct IndexEntryFlags(u8);
409
410 #[derive(imp::IntoBytes)]
411 #[zerocopy(crate = "zerocopy_renamed")]
412 #[repr(C, packed)]
413 pub struct IndexEntry<const SIZE_BLOCK_ID: usize> {
414 block_number: imp::native_endian::U64,
415 flags: IndexEntryFlags,
416 block_id: [u8; SIZE_BLOCK_ID],
417 }
418
419 util_assert_impl_all!(IndexEntry<0>: imp::IntoBytes);
420 util_assert_impl_all!(IndexEntry<1>: imp::IntoBytes);
421
422 //
423 // Rust representation
424 //
425
426 // This test is non-portable, but works so long as Rust happens to lay this
427 // struct out with no padding.
428 #[derive(imp::IntoBytes)]
429 #[zerocopy(crate = "zerocopy_renamed")]
430 struct Unpacked {
431 a: u8,
432 b: u8,
433 }
434
435 util_assert_impl_all!(Unpacked: imp::IntoBytes);