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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ wat = "1.0.83"
name = "wasm-interpreter"
version = "0.2.0"
edition = "2021"
rust-version = "1.81.0" # Keep this in sync with the requirements!
rust-version = "1.87.0" # Keep this in sync with the requirements!
description = """
A WASM interpreter tailored for safety use-cases, such as automotive and avionics applications
"""
Expand Down
2 changes: 1 addition & 1 deletion crates/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "benchmark"
version = "0.1.0"
edition = "2021"
rust-version = "1.81.0"
rust-version = "1.87.0"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion crates/log_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "log_wrapper"
version = "0.1.0"
edition = "2021"
rust-version = "1.81.0" # Keep this in sync with the requirements!
rust-version = "1.87.0" # Keep this in sync with the requirements!
description = """
A WASM interpreter tailored for safety use-cases, such as automotive and avionics applications
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.sdoc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ RELATIONS:
UID: REQ-17
TITLE: Minimum Supported Rust Version (MSRV)
STATEMENT: >>>
The interpreter shall compile on Rust ``1.81.0`` and later versions
The interpreter shall compile on Rust ``1.87.0`` and later versions
<<<
RATIONALE: >>>
An MSRV should be clearly stated to specify which toolchain is acceptable.
Expand Down
3 changes: 2 additions & 1 deletion src/core/reader/section_header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::core::reader::span::Span;
use crate::core::reader::WasmReader;
use crate::core::utils::ToUsizeExt;
use crate::ValidationError;

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -53,7 +54,7 @@ impl SectionHeader {
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
let ty = SectionTy::read(wasm)?;
let size: u32 = wasm.read_var_u32()?;
let contents_span = wasm.make_span(size as usize)?;
let contents_span = wasm.make_span(size.into_usize())?;

Ok(SectionHeader {
ty,
Expand Down
13 changes: 7 additions & 6 deletions src/core/reader/types/element.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::global::GlobalType;
use super::RefType;
use crate::core::indices::{FuncIdx, TableIdx};
use crate::core::indices::FuncIdx;
use crate::core::reader::span::Span;
use crate::core::reader::types::TableType;
use crate::core::reader::WasmReader;
use crate::core::utils::ToUsizeExt;
use crate::read_constant_expression::read_constant_expression;
use crate::validation_stack::ValidationStack;
use crate::{NumType, ValType, ValidationError};
Expand Down Expand Up @@ -230,8 +231,8 @@ impl ElemType {
// start validating elemmode of form active {table x, offset expr}
// 1-2. C.tables[x] must be defined with type: limits t
let table_type = tables
.get(x as usize)
.ok_or(ValidationError::InvalidTableIdx(x as TableIdx))?
.get(x.into_usize())
.ok_or(ValidationError::InvalidTableIdx(x.into_usize()))?
.et;
if table_type != t {
return Err(ValidationError::ActiveElementSegmentTypeMismatch);
Expand Down Expand Up @@ -325,11 +326,11 @@ fn parse_validate_shortened_initializer_list(
) -> Result<ElemItems, ValidationError> {
wasm.read_vec(|w| {
let func_idx = w.read_var_u32()?;
if num_funcs <= func_idx as usize {
if num_funcs <= func_idx.into_usize() {
// TODO fix error
return Err(ValidationError::InvalidFuncIdx(func_idx as usize));
return Err(ValidationError::InvalidFuncIdx(func_idx.into_usize()));
}
validation_context_refs.insert(func_idx as FuncIdx);
validation_context_refs.insert(func_idx.into_usize());
Ok(func_idx)
})
.map(ElemItems::RefFuncs)
Expand Down
3 changes: 2 additions & 1 deletion src/core/reader/types/export.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::core::indices::{FuncIdx, GlobalIdx, MemIdx, TableIdx};
use crate::core::reader::types::import::ImportDesc;
use crate::core::reader::WasmReader;
use crate::core::utils::ToUsizeExt;
use crate::{ValidationError, ValidationInfo};

use super::ExternType;
Expand Down Expand Up @@ -120,7 +121,7 @@ impl ExportDesc {
impl ExportDesc {
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
let desc_id = wasm.read_u8()?;
let desc_idx = wasm.read_var_u32()? as usize;
let desc_idx = wasm.read_var_u32()?.into_usize();

let desc = match desc_id {
0x00 => ExportDesc::Func(desc_idx),
Expand Down
3 changes: 2 additions & 1 deletion src/core/reader/types/import.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::core::indices::TypeIdx;
use crate::core::reader::WasmReader;
use crate::core::utils::ToUsizeExt;
use crate::{ValidationError, ValidationInfo};

use super::global::GlobalType;
Expand Down Expand Up @@ -37,7 +38,7 @@ pub enum ImportDesc {
impl ImportDesc {
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
let desc = match wasm.read_u8()? {
0x00 => Self::Func(wasm.read_var_u32()? as TypeIdx),
0x00 => Self::Func(wasm.read_var_u32()?.into_usize()),
// https://webassembly.github.io/spec/core/binary/types.html#table-types
0x01 => Self::Table(TableType::read(wasm)?),
0x02 => Self::Mem(MemType::read(wasm)?),
Expand Down
6 changes: 3 additions & 3 deletions src/core/reader/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use alloc::vec::Vec;
use core::fmt::{Debug, Formatter};
use global::GlobalType;

use crate::core::indices::TypeIdx;
use crate::core::reader::WasmReader;
use crate::core::utils::ToUsizeExt;
use crate::execution::assert_validated::UnwrapValidatedExt;
use crate::ValidationError;

Expand Down Expand Up @@ -162,7 +162,7 @@ pub enum BlockType {

impl BlockType {
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
if wasm.peek_u8()? as i8 == 0x40 {
if wasm.peek_u8()? == 0x40 {
// Empty block type
let _ = wasm.read_u8().unwrap_validated();
Ok(BlockType::Empty)
Expand Down Expand Up @@ -196,7 +196,7 @@ impl BlockType {
},
}),
BlockType::Type(type_idx) => {
let type_idx = *type_idx as TypeIdx;
let type_idx = type_idx.into_usize();
func_types
.get(type_idx)
.cloned()
Expand Down
5 changes: 3 additions & 2 deletions src/core/reader/types/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use alloc::vec::Vec;

use crate::core::reader::WasmReader;
use crate::core::utils::ToUsizeExt;
use crate::ValidationError;

/// Wasm encodes integers according to the LEB128 format, which specifies that
Expand Down Expand Up @@ -352,7 +353,7 @@ impl<'wasm> WasmReader<'wasm> {

/// Note: If `Err`, the [WasmReader] object is no longer guaranteed to be in a valid state
pub fn read_name(&mut self) -> Result<&'wasm str, ValidationError> {
let len = self.read_var_u32()? as usize;
let len = self.read_var_u32()?.into_usize();

let utf8_str = &self
.full_wasm_binary
Expand Down Expand Up @@ -388,7 +389,7 @@ impl<'wasm> WasmReader<'wasm> {
{
let len = self.read_var_u32()?;
core::iter::repeat_with(|| read_element(self))
.take(len as usize)
.take(len.into_usize())
.collect()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/rw_spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<T> RwSpinLock<T> {
loop {
// s is even, so there are maybe active readers but no active or waiting writer
// -> reader can acquire read guard (as long as an overflow is avoided)
if s % 2 == 0 && s < u32::MAX - 2 {
if s.is_multiple_of(2) && s < u32::MAX - 2 {
match self.state.compare_exchange_weak(
s,
s + 2,
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<T> RwSpinLock<T> {
}

// announce that a writer is waiting if this is not yet announced
if s % 2 == 0 {
if s.is_multiple_of(2) {
match self
.state
// ordering by the book
Expand Down
17 changes: 17 additions & 0 deletions src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ pub fn print_beautiful_fd_extension(second_byte: u32, pc: usize) {
pc,
);
}

#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
pub trait ToUsizeExt {
fn into_usize(self) -> usize;
}

#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl ToUsizeExt for u32 {
fn into_usize(self) -> usize {
// SAFETY: The current trait only exists for architectures where
// pointers are at least 32 bits wide.
unsafe { usize::try_from(self).unwrap_unchecked() }
}
}

#[cfg(target_pointer_width = "16")]
compile_error!("targets with 16 bit wide pointers are currently not supported");
6 changes: 3 additions & 3 deletions src/execution/const_interpreter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::{
assert_validated::UnwrapValidatedExt,
config::Config,
core::{
indices::GlobalIdx,
reader::{
span::Span,
types::{FuncType, ResultType},
WasmReader,
},
utils::ToUsizeExt,
},
unreachable_validated,
value::{self, Ref},
Expand Down Expand Up @@ -51,7 +51,7 @@ pub(crate) fn run_const<T: Config>(
break;
}
GLOBAL_GET => {
let global_idx = wasm.read_var_u32().unwrap_validated() as GlobalIdx;
let global_idx = wasm.read_var_u32().unwrap_validated().into_usize();

//TODO replace double indirection
let global = store
Expand Down Expand Up @@ -92,7 +92,7 @@ pub(crate) fn run_const<T: Config>(
}
REF_FUNC => {
// we already checked for the func_idx to be in bounds during validation
let func_idx = wasm.read_var_u32().unwrap_validated() as usize;
let func_idx = wasm.read_var_u32().unwrap_validated().into_usize();
let func_addr = *store
.modules
.get(module)
Expand Down
Loading