Skip to content
5 changes: 3 additions & 2 deletions crates/wasm-encoder/src/core/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ impl CodeSection {
/// into a new code section encoder:
///
/// ```
/// # use wasmparser::{BinaryReader, CodeSectionReader};
/// # use wasmparser::{BinaryReader, CodeSectionReader, InMemData};
/// // id, size, # entries, entry
/// let code_section = [10, 6, 1, 4, 0, 65, 0, 11];
/// let code_section = InMemData::new(&code_section);
///
/// // Parse the code section.
/// let reader = BinaryReader::new(&code_section, 0);
Expand All @@ -100,7 +101,7 @@ impl CodeSection {
/// // Add the body to a new code section encoder by copying bytes rather
/// // than re-parsing and re-encoding it.
/// let mut encoder = wasm_encoder::CodeSection::new();
/// encoder.raw(&code_section[body_range.start..body_range.end]);
/// encoder.raw(&code_section[body_range]);
/// ```
pub fn raw(&mut self, data: &[u8]) -> &mut Self {
data.encode(&mut self.bytes);
Expand Down
8 changes: 5 additions & 3 deletions crates/wasm-encoder/src/reencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ pub mod utils {
use crate::{CoreTypeEncoder, Encode, Imports};
use alloc::vec::Vec;
use core::ops::Range;
use wasmparser::InMemData;

pub fn parse_core_module<T: ?Sized + Reencode>(
reencoder: &mut T,
Expand All @@ -685,9 +686,10 @@ pub mod utils {
// give us here) and recurse with that. This means that
// users overriding `parse_code_section` always get that
// function called.
let orig_offset = parser.offset() as usize;
let get_original_section = |range: Range<usize>| {
data.get(range.start - orig_offset..range.end - orig_offset)
let orig_offset = parser.offset();
let get_original_section = |range: Range<u64>| {
InMemData::new(data)
.get(range.start - orig_offset..range.end - orig_offset)
.ok_or(Error::InvalidCodeSectionSize)
};
let mut last_section = None;
Expand Down
7 changes: 5 additions & 2 deletions crates/wasm-encoder/src/reencode/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ pub mod component_utils {
use crate::reencode::Error;
use alloc::boxed::Box;
use alloc::vec::Vec;
use wasmparser::InMemData;

pub fn parse_component<T: ?Sized + ReencodeComponent>(
reencoder: &mut T,
Expand All @@ -414,7 +415,8 @@ pub mod component_utils {
| wasmparser::Payload::ModuleSection {
unchecked_range, ..
} => {
remaining = &remaining[unchecked_range.len()..];
let skipped_len = InMemData::range_len(unchecked_range);
remaining = &remaining[skipped_len..];
}
_ => {}
}
Expand All @@ -430,6 +432,7 @@ pub mod component_utils {
payload: wasmparser::Payload<'_>,
whole_component: &[u8],
) -> Result<(), Error<T::Error>> {
let whole_component = InMemData::new(whole_component);
match payload {
wasmparser::Payload::Version {
encoding: wasmparser::Encoding::Component,
Expand Down Expand Up @@ -515,7 +518,7 @@ pub mod component_utils {
component,
parser,
&whole_component[unchecked_range],
whole_component,
whole_component.data,
)?;
}
wasmparser::Payload::ComponentStartSection { start, range: _ } => {
Expand Down
3 changes: 2 additions & 1 deletion crates/wasm-metadata/src/add_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Producers, rewrite_wasm};
use anyhow::Result;
use std::fmt::Debug;
use wasmparser::InMemData;

/// Add metadata (module name, producers) to a WebAssembly file.
///
Expand Down Expand Up @@ -57,7 +58,7 @@ impl AddMetadata {
/// section created.
pub fn to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> {
let add_producers = Producers::from_meta(self);
rewrite_wasm(self, &add_producers, input)
rewrite_wasm(self, &add_producers, InMemData::new(input))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-metadata/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Metadata {
/// Version of the packaged software
pub version: Option<Version>,
/// Byte range of the module in the parent binary
pub range: Range<usize>,
pub range: Range<u64>,
/// Dependencies of the component
pub dependencies: Option<Dependencies>,
}
2 changes: 1 addition & 1 deletion crates/wasm-metadata/src/names/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> ComponentNames<'a> {
}
/// Read a component-name section from a WebAssembly binary. Records the component name, as
/// well as all other component name fields for later serialization.
pub fn from_bytes(bytes: &'a [u8], offset: usize) -> Result<ComponentNames<'a>> {
pub fn from_bytes(bytes: &'a [u8], offset: u64) -> Result<ComponentNames<'a>> {
let reader = BinaryReader::new(bytes, offset);
let section = ComponentNameSectionReader::new(reader);
let mut s = Self::empty();
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-metadata/src/names/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> ModuleNames<'a> {
}
/// Read a name section from a WebAssembly binary. Records the module name, and all other
/// contents of name section, for later serialization.
pub fn from_bytes(bytes: &'a [u8], offset: usize) -> Result<ModuleNames<'a>> {
pub fn from_bytes(bytes: &'a [u8], offset: u64) -> Result<ModuleNames<'a>> {
let reader = BinaryReader::new(bytes, offset);
let section = NameSectionReader::new(reader);
let mut s = Self::empty();
Expand Down
10 changes: 5 additions & 5 deletions crates/wasm-metadata/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops::Range;

use anyhow::Result;
use serde_derive::Serialize;
use wasmparser::{KnownCustom, Parser, Payload::*};
use wasmparser::{InMemData, KnownCustom, Parser, Payload::*};

use crate::{
Authors, ComponentNames, Description, Homepage, Licenses, Metadata, ModuleNames, Producers,
Expand Down Expand Up @@ -40,10 +40,10 @@ impl Payload {
if output.is_empty() {
match encoding {
wasmparser::Encoding::Module => {
output.push(Self::empty_module(0..input.len()))
output.push(Self::empty_module(InMemData::new(input).range()))
}
wasmparser::Encoding::Component => {
output.push(Self::empty_component(0..input.len()))
output.push(Self::empty_component(InMemData::new(input).range()))
}
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Payload {
}
}

fn empty_component(range: Range<usize>) -> Self {
fn empty_component(range: Range<u64>) -> Self {
let mut this = Self::Component {
metadata: Metadata::default(),
children: vec![],
Expand All @@ -200,7 +200,7 @@ impl Payload {
this
}

fn empty_module(range: Range<usize>) -> Self {
fn empty_module(range: Range<u64>) -> Self {
let mut this = Self::Module(Metadata::default());
this.metadata_mut().range = range;
this
Expand Down
6 changes: 3 additions & 3 deletions crates/wasm-metadata/src/producers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use indexmap::{IndexMap, map::Entry};
use wasm_encoder::Encode;
use wasmparser::{BinaryReader, KnownCustom, Parser, ProducersSectionReader};
use wasmparser::{BinaryReader, InMemData, KnownCustom, Parser, ProducersSectionReader};

use crate::{AddMetadata, rewrite_wasm};
/// A representation of a WebAssembly producers section.
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Producers {
Ok(None)
}
/// Read the producers section from a Wasm binary.
pub fn from_bytes(bytes: &[u8], offset: usize) -> Result<Self> {
pub fn from_bytes(bytes: &[u8], offset: u64) -> Result<Self> {
let reader = BinaryReader::new(bytes, offset);
let section = ProducersSectionReader::new(reader)?;
let mut fields = IndexMap::new();
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Producers {
/// Merge into an existing wasm module. Rewrites the module with this producers section
/// merged into its existing one, or adds this producers section if none is present.
pub fn add_to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> {
rewrite_wasm(&Default::default(), self, input)
rewrite_wasm(&Default::default(), self, InMemData::new(input))
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-metadata/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use anyhow::Result;
use std::mem;
use wasm_encoder::ComponentSection as _;
use wasm_encoder::{ComponentSectionId, Encode, Section};
use wasmparser::{KnownCustom, Parser, Payload::*};
use wasmparser::{InMemData, KnownCustom, Parser, Payload::*};

pub(crate) fn rewrite_wasm(
metadata: &AddMetadata,
add_producers: &Producers,
input: &[u8],
input: InMemData,
) -> Result<Vec<u8>> {
let mut producers_found = false;
let mut names_found = false;
Expand Down
40 changes: 20 additions & 20 deletions crates/wasm-mutate/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use std::collections::HashSet;
use std::ops::Range;
use wasm_encoder::{RawSection, SectionId};
use wasmparser::{BinaryReader, Chunk, Parser, Payload};
use wasmparser::{BinaryReader, Chunk, InMemData, Parser, Payload};

/// Provides module information for future usage during mutation
/// an instance of ModuleInfo could be user to determine which mutation could be applied
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct ModuleInfo<'a> {

// raw_sections
pub raw_sections: Vec<RawSection<'a>>,
pub input_wasm: &'a [u8],
pub input_wasm: InMemData<'a>,
}

impl<'a> ModuleInfo<'a> {
Expand All @@ -63,7 +63,7 @@ impl<'a> ModuleInfo<'a> {
let mut parser = Parser::new(0);
let mut info = ModuleInfo::default();
let mut wasm = input_wasm;
info.input_wasm = wasm;
info.input_wasm = InMemData::new(wasm);

loop {
let (payload, consumed) = match parser.parse(wasm, true)? {
Expand All @@ -79,16 +79,16 @@ impl<'a> ModuleInfo<'a> {
size: _,
} => {
info.code = Some(info.raw_sections.len());
info.section(SectionId::Code.into(), range.clone(), input_wasm);
info.section(SectionId::Code.into(), range.clone());
parser.skip_section();
// update slice, bypass the section
wasm = &input_wasm[range.end..];
wasm = info.input_wasm.index(range.end..);

continue;
}
Payload::TypeSection(reader) => {
info.types = Some(info.raw_sections.len());
info.section(SectionId::Type.into(), reader.range(), input_wasm);
info.section(SectionId::Type.into(), reader.range());

// Save function types
for ty in reader.into_iter_err_on_gc_types() {
Expand All @@ -97,7 +97,7 @@ impl<'a> ModuleInfo<'a> {
}
Payload::ImportSection(reader) => {
info.imports = Some(info.raw_sections.len());
info.section(SectionId::Import.into(), reader.range(), input_wasm);
info.section(SectionId::Import.into(), reader.range());

for ty in reader.into_imports() {
match ty?.ty {
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'a> ModuleInfo<'a> {
}
Payload::FunctionSection(reader) => {
info.functions = Some(info.raw_sections.len());
info.section(SectionId::Function.into(), reader.range(), input_wasm);
info.section(SectionId::Function.into(), reader.range());

for ty in reader {
info.function_map.push(ty?);
Expand All @@ -139,7 +139,7 @@ impl<'a> ModuleInfo<'a> {
Payload::TableSection(reader) => {
info.tables = Some(info.raw_sections.len());
info.table_count += reader.count();
info.section(SectionId::Table.into(), reader.range(), input_wasm);
info.section(SectionId::Table.into(), reader.range());

for table in reader {
let table = table?;
Expand All @@ -149,15 +149,15 @@ impl<'a> ModuleInfo<'a> {
Payload::MemorySection(reader) => {
info.memories = Some(info.raw_sections.len());
info.memory_count += reader.count();
info.section(SectionId::Memory.into(), reader.range(), input_wasm);
info.section(SectionId::Memory.into(), reader.range());

for ty in reader {
info.memory_types.push(ty?);
}
}
Payload::GlobalSection(reader) => {
info.globals = Some(info.raw_sections.len());
info.section(SectionId::Global.into(), reader.range(), input_wasm);
info.section(SectionId::Global.into(), reader.range());

for ty in reader {
let ty = ty?;
Expand All @@ -174,36 +174,36 @@ impl<'a> ModuleInfo<'a> {
info.export_names.insert(entry?.name.into());
}

info.section(SectionId::Export.into(), reader.range(), input_wasm);
info.section(SectionId::Export.into(), reader.range());
}
Payload::StartSection { func, range } => {
info.start = Some(info.raw_sections.len());
info.start_function = Some(func);
info.section(SectionId::Start.into(), range, input_wasm);
info.section(SectionId::Start.into(), range);
}
Payload::ElementSection(reader) => {
info.elements = Some(info.raw_sections.len());
info.elements_count = reader.count();
info.section(SectionId::Element.into(), reader.range(), input_wasm);
info.section(SectionId::Element.into(), reader.range());
}
Payload::DataSection(reader) => {
info.data = Some(info.raw_sections.len());
info.data_segments_count = reader.count();
info.section(SectionId::Data.into(), reader.range(), input_wasm);
info.section(SectionId::Data.into(), reader.range());
}
Payload::CustomSection(c) => {
info.section(SectionId::Custom.into(), c.range(), input_wasm);
info.section(SectionId::Custom.into(), c.range());
}
Payload::UnknownSection {
id,
contents: _,
range,
} => {
info.section(id, range, input_wasm);
info.section(id, range);
}
Payload::DataCountSection { count: _, range } => {
info.data_count = Some(info.raw_sections.len());
info.section(SectionId::DataCount.into(), range, input_wasm);
info.section(SectionId::DataCount.into(), range);
}
Payload::Version { .. } => {}
Payload::End(_) => {
Expand Down Expand Up @@ -241,10 +241,10 @@ impl<'a> ModuleInfo<'a> {
}

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

Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-mutate/src/mutators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub trait Mutator {
}

/// Type helper to wrap operator and the byte offset in the code section of a Wasm module
pub type OperatorAndByteOffset<'a> = (Operator<'a>, usize);
pub type OperatorAndByteOffset<'a> = (Operator<'a>, u64);

#[cfg(test)]
fn match_mutation<T>(original: &str, mutator: T, expected: &str)
Expand Down
Loading
Loading