From 870244f4ce9107907817ce80fd443b2f39f5cb0b Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Thu, 23 Feb 2023 20:37:15 -0800 Subject: [PATCH] interp: make WASI impl compatible with SpiderMonkey.wasm --- src/interp.rs | 8 ++++++-- src/interp/wasi.rs | 24 ++++++++++++++++++------ src/ir/module.rs | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/interp.rs b/src/interp.rs index 187ed53..2d1d910 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -1,6 +1,6 @@ //! Waffle IR interpreter. -use crate::entity::PerEntity; +use crate::entity::{EntityRef, PerEntity}; use crate::ir::*; use crate::ops::Operator; use smallvec::{smallvec, SmallVec}; @@ -65,7 +65,11 @@ impl InterpContext { ) -> Option> { let body = match &module.funcs[func] { FuncDecl::Lazy(..) => panic!("Un-expanded function"), - FuncDecl::Import(_, name) => return self.call_import(&name[..], args), + FuncDecl::Import(..) => { + let import = &module.imports[func.index()]; + assert_eq!(import.kind, ImportKind::Func(func)); + return self.call_import(&import.name[..], args); + } FuncDecl::Body(_, _, body) => body, }; diff --git a/src/interp/wasi.rs b/src/interp/wasi.rs index 4ba5a98..43dc4ac 100644 --- a/src/interp/wasi.rs +++ b/src/interp/wasi.rs @@ -9,18 +9,25 @@ pub fn call_wasi( args: &[ConstVal], ) -> Option> { match name { - "__wasi_fd_prestat_get" => { + "fd_prestat_get" => { Some(smallvec![ConstVal::I32(8)]) // BADF } - "__wasi_args_sizes_get" => { + "args_sizes_get" => { let p_argc = args[0].as_u32().unwrap(); let p_argv_size = args[1].as_u32().unwrap(); write_u32(mem, p_argc, 0); write_u32(mem, p_argv_size, 0); Some(smallvec![ConstVal::I32(0)]) } - "__wasi_args_get" => Some(smallvec![ConstVal::I32(0)]), - "__wasi_fd_fdstat_get" => { + "environ_sizes_get" => { + let p_environ_count = args[0].as_u32().unwrap(); + let p_environ_buf_size = args[0].as_u32().unwrap(); + write_u32(mem, p_environ_count, 0); + write_u32(mem, p_environ_buf_size, 0); + Some(smallvec![ConstVal::I32(0)]) + } + "args_get" => Some(smallvec![ConstVal::I32(0)]), + "fd_fdstat_get" => { let fd = args[0].as_u32().unwrap(); let p_fdstat_t = args[1].as_u32().unwrap(); if fd == 1 { @@ -33,7 +40,7 @@ pub fn call_wasi( None } } - "__wasi_fd_write" => { + "fd_write" => { let fd = args[0].as_u32().unwrap(); let p_iovs = args[1].as_u32().unwrap(); let iovs_len = args[2].as_u32().unwrap(); @@ -54,10 +61,15 @@ pub fn call_wasi( None } } - "__wasi_proc_exit" => { + "proc_exit" => { eprintln!("WASI exit: {:?}", args[0]); None } + "clock_time_get" => { + let p_time = args[2].as_u32().unwrap(); + write_u32(mem, p_time, 0); + Some(smallvec![ConstVal::I32(0)]) + } _ => None, } } diff --git a/src/ir/module.rs b/src/ir/module.rs index b610b71..886c736 100644 --- a/src/ir/module.rs +++ b/src/ir/module.rs @@ -83,7 +83,7 @@ pub struct Import { pub kind: ImportKind, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum ImportKind { Table(Table), Func(Func),