aot_runtime.h 29 KB

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