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/Cargo.toml | 21 +++++ crates/core/helloworld.wasm | Bin 0 -> 115 bytes crates/core/helloworld.wat | 15 ++++ crates/core/src/bin.rs | 9 ++ crates/core/src/instructions.rs | 106 ++++++++++++++++++++++++ crates/core/src/lib.rs | 178 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 329 insertions(+) create mode 100644 crates/core/Cargo.toml create mode 100644 crates/core/helloworld.wasm create mode 100644 crates/core/helloworld.wat create mode 100644 crates/core/src/bin.rs create mode 100644 crates/core/src/instructions.rs create mode 100644 crates/core/src/lib.rs (limited to 'crates') diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml new file mode 100644 index 0000000..e8c52b9 --- /dev/null +++ b/crates/core/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name="wasmcore" +version="0.1.0" +edition="2021" + +[lib] +name="wasmcore" +path="src/lib.rs" + +[[bin]] +name="wasmcore" +path="src/bin.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +wasmparser={version="0.118.0", default-features=false} + +[features] +default=["std"] +std=[] diff --git a/crates/core/helloworld.wasm b/crates/core/helloworld.wasm new file mode 100644 index 0000000..a5c95d0 Binary files /dev/null and b/crates/core/helloworld.wasm differ diff --git a/crates/core/helloworld.wat b/crates/core/helloworld.wat new file mode 100644 index 0000000..b74c98f --- /dev/null +++ b/crates/core/helloworld.wat @@ -0,0 +1,15 @@ +(module + ;; Imports from JavaScript namespace + (import "console" "log" (func $log (param i32 i32))) ;; Import log function + (import "js" "mem" (memory 1)) ;; Import 1 page of memory (54kb) + + ;; Data section of our module + (data (i32.const 0) "Hello World from WebAssembly!") + + ;; Function declaration: Exported as helloWorld(), no arguments + (func (export "helloWorld") + i32.const 0 ;; pass offset 0 to log + i32.const 29 ;; pass length 29 to log (strlen of sample text) + call $log + ) +) \ No newline at end of file diff --git a/crates/core/src/bin.rs b/crates/core/src/bin.rs new file mode 100644 index 0000000..c4257e8 --- /dev/null +++ b/crates/core/src/bin.rs @@ -0,0 +1,9 @@ +use wasmcore::{self, Module}; + +pub static WASM: &'static [u8] = include_bytes!("../helloworld.wasm"); + +fn main() { + let module = Module::new(WASM); + + println!("{:#?}", module); +} diff --git a/crates/core/src/instructions.rs b/crates/core/src/instructions.rs new file mode 100644 index 0000000..e3e6a08 --- /dev/null +++ b/crates/core/src/instructions.rs @@ -0,0 +1,106 @@ +// https://webassembly.github.io/spec/core/binary/instructions.html + +// Controll Instructions +pub mod control { + pub const WASM_UNREACHABLE: u8 = 0x00; + pub const WASM_NOP: u8 = 0x01; // do nothing + + // stuctured instructions + pub const WASM_BLOCK: u8 = 0x02; + pub const WASM_LOOP: u8 = 0x03; + pub const WASM_IF: u8 = 0x04; + + pub const WASM_ELSE: u8 = 0x05; + pub const WASM_END: u8 = 0x0B; + pub const WASM_BR: u8 = 0x0C; + pub const WASM_BR_IF: u8 = 0x0D; + pub const WASM_BR_TABLE: u8 = 0x0E; + pub const WASM_RETURN: u8 = 0x0F; + pub const WASM_CALL: u8 = 0x10; + pub const WASM_CALL_INDIRECT: u8 = 0x11; + pub const WASM_DROP: u8 = 0x1A; +} + +// Reference Instructions +pub mod reference { + pub const WASM_REF_NULL: u8 = 0xD0; + pub const WASM_REF_IS_NULL: u8 = 0xD1; + pub const WASM_REF_FUNC: u8 = 0xD2; +} + +// Parametric Instructions +pub mod parametric { + pub const WASM_DROP: u8 = 0x1A; + pub const WASM_SELECT: u8 = 0x1B; + pub const WASM_SELECT_T: u8 = 0x1C; +} + +// Variable Instructions +pub mod variable { + pub const WASM_LOCAL_GET: u8 = 0x20; + pub const WASM_LOCAL_SET: u8 = 0x21; + pub const WASM_LOCAL_TEE: u8 = 0x22; + pub const WASM_GLOBAL_GET: u8 = 0x23; + pub const WASM_GLOBAL_SET: u8 = 0x24; +} + +// Table Instructions +pub mod table { + pub const WASM_TABLE_GET: u8 = 0x25; + pub const WASM_TABLE_SET: u8 = 0x26; + pub const WASM_TABLE_INIT: u8 = 0xFC; + pub const WASM_ELEM_DROP: u8 = 0xFC; + pub const WASM_TABLE_COPY: u8 = 0xFC; + pub const WASM_TABLE_GROW: u8 = 0xFC; + pub const WASM_TABLE_SIZE: u8 = 0xFC; + pub const WASM_TABLE_FILL: u8 = 0xFC; +} + +// Memory Instructions +pub mod memory { + pub const WASM_I32_LOAD: u8 = 0x28; + pub const WASM_I64_LOAD: u8 = 0x29; + pub const WASM_F32_LOAD: u8 = 0x2A; + pub const WASM_F64_LOAD: u8 = 0x2B; + pub const WASM_I32_LOAD8_S: u8 = 0x2C; + pub const WASM_I32_LOAD8_U: u8 = 0x2D; + pub const WASM_I32_LOAD16_S: u8 = 0x2E; + pub const WASM_I32_LOAD16_U: u8 = 0x2F; + pub const WASM_I64_LOAD8_S: u8 = 0x30; + pub const WASM_I64_LOAD8_U: u8 = 0x31; + pub const WASM_I64_LOAD16_S: u8 = 0x32; + pub const WASM_I64_LOAD16_U: u8 = 0x33; + pub const WASM_I64_LOAD32_S: u8 = 0x34; + pub const WASM_I64_LOAD32_U: u8 = 0x35; + pub const WASM_I32_STORE: u8 = 0x36; + pub const WASM_I64_STORE: u8 = 0x37; + pub const WASM_F32_STORE: u8 = 0x38; + pub const WASM_F64_STORE: u8 = 0x39; + pub const WASM_I32_STORE8: u8 = 0x3A; + pub const WASM_I32_STORE16: u8 = 0x3B; + pub const WASM_I64_STORE8: u8 = 0x3C; + pub const WASM_I64_STORE16: u8 = 0x3D; + pub const WASM_I64_STORE32: u8 = 0x3E; + pub const WASM_MEMORY_SIZE: u8 = 0x3F; + pub const WASM_MEMORY_GROW: u8 = 0x40; + pub const WASM_MEMORY_INIT: u8 = 0xFC; + pub const WASM_DATA_DROP: u8 = 0xFC; + pub const WASM_MEMORY_COPY: u8 = 0xFC; + pub const WASM_MEMORY_FILL: u8 = 0xFC; +} + +// Numeric Instructions +pub mod numeric { + // Constants + pub const WASM_I32_CONST: u8 = 0x41; + pub const WASM_I64_CONST: u8 = 0x42; + pub const WASM_F32_CONST: u8 = 0x43; + pub const WASM_F64_CONST: u8 = 0x44; + + // Operations + pub const START_NUMERIC: u8 = 0x45; + pub const END_NUMERIC: u8 = 0xC4; + pub const WASM_SATURATING_TRUNC: u8 = 0xFC; +} + +pub const WASM_VEC: u8 = 0xFD; 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