@@ -1969,14 +1969,36 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
19691969 argc , -1 , -1 , -1 );
19701970}
19711971
1972+ void
1973+ wasm_runtime_set_wasi_addr_pool (wasm_module_t module , const char * addr_pool [],
1974+ uint32 addr_pool_size )
1975+ {
1976+ WASIArguments * wasi_args = NULL ;
1977+
1978+ #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
1979+ if (module -> module_type == Wasm_Module_Bytecode )
1980+ wasi_args = & ((WASMModule * )module )-> wasi_args ;
1981+ #endif
1982+ #if WASM_ENABLE_AOT != 0
1983+ if (module -> module_type == Wasm_Module_AoT )
1984+ wasi_args = & ((AOTModule * )module )-> wasi_args ;
1985+ #endif
1986+
1987+ if (wasi_args ) {
1988+ wasi_args -> addr_pool = addr_pool ;
1989+ wasi_args -> addr_count = addr_pool_size ;
1990+ }
1991+ }
1992+
19721993#if WASM_ENABLE_UVWASI == 0
19731994bool
19741995wasm_runtime_init_wasi (WASMModuleInstanceCommon * module_inst ,
19751996 const char * dir_list [], uint32 dir_count ,
19761997 const char * map_dir_list [], uint32 map_dir_count ,
1977- const char * env [], uint32 env_count , char * argv [],
1978- uint32 argc , int stdinfd , int stdoutfd , int stderrfd ,
1979- char * error_buf , uint32 error_buf_size )
1998+ const char * env [], uint32 env_count ,
1999+ const char * addr_pool [], uint32 addr_pool_size ,
2000+ char * argv [], uint32 argc , int stdinfd , int stdoutfd ,
2001+ int stderrfd , char * error_buf , uint32 error_buf_size )
19802002{
19812003 WASIContext * wasi_ctx ;
19822004 char * argv_buf = NULL ;
@@ -1988,8 +2010,10 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
19882010 struct fd_table * curfds = NULL ;
19892011 struct fd_prestats * prestats = NULL ;
19902012 struct argv_environ_values * argv_environ = NULL ;
2013+ struct addr_pool * apool = NULL ;
19912014 bool fd_table_inited = false, fd_prestats_inited = false;
19922015 bool argv_environ_inited = false;
2016+ bool addr_pool_inited = false;
19932017 __wasi_fd_t wasm_fd = 3 ;
19942018 int32 raw_fd ;
19952019 char * path , resolved_path [PATH_MAX ];
@@ -2063,7 +2087,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
20632087 if (!(curfds = wasm_runtime_malloc (sizeof (struct fd_table )))
20642088 || !(prestats = wasm_runtime_malloc (sizeof (struct fd_prestats )))
20652089 || !(argv_environ =
2066- wasm_runtime_malloc (sizeof (struct argv_environ_values )))) {
2090+ wasm_runtime_malloc (sizeof (struct argv_environ_values )))
2091+ || !(apool = wasm_runtime_malloc (sizeof (struct addr_pool )))) {
20672092 set_error_buf (error_buf , error_buf_size ,
20682093 "Init wasi environment failed: allocate memory failed" );
20692094 goto fail ;
@@ -2094,6 +2119,14 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
20942119 }
20952120 argv_environ_inited = true;
20962121
2122+ if (!addr_pool_init (apool )) {
2123+ set_error_buf (error_buf , error_buf_size ,
2124+ "Init wasi environment failed: "
2125+ "init the address pool failed" );
2126+ goto fail ;
2127+ }
2128+ addr_pool_inited = true;
2129+
20972130 /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
20982131 if (!fd_table_insert_existing (curfds , 0 , (stdinfd != -1 ) ? stdinfd : 0 )
20992132 || !fd_table_insert_existing (curfds , 1 , (stdoutfd != -1 ) ? stdoutfd : 1 )
@@ -2128,9 +2161,34 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
21282161 fd_prestats_insert (prestats , dir_list [i ], wasm_fd );
21292162 }
21302163
2164+ /* addr_pool(textual) -> apool */
2165+ for (i = 0 ; i < addr_pool_size ; i ++ ) {
2166+ char * cp , * address , * mask ;
2167+ bool ret = false;
2168+
2169+ cp = bh_strdup (addr_pool [i ]);
2170+ if (!cp ) {
2171+ set_error_buf (error_buf , error_buf_size ,
2172+ "Init wasi environment failed: copy address failed" );
2173+ goto fail ;
2174+ }
2175+
2176+ address = strtok (cp , "/" );
2177+ mask = strtok (NULL , "/" );
2178+
2179+ ret = addr_pool_insert (apool , address , (uint8 )(atoi (mask )));
2180+ wasm_runtime_free (cp );
2181+ if (!ret ) {
2182+ set_error_buf (error_buf , error_buf_size ,
2183+ "Init wasi environment failed: store address failed" );
2184+ goto fail ;
2185+ }
2186+ }
2187+
21312188 wasi_ctx -> curfds = curfds ;
21322189 wasi_ctx -> prestats = prestats ;
21332190 wasi_ctx -> argv_environ = argv_environ ;
2191+ wasi_ctx -> addr_pool = apool ;
21342192 wasi_ctx -> argv_buf = argv_buf ;
21352193 wasi_ctx -> argv_list = argv_list ;
21362194 wasi_ctx -> env_buf = env_buf ;
@@ -2145,12 +2203,16 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
21452203 fd_prestats_destroy (prestats );
21462204 if (fd_table_inited )
21472205 fd_table_destroy (curfds );
2206+ if (addr_pool_inited )
2207+ addr_pool_destroy (apool );
21482208 if (curfds )
21492209 wasm_runtime_free (curfds );
21502210 if (prestats )
21512211 wasm_runtime_free (prestats );
21522212 if (argv_environ )
21532213 wasm_runtime_free (argv_environ );
2214+ if (apool )
2215+ wasm_runtime_free (apool );
21542216 if (argv_buf )
21552217 wasm_runtime_free (argv_buf );
21562218 if (argv_list )
@@ -2208,9 +2270,10 @@ bool
22082270wasm_runtime_init_wasi (WASMModuleInstanceCommon * module_inst ,
22092271 const char * dir_list [], uint32 dir_count ,
22102272 const char * map_dir_list [], uint32 map_dir_count ,
2211- const char * env [], uint32 env_count , char * argv [],
2212- uint32 argc , int stdinfd , int stdoutfd , int stderrfd ,
2213- char * error_buf , uint32 error_buf_size )
2273+ const char * env [], uint32 env_count ,
2274+ const char * addr_pool [], uint32 addr_pool_size ,
2275+ char * argv [], uint32 argc , int stdinfd , int stdoutfd ,
2276+ int stderrfd , char * error_buf , uint32 error_buf_size )
22142277{
22152278 uvwasi_t * uvwasi = NULL ;
22162279 uvwasi_options_t init_options ;
@@ -2373,6 +2436,10 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
23732436 fd_prestats_destroy (wasi_ctx -> prestats );
23742437 wasm_runtime_free (wasi_ctx -> prestats );
23752438 }
2439+ if (wasi_ctx -> addr_pool ) {
2440+ addr_pool_destroy (wasi_ctx -> addr_pool );
2441+ wasm_runtime_free (wasi_ctx -> addr_pool );
2442+ }
23762443 if (wasi_ctx -> argv_buf )
23772444 wasm_runtime_free (wasi_ctx -> argv_buf );
23782445 if (wasi_ctx -> argv_list )
0 commit comments