@@ -2328,12 +2328,8 @@ wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
23282328
23292329#if WASM_ENABLE_LIBC_WASI != 0
23302330
2331- void
2332- wasm_runtime_set_wasi_args_ex (WASMModuleCommon * module , const char * dir_list [],
2333- uint32 dir_count , const char * map_dir_list [],
2334- uint32 map_dir_count , const char * env_list [],
2335- uint32 env_count , char * argv [], int argc ,
2336- int stdinfd , int stdoutfd , int stderrfd )
2331+ static WASIArguments *
2332+ get_wasi_args_from_module (wasm_module_t module )
23372333{
23382334 WASIArguments * wasi_args = NULL ;
23392335
@@ -2346,6 +2342,18 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
23462342 wasi_args = & ((AOTModule * )module )-> wasi_args ;
23472343#endif
23482344
2345+ return wasi_args ;
2346+ }
2347+
2348+ void
2349+ wasm_runtime_set_wasi_args_ex (WASMModuleCommon * module , const char * dir_list [],
2350+ uint32 dir_count , const char * map_dir_list [],
2351+ uint32 map_dir_count , const char * env_list [],
2352+ uint32 env_count , char * argv [], int argc ,
2353+ int stdinfd , int stdoutfd , int stderrfd )
2354+ {
2355+ WASIArguments * wasi_args = get_wasi_args_from_module (module );
2356+
23492357 if (wasi_args ) {
23502358 wasi_args -> dir_list = dir_list ;
23512359 wasi_args -> dir_count = dir_count ;
@@ -2376,30 +2384,75 @@ void
23762384wasm_runtime_set_wasi_addr_pool (wasm_module_t module , const char * addr_pool [],
23772385 uint32 addr_pool_size )
23782386{
2379- WASIArguments * wasi_args = NULL ;
2380-
2381- #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
2382- if (module -> module_type == Wasm_Module_Bytecode )
2383- wasi_args = & ((WASMModule * )module )-> wasi_args ;
2384- #endif
2385- #if WASM_ENABLE_AOT != 0
2386- if (module -> module_type == Wasm_Module_AoT )
2387- wasi_args = & ((AOTModule * )module )-> wasi_args ;
2388- #endif
2387+ WASIArguments * wasi_args = get_wasi_args_from_module (module );
23892388
23902389 if (wasi_args ) {
23912390 wasi_args -> addr_pool = addr_pool ;
23922391 wasi_args -> addr_count = addr_pool_size ;
23932392 }
23942393}
23952394
2395+ void
2396+ wasm_runtime_set_wasi_ns_lookup_pool (wasm_module_t module ,
2397+ const char * ns_lookup_pool [],
2398+ uint32 ns_lookup_pool_size )
2399+ {
2400+ WASIArguments * wasi_args = get_wasi_args_from_module (module );
2401+
2402+ if (wasi_args ) {
2403+ wasi_args -> ns_lookup_pool = ns_lookup_pool ;
2404+ wasi_args -> ns_lookup_count = ns_lookup_pool_size ;
2405+ }
2406+ }
2407+
23962408#if WASM_ENABLE_UVWASI == 0
2409+ static bool
2410+ copy_string_array (const char * array [], uint32 array_size , char * * buf_ptr ,
2411+ char * * * list_ptr , uint64 * out_buf_size )
2412+ {
2413+ uint64 buf_size = 0 , total_size ;
2414+ uint32 buf_offset = 0 , i ;
2415+ char * buf = NULL , * * list = NULL ;
2416+
2417+ for (i = 0 ; i < array_size ; i ++ )
2418+ buf_size += strlen (array [i ]) + 1 ;
2419+
2420+ /* We add +1 to generate null-terminated array of strings */
2421+ total_size = sizeof (char * ) * (uint64 )array_size + 1 ;
2422+ if (total_size >= UINT32_MAX
2423+ || (total_size > 0 && !(list = wasm_runtime_malloc ((uint32 )total_size )))
2424+ || buf_size >= UINT32_MAX
2425+ || (buf_size > 0 && !(buf = wasm_runtime_malloc ((uint32 )buf_size )))) {
2426+
2427+ if (buf )
2428+ wasm_runtime_free (buf );
2429+ if (list )
2430+ wasm_runtime_free (list );
2431+ return false;
2432+ }
2433+
2434+ for (i = 0 ; i < array_size ; i ++ ) {
2435+ list [i ] = buf + buf_offset ;
2436+ bh_strcpy_s (buf + buf_offset , (uint32 )buf_size - buf_offset , array [i ]);
2437+ buf_offset += (uint32 )(strlen (array [i ]) + 1 );
2438+ }
2439+ list [array_size ] = NULL ;
2440+
2441+ * list_ptr = list ;
2442+ * buf_ptr = buf ;
2443+ if (out_buf_size )
2444+ * out_buf_size = buf_size ;
2445+
2446+ return true;
2447+ }
2448+
23972449bool
23982450wasm_runtime_init_wasi (WASMModuleInstanceCommon * module_inst ,
23992451 const char * dir_list [], uint32 dir_count ,
24002452 const char * map_dir_list [], uint32 map_dir_count ,
24012453 const char * env [], uint32 env_count ,
24022454 const char * addr_pool [], uint32 addr_pool_size ,
2455+ const char * ns_lookup_pool [], uint32 ns_lookup_pool_size ,
24032456 char * argv [], uint32 argc , int stdinfd , int stdoutfd ,
24042457 int stderrfd , char * error_buf , uint32 error_buf_size )
24052458{
@@ -2408,8 +2461,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
24082461 char * * argv_list = NULL ;
24092462 char * env_buf = NULL ;
24102463 char * * env_list = NULL ;
2411- uint64 argv_buf_size = 0 , env_buf_size = 0 , total_size ;
2412- uint32 argv_buf_offset = 0 , env_buf_offset = 0 ;
2464+ char * ns_lookup_buf = NULL ;
2465+ char * * ns_lookup_list = NULL ;
2466+ uint64 argv_buf_size = 0 , env_buf_size = 0 ;
24132467 struct fd_table * curfds = NULL ;
24142468 struct fd_prestats * prestats = NULL ;
24152469 struct argv_environ_values * argv_environ = NULL ;
@@ -2443,50 +2497,20 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
24432497#endif
24442498
24452499 /* process argv[0], trip the path and suffix, only keep the program name */
2446- for (i = 0 ; i < argc ; i ++ )
2447- argv_buf_size += strlen (argv [i ]) + 1 ;
2448-
2449- total_size = sizeof (char * ) * (uint64 )argc ;
2450- if (total_size >= UINT32_MAX
2451- || (total_size > 0
2452- && !(argv_list = wasm_runtime_malloc ((uint32 )total_size )))
2453- || argv_buf_size >= UINT32_MAX
2454- || (argv_buf_size > 0
2455- && !(argv_buf = wasm_runtime_malloc ((uint32 )argv_buf_size )))) {
2500+ if (!copy_string_array ((const char * * )argv , argc , & argv_buf , & argv_list ,
2501+ & argv_buf_size )) {
24562502 set_error_buf (error_buf , error_buf_size ,
24572503 "Init wasi environment failed: allocate memory failed" );
24582504 goto fail ;
24592505 }
24602506
2461- for (i = 0 ; i < argc ; i ++ ) {
2462- argv_list [i ] = argv_buf + argv_buf_offset ;
2463- bh_strcpy_s (argv_buf + argv_buf_offset ,
2464- (uint32 )argv_buf_size - argv_buf_offset , argv [i ]);
2465- argv_buf_offset += (uint32 )(strlen (argv [i ]) + 1 );
2466- }
2467-
2468- for (i = 0 ; i < env_count ; i ++ )
2469- env_buf_size += strlen (env [i ]) + 1 ;
2470-
2471- total_size = sizeof (char * ) * (uint64 )env_count ;
2472- if (total_size >= UINT32_MAX
2473- || (total_size > 0
2474- && !(env_list = wasm_runtime_malloc ((uint32 )total_size )))
2475- || env_buf_size >= UINT32_MAX
2476- || (env_buf_size > 0
2477- && !(env_buf = wasm_runtime_malloc ((uint32 )env_buf_size )))) {
2507+ if (!copy_string_array (env , env_count , & env_buf , & env_list ,
2508+ & env_buf_size )) {
24782509 set_error_buf (error_buf , error_buf_size ,
24792510 "Init wasi environment failed: allocate memory failed" );
24802511 goto fail ;
24812512 }
24822513
2483- for (i = 0 ; i < env_count ; i ++ ) {
2484- env_list [i ] = env_buf + env_buf_offset ;
2485- bh_strcpy_s (env_buf + env_buf_offset ,
2486- (uint32 )env_buf_size - env_buf_offset , env [i ]);
2487- env_buf_offset += (uint32 )(strlen (env [i ]) + 1 );
2488- }
2489-
24902514 if (!(curfds = wasm_runtime_malloc (sizeof (struct fd_table )))
24912515 || !(prestats = wasm_runtime_malloc (sizeof (struct fd_prestats )))
24922516 || !(argv_environ =
@@ -2588,6 +2612,13 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
25882612 }
25892613 }
25902614
2615+ if (!copy_string_array (ns_lookup_pool , ns_lookup_pool_size , & ns_lookup_buf ,
2616+ & ns_lookup_list , NULL )) {
2617+ set_error_buf (error_buf , error_buf_size ,
2618+ "Init wasi environment failed: allocate memory failed" );
2619+ goto fail ;
2620+ }
2621+
25912622 wasi_ctx -> curfds = curfds ;
25922623 wasi_ctx -> prestats = prestats ;
25932624 wasi_ctx -> argv_environ = argv_environ ;
@@ -2596,6 +2627,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
25962627 wasi_ctx -> argv_list = argv_list ;
25972628 wasi_ctx -> env_buf = env_buf ;
25982629 wasi_ctx -> env_list = env_list ;
2630+ wasi_ctx -> ns_lookup_buf = ns_lookup_buf ;
2631+ wasi_ctx -> ns_lookup_list = ns_lookup_list ;
25992632
26002633 return true;
26012634
@@ -2624,6 +2657,10 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
26242657 wasm_runtime_free (env_buf );
26252658 if (env_list )
26262659 wasm_runtime_free (env_list );
2660+ if (ns_lookup_buf )
2661+ wasm_runtime_free (ns_lookup_buf );
2662+ if (ns_lookup_list )
2663+ wasm_runtime_free (ns_lookup_list );
26272664 return false;
26282665}
26292666#else /* else of WASM_ENABLE_UVWASI == 0 */
@@ -2675,6 +2712,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
26752712 const char * map_dir_list [], uint32 map_dir_count ,
26762713 const char * env [], uint32 env_count ,
26772714 const char * addr_pool [], uint32 addr_pool_size ,
2715+ const char * ns_lookup_pool [], uint32 ns_lookup_pool_size ,
26782716 char * argv [], uint32 argc , int stdinfd , int stdoutfd ,
26792717 int stderrfd , char * error_buf , uint32 error_buf_size )
26802718{
@@ -2851,6 +2889,11 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
28512889 wasm_runtime_free (wasi_ctx -> env_buf );
28522890 if (wasi_ctx -> env_list )
28532891 wasm_runtime_free (wasi_ctx -> env_list );
2892+ if (wasi_ctx -> ns_lookup_buf )
2893+ wasm_runtime_free (wasi_ctx -> ns_lookup_buf );
2894+ if (wasi_ctx -> ns_lookup_list )
2895+ wasm_runtime_free (wasi_ctx -> ns_lookup_list );
2896+
28542897 wasm_runtime_free (wasi_ctx );
28552898 }
28562899}
0 commit comments