wasm_runtime.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_RUNTIME_H
  6. #define _WASM_RUNTIME_H
  7. #include "wasm.h"
  8. #include "bh_atomic.h"
  9. #include "bh_bitmap.h"
  10. #include "bh_hashmap.h"
  11. #include "../common/wasm_runtime_common.h"
  12. #include "../common/wasm_exec_env.h"
  13. #if WASM_ENABLE_WASI_NN != 0
  14. #include "../libraries/wasi-nn/src/wasi_nn_private.h"
  15. #endif
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define EXCEPTION_BUF_LEN 128
  20. typedef struct WASMModuleInstance WASMModuleInstance;
  21. typedef struct WASMFunctionInstance WASMFunctionInstance;
  22. typedef struct WASMMemoryInstance WASMMemoryInstance;
  23. typedef struct WASMTableInstance WASMTableInstance;
  24. typedef struct WASMGlobalInstance WASMGlobalInstance;
  25. #if WASM_ENABLE_TAGS != 0
  26. typedef struct WASMTagInstance WASMTagInstance;
  27. #endif
  28. /**
  29. * When LLVM JIT, WAMR compiler or AOT is enabled, we should ensure that
  30. * some offsets of the same field in the interpreter module instance and
  31. * aot module instance are the same, so that the LLVM JITed/AOTed code
  32. * can smoothly access the interpreter module instance.
  33. * Same for the memory instance and table instance.
  34. * We use the macro DefPointer to define some related pointer fields.
  35. */
  36. #if (WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 \
  37. || WASM_ENABLE_AOT != 0) \
  38. && UINTPTR_MAX == UINT32_MAX
  39. /* Add u32 padding if LLVM JIT, WAMR compiler or AOT is enabled on
  40. 32-bit platform */
  41. #define DefPointer(type, field) \
  42. type field; \
  43. uint32 field##_padding
  44. #else
  45. #define DefPointer(type, field) type field
  46. #endif
  47. typedef enum WASMExceptionID {
  48. EXCE_UNREACHABLE = 0,
  49. EXCE_OUT_OF_MEMORY,
  50. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS,
  51. EXCE_INTEGER_OVERFLOW,
  52. EXCE_INTEGER_DIVIDE_BY_ZERO,
  53. EXCE_INVALID_CONVERSION_TO_INTEGER,
  54. EXCE_INVALID_FUNCTION_TYPE_INDEX,
  55. EXCE_INVALID_FUNCTION_INDEX,
  56. EXCE_UNDEFINED_ELEMENT,
  57. EXCE_UNINITIALIZED_ELEMENT,
  58. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  59. EXCE_NATIVE_STACK_OVERFLOW,
  60. EXCE_UNALIGNED_ATOMIC,
  61. EXCE_AUX_STACK_OVERFLOW,
  62. EXCE_AUX_STACK_UNDERFLOW,
  63. EXCE_OUT_OF_BOUNDS_TABLE_ACCESS,
  64. EXCE_OPERAND_STACK_OVERFLOW,
  65. EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC,
  66. EXCE_ALREADY_THROWN,
  67. EXCE_NUM,
  68. } WASMExceptionID;
  69. typedef union {
  70. uint64 u64;
  71. uint32 u32[2];
  72. } MemBound;
  73. struct WASMMemoryInstance {
  74. /* Module type */
  75. uint32 module_type;
  76. /* Whether the memory is shared */
  77. uint8 is_shared_memory;
  78. /* One byte padding */
  79. uint8 __padding__;
  80. /* Reference count of the memory instance:
  81. 0: non-shared memory, > 0: shared memory */
  82. bh_atomic_16_t ref_count;
  83. /* Number bytes per page */
  84. uint32 num_bytes_per_page;
  85. /* Current page count */
  86. uint32 cur_page_count;
  87. /* Maximum page count */
  88. uint32 max_page_count;
  89. /* Memory data size */
  90. uint32 memory_data_size;
  91. /**
  92. * Memory data begin address, Note:
  93. * the app-heap might be inserted in to the linear memory,
  94. * when memory is re-allocated, the heap data and memory data
  95. * must be copied to new memory also
  96. */
  97. DefPointer(uint8 *, memory_data);
  98. /* Memory data end address */
  99. DefPointer(uint8 *, memory_data_end);
  100. /* Heap data base address */
  101. DefPointer(uint8 *, heap_data);
  102. /* Heap data end address */
  103. DefPointer(uint8 *, heap_data_end);
  104. /* The heap created */
  105. DefPointer(void *, heap_handle);
  106. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  107. || WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_AOT != 0
  108. MemBound mem_bound_check_1byte;
  109. MemBound mem_bound_check_2bytes;
  110. MemBound mem_bound_check_4bytes;
  111. MemBound mem_bound_check_8bytes;
  112. MemBound mem_bound_check_16bytes;
  113. #endif
  114. };
  115. struct WASMTableInstance {
  116. /* Current size */
  117. uint32 cur_size;
  118. /* Maximum size */
  119. uint32 max_size;
  120. /* Table elements */
  121. uint32 elems[1];
  122. };
  123. struct WASMGlobalInstance {
  124. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  125. uint8 type;
  126. /* mutable or constant */
  127. bool is_mutable;
  128. /* data offset to base_addr of WASMMemoryInstance */
  129. uint32 data_offset;
  130. /* initial value */
  131. WASMValue initial_value;
  132. #if WASM_ENABLE_MULTI_MODULE != 0
  133. /* just for import, keep the reference here */
  134. WASMModuleInstance *import_module_inst;
  135. WASMGlobalInstance *import_global_inst;
  136. #endif
  137. };
  138. struct WASMFunctionInstance {
  139. /* whether it is import function or WASM function */
  140. bool is_import_func;
  141. /* parameter count */
  142. uint16 param_count;
  143. /* local variable count, 0 for import function */
  144. uint16 local_count;
  145. /* cell num of parameters */
  146. uint16 param_cell_num;
  147. /* cell num of return type */
  148. uint16 ret_cell_num;
  149. /* cell num of local variables, 0 for import function */
  150. uint16 local_cell_num;
  151. #if WASM_ENABLE_FAST_INTERP != 0
  152. /* cell num of consts */
  153. uint16 const_cell_num;
  154. #endif
  155. uint16 *local_offsets;
  156. /* parameter types */
  157. uint8 *param_types;
  158. /* local types, NULL for import function */
  159. uint8 *local_types;
  160. union {
  161. WASMFunctionImport *func_import;
  162. WASMFunction *func;
  163. } u;
  164. #if WASM_ENABLE_MULTI_MODULE != 0
  165. WASMModuleInstance *import_module_inst;
  166. WASMFunctionInstance *import_func_inst;
  167. #endif
  168. #if WASM_ENABLE_PERF_PROFILING != 0
  169. /* total execution time */
  170. uint64 total_exec_time;
  171. /* total execution count */
  172. uint32 total_exec_cnt;
  173. /* children execution time */
  174. uint64 children_exec_time;
  175. #endif
  176. };
  177. #if WASM_ENABLE_TAGS != 0
  178. struct WASMTagInstance {
  179. bool is_import_tag;
  180. /* tag attribute */
  181. uint8 attribute;
  182. /* tag type index */
  183. uint32 type;
  184. union {
  185. WASMTagImport *tag_import;
  186. WASMTag *tag;
  187. } u;
  188. #if WASM_ENABLE_MULTI_MODULE != 0
  189. WASMModuleInstance *import_module_inst;
  190. WASMTagInstance *import_tag_inst;
  191. #endif
  192. };
  193. #endif
  194. #if WASM_ENABLE_EXCE_HANDLING != 0
  195. #define INVALID_TAGINDEX ((uint32)0xFFFFFFFF)
  196. #define SET_INVALID_TAGINDEX(tag) (tag = INVALID_TAGINDEX)
  197. #define IS_INVALID_TAGINDEX(tag) ((tag & INVALID_TAGINDEX) == INVALID_TAGINDEX)
  198. #endif
  199. typedef struct WASMExportFuncInstance {
  200. char *name;
  201. WASMFunctionInstance *function;
  202. } WASMExportFuncInstance;
  203. typedef struct WASMExportGlobInstance {
  204. char *name;
  205. WASMGlobalInstance *global;
  206. } WASMExportGlobInstance;
  207. typedef struct WASMExportTabInstance {
  208. char *name;
  209. WASMTableInstance *table;
  210. } WASMExportTabInstance;
  211. typedef struct WASMExportMemInstance {
  212. char *name;
  213. WASMMemoryInstance *memory;
  214. } WASMExportMemInstance;
  215. #if WASM_ENABLE_TAGS != 0
  216. typedef struct WASMExportTagInstance {
  217. char *name;
  218. WASMTagInstance *tag;
  219. } WASMExportTagInstance;
  220. #endif
  221. /* wasm-c-api import function info */
  222. typedef struct CApiFuncImport {
  223. /* host func pointer after linked */
  224. void *func_ptr_linked;
  225. /* whether the host func has env argument */
  226. bool with_env_arg;
  227. /* the env argument of the host func */
  228. void *env_arg;
  229. } CApiFuncImport;
  230. /* The common part of WASMModuleInstanceExtra and AOTModuleInstanceExtra */
  231. typedef struct WASMModuleInstanceExtraCommon {
  232. CApiFuncImport *c_api_func_imports;
  233. void *contexts[WASM_MAX_INSTANCE_CONTEXTS];
  234. /* pointer to the exec env currently used */
  235. WASMExecEnv *cur_exec_env;
  236. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  237. /* Disable bounds checks or not */
  238. bool disable_bounds_checks;
  239. #endif
  240. #if WASM_ENABLE_BULK_MEMORY != 0
  241. bh_bitmap *data_dropped;
  242. #endif
  243. #if WASM_ENABLE_REF_TYPES != 0
  244. bh_bitmap *elem_dropped;
  245. #endif
  246. } WASMModuleInstanceExtraCommon;
  247. /* Extra info of WASM module instance for interpreter/jit mode */
  248. typedef struct WASMModuleInstanceExtra {
  249. WASMModuleInstanceExtraCommon common;
  250. WASMGlobalInstance *globals;
  251. WASMFunctionInstance *functions;
  252. uint32 global_count;
  253. uint32 function_count;
  254. WASMFunctionInstance *start_function;
  255. WASMFunctionInstance *malloc_function;
  256. WASMFunctionInstance *free_function;
  257. WASMFunctionInstance *retain_function;
  258. RunningMode running_mode;
  259. #if WASM_ENABLE_MULTI_MODULE != 0
  260. bh_list sub_module_inst_list_head;
  261. bh_list *sub_module_inst_list;
  262. /* linked table instances of import table instances */
  263. WASMTableInstance **table_insts_linked;
  264. #endif
  265. #if WASM_ENABLE_TAGS != 0
  266. uint32 tag_count;
  267. uint32 export_tag_count;
  268. WASMTagInstance *tags;
  269. WASMExportTagInstance *export_tags;
  270. void **import_tag_ptrs;
  271. #endif
  272. #if WASM_ENABLE_MEMORY_PROFILING != 0
  273. uint32 max_aux_stack_used;
  274. #endif
  275. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  276. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  277. && WASM_ENABLE_LAZY_JIT != 0)
  278. WASMModuleInstance *next;
  279. #endif
  280. } WASMModuleInstanceExtra;
  281. struct AOTFuncPerfProfInfo;
  282. struct WASMModuleInstance {
  283. /* Module instance type, for module instance loaded from
  284. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  285. for module instance loaded from AOT file, this field is
  286. Wasm_Module_AoT, and this structure should be treated as
  287. AOTModuleInstance structure. */
  288. uint32 module_type;
  289. uint32 memory_count;
  290. DefPointer(WASMMemoryInstance **, memories);
  291. /* global and table info */
  292. uint32 global_data_size;
  293. uint32 table_count;
  294. DefPointer(uint8 *, global_data);
  295. /* For AOTModuleInstance, it denotes `AOTTableInstance *` */
  296. DefPointer(WASMTableInstance **, tables);
  297. /* import func ptrs + llvm jit func ptrs */
  298. DefPointer(void **, func_ptrs);
  299. /* function type indexes */
  300. DefPointer(uint32 *, func_type_indexes);
  301. uint32 export_func_count;
  302. uint32 export_global_count;
  303. uint32 export_memory_count;
  304. uint32 export_table_count;
  305. /* For AOTModuleInstance, it denotes `AOTFunctionInstance *` */
  306. DefPointer(WASMExportFuncInstance *, export_functions);
  307. DefPointer(WASMExportGlobInstance *, export_globals);
  308. DefPointer(WASMExportMemInstance *, export_memories);
  309. DefPointer(WASMExportTabInstance *, export_tables);
  310. /* The exception buffer of wasm interpreter for current thread. */
  311. char cur_exception[EXCEPTION_BUF_LEN];
  312. /* The WASM module or AOT module, for AOTModuleInstance,
  313. it denotes `AOTModule *` */
  314. DefPointer(WASMModule *, module);
  315. DefPointer(void *, used_to_be_wasi_ctx); /* unused */
  316. DefPointer(WASMExecEnv *, exec_env_singleton);
  317. /* Array of function pointers to import functions,
  318. not available in AOTModuleInstance */
  319. DefPointer(void **, import_func_ptrs);
  320. /* Array of function pointers to fast jit functions,
  321. not available in AOTModuleInstance:
  322. Only when the multi-tier JIT macros are all enabled and the running
  323. mode of current module instance is set to Mode_Fast_JIT, runtime
  324. will allocate new memory for it, otherwise it always points to the
  325. module->fast_jit_func_ptrs */
  326. DefPointer(void **, fast_jit_func_ptrs);
  327. /* The custom data that can be set/get by wasm_{get|set}_custom_data */
  328. DefPointer(void *, custom_data);
  329. /* Stack frames, used in call stack dump and perf profiling */
  330. DefPointer(Vector *, frames);
  331. /* Function performance profiling info list, only available
  332. in AOTModuleInstance */
  333. DefPointer(struct AOTFuncPerfProfInfo *, func_perf_profilings);
  334. /* WASM/AOT module extra info, for AOTModuleInstance,
  335. it denotes `AOTModuleInstanceExtra *` */
  336. DefPointer(WASMModuleInstanceExtra *, e);
  337. /* Default WASM operand stack size */
  338. uint32 default_wasm_stack_size;
  339. uint32 reserved[3];
  340. /*
  341. * +------------------------------+ <-- memories
  342. * | WASMMemoryInstance[mem_count], mem_count is always 1 for LLVM JIT/AOT
  343. * +------------------------------+ <-- global_data
  344. * | global data
  345. * +------------------------------+ <-- tables
  346. * | WASMTableInstance[table_count]
  347. * +------------------------------+ <-- e
  348. * | WASMModuleInstanceExtra
  349. * +------------------------------+
  350. */
  351. union {
  352. uint64 _make_it_8_byte_aligned_;
  353. WASMMemoryInstance memory_instances[1];
  354. uint8 bytes[1];
  355. } global_table_data;
  356. };
  357. struct WASMInterpFrame;
  358. typedef struct WASMInterpFrame WASMRuntimeFrame;
  359. #if WASM_ENABLE_MULTI_MODULE != 0
  360. typedef struct WASMSubModInstNode {
  361. bh_list_link l;
  362. /* point to a string pool */
  363. const char *module_name;
  364. WASMModuleInstance *module_inst;
  365. } WASMSubModInstNode;
  366. #endif
  367. /**
  368. * Return the code block of a function.
  369. *
  370. * @param func the WASM function instance
  371. *
  372. * @return the code block of the function
  373. */
  374. static inline uint8 *
  375. wasm_get_func_code(WASMFunctionInstance *func)
  376. {
  377. #if WASM_ENABLE_FAST_INTERP == 0
  378. return func->is_import_func ? NULL : func->u.func->code;
  379. #else
  380. return func->is_import_func ? NULL : func->u.func->code_compiled;
  381. #endif
  382. }
  383. /**
  384. * Return the code block end of a function.
  385. *
  386. * @param func the WASM function instance
  387. *
  388. * @return the code block end of the function
  389. */
  390. static inline uint8 *
  391. wasm_get_func_code_end(WASMFunctionInstance *func)
  392. {
  393. #if WASM_ENABLE_FAST_INTERP == 0
  394. return func->is_import_func ? NULL
  395. : func->u.func->code + func->u.func->code_size;
  396. #else
  397. return func->is_import_func
  398. ? NULL
  399. : func->u.func->code_compiled + func->u.func->code_compiled_size;
  400. #endif
  401. }
  402. WASMModule *
  403. wasm_load(uint8 *buf, uint32 size,
  404. #if WASM_ENABLE_MULTI_MODULE != 0
  405. bool main_module,
  406. #endif
  407. char *error_buf, uint32 error_buf_size);
  408. WASMModule *
  409. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  410. uint32 error_buf_size);
  411. void
  412. wasm_unload(WASMModule *module);
  413. WASMModuleInstance *
  414. wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
  415. WASMExecEnv *exec_env_main, uint32 stack_size,
  416. uint32 heap_size, char *error_buf, uint32 error_buf_size);
  417. void
  418. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst);
  419. double
  420. wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst);
  421. double
  422. wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
  423. const char *func_name);
  424. void
  425. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
  426. bool
  427. wasm_set_running_mode(WASMModuleInstance *module_inst,
  428. RunningMode running_mode);
  429. WASMFunctionInstance *
  430. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  431. const char *signature);
  432. #if WASM_ENABLE_MULTI_MODULE != 0
  433. WASMGlobalInstance *
  434. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
  435. WASMMemoryInstance *
  436. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
  437. WASMTableInstance *
  438. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
  439. #if WASM_ENABLE_TAGS != 0
  440. WASMTagInstance *
  441. wasm_lookup_tag(const WASMModuleInstance *module_inst, const char *name,
  442. const char *signature);
  443. #endif
  444. #endif
  445. bool
  446. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  447. unsigned argc, uint32 argv[]);
  448. void
  449. wasm_set_exception(WASMModuleInstance *module, const char *exception);
  450. void
  451. wasm_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id);
  452. const char *
  453. wasm_get_exception(WASMModuleInstance *module);
  454. /**
  455. * @brief Copy exception in buffer passed as parameter. Thread-safe version of
  456. * `wasm_get_exception()`
  457. * @note Buffer size must be no smaller than EXCEPTION_BUF_LEN
  458. * @return true if exception found
  459. */
  460. bool
  461. wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf);
  462. uint32
  463. wasm_module_malloc_internal(WASMModuleInstance *module_inst,
  464. WASMExecEnv *exec_env, uint32 size,
  465. void **p_native_addr);
  466. uint32
  467. wasm_module_realloc_internal(WASMModuleInstance *module_inst,
  468. WASMExecEnv *exec_env, uint32 ptr, uint32 size,
  469. void **p_native_addr);
  470. void
  471. wasm_module_free_internal(WASMModuleInstance *module_inst,
  472. WASMExecEnv *exec_env, uint32 ptr);
  473. uint32
  474. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  475. void **p_native_addr);
  476. uint32
  477. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  478. void **p_native_addr);
  479. void
  480. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr);
  481. uint32
  482. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  483. uint32 size);
  484. /**
  485. * Check whether the app address and the buf is inside the linear memory,
  486. * and convert the app address into native address
  487. */
  488. bool
  489. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  490. uint32 app_buf_addr, uint32 app_buf_size,
  491. void **p_native_addr);
  492. WASMMemoryInstance *
  493. wasm_get_default_memory(WASMModuleInstance *module_inst);
  494. bool
  495. wasm_enlarge_memory(WASMModuleInstance *module_inst, uint32 inc_page_count);
  496. bool
  497. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  498. uint32 argc, uint32 argv[]);
  499. #if WASM_ENABLE_THREAD_MGR != 0
  500. bool
  501. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size);
  502. bool
  503. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
  504. #endif
  505. void
  506. wasm_get_module_mem_consumption(const WASMModule *module,
  507. WASMModuleMemConsumption *mem_conspn);
  508. void
  509. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module,
  510. WASMModuleInstMemConsumption *mem_conspn);
  511. #if WASM_ENABLE_REF_TYPES != 0
  512. static inline bool
  513. wasm_elem_is_active(uint32 mode)
  514. {
  515. return (mode & 0x1) == 0x0;
  516. }
  517. static inline bool
  518. wasm_elem_is_passive(uint32 mode)
  519. {
  520. return (mode & 0x1) == 0x1;
  521. }
  522. static inline bool
  523. wasm_elem_is_declarative(uint32 mode)
  524. {
  525. return (mode & 0x3) == 0x3;
  526. }
  527. bool
  528. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  529. uint32 inc_entries, uint32 init_val);
  530. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  531. static inline WASMTableInstance *
  532. wasm_get_table_inst(const WASMModuleInstance *module_inst, uint32 tbl_idx)
  533. {
  534. /* careful, it might be a table in another module */
  535. WASMTableInstance *tbl_inst = module_inst->tables[tbl_idx];
  536. #if WASM_ENABLE_MULTI_MODULE != 0
  537. if (tbl_idx < module_inst->module->import_table_count
  538. && module_inst->e->table_insts_linked[tbl_idx]) {
  539. tbl_inst = module_inst->e->table_insts_linked[tbl_idx];
  540. }
  541. #endif
  542. bh_assert(tbl_inst);
  543. return tbl_inst;
  544. }
  545. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  546. bool
  547. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);
  548. /**
  549. * @brief Dump wasm call stack or get the size
  550. *
  551. * @param exec_env the execution environment
  552. * @param print whether to print to stdout or not
  553. * @param buf buffer to store the dumped content
  554. * @param len length of the buffer
  555. *
  556. * @return when print is true, return the bytes printed out to stdout; when
  557. * print is false and buf is NULL, return the size required to store the
  558. * callstack content; when print is false and buf is not NULL, return the size
  559. * dumped to the buffer, 0 means error and data in buf may be invalid
  560. */
  561. uint32
  562. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  563. uint32 len);
  564. #endif
  565. const uint8 *
  566. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  567. uint32 *len);
  568. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  569. || WASM_ENABLE_WAMR_COMPILER != 0
  570. void
  571. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id);
  572. /**
  573. * Check whether the app address and the buf is inside the linear memory,
  574. * and convert the app address into native address
  575. */
  576. bool
  577. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  578. uint32 app_buf_addr, uint32 app_buf_size,
  579. void **p_native_addr);
  580. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  581. || WASM_ENABLE_WAMR_COMPILER != 0 */
  582. #if WASM_ENABLE_FAST_JIT != 0
  583. bool
  584. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  585. uint32 type_idx, uint32 argc, uint32 *argv);
  586. bool
  587. fast_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
  588. struct WASMInterpFrame *prev_frame);
  589. #endif
  590. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  591. bool
  592. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  593. uint32 argc, uint32 *argv);
  594. bool
  595. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  596. uint32 *argv);
  597. #if WASM_ENABLE_BULK_MEMORY != 0
  598. bool
  599. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  600. uint32 offset, uint32 len, uint32 dst);
  601. bool
  602. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index);
  603. #endif
  604. #if WASM_ENABLE_REF_TYPES != 0
  605. void
  606. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx);
  607. void
  608. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  609. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  610. uint32 dst_offset);
  611. void
  612. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  613. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  614. uint32 dst_offset);
  615. void
  616. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  617. uint32 length, uint32 val, uint32 data_offset);
  618. uint32
  619. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  620. uint32 inc_entries, uint32 init_val);
  621. #endif
  622. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  623. bool
  624. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);
  625. void
  626. llvm_jit_free_frame(WASMExecEnv *exec_env);
  627. #endif
  628. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  629. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  630. void
  631. wasm_propagate_wasi_args(WASMModule *module);
  632. #endif
  633. #if WASM_ENABLE_THREAD_MGR != 0
  634. void
  635. exception_lock(WASMModuleInstance *module_inst);
  636. void
  637. exception_unlock(WASMModuleInstance *module_inst);
  638. #else
  639. #define exception_lock(module_inst) (void)(module_inst)
  640. #define exception_unlock(module_inst) (void)(module_inst)
  641. #endif
  642. #ifdef __cplusplus
  643. }
  644. #endif
  645. #endif /* end of _WASM_RUNTIME_H */