Skip to content

Commit e59469e

Browse files
committed
stop hardcoding pagesize
1 parent 966cf05 commit e59469e

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/discrete_alloc.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use nix::sys::mman::ProtFlags;
55

66
static ALLOCATOR: sync::Mutex<MachineAlloc> = sync::Mutex::new(MachineAlloc::empty());
77

8-
// FIXME: Assume 4k pages for now, needs adjusting
9-
108
/// A distinct allocator for the `MiriMachine`, allowing us to manage its
119
/// memory separately from that of Miri itself.
1210
#[derive(Debug)]
@@ -25,31 +23,33 @@ unsafe impl Send for MachineAlloc {}
2523
impl MachineAlloc {
2624
// Allocation-related methods
2725

28-
/// Initializes the allocator with 4k pages.
26+
/// Initializes the allocator with placeholder 4k pages.
2927
const fn empty() -> Self {
3028
Self {
3129
pages: Vec::new(),
3230
huge_allocs: Vec::new(),
3331
allocated: Vec::new(),
3432
page_size: 4096,
35-
//taken_sigaction: None,
3633
enabled: false,
3734
ffi_mode: false,
3835
}
3936
}
4037

4138
/// SAFETY: There must be no existing `MiriAllocBytes`
39+
#[allow(clippy::cast_sign_loss)]
40+
#[allow(clippy::cast_possible_truncation)]
4241
pub unsafe fn enable() {
4342
let mut alloc = ALLOCATOR.lock().unwrap();
4443
alloc.enabled = true;
45-
}
46-
47-
/// SAFETY: `MachineAlloc` must be empty.
48-
#[allow(clippy::cast_possible_truncation)]
49-
pub unsafe fn set_pagesize(page_size: u64) {
50-
let mut alloc = ALLOCATOR.lock().unwrap();
51-
assert!(page_size == 4096 || page_size == 4096 * 4);
52-
alloc.page_size = page_size as usize;
44+
// This needs to specifically be the system pagesize!
45+
alloc.page_size = unsafe {
46+
let ret = libc::sysconf(libc::_SC_PAGE_SIZE);
47+
if ret > 0 {
48+
ret as _
49+
} else {
50+
4096 // fallback
51+
}
52+
}
5353
}
5454

5555
/// Returns a vector of page addresses managed by the allocator.
@@ -79,7 +79,7 @@ impl MachineAlloc {
7979
#[inline]
8080
fn huge_normalized_layout(&self, layout: Layout) -> (usize, usize) {
8181
let size = layout.size().next_multiple_of(self.page_size);
82-
let align = std::cmp::max(layout.align(), 4096);
82+
let align = std::cmp::max(layout.align(), self.page_size);
8383
(size, align)
8484
}
8585

src/machine.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,6 @@ impl<'tcx> MiriMachine<'tcx> {
744744
(
745745
unsafe {
746746
discrete_alloc::MachineAlloc::enable();
747-
discrete_alloc::MachineAlloc::set_pagesize(page_size);
748747
libloading::Library::new(lib_file_path)
749748
.expect("failed to read specified extern shared object file")
750749
},

src/shims/trace.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use nix::unistd;
44

55
use crate::discrete_alloc;
66

7-
// Note: we do NOT ever want to block the child from accessing this!!
7+
// We do NOT ever want to block the child from accessing this!!
88
static SUPERVISOR: std::sync::Mutex<Option<Supervisor>> = std::sync::Mutex::new(None);
99
static mut PAGE_ADDR: *mut libc::c_void = std::ptr::null_mut();
10+
static mut PAGE_SIZE: usize = 4096;
1011
static mut CLICK_HERE_4_FREE_STACK: [u8; 1024] = [0; 1024];
1112

1213
#[cfg(target_pointer_width = "64")]
@@ -67,6 +68,15 @@ impl Supervisor {
6768
drop(sv);
6869

6970
if is_none {
71+
#[allow(clippy::cast_possible_truncation)]
72+
#[allow(clippy::cast_sign_loss)]
73+
unsafe {
74+
let ret = libc::sysconf(libc::_SC_PAGESIZE);
75+
if ret > 0 {
76+
PAGE_SIZE = ret as _;
77+
}
78+
}
79+
7080
let (t_message, r_message) = ipc::channel().unwrap();
7181
let (t_event, r_event) = ipc::channel().unwrap();
7282
unsafe {
@@ -407,7 +417,7 @@ fn handle_segfault(
407417
) {
408418
let siginfo = ptrace::getsiginfo(pid).unwrap();
409419
let addr = unsafe { siginfo.si_addr().addr() };
410-
let page_addr = addr - addr % 4096;
420+
let page_addr = addr - addr % unsafe { PAGE_SIZE };
411421

412422
if ch_pages.contains(&page_addr) {
413423
// Overall structure:
@@ -578,14 +588,14 @@ fn intercept_retptr(
578588
// manually, so we *must not ever* unwind from it
579589
pub unsafe extern "C" fn mempr_off() {
580590
unsafe {
581-
let _ = libc::mprotect(PAGE_ADDR, 4096, libc::PROT_READ | libc::PROT_WRITE);
591+
let _ = libc::mprotect(PAGE_ADDR, PAGE_SIZE, libc::PROT_READ | libc::PROT_WRITE);
582592
}
583593
let _ = signal::raise(signal::SIGSTOP);
584594
}
585595

586596
pub unsafe extern "C" fn mempr_on() {
587597
unsafe {
588-
let _ = libc::mprotect(PAGE_ADDR, 4096, libc::PROT_NONE);
598+
let _ = libc::mprotect(PAGE_ADDR, PAGE_SIZE, libc::PROT_NONE);
589599
}
590600
let _ = signal::raise(signal::SIGSTOP);
591601
}

0 commit comments

Comments
 (0)