From dd8d561abb661c19f5fbc6e2e5c729e73418e1ea Mon Sep 17 00:00:00 2001 From: "Tanya L. Crenshaw" Date: Mon, 6 Apr 2020 22:50:55 +0000 Subject: [PATCH 1/6] Fail instead of lucet_bail. --- lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs | 10 ++++++++++ .../lucet-runtime-internals/src/alloc/tests.rs | 2 +- .../lucet-runtime-internals/src/region/mmap.rs | 5 ++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs b/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs index cfbca292d..30703fd1a 100644 --- a/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs +++ b/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs @@ -1,6 +1,7 @@ use crate::error::Error; use crate::module::Module; use crate::region::RegionInternal; +use anyhow::format_err; use libc::c_void; use lucet_module::GlobalValue; use nix::unistd::{sysconf, SysconfVar}; @@ -92,6 +93,15 @@ impl Slot { pub fn stack_top(&self) -> *mut c_void { (self.stack as usize + self.limits.stack_size) as *mut c_void } + + pub fn is_heap_page_aligned(&self) -> Result<(), Error> { + if self.heap as usize % host_page_size() == 0 { + return Ok(()); + } + Err(Error::InternalError(format_err!( + "heap is not page-aligned; this is a bug" + ))) + } } /// The structure that manages the allocations backing an `Instance`. diff --git a/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs b/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs index 4db699b0e..844894ba1 100644 --- a/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs +++ b/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs @@ -4,7 +4,7 @@ macro_rules! alloc_tests { use libc::c_void; use std::sync::Arc; use $TestRegion as TestRegion; - use $crate::alloc::{host_page_size, Limits, MINSIGSTKSZ}; + use $crate::alloc::{host_page_size, Limits, Slot, MINSIGSTKSZ}; use $crate::context::{Context, ContextHandle}; use $crate::error::Error; use $crate::instance::InstanceInternal; diff --git a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs index 66e1b3920..bba2e0ea5 100644 --- a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs +++ b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs @@ -91,9 +91,8 @@ impl RegionInternal for MmapRegion { .pop() .ok_or(Error::RegionFull(self.capacity))?; - if slot.heap as usize % host_page_size() != 0 { - lucet_bail!("heap is not page-aligned; this is a bug"); - } + slot.is_heap_page_aligned() + .expect("Heap must be page aligned"); for (ptr, len) in [ // make the stack read/writable From f6f2f461e2f4125f6353995716ac07132a3deb51 Mon Sep 17 00:00:00 2001 From: "Tanya L. Crenshaw" Date: Mon, 6 Apr 2020 22:54:23 +0000 Subject: [PATCH 2/6] Make diff smaller. --- lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs b/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs index 844894ba1..4db699b0e 100644 --- a/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs +++ b/lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs @@ -4,7 +4,7 @@ macro_rules! alloc_tests { use libc::c_void; use std::sync::Arc; use $TestRegion as TestRegion; - use $crate::alloc::{host_page_size, Limits, Slot, MINSIGSTKSZ}; + use $crate::alloc::{host_page_size, Limits, MINSIGSTKSZ}; use $crate::context::{Context, ContextHandle}; use $crate::error::Error; use $crate::instance::InstanceInternal; From 36bea1351a2ce30ec7ad666ee5dac4e0cd1fb38c Mon Sep 17 00:00:00 2001 From: "Tanya L. Crenshaw" <56939192+fst-crenshaw@users.noreply.github.com> Date: Mon, 6 Apr 2020 16:16:06 -0700 Subject: [PATCH 3/6] Update lucet-runtime/lucet-runtime-internals/src/region/mmap.rs Co-Authored-By: Adam C. Foltzer --- lucet-runtime/lucet-runtime-internals/src/region/mmap.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs index bba2e0ea5..4289b65c9 100644 --- a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs +++ b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs @@ -91,8 +91,7 @@ impl RegionInternal for MmapRegion { .pop() .ok_or(Error::RegionFull(self.capacity))?; - slot.is_heap_page_aligned() - .expect("Heap must be page aligned"); + assert_neq!(slot.heap as usize % host_page_size(), 0, "heap must be page-aligned"); for (ptr, len) in [ // make the stack read/writable From c23f4152db5ca90b60afedd4dfd121492534cfb5 Mon Sep 17 00:00:00 2001 From: "Tanya L. Crenshaw" Date: Mon, 6 Apr 2020 23:16:18 +0000 Subject: [PATCH 4/6] Delete that which is not needed. --- lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs b/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs index 30703fd1a..2f5f7fee8 100644 --- a/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs +++ b/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs @@ -93,15 +93,6 @@ impl Slot { pub fn stack_top(&self) -> *mut c_void { (self.stack as usize + self.limits.stack_size) as *mut c_void } - - pub fn is_heap_page_aligned(&self) -> Result<(), Error> { - if self.heap as usize % host_page_size() == 0 { - return Ok(()); - } - Err(Error::InternalError(format_err!( - "heap is not page-aligned; this is a bug" - ))) - } } /// The structure that manages the allocations backing an `Instance`. From 910711eb1522b0c0a9346b197419d4015d8a5b05 Mon Sep 17 00:00:00 2001 From: "Tanya L. Crenshaw" Date: Mon, 6 Apr 2020 23:19:15 +0000 Subject: [PATCH 5/6] Assert that the mod finds no remainder. Then there is alignment. --- lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs | 1 - lucet-runtime/lucet-runtime-internals/src/region/mmap.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs b/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs index 2f5f7fee8..cfbca292d 100644 --- a/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs +++ b/lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs @@ -1,7 +1,6 @@ use crate::error::Error; use crate::module::Module; use crate::region::RegionInternal; -use anyhow::format_err; use libc::c_void; use lucet_module::GlobalValue; use nix::unistd::{sysconf, SysconfVar}; diff --git a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs index 4289b65c9..de73e0dd3 100644 --- a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs +++ b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs @@ -91,7 +91,7 @@ impl RegionInternal for MmapRegion { .pop() .ok_or(Error::RegionFull(self.capacity))?; - assert_neq!(slot.heap as usize % host_page_size(), 0, "heap must be page-aligned"); + assert_eq!(slot.heap as usize % host_page_size(), 0, "heap must be page-aligned"); for (ptr, len) in [ // make the stack read/writable From 1f9cc0355f9714f915e8509427bedcbc4a200450 Mon Sep 17 00:00:00 2001 From: "Tanya L. Crenshaw" Date: Mon, 6 Apr 2020 23:43:29 +0000 Subject: [PATCH 6/6] fmt. --- lucet-runtime/lucet-runtime-internals/src/region/mmap.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs index d3678c1fd..b8542535b 100644 --- a/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs +++ b/lucet-runtime/lucet-runtime-internals/src/region/mmap.rs @@ -113,7 +113,11 @@ impl RegionInternal for MmapRegion { .pop() .ok_or(Error::RegionFull(self.capacity))?; - assert_eq!(slot.heap as usize % host_page_size(), 0, "heap must be page-aligned"); + assert_eq!( + slot.heap as usize % host_page_size(), + 0, + "heap must be page-aligned" + ); for (ptr, len) in [ // make the stack read/writable