Skip to content

Commit 52ad2ad

Browse files
authored
Fix potential UB (#524)
* fix potential UB as reported by miri * add miri --doc CI testing * make miri aware of non-UB in InstructionPtr::new * apply rustfmt * refactor InstructionPtr constructor
1 parent dab2565 commit 52ad2ad

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

.github/workflows/rust.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,16 @@ jobs:
135135
toolchain: nightly
136136
override: true
137137
components: miri
138-
- name: Clippy (default features)
138+
- name: Clippy (--lib)
139139
uses: actions-rs/cargo@v1
140140
with:
141141
command: miri
142142
args: test --lib --workspace
143+
- name: Clippy (--doc)
144+
uses: actions-rs/cargo@v1
145+
with:
146+
command: miri
147+
args: test --doc --workspace
143148

144149
clippy:
145150
name: Clippy

crates/wasmi/src/engine/code_map.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use super::Instruction;
44
use alloc::vec::Vec;
5-
use core::ptr::NonNull;
65
use wasmi_arena::Index;
76

87
/// A reference to a Wasm function body stored in the [`CodeMap`].
@@ -102,7 +101,7 @@ impl CodeMap {
102101
/// Returns an [`InstructionPtr`] to the instruction at [`InstructionsRef`].
103102
#[inline]
104103
pub fn instr_ptr(&self, iref: InstructionsRef) -> InstructionPtr {
105-
InstructionPtr::new(&self.insts[iref.start])
104+
InstructionPtr::new(self.insts[iref.start..].as_ptr())
106105
}
107106

108107
/// Returns the [`FuncHeader`] of the [`FuncBody`].
@@ -137,15 +136,14 @@ impl CodeMap {
137136
#[derive(Debug, Copy, Clone)]
138137
pub struct InstructionPtr {
139138
/// The pointer to the instruction.
140-
ptr: NonNull<Instruction>,
139+
ptr: *const Instruction,
141140
}
142141

143142
impl InstructionPtr {
144143
/// Creates a new [`InstructionPtr`] for `instr`.
145-
pub fn new(instr: &Instruction) -> Self {
146-
Self {
147-
ptr: NonNull::from(instr),
148-
}
144+
#[inline]
145+
pub fn new(ptr: *const Instruction) -> Self {
146+
Self { ptr }
149147
}
150148

151149
/// Offset the [`InstructionPtr`] by the given value.
@@ -157,8 +155,7 @@ impl InstructionPtr {
157155
/// bounds of the instructions of the same compiled Wasm function.
158156
#[inline(always)]
159157
pub unsafe fn offset(&mut self, by: isize) {
160-
let new_ptr = &*self.ptr.as_ptr().offset(by);
161-
self.ptr = NonNull::from(new_ptr);
158+
self.ptr = self.ptr.offset(by);
162159
}
163160

164161
/// Returns a shared reference to the currently pointed at [`Instruction`].
@@ -170,6 +167,6 @@ impl InstructionPtr {
170167
/// the boundaries of its associated compiled Wasm function.
171168
#[inline(always)]
172169
pub unsafe fn get(&self) -> &Instruction {
173-
self.ptr.as_ref()
170+
&*self.ptr
174171
}
175172
}

0 commit comments

Comments
 (0)