aot_runtime.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _AOT_RUNTIME_H_
  6. #define _AOT_RUNTIME_H_
  7. #include "bh_platform.h"
  8. #include "../common/wasm_runtime_common.h"
  9. #include "../interpreter/wasm_runtime.h"
  10. #include "../compilation/aot.h"
  11. #if WASM_ENABLE_GC != 0
  12. #include "gc_export.h"
  13. #endif
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* Wasm feature supported, mainly used by AOTTargetInfo now */
  18. #define WASM_FEATURE_SIMD_128BIT (1 << 0)
  19. #define WASM_FEATURE_BULK_MEMORY (1 << 1)
  20. #define WASM_FEATURE_MULTI_THREAD (1 << 2)
  21. #define WASM_FEATURE_REF_TYPES (1 << 3)
  22. #define WASM_FEATURE_GARBAGE_COLLECTION (1 << 4)
  23. #define WASM_FEATURE_EXCEPTION_HANDLING (1 << 5)
  24. #define WASM_FEATURE_TINY_STACK_FRAME (1 << 6)
  25. #define WASM_FEATURE_MULTI_MEMORY (1 << 7)
  26. #define WASM_FEATURE_DYNAMIC_LINKING (1 << 8)
  27. #define WASM_FEATURE_COMPONENT_MODEL (1 << 9)
  28. #define WASM_FEATURE_RELAXED_SIMD (1 << 10)
  29. #define WASM_FEATURE_FLEXIBLE_VECTORS (1 << 11)
  30. /* Stack frame is created at the beginning of the function,
  31. * and not at the beginning of each function call */
  32. #define WASM_FEATURE_FRAME_PER_FUNCTION (1 << 12)
  33. #define WASM_FEATURE_FRAME_NO_FUNC_IDX (1 << 13)
  34. typedef enum AOTSectionType {
  35. AOT_SECTION_TYPE_TARGET_INFO = 0,
  36. AOT_SECTION_TYPE_INIT_DATA = 1,
  37. AOT_SECTION_TYPE_TEXT = 2,
  38. AOT_SECTION_TYPE_FUNCTION = 3,
  39. AOT_SECTION_TYPE_EXPORT = 4,
  40. AOT_SECTION_TYPE_RELOCATION = 5,
  41. /*
  42. * Note: We haven't had anything to use AOT_SECTION_TYPE_SIGNATURE.
  43. * It's just reserved for possible module signing features.
  44. */
  45. AOT_SECTION_TYPE_SIGNATURE = 6,
  46. AOT_SECTION_TYPE_CUSTOM = 100,
  47. } AOTSectionType;
  48. typedef enum AOTCustomSectionType {
  49. AOT_CUSTOM_SECTION_RAW = 0,
  50. AOT_CUSTOM_SECTION_NATIVE_SYMBOL = 1,
  51. AOT_CUSTOM_SECTION_ACCESS_CONTROL = 2,
  52. AOT_CUSTOM_SECTION_NAME = 3,
  53. AOT_CUSTOM_SECTION_STRING_LITERAL = 4,
  54. } AOTCustomSectionType;
  55. typedef struct AOTObjectDataSection {
  56. char *name;
  57. uint8 *data;
  58. uint32 size;
  59. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  60. bool is_name_allocated;
  61. bool is_data_allocated;
  62. #endif
  63. } AOTObjectDataSection;
  64. /* Relocation info */
  65. typedef struct AOTRelocation {
  66. uint64 relocation_offset;
  67. int64 relocation_addend;
  68. uint32 relocation_type;
  69. char *symbol_name;
  70. /* index in the symbol offset field */
  71. uint32 symbol_index;
  72. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  73. bool is_symbol_name_allocated;
  74. #endif
  75. } AOTRelocation;
  76. /* Relocation Group */
  77. typedef struct AOTRelocationGroup {
  78. char *section_name;
  79. /* index in the symbol offset field */
  80. uint32 name_index;
  81. uint32 relocation_count;
  82. AOTRelocation *relocations;
  83. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  84. bool is_section_name_allocated;
  85. #endif
  86. } AOTRelocationGroup;
  87. /* AOT function instance */
  88. typedef struct AOTFunctionInstance {
  89. char *func_name;
  90. uint32 func_index;
  91. bool is_import_func;
  92. union {
  93. struct {
  94. AOTFuncType *func_type;
  95. /* function pointer linked */
  96. void *func_ptr;
  97. } func;
  98. AOTImportFunc *func_import;
  99. } u;
  100. } AOTFunctionInstance;
  101. /* Map of a function index to the element ith in
  102. the export functions array */
  103. typedef struct ExportFuncMap {
  104. uint32 func_idx;
  105. uint32 export_idx;
  106. } ExportFuncMap;
  107. typedef struct AOTModuleInstanceExtra {
  108. DefPointer(const uint32 *, stack_sizes);
  109. /*
  110. * Adjusted shared heap based addr to simple the calculation
  111. * in the aot code. The value is:
  112. * shared_heap->base_addr - shared_heap->start_off
  113. */
  114. DefPointer(uint8 *, shared_heap_base_addr_adj);
  115. MemBound shared_heap_start_off;
  116. WASMModuleInstanceExtraCommon common;
  117. /**
  118. * maps of func indexes to export func indexes, which
  119. * is sorted by func index for a quick lookup and is
  120. * created only when first time used.
  121. */
  122. ExportFuncMap *export_func_maps;
  123. AOTFunctionInstance **functions;
  124. uint32 function_count;
  125. #if WASM_ENABLE_MULTI_MODULE != 0
  126. bh_list sub_module_inst_list_head;
  127. bh_list *sub_module_inst_list;
  128. WASMModuleInstanceCommon **import_func_module_insts;
  129. #endif
  130. #if WASM_ENABLE_SHARED_HEAP != 0
  131. WASMSharedHeap *shared_heap;
  132. #endif
  133. } AOTModuleInstanceExtra;
  134. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  135. typedef struct GOTItem {
  136. uint32 func_idx;
  137. struct GOTItem *next;
  138. } GOTItem, *GOTItemList;
  139. #endif
  140. #if WASM_ENABLE_GC != 0
  141. typedef struct LocalRefFlag {
  142. uint32 local_ref_flag_cell_num;
  143. uint8 *local_ref_flags;
  144. } LocalRefFlag;
  145. #endif
  146. typedef struct AOTModule {
  147. uint32 module_type;
  148. /* the package version read from the AOT file */
  149. uint32 package_version;
  150. /* import memories */
  151. uint32 import_memory_count;
  152. AOTImportMemory *import_memories;
  153. /* memory info */
  154. uint32 memory_count;
  155. AOTMemory *memories;
  156. /* init data */
  157. uint32 mem_init_data_count;
  158. AOTMemInitData **mem_init_data_list;
  159. /* native symbol */
  160. void **native_symbol_list;
  161. /* import tables */
  162. uint32 import_table_count;
  163. AOTImportTable *import_tables;
  164. /* tables */
  165. uint32 table_count;
  166. AOTTable *tables;
  167. /* table init data info */
  168. uint32 table_init_data_count;
  169. AOTTableInitData **table_init_data_list;
  170. /* type info */
  171. uint32 type_count;
  172. AOTType **types;
  173. /* import global variable info */
  174. uint32 import_global_count;
  175. AOTImportGlobal *import_globals;
  176. /* global variable info */
  177. uint32 global_count;
  178. AOTGlobal *globals;
  179. /* total global variable size */
  180. uint32 global_data_size;
  181. /* import function info */
  182. uint32 import_func_count;
  183. AOTImportFunc *import_funcs;
  184. /* function info */
  185. uint32 func_count;
  186. /* func pointers of AOTed (un-imported) functions */
  187. void **func_ptrs;
  188. /* func type indexes of AOTed (un-imported) functions */
  189. uint32 *func_type_indexes;
  190. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  191. /* max local cell nums of AOTed (un-imported) functions */
  192. uint32 *max_local_cell_nums;
  193. /* max stack cell nums of AOTed (un-imported) functions */
  194. uint32 *max_stack_cell_nums;
  195. #endif
  196. #if WASM_ENABLE_GC != 0
  197. /* params + locals ref flags of (both import and AOTed) functions */
  198. LocalRefFlag *func_local_ref_flags;
  199. #endif
  200. /* export info */
  201. uint32 export_count;
  202. AOTExport *exports;
  203. /* start function index, -1 denotes no start function */
  204. uint32 start_func_index;
  205. /* start function, point to AOTed function */
  206. void *start_function;
  207. uint32 malloc_func_index;
  208. uint32 free_func_index;
  209. uint32 retain_func_index;
  210. /* AOTed code */
  211. void *code;
  212. uint32 code_size;
  213. /* literal for AOTed code */
  214. uint8 *literal;
  215. uint32 literal_size;
  216. #if defined(BH_PLATFORM_WINDOWS)
  217. /* extra plt data area for __ymm, __xmm and __real constants
  218. in Windows platform */
  219. uint8 *extra_plt_data;
  220. uint32 extra_plt_data_size;
  221. uint32 ymm_plt_count;
  222. uint32 xmm_plt_count;
  223. uint32 real_plt_count;
  224. uint32 float_plt_count;
  225. #endif
  226. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  227. uint32 got_item_count;
  228. GOTItemList got_item_list;
  229. GOTItemList got_item_list_end;
  230. void **got_func_ptrs;
  231. #endif
  232. /* data sections in AOT object file, including .data, .rodata
  233. and .rodata.cstN. */
  234. AOTObjectDataSection *data_sections;
  235. uint32 data_section_count;
  236. /* constant string set */
  237. HashMap *const_str_set;
  238. /* the index of auxiliary __data_end global,
  239. -1 means unexported */
  240. uint32 aux_data_end_global_index;
  241. /* auxiliary __data_end exported by wasm app */
  242. uint64 aux_data_end;
  243. /* the index of auxiliary __heap_base global,
  244. -1 means unexported */
  245. uint32 aux_heap_base_global_index;
  246. /* auxiliary __heap_base exported by wasm app */
  247. uint64 aux_heap_base;
  248. /* the index of auxiliary stack top global,
  249. -1 means unexported */
  250. uint32 aux_stack_top_global_index;
  251. /* auxiliary stack bottom resolved */
  252. uint64 aux_stack_bottom;
  253. /* auxiliary stack size resolved */
  254. uint32 aux_stack_size;
  255. /* is indirect mode or not */
  256. bool is_indirect_mode;
  257. #if WASM_ENABLE_LIBC_WASI != 0
  258. WASIArguments wasi_args;
  259. bool import_wasi_api;
  260. #endif
  261. #if WASM_ENABLE_MULTI_MODULE != 0
  262. /* TODO: add mutex for mutli-thread? */
  263. bh_list import_module_list_head;
  264. bh_list *import_module_list;
  265. #endif
  266. #if WASM_ENABLE_GC != 0
  267. /* Ref types hash set */
  268. HashMap *ref_type_set;
  269. struct WASMRttType **rtt_types;
  270. korp_mutex rtt_type_lock;
  271. #if WASM_ENABLE_STRINGREF != 0
  272. /* special rtts for stringref types
  273. - stringref
  274. - stringview_wtf8
  275. - stringview_wtf16
  276. - stringview_iter
  277. */
  278. struct WASMRttType *stringref_rtts[4];
  279. uint32 string_literal_count;
  280. uint32 *string_literal_lengths;
  281. const uint8 **string_literal_ptrs;
  282. #endif
  283. #endif
  284. #if WASM_ENABLE_DEBUG_AOT != 0
  285. void *elf_hdr;
  286. uint32 elf_size;
  287. #endif
  288. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  289. const char **aux_func_names;
  290. uint32 *aux_func_indexes;
  291. uint32 aux_func_name_count;
  292. #endif
  293. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  294. WASMCustomSection *custom_section_list;
  295. #endif
  296. /* user defined name */
  297. char *name;
  298. /* Whether the underlying wasm binary buffer can be freed */
  299. bool is_binary_freeable;
  300. /* `.data` sections merged into one mmaped to reduce the tlb cache miss */
  301. uint8 *merged_data_sections;
  302. uint32 merged_data_sections_size;
  303. /* `.data` and `.text` sections merged into one large mmaped section */
  304. uint8 *merged_data_text_sections;
  305. uint32 merged_data_text_sections_size;
  306. #if WASM_ENABLE_AOT_STACK_FRAME != 0
  307. uint32 feature_flags;
  308. #endif
  309. } AOTModule;
  310. #define AOTMemoryInstance WASMMemoryInstance
  311. #define AOTTableInstance WASMTableInstance
  312. #define AOTModuleInstance WASMModuleInstance
  313. #if WASM_ENABLE_MULTI_MODULE != 0
  314. #define AOTSubModInstNode WASMSubModInstNode
  315. #endif
  316. /* Target info, read from ELF header of object file */
  317. typedef struct AOTTargetInfo {
  318. /* Binary type, elf32l/elf32b/elf64l/elf64b */
  319. uint16 bin_type;
  320. /* ABI type */
  321. uint16 abi_type;
  322. /* Object file type */
  323. uint16 e_type;
  324. /* Architecture */
  325. uint16 e_machine;
  326. /* Object file version */
  327. uint32 e_version;
  328. /* Processor-specific flags */
  329. uint32 e_flags;
  330. /* Specify wasm features supported */
  331. uint64 feature_flags;
  332. /* Reserved */
  333. uint64 reserved;
  334. /* Arch name */
  335. char arch[16];
  336. } AOTTargetInfo;
  337. typedef struct AOTFuncPerfProfInfo {
  338. /* total execution time */
  339. uint64 total_exec_time;
  340. /* total execution count */
  341. uint32 total_exec_cnt;
  342. /* children execution time */
  343. uint64 children_exec_time;
  344. } AOTFuncPerfProfInfo;
  345. /* AOT auxiliary call stack */
  346. typedef struct AOTFrame {
  347. /* The frame of the caller which is calling current function */
  348. struct AOTFrame *prev_frame;
  349. /* The non-imported function index of current function */
  350. uintptr_t func_index;
  351. /* Used when performance profiling is enabled */
  352. uintptr_t time_started;
  353. /* Used when performance profiling is enabled */
  354. AOTFuncPerfProfInfo *func_perf_prof_info;
  355. /* Instruction pointer: offset to the bytecode array */
  356. uintptr_t ip_offset;
  357. /* Operand stack top pointer of the current frame */
  358. uint32 *sp;
  359. /* Frame ref flags (GC only) */
  360. uint8 *frame_ref;
  361. /**
  362. * Frame data, the layout is:
  363. * local area: parameters and local variables
  364. * stack area: wasm operand stack
  365. * frame ref flags (GC only):
  366. * whether each cell in local and stack area is a GC obj
  367. * currently local's ref flags are stored in AOTModule,
  368. * here we only reserve the padding bytes
  369. */
  370. uint32 lp[1];
  371. } AOTFrame;
  372. #if WASM_ENABLE_STATIC_PGO != 0
  373. /* The bitmaps fields in LLVMProfileRawHeader, LLVMProfileData,
  374. * LLVMProfileData_64 all dummy fields, it's used in MC/DC code coverage
  375. * instead of PGO. See https://llvm.org/docs/InstrProfileFormat.html#bitmap */
  376. typedef struct LLVMProfileRawHeader {
  377. uint64 magic;
  378. uint64 version;
  379. uint64 binary_ids_size;
  380. uint64 num_prof_data;
  381. uint64 padding_bytes_before_counters;
  382. uint64 num_prof_counters;
  383. uint64 padding_bytes_after_counters;
  384. uint64 num_prof_bitmaps;
  385. uint64 padding_bytes_after_bitmaps;
  386. uint64 names_size;
  387. uint64 counters_delta;
  388. uint64 bitmap_delta;
  389. uint64 names_delta;
  390. uint64 value_kind_last;
  391. } LLVMProfileRawHeader;
  392. typedef struct ValueProfNode {
  393. uint64 value;
  394. uint64 count;
  395. struct ValueProfNode *next;
  396. } ValueProfNode;
  397. /* The profiling data of data sections created by aot compiler and
  398. used when profiling, the width of pointer can be 8 bytes (64-bit)
  399. or 4 bytes (32-bit) */
  400. typedef struct LLVMProfileData {
  401. uint64 func_md5;
  402. uint64 func_hash;
  403. uint64 offset_counters;
  404. uint64 offset_bitmaps;
  405. uintptr_t func_ptr;
  406. ValueProfNode **values;
  407. uint32 num_counters;
  408. uint16 num_value_sites[2];
  409. uint32 num_bitmaps;
  410. } LLVMProfileData;
  411. /* The profiling data for writing to the output file, the width of
  412. pointer is 8 bytes suppose we always use wamrc and llvm-profdata
  413. with 64-bit mode */
  414. typedef struct LLVMProfileData_64 {
  415. uint64 func_md5;
  416. uint64 func_hash;
  417. uint64 offset_counters;
  418. uint64 offset_bitmaps;
  419. uint64 func_ptr;
  420. uint64 values;
  421. uint32 num_counters;
  422. uint16 num_value_sites[2];
  423. uint32 num_bitmaps;
  424. } LLVMProfileData_64;
  425. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  426. /**
  427. * Load a AOT module from aot file buffer
  428. * @param buf the byte buffer which contains the AOT file data
  429. * @param size the size of the buffer
  430. * @param error_buf output of the error info
  431. * @param error_buf_size the size of the error string
  432. *
  433. * @return return AOT module loaded, NULL if failed
  434. */
  435. AOTModule *
  436. aot_load_from_aot_file(const uint8 *buf, uint32 size, const LoadArgs *args,
  437. char *error_buf, uint32 error_buf_size);
  438. /**
  439. * Load a AOT module from a specified AOT section list.
  440. *
  441. * @param section_list the section list which contains each section data
  442. * @param error_buf output of the error info
  443. * @param error_buf_size the size of the error string
  444. *
  445. * @return return AOT module loaded, NULL if failed
  446. */
  447. AOTModule *
  448. aot_load_from_sections(AOTSection *section_list, char *error_buf,
  449. uint32 error_buf_size);
  450. /**
  451. * Unload a AOT module.
  452. *
  453. * @param module the module to be unloaded
  454. */
  455. void
  456. aot_unload(AOTModule *module);
  457. /**
  458. * Resolve symbols for an AOT module
  459. */
  460. bool
  461. aot_resolve_symbols(AOTModule *module);
  462. /**
  463. * Helper function to resolve a single function
  464. */
  465. bool
  466. aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func);
  467. /**
  468. * Instantiate a AOT module.
  469. *
  470. * @param module the AOT module to instantiate
  471. * @param parent the parent module instance
  472. * @param heap_size the default heap size of the module instance, a heap will
  473. * be created besides the app memory space. Both wasm app and native
  474. * function can allocate memory from the heap. If heap_size is 0, the
  475. * default heap size will be used.
  476. * @param error_buf buffer to output the error info if failed
  477. * @param error_buf_size the size of the error buffer
  478. *
  479. * @return return the instantiated AOT module instance, NULL if failed
  480. */
  481. AOTModuleInstance *
  482. aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
  483. WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size,
  484. uint32 max_memory_pages, char *error_buf,
  485. uint32 error_buf_size);
  486. /**
  487. * Deinstantiate a AOT module instance, destroy the resources.
  488. *
  489. * @param module_inst the AOT module instance to destroy
  490. * @param is_sub_inst the flag of sub instance
  491. */
  492. void
  493. aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
  494. /**
  495. * Lookup an exported function in the AOT module instance.
  496. *
  497. * @param module_inst the module instance
  498. * @param name the name of the function
  499. *
  500. * @return the function instance found
  501. */
  502. AOTFunctionInstance *
  503. aot_lookup_function(const AOTModuleInstance *module_inst, const char *name);
  504. /**
  505. * Lookup an exported function in the AOT module instance with
  506. * the function index.
  507. */
  508. AOTFunctionInstance *
  509. aot_lookup_function_with_idx(AOTModuleInstance *module_inst, uint32 func_idx);
  510. AOTMemoryInstance *
  511. aot_lookup_memory(AOTModuleInstance *module_inst, char const *name);
  512. AOTMemoryInstance *
  513. aot_get_default_memory(AOTModuleInstance *module_inst);
  514. AOTMemoryInstance *
  515. aot_get_memory_with_idx(AOTModuleInstance *module_inst, uint32 mem_idx);
  516. /**
  517. * Get a function in the AOT module instance.
  518. *
  519. * @param module_inst the module instance
  520. * @param func_idx the index of the function
  521. *
  522. * @return the function instance found
  523. */
  524. AOTFunctionInstance *
  525. aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx);
  526. /**
  527. * Call the given AOT function of a AOT module instance with
  528. * arguments.
  529. *
  530. * @param exec_env the execution environment
  531. * @param function the function to be called
  532. * @param argc the number of arguments
  533. * @param argv the arguments. If the function method has return value,
  534. * the first (or first two in case 64-bit return value) element of
  535. * argv stores the return value of the called AOT function after this
  536. * function returns.
  537. *
  538. * @return true if success, false otherwise and exception will be thrown,
  539. * the caller can call aot_get_exception to get exception info.
  540. */
  541. bool
  542. aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
  543. unsigned argc, uint32 argv[]);
  544. /**
  545. * Set AOT module instance exception with exception string
  546. *
  547. * @param module the AOT module instance
  548. *
  549. * @param exception current exception string
  550. */
  551. void
  552. aot_set_exception(AOTModuleInstance *module_inst, const char *exception);
  553. void
  554. aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id);
  555. /**
  556. * Get exception info of the AOT module instance.
  557. *
  558. * @param module_inst the AOT module instance
  559. *
  560. * @return the exception string
  561. */
  562. const char *
  563. aot_get_exception(AOTModuleInstance *module_inst);
  564. /**
  565. * @brief Copy exception in buffer passed as parameter. Thread-safe version of
  566. * `aot_get_exception()`
  567. * @note Buffer size must be no smaller than EXCEPTION_BUF_LEN
  568. * @return true if exception found, false otherwise
  569. */
  570. bool
  571. aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf);
  572. uint64
  573. aot_module_malloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  574. uint64 size, void **p_native_addr);
  575. uint64
  576. aot_module_realloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  577. uint64 ptr, uint64 size, void **p_native_addr);
  578. void
  579. aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  580. uint64 ptr);
  581. uint64
  582. aot_module_malloc(AOTModuleInstance *module_inst, uint64 size,
  583. void **p_native_addr);
  584. uint64
  585. aot_module_realloc(AOTModuleInstance *module_inst, uint64 ptr, uint64 size,
  586. void **p_native_addr);
  587. void
  588. aot_module_free(AOTModuleInstance *module_inst, uint64 ptr);
  589. uint64
  590. aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
  591. uint64 size);
  592. bool
  593. aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count);
  594. bool
  595. aot_enlarge_memory_with_idx(AOTModuleInstance *module_inst,
  596. uint32 inc_page_count, uint32 memidx);
  597. /**
  598. * Invoke native function from aot code
  599. */
  600. bool
  601. aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  602. uint32 *argv);
  603. bool
  604. aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
  605. uint32 argc, uint32 *argv);
  606. /**
  607. * Check whether the app address and the buf is inside the linear memory,
  608. * and convert the app address into native address
  609. */
  610. bool
  611. aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str,
  612. uint64 app_buf_addr, uint64 app_buf_size,
  613. void **p_native_addr);
  614. uint32
  615. aot_get_plt_table_size(void);
  616. void *
  617. aot_memmove(void *dest, const void *src, size_t n);
  618. void *
  619. aot_memset(void *s, int c, size_t n);
  620. double
  621. aot_sqrt(double x);
  622. float
  623. aot_sqrtf(float x);
  624. #if WASM_ENABLE_BULK_MEMORY != 0
  625. bool
  626. aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset,
  627. uint32 len, size_t dst);
  628. bool
  629. aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index);
  630. #endif
  631. #if WASM_ENABLE_THREAD_MGR != 0
  632. bool
  633. aot_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset, uint32 size);
  634. bool
  635. aot_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset, uint32 *size);
  636. #endif
  637. void
  638. aot_get_module_mem_consumption(const AOTModule *module,
  639. WASMModuleMemConsumption *mem_conspn);
  640. void
  641. aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
  642. WASMModuleInstMemConsumption *mem_conspn);
  643. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  644. void
  645. aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx);
  646. void
  647. aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx,
  648. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  649. uint32 dst_offset);
  650. void
  651. aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx,
  652. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  653. uint32 dst_offset);
  654. void
  655. aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length,
  656. table_elem_type_t val, uint32 data_offset);
  657. uint32
  658. aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx,
  659. uint32 inc_entries, table_elem_type_t init_val);
  660. #endif
  661. bool
  662. aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);
  663. void
  664. aot_free_frame(WASMExecEnv *exec_env);
  665. void
  666. aot_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame);
  667. bool
  668. aot_create_call_stack(struct WASMExecEnv *exec_env);
  669. #if WAMR_ENABLE_COPY_CALLSTACK != 0
  670. uint32
  671. aot_copy_callstack(WASMExecEnv *exec_env, wasm_frame_t *buffer,
  672. const uint32 length, const uint32 skip_n, char *error_buf,
  673. uint32_t error_buf_size);
  674. #endif // WAMR_ENABLE_COPY_CALLSTACK
  675. /**
  676. * @brief Dump wasm call stack or get the size
  677. *
  678. * @param exec_env the execution environment
  679. * @param print whether to print to stdout or not
  680. * @param buf buffer to store the dumped content
  681. * @param len length of the buffer
  682. *
  683. * @return when print is true, return the bytes printed out to stdout; when
  684. * print is false and buf is NULL, return the size required to store the
  685. * callstack content; when print is false and buf is not NULL, return the size
  686. * dumped to the buffer, 0 means error and data in buf may be invalid
  687. */
  688. uint32
  689. aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len);
  690. void
  691. aot_dump_perf_profiling(const AOTModuleInstance *module_inst);
  692. double
  693. aot_summarize_wasm_execute_time(const AOTModuleInstance *inst);
  694. double
  695. aot_get_wasm_func_exec_time(const AOTModuleInstance *inst,
  696. const char *func_name);
  697. const uint8 *
  698. aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len);
  699. const void *
  700. aot_get_data_section_addr(AOTModule *module, const char *section_name,
  701. uint32 *p_data_size);
  702. #if WASM_ENABLE_STATIC_PGO != 0
  703. void
  704. llvm_profile_instrument_target(uint64 target_value, void *data,
  705. uint32 counter_idx);
  706. void
  707. llvm_profile_instrument_memop(uint64 target_value, void *data,
  708. uint32 counter_idx);
  709. uint32
  710. aot_get_pgo_prof_data_size(AOTModuleInstance *module_inst);
  711. uint32
  712. aot_dump_pgo_prof_data_to_buf(AOTModuleInstance *module_inst, char *buf,
  713. uint32 len);
  714. void
  715. aot_exchange_uint16(uint8 *p_data);
  716. void
  717. aot_exchange_uint32(uint8 *p_data);
  718. void
  719. aot_exchange_uint64(uint8 *p_data);
  720. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  721. #if WASM_ENABLE_GC != 0
  722. void *
  723. aot_create_func_obj(AOTModuleInstance *module_inst, uint32 func_idx,
  724. bool throw_exce, char *error_buf, uint32 error_buf_size);
  725. bool
  726. aot_obj_is_instance_of(AOTModuleInstance *module_inst, WASMObjectRef gc_obj,
  727. uint32 type_index);
  728. /* Whether func type1 is one of super types of func type2 */
  729. bool
  730. aot_func_type_is_super_of(AOTModuleInstance *module_inst, uint32 type_idx1,
  731. uint32 type_idx2);
  732. WASMRttTypeRef
  733. aot_rtt_type_new(AOTModuleInstance *module_inst, uint32 type_index);
  734. bool
  735. aot_array_init_with_data(AOTModuleInstance *module_inst, uint32 seg_index,
  736. uint32 data_seg_offset, WASMArrayObjectRef array_obj,
  737. uint32 elem_size, uint32 array_len);
  738. bool
  739. aot_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap);
  740. #endif /* end of WASM_ENABLE_GC != 0 */
  741. char *
  742. aot_const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
  743. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  744. bool is_vram_word_align,
  745. #endif
  746. char *error_buf, uint32 error_buf_size);
  747. bool
  748. aot_set_module_name(AOTModule *module, const char *name, char *error_buf,
  749. uint32_t error_buf_size);
  750. const char *
  751. aot_get_module_name(AOTModule *module);
  752. #ifdef __cplusplus
  753. } /* end of extern "C" */
  754. #endif
  755. #endif /* end of _AOT_RUNTIME_H_ */