@@ -13,7 +13,7 @@ use tokio::{
1313} ;
1414use wasmer_cache:: { Cache , FileSystemCache } ;
1515use wasi_process2:: WasiProcess ;
16- use wasmer_wasi:: { WasiState , WasiVersion , WasiEnv } ;
16+ use wasmer_wasi:: { WasiFunctionEnv , WasiVersion , WasiEnv } ;
1717use wasmer:: { AsStoreMut , AsStoreRef , Instance , FunctionEnv } ;
1818
1919use logic:: { GameMode , MainOutput , RobotRunner } ;
@@ -181,7 +181,6 @@ enum RunnerKind {
181181 runner : WasiRunner ,
182182 /// the directory that we store the source file in; we need to keep it open
183183 _dir : tempfile:: TempDir ,
184- memory : wasmer:: Memory ,
185184 } ,
186185}
187186
@@ -197,12 +196,11 @@ impl RobotRunner for Runner {
197196 let inner = async move {
198197 match kind {
199198 RunnerKind :: Command ( r) => r. run ( input) . await ,
200- RunnerKind :: Wasi { runner, memory , .. } => {
199+ RunnerKind :: Wasi { runner, .. } => {
201200 log:: debug!(
202- "start of turn {} w/ {} units: {:?} allocated " ,
201+ "start of turn {} w/ {} units" ,
203202 input. state. turn,
204203 input. state. objs. len( ) ,
205- memory. size( )
206204 ) ;
207205 runner. run ( input) . await
208206 }
@@ -227,7 +225,6 @@ impl Runner {
227225 }
228226
229227 async fn new_wasm (
230- store : & wasmer:: Store ,
231228 module : & wasmer:: Module ,
232229 version : WasiVersion ,
233230 args : & [ String ] ,
@@ -240,14 +237,17 @@ impl Runner {
240237 . unwrap ( )
241238 . args ( args)
242239 . arg ( "/source/sourcecode" ) ;
243- let env = FunctionEnv :: new ( & mut STORE . as_store_mut ( ) , WasiEnv :: new ( state. build ( ) ?) ) ;
244- let instance = {
245- // imports isn't Send
246- let imports = wasmer_wasi:: generate_import_object_from_env ( & mut store. as_store_mut ( ) , & env, version) ;
247- wasmer:: Instance :: new ( & mut store. as_store_mut ( ) , module, & imports) ?
240+ let mut proc = unsafe {
241+ let wasi_env = WasiEnv :: new ( state. build ( ) ?) ;
242+ let mut env = WasiFunctionEnv :: new ( & mut STORE . as_store_mut ( ) , wasi_env) ;
243+ let instance = {
244+ // imports isn't Send
245+ let imports = wasmer_wasi:: generate_import_object_from_env ( & mut STORE . as_store_mut ( ) , & env. env , version) ;
246+ wasmer:: Instance :: new ( & mut STORE . as_store_mut ( ) , module, & imports) ?
247+ } ;
248+ env. initialize ( & mut STORE . as_store_mut ( ) , & instance) ?;
249+ WasiProcess :: new ( & mut STORE , & instance, Default :: default ( ) ) ?
248250 } ;
249- let memory = instance. exports . get :: < wasmer:: Memory > ( "memory" ) . unwrap ( ) ;
250- let mut proc = WasiProcess :: new ( & mut store, & instance, Default :: default ( ) ) ?;
251251
252252 let stdin = io:: BufWriter :: new ( proc. stdin . take ( ) . unwrap ( ) ) ;
253253 let stdout = io:: BufReader :: new ( proc. stdout . take ( ) . unwrap ( ) ) ;
@@ -263,7 +263,6 @@ impl Runner {
263263 kind : RunnerKind :: Wasi {
264264 runner,
265265 _dir : dir,
266- memory : memory. clone ( ) ,
267266 } ,
268267 timeout : None ,
269268 } ) ;
@@ -279,18 +278,13 @@ impl Runner {
279278 anyhow ! ( "robot {}/{} has no open source published code" , user, robot)
280279 } ) ?;
281280 let sourcedir = make_sourcedir_inline ( & code) ?;
282- let store = & * STORE ;
283- let ( module, version) = info. lang . get_wasm ( store) ?;
284- Runner :: new_wasm ( store, module, version, & [ ] , sourcedir) . await
281+ let ( module, version) = info. lang . get_wasm ( ) ?;
282+ Runner :: new_wasm ( module, version, & [ ] , sourcedir) . await
285283 }
286284 RobotId :: Local { source, lang } => {
287285 let sourcedir = make_sourcedir ( source) ?;
288- let store = & * STORE ;
289- let ( module, version) = lang. get_wasm ( store) ?;
290- // This is very strange, but this exactly println in this exact place is necessary to avoid
291- // error: "corrupted binary: misaligned metadata"
292- println ! ( "IT worked" ) ;
293- Runner :: new_wasm ( store, module, version, & [ ] , sourcedir) . await
286+ let ( module, version) = lang. get_wasm ( ) ?;
287+ Runner :: new_wasm ( module, version, & [ ] , sourcedir) . await
294288 }
295289 RobotId :: Command { command, args } => {
296290 let mut cmd = Command :: new ( command) ;
@@ -310,22 +304,20 @@ impl Runner {
310304 let wasm = tokio:: fs:: read ( runner)
311305 . await
312306 . with_context ( || format ! ( "couldn't read {}" , runner) ) ?;
313- let store = & * STORE ;
314- let ( module, version) = wasm_from_cache_or_compile ( store, & wasm)
307+ let ( module, version) = wasm_from_cache_or_compile ( & wasm)
315308 . with_context ( || format ! ( "couldn't compile wasm module at {}" , runner) ) ?;
316- Runner :: new_wasm ( store , & module, version, & runner_args, sourcedir) . await
309+ Runner :: new_wasm ( & module, version, & runner_args, sourcedir) . await
317310 }
318311 RobotId :: Inline { lang, source } => {
319312 let sourcedir = make_sourcedir_inline ( source) ?;
320- let store = & * STORE ;
321- let ( module, version) = lang. get_wasm ( store) ?;
322- Runner :: new_wasm ( store, module, version, & [ ] , sourcedir) . await
313+ let ( module, version) = lang. get_wasm ( ) ?;
314+ Runner :: new_wasm ( module, version, & [ ] , sourcedir) . await
323315 }
324316 }
325317 }
326318}
327319
328- static STORE : Lazy < wasmer:: Store > = Lazy :: new ( || {
320+ static mut STORE : Lazy < wasmer:: Store > = Lazy :: new ( || {
329321 let engine = wasmer:: Engine :: headless ( ) ;
330322 wasmer:: Store :: new ( & engine)
331323} ) ;
@@ -583,25 +575,24 @@ fn get_wasm_cache() -> anyhow::Result<FileSystemCache> {
583575}
584576
585577fn wasm_from_cache_or_compile (
586- store : & wasmer:: Store ,
587578 wasm : & [ u8 ] ,
588579) -> anyhow:: Result < ( wasmer:: Module , WasiVersion ) > {
589580 let module = match get_wasm_cache ( ) {
590581 Ok ( mut cache) => {
591582 let hash = wasmer_cache:: Hash :: generate ( wasm) ;
592583 // unsafe because wasmer loads arbitrary code from this directory, but the wasmer
593584 // cli does the same thing, and there's no cve for it ¯\_(ツ)_/¯
594- let module = unsafe { cache. load ( store , hash) } ;
585+ let module = unsafe { cache. load ( & * STORE , hash) } ;
595586 match module {
596587 Ok ( m) => m,
597588 Err ( _) => {
598- let module = wasmer:: Module :: new ( store , wasm) ?;
589+ let module = unsafe { wasmer:: Module :: new ( & * STORE , wasm) ? } ;
599590 let _ = cache. store ( hash, & module) ;
600591 module
601592 }
602593 }
603594 }
604- Err ( _) => wasmer:: Module :: new ( store , wasm) ?,
595+ Err ( _) => unsafe { wasmer:: Module :: new ( & * STORE , wasm) ? }
605596 } ;
606597 let version = wasmer_wasi:: get_wasi_version ( & module, false ) . unwrap_or ( WasiVersion :: Latest ) ;
607598 Ok ( ( module, version) )
@@ -623,14 +614,13 @@ impl Lang {
623614 }
624615 }
625616 fn get_wasm (
626- self ,
627- store : & wasmer:: Store ,
617+ self
628618 ) -> anyhow:: Result < ( & ' static wasmer:: Module , WasiVersion ) > {
629619 macro_rules! lang_runner {
630620 ( $bytes: expr) => { {
631621 static MODULE : OnceCell <( wasmer:: Module , WasiVersion ) > = OnceCell :: new( ) ;
632622 let ( module, version) = MODULE . get_or_try_init( || {
633- let module = unsafe { wasmer:: Module :: deserialize( store , $bytes) ? } ;
623+ let module = unsafe { wasmer:: Module :: deserialize( & * STORE , $bytes) ? } ;
634624 let version = wasmer_wasi:: get_wasi_version( & module, false )
635625 . unwrap_or( WasiVersion :: Latest ) ;
636626 Ok :: <_, anyhow:: Error >( ( module, version) )
@@ -639,9 +629,6 @@ impl Lang {
639629 } } ;
640630 }
641631 let lang = self ;
642- // This is very strange, but this exactly println in this exact place is necessary to avoid
643- // error: "corrupted binary: misaligned metadata"
644- println ! ( "ATTEMPTING" ) ;
645632 Ok ( include ! ( concat!( env!( "OUT_DIR" ) , "/lang_runners.rs" ) ) )
646633 }
647634}
0 commit comments