diff --git a/Cargo.lock b/Cargo.lock index 7e6808bbfcf..685e1b7f821 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3231,6 +3231,7 @@ dependencies = [ "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)", "geckoservo 0.0.1", + "hashglobe 0.1.0", "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "malloc_size_of 0.0.1", diff --git a/components/hashglobe/src/lib.rs b/components/hashglobe/src/lib.rs index 8a2d070e0e2..cdb98ad2003 100644 --- a/components/hashglobe/src/lib.rs +++ b/components/hashglobe/src/lib.rs @@ -52,3 +52,6 @@ impl fmt::Display for FailedAllocationError { self.reason.fmt(f) } } + +// The size of memory pages on this system. Set when initializing geckolib. +pub static SYSTEM_PAGE_SIZE: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::ATOMIC_USIZE_INIT; diff --git a/components/hashglobe/src/table.rs b/components/hashglobe/src/table.rs index 45dcb2673fa..602cd2131b4 100644 --- a/components/hashglobe/src/table.rs +++ b/components/hashglobe/src/table.rs @@ -777,7 +777,7 @@ impl RawTable { // FORK NOTE: Uses alloc shim instead of Heap.alloc - let buffer = alloc(size, alignment); + let buffer = alloc(round_up_to_page_size(size), alignment); if buffer.is_null() { @@ -1201,3 +1201,19 @@ impl Drop for RawTable { } } } + +// Force all allocations to fill their pages for the duration of the mprotect +// experiment. +#[inline] +fn round_up_to_page_size(size: usize) -> usize { + let page_size = ::SYSTEM_PAGE_SIZE.load(::std::sync::atomic::Ordering::Relaxed); + debug_assert!(page_size != 0); + let mut result = size; + let remainder = size % page_size; + if remainder != 0 { + result += page_size - remainder; + } + debug_assert!(result % page_size == 0); + debug_assert!(result - size < page_size); + result +} diff --git a/ports/geckolib/Cargo.toml b/ports/geckolib/Cargo.toml index 4043e9beb56..4f6b6fa670f 100644 --- a/ports/geckolib/Cargo.toml +++ b/ports/geckolib/Cargo.toml @@ -17,6 +17,7 @@ gecko_debug = ["style/gecko_debug"] atomic_refcell = "0.1" cssparser = "0.21.1" env_logger = {version = "0.4", default-features = false} # disable `regex` to reduce code size +hashglobe = {path = "../../components/hashglobe"} libc = "0.2" log = {version = "0.3.5", features = ["release_max_level_info"]} malloc_size_of = {path = "../../components/malloc_size_of"} diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index 69c039a9817..d79b2ca6496 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -181,6 +181,10 @@ pub extern "C" fn Servo_Initialize(dummy_url_data: *mut URLExtraData) { // Initialize the dummy url data unsafe { DUMMY_URL_DATA = dummy_url_data; } + + // Set the system page size. + let page_size = unsafe { bindings::Gecko_GetSystemPageSize() }; + ::hashglobe::SYSTEM_PAGE_SIZE.store(page_size, ::std::sync::atomic::Ordering::Relaxed); } #[no_mangle] diff --git a/ports/geckolib/lib.rs b/ports/geckolib/lib.rs index ca57307c245..7bfc0525059 100644 --- a/ports/geckolib/lib.rs +++ b/ports/geckolib/lib.rs @@ -6,6 +6,7 @@ extern crate cssparser; extern crate env_logger; +extern crate hashglobe; extern crate libc; #[macro_use] extern crate log; extern crate malloc_size_of; diff --git a/tests/unit/stylo/Cargo.toml b/tests/unit/stylo/Cargo.toml index 40248fb0420..bbe601c56b8 100644 --- a/tests/unit/stylo/Cargo.toml +++ b/tests/unit/stylo/Cargo.toml @@ -17,6 +17,7 @@ cssparser = "0.21.1" env_logger = "0.4" euclid = "0.15" geckoservo = {path = "../../../ports/geckolib"} +hashglobe = {path = "../../../components/hashglobe"} libc = "0.2" log = {version = "0.3.5", features = ["release_max_level_info"]} malloc_size_of = {path = "../../../components/malloc_size_of"} diff --git a/tests/unit/stylo/lib.rs b/tests/unit/stylo/lib.rs index 6b460558953..ac7c61f587c 100644 --- a/tests/unit/stylo/lib.rs +++ b/tests/unit/stylo/lib.rs @@ -6,6 +6,7 @@ extern crate atomic_refcell; extern crate cssparser; extern crate env_logger; extern crate geckoservo; +extern crate hashglobe; #[macro_use] extern crate log; extern crate malloc_size_of; extern crate selectors;