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

Commit ca7757e

Browse files
committed
fix provenance for TaggedFuncRef
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 18a15c2 commit ca7757e

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

crates/wasmtime/src/runtime/vm/table.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::prelude::*;
88
use crate::runtime::vm::stack_switching::VMContObj;
99
use crate::runtime::vm::vmcontext::{VMFuncRef, VMTableDefinition};
10-
use crate::runtime::vm::{GcStore, SendSyncPtr, VMGcRef, VMStore};
10+
use crate::runtime::vm::{GcStore, SendSyncPtr, VMGcRef, VMStore, VmPtr};
1111
use core::alloc::Layout;
1212
use core::mem;
1313
use core::ops::Range;
@@ -134,19 +134,26 @@ impl From<VMContObj> for TableElement {
134134

135135
#[derive(Copy, Clone)]
136136
#[repr(transparent)]
137-
struct TaggedFuncRef(*mut VMFuncRef);
137+
struct TaggedFuncRef(Option<VmPtr<VMFuncRef>>);
138138

139139
impl TaggedFuncRef {
140-
const UNINIT: TaggedFuncRef = TaggedFuncRef(ptr::null_mut());
140+
const UNINIT: TaggedFuncRef = TaggedFuncRef(None);
141141

142142
/// Converts the given `ptr`, a valid funcref pointer, into a tagged pointer
143143
/// by adding in the `FUNCREF_INIT_BIT`.
144144
fn from(ptr: Option<NonNull<VMFuncRef>>, lazy_init: bool) -> Self {
145-
let ptr = ptr.map(|p| p.as_ptr()).unwrap_or(ptr::null_mut());
146145
if lazy_init {
147-
let masked = ptr.map_addr(|a| a | FUNCREF_INIT_BIT);
146+
let masked = match ptr {
147+
Some(ptr) => Some(ptr.map_addr(|a| a | FUNCREF_INIT_BIT).into()),
148+
None => Some(
149+
NonNull::new(core::ptr::without_provenance_mut(FUNCREF_INIT_BIT))
150+
.unwrap()
151+
.into(),
152+
),
153+
};
148154
TaggedFuncRef(masked)
149155
} else {
156+
let ptr = ptr.map(|p| p.into());
150157
TaggedFuncRef(ptr)
151158
}
152159
}
@@ -155,13 +162,14 @@ impl TaggedFuncRef {
155162
/// for null (not a tagged value) or `FuncRef` for otherwise tagged values.
156163
fn into_table_element(self, lazy_init: bool) -> TableElement {
157164
let ptr = self.0;
158-
if lazy_init && ptr.is_null() {
165+
if lazy_init && ptr.is_none() {
159166
TableElement::UninitFunc
160167
} else {
161168
// Masking off the tag bit is harmless whether the table uses lazy
162169
// init or not.
163-
let unmasked = ptr.map_addr(|a| a & FUNCREF_MASK);
164-
TableElement::FuncRef(NonNull::new(unmasked))
170+
let unmasked =
171+
ptr.and_then(|ptr| NonNull::new(ptr.as_ptr().map_addr(|a| a & FUNCREF_MASK)));
172+
TableElement::FuncRef(unmasked)
165173
}
166174
}
167175
}

0 commit comments

Comments
 (0)