Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 30ff57b

Browse files
froydnjiximeow
authored andcommitted
move host_page_size to sysdeps
1 parent d55dc62 commit 30ff57b

7 files changed

Lines changed: 44 additions & 32 deletions

File tree

lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
use crate::error::Error;
22
use crate::module::Module;
33
use crate::region::RegionInternal;
4+
use crate::sysdeps::host_page_size;
45
use libc::c_void;
56
use lucet_module::GlobalValue;
6-
use nix::unistd::{sysconf, SysconfVar};
77
use rand::{thread_rng, Rng, RngCore};
8-
use std::sync::{Arc, Mutex, Once, Weak};
9-
10-
pub const HOST_PAGE_SIZE_EXPECTED: usize = 4096;
11-
static mut HOST_PAGE_SIZE: usize = 0;
12-
static HOST_PAGE_SIZE_INIT: Once = Once::new();
13-
14-
/// Our host is Linux x86_64, which should always use a 4K page.
15-
///
16-
/// We double check the expected value using `sysconf` at runtime.
17-
pub fn host_page_size() -> usize {
18-
unsafe {
19-
HOST_PAGE_SIZE_INIT.call_once(|| match sysconf(SysconfVar::PAGE_SIZE) {
20-
Ok(Some(sz)) => {
21-
if sz as usize == HOST_PAGE_SIZE_EXPECTED {
22-
HOST_PAGE_SIZE = HOST_PAGE_SIZE_EXPECTED;
23-
} else {
24-
panic!(
25-
"host page size was {}; expected {}",
26-
sz, HOST_PAGE_SIZE_EXPECTED
27-
);
28-
}
29-
}
30-
_ => panic!("could not get host page size from sysconf"),
31-
});
32-
HOST_PAGE_SIZE
33-
}
34-
}
8+
use std::sync::{Arc, Mutex, Weak};
359

3610
pub fn instance_heap_offset() -> usize {
3711
1 * host_page_size()

lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ macro_rules! alloc_tests {
66
use rand::{thread_rng, Rng, SeedableRng};
77
use std::sync::{Arc, Mutex};
88
use $TestRegion as TestRegion;
9-
use $crate::alloc::{host_page_size, AllocStrategy, Limits, MINSIGSTKSZ};
9+
use $crate::alloc::{AllocStrategy, Limits, MINSIGSTKSZ};
1010
use $crate::context::{Context, ContextHandle};
1111
use $crate::error::Error;
1212
use $crate::instance::InstanceInternal;
1313
use $crate::module::{
1414
FunctionPointer, GlobalValue, HeapSpec, MockExportBuilder, MockModuleBuilder, Module,
1515
};
1616
use $crate::region::Region;
17+
use $crate::sysdeps::host_page_size;
1718
use $crate::val::Val;
1819
use $crate::vmctx::lucet_vmctx;
1920

lucet-runtime/lucet-runtime-internals/src/instance.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ pub use crate::instance::execution::{KillError, KillState, KillSuccess, KillSwit
77
pub use crate::instance::signals::{signal_handler_none, SignalBehavior, SignalHandler};
88
pub use crate::instance::state::State;
99

10-
use crate::alloc::{Alloc, HOST_PAGE_SIZE_EXPECTED};
10+
use crate::alloc::Alloc;
1111
use crate::context::Context;
1212
use crate::embed_ctx::CtxMap;
1313
use crate::error::Error;
1414
#[cfg(feature = "concurrent_testpoints")]
1515
use crate::lock_testpoints::LockTestpoints;
1616
use crate::module::{self, FunctionHandle, Global, GlobalValue, Module, TrapCode};
1717
use crate::region::RegionInternal;
18+
use crate::sysdeps::HOST_PAGE_SIZE_EXPECTED;
1819
use crate::val::{UntypedRetVal, Val};
1920
use crate::WASM_PAGE_SIZE;
2021
use libc::{c_void, pthread_self, siginfo_t, uintptr_t};

lucet-runtime/lucet-runtime-internals/src/module/sparse_page_data.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ macro_rules! sparse_page_data_tests {
33
( $TestRegion:path ) => {
44
use std::sync::Arc;
55
use $TestRegion as TestRegion;
6-
use $crate::alloc::{host_page_size, Limits};
6+
use $crate::alloc::Limits;
77
use $crate::instance::InstanceInternal;
88
use $crate::module::{MockModuleBuilder, Module};
99
use $crate::region::Region;
10+
use $crate::sysdeps::host_page_size;
1011

1112
const FIRST_MESSAGE: &'static [u8] = b"hello from mock_sparse_module!";
1213
const SECOND_MESSAGE: &'static [u8] = b"hello again from mock_sparse_module!";

lucet-runtime/lucet-runtime-internals/src/region/mmap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::alloc::{host_page_size, instance_heap_offset, Alloc, AllocStrategy, Limits, Slot};
1+
use crate::alloc::{instance_heap_offset, Alloc, AllocStrategy, Limits, Slot};
22
use crate::embed_ctx::CtxMap;
33
use crate::error::Error;
44
use crate::instance::{new_instance_handle, Instance, InstanceHandle};
55
use crate::module::Module;
66
use crate::region::{Region, RegionCreate, RegionInternal};
7+
use crate::sysdeps::host_page_size;
78
use libc::c_void;
89
#[cfg(not(target_os = "linux"))]
910
use libc::memset;

lucet-runtime/lucet-runtime-internals/src/sysdeps/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ mod macos;
44
#[cfg(target_os = "linux")]
55
mod linux;
66

7+
#[cfg(unix)]
8+
mod unix;
9+
710
#[cfg(target_os = "macos")]
811
pub use macos::*;
912

1013
#[cfg(target_os = "linux")]
1114
pub use linux::*;
15+
16+
#[cfg(unix)]
17+
pub use unix::*;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use nix::unistd::{sysconf, SysconfVar};
2+
use std::sync::Once;
3+
4+
pub const HOST_PAGE_SIZE_EXPECTED: usize = 4096;
5+
static mut HOST_PAGE_SIZE: usize = 0;
6+
static HOST_PAGE_SIZE_INIT: Once = Once::new();
7+
8+
/// Linux x86-64 and Mac x86-64 hosts should always use a 4K page.
9+
///
10+
/// We double check the expected value using `sysconf` at runtime.
11+
pub fn host_page_size() -> usize {
12+
unsafe {
13+
HOST_PAGE_SIZE_INIT.call_once(|| match sysconf(SysconfVar::PAGE_SIZE) {
14+
Ok(Some(sz)) => {
15+
if sz as usize == HOST_PAGE_SIZE_EXPECTED {
16+
HOST_PAGE_SIZE = HOST_PAGE_SIZE_EXPECTED;
17+
} else {
18+
panic!(
19+
"host page size was {}; expected {}",
20+
sz, HOST_PAGE_SIZE_EXPECTED
21+
);
22+
}
23+
}
24+
_ => panic!("could not get host page size from sysconf"),
25+
});
26+
HOST_PAGE_SIZE
27+
}
28+
}

0 commit comments

Comments
 (0)