aot_runtime.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. } AOTObjectDataSection;
  38. /* Relocation info */
  39. typedef struct AOTRelocation {
  40. uint64 relocation_offset;
  41. int64 relocation_addend;
  42. uint32 relocation_type;
  43. char *symbol_name;
  44. /* index in the symbol offset field */
  45. uint32 symbol_index;
  46. } AOTRelocation;
  47. /* Relocation Group */
  48. typedef struct AOTRelocationGroup {
  49. char *section_name;
  50. /* index in the symbol offset field */
  51. uint32 name_index;
  52. uint32 relocation_count;
  53. AOTRelocation *relocations;
  54. } AOTRelocationGroup;
  55. /* AOT function instance */
  56. typedef struct AOTFunctionInstance {
  57. char *func_name;
  58. uint32 func_index;
  59. bool is_import_func;
  60. union {
  61. struct {
  62. AOTFuncType *func_type;
  63. /* function pointer linked */
  64. void *func_ptr;
  65. } func;
  66. AOTImportFunc *func_import;
  67. } u;
  68. } AOTFunctionInstance;
  69. typedef struct AOTModuleInstanceExtra {
  70. CApiFuncImport *c_api_func_imports;
  71. #if WASM_ENABLE_WASI_NN != 0
  72. WASINNContext *wasi_nn_ctx;
  73. #endif
  74. } AOTModuleInstanceExtra;
  75. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  76. /* clang-format off */
  77. typedef struct AOTUnwindInfo {
  78. uint8 Version : 3;
  79. uint8 Flags : 5;
  80. uint8 SizeOfProlog;
  81. uint8 CountOfCodes;
  82. uint8 FrameRegister : 4;
  83. uint8 FrameOffset : 4;
  84. struct {
  85. struct {
  86. uint8 CodeOffset;
  87. uint8 UnwindOp : 4;
  88. uint8 OpInfo : 4;
  89. };
  90. uint16 FrameOffset;
  91. } UnwindCode[1];
  92. } AOTUnwindInfo;
  93. /* clang-format on */
  94. /* size of mov instruction and jmp instruction */
  95. #define PLT_ITEM_SIZE 12
  96. #endif
  97. typedef struct AOTModule {
  98. uint32 module_type;
  99. /* import memories */
  100. uint32 import_memory_count;
  101. AOTImportMemory *import_memories;
  102. /* memory info */
  103. uint32 memory_count;
  104. AOTMemory *memories;
  105. /* init data */
  106. uint32 mem_init_data_count;
  107. AOTMemInitData **mem_init_data_list;
  108. /* native symbol */
  109. void **native_symbol_list;
  110. /* import tables */
  111. uint32 import_table_count;
  112. AOTImportTable *import_tables;
  113. /* tables */
  114. uint32 table_count;
  115. AOTTable *tables;
  116. /* table init data info */
  117. uint32 table_init_data_count;
  118. AOTTableInitData **table_init_data_list;
  119. /* function type info */
  120. uint32 func_type_count;
  121. AOTFuncType **func_types;
  122. /* import global variable info */
  123. uint32 import_global_count;
  124. AOTImportGlobal *import_globals;
  125. /* global variable info */
  126. uint32 global_count;
  127. AOTGlobal *globals;
  128. /* total global variable size */
  129. uint32 global_data_size;
  130. /* import function info */
  131. uint32 import_func_count;
  132. AOTImportFunc *import_funcs;
  133. /* function info */
  134. uint32 func_count;
  135. /* func pointers of AOTed (un-imported) functions */
  136. void **func_ptrs;
  137. /* func type indexes of AOTed (un-imported) functions */
  138. uint32 *func_type_indexes;
  139. /* export info */
  140. uint32 export_count;
  141. AOTExport *exports;
  142. /* start function index, -1 denotes no start function */
  143. uint32 start_func_index;
  144. /* start function, point to AOTed function */
  145. void *start_function;
  146. uint32 malloc_func_index;
  147. uint32 free_func_index;
  148. uint32 retain_func_index;
  149. /* AOTed code */
  150. void *code;
  151. uint32 code_size;
  152. /* literal for AOTed code */
  153. uint8 *literal;
  154. uint32 literal_size;
  155. #if defined(BH_PLATFORM_WINDOWS)
  156. /* extra plt data area for __ymm, __xmm and __real constants
  157. in Windows platform */
  158. uint8 *extra_plt_data;
  159. uint32 extra_plt_data_size;
  160. uint32 ymm_plt_count;
  161. uint32 xmm_plt_count;
  162. uint32 real_plt_count;
  163. uint32 float_plt_count;
  164. #endif
  165. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  166. /* dynamic function table to be added by RtlAddFunctionTable(),
  167. used to unwind the call stack and register exception handler
  168. for AOT functions */
  169. RUNTIME_FUNCTION *rtl_func_table;
  170. bool rtl_func_table_registered;
  171. #endif
  172. /* data sections in AOT object file, including .data, .rodata
  173. and .rodata.cstN. */
  174. AOTObjectDataSection *data_sections;
  175. uint32 data_section_count;
  176. /* constant string set */
  177. HashMap *const_str_set;
  178. /* the index of auxiliary __data_end global,
  179. -1 means unexported */
  180. uint32 aux_data_end_global_index;
  181. /* auxiliary __data_end exported by wasm app */
  182. uint32 aux_data_end;
  183. /* the index of auxiliary __heap_base global,
  184. -1 means unexported */
  185. uint32 aux_heap_base_global_index;
  186. /* auxiliary __heap_base exported by wasm app */
  187. uint32 aux_heap_base;
  188. /* the index of auxiliary stack top global,
  189. -1 means unexported */
  190. uint32 aux_stack_top_global_index;
  191. /* auxiliary stack bottom resolved */
  192. uint32 aux_stack_bottom;
  193. /* auxiliary stack size resolved */
  194. uint32 aux_stack_size;
  195. /* is indirect mode or not */
  196. bool is_indirect_mode;
  197. #if WASM_ENABLE_LIBC_WASI != 0
  198. WASIArguments wasi_args;
  199. bool import_wasi_api;
  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. /* Target info, read from ELF header of object file */
  218. typedef struct AOTTargetInfo {
  219. /* Binary type, elf32l/elf32b/elf64l/elf64b */
  220. uint16 bin_type;
  221. /* ABI type */
  222. uint16 abi_type;
  223. /* Object file type */
  224. uint16 e_type;
  225. /* Architecture */
  226. uint16 e_machine;
  227. /* Object file version */
  228. uint32 e_version;
  229. /* Processor-specific flags */
  230. uint32 e_flags;
  231. /* Reserved */
  232. uint32 reserved;
  233. /* Arch name */
  234. char arch[16];
  235. } AOTTargetInfo;
  236. typedef struct AOTFuncPerfProfInfo {
  237. /* total execution time */
  238. uint64 total_exec_time;
  239. /* total execution count */
  240. uint32 total_exec_cnt;
  241. } AOTFuncPerfProfInfo;
  242. /* AOT auxiliary call stack */
  243. typedef struct AOTFrame {
  244. struct AOTFrame *prev_frame;
  245. uint32 func_index;
  246. #if WASM_ENABLE_PERF_PROFILING != 0
  247. uint64 time_started;
  248. AOTFuncPerfProfInfo *func_perf_prof_info;
  249. #endif
  250. } AOTFrame;
  251. /**
  252. * Load a AOT module from aot file buffer
  253. * @param buf the byte buffer which contains the AOT file data
  254. * @param size the size of the buffer
  255. * @param error_buf output of the error info
  256. * @param error_buf_size the size of the error string
  257. *
  258. * @return return AOT module loaded, NULL if failed
  259. */
  260. AOTModule *
  261. aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf,
  262. uint32 error_buf_size);
  263. /**
  264. * Load a AOT module from a specified AOT section list.
  265. *
  266. * @param section_list the section list which contains each section data
  267. * @param error_buf output of the error info
  268. * @param error_buf_size the size of the error string
  269. *
  270. * @return return AOT module loaded, NULL if failed
  271. */
  272. AOTModule *
  273. aot_load_from_sections(AOTSection *section_list, char *error_buf,
  274. uint32 error_buf_size);
  275. /**
  276. * Unload a AOT module.
  277. *
  278. * @param module the module to be unloaded
  279. */
  280. void
  281. aot_unload(AOTModule *module);
  282. /**
  283. * Instantiate a AOT module.
  284. *
  285. * @param module the AOT module to instantiate
  286. * @param is_sub_inst the flag of sub instance
  287. * @param heap_size the default heap size of the module instance, a heap will
  288. * be created besides the app memory space. Both wasm app and native
  289. * function can allocate memory from the heap. If heap_size is 0, the
  290. * default heap size will be used.
  291. * @param error_buf buffer to output the error info if failed
  292. * @param error_buf_size the size of the error buffer
  293. *
  294. * @return return the instantiated AOT module instance, NULL if failed
  295. */
  296. AOTModuleInstance *
  297. aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
  298. uint32 stack_size, uint32 heap_size, char *error_buf,
  299. uint32 error_buf_size);
  300. /**
  301. * Deinstantiate a AOT module instance, destroy the resources.
  302. *
  303. * @param module_inst the AOT module instance to destroy
  304. * @param is_sub_inst the flag of sub instance
  305. */
  306. void
  307. aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
  308. /**
  309. * Lookup an exported function in the AOT module instance.
  310. *
  311. * @param module_inst the module instance
  312. * @param name the name of the function
  313. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  314. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  315. *
  316. * @return the function instance found
  317. */
  318. AOTFunctionInstance *
  319. aot_lookup_function(const AOTModuleInstance *module_inst, const char *name,
  320. const char *signature);
  321. /**
  322. * Call the given AOT function of a AOT module instance with
  323. * arguments.
  324. *
  325. * @param exec_env the execution environment
  326. * @param function the function to be called
  327. * @param argc the number of arguments
  328. * @param argv the arguments. If the function method has return value,
  329. * the first (or first two in case 64-bit return value) element of
  330. * argv stores the return value of the called AOT function after this
  331. * function returns.
  332. *
  333. * @return true if success, false otherwise and exception will be thrown,
  334. * the caller can call aot_get_exception to get exception info.
  335. */
  336. bool
  337. aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
  338. unsigned argc, uint32 argv[]);
  339. /**
  340. * Set AOT module instance exception with exception string
  341. *
  342. * @param module the AOT module instance
  343. *
  344. * @param exception current exception string
  345. */
  346. void
  347. aot_set_exception(AOTModuleInstance *module_inst, const char *exception);
  348. void
  349. aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id);
  350. /**
  351. * Get exception info of the AOT module instance.
  352. *
  353. * @param module_inst the AOT module instance
  354. *
  355. * @return the exception string
  356. */
  357. const char *
  358. aot_get_exception(AOTModuleInstance *module_inst);
  359. /**
  360. * @brief Copy exception in buffer passed as parameter. Thread-safe version of
  361. * `aot_get_exception()`
  362. * @note Buffer size must be no smaller than EXCEPTION_BUF_LEN
  363. * @return true if exception found, false otherwise
  364. */
  365. bool
  366. aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf);
  367. uint32
  368. aot_module_malloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  369. uint32 size, void **p_native_addr);
  370. uint32
  371. aot_module_realloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  372. uint32 ptr, uint32 size, void **p_native_addr);
  373. void
  374. aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
  375. uint32 ptr);
  376. uint32
  377. aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
  378. void **p_native_addr);
  379. uint32
  380. aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
  381. void **p_native_addr);
  382. void
  383. aot_module_free(AOTModuleInstance *module_inst, uint32 ptr);
  384. uint32
  385. aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
  386. uint32 size);
  387. bool
  388. aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count);
  389. /**
  390. * Invoke native function from aot code
  391. */
  392. bool
  393. aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  394. uint32 *argv);
  395. bool
  396. aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
  397. uint32 argc, uint32 *argv);
  398. /**
  399. * Check whether the app address and the buf is inside the linear memory,
  400. * and convert the app address into native address
  401. */
  402. bool
  403. aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str,
  404. uint32 app_buf_addr, uint32 app_buf_size,
  405. void **p_native_addr);
  406. uint32
  407. aot_get_plt_table_size();
  408. void *
  409. aot_memmove(void *dest, const void *src, size_t n);
  410. void *
  411. aot_memset(void *s, int c, size_t n);
  412. double
  413. aot_sqrt(double x);
  414. float
  415. aot_sqrtf(float x);
  416. #if WASM_ENABLE_BULK_MEMORY != 0
  417. bool
  418. aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset,
  419. uint32 len, uint32 dst);
  420. bool
  421. aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index);
  422. #endif
  423. #if WASM_ENABLE_THREAD_MGR != 0
  424. bool
  425. aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size);
  426. bool
  427. aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
  428. #endif
  429. void
  430. aot_get_module_mem_consumption(const AOTModule *module,
  431. WASMModuleMemConsumption *mem_conspn);
  432. void
  433. aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
  434. WASMModuleInstMemConsumption *mem_conspn);
  435. #if WASM_ENABLE_REF_TYPES != 0
  436. void
  437. aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx);
  438. void
  439. aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx,
  440. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  441. uint32 dst_offset);
  442. void
  443. aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx,
  444. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  445. uint32 dst_offset);
  446. void
  447. aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length,
  448. uint32 val, uint32 data_offset);
  449. uint32
  450. aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx,
  451. uint32 inc_entries, uint32 init_val);
  452. #endif
  453. bool
  454. aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);
  455. void
  456. aot_free_frame(WASMExecEnv *exec_env);
  457. bool
  458. aot_create_call_stack(struct WASMExecEnv *exec_env);
  459. /**
  460. * @brief Dump wasm call stack or get the size
  461. *
  462. * @param exec_env the execution environment
  463. * @param print whether to print to stdout or not
  464. * @param buf buffer to store the dumped content
  465. * @param len length of the buffer
  466. *
  467. * @return when print is true, return the bytes printed out to stdout; when
  468. * print is false and buf is NULL, return the size required to store the
  469. * callstack content; when print is false and buf is not NULL, return the size
  470. * dumped to the buffer, 0 means error and data in buf may be invalid
  471. */
  472. uint32
  473. aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len);
  474. void
  475. aot_dump_perf_profiling(const AOTModuleInstance *module_inst);
  476. const uint8 *
  477. aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len);
  478. #ifdef __cplusplus
  479. } /* end of extern "C" */
  480. #endif
  481. #endif /* end of _AOT_RUNTIME_H_ */