Skip to content

Commit ab4d702

Browse files
authored
Merge pull request #140 from acheron2302/main
Adding heap management for allocation inside heap
2 parents bc0c6c9 + 9bbb417 commit ab4d702

File tree

19 files changed

+628
-44
lines changed

19 files changed

+628
-44
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ jobs:
8888
- name: Run coverage
8989
if: runner.os == 'Linux'
9090
run: |
91-
cd crates/libmwemu
92-
cargo tarpaulin --output-dir ../../ --out Xml --timeout 200
91+
cargo tarpaulin --no-default-features --out Xml --timeout 200
92+
9393
9494
- name: Upload coverage reports to Codecov
9595
if: runner.os == 'Linux'

Cargo.lock

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[workspace]
22
resolver = "2"
33
members = [
4-
# "crates/pymwemu",
4+
"crates/pymwemu",
55
"crates/libmwemu",
66
"crates/mwemu",
77
]

crates/libmwemu/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bytemuck = "1.23.1"
3535
minidump = "0.26.0"
3636
serde_yaml = "0.9"
3737
fast_log = { version = "1.7" }
38+
nohash-hasher = "0.2.0"
3839

3940
[dev-dependencies]
4041
env_logger = "0.11.8"

crates/libmwemu/src/emu/initialization.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
use crate::{get_bit, kuser_shared, set_bit, structures, winapi::winapi32, winapi::winapi64};
2121

2222
use fast_log::appender::{Command, FastLogRecord, RecordFormat};
23+
use crate::maps::heap_allocation::O1Heap;
2324

2425
pub struct CustomLogFormat;
2526
impl RecordFormat for CustomLogFormat {
@@ -94,6 +95,7 @@ impl Emu {
9495
definitions: HashMap::new(),
9596
stored_contexts: HashMap::new(),
9697
entropy: 0.0,
98+
heap_management: None,
9799
}
98100
}
99101

@@ -353,15 +355,18 @@ impl Emu {
353355
if dyn_link {
354356
//heap.set_base(0x555555579000);
355357
} else {
356-
let heap_sz = 0x4d8000 - 0x4b5000;
358+
// here we are allocating 4MB of heap memory
359+
let heap_sz = 0x885900 - 0x4b5000;
357360
self.heap_addr = self.maps.alloc(heap_sz).expect("cannot allocate heap");
358361
let heap = self
359362
.maps
360-
.create_map("heap", self.heap_addr, heap_sz, Permission::READ_WRITE) //.create_map("heap", 0x4b5b00, 0x4d8000 - 0x4b5000)
363+
.create_map(".heap", self.heap_addr, heap_sz, Permission::READ_WRITE) //.create_map("heap", 0x4b5b00, 0x4d8000 - 0x4b5000)
361364
.expect("cannot create heap map");
362365
heap.load("heap.bin");
363-
}
364366

367+
self.heap_management = Some(Box::new(O1Heap::new(self.heap_addr, heap_sz as u32).expect("Expect new heap_management but failed")));
368+
}
369+
365370
self.regs_mut().rbp = 0;
366371

367372
self.fs_mut().insert(0xffffffffffffffc8, 0); //0x4b6c50
@@ -521,5 +526,14 @@ impl Emu {
521526
let stack_size = 0x100000;
522527
teb.nt_tib.stack_limit = self.cfg.stack_addr + stack_size + 0x2000;
523528
teb.save(teb_map);
529+
530+
let heap_sz = 0x885900 - 0x4b5000;
531+
self.heap_addr = self.maps.alloc(heap_sz).expect("cannot allocate heap");
532+
let heap = self
533+
.maps
534+
.create_map(".heap", self.heap_addr, heap_sz, Permission::READ_WRITE)
535+
.expect("cannot create heap map");
536+
537+
self.heap_management = Some(Box::new(O1Heap::new(self.heap_addr, heap_sz as u32).expect("Expect new heap_management but failed")));
524538
}
525539
}

crates/libmwemu/src/emu/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::{
2222
structures::MemoryOperation,
2323
thread_context::ThreadContext,
2424
};
25+
use crate::maps::heap_allocation::O1Heap;
2526

2627
mod banzai;
2728
mod call_stack;
@@ -101,4 +102,5 @@ pub struct Emu {
101102
pub definitions: HashMap<u64, Definition>,
102103
pub stored_contexts: HashMap<String, StoredContext>,
103104
pub entropy: f64,
105+
pub heap_management: Option<Box<O1Heap>>,
104106
}

crates/libmwemu/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub mod winapi;
4444

4545
#[cfg(test)]
4646
mod tests;
47+
mod utils;
4748

4849
use config::Config;
4950
use emu::Emu;

0 commit comments

Comments
 (0)