Skip to content

Commit 61af010

Browse files
authored
Rollup merge of rust-lang#127792 - workingjubilee:read-unaligned-is-dwarfier, r=joboet
std: Use `read_unaligned` for reads from DWARF There's a lot of... *stuff* going on here. Meanwhile, `read_unaligned` has been available since 1.17.0, so let's just use that.
2 parents d3cf2e1 + e8527cd commit 61af010

File tree

1 file changed

+12
-14
lines changed
  • std/src/sys/personality/dwarf

1 file changed

+12
-14
lines changed

std/src/sys/personality/dwarf/mod.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,30 @@ pub struct DwarfReader {
1717
pub ptr: *const u8,
1818
}
1919

20-
#[repr(C, packed)]
21-
struct Unaligned<T>(T);
22-
20+
#[forbid(unsafe_op_in_unsafe_fn)]
2321
impl DwarfReader {
2422
pub fn new(ptr: *const u8) -> DwarfReader {
2523
DwarfReader { ptr }
2624
}
2725

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

38-
// ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable
39-
// Length Data".
37+
/// ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable Length Data".
4038
pub unsafe fn read_uleb128(&mut self) -> u64 {
4139
let mut shift: usize = 0;
4240
let mut result: u64 = 0;
4341
let mut byte: u8;
4442
loop {
45-
byte = self.read::<u8>();
43+
byte = unsafe { self.read::<u8>() };
4644
result |= ((byte & 0x7F) as u64) << shift;
4745
shift += 7;
4846
if byte & 0x80 == 0 {
@@ -57,7 +55,7 @@ impl DwarfReader {
5755
let mut result: u64 = 0;
5856
let mut byte: u8;
5957
loop {
60-
byte = self.read::<u8>();
58+
byte = unsafe { self.read::<u8>() };
6159
result |= ((byte & 0x7F) as u64) << shift;
6260
shift += 7;
6361
if byte & 0x80 == 0 {

0 commit comments

Comments
 (0)