aot_runtime.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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_JIT != 0
  12. #include "../compilation/aot_llvm.h"
  13. #endif
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef enum AOTExceptionID {
  18. EXCE_UNREACHABLE = 0,
  19. EXCE_OUT_OF_MEMORY,
  20. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS,
  21. EXCE_INTEGER_OVERFLOW,
  22. EXCE_INTEGER_DIVIDE_BY_ZERO,
  23. EXCE_INVALID_CONVERSION_TO_INTEGER,
  24. EXCE_INVALID_FUNCTION_TYPE_INDEX,
  25. EXCE_INVALID_FUNCTION_INDEX,
  26. EXCE_UNDEFINED_ELEMENT,
  27. EXCE_UNINITIALIZED_ELEMENT,
  28. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  29. EXCE_NATIVE_STACK_OVERFLOW,
  30. EXCE_UNALIGNED_ATOMIC,
  31. EXCE_NUM,
  32. } AOTExceptionID;
  33. typedef enum AOTSectionType {
  34. AOT_SECTION_TYPE_TARGET_INFO = 0,
  35. AOT_SECTION_TYPE_INIT_DATA,
  36. AOT_SECTION_TYPE_TEXT,
  37. AOT_SECTION_TYPE_FUNCTION,
  38. AOT_SECTION_TYPE_EXPORT,
  39. AOT_SECTION_TYPE_RELOCATION,
  40. AOT_SECTION_TYPE_SIGANATURE
  41. } AOTSectionType;
  42. typedef struct AOTObjectDataSection {
  43. char *name;
  44. uint8 *data;
  45. uint32 size;
  46. } AOTObjectDataSection;
  47. /* Relocation info */
  48. typedef struct AOTRelocation {
  49. uint64 relocation_offset;
  50. uint64 relocation_addend;
  51. uint32 relocation_type;
  52. char *symbol_name;
  53. /* index in the symbol offset field */
  54. uint32 symbol_index;
  55. } AOTRelocation;
  56. /* Relocation Group */
  57. typedef struct AOTRelocationGroup {
  58. char *section_name;
  59. /* index in the symbol offset field */
  60. uint32 name_index;
  61. uint32 relocation_count;
  62. AOTRelocation *relocations;
  63. } AOTRelocationGroup;
  64. /* AOT function instance */
  65. typedef struct AOTFunctionInstance {
  66. char *func_name;
  67. uint32 func_index;
  68. bool is_import_func;
  69. union {
  70. struct {
  71. AOTFuncType *func_type;
  72. /* function pointer linked */
  73. void *func_ptr;
  74. } func;
  75. AOTImportFunc *func_import;
  76. } u;
  77. } AOTFunctionInstance;
  78. typedef struct AOTModule {
  79. uint32 module_type;
  80. /* import memories */
  81. uint32 import_memory_count;
  82. AOTImportMemory *import_memories;
  83. /* memory info */
  84. uint32 memory_count;
  85. AOTMemory *memories;
  86. /* init data */
  87. uint32 mem_init_data_count;
  88. AOTMemInitData **mem_init_data_list;
  89. /* import tables */
  90. uint32 import_table_count;
  91. AOTImportTable *import_tables;
  92. /* tables */
  93. uint32 table_count;
  94. AOTTable *tables;
  95. /* table init data info */
  96. uint32 table_init_data_count;
  97. AOTTableInitData **table_init_data_list;
  98. /* function type info */
  99. uint32 func_type_count;
  100. AOTFuncType **func_types;
  101. /* import global varaible info */
  102. uint32 import_global_count;
  103. AOTImportGlobal *import_globals;
  104. /* global variable info */
  105. uint32 global_count;
  106. AOTGlobal *globals;
  107. /* total global variable size */
  108. uint32 global_data_size;
  109. /* import function info */
  110. uint32 import_func_count;
  111. AOTImportFunc *import_funcs;
  112. /* function info */
  113. uint32 func_count;
  114. /* point to AOTed/JITed functions */
  115. void **func_ptrs;
  116. /* function type indexes */
  117. uint32 *func_type_indexes;
  118. /* export info */
  119. uint32 export_count;
  120. AOTExport *exports;
  121. /* start function index, -1 denotes no start function */
  122. uint32 start_func_index;
  123. /* start function, point to AOTed/JITed function */
  124. void *start_function;
  125. uint32 malloc_func_index;
  126. uint32 free_func_index;
  127. uint32 retain_func_index;
  128. /* AOTed code, NULL for JIT mode */
  129. void *code;
  130. uint32 code_size;
  131. /* literal for AOTed code, NULL for JIT mode */
  132. uint8 *literal;
  133. uint32 literal_size;
  134. /* data sections in AOT object file, including .data, .rodata
  135. * and .rodata.cstN. NULL for JIT mode. */
  136. AOTObjectDataSection *data_sections;
  137. uint32 data_section_count;
  138. /* constant string set */
  139. HashMap *const_str_set;
  140. /* the index of auxiliary __data_end global,
  141. -1 means unexported */
  142. uint32 aux_data_end_global_index;
  143. /* auxiliary __data_end exported by wasm app */
  144. uint32 aux_data_end;
  145. /* the index of auxiliary __heap_base global,
  146. -1 means unexported */
  147. uint32 aux_heap_base_global_index;
  148. /* auxiliary __heap_base exported by wasm app */
  149. uint32 aux_heap_base;
  150. /* the index of auxiliary stack top global,
  151. -1 means unexported */
  152. uint32 aux_stack_top_global_index;
  153. /* auxiliary stack bottom resolved */
  154. uint32 aux_stack_bottom;
  155. /* auxiliary stack size resolved */
  156. uint32 aux_stack_size;
  157. /* is jit mode or not */
  158. bool is_jit_mode;
  159. #if WASM_ENABLE_JIT != 0
  160. WASMModule *wasm_module;
  161. AOTCompContext *comp_ctx;
  162. AOTCompData *comp_data;
  163. #endif
  164. #if WASM_ENABLE_LIBC_WASI != 0
  165. WASIArguments wasi_args;
  166. bool is_wasi_module;
  167. #endif
  168. } AOTModule;
  169. typedef union {
  170. uint64 _make_it_8_bytes_;
  171. void *ptr;
  172. } AOTPointer;
  173. typedef union {
  174. uint64 u64;
  175. uint32 u32[2];
  176. } MemBound;
  177. typedef struct AOTMemoryInstance {
  178. uint32 module_type;
  179. /* shared memory flag */
  180. bool is_shared;
  181. /* memory space info */
  182. uint32 num_bytes_per_page;
  183. uint32 cur_page_count;
  184. uint32 max_page_count;
  185. uint32 memory_data_size;
  186. AOTPointer memory_data;
  187. AOTPointer memory_data_end;
  188. /* heap space info */
  189. AOTPointer heap_data;
  190. AOTPointer heap_data_end;
  191. AOTPointer heap_handle;
  192. /* boundary check constants for aot code */
  193. MemBound mem_bound_check_1byte;
  194. MemBound mem_bound_check_2bytes;
  195. MemBound mem_bound_check_4bytes;
  196. MemBound mem_bound_check_8bytes;
  197. MemBound mem_bound_check_16bytes;
  198. } AOTMemoryInstance;
  199. typedef struct AOTModuleInstance {
  200. uint32 module_type;
  201. /* memories */
  202. uint32 memory_count;
  203. AOTPointer memories;
  204. /* global and table info */
  205. uint32 global_data_size;
  206. uint32 table_size;
  207. AOTPointer global_data;
  208. AOTPointer table_data;
  209. /* funciton pointer array */
  210. AOTPointer func_ptrs;
  211. /* function type indexes */
  212. AOTPointer func_type_indexes;
  213. /* export info */
  214. uint32 export_func_count;
  215. uint32 export_global_count;
  216. uint32 export_mem_count;
  217. uint32 export_tab_count;
  218. AOTPointer export_funcs;
  219. AOTPointer export_globals;
  220. AOTPointer export_memories;
  221. AOTPointer export_tables;
  222. /* The exception buffer for current thread. */
  223. char cur_exception[128];
  224. /* The custom data that can be set/get by
  225. * wasm_runtime_set_custom_data/wasm_runtime_get_custom_data */
  226. AOTPointer custom_data;
  227. /* The AOT module */
  228. AOTPointer aot_module;
  229. /* WASI context */
  230. AOTPointer wasi_ctx;
  231. /* others */
  232. uint32 temp_ret;
  233. uint32 llvm_stack;
  234. uint32 default_wasm_stack_size;
  235. /* reserved */
  236. uint32 reserved[11];
  237. union {
  238. uint64 _make_it_8_byte_aligned_;
  239. AOTMemoryInstance memory_instances[1];
  240. uint8 bytes[1];
  241. } global_table_data;
  242. } AOTModuleInstance;
  243. /* Target info, read from ELF header of object file */
  244. typedef struct AOTTargetInfo {
  245. /* Binary type, elf32l/elf32b/elf64l/elf64b */
  246. uint16 bin_type;
  247. /* ABI type */
  248. uint16 abi_type;
  249. /* Object file type */
  250. uint16 e_type;
  251. /* Architecture */
  252. uint16 e_machine;
  253. /* Object file version */
  254. uint32 e_version;
  255. /* Processor-specific flags */
  256. uint32 e_flags;
  257. /* Reserved */
  258. uint32 reserved;
  259. /* Arch name */
  260. char arch[16];
  261. } AOTTargetInfo;
  262. /**
  263. * Load a AOT module from aot file buffer
  264. * @param buf the byte buffer which contains the AOT file data
  265. * @param size the size of the buffer
  266. * @param error_buf output of the error info
  267. * @param error_buf_size the size of the error string
  268. *
  269. * @return return AOT module loaded, NULL if failed
  270. */
  271. AOTModule*
  272. aot_load_from_aot_file(const uint8 *buf, uint32 size,
  273. char *error_buf, uint32 error_buf_size);
  274. /**
  275. * Load a AOT module from a specified AOT section list.
  276. *
  277. * @param section_list the section list which contains each section data
  278. * @param error_buf output of the error info
  279. * @param error_buf_size the size of the error string
  280. *
  281. * @return return AOT module loaded, NULL if failed
  282. */
  283. AOTModule*
  284. aot_load_from_sections(AOTSection *section_list,
  285. char *error_buf, uint32 error_buf_size);
  286. #if WASM_ENABLE_JIT != 0
  287. /**
  288. * Convert WASM module to AOT module
  289. *
  290. * @param wasm_module the WASM module to convert
  291. * @param error_buf output of the error info
  292. * @param error_buf_size the size of the error string
  293. *
  294. * @return return AOT module loaded, NULL if failed
  295. */
  296. AOTModule*
  297. aot_convert_wasm_module(WASMModule *wasm_module,
  298. char *error_buf, uint32 error_buf_size);
  299. #endif
  300. /**
  301. * Unload a AOT module.
  302. *
  303. * @param module the module to be unloaded
  304. */
  305. void
  306. aot_unload(AOTModule *module);
  307. /**
  308. * Instantiate a AOT module.
  309. *
  310. * @param module the AOT module to instantiate
  311. * @param is_sub_inst the flag of sub instance
  312. * @param heap_size the default heap size of the module instance, a heap will
  313. * be created besides the app memory space. Both wasm app and native
  314. * function can allocate memory from the heap. If heap_size is 0, the
  315. * default heap size will be used.
  316. * @param error_buf buffer to output the error info if failed
  317. * @param error_buf_size the size of the error buffer
  318. *
  319. * @return return the instantiated AOT module instance, NULL if failed
  320. */
  321. AOTModuleInstance*
  322. aot_instantiate(AOTModule *module, bool is_sub_inst,
  323. uint32 stack_size, uint32 heap_size,
  324. char *error_buf, uint32 error_buf_size);
  325. /**
  326. * Deinstantiate a AOT module instance, destroy the resources.
  327. *
  328. * @param module_inst the AOT module instance to destroy
  329. * @param is_sub_inst the flag of sub instance
  330. */
  331. void
  332. aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
  333. /**
  334. * Lookup an exported function in the AOT module instance.
  335. *
  336. * @param module_inst the module instance
  337. * @param name the name of the function
  338. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  339. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  340. *
  341. * @return the function instance found
  342. */
  343. AOTFunctionInstance*
  344. aot_lookup_function(const AOTModuleInstance *module_inst,
  345. const char *name, const char *signature);
  346. /**
  347. * Call the given AOT function of a AOT module instance with
  348. * arguments.
  349. *
  350. * @param exec_env the execution environment
  351. * @param function the function to be called
  352. * @param argc the number of arguments
  353. * @param argv the arguments. If the function method has return value,
  354. * the first (or first two in case 64-bit return value) element of
  355. * argv stores the return value of the called AOT function after this
  356. * function returns.
  357. *
  358. * @return true if success, false otherwise and exception will be thrown,
  359. * the caller can call aot_get_exception to get exception info.
  360. */
  361. bool
  362. aot_call_function(WASMExecEnv *exec_env,
  363. AOTFunctionInstance *function,
  364. unsigned argc, uint32 argv[]);
  365. bool
  366. aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
  367. AOTFunctionInstance *function,
  368. unsigned argc, uint32 argv[]);
  369. /**
  370. * Set AOT module instance exception with exception string
  371. *
  372. * @param module the AOT module instance
  373. *
  374. * @param exception current exception string
  375. */
  376. void
  377. aot_set_exception(AOTModuleInstance *module_inst,
  378. const char *exception);
  379. void
  380. aot_set_exception_with_id(AOTModuleInstance *module_inst,
  381. uint32 id);
  382. /**
  383. * Get exception info of the AOT module instance.
  384. *
  385. * @param module_inst the AOT module instance
  386. *
  387. * @return the exception string
  388. */
  389. const char*
  390. aot_get_exception(AOTModuleInstance *module_inst);
  391. /**
  392. * Clear exception info of the AOT module instance.
  393. *
  394. * @param module_inst the AOT module instance
  395. */
  396. void
  397. aot_clear_exception(AOTModuleInstance *module_inst);
  398. uint32
  399. aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
  400. void **p_native_addr);
  401. void
  402. aot_module_free(AOTModuleInstance *module_inst, uint32 ptr);
  403. uint32
  404. aot_module_dup_data(AOTModuleInstance *module_inst,
  405. const char *src, uint32 size);
  406. bool
  407. aot_validate_app_addr(AOTModuleInstance *module_inst,
  408. uint32 app_offset, uint32 size);
  409. bool
  410. aot_validate_native_addr(AOTModuleInstance *module_inst,
  411. void *native_ptr, uint32 size);
  412. void *
  413. aot_addr_app_to_native(AOTModuleInstance *module_inst, uint32 app_offset);
  414. uint32
  415. aot_addr_native_to_app(AOTModuleInstance *module_inst, void *native_ptr);
  416. bool
  417. aot_get_app_addr_range(AOTModuleInstance *module_inst,
  418. uint32 app_offset,
  419. uint32 *p_app_start_offset,
  420. uint32 *p_app_end_offset);
  421. bool
  422. aot_get_native_addr_range(AOTModuleInstance *module_inst,
  423. uint8 *native_ptr,
  424. uint8 **p_native_start_addr,
  425. uint8 **p_native_end_addr);
  426. bool
  427. aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count);
  428. /**
  429. * Compare whether two wasm types are equal according to the indexs
  430. *
  431. * @param module_inst the AOT module instance
  432. * @param type1_idx index of the first wasm type
  433. * @param type2_idx index of the second wasm type
  434. *
  435. * @return true if equal, false otherwise
  436. */
  437. bool
  438. aot_is_wasm_type_equal(AOTModuleInstance *module_inst,
  439. uint32 type1_idx, uint32 type2_idx);
  440. /**
  441. * Invoke native function from aot code
  442. */
  443. bool
  444. aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
  445. uint32 argc, uint32 *argv);
  446. bool
  447. aot_call_indirect(WASMExecEnv *exec_env,
  448. bool check_func_type, uint32 func_type_idx,
  449. uint32 table_elem_idx,
  450. uint32 argc, uint32 *argv);
  451. uint32
  452. aot_get_plt_table_size();
  453. void *
  454. aot_memmove(void *dest, const void *src, size_t n);
  455. void *
  456. aot_memset(void *s, int c, size_t n);
  457. #if WASM_ENABLE_BULK_MEMORY != 0
  458. bool
  459. aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index,
  460. uint32 offset, uint32 len, uint32 dst);
  461. bool
  462. aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index);
  463. #endif
  464. #if WASM_ENABLE_THREAD_MGR != 0
  465. bool
  466. aot_set_aux_stack(WASMExecEnv *exec_env,
  467. uint32 start_offset, uint32 size);
  468. bool
  469. aot_get_aux_stack(WASMExecEnv *exec_env,
  470. uint32 *start_offset, uint32 *size);
  471. #endif
  472. #ifdef OS_ENABLE_HW_BOUND_CHECK
  473. bool
  474. aot_signal_init();
  475. void
  476. aot_signal_destroy();
  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. #ifdef __cplusplus
  485. } /* end of extern "C" */
  486. #endif
  487. #endif /* end of _AOT_RUNTIME_H_ */