Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 92fa0f8

Browse files
authored
UB fixes (#287)
Validating Cell construction with NockStack Use the active fork, memmap2
1 parent f9915a7 commit 92fa0f8

File tree

6 files changed

+19
-10
lines changed

6 files changed

+19
-10
lines changed

Cargo.lock

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ibig/src/fast_divide.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ mod tests {
212212
use rand::prelude::*;
213213

214214
#[test]
215+
#[cfg_attr(miri, ignore)]
215216
fn test_fast_divide_small() {
216217
let mut rng = StdRng::seed_from_u64(1);
217218
for _ in 0..1000000 {
@@ -227,6 +228,7 @@ mod tests {
227228
}
228229

229230
#[test]
231+
#[cfg_attr(miri, ignore)]
230232
fn test_fast_divide_normalized() {
231233
let mut rng = StdRng::seed_from_u64(1);
232234
for _ in 0..1000000 {

rust/sword/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ intmap = "1.1.0"
2121
json = "0.12.4"
2222
lazy_static = "1.4.0"
2323
libc = "0.2.126"
24-
memmap = "0.7.0"
24+
memmap2 = "^0.9.5"
2525
num-derive = "0.3"
2626
num-traits = "0.2"
2727
rand = "0.8.5"

rust/sword/src/mem.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{assert_acyclic, assert_no_forwarding_pointers, assert_no_junior_poin
44
use assert_no_alloc::permit_alloc;
55
use either::Either::{self, Left, Right};
66
use ibig::Stack;
7-
use memmap::MmapMut;
7+
use memmap2::MmapMut;
88
use std::alloc::Layout;
99
use std::panic::panic_any;
1010
use std::ptr::copy_nonoverlapping;
@@ -196,8 +196,8 @@ impl NockStack {
196196
return Err(NewStackError::StackTooSmall);
197197
}
198198
let free = size - (top_slots + RESERVED);
199-
let memory = MmapMut::map_anon(size << 3)?;
200-
let start = memory.as_ptr() as *const u64;
199+
let mut memory = MmapMut::map_anon(size << 3)?;
200+
let start = memory.as_mut_ptr() as *mut u64;
201201
// Here, frame_pointer < alloc_pointer, so the initial frame is West
202202
let frame_pointer = unsafe { start.add(RESERVED + top_slots) } as *mut u64;
203203
let stack_pointer = frame_pointer;
@@ -213,7 +213,7 @@ impl NockStack {
213213
);
214214
Ok((
215215
NockStack {
216-
start,
216+
start: start as *const u64,
217217
size,
218218
frame_pointer,
219219
stack_pointer,

rust/sword/src/noun.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ impl Cell {
662662

663663
pub unsafe fn new_raw_mut<A: NounAllocator>(allocator: &mut A) -> (Cell, *mut CellMemory) {
664664
let memory = allocator.alloc_cell();
665+
assert!(memory as usize % std::mem::align_of::<CellMemory>() == 0, "Memory is not aligned, {} {}", memory as usize, std::mem::align_of::<CellMemory>());
665666
(*memory).metadata = 0;
666667
(Self::from_raw_pointer(memory), memory)
667668
}

rust/sword/src/serialization.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,11 @@ mod tests {
715715
println!("got expected error: {:?}", e);
716716
}
717717
}
718+
719+
#[test]
720+
fn test_cell_construction() {
721+
let mut stack = setup_stack();
722+
let (cell, cell_mem_ptr) = unsafe { Cell::new_raw_mut(&mut stack) };
723+
unsafe { assert!(cell_mem_ptr as *const CellMemory == cell.to_raw_pointer()) };
724+
}
718725
}

0 commit comments

Comments
 (0)