@@ -282,17 +282,21 @@ fn add_debug_to_linker<
282282 Ok ( ( ) )
283283}
284284
285- /// Register the 4 argv/environ host functions that glibc's `_start()` calls
286- /// to initialize `argc`, `argv`, and `environ` before entering `main()`.
287- fn add_environ_to_linker <
285+ /// Register the 5 environ/args/random host functions under a given module name.
286+ ///
287+ /// glibc's `_start()` imports these from `"lind"`, while Rust std compiled with
288+ /// `wasm32-wasip1` imports them from `"wasi_snapshot_preview1"`. We call this
289+ /// function twice to register under both module names, avoiding duplication.
290+ fn add_environ_funcs_to_linker <
288291 T : LindHost < T , U > + Clone + Send + ' static + std:: marker:: Sync ,
289292 U : Clone + Send + ' static + std:: marker:: Sync ,
290293> (
291294 linker : & mut wasmtime:: Linker < T > ,
295+ module : & str ,
292296 get_environ : impl Fn ( & T ) -> & LindEnviron + Send + Sync + Copy + ' static ,
293297) -> anyhow:: Result < ( ) > {
294298 linker. func_wrap (
295- "lind" ,
299+ module ,
296300 "args_sizes_get" ,
297301 move |caller : Caller < ' _ , T > , ptr_argc : i32 , ptr_buf_size : i32 | -> i32 {
298302 let cx = get_environ ( caller. data ( ) ) ;
@@ -308,7 +312,7 @@ fn add_environ_to_linker<
308312 ) ?;
309313
310314 linker. func_wrap (
311- "lind" ,
315+ module ,
312316 "args_get" ,
313317 move |caller : Caller < ' _ , T > , argv_ptrs : i32 , argv_buf : i32 | -> i32 {
314318 let cx = get_environ ( caller. data ( ) ) ;
@@ -330,7 +334,7 @@ fn add_environ_to_linker<
330334 ) ?;
331335
332336 linker. func_wrap (
333- "lind" ,
337+ module ,
334338 "environ_sizes_get" ,
335339 move |caller : Caller < ' _ , T > , ptr_count : i32 , ptr_buf_size : i32 | -> i32 {
336340 let cx = get_environ ( caller. data ( ) ) ;
@@ -350,7 +354,7 @@ fn add_environ_to_linker<
350354 ) ?;
351355
352356 linker. func_wrap (
353- "lind" ,
357+ module ,
354358 "environ_get" ,
355359 move |caller : Caller < ' _ , T > , env_ptrs : i32 , env_buf : i32 | -> i32 {
356360 let cx = get_environ ( caller. data ( ) ) ;
@@ -372,6 +376,19 @@ fn add_environ_to_linker<
372376 } ,
373377 ) ?;
374378
379+ linker. func_wrap (
380+ module,
381+ "random_get" ,
382+ move |caller : Caller < ' _ , T > , buf : i32 , buf_len : i32 | -> i32 {
383+ let base = get_memory_base ( & caller) as * mut u8 ;
384+ let slice =
385+ unsafe { std:: slice:: from_raw_parts_mut ( base. add ( buf as usize ) , buf_len as usize ) } ;
386+ let mut file = std:: fs:: File :: open ( "/dev/urandom" ) . unwrap ( ) ;
387+ std:: io:: Read :: read_exact ( & mut file, slice) . unwrap ( ) ;
388+ 0
389+ } ,
390+ ) ?;
391+
375392 Ok ( ( ) )
376393}
377394
@@ -381,7 +398,7 @@ fn add_environ_to_linker<
381398/// - **syscall**: the unified `make-syscall` entry point
382399/// - **runtime**: memory base, cage ID, setjmp/longjmp, epoch callback, debug panic
383400/// - **debug** (lind_debug feature only): `lind_debug_num`, `lind_debug_str`
384- /// - **environ**: argv/environ functions for glibc `_start() `
401+ /// - **environ**: argv/environ/random_get under both `"lind"` and `"wasi_snapshot_preview1" `
385402pub fn add_to_linker <
386403 T : LindHost < T , U > + Clone + Send + ' static + std:: marker:: Sync ,
387404 U : Clone + Send + ' static + std:: marker:: Sync ,
@@ -393,6 +410,7 @@ pub fn add_to_linker<
393410 add_runtime_to_linker ( linker) ?;
394411 #[ cfg( feature = "lind_debug" ) ]
395412 add_debug_to_linker ( linker) ?;
396- add_environ_to_linker ( linker, get_environ) ?;
413+ add_environ_funcs_to_linker ( linker, "lind" , get_environ) ?;
414+ add_environ_funcs_to_linker ( linker, "wasi_snapshot_preview1" , get_environ) ?;
397415 Ok ( ( ) )
398416}
0 commit comments