Skip to content

Commit

Permalink
file: use a nightly API for DST initialization
Browse files Browse the repository at this point in the history
The `FileInfo`, `FileSystemInfo`, and `FileSystemVolumeLabel` structs
are all DSTs because they end with a `Char16` slice containing a name. A
wide pointer (or reference) to such a struct contains two parts: the
base memory address, and the number of elements in the slice.

Previously there was no officially-documented way to create such a wide
pointer, so uefi-rs used a trick that is used [elsewhere in the Rust
ecosystem][1]: create a slice matching the type and length of the slice
at the end of the struct, but passing in the desired base pointer
instead. Then the slice type is cast to match the desired struct type.

There is now a standard (albeit currently unstable) API for performing
this construction added by [RFC 2580][2]. This requires enabling the
`ptr_metadata` feature. The runtime result should be the same, but
without relying on undocumented Rust internals.

[1]: rust-lang/unsafe-code-guidelines#288 (comment)
[2]: https://rust-lang.github.io/rfcs/2580-ptr-meta.html
  • Loading branch information
nicholasbishop committed Feb 22, 2022
1 parent f53256e commit 224d3d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#![feature(try_trait_v2)]
#![feature(abi_efiapi)]
#![feature(negative_impls)]
#![feature(ptr_metadata)]
#![no_std]
// Enable some additional warnings and lints.
#![warn(missing_docs, unused)]
Expand Down
25 changes: 14 additions & 11 deletions src/proto/media/file/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use super::FileAttribute;
use crate::data_types::{chars::NUL_16, Align};
use crate::table::runtime::Time;
use crate::{unsafe_guid, CStr16, Char16, Identify};
use core::cmp;
use core::convert::TryInto;
use core::ffi::c_void;
use core::mem;
use core::slice;
use core::{cmp, mem, ptr};

/// Common trait for data structures that can be used with
/// `File::set_info()` or `File::get_info()`.
Expand Down Expand Up @@ -89,11 +87,17 @@ impl<Header> NamedFileProtocolInfo<Header> {
// Drop implementation. Thus, we are now ready to build a correctly
// sized &mut Self and go back to the realm of safe code.
debug_assert!(!mem::needs_drop::<Char16>());
let info_ptr = unsafe {
slice::from_raw_parts_mut(storage.as_mut_ptr() as *mut Char16, name_length_ucs2)
as *mut [Char16] as *mut Self
let info = unsafe {
// A wide pointer to a dynamically-sized struct ending with
// a slice contains a pointer to the start of the memory,
// followed by the number of elements in the slice.
//
// For more details of wide pointers, see
// https://doc.rust-lang.org/nightly/core/ptr/trait.Pointee.html
let fat_ptr =
ptr::from_raw_parts_mut::<Self>(storage.as_mut_ptr() as *mut (), name_length_ucs2);
&mut *fat_ptr
};
let info = unsafe { &mut *info_ptr };
debug_assert_eq!(info.name.len(), name_length_ucs2);

// Write down the UCS-2 name before returning the storage reference
Expand All @@ -119,10 +123,9 @@ impl<Header> FromUefi for NamedFileProtocolInfo<Header> {
let byte_ptr = ptr as *mut u8;
let name_ptr = byte_ptr.add(mem::size_of::<Header>()) as *mut Char16;
let name = CStr16::from_ptr(name_ptr);
let name_len = name.to_u16_slice_with_nul().len();
let fat_ptr = slice::from_raw_parts_mut(ptr as *mut Char16, name_len);
let self_ptr = fat_ptr as *mut [Char16] as *mut Self;
&mut *self_ptr
let name_len_chars = name.as_slice().len();
let fat_ptr = ptr::from_raw_parts_mut::<Self>(ptr as *mut (), name_len_chars);
&mut *fat_ptr
}
}

Expand Down

0 comments on commit 224d3d7

Please sign in to comment.