Skip to content
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
2 changes: 1 addition & 1 deletion cranelift/wasm/src/environ/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
&mut self,
_table_index: TableIndex,
_base: Option<GlobalIndex>,
_offset: u32,
_offset: u64,
_elements: Box<[FuncIndex]>,
) -> WasmResult<()> {
// We do nothing
Expand Down
2 changes: 1 addition & 1 deletion cranelift/wasm/src/environ/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ pub trait ModuleEnvironment<'data>: TypeConvert {
&mut self,
table_index: TableIndex,
base: Option<GlobalIndex>,
offset: u32,
offset: u64,
elements: Box<[FuncIndex]>,
) -> WasmResult<()>;

Expand Down
15 changes: 8 additions & 7 deletions cranelift/wasm/src/sections_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
TypeIndex, WasmError, WasmResult,
};
use cranelift_entity::packed_option::ReservedValue;
use cranelift_entity::EntityRef;
use cranelift_entity::{EntityRef, Unsigned};
use std::boxed::Box;
use std::vec::Vec;
use wasmparser::{
Expand Down Expand Up @@ -185,7 +185,7 @@ pub fn parse_export_section<'data>(
// The input has already been validated, so we should be able to
// assume valid UTF-8 and use `from_utf8_unchecked` if performance
// becomes a concern here.
let index = index as usize;
let index = usize::try_from(index)?;
match *kind {
ExternalKind::Func => environ.declare_func_export(FuncIndex::new(index), name)?,
ExternalKind::Table => environ.declare_table_export(TableIndex::new(index), name)?,
Expand Down Expand Up @@ -252,7 +252,8 @@ pub fn parse_element_section<'data>(
} => {
let mut offset_expr_reader = offset_expr.get_binary_reader();
let (base, offset) = match offset_expr_reader.read_operator()? {
Operator::I32Const { value } => (None, value as u32),
Operator::I32Const { value } => (None, u64::from(value.unsigned())),
Operator::I64Const { value } => (None, value.unsigned()),
Operator::GlobalGet { global_index } => {
(Some(GlobalIndex::from_u32(global_index)), 0)
}
Expand All @@ -271,7 +272,7 @@ pub fn parse_element_section<'data>(
)?
}
ElementKind::Passive => {
let index = ElemIndex::from_u32(index as u32);
let index = ElemIndex::from_u32(u32::try_from(index)?);
environ.declare_passive_element(index, segments)?;
}
ElementKind::Declared => {
Expand Down Expand Up @@ -302,8 +303,8 @@ pub fn parse_data_section<'data>(
} => {
let mut offset_expr_reader = offset_expr.get_binary_reader();
let (base, offset) = match offset_expr_reader.read_operator()? {
Operator::I32Const { value } => (None, value as u64),
Operator::I64Const { value } => (None, value as u64),
Operator::I32Const { value } => (None, u64::try_from(value)?),
Operator::I64Const { value } => (None, u64::try_from(value)?),
Operator::GlobalGet { global_index } => {
(Some(GlobalIndex::from_u32(global_index)), 0)
}
Expand All @@ -322,7 +323,7 @@ pub fn parse_data_section<'data>(
)?;
}
DataKind::Passive => {
let index = DataIndex::from_u32(index as u32);
let index = DataIndex::from_u32(u32::try_from(index)?);
environ.declare_passive_data(index, data)?;
}
}
Expand Down
30 changes: 21 additions & 9 deletions cranelift/wasm/src/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir::{self, condcodes::IntCC, immediates::Imm64, InstBuilder};
use cranelift_codegen::isa::TargetIsa;
use cranelift_frontend::FunctionBuilder;

