aot_runtime.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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_WASI_NN != 0
  12. #include "../libraries/wasi-nn/src/wasi_nn_private.h"
  13. #endif
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef enum AOTSectionType {
  18. AOT_SECTION_TYPE_TARGET_INFO = 0,
  19. AOT_SECTION_TYPE_INIT_DATA = 1,
  20. AOT_SECTION_TYPE_TEXT = 2,
  21. AOT_SECTION_TYPE_FUNCTION = 3,
  22. AOT_SECTION_TYPE_EXPORT = 4,
  23. AOT_SECTION_TYPE_RELOCATION = 5,
  24. AOT_SECTION_TYPE_SIGANATURE = 6,
  25. AOT_SECTION_TYPE_CUSTOM = 100,
  26. } AOTSectionType;
  27. typedef enum AOTCustomSectionType {
  28. AOT_CUSTOM_SECTION_RAW = 0,
  29. AOT_CUSTOM_SECTION_NATIVE_SYMBOL = 1,
  30. AOT_CUSTOM_SECTION_ACCESS_CONTROL = 2,
  31. AOT_CUSTOM_SECTION_NAME = 3,
  32. } AOTCustomSectionType;
  33. typedef struct AOTObjectDataSection {
  34. char *name;
  35. uint8 *data;
  36. uint32 size;
  37. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  38. bool is_name_allocated;
  39. bool is_data_allocated;
  40. #endif
  41. } AOTObjectDataSection;
  42. /* Relocation info */
  43. typedef struct AOTRelocation {
  44. uint64 relocation_offset;
  45. int64 relocation_addend;
  46. uint32 relocation_type;
  47. char *symbol_name;
  48. /* index in the symbol offset field */
  49. uint32 symbol_index;
  50. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  51. bool is_symbol_name_allocated;
  52. #endif
  53. } AOTRelocation;
  54. /* Relocation Group */
  55. typedef struct AOTRelocationGroup {
  56. char *section_name;
  57. /* index in the symbol offset field */
  58. uint32 name_index;
  59. uint32 relocation_count;
  60. AOTRelocation *relocations;
  61. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  62. bool is_section_name_allocated;
  63. #endif
  64. } AOTRelocationGroup;
  65. /* AOT function instance */
  66. typedef struct AOTFunctionInstance {
  67. char *func_name;
  68. uint32 func_index;
  69. bool is_import_func;
  70. union {
  71. struct {
  72. AOTFuncType *func_type;
  73. /* function pointer linked */
  74. void *func_ptr;
  75. } func;
  76. AOTImportFunc *func_import;
  77. } u;
  78. } AOTFunctionInstance;
  79. typedef struct AOTModuleInstanceExtra {
  80. DefPointer(const uint32 *, stack_sizes);
  81. WASMModuleInstanceExtraCommon common;
  82. #if WASM_ENABLE_MULTI_MODULE != 0
  83. bh_list sub_module_inst_list_head;
  84. bh_list *sub_module_inst_list;
  85. #endif
  86. } AOTModuleInstanceExtra;
  87. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  88. typedef struct GOTItem {
  89. uint32 func_idx;
  90. struct GOTItem *next;
  91. } GOTItem, *GOTItemList;
  92. #endif
  93. typedef struct AOTModule {
  94. uint32 module_type;
  95. /* import memories */
  96. uint32 import_memory_count;
  97. AOTImportMemory *import_memories;
  98. /* memory info */
  99. uint32 memory_count;
  100. AOTMemory *memories;
  101. /* init data */
  102. uint32 mem_init_data_count;
  103. AOTMemInitData **mem_init_data_list;
  104. /* native symbol */
  105. void **native_symbol_list;
  106. /* import tables */
  107. uint32 import_table_count;
  108. AOTImportTable *import_tables;
  109. /* tables */
  110. uint32 table_count;
  111. AOTTable *tables;
  112. /* table init data info */
  113. uint32 table_init_data_count;
  114. AOTTableInitData **table_init_data_list;
  115. /* function type info */
  116. uint32 func_type_count;
  117. AOTFuncType **func_types;
  118. /* import global variable info */
  119. uint32 import_global_count;
  120. AOTImportGlobal *import_globals;
  121. /* global variable info */
  122. uint32 global_count;
  123. AOTGlobal *globals;
  124. /* total global variable size */
  125. uint32 global_data_size;
  126. /* import function info */
  127. uint32 import_func_count;
  128. AOTImportFunc *import_funcs;
  129. /* function info */
  130. uint32 func_count;
  131. /* func pointers of AOTed (un-imported) functions */
  132. void **func_ptrs;
  133. /* func type indexes of AOTed (un-imported) functions */
  134. uint32 *func_type_indexes;
  135. /* export info */
  136. uint32 export_count;
  137. AOTExport *exports;
  138. /* start function index, -1 denotes no start function */
  139. uint32 start_func_index;
  140. /* start function, point to AOTed function */
  141. void *start_function;
  142. uint32 malloc_func_index;
  143. uint32 free_func_index;
  144. uint32 retain_func_index;
  145. /* AOTed code */
  146. void *code;
  147. uint32 code_size;
  148. /* literal for AOTed code */
  149. uint8 *literal;
  150. uint32 literal_size;
  151. #if defined(BH_PLATFORM_WINDOWS)
  152. /* extra plt data area for __ymm, __xmm and __real constants
  153. in Windows platform */
  154. uint8 *extra_plt_data;
  155. uint32 extra_plt_data_size;
  156. uint32 ymm_plt_count;
  157. uint32 xmm_plt_count;
  158. uint32 real_plt_count;
  159. uint32 float_plt_count;
  160. #endif
  161. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  162. uint32 got_item_count;
  163. GOTItemList got_item_list;
  164. GOTItemList got_item_list_end;
  165. void **got_func_ptrs;
  166. #endif
  167. /* data sections in AOT object file, including .data, .rodata
  168. and .rodata.cstN. */
  169. AOTObjectDataSection *data_sections;
  170. uint32 data_section_count;
  171. /* constant string set */
  172. HashMap *const_str_set;
  173. /* the index of auxiliary __data_end global,
  174. -1 means unexported */
  175. uint32 aux_data_end_global_index;
  176. /* auxiliary __data_end exported by wasm app */
  177. uint32 aux_data_end;
  178. /* the index of auxiliary __heap_base global,
  179. -1 means unexported */
  180. uint32 aux_heap_base_global_index;
  181. /* auxiliary __heap_base exported by wasm app */
  182. uint32 aux_heap_base;
  183. /* the index of auxiliary stack top global,
  184. -1 means unexported */
  185. uint32 aux_stack_top_global_index;
  186. /* auxiliary stack bottom resolved */
  187. uint32 aux_stack_bottom;
  188. /* auxiliary stack size resolved */
  189. uint32 aux_stack_size;
  190. /* is indirect mode or not */
  191. bool is_indirect_mode;
  192. #if WASM_ENABLE_LIBC_WASI != 0
  193. WASIArguments wasi_args;
  194. bool import_wasi_api;
  195. #endif
  196. #if WASM_ENABLE_MULTI_MODULE != 0
  197. /* TODO: add mutex for mutli-thread? */
  198. bh_list import_module_list_head;
  199. bh_list *import_module_list;
  200. #endif
  201. #if WASM_ENABLE_DEBUG_AOT != 0
  202. void *elf_hdr;
  203. uint32 elf_size;
  204. #endif
  205. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  206. const char **aux_func_names;
  207. uint32 *aux_func_indexes;
  208. uint32 aux_func_name_count;
  209. #endif
  210. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  211. WASMCustomSection *custom_section_list;
  212. #endif
  213. } AOTModule;
  214. #define AOTMemoryInstance WASMMemoryInstance
  215. #define AOTTableInstance WASMTableInstance
  216. #define AOTModuleInstance WASMModuleInstance
  217. #if WASM_ENABLE_MULTI_MODULE != 0
  218. #define AOTSubModInstNode WASMSubModInstNode
  219. #endif
  220. /* Target info, read from ELF header of object file */
  221. typedef struct AOTTargetInfo {
  222. /* Binary type, elf32l/elf32b/elf64l/elf64b */
  223. uint16 bin_type;
  224. /* ABI type */
  225. uint16 abi_type;
  226. /* Object file type */
  227. uint16 e_type;
  228. /* Architecture */
  229. uint16 e_machine;
  230. /* Object file version */
  231. uint32 e_version;
  232. /* Processor-specific flags */
  233. uint32 e_flags;
  234. /* Reserved */
  235. uint32 reserved;
  236. /* Arch name */
  237. char arch[16];
  238. } AOTTargetInfo;
  239. typedef struct AOTFuncPerfProfInfo {
  240. /* total execution time */
  241. uint64 total_exec_time;
  242. /* total execution count */
  243. uint32 total_exec_cnt;
  244. /* children execution time */
  245. uint64 children_exec_time;
  246. } AOTFuncPerfProfInfo;
  247. /* AOT auxiliary call stack */
  248. typedef struct AOTFrame {
  249. struct AOTFrame *prev_frame;
  250. uint32 func_index;
  251. #if WASM_ENABLE_PERF_PROFILING != 0
  252. uint64 time_started;
  253. AOTFuncPerfProfInfo *func_perf_prof_info;
  254. #endif
  255. } AOTFrame;
  256. #if WASM_ENABLE_STATIC_PGO != 0
  257. typedef struct LLVMProfileRawHeader {
  258. uint64 magic;
  259. uint64 version;
  260. uint64 binary_ids_size;
  261. uint64 num_prof_data;
  262. uint64 padding_bytes_before_counters;
  263. uint64 num_prof_counters;
  264. uint64 padding_bytes_after_counters;
  265. uint64 names_size;
  266. uint64 counters_delta;
  267. uint64 names_delta;
  268. uint64 value_kind_last;
  269. } LLVMProfileRawHeader;
  270. typedef struct ValueProfNode {
  271. uint64 value;
  272. uint64 count;
  273. struct ValueProfNode *next;
  274. } ValueProfNode;
  275. /* The profiling data of data sections created by aot compiler and
  276. used when profiling, the width of pointer can be 8 bytes (64-bit)
  277. or 4 bytes (32-bit) */
  278. typedef struct LLVMProfileData {
  279. uint64 func_md5;
  280. uint64 func_hash;
  281. uint64 offset_counters;
  282. uintptr_t func_ptr;
  283. ValueProfNode **values;
  284. uint32 num_counters;
  285. uint16 num_value_sites[2];
  286. } LLVMProfileData;
  287. /* The profiling data for writting to the output file, the width of
  288. pointer is 8 bytes suppose we always use wamrc and llvm-profdata
  289. with 64-bit mode */
  290. typedef struct LLVMProfileData_64 {
  291. uint64 func_md5;
  292. uint64 func_hash;
  293. uint64 offset_counters;
  294. uint64 func_ptr;
  295. uint64 values;
  296. uint32 num_counters;
  297. uint16 num_value_sites[2];
  298. } LLVMProfileData_64;
  299. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  300. /**
  301. * Load a AOT module from aot file buffer
  302. * @param buf the byte buffer which contains the AOT file data
  303. * @param size the size of the buffer
  304. * @param error_buf output of the error info
  305. * @param error_buf_size the size of the error string
  306. *
  307. * @return return AOT module loaded, NULL if failed
  308. */
  309. AOTModule *
  310. aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf,
  311. uint32 error_buf_size);
  312. /**
  313. * Load a AOT module from a specified AOT section list.
  314. *
  315. * @param section_list the section list which contains each section data
  316. * @param error_buf output of the error info
  317. * @param error_buf_size the size of the error string
  318. *
  319. * @return return AOT module loaded, NULL if failed
  320. */
  321. AOTModule *
  322. aot_load_from_sections(AOTSection *section_list, char *error_buf,
  323. uint32 error_buf_size);
  324. /**
  325. * Unload a AOT module.
  326. *
  327. * @param module the module to be unloaded
  328. */
  329. void
  330. aot_unload(AOTModule *module);
  331. /**
  332. * Instantiate a AOT module.
  333. *
  334. * @param module the AOT module to instantiate
  335. * @param parent the parent module instance
  336. * @param heap_size the default heap size of the module instance, a heap will
  337. * be created besides the app memory space. Both wasm app and native
  338. * function can allocate memory from the heap. If heap_size is 0, the
  339. * default heap size will be used.
  340. * @param error_buf buffer to output the error info if failed
  341. * @param error_buf_size the size of the error buffer
  342. *
  343. * @return return the instantiated AOT module instance, NULL if failed
  344. */
  345. AOTModuleInstance *
  346. aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
  347. WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size,
  348. char *error_buf, uint32 error_buf_size);
  349. /**
  350. * Deinstantiate a AOT module instance, destroy the resources.
  351. *
  352. * @param module_inst the AOT module instance to destroy
  353. * @param is_sub_inst the flag of sub instance
  354. */
  355. void
  356. aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
  357. /**
  358. * Lookup an exported function in the AOT module instance.
  359. *
  360. * @param module_inst the module instance
  361. * @param name the name of the function
  362. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  363. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  364. *
  365. * @return the function instance found
  366. */
  367. AOTFunctionInstance *
  368. aot_lookup_function(const AOTModuleInstance *module_inst, const char *name,
  369. const char *signature);
  370. /**
  371. * Call the given AOT function of a AOT module instance with
  372. * arguments.
  373. *
  374. * @param exec_env the execution environment
  375. * @param function the function to be called
  376. * @param argc the number of arguments
  377. * @param argv the arguments. If the function method has return value,
  378. * the first (or first two in case 64-bit return value) element of
  379. * argv stores the return value of the called AOT function after this
  380. * function returns.
  381. *
  382. * @return true if success, false otherwise and exception will be thrown,
  383. * the caller can call aot_get_exception to get exception info.
  384. */
  385. bool
  386. aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
  387. unsigned argc, uint32 argv[]);
  388. /**
  389. * Set AOT module instance exception with exception string
  390. *
  391. * @param module the AOT module instance
  392. *
  393. * @param exception current exception string
  394. */
  395. void
  396. aot_set_exception(AOTModuleInstance *module_inst, const char *exception);
  397. void
  398. aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id);
  399. /**
  400. * Get exception info of the AOT module instance.
  401. *
  402. * @param module_inst the AOT module instance
  403. *
  404. * @return the exception string
  405. */
  406. const char *
  407. aot_get_exception(AOTModuleInstance *module_inst);
  408. /**
  409. * @brief Copy exception in buffer passed as parameter. Thread-safe version of
  410. * `aot_get_exception()`
  411. * @note Buffer size must be no smaller than EXCEPTION_BUF_LEN
  412. * @return true if exception found, false otherwise
  413. */
  414. bool
  415. aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf);
  416. uint32
  417. aot_module_malloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  418. uint32 size, void **p_native_addr);
  419. uint32
  420. aot_module_realloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  421. uint32 ptr, uint32 size, void **p_native_addr);
  422. void
  423. aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  424. uint32 ptr);
  425. uint32
  426. aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
  427. void **p_native_addr);
  428. uint32
  429. aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
  430. void **p_native_addr);
  431. void
  432. aot_module_free(AOTModuleInstance *module_inst, uint32 ptr);
  433. uint32
  434. aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
  435. uint32 size);
  436. bool
  437. aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count);
  438. /**
  439. * Invoke native function from aot code
  440. */
  441. bool
  442. aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  443. uint32 *argv);
  444. bool
  445. aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
  446. uint32 argc, uint32 *argv);
  447. /**
  448. * Check whether the app address and the buf is inside the linear memory,
  449. * and convert the app address into native address
  450. */
  451. bool
  452. aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str,
  453. uint32 app_buf_addr, uint32 app_buf_size,
  454. void **p_native_addr);
  455. uint32
  456. aot_get_plt_table_size();
  457. void *
  458. aot_memmove(void *dest, const void *src, size_t n);
  459. void *
  460. aot_memset(void *s, int c, size_t n);
  461. double
  462. aot_sqrt(double x);
  463. float
  464. aot_sqrtf(float x);
  465. #if WASM_ENABLE_BULK_MEMORY != 0
  466. bool
  467. aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset,
  468. uint32 len, uint32 dst);
  469. bool
  470. aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index);
  471. #endif
  472. #if WASM_ENABLE_THREAD_MGR != 0
  473. bool
  474. aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size);
  475. bool
  476. aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
  477. #endif
  478. void
  479. aot_get_module_mem_consumption(const AOTModule *module,
  480. WASMModuleMemConsumption *mem_conspn);
  481. void
  482. aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
  483. WASMModuleInstMemConsumption *mem_conspn);
  484. #if WASM_ENABLE_REF_TYPES != 0
  485. void
  486. aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx);
  487. void
  488. aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx,
  489. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  490. uint32 dst_offset);
  491. void
  492. aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx,
  493. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  494. uint32 dst_offset);
  495. void
  496. aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length,
  497. uint32 val, uint32 data_offset);
  498. uint32
  499. aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx,
  500. uint32 inc_entries, uint32 init_val);
  501. #endif
  502. bool
  503. aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);
  504. void
  505. aot_free_frame(WASMExecEnv *exec_env);
  506. bool
  507. aot_create_call_stack(struct WASMExecEnv *exec_env);
  508. /**
  509. * @brief Dump wasm call stack or get the size
  510. *
  511. * @param exec_env the execution environment
  512. * @param print whether to print to stdout or not
  513. * @param buf buffer to store the dumped content
  514. * @param len length of the buffer
  515. *
  516. * @return when print is true, return the bytes printed out to stdout; when
  517. * print is false and buf is NULL, return the size required to store the
  518. * callstack content; when print is false and buf is not NULL, return the size
  519. * dumped to the buffer, 0 means error and data in buf may be invalid
  520. */
  521. uint32
  522. aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len);
  523. void
  524. aot_dump_perf_profiling(const AOTModuleInstance *module_inst);
  525. double
  526. aot_summarize_wasm_execute_time(const AOTModuleInstance *inst);
  527. double
  528. aot_get_wasm_func_exec_time(const AOTModuleInstance *inst,
  529. const char *func_name);
  530. const uint8 *
  531. aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len);
  532. const void *
  533. aot_get_data_section_addr(AOTModule *module, const char *section_name,
  534. uint32 *p_data_size);
  535. #if WASM_ENABLE_STATIC_PGO != 0
  536. void
  537. llvm_profile_instrument_target(uint64 target_value, void *data,
  538. uint32 counter_idx);
  539. void
  540. llvm_profile_instrument_memop(uint64 target_value, void *data,
  541. uint32 counter_idx);
  542. uint32
  543. aot_get_pgo_prof_data_size(AOTModuleInstance *module_inst);
  544. uint32
  545. aot_dump_pgo_prof_data_to_buf(AOTModuleInstance *module_inst, char *buf,
  546. uint32 len);
  547. void
  548. aot_exchange_uint16(uint8 *p_data);
  549. void
  550. aot_exchange_uint32(uint8 *p_data);
  551. void
  552. aot_exchange_uint64(uint8 *p_data);
  553. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  554. #ifdef __cplusplus
  555. } /* end of extern "C" */
  556. #endif
  557. #endif /* end of _AOT_RUNTIME_H_ */