From 1256c4248d2a3a9f73c2a14d997a337ba0eee2b4 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 21 Nov 2023 16:48:33 +0100 Subject: initial commit Signed-off-by: Henry Gressmann --- crates/core/src/lib.rs | 178 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 crates/core/src/lib.rs (limited to 'crates/core/src/lib.rs') diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs new file mode 100644 index 0000000..ee8a3be --- /dev/null +++ b/crates/core/src/lib.rs @@ -0,0 +1,178 @@ +#![no_std] +#![forbid(unsafe_code)] + +#[cfg(feature = "std")] +extern crate std; +use std::println; + +extern crate alloc; +use alloc::vec::Vec; + +use wasmparser::{ + DataSectionReader, ElementSectionReader, ExportSectionReader, FunctionBody, + FunctionSectionReader, GlobalSectionReader, ImportSectionReader, MemorySectionReader, Payload, + TableSectionReader, TypeSectionReader, Validator, +}; +mod instructions; + +struct Store {} + +pub struct Module<'a> { + reader: ModuleReader<'a>, +} + +impl<'a> core::fmt::Debug for Module<'a> { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("Module") + .field("version", &self.reader.version) + .field("type_section", &self.reader.type_section) + .field("function_section", &self.reader.function_section) + .field("table_section", &self.reader.table_section) + .field("memory_section", &self.reader.memory_section) + .field("global_section", &self.reader.global_section) + .field("element_section", &self.reader.element_section) + .field("data_section", &self.reader.data_section) + .field("code_section", &self.reader.code_section) + .field("import_section", &self.reader.import_section) + .field("export_section", &self.reader.export_section) + .finish() + } +} + +#[derive(Default)] +pub struct ModuleReader<'a> { + pub version: Option, + pub type_section: Option>, + pub function_section: Option>, + pub table_section: Option>, + pub memory_section: Option>, + pub global_section: Option>, + pub element_section: Option>, + pub data_section: Option>, + pub code_section: Option>, + pub import_section: Option>, + pub export_section: Option>, +} + +#[derive(Debug)] +pub struct CodeSection<'a> { + pub(crate) functions: Vec>, +} + +impl<'a> CodeSection<'a> { + fn new() -> Self { + Self { + functions: Vec::new(), + } + } +} + +impl<'a> Module<'a> { + pub fn new(wasm: &'a [u8]) -> Result { + let mut validator = Validator::new(); + let mut reader = ModuleReader::new(); + + for payload in wasmparser::Parser::new(0).parse_all(wasm) { + reader.process_payload(payload.unwrap(), &mut validator)?; + } + + Ok(Self { reader }) + } +} + +impl<'a> ModuleReader<'a> { + pub fn new() -> Self { + Self::default() + } + + pub fn process_payload( + &mut self, + payload: Payload<'a>, + validator: &mut Validator, + ) -> Result { + use wasmparser::Payload::*; + match payload { + Version { + num, + encoding, + range, + } => { + validator.version(num, encoding, &range).map_err(|_| ())?; + self.version = Some(num); + match encoding { + wasmparser::Encoding::Module => {} + wasmparser::Encoding::Component => return Err(()), + } + } + TypeSection(reader) => { + validator.type_section(&reader).map_err(|_| ())?; + self.type_section = Some(reader); + } + FunctionSection(reader) => { + validator.function_section(&reader).map_err(|_| ())?; + self.function_section = Some(reader); + } + TableSection(reader) => { + validator.table_section(&reader).map_err(|_| ())?; + self.table_section = Some(reader); + } + MemorySection(reader) => { + validator.memory_section(&reader).map_err(|_| ())?; + self.memory_section = Some(reader); + } + GlobalSection(reader) => { + validator.global_section(&reader).map_err(|_| ())?; + self.global_section = Some(reader); + } + ElementSection(reader) => { + validator.element_section(&reader).map_err(|_| ())?; + self.element_section = Some(reader); + } + DataSection(reader) => { + validator.data_section(&reader).map_err(|_| ())?; + self.data_section = Some(reader); + } + CodeSectionStart { count, range, .. } => { + validator + .code_section_start(count, &range) + .map_err(|_| ())?; + + self.code_section = Some(CodeSection::new()); + } + CodeSectionEntry(function) => { + validator.code_section_entry(&function).map_err(|_| ())?; + + if let Some(code_section) = &mut self.code_section { + code_section.functions.push(function); + } else { + return Err(()); + } + } + ImportSection(reader) => { + validator.import_section(&reader).map_err(|_| ())?; + self.import_section = Some(reader); + } + ExportSection(reader) => { + validator.export_section(&reader).map_err(|_| ())?; + self.export_section = Some(reader); + } + + End(offset) => { + validator.end(offset).map_err(|_| ())?; + return Ok(true); + } + x => println!("Unknown payload: {:?}", x), + }; + + Ok(false) + } +} +struct Instance {} + +pub fn parse(wasm: &[u8]) -> Result, ()> { + for payload in wasmparser::Parser::new(0).parse_all(wasm) { + return Ok(payload.unwrap()); + } + + return Err(()); +} -- cgit v1.3.1