wasm_multimodules_program.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #ifndef _WASM_MULTIMODULES_PROGRAM_H_
  2. #define _WASM_MULTIMODULES_PROGRAM_H_
  3. #include "../include/wasm_export.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. typedef struct ConstStrDescription ConstStrDescription;
  8. #define TABLE_SPACE_BITS_LEN 10
  9. #define TABLE_SPACE_SLOT_SIZE (1 << TABLE_SPACE_BITS_LEN)
  10. #define TABLE_SPACE_FOR_INSTS_TOP_BOUNDARY 0x0fff0000u
  11. #define MAX_INST_ID ((TABLE_SPACE_FOR_INSTS_TOP_BOUNDARY >> TABLE_SPACE_BITS_LEN) + 1)
  12. #define BUILTIN_LIBC_INST_ID (MAX_INST_ID + 10u) //keep a gap
  13. #define TABLE_SPACE_FOR_BUILTIN_LIBC ((BUILTIN_LIBC_INST_ID - 1) << 10)
  14. typedef struct WASMModule WASMModule;
  15. struct WASMModuleInstance;
  16. typedef struct WASMModuleCommon WASMModuleCommon;
  17. typedef struct WASMModuleInstanceCommon WASMModuleInstanceCommon;
  18. typedef struct WASMModuleInstance WASMModuleInstance;
  19. typedef struct AOTModuleInstance AOTModuleInstance;
  20. typedef struct WASMTableInstance WASMTableInstance;
  21. typedef struct WASMGlobalInstance WASMGlobalInstance;
  22. typedef struct WASMFunctionInstance WASMFunctionInstance;
  23. typedef struct AOTExportFunctionInstance AOTExportFunctionInstance;
  24. typedef struct WASMExecEnv WASMExecEnv;
  25. typedef struct AOTModule AOTModule;
  26. typedef struct AOTImportFunc AOTImportFunc;
  27. typedef struct WASMRuntime WASMRuntime;
  28. typedef struct WASMProgramInstance WASMProgramInstance;
  29. typedef struct WASMModuleInstanceHead WASMModuleInstanceHead;
  30. typedef struct WASMExport WASMExport;
  31. typedef struct WASMType WASMType;
  32. typedef WASMType AOTFuncType;
  33. typedef struct wasm_runtime_config_ {
  34. bool need_load_dependencies;
  35. bool auto_update_extension;
  36. bool launch_AS_module;
  37. } wasm_runtime_config;
  38. struct WASMRuntime {
  39. wasm_runtime_config config;
  40. // save all const strings and hash values.
  41. HashMap * global_const_str_pool;
  42. // save all const strings pointers based on string index,
  43. // can compare pointers instead of content.
  44. ConstStrDescription * global_const_str_index_array;
  45. uint32 csp_size;
  46. uint32 csp_strings_count;
  47. uint32 csp_free_index;
  48. HashMap * all_loaded_modules;
  49. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  50. WASMProgramInstance * cur_loading_program;
  51. #endif
  52. module_reader reader;
  53. module_destroyer destroyer;
  54. };
  55. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  56. typedef enum {
  57. EARLY_BINDING = 0,
  58. LAZY_BINDING
  59. } WASM_BINDING_MODE;
  60. typedef enum {
  61. FROM_BUILTIN_LIBC = 0, // when root module and dependencies modules independently link against builtin libc.
  62. FROM_ROOT // import malloc/free/realloc from root module exports.
  63. } WASM_IMPORT_MEMOP_MODE;
  64. typedef struct wasm_program_config_ {
  65. WASM_BINDING_MODE binding_mode;
  66. WASM_IMPORT_MEMOP_MODE import_memop_mode;
  67. bool use_tbl_as_cache; // allocate extra table space as resolve cache to store exports resolving result.
  68. bool root_is_AS_module;
  69. bool use_resolve_cache;
  70. } wasm_program_config;
  71. #define BINDING_MODE_MASK 0x1 // 0 is lazy_binding
  72. #define MEM_ALLOCATOR_MASK (0x1 << 1) // 0 is from builtin libc, 1 is from root module.
  73. #define USE_TBL_AS_CACHE_MASK (0x1 << 2)
  74. #define ROOT_IS_AS_MODULE_MASK (0x1 << 3)
  75. #define USE_RESOLVE_CACHE_MASK (0x1 << 4)
  76. #define PROGRAM_RESOLVING_CACHE_LINE_LEN 2
  77. #define PROGRAM_RESOLVING_CACHE_LINE_COUNT 16
  78. typedef struct wasm_resolving_cache_entry_ {
  79. int32 index;
  80. void * func_inst;
  81. #if WASM_ENABLE_AOT != 0
  82. void * module_inst; // interp doesn't need it, we saved the module inst in wasmfunctioninstance.
  83. #endif
  84. } wasm_resolving_cache_entry;
  85. struct WASMProgramInstance {
  86. /* explicit dependency modules, means loading by dlopen */
  87. /* implicit dependency modules, means these modules in dylink section
  88. they will be recorded in WASMModule */
  89. WASMRuntime * runtime;
  90. wasm_program_config config;
  91. /* current program's root module instance, used to allocate init
  92. memory space for other modules */
  93. WASMModuleInstanceCommon * root_module_inst;
  94. // bh_list loading_modules_list;
  95. /* all dependency modules of current instance, avoid loading same module at runtime */
  96. /* they includes implicit modules comes from root module and explicit modules added
  97. at runtime by dlopen() */
  98. HashMap * global_modules_inst_name_hmap;
  99. wasm_resolving_cache_entry * resolving_cache;
  100. // alloc module instance id and table space according to instance id.
  101. HashMap * global_modules_inst_id_hmap;
  102. uint32 next_free_inst_id;
  103. // here, use a function array to simulate a internal libc module.
  104. // a little bit ugly, seems we should create a real libc wasm module internally.
  105. #if WASM_ENABLE_LIBC_BUILTIN != 0
  106. void ** builtin_libc_funcs;
  107. uint32 builtin_libc_funcs_count;
  108. uint32 builtin_libc_funcs_size;
  109. /* memory allocated in user linear space, it's wasm linear addr, uint32 */
  110. uint32 ctype_tolower_loc_space;
  111. #endif
  112. bool clean_all;
  113. WASMModuleInstanceCommon * exception_inst;
  114. char *error_buf;
  115. uint32 error_buf_size;
  116. };
  117. #endif
  118. void
  119. wasm_runtime_set_module_reader(const module_reader reader_cb,
  120. const module_destroyer destroyer_cb);
  121. module_reader
  122. wasm_runtime_get_module_reader();
  123. module_destroyer
  124. wasm_runtime_get_module_destroyer();
  125. WASMModuleCommon *
  126. wasm_runtime_get_module_by_name(WASMRuntime * runtime, const ConstStrDescription * module_name);
  127. bool
  128. wasm_runtime_is_system_symbol(WASMRuntime * runtime, const ConstStrDescription * key);
  129. bool
  130. wasm_runtime_is_memop_symbol(WASMRuntime * runtime, const ConstStrDescription * key);
  131. uint32
  132. wasm_runtime_get_syssymbol_id(WASMRuntime * runtime, const ConstStrDescription * key);
  133. const ConstStrDescription *
  134. wasm_runtime_records_const_filename_string(WASMRuntime * runtime,
  135. const char * str, const uint32 len,
  136. char* error_buf, uint32 error_buf_size);
  137. const ConstStrDescription *
  138. wasm_runtime_records_const_string(WASMRuntime * runtime,
  139. const char * str, const uint32 len,
  140. char* error_buf, uint32 error_buf_size);
  141. const ConstStrDescription *
  142. upgrade_module_extension(const WASMRuntime *runtime,
  143. const ConstStrDescription * key_module_name,
  144. const package_type_t expected_module_type,
  145. char * error_buf,
  146. uint32 error_buf_size);
  147. WASMRuntime *
  148. wasm_runtime_get_runtime();
  149. bool
  150. wasm_runtime_runtime_init(bool standalone, bool auto_ext_name);
  151. void
  152. wasm_runtime_runtime_destroy();
  153. WASMModuleInstanceCommon *
  154. wasm_program_get_root_module_from_inst(const WASMModuleInstanceCommon * module_inst);
  155. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  156. #if WASM_ENABLE_LIBC_BUILTIN != 0
  157. uint32
  158. wasm_program_get_ctype_tolower_mem(WASMModuleInstanceCommon * module_inst);
  159. void
  160. wasm_program_set_ctype_tolower_mem(WASMModuleInstanceCommon * module_inst, uint32 addr);
  161. #endif
  162. WASMProgramInstance *
  163. wasm_runtime_create_program_internal(
  164. char * error_buf, uint32 error_buf_size,
  165. uint32 dlopen_mode);
  166. void
  167. wasm_runtime_destroy_program_internal(WASMProgramInstance * program);
  168. bool
  169. wasm_program_is_root_module(const WASMModuleInstanceCommon * module_inst);
  170. WASMModuleInstanceCommon *
  171. wasm_program_get_root_module(const WASMProgramInstance * program);
  172. WASMModuleInstanceCommon *
  173. wasm_program_get_root_module_from_env(const wasm_exec_env_t env);
  174. void
  175. wasm_program_set_root_module(WASMProgramInstance * program, const WASMModuleInstanceCommon * module_inst);
  176. void
  177. wasm_program_remove_module_inst_from_name_hmap(WASMProgramInstance * program, WASMModuleInstance * module_inst);
  178. void
  179. wasm_program_cache_resolve_result(WASMProgramInstance * program, int32 id, void * result_func, void * module_inst);
  180. void *
  181. wasm_program_lookup_cached_resolving_func(WASMProgramInstance * program, int32 id);
  182. void
  183. wasm_program_invalidate_cached_wasm_func(WASMProgramInstance * program, wasm_module_inst_t module_inst);
  184. uint32
  185. wasm_program_alloc_module_instance_id(WASMProgramInstance * program, WASMModuleInstanceCommon * module_inst);
  186. void
  187. wasm_program_free_module_instance_id(WASMProgramInstance * program, uint32 inst_id);
  188. WASMModuleInstanceCommon *
  189. wasm_program_get_module_inst_by_id(WASMProgramInstance * program, uint32 inst_idx);
  190. WASMModuleInstanceCommon *
  191. wasm_program_get_module_inst_by_name(WASMProgramInstance * program, const ConstStrDescription * module_name);
  192. WASMModuleInstanceCommon *
  193. wasm_program_get_dep_module_inst_by_name(WASMModuleInstanceCommon * caller_module_inst, const ConstStrDescription * module_name);
  194. bool
  195. wasm_program_insert_module_inst_by_name(WASMProgramInstance * program,
  196. WASMModuleInstanceCommon * module_inst,
  197. const ConstStrDescription * module_name);
  198. WASMModuleInstanceCommon *
  199. wasm_program_instantiate_dependencies(WASMRuntime * runtime,
  200. WASMProgramInstance * program_inst,
  201. WASMModuleInstanceCommon * caller_module_inst,
  202. WASMModuleCommon * module);
  203. void
  204. wasm_program_close_dependencies(wasm_module_inst_t module_inst,
  205. uint32 inst_id);
  206. WASMModuleInstanceCommon *
  207. wasm_program_open_dependencies_general(WASMRuntime * runtime,
  208. WASMProgramInstance * program_inst,
  209. WASMModuleInstanceCommon * caller_module_inst,
  210. const char * path);
  211. uint32
  212. wasm_program_open_dependencies(wasm_module_inst_t module_inst,
  213. const char * path);
  214. uint32
  215. wasm_program_lookup_symbol_from_module(wasm_module_inst_t caller_module,
  216. uint32 module_handle, const char * symbol);
  217. bool
  218. wasm_program_resolve_op_call(WASMProgramInstance * program,
  219. WASMModuleInstance * caller_module_inst,
  220. WASMModuleInstance ** p_callee_module_inst,
  221. WASMFunctionInstance ** p_call_func);
  222. bool
  223. wasm_program_resolve_aot_function(WASMProgramInstance * program,
  224. AOTModuleInstance * caller_module_inst,
  225. AOTModuleInstance ** p_callee_module_inst,
  226. uint32 import_func_id);
  227. uint32
  228. wasm_program_alloc_table_space_by_size(uint32 inst_id,
  229. uint32 needed_size);
  230. uint32
  231. wasm_program_alloc_table_space_by_table(WASMModuleInstance * module_inst,
  232. WASMTableInstance * table);
  233. bool
  234. wasm_program_link_sp_wasm_globals(WASMProgramInstance * program,
  235. WASMGlobalInstance * global,
  236. const ConstStrDescription * field_name);
  237. bool
  238. wasm_program_link_got_globals(WASMProgramInstance * program,
  239. WASMModuleInstance * module_inst,
  240. WASMGlobalInstance * global,
  241. const ConstStrDescription * field_name);
  242. #if WASM_ENABLE_AOT != 0
  243. bool
  244. wasm_program_link_sp_aot_globals(WASMProgramInstance * program,
  245. uint8 * p_global_data,
  246. const ConstStrDescription * global_name);
  247. bool
  248. wasm_program_link_aot_got_globals(WASMProgramInstance * program,
  249. AOTModuleInstance * module_inst,
  250. uint8 * p_global_data,
  251. const ConstStrDescription * field_name);
  252. #endif
  253. bool
  254. wasm_program_resolve_op_call_indirect(WASMProgramInstance * program,
  255. WASMModuleInstance * module_inst,
  256. uint32 tbl_idx,
  257. int32 tbl_slot_id,
  258. WASMType * func_type,
  259. WASMModuleInstance ** p_callee_module_inst,
  260. WASMFunctionInstance ** p_call_func);
  261. #if WASM_ENABLE_AOT != 0
  262. bool
  263. wasm_program_resolve_aot_op_call_indirect(WASMExecEnv *exec_env,
  264. WASMProgramInstance * program,
  265. AOTModuleInstance * module_inst,
  266. uint32 tbl_idx,
  267. int32 table_elem_idx,
  268. uint32 expected_type_idx,
  269. AOTFuncType * expected_type,
  270. AOTModuleInstance ** p_callee_module_inst,
  271. AOTModule ** p_callee_module,
  272. uint32 * p_call_func_index,
  273. AOTImportFunc ** p_import_func);
  274. #endif
  275. bool
  276. wasm_program_validate_mode_compatiability(WASMProgramInstance * program);
  277. WASMModuleCommon *
  278. load_implicit_dependency_module(const WASMModuleCommon *parent_module,
  279. const ConstStrDescription * key,
  280. char * error_buf,
  281. uint32 error_buf_size);
  282. #endif
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif