Skip to content

Commit

Permalink
Merge pull request #556 from alexcrichton/use-std-range-type
Browse files Browse the repository at this point in the history
Use std::ops::Range instead of a custom type
  • Loading branch information
fitzgen authored Apr 15, 2022
2 parents 519d152 + 478ddaa commit ff5025e
Show file tree
Hide file tree
Showing 35 changed files with 158 additions and 190 deletions.
7 changes: 4 additions & 3 deletions crates/wasm-mutate/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};
use std::collections::HashSet;
use std::convert::TryFrom;
use std::ops::Range;
use wasm_encoder::{RawSection, SectionId};
use wasmparser::{Chunk, Parser, Payload, SectionReader};

Expand Down Expand Up @@ -79,7 +80,7 @@ impl<'a> ModuleInfo<'a> {
size: _,
} => {
info.code = Some(info.raw_sections.len());
info.section(SectionId::Code.into(), range, input_wasm);
info.section(SectionId::Code.into(), range.clone(), input_wasm);
parser.skip_section();
// update slice, bypass the section
wasm = &input_wasm[range.end..];
Expand Down Expand Up @@ -249,10 +250,10 @@ impl<'a> ModuleInfo<'a> {
}

/// Registers a new raw_section in the ModuleInfo
pub fn section(&mut self, id: u8, range: wasmparser::Range, full_wasm: &'a [u8]) {
pub fn section(&mut self, id: u8, range: Range<usize>, full_wasm: &'a [u8]) {
self.raw_sections.push(RawSection {
id,
data: &full_wasm[range.start..range.end],
data: &full_wasm[range],
});
}

Expand Down
17 changes: 9 additions & 8 deletions crates/wasm-mutate/src/mutators/codemotion/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::{
module::map_block_type,
mutators::{codemotion::ir::parse_context::ParseContext, OperatorAndByteOffset},
};
use std::ops::Range;
use wasm_encoder::{Function, Instruction};
use wasmparser::{BlockType, Operator, Range};
use wasmparser::{BlockType, Operator};

use self::parse_context::{Ast, Node, State};

Expand Down Expand Up @@ -169,7 +170,7 @@ pub trait AstWriter {
&self,
_ast: &Ast,
_nodeidx: usize,
range: Range,
range: Range<usize>,
newfunc: &mut Function,
operators: &Vec<OperatorAndByteOffset>,
input_wasm: &'a [u8],
Expand All @@ -192,7 +193,7 @@ pub trait AstWriter {
&self,
ast: &Ast,
nodeidx: usize,
range: Range,
range: Range<usize>,
newfunc: &mut Function,
operators: &Vec<OperatorAndByteOffset>,
input_wasm: &'a [u8],
Expand Down Expand Up @@ -232,7 +233,7 @@ pub trait AstWriter {
)?;
}
Node::Code { range } => {
self.write_code(ast, nodeidx, *range, newfunc, operators, input_wasm)?;
self.write_code(ast, nodeidx, range.clone(), newfunc, operators, input_wasm)?;
}
Node::Loop { body, ty, range: _ } => {
self.write_loop(ast, nodeidx, body, newfunc, operators, input_wasm, ty)?
Expand Down Expand Up @@ -319,7 +320,7 @@ impl AstBuilder {
consequent: then_branch,
alternative: None,
ty: ty.expect("Missing if type"),
range: Range::new(frame_start, idx),
range: frame_start..idx,
});
}
State::Else => {
Expand All @@ -337,7 +338,7 @@ impl AstBuilder {
consequent: then_branch,
alternative: Some(else_branch),
ty: ty.expect("Missing if type"),
range: Range::new(if_start, idx),
range: if_start..idx,
});
}
State::Loop => {
Expand All @@ -347,7 +348,7 @@ impl AstBuilder {
parse_context.push_node_to_current_parsing(Node::Loop {
body: children,
ty: ty.expect("Missing block type for loop"),
range: Range::new(frame_start, idx),
range: frame_start..idx,
});
}
State::Block => {
Expand All @@ -357,7 +358,7 @@ impl AstBuilder {
parse_context.push_node_to_current_parsing(Node::Block {
body: children,
ty: ty.expect("Missing block type for loop"),
range: Range::new(frame_start, idx),
range: frame_start..idx,
});
}
State::Root => {
Expand Down
19 changes: 10 additions & 9 deletions crates/wasm-mutate/src/mutators/codemotion/ir/parse_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{Error, Result};
use wasmparser::{BlockType, Range};
use std::ops::Range;
use wasmparser::BlockType;

#[derive(Debug, Default)]
pub struct Ast {
Expand Down Expand Up @@ -50,12 +51,12 @@ pub enum Node {
alternative: Option<Vec<usize>>,
/// The block type for the branches.
ty: BlockType,
range: Range,
range: Range<usize>,
},
/// Code node
Code {
/// Range on the instructions stream
range: Range,
range: Range<usize>,
},
/// Loop Node
Loop {
Expand All @@ -64,7 +65,7 @@ pub enum Node {
/// Block type
ty: BlockType,
/// Range on the instructions stream
range: Range,
range: Range<usize>,
},
/// Block Node
Block {
Expand All @@ -73,7 +74,7 @@ pub enum Node {
/// Block type
ty: BlockType,
/// Range on the instructions stream
range: Range,
range: Range<usize>,
},
/// Special node to wrap the root nodes of the Ast
Root(Vec<usize>),
Expand All @@ -93,7 +94,7 @@ pub(crate) struct ParseContext {
current_parsing: Vec<usize>,
stack: Vec<Vec<usize>>,
frames: Vec<(State, Option<BlockType>, usize)>,
current_code_range: Range,
current_code_range: Range<usize>,
nodes: Vec<Node>,

ifs: Vec<usize>,
Expand All @@ -104,7 +105,7 @@ pub(crate) struct ParseContext {
impl Default for ParseContext {
fn default() -> Self {
ParseContext {
current_code_range: Range::new(0, 0),
current_code_range: 0..0,
current_parsing: Vec::new(),
stack: Vec::new(),
frames: Vec::new(),
Expand Down Expand Up @@ -185,13 +186,13 @@ impl ParseContext {
/// Pushes the current code parsing as a `Node::Code` instance
pub fn push_current_code_as_node(&mut self) -> usize {
self.push_node_to_current_parsing(Node::Code {
range: self.current_code_range,
range: self.current_code_range.clone(),
})
}

/// Resets the current code parsing
pub fn reset_code_range_at(&mut self, idx: usize) {
self.current_code_range = Range::new(idx, idx);
self.current_code_range = idx..idx;
}

/// Augmnents current code parsing to include the next instruction
Expand Down
3 changes: 2 additions & 1 deletion crates/wasm-mutate/src/mutators/peephole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::{
};
use egg::{Rewrite, Runner};
use rand::{prelude::SmallRng, Rng};
use std::ops::Range;
use std::{borrow::Cow, fmt::Debug};
use wasm_encoder::{CodeSection, Function, GlobalSection, Instruction, Module, ValType};
use wasmparser::{CodeSectionReader, FunctionBody, GlobalSectionReader, LocalsReader};
Expand Down Expand Up @@ -467,7 +468,7 @@ pub(crate) trait CodeMutator {
operator_index: usize,
operators: Vec<OperatorAndByteOffset>,
funcreader: FunctionBody,
body_range: wasmparser::Range,
body_range: Range<usize>,
function_data: &[u8],
) -> Result<Function>;

Expand Down
11 changes: 5 additions & 6 deletions crates/wasm-mutate/src/mutators/peephole/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::mutators::OperatorAndByteOffset;
use crate::{ModuleInfo, WasmMutate};
use egg::{Id, Language, RecExpr};
use std::collections::HashMap;
use wasmparser::{Operator, Range};
use std::ops::Range;
use wasmparser::Operator;

/// It executes a minimal symbolic evaluation of the stack to detect operands
/// location in the code for certain operators
Expand All @@ -34,7 +35,7 @@ pub struct DFGBuilder {
/// function
#[derive(Debug)]
pub struct BBlock {
pub(crate) range: Range,
pub(crate) range: Range<usize>,
}

/// Node of a DFG extracted from a basic block in the Wasm code
Expand Down Expand Up @@ -226,10 +227,8 @@ impl<'a> DFGBuilder {
operator_index: usize,
operators: &[OperatorAndByteOffset],
) -> Option<BBlock> {
let mut range = Range {
start: operator_index,
end: operator_index + 1, // The range is inclusive in the last operator
};
// The range is inclusive in the last operator
let mut range = operator_index..operator_index + 1;
// We only need the basic block upward
let mut found = false;
loop {
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-mutate/src/mutators/peephole/eggsy/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Encoder {
egraph: &EG,
) -> crate::Result<Vec<ResourceRequest>> {
// Copy previous code
let range = basicblock.range;
let range = basicblock.range.clone();
let byterange = (&operators[0].1, &operators[range.start].1);
let bytes = &config.info().get_code_section().data[*byterange.0..*byterange.1];
newfunc.raw(bytes.iter().copied());
Expand All @@ -60,7 +60,7 @@ impl Encoder {
}

// Copy remaining function
let range = basicblock.range;
let range = basicblock.range.clone();
let byterange = (
&operators[range.end].1, // In the worst case the next instruction will be and end
&operators[operators.len() - 1].1,
Expand Down
33 changes: 3 additions & 30 deletions crates/wasmparser/src/binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{ComponentArg, ComponentArgKind};
use std::convert::TryInto;
use std::error::Error;
use std::fmt;
use std::ops::Range;
use std::str;

fn is_name(name: &str, expected: &'static str) -> bool {
Expand All @@ -38,31 +39,6 @@ fn is_name_prefix(name: &str, prefix: &'static str) -> bool {

const WASM_MAGIC_NUMBER: &[u8; 4] = b"\0asm";

/// Bytecode range in the WebAssembly module.
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Range {
/// The start bound of the range.
pub start: usize,
/// The end bound of the range.
pub end: usize,
}

impl Range {
/// Constructs a new instance of `Range`.
///
/// # Panics
/// If `start` is greater than `end`.
pub fn new(start: usize, end: usize) -> Range {
assert!(start <= end);
Range { start, end }
}

/// Returns a new slice between `start` and `end - 1` from `data`.
pub fn slice<'a>(&self, data: &'a [u8]) -> &'a [u8] {
&data[self.start..self.end]
}
}

/// A binary reader for WebAssembly modules.
#[derive(Debug, Clone)]
pub struct BinaryReaderError {
Expand Down Expand Up @@ -181,11 +157,8 @@ impl<'a> BinaryReader<'a> {
}

/// Returns a range from the starting offset to the end of the buffer.
pub fn range(&self) -> Range {
Range {
start: self.original_offset,
end: self.original_offset + self.buffer.len(),
}
pub fn range(&self) -> Range<usize> {
self.original_offset..self.original_offset + self.buffer.len()
}

pub(crate) fn remaining_buffer(&self) -> &'a [u8] {
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmparser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#![deny(missing_docs)]

pub use crate::binary_reader::{BinaryReader, BinaryReaderError, Range, Result};
pub use crate::binary_reader::{BinaryReader, BinaryReaderError, Result};
pub use crate::module_resources::*;
pub use crate::parser::*;
pub use crate::readers::*;
Expand Down
Loading

0 comments on commit ff5025e

Please sign in to comment.