Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/wasm-compose/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ impl<'a> Component<'a> {
let mut cur = bytes.as_ref();
loop {
match parser.parse(cur, true)? {
Chunk::Parsed { payload, consumed } => {
Chunk::Parsed {
payload,
consumed,
offset: _,
} => {
cur = &cur[consumed..];

match validator.payload(&payload)? {
Expand Down
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, OffsetConverter};
/// // id, size, # entries, entry
/// let code_section = [10, 6, 1, 4, 0, 65, 0, 11];
/// let offset = OffsetConverter::from_start(0);
///
/// // 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[offset.convert_range(&body_range)]);
/// ```
pub fn raw(&mut self, data: &[u8]) -> &mut Self {
data.encode(&mut self.bytes);
Expand Down
14 changes: 8 additions & 6 deletions crates/wasm-encoder/src/reencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,19 +679,21 @@ pub mod utils {
reencoder.intersperse_section_hook(module, after, before)
}

let mut last_section = None;

let offset = wasmparser::OffsetConverter::from_start(parser.offset());
// Convert from `range` to a byte range within `data` while
// accounting for various offsets. Then create a
// `CodeSectionReader` (which notably the payload does not
// 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)
.ok_or(Error::InvalidCodeSectionSize)
let get_original_section = |range: Range<u64>| {
let data_range = offset
.try_convert_range(&range)
.ok_or(Error::InvalidCodeSectionSize)?;
data.get(data_range).ok_or(Error::InvalidCodeSectionSize)
};
let mut last_section = None;

for section in parser.parse_all(data) {
match section? {
wasmparser::Payload::Version {
Expand Down
22 changes: 15 additions & 7 deletions crates/wasm-encoder/src/reencode/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub trait ReencodeComponent: Reencode {
parser: wasmparser::Parser,
data: &[u8],
) -> Result<(), Error<Self::Error>> {
// so we can slice into the data with the offsets from the parser
assert_eq!(parser.offset(), 0, "data must be parsed at offset 0");
component_utils::parse_component(self, component, parser, data, data)
}

Expand Down Expand Up @@ -400,10 +402,14 @@ pub mod component_utils {
) -> Result<(), Error<T::Error>> {
let mut remaining = data;
while !remaining.is_empty() {
let section = match parser.parse(remaining, true)? {
wasmparser::Chunk::Parsed { consumed, payload } => {
let (section, offset) = match parser.parse(remaining, true)? {
wasmparser::Chunk::Parsed {
consumed,
payload,
offset,
} => {
remaining = &remaining[consumed..];
payload
(payload, offset)
}
wasmparser::Chunk::NeedMoreData(_) => unreachable!(),
};
Expand All @@ -414,7 +420,8 @@ pub mod component_utils {
| wasmparser::Payload::ModuleSection {
unchecked_range, ..
} => {
remaining = &remaining[unchecked_range.len()..];
let skipped_len = offset.convert_range(unchecked_range).len();
remaining = &remaining[skipped_len..];
}
_ => {}
}
Expand All @@ -430,6 +437,7 @@ pub mod component_utils {
payload: wasmparser::Payload<'_>,
whole_component: &[u8],
) -> Result<(), Error<T::Error>> {
let offset = wasmparser::OffsetConverter::from_start(0);
match payload {
wasmparser::Payload::Version {
encoding: wasmparser::Encoding::Component,
Expand Down Expand Up @@ -504,7 +512,7 @@ pub mod component_utils {
reencoder.parse_component_submodule(
component,
parser,
&whole_component[unchecked_range],
&whole_component[offset.convert_range(&unchecked_range)],
)?;
}
wasmparser::Payload::ComponentSection {
Expand All @@ -514,7 +522,7 @@ pub mod component_utils {
reencoder.parse_component_subcomponent(
component,
parser,
&whole_component[unchecked_range],
&whole_component[offset.convert_range(&unchecked_range)],
whole_component,
)?;
}
Expand All @@ -525,7 +533,7 @@ pub mod component_utils {

other => match other.as_section() {
Some((id, range)) => {
let section = &whole_component[range];
let section = &whole_component[offset.convert_range(&range)];
reencoder.parse_unknown_component_section(component, id, section)?;
}
None => unreachable!(),
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
11 changes: 6 additions & 5 deletions crates/wasm-metadata/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ impl Payload {
let mut output = Vec::new();

for payload in Parser::new(0).parse_all(&input) {
match payload? {
let payload = payload?;
match payload {
Version { encoding, .. } => {
if output.is_empty() {
match encoding {
wasmparser::Encoding::Module => {
output.push(Self::empty_module(0..input.len()))
output.push(Self::empty_module(0..input.len() as u64))
}
wasmparser::Encoding::Component => {
output.push(Self::empty_component(0..input.len()))
output.push(Self::empty_component(0..input.len() as u64))
}
}
}
Expand Down Expand Up @@ -191,7 +192,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 +201,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
5 changes: 2 additions & 3 deletions crates/wasm-metadata/src/producers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ impl Producers {
pub fn from_wasm(bytes: &[u8]) -> Result<Option<Self>> {
let mut depth = 0;
for payload in Parser::new(0).parse_all(bytes) {
let payload = payload?;
use wasmparser::Payload::*;
match payload {
match payload? {
ModuleSection { .. } | ComponentSection { .. } => depth += 1,
End { .. } => depth -= 1,
CustomSection(c) if depth == 0 => {
Expand All @@ -58,7 +57,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
4 changes: 2 additions & 2 deletions crates/wasm-metadata/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) fn rewrite_wasm(
let mut names_found = false;
let mut stack = Vec::new();
let mut output = Vec::new();
for payload in Parser::new(0).parse_all(&input) {
for payload in Parser::new(0).parse_all(input) {
let payload = payload?;

// Track nesting depth, so that we don't mess with inner producer sections:
Expand Down Expand Up @@ -169,7 +169,7 @@ pub(crate) fn rewrite_wasm(
if let Some((id, range)) = payload.as_section() {
wasm_encoder::RawSection {
id,
data: &input[range],
data: &input[range.start as usize..range.end as usize],
}
.append_to(&mut output);
}
Expand Down
Loading
Loading