Skip to content

Fix potential UB #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ jobs:
toolchain: nightly
override: true
components: miri
- name: Clippy (default features)
- name: Clippy (--lib)
uses: actions-rs/cargo@v1
with:
command: miri
args: test --lib --workspace
- name: Clippy (--doc)
uses: actions-rs/cargo@v1
with:
command: miri
args: test --doc --workspace

clippy:
name: Clippy
Expand Down
17 changes: 7 additions & 10 deletions crates/wasmi/src/engine/code_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use super::Instruction;
use alloc::vec::Vec;
use core::ptr::NonNull;
use wasmi_arena::Index;

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

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

impl InstructionPtr {
/// Creates a new [`InstructionPtr`] for `instr`.
pub fn new(instr: &Instruction) -> Self {
Self {
ptr: NonNull::from(instr),
}
#[inline]
pub fn new(ptr: *const Instruction) -> Self {
Self { ptr }
}

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

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