Skip to content

Commit 5295297

Browse files
authored
Merge pull request #139 from acheron2302/main
Fixing the memory alignment when mapping pe
2 parents e8344b7 + baaa0ca commit 5295297

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

crates/libmwemu/src/emu/loaders.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ use crate::pe::pe32::PE32;
99
use crate::pe::pe64::PE64;
1010
use crate::peb::{peb32, peb64};
1111

12+
macro_rules! align_up {
13+
($size:expr, $align:expr) => {{
14+
// Ensure alignment is a power of two at compile time if possible
15+
($size + $align - 1) & !($align - 1)
16+
}};
17+
}
18+
1219
impl Emu {
1320
/// Complex funtion called from many places and with multiple purposes.
1421
/// This is called from load_code() if sample is PE32, but also from load_library etc.
@@ -99,13 +106,14 @@ impl Emu {
99106
log::info!("base: 0x{:x}", base);
100107
}
101108

109+
let sec_allign = pe32.opt.section_alignment;
102110
// 4. map pe and then sections
103111
let pemap = self
104112
.maps
105113
.create_map(
106114
&format!("{}.pe", filename2),
107115
base.into(),
108-
pe32.opt.size_of_headers.into(),
116+
align_up!(pe32.opt.size_of_headers, sec_allign) as u64,
109117
Permission::READ_WRITE,
110118
)
111119
.expect("cannot create pe map");
@@ -145,7 +153,7 @@ impl Emu {
145153
let map = match self.maps.create_map(
146154
&format!("{}{}", filename2, sect_name),
147155
base as u64 + sect.virtual_address as u64,
148-
sz,
156+
align_up!(sz, sec_allign as u64),
149157
permission,
150158
) {
151159
Ok(m) => m,
@@ -260,11 +268,12 @@ impl Emu {
260268
log::info!("base: 0x{:x}", base);
261269
}
262270

271+
let sec_allign = pe64.opt.section_alignment;
263272
// 4. map pe and then sections
264273
let pemap = match self.maps.create_map(
265274
&format!("{}.pe", filename2),
266275
base,
267-
pe64.opt.size_of_headers.into(),
276+
align_up!(pe64.opt.size_of_headers, sec_allign) as u64,
268277
Permission::READ_WRITE,
269278
) {
270279
Ok(m) => m,
@@ -308,7 +317,7 @@ impl Emu {
308317
let map = match self.maps.create_map(
309318
&format!("{}{}", filename2, sect_name),
310319
base + sect.virtual_address as u64,
311-
sz,
320+
align_up!(sz, sec_allign as u64),
312321
permission,
313322
) {
314323
Ok(m) => m,

crates/libmwemu/src/engine/instructions/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn execute(emu: &mut Emu, ins: &Instruction, instruction_sz: usize, _rep_ste
3535
emu.stack_lvl.push(0);
3636
emu.stack_lvl_idx += 1;
3737
}*/
38-
38+
3939
let rip = emu.regs().rip;
4040
emu.call_stack_mut().push((rip, addr));
4141

crates/libmwemu/src/engine/instructions/mov.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub fn execute(emu: &mut Emu, ins: &Instruction, instruction_sz: usize, _rep_ste
1616
emu.show_instruction_comment(color!("LightCyan"), ins, &format!("0x{:x}", value1));
1717
}
1818

19-
2019
/*
2120
if emu.pos == 189464541 {
2221
let addr = emu.get_operand_value(ins, 1, false).unwrap();

0 commit comments

Comments
 (0)