Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read/wasm: multiple fixes #649

Merged
merged 3 commits into from
Mar 23, 2024
Merged
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
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
Loading