| 1 |
// Copyright 2023 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 is `imp::TryFromBytes` if: |
| 16 |
// - all fields are `imp::TryFromBytes` |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn zst() { |
| 20 |
// FIXME(#5): Use `try_transmute` in this test once it's available. |
| 21 |
let candidate = ::zerocopy::Ptr::from_ref(&()); |
| 22 |
let candidate = candidate.forget_aligned(); |
| 23 |
// SAFETY: `&()` trivially consists entirely of initialized bytes. |
| 24 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 25 |
let is_bit_valid = <() as imp::TryFromBytes>::is_bit_valid(candidate); |
| 26 |
imp::assert!(is_bit_valid); |
| 27 |
} |
| 28 |
|
| 29 |
#[derive(imp::TryFromBytes)] |
| 30 |
#[repr(C)] |
| 31 |
struct One { |
| 32 |
a: u8, |
| 33 |
} |
| 34 |
|
| 35 |
util_assert_impl_all!(One: imp::TryFromBytes); |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn one() { |
| 39 |
// FIXME(#5): Use `try_transmute` in this test once it's available. |
| 40 |
let candidate = ::zerocopy::Ptr::from_ref(&One { a: 42 }); |
| 41 |
let candidate = candidate.forget_aligned(); |
| 42 |
// SAFETY: `&One` consists entirely of initialized bytes. |
| 43 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 44 |
let is_bit_valid = <One as imp::TryFromBytes>::is_bit_valid(candidate); |
| 45 |
imp::assert!(is_bit_valid); |
| 46 |
} |
| 47 |
|
| 48 |
#[derive(imp::TryFromBytes)] |
| 49 |
#[repr(C)] |
| 50 |
struct Two { |
| 51 |
a: bool, |
| 52 |
b: (), |
| 53 |
} |
| 54 |
|
| 55 |
util_assert_impl_all!(Two: imp::TryFromBytes); |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn two() { |
| 59 |
// FIXME(#5): Use `try_transmute` in this test once it's available. |
| 60 |
let candidate = ::zerocopy::Ptr::from_ref(&Two { a: false, b: () }); |
| 61 |
let candidate = candidate.forget_aligned(); |
| 62 |
// SAFETY: `&Two` consists entirely of initialized bytes. |
| 63 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 64 |
let is_bit_valid = <Two as imp::TryFromBytes>::is_bit_valid(candidate); |
| 65 |
imp::assert!(is_bit_valid); |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn two_bad() { |
| 70 |
// FIXME(#5): Use `try_transmute` in this test once it's available. |
| 71 |
let candidate = ::zerocopy::Ptr::from_ref(&[2u8][..]); |
| 72 |
let candidate = candidate.forget_aligned(); |
| 73 |
// SAFETY: `&Two` consists entirely of initialized bytes. |
| 74 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 75 |
|
| 76 |
// SAFETY: |
| 77 |
// - The cast preserves address and size. As a result, the cast will address |
| 78 |
// the same bytes as `c`. |
| 79 |
// - The cast preserves provenance. |
| 80 |
// - Neither the input nor output types contain any `UnsafeCell`s. |
| 81 |
let candidate = unsafe { candidate.cast_unsized_unchecked(|p| p.cast::<Two>()) }; |
| 82 |
|
| 83 |
// SAFETY: `candidate`'s referent is as-initialized as `Two`. |
| 84 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 85 |
|
| 86 |
let is_bit_valid = <Two as imp::TryFromBytes>::is_bit_valid(candidate); |
| 87 |
imp::assert!(!is_bit_valid); |
| 88 |
} |
| 89 |
|
| 90 |
#[derive(imp::TryFromBytes)] |
| 91 |
#[repr(C)] |
| 92 |
struct Unsized { |
| 93 |
a: [u8], |
| 94 |
} |
| 95 |
|
| 96 |
util_assert_impl_all!(Unsized: imp::TryFromBytes); |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn un_sized() { |
| 100 |
// FIXME(#5): Use `try_transmute` in this test once it's available. |
| 101 |
let candidate = ::zerocopy::Ptr::from_ref(&[16, 12, 42][..]); |
| 102 |
let candidate = candidate.forget_aligned(); |
| 103 |
// SAFETY: `&Unsized` consists entirely of initialized bytes. |
| 104 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 105 |
|
| 106 |
// SAFETY: |
| 107 |
// - The cast preserves address and size. As a result, the cast will address |
| 108 |
// the same bytes as `c`. |
| 109 |
// - The cast preserves provenance. |
| 110 |
// - Neither the input nor output types contain any `UnsafeCell`s. |
| 111 |
let candidate = unsafe { |
| 112 |
candidate.cast_unsized_unchecked(|p| { |
| 113 |
let ptr = |
| 114 |
imp::core::ptr::NonNull::new_unchecked(p.as_non_null().as_ptr() as *mut Unsized); |
| 115 |
::zerocopy::pointer::PtrInner::new(ptr) |
| 116 |
}) |
| 117 |
}; |
| 118 |
|
| 119 |
// SAFETY: `candidate`'s referent is as-initialized as `Two`. |
| 120 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 121 |
let is_bit_valid = <Unsized as imp::TryFromBytes>::is_bit_valid(candidate); |
| 122 |
imp::assert!(is_bit_valid); |
| 123 |
} |
| 124 |
|
| 125 |
#[derive(imp::TryFromBytes)] |
| 126 |
#[repr(C)] |
| 127 |
struct TypeParams<'a, T: ?imp::Sized, I: imp::Iterator> { |
| 128 |
a: I::Item, |
| 129 |
b: u8, |
| 130 |
c: imp::PhantomData<&'a [u8]>, |
| 131 |
d: imp::PhantomData<&'static str>, |
| 132 |
e: imp::PhantomData<imp::String>, |
| 133 |
f: T, |
| 134 |
} |
| 135 |
|
| 136 |
util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::TryFromBytes); |
| 137 |
util_assert_impl_all!(TypeParams<'static, util::AU16, imp::IntoIter<()>>: imp::TryFromBytes); |
| 138 |
util_assert_impl_all!(TypeParams<'static, [util::AU16], imp::IntoIter<()>>: imp::TryFromBytes); |
| 139 |
|
| 140 |
// Deriving `imp::TryFromBytes` should work if the struct has bounded parameters. |
| 141 |
|
| 142 |
#[derive(imp::TryFromBytes)] |
| 143 |
#[repr(transparent)] |
| 144 |
struct WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::TryFromBytes, const N: usize>( |
| 145 |
imp::PhantomData<&'a &'b ()>, |
| 146 |
[T], |
| 147 |
) |
| 148 |
where |
| 149 |
'a: 'b, |
| 150 |
'b: 'a, |
| 151 |
T: 'a + 'b + imp::TryFromBytes; |
| 152 |
|
| 153 |
util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::TryFromBytes); |
| 154 |
|
| 155 |
#[derive(imp::FromBytes)] |
| 156 |
#[repr(C)] |
| 157 |
struct MaybeFromBytes<T>(T); |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn test_maybe_from_bytes() { |
| 161 |
// When deriving `FromBytes` on a type with no generic parameters, we emit a |
| 162 |
// trivial `is_bit_valid` impl that always returns true. This test confirms |
| 163 |
// that we *don't* spuriously do that when generic parameters are present. |
| 164 |
|
| 165 |
let candidate = ::zerocopy::Ptr::from_ref(&[2u8][..]); |
| 166 |
let candidate = candidate.bikeshed_recall_initialized_from_bytes(); |
| 167 |
|
| 168 |
// SAFETY: |
| 169 |
// - The cast preserves address and size. As a result, the cast will address |
| 170 |
// the same bytes as `c`. |
| 171 |
// - The cast preserves provenance. |
| 172 |
// - Neither the input nor output types contain any `UnsafeCell`s. |
| 173 |
let candidate = |
| 174 |
unsafe { candidate.cast_unsized_unchecked(|p| p.cast::<MaybeFromBytes<bool>>()) }; |
| 175 |
|
| 176 |
// SAFETY: `[u8]` consists entirely of initialized bytes. |
| 177 |
let candidate = unsafe { candidate.assume_initialized() }; |
| 178 |
let is_bit_valid = <MaybeFromBytes<bool> as imp::TryFromBytes>::is_bit_valid(candidate); |
| 179 |
imp::assert!(!is_bit_valid); |
| 180 |
} |
| 181 |
|
| 182 |
#[derive(Debug, PartialEq, Eq, imp::TryFromBytes, imp::Immutable, imp::KnownLayout)] |
| 183 |
#[repr(C, packed)] |
| 184 |
struct CPacked { |
| 185 |
a: u8, |
| 186 |
// NOTE: The `u32` type is not guaranteed to have alignment 4, although it |
| 187 |
// does on many platforms. However, to fix this would require a custom type |
| 188 |
// with a `#[repr(align(4))]` attribute, and `#[repr(packed)]` types are not |
| 189 |
// allowed to transitively contain `#[repr(align(...))]` types. Thus, we |
| 190 |
// have no choice but to use `u32` here. Luckily, these tests run in CI on |
| 191 |
// platforms on which `u32` has alignment 4, so this isn't that big of a |
| 192 |
// deal. |
| 193 |
b: u32, |
| 194 |
} |
| 195 |
|
| 196 |
#[test] |
| 197 |
fn c_packed() { |
| 198 |
let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF]; |
| 199 |
let converted = <CPacked as imp::TryFromBytes>::try_ref_from_bytes(candidate); |
| 200 |
imp::assert_eq!(converted, imp::Ok(&CPacked { a: 42, b: u32::MAX })); |
| 201 |
} |
| 202 |
|
| 203 |
#[derive(imp::TryFromBytes, imp::KnownLayout, imp::Immutable)] |
| 204 |
#[repr(C, packed)] |
| 205 |
struct CPackedUnsized { |
| 206 |
a: u8, |
| 207 |
// NOTE: The `u32` type is not guaranteed to have alignment 4, although it |
| 208 |
// does on many platforms. However, to fix this would require a custom type |
| 209 |
// with a `#[repr(align(4))]` attribute, and `#[repr(packed)]` types are not |
| 210 |
// allowed to transitively contain `#[repr(align(...))]` types. Thus, we |
| 211 |
// have no choice but to use `u32` here. Luckily, these tests run in CI on |
| 212 |
// platforms on which `u32` has alignment 4, so this isn't that big of a |
| 213 |
// deal. |
| 214 |
b: [u32], |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn c_packed_unsized() { |
| 219 |
let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF]; |
| 220 |
let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate); |
| 221 |
imp::assert!(converted.is_ok()); |
| 222 |
} |
| 223 |
|
| 224 |
#[derive(imp::TryFromBytes)] |
| 225 |
#[repr(packed)] |
| 226 |
struct PackedUnsized { |
| 227 |
a: u8, |
| 228 |
// NOTE: The `u32` type is not guaranteed to have alignment 4, although it |
| 229 |
// does on many platforms. However, to fix this would require a custom type |
| 230 |
// with a `#[repr(align(4))]` attribute, and `#[repr(packed)]` types are not |
| 231 |
// allowed to transitively contain `#[repr(align(...))]` types. Thus, we |
| 232 |
// have no choice but to use `u32` here. Luckily, these tests run in CI on |
| 233 |
// platforms on which `u32` has alignment 4, so this isn't that big of a |
| 234 |
// deal. |
| 235 |
b: [u32], |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn packed_unsized() { |
| 240 |
let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF]; |
| 241 |
let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate); |
| 242 |
imp::assert!(converted.is_ok()); |
| 243 |
|
| 244 |
let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]; |
| 245 |
let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate); |
| 246 |
imp::assert!(converted.is_err()); |
| 247 |
|
| 248 |
let candidate = &[42u8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]; |
| 249 |
let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate); |
| 250 |
imp::assert!(converted.is_ok()); |
| 251 |
} |
| 252 |
|
| 253 |
#[derive(imp::TryFromBytes)] |
| 254 |
struct A; |
| 255 |
|
| 256 |
#[derive(imp::TryFromBytes)] |
| 257 |
struct B { |
| 258 |
a: A, |
| 259 |
} |