Skip to content

Commit 37c0b66

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

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
@@ -1,6 +1,7 @@
11
use crate::core::indices::{FuncIdx, GlobalIdx, MemIdx, TableIdx};
22
use crate::core::reader::types::import::ImportDesc;
33
use crate::core::reader::WasmReader;
4+
use crate::core::utils::ToUsizeExt;
45
use crate::{ValidationError, ValidationInfo};
56

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

125126
let desc = match desc_id {
126127
0x00 => ExportDesc::Func(desc_idx),

src/core/reader/types/import.rs

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

56
use super::global::GlobalType;
@@ -37,7 +38,7 @@ pub enum ImportDesc {
3738
impl ImportDesc {
3839
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
3940
let desc = match wasm.read_u8()? {
40-
0x00 => Self::Func(wasm.read_var_u32()? as TypeIdx),
41+
0x00 => Self::Func(wasm.read_var_u32()?.into_usize()),
4142
// https://webassembly.github.io/spec/core/binary/types.html#table-types
4243
0x01 => Self::Table(TableType::read(wasm)?),
4344
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

@@ -162,7 +162,7 @@ pub enum BlockType {
162162

163163
impl BlockType {
164164
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
165-
if wasm.peek_u8()? as i8 == 0x40 {
165+
if wasm.peek_u8()? == 0x40 {
166166
// Empty block type
167167
let _ = wasm.read_u8().unwrap_validated();
168168
Ok(BlockType::Empty)
@@ -196,7 +196,7 @@ impl BlockType {
196196
},
197197
}),
198198
BlockType::Type(type_idx) => {
199-
let type_idx = *type_idx as TypeIdx;
199+
let type_idx = type_idx.into_usize();
200200
func_types
201201
.get(type_idx)
202202
.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<'wasm> WasmReader<'wasm> {
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<&'wasm 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
@@ -388,7 +389,7 @@ impl<'wasm> WasmReader<'wasm> {
388389
{
389390
let len = self.read_var_u32()?;
390391
core::iter::repeat_with(|| read_element(self))
391-
.take(len as usize)
392+
.take(len.into_usize())
392393
.collect()
393394
}
394395
}

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)