-
Notifications
You must be signed in to change notification settings - Fork 13
Add PE support for x86_64 #13
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19cbea0
Expand UnwindRegs to all gp registers.
afranchuk 73d0937
Add support for PE unwinding for x86_64.
afranchuk 90a1eaa
Add support for caching stack alloc + register pops.
afranchuk 84e0bd0
Remove memory allocation from PE unwind rules.
afranchuk b9c4df3
Merge branch 'main' into pe-support
mstange File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use super::arch::ArchAarch64; | ||
use crate::pe::{PeSections, PeUnwinderError, PeUnwinding}; | ||
use crate::unwind_result::UnwindResult; | ||
|
||
impl PeUnwinding for ArchAarch64 { | ||
fn unwind_frame<F, D>( | ||
_sections: PeSections<D>, | ||
_address: u32, | ||
_regs: &mut Self::UnwindRegs, | ||
_read_stack: &mut F, | ||
) -> Result<UnwindResult<Self::UnwindRule>, PeUnwinderError> | ||
where | ||
F: FnMut(u64) -> Result<u64, ()>, | ||
D: std::ops::Deref<Target = [u8]>, | ||
{ | ||
Err(PeUnwinderError::Aarch64Unsupported) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::{arch::Arch, unwind_result::UnwindResult}; | ||
use std::ops::Range; | ||
|
||
#[derive(thiserror::Error, Clone, Copy, Debug, PartialEq, Eq)] | ||
pub enum PeUnwinderError { | ||
#[error("failed to read unwind info memory at RVA {0:x}")] | ||
MissingUnwindInfoData(u32), | ||
#[error("failed to read instruction memory at RVA {0:x}")] | ||
MissingInstructionData(u32), | ||
#[error("failed to read stack{}", .0.map(|a| format!(" at address {a:x}")).unwrap_or_default())] | ||
MissingStackData(Option<u64>), | ||
#[error("failed to parse UnwindInfo")] | ||
UnwindInfoParseError, | ||
#[error("AArch64 is not yet supported")] | ||
Aarch64Unsupported, | ||
} | ||
|
||
pub struct PeSections<'a, D> { | ||
pub pdata: &'a D, | ||
pub rdata: Option<&'a (Range<u32>, D)>, | ||
pub xdata: Option<&'a (Range<u32>, D)>, | ||
pub text: Option<&'a (Range<u32>, D)>, | ||
} | ||
|
||
impl<'a, D> PeSections<'a, D> | ||
where | ||
D: std::ops::Deref<Target = [u8]>, | ||
{ | ||
pub fn unwind_info_memory_at_rva(&self, rva: u32) -> Result<&'a [u8], PeUnwinderError> { | ||
[&self.rdata, &self.xdata] | ||
.into_iter() | ||
.find_map(|o| o.and_then(|m| memory_at_rva(m, rva))) | ||
.ok_or(PeUnwinderError::MissingUnwindInfoData(rva)) | ||
} | ||
|
||
pub fn text_memory_at_rva(&self, rva: u32) -> Result<&'a [u8], PeUnwinderError> { | ||
self.text | ||
.and_then(|m| memory_at_rva(m, rva)) | ||
.ok_or(PeUnwinderError::MissingInstructionData(rva)) | ||
} | ||
} | ||
|
||
fn memory_at_rva<D: std::ops::Deref<Target = [u8]>>( | ||
(range, data): &(Range<u32>, D), | ||
address: u32, | ||
) -> Option<&[u8]> { | ||
if range.contains(&address) { | ||
let offset = address - range.start; | ||
Some(&data[(offset as usize)..]) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub trait PeUnwinding: Arch { | ||
fn unwind_frame<F, D>( | ||
sections: PeSections<D>, | ||
address: u32, | ||
regs: &mut Self::UnwindRegs, | ||
read_stack: &mut F, | ||
) -> Result<UnwindResult<Self::UnwindRule>, PeUnwinderError> | ||
where | ||
F: FnMut(u64) -> Result<u64, ()>, | ||
D: std::ops::Deref<Target = [u8]>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use super::{arch::ArchX86_64, unwind_rule::UnwindRuleX86_64, unwindregs::Reg}; | ||
use crate::arch::Arch; | ||
use crate::pe::{PeSections, PeUnwinderError, PeUnwinding}; | ||
use crate::unwind_result::UnwindResult; | ||
use std::ops::ControlFlow; | ||
|
||
use pe_unwind_info::x86_64::{ | ||
FunctionEpilogInstruction, FunctionEpilogParser, FunctionTableEntries, Register, UnwindInfo, | ||
UnwindInfoTrailer, UnwindState, | ||
}; | ||
|
||
struct State<'a, F> { | ||
regs: &'a mut <ArchX86_64 as Arch>::UnwindRegs, | ||
read_stack: &'a mut F, | ||
} | ||
|
||
impl<F> UnwindState for State<'_, F> | ||
where | ||
F: FnMut(u64) -> Result<u64, ()>, | ||
{ | ||
fn read_register(&mut self, register: Register) -> u64 { | ||
self.regs.get(convert_pe_register(register)) | ||
} | ||
|
||
fn read_stack(&mut self, addr: u64) -> Option<u64> { | ||
(self.read_stack)(addr).ok() | ||
} | ||
|
||
fn write_register(&mut self, register: Register, value: u64) { | ||
self.regs.set(convert_pe_register(register), value) | ||
} | ||
|
||
fn write_xmm_register(&mut self, _register: pe_unwind_info::x86_64::XmmRegister, _value: u128) { | ||
// Ignore | ||
} | ||
} | ||
|
||
fn convert_pe_register(r: Register) -> Reg { | ||
match r { | ||
Register::RAX => Reg::RAX, | ||
Register::RCX => Reg::RCX, | ||
Register::RDX => Reg::RDX, | ||
Register::RBX => Reg::RBX, | ||
Register::RSP => Reg::RSP, | ||
Register::RBP => Reg::RBP, | ||
Register::RSI => Reg::RSI, | ||
Register::RDI => Reg::RDI, | ||
Register::R8 => Reg::R8, | ||
Register::R9 => Reg::R9, | ||
Register::R10 => Reg::R10, | ||
Register::R11 => Reg::R11, | ||
Register::R12 => Reg::R12, | ||
Register::R13 => Reg::R13, | ||
Register::R14 => Reg::R14, | ||
Register::R15 => Reg::R15, | ||
} | ||
} | ||
|
||
impl PeUnwinding for ArchX86_64 { | ||
fn unwind_frame<F, D>( | ||
sections: PeSections<D>, | ||
address: u32, | ||
regs: &mut Self::UnwindRegs, | ||
read_stack: &mut F, | ||
) -> Result<UnwindResult<Self::UnwindRule>, PeUnwinderError> | ||
where | ||
F: FnMut(u64) -> Result<u64, ()>, | ||
D: std::ops::Deref<Target = [u8]>, | ||
{ | ||
let entries = FunctionTableEntries::parse(sections.pdata); | ||
let Some(mut function) = entries.lookup(address) else { | ||
return Ok(UnwindResult::ExecRule(UnwindRuleX86_64::JustReturn)); | ||
}; | ||
|
||
let read_stack_err = |read_stack: &mut F, addr| { | ||
read_stack(addr).map_err(|()| PeUnwinderError::MissingStackData(Some(addr))) | ||
}; | ||
|
||
let offset = address - function.begin_address.get(); | ||
let mut is_chained = false; | ||
loop { | ||
let unwind_info_address = function.unwind_info_address.get(); | ||
let unwind_info = | ||
UnwindInfo::parse(sections.unwind_info_memory_at_rva(unwind_info_address)?) | ||
.ok_or(PeUnwinderError::UnwindInfoParseError)?; | ||
|
||
if !is_chained { | ||
// Check whether the address is in the function epilog. If so, we need to | ||
// simulate the remaining epilog instructions (unwind codes don't account for | ||
// unwinding from the epilog). | ||
let bytes = (function.end_address.get() - address) as usize; | ||
let instruction = §ions.text_memory_at_rva(address)?[..bytes]; | ||
let mut epilog_parser = FunctionEpilogParser::new(); | ||
if let Some(epilog_instructions) = | ||
epilog_parser.is_function_epilog(instruction, unwind_info.frame_register()) | ||
{ | ||
for instruction in epilog_instructions.iter() { | ||
match instruction { | ||
FunctionEpilogInstruction::AddSP(offset) => { | ||
let rsp = regs.get(Reg::RSP); | ||
regs.set(Reg::RSP, rsp + *offset as u64); | ||
} | ||
FunctionEpilogInstruction::AddSPFromFP(offset) => { | ||
let fp = unwind_info | ||
.frame_register() | ||
.expect("invalid fp register offset"); | ||
let fp = convert_pe_register(fp); | ||
let fp = regs.get(fp); | ||
regs.set(Reg::RSP, fp + *offset as u64); | ||
} | ||
FunctionEpilogInstruction::Pop(reg) => { | ||
let rsp = regs.get(Reg::RSP); | ||
let val = read_stack_err(read_stack, rsp)?; | ||
regs.set(convert_pe_register(*reg), val); | ||
regs.set(Reg::RSP, rsp + 8); | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
let mut state = State { regs, read_stack }; | ||
for (_, op) in unwind_info | ||
.unwind_operations() | ||
.skip_while(|(o, _)| !is_chained && *o as u32 > offset) | ||
{ | ||
if let ControlFlow::Break(ra) = unwind_info | ||
.resolve_operation(&mut state, &op) | ||
.ok_or(PeUnwinderError::MissingStackData(None))? | ||
{ | ||
return Ok(UnwindResult::Uncacheable(ra)); | ||
} | ||
} | ||
if let Some(UnwindInfoTrailer::ChainedUnwindInfo { chained }) = unwind_info.trailer() { | ||
is_chained = true; | ||
function = chained; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
let rsp = regs.get(Reg::RSP); | ||
let ra = read_stack_err(read_stack, rsp)?; | ||
regs.set(Reg::RSP, rsp + 8); | ||
|
||
Ok(UnwindResult::Uncacheable(ra)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.