Skip to content

Commit

Permalink
fix: qualify zerocopy to avoid duplicate import errror
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 authored and afranchuk committed Aug 26, 2024
1 parent 82a171d commit 7825141
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
use arrayvec::ArrayVec;
use std::ops::ControlFlow;
use thiserror::Error;
use zerocopy::{FromBytes, Ref, Unaligned, LE};
use zerocopy_derive::{FromBytes, FromZeroes, Unaligned};

type U16 = zerocopy::U16<LE>;
type U16 = zerocopy::U16<zerocopy::LE>;

/// Little-endian u32.
pub type U32 = zerocopy::U32<LE>;
pub type U32 = zerocopy::U32<zerocopy::LE>;

/// A view over function table entries in the `.pdata` section.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -46,7 +45,7 @@ impl<'a> FunctionTableEntries<'a> {
/// Get the `RuntimeFunction`s in the function table, if the parsed data is well-aligned and
/// sized.
pub fn functions(&self) -> Option<&'a [RuntimeFunction]> {
Ref::new_slice_unaligned(self.data).map(|lv| lv.into_slice())
zerocopy::Ref::new_slice_unaligned(self.data).map(|lv| lv.into_slice())
}

/// Lookup the runtime function that contains the given relative virtual address.
Expand Down Expand Up @@ -160,7 +159,7 @@ impl<'a> Iterator for FunctionTableEntries<'a> {
type Item = &'a RuntimeFunction;

fn next(&mut self) -> Option<Self::Item> {
let (rf, rest) = Ref::<_, RuntimeFunction>::new_unaligned_from_prefix(self.data)?;
let (rf, rest) = zerocopy::Ref::<_, RuntimeFunction>::new_unaligned_from_prefix(self.data)?;
self.data = rest;
Some(rf.into_ref())
}
Expand All @@ -174,13 +173,15 @@ struct Sections<'a> {

impl<'a> Sections<'a> {
pub fn parse(image: &'a [u8]) -> Option<Self> {
let sig_offset = Ref::<_, U32>::new_unaligned(image.get(0x3c..0x40)?)?.get() as usize;
let sig_offset =
zerocopy::Ref::<_, U32>::new_unaligned(image.get(0x3c..0x40)?)?.get() as usize;
// Offset to the COFF header
let coff_image = image.get(sig_offset + 4..)?;
let section_count = Ref::<_, U16>::new_unaligned(coff_image.get(2..4)?)?.get() as usize;
let section_count =
zerocopy::Ref::<_, U16>::new_unaligned(coff_image.get(2..4)?)?.get() as usize;
let size_of_optional_header =
Ref::<_, U16>::new_unaligned(coff_image.get(16..18)?)?.get() as usize;
let sections = Ref::<_, [Section]>::new_slice_unaligned_from_prefix(
zerocopy::Ref::<_, U16>::new_unaligned(coff_image.get(16..18)?)?.get() as usize;
let sections = zerocopy::Ref::<_, [Section]>::new_slice_unaligned_from_prefix(
&coff_image[20 + size_of_optional_header..],
section_count,
)?
Expand Down Expand Up @@ -526,7 +527,7 @@ impl FunctionEpilogInstruction {
if allow_add_sp && ip.len() >= 3 {
// add RSP,imm32
if rex & 0x8 != 0 && ip[0] == 0x81 && ip[1] == 0xc4 {
let (val, rest) = Ref::<_, U32>::new_unaligned_from_prefix(&ip[2..])
let (val, rest) = zerocopy::Ref::<_, U32>::new_unaligned_from_prefix(&ip[2..])
.ok_or(InstructionParseError::NotEnoughData)?;
return Ok(Some((FunctionEpilogInstruction::AddSP(val.get()), rest)));
}
Expand All @@ -551,8 +552,9 @@ impl FunctionEpilogInstruction {
)));
// lea RSP,disp32[FP]
} else if op_mod == 0b10 {
let (val, rest) = Ref::<_, U32>::new_unaligned_from_prefix(&ip[2..])
.ok_or(InstructionParseError::NotEnoughData)?;
let (val, rest) =
zerocopy::Ref::<_, U32>::new_unaligned_from_prefix(&ip[2..])
.ok_or(InstructionParseError::NotEnoughData)?;
return Ok(Some((
FunctionEpilogInstruction::AddSPFromFP(val.get()),
rest,
Expand Down Expand Up @@ -707,12 +709,14 @@ impl<'a> UnwindInfo<'a> {
///
/// Returns None if there aren't enough bytes or the alignment is incorrect.
pub fn parse(data: &'a [u8]) -> Option<Self> {
let (header, rest) = Ref::<_, UnwindInfoHeader>::new_unaligned_from_prefix(data)?;
let (header, rest) = zerocopy::Ref::<_, UnwindInfoHeader>::new_unaligned_from_prefix(data)?;
if header.version() != 1 {
return None;
}
let (unwind_codes, rest) =
Ref::new_slice_unaligned_from_prefix(rest, header.unwind_codes_len as usize * 2)?;
let (unwind_codes, rest) = zerocopy::Ref::new_slice_unaligned_from_prefix(
rest,
header.unwind_codes_len as usize * 2,
)?;
Some(UnwindInfo {
header: header.into_ref(),
unwind_codes: unwind_codes.into_slice(),
Expand All @@ -730,20 +734,21 @@ impl<'a> UnwindInfo<'a> {
let flags = self.flags();
if flags.contains(UnwindInfoFlags::EHANDLER) {
let (handler_address, handler_data) =
Ref::<_, U32>::new_unaligned_from_prefix(self.rest)?;
zerocopy::Ref::<_, U32>::new_unaligned_from_prefix(self.rest)?;
Some(UnwindInfoTrailer::ExceptionHandler {
handler_address: handler_address.into_ref(),
handler_data,
})
} else if flags.contains(UnwindInfoFlags::UHANDLER) {
let (handler_address, handler_data) =
Ref::<_, U32>::new_unaligned_from_prefix(self.rest)?;
zerocopy::Ref::<_, U32>::new_unaligned_from_prefix(self.rest)?;
Some(UnwindInfoTrailer::TerminationHandler {
handler_address: handler_address.into_ref(),
handler_data,
})
} else if flags.contains(UnwindInfoFlags::CHAININFO) {
let (chained, _) = Ref::<_, RuntimeFunction>::new_unaligned_from_prefix(self.rest)?;
let (chained, _) =
zerocopy::Ref::<_, RuntimeFunction>::new_unaligned_from_prefix(self.rest)?;
Some(UnwindInfoTrailer::ChainedUnwindInfo {
chained: chained.into_ref(),
})
Expand Down Expand Up @@ -775,8 +780,8 @@ impl<'a> UnwindOperations<'a> {
c.read::<UnwindCode>()
}

fn read<T: Unaligned + FromBytes>(&mut self) -> Option<&'a T> {
let (v, rest) = Ref::<_, T>::new_unaligned_from_prefix(self.0)?;
fn read<T: zerocopy::Unaligned + zerocopy::FromBytes>(&mut self) -> Option<&'a T> {
let (v, rest) = zerocopy::Ref::<_, T>::new_unaligned_from_prefix(self.0)?;
self.0 = rest;
Some(v.into_ref())
}
Expand Down

0 comments on commit 7825141

Please sign in to comment.