Skip to content
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

add mmap manager to reuse unmapped memory (WIP) #42

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 128 additions & 9 deletions omo/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use anyhow::Result;

use crate::arch::ArchInfo;
use std::borrow::BorrowMut;

use crate::{engine::Machine, utils::Packer, PAGE_SIZE};
use anyhow::Result;
use log::info;
use unicorn_engine::{
unicorn_const::{uc_error, MemRegion, Permission},
Unicorn,
};

use crate::{
arch::ArchInfo,
engine::Machine,
errors::{from_raw_syscall_ret, EmulatorError},
utils::Packer,
PAGE_SIZE,
};

pub type PointerSizeT = u8;

#[derive(Debug)]
struct MapInfo {
info: MemRegion,
Expand Down Expand Up @@ -36,7 +44,14 @@ pub trait Memory {
}
fn mem_map(&mut self, region: MemRegion, info: Option<String>) -> Result<(), uc_error>;
fn mem_unmap(&mut self, addr: u64, size: usize) -> Result<(), uc_error>;
fn is_mapped(&self, addr: u64, size: usize) -> Result<bool, uc_error>;
// Query whether the memory range starting at `addr` and is of length of `size` bytes
// is fully mapped.
//
// Returns: True if the specified memory range is taken fully, False otherwise
fn is_mapped(&self, addr: u64, size: usize) -> bool;
// Choose mmap address by size.
// Start searching with mmap base address in config.
fn next_mmap_address(&self, base_address: u64, size: usize) -> Result<u64, EmulatorError>;
fn mprotect(&mut self, addr: u64, size: usize, perm: Permission) -> Result<(), uc_error>;
fn read(&self, addr: u64, size: usize) -> Result<Vec<u8>, uc_error>;
fn read_ptr(&self, address: u64, pointersize: Option<PointerSizeT>) -> Result<u64, uc_error>;
Expand Down Expand Up @@ -75,16 +90,120 @@ impl<'a, A> Memory for Unicorn<'a, Machine<A>> {
MemRegion { begin, end, perms },
info.unwrap_or_else(|| "[mapped]".to_string()),
);
log::debug!("mmapped: {:?}", self.get_data().memories.map_info);
Ok(())
}
fn mem_unmap(&mut self, addr: u64, size: usize) -> Result<(), uc_error> {
// TODO: manage map_info
let begin = addr;
let end = addr + size as u64;
let mut wait_rm: Vec<MemRegion> = Vec::new();
let mut wait_add: Vec<MapInfo> = Vec::new();

let mut unmap_begin = begin;

for mut i in &self.get_data_mut().memories.map_info {
if unmap_begin < i.info.begin {
unmap_begin = i.info.begin // illegal range -> illegal range.
}
if unmap_begin >= end {
break; // all marked.
}
if unmap_begin >= i.info.end {
continue; // no overlap in this range, try next.
}
if unmap_begin == i.info.begin {
wait_rm.push(MemRegion {
begin: i.info.begin,
end: i.info.end,
perms: i.info.perms,
});
if end < i.info.end {
wait_add.push(MapInfo {
info: MemRegion {
begin: end,
end: i.info.end,
perms: i.info.perms,
},
label: i.label.clone(),
});
};
} else {
// unmap_begin > i.info.begin && unmap_begin < i.info.end
wait_rm.push(MemRegion {
begin: i.info.begin,
end: i.info.end,
perms: i.info.perms,
});
wait_add.push(MapInfo {
info: MemRegion {
begin: i.info.begin,
end: unmap_begin,
perms: i.info.perms,
},
label: i.label.clone(),
});
if end < i.info.end {
wait_add.push(MapInfo {
info: MemRegion {
begin: end,
end: i.info.end,
perms: i.info.perms,
},
label: i.label.clone(),
});
}
}
}

for ri in wait_rm {
self.get_data_mut()
.memories
.map_info
.retain(|i| !(i.info.begin == ri.begin && i.info.end == ri.end))
}
for ai in wait_add {
self.get_data_mut().memories.map_info.push(ai)
}
self.get_data_mut()
.memories
.map_info
.sort_by_key(|info| info.info.begin);

Unicorn::mem_unmap(self, addr, size)
}
fn is_mapped(&self, _addr: u64, _size: usize) -> Result<bool, uc_error> {
// FIXME: impl it.
Ok(false)
fn is_mapped(&self, addr: u64, size: usize) -> bool {
let mut begin = addr;
let mut end = addr + size as u64;
for i in &self.get_data().memories.map_info {
if begin < i.info.begin {
break;
}
if end <= i.info.end {
return true;
}
begin = i.info.end
}
false
}

fn next_mmap_address(&self, base_address: u64, size: usize) -> Result<u64, EmulatorError> {
let mut addr = base_address;
let size = size as u64;
for i in &self.get_data().memories.map_info {
if i.info.begin < base_address {
continue;
}
if addr + size <= i.info.begin {
break;
}
addr = i.info.end
}
if addr + size > (1 << 32) - 1 {
return Err(from_raw_syscall_ret(-12)); // ENOMEM, Cannot allocate memory
}
Ok(addr)
}

fn mprotect(&mut self, addr: u64, size: usize, perm: Permission) -> Result<(), uc_error> {
// TODO: manage map_info
Unicorn::mem_protect(self, addr, size, perm)
Expand Down
12 changes: 6 additions & 6 deletions omo/src/os/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ impl Inner {
},
Some("[brk]".to_string()),
)?;
log::debug!("brk mmaped, begin: {}, end: {}", cur_brk_addr, new_brk_addr)
} else if inp < cur_brk_addr {
Memory::mem_unmap(core, new_brk_addr, (cur_brk_addr - new_brk_addr) as usize)?;
}
Expand Down Expand Up @@ -641,7 +642,7 @@ impl Inner {
fd: u64,
pgoffset: u64, // TODO no fd supports yet
_ver: u8,
) -> Result<i64, uc_error> {
) -> Result<i64, EmulatorError> {
let fd = fd as i32;

log::debug!(
Expand Down Expand Up @@ -675,7 +676,7 @@ impl Inner {
let mut need_map = true;
if mmap_base != 0 {
// already mapped.
if Memory::is_mapped(core, mmap_base as u64, mmap_size as usize)? {
if Memory::is_mapped(core, mmap_base, mmap_size as usize) {
// if map fixed, we just protect mem
if flags & MAP_FIXED != 0 {
log::debug!("mmap2 - MAP_FIXED, mapping not needed");
Expand All @@ -690,8 +691,7 @@ impl Inner {
}
if need_map {
if mmap_base == 0 {
mmap_base = self.mmap_address;
self.mmap_address = mmap_base + mmap_size;
mmap_base = Memory::next_mmap_address(core, self.mmap_address, mmap_size as usize)?;
}

log::debug!(
Expand Down Expand Up @@ -719,7 +719,7 @@ impl Inner {
mmap_base + mmap_size
);
}
// TODO: should handle fd?

if fd != -1 {
log::warn!("[mmap2] fd {} not handled", fd);
}
Expand Down Expand Up @@ -750,7 +750,7 @@ impl Inner {
addr: u64,
length: u64,
) -> Result<i64, uc_error> {
log::debug!("[munmap] addr: {:#x}, length: {:#x}", addr, length);
log::debug!("[munmap] addr: {}, length: {}", addr, length);
let length = align_up(length as u32, core.pagesize() as u32);
Memory::mem_unmap(core, addr, length as usize)?;
Ok(0)
Expand Down