uefi-raw: replace ABI-related unit tests with consts checks

This is best practice in the ecosystem and ensures that the ABI is
correct for the target platform - which may be different from the
test platform.
This commit is contained in:
Philipp Schuster 2026-08-23 15:24:51 +02:00
parent 3afc565b35
commit 4a3d96ea33
No known key found for this signature in database
3 changed files with 31 additions and 31 deletions

View file

@ -86,6 +86,14 @@ pub type VirtualAddress = u64;
#[repr(transparent)]
pub struct Boolean(pub u8);
// Ensure ABI guarantees for Boolean, as promised in [0].
//
// [0]: https://github.com/tianocore/edk2/blob/b0f43dd3fdec2363e3548ec31eb455dc1c4ac761/MdePkg/Include/X64/ProcessorBind.h#L192
const _: () = {
assert!(align_of::<Boolean>() == 1);
assert!(size_of::<Boolean>() == 1);
};
impl Boolean {
/// [`Boolean`] representing `true`.
///
@ -169,12 +177,10 @@ mod tests {
use super::*;
#[test]
/// Test the properties promised in [0]. This also applies for the other
/// architectures.
/// Test the properties promised in [0] and convenient rusty conversions.
///
/// [0] https://github.com/tianocore/edk2/blob/b0f43dd3fdec2363e3548ec31eb455dc1c4ac761/MdePkg/Include/X64/ProcessorBind.h#L192
fn test_boolean_abi() {
assert_eq!(size_of::<Boolean>(), 1);
fn test_boolean_conversions() {
assert_eq!(Boolean::from(true).0, 1);
assert_eq!(Boolean::from(false).0, 0);
assert_eq!(Boolean::TRUE.0, 1);

View file

@ -130,6 +130,20 @@ pub union IpAddress {
pub v6: Ipv6Address,
}
// Ensure ABI guarantees for IpAddress.
const _: () = {
#[repr(C, packed)]
struct PackedHelper<T>(T);
assert!(align_of::<IpAddress>() == 4);
assert!(size_of::<IpAddress>() == 16);
// The type must be usable in a packed struct, even when it is normally
// 4 byte aligned.
assert!(align_of::<PackedHelper<IpAddress>>() == 1);
assert!(size_of::<PackedHelper<IpAddress>>() == 16);
};
impl IpAddress {
/// Zeroed variant where all bytes are guaranteed to be initialized to zero.
pub const ZERO: Self = Self { addr: [0; 4] };
@ -355,20 +369,6 @@ mod tests {
assert_eq!(efi_mac_addr.into_ethernet_addr(), ethernet_octets);
}
// Ensure that our IpAddress type can be put into a packed struct,
// even when it is normally 4 byte aligned.
#[test]
fn test_efi_ip_address_abi() {
#[repr(C, packed)]
struct PackedHelper<T>(T);
assert_eq!(align_of::<IpAddress>(), 4);
assert_eq!(size_of::<IpAddress>(), 16);
assert_eq!(align_of::<PackedHelper<IpAddress>>(), 1);
assert_eq!(size_of::<PackedHelper<IpAddress>>(), 16);
}
/// Tests the From-impls from the documentation.
#[test]
fn test_promised_from_impls() {

View file

@ -64,6 +64,13 @@ pub struct DevicePathProtocol {
// followed by payload (dynamically sized)
}
// Ensure ABI guarantees for DevicePathProtocol. The struct is naturally
// packed; thus, we don't need to explicitly specify `packed`.
const _: () = {
assert!(size_of::<DevicePathProtocol>() == 4);
assert!(align_of::<DevicePathProtocol>() == 1);
};
impl DevicePathProtocol {
pub const GUID: Guid = guid!("09576e91-6d3f-11d2-8e39-00a0c969723b");
@ -300,16 +307,3 @@ pub struct DevicePathUtilitiesProtocol {
impl DevicePathUtilitiesProtocol {
pub const GUID: Guid = guid!("0379be4e-d706-437d-b037-edb82fb772a4");
}
#[cfg(test)]
mod tests {
use super::*;
/// Test that ensures the struct is packed. Thus, we don't need to
/// explicitly specify `packed`.
#[test]
fn abi() {
assert_eq!(size_of::<DevicePathProtocol>(), 4);
assert_eq!(align_of::<DevicePathProtocol>(), 1);
}
}