Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: Use read_unaligned for reads from DWARF #127792

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions library/std/src/sys/personality/dwarf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,30 @@ pub struct DwarfReader {
pub ptr: *const u8,
}

#[repr(C, packed)]
struct Unaligned<T>(T);

#[deny(unsafe_op_in_unsafe_fn)]
workingjubilee marked this conversation as resolved.
Show resolved Hide resolved
impl DwarfReader {
pub fn new(ptr: *const u8) -> DwarfReader {
DwarfReader { ptr }
}

// DWARF streams are packed, so e.g., a u32 would not necessarily be aligned
// on a 4-byte boundary. This may cause problems on platforms with strict
// alignment requirements. By wrapping data in a "packed" struct, we are
// telling the backend to generate "misalignment-safe" code.
/// Read a type T and then bump the pointer by that amount.
///
/// DWARF streams are "packed", so all types must be read at align 1.
pub unsafe fn read<T: Copy>(&mut self) -> T {
let Unaligned(result) = *(self.ptr as *const Unaligned<T>);
self.ptr = self.ptr.add(mem::size_of::<T>());
result
unsafe {
let result = self.ptr.cast::<T>().read_unaligned();
self.ptr = self.ptr.byte_add(mem::size_of::<T>());
result
}
}

// ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable
// Length Data".
/// ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable Length Data".
pub unsafe fn read_uleb128(&mut self) -> u64 {
let mut shift: usize = 0;
let mut result: u64 = 0;
let mut byte: u8;
loop {
byte = self.read::<u8>();
byte = unsafe { self.read::<u8>() };
result |= ((byte & 0x7F) as u64) << shift;
shift += 7;
if byte & 0x80 == 0 {
Expand All @@ -57,7 +55,7 @@ impl DwarfReader {
let mut result: u64 = 0;
let mut byte: u8;
loop {
byte = self.read::<u8>();
byte = unsafe { self.read::<u8>() };
result |= ((byte & 0x7F) as u64) << shift;
shift += 7;
if byte & 0x80 == 0 {
Expand Down
Loading