custom sections

This commit is contained in:
Graham Kelly 2024-02-03 13:47:08 -05:00
parent 9174f826aa
commit 2f1a25c569
4 changed files with 16 additions and 3 deletions

View file

@ -23,6 +23,7 @@ addr2line = "0.19"
# For fuzzing only. Versions must match those in fuzz/Cargo.toml. # For fuzzing only. Versions must match those in fuzz/Cargo.toml.
libfuzzer-sys = { version = "0.4", optional = true } libfuzzer-sys = { version = "0.4", optional = true }
wasm-smith = { version = "0.8", optional = true } wasm-smith = { version = "0.8", optional = true }
indexmap = "2.2.2"
[features] [features]
default = [] default = []

View file

@ -7,6 +7,7 @@ use crate::Operator;
use anyhow::Result; use anyhow::Result;
use rayon::prelude::*; use rayon::prelude::*;
use std::borrow::Cow; use std::borrow::Cow;
use wasm_encoder::CustomSection;
pub mod stackify; pub mod stackify;
use stackify::{Context as StackifyContext, WasmBlock}; use stackify::{Context as StackifyContext, WasmBlock};
@ -787,6 +788,9 @@ pub fn compile(module: &Module<'_>) -> anyhow::Result<Vec<u8>> {
} }
names.functions(&func_names); names.functions(&func_names);
into_mod.section(&names); into_mod.section(&names);
for (k, v) in module.custom_sections.iter() {
into_mod.section(&CustomSection { name: &k, data: &v });
}
Ok(into_mod.finish()) Ok(into_mod.finish())
} }

View file

@ -297,7 +297,11 @@ fn handle_payload<'a>(
extra_sections.debug_rnglists = extra_sections.debug_rnglists =
gimli::DebugRngLists::new(reader.data(), gimli::LittleEndian); gimli::DebugRngLists::new(reader.data(), gimli::LittleEndian);
} }
Payload::CustomSection(_) => {} Payload::CustomSection(reader) => {
module
.custom_sections
.insert(reader.name().to_owned(), reader.data().to_owned());
}
Payload::Version { .. } => {} Payload::Version { .. } => {}
Payload::ElementSection(reader) => { Payload::ElementSection(reader) => {
for element in reader { for element in reader {
@ -1151,9 +1155,9 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> {
| wasmparser::Operator::TableGet { .. } | wasmparser::Operator::TableGet { .. }
| wasmparser::Operator::TableSet { .. } | wasmparser::Operator::TableSet { .. }
| wasmparser::Operator::TableGrow { .. } | wasmparser::Operator::TableGrow { .. }
| wasmparser::Operator::TableSize { .. } | wasmparser::Operator::TableSize { .. }
| wasmparser::Operator::MemoryCopy { .. } | wasmparser::Operator::MemoryCopy { .. }
| wasmparser::Operator::MemoryFill { .. }=> { | wasmparser::Operator::MemoryFill { .. } => {
self.emit(Operator::try_from(&op).unwrap(), loc)? self.emit(Operator::try_from(&op).unwrap(), loc)?
} }

View file

@ -3,6 +3,7 @@ use crate::entity::{EntityRef, EntityVec};
use crate::ir::{Debug, DebugMap, FunctionBody}; use crate::ir::{Debug, DebugMap, FunctionBody};
use crate::{backend, frontend}; use crate::{backend, frontend};
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap;
pub use crate::frontend::FrontendOptions; pub use crate::frontend::FrontendOptions;
@ -19,6 +20,7 @@ pub struct Module<'a> {
pub start_func: Option<Func>, pub start_func: Option<Func>,
pub debug: Debug, pub debug: Debug,
pub debug_map: DebugMap, pub debug_map: DebugMap,
pub custom_sections: IndexMap<String,Vec<u8>>
} }
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -143,6 +145,7 @@ impl<'a> Module<'a> {
start_func: None, start_func: None,
debug: Debug::default(), debug: Debug::default(),
debug_map: DebugMap::default(), debug_map: DebugMap::default(),
custom_sections: IndexMap::new(),
} }
} }
@ -165,6 +168,7 @@ impl<'a> Module<'a> {
start_func: self.start_func, start_func: self.start_func,
debug: self.debug, debug: self.debug,
debug_map: self.debug_map, debug_map: self.debug_map,
custom_sections: self.custom_sections,
} }
} }
} }