Skip to content

Commit

Permalink
Merge pull request #649 from philipc/wasm
Browse files Browse the repository at this point in the history
read/wasm: multiple fixes
  • Loading branch information
philipc authored Mar 23, 2024
2 parents 90f6573 + 6b4adc8 commit bd15e0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl FileKind {
#[cfg(feature = "macho")]
[0xca, 0xfe, 0xba, 0xbf, ..] => FileKind::MachOFat64,
#[cfg(feature = "wasm")]
[0x00, b'a', b's', b'm', ..] => FileKind::Wasm,
[0x00, b'a', b's', b'm', _, _, 0x00, 0x00] => FileKind::Wasm,
#[cfg(feature = "pe")]
[b'M', b'Z', ..] if offset == 0 => {
// offset == 0 restriction is because optional_header_magic only looks at offset 0
Expand Down
22 changes: 18 additions & 4 deletions src/read/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ enum SectionId {
Code = 10,
Data = 11,
DataCount = 12,
Tag = 13,
}
// Update this constant when adding new section id:
const MAX_SECTION_ID: usize = SectionId::DataCount as usize;
const MAX_SECTION_ID: usize = SectionId::Tag as usize;

/// A WebAssembly object file.
#[derive(Debug)]
Expand Down Expand Up @@ -113,6 +114,11 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> {
let payload = payload.read_error("Invalid Wasm section header")?;

match payload {
wp::Payload::Version { encoding, .. } => {
if encoding != wp::Encoding::Module {
return Err(Error("Unsupported Wasm encoding"));
}
}
wp::Payload::TypeSection(section) => {
file.add_section(SectionId::Type, section.range(), "");
}
Expand Down Expand Up @@ -205,8 +211,9 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> {
if let Some(local_func_id) =
export.index.checked_sub(imported_funcs_count)
{
let local_func_kind =
&mut local_func_kinds[local_func_id as usize];
let local_func_kind = local_func_kinds
.get_mut(local_func_id as usize)
.read_error("Invalid Wasm export index")?;
if let LocalFunctionKind::Unknown = local_func_kind {
*local_func_kind = LocalFunctionKind::Exported {
symbol_ids: Vec::new(),
Expand Down Expand Up @@ -273,7 +280,9 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> {
file.entry = address;
}

let local_func_kind = &mut local_func_kinds[i];
let local_func_kind = local_func_kinds
.get_mut(i)
.read_error("Invalid Wasm code section index")?;
match local_func_kind {
LocalFunctionKind::Unknown => {
*local_func_kind = LocalFunctionKind::Local {
Expand Down Expand Up @@ -306,6 +315,9 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> {
wp::Payload::DataCountSection { range, .. } => {
file.add_section(SectionId::DataCount, range, "");
}
wp::Payload::TagSection(section) => {
file.add_section(SectionId::Tag, section.range(), "");
}
wp::Payload::CustomSection(section) => {
let name = section.name();
let size = section.data().len();
Expand Down Expand Up @@ -683,6 +695,7 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for WasmSection<'data
SectionId::Code => "<code>",
SectionId::Data => "<data>",
SectionId::DataCount => "<data_count>",
SectionId::Tag => "<tag>",
})
}

Expand Down Expand Up @@ -715,6 +728,7 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for WasmSection<'data
SectionId::Code => SectionKind::Text,
SectionId::Data => SectionKind::Data,
SectionId::DataCount => SectionKind::UninitializedData,
SectionId::Tag => SectionKind::Data,
}
}

Expand Down

0 comments on commit bd15e0c

Please sign in to comment.