Skip to content

Commit 17952d5

Browse files
committed
Fixing the mapping memory allignment
1 parent 78d6ad0 commit 17952d5

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

crates/libmwemu/src/emu/loaders.rs

Lines changed: 10 additions & 2 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.
@@ -260,11 +267,12 @@ impl Emu {
260267
log::info!("base: 0x{:x}", base);
261268
}
262269

270+
let sec_allign = pe64.opt.section_alignment;
263271
// 4. map pe and then sections
264272
let pemap = match self.maps.create_map(
265273
&format!("{}.pe", filename2),
266274
base,
267-
pe64.opt.size_of_headers.into(),
275+
align_up!(pe64.opt.size_of_headers, sec_allign) as u64,
268276
Permission::READ_WRITE,
269277
) {
270278
Ok(m) => m,
@@ -308,7 +316,7 @@ impl Emu {
308316
let map = match self.maps.create_map(
309317
&format!("{}{}", filename2, sect_name),
310318
base + sect.virtual_address as u64,
311-
sz,
319+
align_up!(sz, sec_allign as u64),
312320
permission,
313321
) {
314322
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)