Skip to content

Commit 3fb67f2

Browse files
refactor: replace as uses with new u32->usize helper
Signed-off-by: Florian Hartung <florian.hartung@dlr.de>
1 parent df623b7 commit 3fb67f2

File tree

15 files changed

+179
-157
lines changed

15 files changed

+179
-157
lines changed

src/core/reader/section_header.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::core::reader::span::Span;
22
use crate::core::reader::WasmReader;
3+
use crate::core::utils::ToUsizeExt;
34
use crate::ValidationError;
45

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

5859
Ok(SectionHeader {
5960
ty,

src/core/reader/types/element.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use super::global::GlobalType;
22
use super::RefType;
3-
use crate::core::indices::{FuncIdx, TableIdx};
3+
use crate::core::indices::FuncIdx;
44
use crate::core::reader::span::Span;
55
use crate::core::reader::types::TableType;
66
use crate::core::reader::WasmReader;
7+
use crate::core::utils::ToUsizeExt;
78
use crate::read_constant_expression::read_constant_expression;
89
use crate::validation_stack::ValidationStack;
910
use crate::{NumType, ValType, ValidationError};
@@ -230,8 +231,8 @@ impl ElemType {
230231
// start validating elemmode of form active {table x, offset expr}
231232
// 1-2. C.tables[x] must be defined with type: limits t
232233
let table_type = tables
233-
.get(x as usize)
234-
.ok_or(ValidationError::InvalidTableIdx(x as TableIdx))?
234+
.get(x.into_usize())
235+
.ok_or(ValidationError::InvalidTableIdx(x.into_usize()))?
235236
.et;
236237
if table_type != t {
237238
return Err(ValidationError::ActiveElementSegmentTypeMismatch);
@@ -325,11 +326,11 @@ fn parse_validate_shortened_initializer_list(
325326
) -> Result<ElemItems, ValidationError> {
326327
wasm.read_vec(|w| {
327328
let func_idx = w.read_var_u32()?;
328-
if num_funcs <= func_idx as usize {
329+
if num_funcs <= func_idx.into_usize() {
329330
// TODO fix error
330-
return Err(ValidationError::InvalidFuncIdx(func_idx as usize));
331+
return Err(ValidationError::InvalidFuncIdx(func_idx.into_usize()));
331332
}
332-
validation_context_refs.insert(func_idx as FuncIdx);
333+
validation_context_refs.insert(func_idx.into_usize());
333334
Ok(func_idx)
334335
})
335336
.map(ElemItems::RefFuncs)

src/core/reader/types/export.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloc::string::String;
44
use crate::core::indices::{FuncIdx, GlobalIdx, MemIdx, TableIdx};
55
use crate::core::reader::types::import::ImportDesc;
66
use crate::core::reader::WasmReader;
7+
use crate::core::utils::ToUsizeExt;
78
use crate::{ValidationError, ValidationInfo};
89

910
use super::ExternType;
@@ -158,7 +159,7 @@ impl ExportDesc {
158159
impl ExportDesc {
159160
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
160161
let desc_id = wasm.read_u8()?;
161-
let desc_idx = wasm.read_var_u32()? as usize;
162+
let desc_idx = wasm.read_var_u32()?.into_usize();
162163

163164
let desc = match desc_id {
164165
0x00 => ExportDesc::FuncIdx(desc_idx),

src/core/reader/types/import.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use alloc::string::String;
33

44
use crate::core::indices::TypeIdx;
55
use crate::core::reader::WasmReader;
6+
use crate::core::utils::ToUsizeExt;
67
use crate::{ValidationError, ValidationInfo};
78

89
use super::global::GlobalType;
@@ -43,7 +44,7 @@ pub enum ImportDesc {
4344
impl ImportDesc {
4445
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
4546
let desc = match wasm.read_u8()? {
46-
0x00 => Self::Func(wasm.read_var_u32()? as TypeIdx),
47+
0x00 => Self::Func(wasm.read_var_u32()?.into_usize()),
4748
// https://webassembly.github.io/spec/core/binary/types.html#table-types
4849
0x01 => Self::Table(TableType::read(wasm)?),
4950
0x02 => Self::Mem(MemType::read(wasm)?),

src/core/reader/types/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use alloc::vec::Vec;
66
use core::fmt::{Debug, Formatter};
77
use global::GlobalType;
88

9-
use crate::core::indices::TypeIdx;
109
use crate::core::reader::WasmReader;
10+
use crate::core::utils::ToUsizeExt;
1111
use crate::execution::assert_validated::UnwrapValidatedExt;
1212
use crate::ValidationError;
1313

@@ -163,7 +163,7 @@ pub enum BlockType {
163163

164164
impl BlockType {
165165
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
166-
if wasm.peek_u8()? as i8 == 0x40 {
166+
if wasm.peek_u8()? == 0x40 {
167167
// Empty block type
168168
let _ = wasm.read_u8().unwrap_validated();
169169
Ok(BlockType::Empty)
@@ -197,7 +197,7 @@ impl BlockType {
197197
},
198198
}),
199199
BlockType::Type(type_idx) => {
200-
let type_idx = *type_idx as TypeIdx;
200+
let type_idx = type_idx.into_usize();
201201
func_types
202202
.get(type_idx)
203203
.cloned()

src/core/reader/types/values.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use alloc::vec::Vec;
99

1010
use crate::core::reader::WasmReader;
11+
use crate::core::utils::ToUsizeExt;
1112
use crate::ValidationError;
1213

1314
/// Wasm encodes integers according to the LEB128 format, which specifies that
@@ -352,7 +353,7 @@ impl WasmReader<'_> {
352353

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

357358
let utf8_str = &self
358359
.full_wasm_binary
@@ -386,7 +387,7 @@ impl WasmReader<'_> {
386387
{
387388
let len = self.read_var_u32()?;
388389
core::iter::repeat_with(|| read_element(self))
389-
.take(len as usize)
390+
.take(len.into_usize())
390391
.collect()
391392
}
392393
}

src/execution/const_interpreter_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use crate::{
55
assert_validated::UnwrapValidatedExt,
66
config::Config,
77
core::{
8-
indices::GlobalIdx,
98
reader::{
109
span::Span,
1110
types::{FuncType, ResultType},
1211
WasmReader,
1312
},
13+
utils::ToUsizeExt,
1414
},
1515
unreachable_validated,
1616
value::{self, Ref},
@@ -51,7 +51,7 @@ pub(crate) fn run_const<T: Config>(
5151
break;
5252
}
5353
GLOBAL_GET => {
54-
let global_idx = wasm.read_var_u32().unwrap_validated() as GlobalIdx;
54+
let global_idx = wasm.read_var_u32().unwrap_validated().into_usize();
5555

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

0 commit comments

Comments
 (0)