/// Size of a WebAssembly table, in elements.
Expand All @@ -8,7 +9,7 @@ pub enum TableSize {
/// Non-resizable table.
Static {
/// Non-resizable tables have a constant size known at compile time.
bound: u32,
bound: u64,
},
/// Resizable table.
Dynamic {
Expand All @@ -20,10 +21,21 @@ pub enum TableSize {

impl TableSize {
/// Get a CLIF value representing the current bounds of this table.
pub fn bound(&self, mut pos: FuncCursor, index_ty: ir::Type) -> ir::Value {
pub fn bound(&self, isa: &dyn TargetIsa, mut pos: FuncCursor, index_ty: ir::Type) -> ir::Value {
match *self {
TableSize::Static { bound } => pos.ins().iconst(index_ty, Imm64::new(i64::from(bound))),
TableSize::Dynamic { bound_gv } => pos.ins().global_value(index_ty, bound_gv),
// Instead of `i64::try_from(bound)`, here we just want to direcly interpret `bound` as an i64.
TableSize::Static { bound } => pos.ins().iconst(index_ty, Imm64::new(bound as i64)),
TableSize::Dynamic { bound_gv } => {
let ty = pos.func.global_values[bound_gv].global_type(isa);
let gv = pos.ins().global_value(ty, bound_gv);
if index_ty == ty {
gv
} else if index_ty.bytes() < ty.bytes() {
pos.ins().ireduce(index_ty, gv)
} else {
pos.ins().uextend(index_ty, gv)
}
}
}
}
}
Expand All @@ -46,22 +58,22 @@ impl TableData {
/// given index within this table.
pub fn prepare_table_addr(
&self,
isa: &dyn TargetIsa,
pos: &mut FunctionBuilder,
mut index: ir::Value,
addr_ty: ir::Type,
enable_table_access_spectre_mitigation: bool,
) -> (ir::Value, ir::MemFlags) {
let index_ty = pos.func.dfg.value_type(index);
let addr_ty = isa.pointer_type();

// Start with the bounds check. Trap if `index + 1 > bound`.
let bound = self.bound.bound(pos.cursor(), index_ty);
let bound = self.bound.bound(isa, pos.cursor(), index_ty);

// `index > bound - 1` is the same as `index >= bound`.
let oob = pos
.ins()
.icmp(IntCC::UnsignedGreaterThanOrEqual, index, bound);

if !enable_table_access_spectre_mitigation {
if !isa.flags().enable_table_access_spectre_mitigation() {
pos.ins().trapnz(oob, ir::TrapCode::TableOutOfBounds);
}

Expand All @@ -88,7 +100,7 @@ impl TableData {
let base_flags = ir::MemFlags::new()
.with_aligned()
.with_alias_region(Some(ir::AliasRegion::Table));
if enable_table_access_spectre_mitigation {
if isa.flags().enable_table_access_spectre_mitigation() {
// Short-circuit the computed table element address to a null pointer
// when out-of-bounds. The consumer of this address will trap when
// trying to access it.
Expand Down
10 changes: 5 additions & 5 deletions crates/c-api/include/wasmtime/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ wasmtime_table_type(const wasmtime_context_t *store,
*/
WASM_API_EXTERN bool wasmtime_table_get(wasmtime_context_t *store,
const wasmtime_table_t *table,
uint32_t index, wasmtime_val_t *val);
uint64_t index, wasmtime_val_t *val);

/**
* \brief Sets a value in a table.
Expand All @@ -74,12 +74,12 @@ WASM_API_EXTERN bool wasmtime_table_get(wasmtime_context_t *store,
*/
WASM_API_EXTERN wasmtime_error_t *
wasmtime_table_set(wasmtime_context_t *store, const wasmtime_table_t *table,
uint32_t index, const wasmtime_val_t *value);
uint64_t index, const wasmtime_val_t *value);

/**
* \brief Returns the size, in elements, of the specified table
*/
WASM_API_EXTERN uint32_t wasmtime_table_size(const wasmtime_context_t *store,
WASM_API_EXTERN uint64_t wasmtime_table_size(const wasmtime_context_t *store,
const wasmtime_table_t *table);

/**
Expand All @@ -101,8 +101,8 @@ WASM_API_EXTERN uint32_t wasmtime_table_size(const wasmtime_context_t *store,
*/
WASM_API_EXTERN wasmtime_error_t *
wasmtime_table_grow(wasmtime_context_t *store, const wasmtime_table_t *table,
uint32_t delta, const wasmtime_val_t *init,
uint32_t *prev_size);
uint64_t delta, const wasmtime_val_t *init,
uint64_t *prev_size);

#ifdef __cplusplus
} // extern "C"
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub extern "C" fn wasmtime_store_limiter(
limiter = limiter.memory_size(memory_size as usize);
}
if table_elements >= 0 {
limiter = limiter.table_elements(table_elements as u32);
limiter = limiter.table_elements(table_elements as usize);
}
if instances >= 0 {
limiter = limiter.instances(instances as usize);
Expand Down
22 changes: 13 additions & 9 deletions crates/c-api/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub unsafe extern "C" fn wasm_table_get(
index: wasm_table_size_t,
) -> Option<Box<wasm_ref_t>> {
let table = t.table();
let r = table.get(t.ext.store.context_mut(), index)?;
let r = table.get(t.ext.store.context_mut(), u64::from(index))?;
wasm_ref_t::new(r)
}

Expand All @@ -79,14 +79,16 @@ pub unsafe extern "C" fn wasm_table_set(
) -> bool {
let table = t.table();
let val = option_wasm_ref_t_to_ref(r, &table.ty(t.ext.store.context()));
table.set(t.ext.store.context_mut(), index, val).is_ok()
table
.set(t.ext.store.context_mut(), u64::from(index), val)
.is_ok()
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
let table = t.table();
let store = t.ext.store.context();
table.size(&store)
u32::try_from(table.size(&store)).unwrap()
}

#[no_mangle]
Expand All @@ -97,7 +99,9 @@ pub unsafe extern "C" fn wasm_table_grow(
) -> bool {
let table = t.table();
let init = option_wasm_ref_t_to_ref(init, &table.ty(t.ext.store.context()));
table.grow(t.ext.store.context_mut(), delta, init).is_ok()
table
.grow(t.ext.store.context_mut(), u64::from(delta), init)
.is_ok()
}

#[no_mangle]
Expand Down Expand Up @@ -139,7 +143,7 @@ pub unsafe extern "C" fn wasmtime_table_type(
pub extern "C" fn wasmtime_table_get(
store: WasmtimeStoreContextMut<'_>,
table: &Table,
index: u32,
index: u64,
ret: &mut MaybeUninit<wasmtime_val_t>,
) -> bool {
let mut scope = RootScope::new(store);
Expand All @@ -156,7 +160,7 @@ pub extern "C" fn wasmtime_table_get(
pub unsafe extern "C" fn wasmtime_table_set(
mut store: WasmtimeStoreContextMut<'_>,
table: &Table,
index: u32,
index: u64,
val: &wasmtime_val_t,
) -> Option<Box<wasmtime_error_t>> {
let mut scope = RootScope::new(&mut store);
Expand All @@ -170,17 +174,17 @@ pub unsafe extern "C" fn wasmtime_table_set(
}

#[no_mangle]
pub extern "C" fn wasmtime_table_size(store: WasmtimeStoreContext<'_>, table: &Table) -> u32 {
pub extern "C" fn wasmtime_table_size(store: WasmtimeStoreContext<'_>, table: &Table) -> u64 {
table.size(store)
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_grow(
mut store: WasmtimeStoreContextMut<'_>,
table: &Table,
delta: u32,
delta: u64,
val: &wasmtime_val_t,
prev_size: &mut u32,
prev_size: &mut u64,
) -> Option<Box<wasmtime_error_t>> {
let mut scope = RootScope::new(&mut store);
handle_result(
Expand Down
4 changes: 2 additions & 2 deletions crates/c-api/src/types/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub extern "C" fn wasm_tabletype_element(tt: &wasm_tabletype_t) -> &wasm_valtype
pub extern "C" fn wasm_tabletype_limits(tt: &wasm_tabletype_t) -> &wasm_limits_t {
let tt = tt.ty();
tt.limits_cache.get_or_init(|| wasm_limits_t {
min: tt.ty.minimum(),
max: tt.ty.maximum().unwrap_or(u32::max_value()),
min: u32::try_from(tt.ty.minimum()).unwrap(),
max: u32::try_from(tt.ty.maximum().unwrap_or(u64::from(u32::MAX))).unwrap(),
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ wasmtime_option_group! {

/// The maximum table elements for any table defined in a module when
/// using the pooling allocator.
pub pooling_table_elements: Option<u32>,
pub pooling_table_elements: Option<usize>,

/// The maximum size, in bytes, allocated for a core instance's metadata
/// when using the pooling allocator.
Expand Down Expand Up @@ -215,7 +215,7 @@ wasmtime_option_group! {
/// WebAssembly modules to return -1 and fail.
pub max_memory_size: Option<usize>,
/// Maximum size, in table elements, that a table is allowed to reach.
pub max_table_elements: Option<u32>,
pub max_table_elements: Option<usize>,
/// Maximum number of WebAssembly instances allowed to be created.
pub max_instances: Option<usize>,
/// Maximum number of WebAssembly tables allowed to be created.
Expand Down
Loading