Skip to content

Commit

Permalink
fix: fixed clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
nerodesu017 committed Jun 25, 2024
1 parent 71c7aba commit 86c5b58
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion examples/stuff/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> ExitCode {
(export "add" (func $add))
)
"#;
let wasm_bytes = wat::parse_str(&wat).unwrap();
let wasm_bytes = wat::parse_str(wat).unwrap();

let validation_info = match validate(&wasm_bytes) {
Ok(table) => table,
Expand Down
3 changes: 1 addition & 2 deletions src/core/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::core::indices::GlobalIdx;
use core::fmt::{Display, Formatter, Write};
use core::fmt::{Display, Formatter};
use core::str::Utf8Error;

use crate::core::reader::section_header::SectionTy;
use crate::core::reader::types::ValType;
use crate::execution::store::GlobalInst;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Error {
Expand Down
4 changes: 2 additions & 2 deletions src/core/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<'a> WasmReader<'a> {
}

pub fn remaining_bytes(&self) -> &[u8] {
&self.current
self.current
}

pub fn current_idx(&self) -> usize {
Expand All @@ -48,7 +48,7 @@ impl<'a> WasmReader<'a> {
Ok(bytes.try_into().expect("the slice length to be exactly N"))
}
pub fn peek_u8(&self) -> Result<u8> {
self.current.get(0).copied().ok_or(Error::Eof)
self.current.first().copied().ok_or(Error::Eof)
}

pub fn measure_num_read_bytes<T>(
Expand Down
10 changes: 5 additions & 5 deletions src/core/reader/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ impl ValType {

impl WasmReadable for ValType {
fn read(wasm: &mut WasmReader) -> Result<Self> {
let numtype = NumType::read(wasm).map(|ty| ValType::NumType(ty));
let numtype = NumType::read(wasm).map(ValType::NumType);
let vectype = VecType::read(wasm).map(|_ty| ValType::VecType);
let reftype = RefType::read(wasm).map(|ty| ValType::RefType(ty));
let reftype = RefType::read(wasm).map(ValType::RefType);

numtype
.or(vectype)
Expand All @@ -137,9 +137,9 @@ impl WasmReadable for ValType {
}

fn read_unvalidated(wasm: &mut WasmReader) -> Self {
let numtype = NumType::read(wasm).map(|ty| ValType::NumType(ty));
let numtype = NumType::read(wasm).map(ValType::NumType);
let vectype = VecType::read(wasm).map(|_ty| ValType::VecType);
let reftype = RefType::read(wasm).map(|ty| ValType::RefType(ty));
let reftype = RefType::read(wasm).map(ValType::RefType);

numtype.or(vectype).or(reftype).unwrap_validated()
}
Expand All @@ -153,7 +153,7 @@ pub struct ResultType {

impl WasmReadable for ResultType {
fn read(wasm: &mut WasmReader) -> Result<Self> {
let valtypes = wasm.read_vec(|wasm| ValType::read(wasm))?;
let valtypes = wasm.read_vec(ValType::read)?;

Ok(ResultType { valtypes })
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/reader/types/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use crate::{Error, Result};
impl WasmReader<'_> {
/// Note: If `Err`, the [Wasm] object is no longer guaranteed to be in a valid state
pub fn read_u8(&mut self) -> Result<u8> {
let value = *self.current.get(0).ok_or(Error::Eof)?;
let value = *self.current.first().ok_or(Error::Eof)?;

self.current = &self
self.current = self
.current
.get(1..)
.expect("slice to contain at least 1 element");
Expand Down Expand Up @@ -72,7 +72,7 @@ impl WasmReader<'_> {
let (utf8_str, rest) = self.current.split_at(len); // Cannot panic because check is done above
self.current = rest;

core::str::from_utf8(utf8_str).map_err(|err| Error::MalformedUtf8String(err))
core::str::from_utf8(utf8_str).map_err(Error::MalformedUtf8String)
}

pub fn read_vec_enumerated<T, F>(&mut self, mut read_element: F) -> Result<Vec<T>>
Expand Down
2 changes: 1 addition & 1 deletion src/execution/assert_validated.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Helpers for assertions due to prior validation of a WASM program.
use core::fmt::{Debug, Display};
use core::fmt::{Debug};

pub(crate) trait UnwrapValidatedExt<T> {
fn unwrap_validated(self) -> T;
Expand Down
16 changes: 8 additions & 8 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::execution::locals::Locals;
use crate::execution::store::{FuncInst, GlobalInst, MemInst, Store};
use crate::execution::value::Value;
use crate::validation::code::read_declared_locals;
use crate::value::{InteropValue, InteropValueList};
use crate::value::InteropValueList;
use crate::{Result, ValidationInfo};

// TODO
Expand All @@ -32,7 +32,7 @@ impl<'b> RuntimeInstance<'b> {
pub fn new(validation_info: &'_ ValidationInfo<'b>) -> Result<Self> {
trace!("Starting instantiation of bytecode");

let store = Self::init_store(&validation_info);
let store = Self::init_store(validation_info);

let mut instance = RuntimeInstance {
wasm_bytecode: validation_info.wasm,
Expand All @@ -50,7 +50,7 @@ impl<'b> RuntimeInstance<'b> {
pub fn invoke_func<Param: InteropValueList, Returns: InteropValueList>(
&mut self,
func_idx: FuncIdx,
mut param: Param,
param: Param,
) -> Returns {
let func_inst = self.store.funcs.get(func_idx).expect("valid FuncIdx");
let func_ty = self.types.get(func_inst.ty).unwrap_validated();
Expand All @@ -73,9 +73,9 @@ impl<'b> RuntimeInstance<'b> {
self.function(func_idx, &mut stack);

// Pop return values from stack
let mut return_values = Returns::TYS
let return_values = Returns::TYS
.iter()
.map(|ty| stack.pop_value(ty.clone()))
.map(|ty| stack.pop_value(*ty))
.collect::<Vec<Value>>();

// Values are reversed because they were popped from stack one-by-one. Now reverse them back
Expand Down Expand Up @@ -147,7 +147,7 @@ impl<'b> RuntimeInstance<'b> {
let relative_address: u32 =
stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = self.store.mems.get(0).unwrap_validated(); // there is only one memory allowed as of now
let mem = self.store.mems.first().unwrap_validated(); // there is only one memory allowed as of now

let data: u32 = {
// The spec states that this should be a 33 bit integer
Expand Down Expand Up @@ -227,7 +227,7 @@ impl<'b> RuntimeInstance<'b> {
wasm_reader.move_start_to(*func);

let (locals, bytes_read) = wasm_reader
.measure_num_read_bytes(|wasm| read_declared_locals(wasm))
.measure_num_read_bytes(read_declared_locals)
.unwrap_validated();

let code_expr = wasm_reader.make_span(func.len() - bytes_read);
Expand All @@ -244,7 +244,7 @@ impl<'b> RuntimeInstance<'b> {
let memory_instances: Vec<MemInst> = validation_info
.memories
.iter()
.map(|ty| MemInst::new(ty.clone()))
.map(|ty| MemInst::new(*ty))
.collect();

let global_instances: Vec<GlobalInst> = validation_info
Expand Down
2 changes: 1 addition & 1 deletion src/execution/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl InteropValueList for () {
}

fn from_values(_values: impl Iterator<Item = Value>) -> Self {
()

}
}

Expand Down
4 changes: 2 additions & 2 deletions src/validation/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn read_instructions(
0x20 => {
let local_idx = wasm.read_var_u32()? as LocalIdx;
let local_ty = locals.get(local_idx).ok_or(Error::InvalidLocalIdx)?;
value_stack.push_back(local_ty.clone());
value_stack.push_back(*local_ty);
}
// local.set [t] -> [0]
0x21 => {
Expand Down Expand Up @@ -180,7 +180,7 @@ fn read_instructions(
}
}

fn validate_value_stack<F>(return_ty: ResultType, mut f: F) -> Result<()>
fn validate_value_stack<F>(return_ty: ResultType, f: F) -> Result<()>
where
F: FnOnce(&mut VecDeque<ValType>) -> Result<()>,
{
Expand Down
44 changes: 22 additions & 22 deletions src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,89 +46,89 @@ pub fn validate(wasm: &[u8]) -> Result<ValidationInfo> {
let mut header = None;
read_next_header(&mut wasm, &mut header)?;

let mut skip_section = |wasm: &mut WasmReader, section_header: &mut Option<SectionHeader>| {
let skip_section = |wasm: &mut WasmReader, section_header: &mut Option<SectionHeader>| {
handle_section(wasm, section_header, SectionTy::Custom, |wasm, h| {
wasm.skip(h.contents.len())
})
};

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let types = handle_section(&mut wasm, &mut header, SectionTy::Type, |wasm, _| {
wasm.read_vec(|wasm| FuncType::read(wasm))
wasm.read_vec(FuncType::read)
})?
.unwrap_or_default();

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let imports = handle_section(&mut wasm, &mut header, SectionTy::Import, |wasm, _| {
wasm.read_vec(|wasm| Import::read(wasm))
wasm.read_vec(Import::read)
})?
.unwrap_or_default();

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let functions = handle_section(&mut wasm, &mut header, SectionTy::Function, |wasm, _| {
wasm.read_vec(|wasm| wasm.read_var_u32().map(|u| u as usize))
})?
.unwrap_or_default();

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let tables = handle_section(&mut wasm, &mut header, SectionTy::Table, |wasm, _| {
wasm.read_vec(|wasm| TableType::read(wasm))
wasm.read_vec(TableType::read)
})?
.unwrap_or_default();

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let memories = handle_section(&mut wasm, &mut header, SectionTy::Memory, |wasm, _| {
wasm.read_vec(|wasm| MemType::read(wasm))
wasm.read_vec(MemType::read)
})?
.unwrap_or_default();
if memories.len() > 1 {
return Err(Error::MoreThanOneMemory);
}

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let globals = handle_section(&mut wasm, &mut header, SectionTy::Global, |wasm, _| {
wasm.read_vec(|wasm| {
let global = Global::read(wasm);

// TODO validate instructions in `global.init_expr`. Furthermore all of these instructions need to be constant.
// See https://webassembly.github.io/spec/core/valid/instructions.html#valid-constant
// Maybe we can also execute constant expressions right here so they do not even exist in the runtime environment. <-- Needs further research to check if this is even possible
global
Global::read(wasm)
})
})?
.unwrap_or_default();

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let exports = handle_section(&mut wasm, &mut header, SectionTy::Export, |wasm, _| {
wasm.read_vec(|wasm| Export::read(wasm))
wasm.read_vec(Export::read)
})?
.unwrap_or_default();

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let start = handle_section(&mut wasm, &mut header, SectionTy::Start, |wasm, _| {
wasm.read_var_u32().map(|idx| idx as FuncIdx)
})?;

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let _: Option<()> = handle_section(&mut wasm, &mut header, SectionTy::Element, |_, _| {
todo!("element section not yet supported")
})?;

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let _: Option<()> = handle_section(&mut wasm, &mut header, SectionTy::DataCount, |_, _| {
todo!("data count section not yet supported")
})?;

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let func_blocks = handle_section(&mut wasm, &mut header, SectionTy::Code, |wasm, h| {
code::validate_code_section(wasm, h, &types, &globals)
Expand All @@ -137,13 +137,13 @@ pub fn validate(wasm: &[u8]) -> Result<ValidationInfo> {

assert_eq!(func_blocks.len(), functions.len(), "these should be equal"); // TODO check if this is in the spec

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

let _: Option<()> = handle_section(&mut wasm, &mut header, SectionTy::Data, |_, _| {
todo!("data section not yet supported")
})?;

while let Some(_) = skip_section(&mut wasm, &mut header)? {}
while (skip_section(&mut wasm, &mut header)?).is_some() {}

// All sections should have been handled
if let Some(header) = header {
Expand All @@ -166,7 +166,7 @@ pub fn validate(wasm: &[u8]) -> Result<ValidationInfo> {
}

fn read_next_header(wasm: &mut WasmReader, header: &mut Option<SectionHeader>) -> Result<()> {
if header.is_none() && wasm.remaining_bytes().len() > 0 {
if header.is_none() && !wasm.remaining_bytes().is_empty() {
*header = Some(SectionHeader::read(wasm)?);
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/add_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn add_one() {
i32.add)
)
"#;
let wasm_bytes = wat::parse_str(&wat).unwrap();
let wasm_bytes = wat::parse_str(wat).unwrap();

let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn basic_memory() {
i32.load)
)
"#;
let wasm_bytes = wat::parse_str(&wat).unwrap();
let wasm_bytes = wat::parse_str(wat).unwrap();

let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");
Expand Down
2 changes: 1 addition & 1 deletion tests/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn globals() {
global.get $my_global)
)
"#;
let wasm_bytes = wat::parse_str(&wat).unwrap();
let wasm_bytes = wat::parse_str(wat).unwrap();

let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");
Expand Down
2 changes: 1 addition & 1 deletion tests/start_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn start_function() {
i32.load)
)
"#;
let wasm_bytes = wat::parse_str(&wat).unwrap();
let wasm_bytes = wat::parse_str(wat).unwrap();

let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");
Expand Down

0 comments on commit 86c5b58

Please sign in to comment.