aot_runtime.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #define AOT_MAGIC_NUMBER 0x746f6100
  18. #define AOT_CURRENT_VERSION 1
  19. typedef enum AOTExceptionID {
  20. EXCE_UNREACHABLE = 0,
  21. EXCE_OUT_OF_MEMORY,
  22. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS,
  23. EXCE_INTEGER_OVERFLOW,
  24. EXCE_INTEGER_DIVIDE_BY_ZERO,
  25. EXCE_INVALID_CONVERSION_TO_INTEGER,
  26. EXCE_INVALID_FUNCTION_TYPE_INDEX,
  27. EXCE_INVALID_FUNCTION_INDEX,
  28. EXCE_UNDEFINED_ELEMENT,
  29. EXCE_UNINITIALIZED_ELEMENT,
  30. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  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. typedef struct AOTModule {
  65. uint32 module_type;
  66. /* memory info */
  67. uint32 num_bytes_per_page;
  68. uint32 mem_init_page_count;
  69. uint32 mem_max_page_count;
  70. uint32 mem_init_data_count;
  71. AOTMemInitData **mem_init_data_list;
  72. /* table info */
  73. uint32 table_size;
  74. uint32 table_init_data_count;
  75. AOTTableInitData **table_init_data_list;
  76. /* function type info */
  77. uint32 func_type_count;
  78. AOTFuncType **func_types;
  79. /* import global varaible info */
  80. uint32 import_global_count;
  81. AOTImportGlobal *import_globals;
  82. /* global variable info */
  83. uint32 global_count;
  84. AOTGlobal *globals;
  85. /* total global variable size */
  86. uint32 global_data_size;
  87. /* import function info */
  88. uint32 import_func_count;
  89. AOTImportFunc *import_funcs;
  90. /* function info */
  91. uint32 func_count;
  92. /* point to AOTed/JITed functions */
  93. void **func_ptrs;
  94. /* function type indexes */
  95. uint32 *func_type_indexes;
  96. /* export function info */
  97. uint32 export_func_count;
  98. AOTExportFunc *export_funcs;
  99. /* start function index, -1 denotes no start function */
  100. uint32 start_func_index;
  101. /* start function, point to AOTed/JITed function */
  102. void *start_function;
  103. /* AOTed code, NULL for JIT mode */
  104. void *code;
  105. uint32 code_size;
  106. /* data sections in AOT object file, including .data, .rodata
  107. * and .rodata.cstN. NULL for JIT mode. */
  108. AOTObjectDataSection *data_sections;
  109. uint32 data_section_count;
  110. /* constant string set */
  111. HashMap *const_str_set;
  112. uint32 llvm_aux_data_end;
  113. uint32 llvm_aux_stack_bottom;
  114. uint32 llvm_aux_stack_size;
  115. uint32 llvm_aux_stack_global_index;
  116. /* is jit mode or not */
  117. bool is_jit_mode;
  118. #if WASM_ENABLE_JIT
  119. WASMModule *wasm_module;
  120. AOTCompContext *comp_ctx;
  121. AOTCompData *comp_data;
  122. #endif
  123. #if WASM_ENABLE_LIBC_WASI != 0
  124. WASIArguments wasi_args;
  125. bool is_wasi_module;
  126. #endif
  127. } AOTModule;
  128. typedef union {
  129. uint64 _make_it_8_bytes_;
  130. void *ptr;
  131. } AOTPointer;
  132. typedef struct AOTModuleInstance {
  133. uint32 module_type;
  134. /* memory space info */
  135. uint32 mem_cur_page_count;
  136. uint32 mem_max_page_count;
  137. uint32 memory_data_size;
  138. AOTPointer memory_data;
  139. AOTPointer memory_data_end;
  140. /* heap space info */
  141. int32 heap_base_offset;
  142. uint32 heap_data_size;
  143. AOTPointer heap_data;
  144. AOTPointer heap_data_end;
  145. AOTPointer heap_handle;
  146. /* global and table info */
  147. uint32 global_data_size;
  148. uint32 table_size;
  149. AOTPointer global_data;
  150. AOTPointer table_data;
  151. /* funciton pointer array */
  152. AOTPointer func_ptrs;
  153. /* function type indexes */
  154. AOTPointer func_type_indexes;
  155. /* The exception buffer for current thread. */
  156. char cur_exception[128];
  157. /* The custom data that can be set/get by
  158. * wasm_runtime_set_custom_data/wasm_runtime_get_custom_data */
  159. AOTPointer custom_data;
  160. /* The AOT module */
  161. AOTPointer aot_module;
  162. /* WASI context */
  163. AOTPointer wasi_ctx;
  164. /* others */
  165. int32 temp_ret;
  166. uint32 llvm_stack;
  167. int32 DYNAMICTOP_PTR_offset;
  168. uint32 default_wasm_stack_size;
  169. /* reserved */
  170. uint32 reserved[16];
  171. union {
  172. uint64 _make_it_8_byte_aligned_;
  173. uint8 bytes[1];
  174. } global_table_heap_data;
  175. } AOTModuleInstance;
  176. typedef AOTExportFunc AOTFunctionInstance;
  177. /* Target info, read from ELF header of object file */
  178. typedef struct AOTTargetInfo {
  179. /* Binary type, elf32l/elf32b/elf64l/elf64b */
  180. uint16 bin_type;
  181. /* ABI type */
  182. uint16 abi_type;
  183. /* Object file type */
  184. uint16 e_type;
  185. /* Architecture */
  186. uint16 e_machine;
  187. /* Object file version */
  188. uint32 e_version;
  189. /* Processor-specific flags */
  190. uint32 e_flags;
  191. /* Reserved */
  192. uint32 reserved;
  193. /* Arch name */
  194. char arch[16];
  195. } AOTTargetInfo;
  196. /**
  197. * Load a AOT module from aot file buffer
  198. * @param buf the byte buffer which contains the AOT file data
  199. * @param size the size of the buffer
  200. * @param error_buf output of the error info
  201. * @param error_buf_size the size of the error string
  202. *
  203. * @return return AOT module loaded, NULL if failed
  204. */
  205. AOTModule*
  206. aot_load_from_aot_file(const uint8 *buf, uint32 size,
  207. char *error_buf, uint32 error_buf_size);
  208. /**
  209. * Load a AOT module from a specified AOT section list.
  210. *
  211. * @param section_list the section list which contains each section data
  212. * @param error_buf output of the error info
  213. * @param error_buf_size the size of the error string
  214. *
  215. * @return return AOT module loaded, NULL if failed
  216. */
  217. AOTModule*
  218. aot_load_from_sections(AOTSection *section_list,
  219. char *error_buf, uint32 error_buf_size);
  220. #if WASM_ENABLE_JIT != 0
  221. /**
  222. * Convert WASM module to AOT module
  223. *
  224. * @param wasm_module the WASM module to convert
  225. * @param error_buf output of the error info
  226. * @param error_buf_size the size of the error string
  227. *
  228. * @return return AOT module loaded, NULL if failed
  229. */
  230. AOTModule*
  231. aot_convert_wasm_module(WASMModule *wasm_module,
  232. char *error_buf, uint32 error_buf_size);
  233. #endif
  234. /**
  235. * Unload a AOT module.
  236. *
  237. * @param module the module to be unloaded
  238. */
  239. void
  240. aot_unload(AOTModule *module);
  241. /**
  242. * Instantiate a AOT module.
  243. *
  244. * @param module the AOT module to instantiate
  245. * @param heap_size the default heap size of the module instance, a heap will
  246. * be created besides the app memory space. Both wasm app and native
  247. * function can allocate memory from the heap. If heap_size is 0, the
  248. * default heap size will be used.
  249. * @param error_buf buffer to output the error info if failed
  250. * @param error_buf_size the size of the error buffer
  251. *
  252. * @return return the instantiated AOT module instance, NULL if failed
  253. */
  254. AOTModuleInstance*
  255. aot_instantiate(AOTModule *module,
  256. uint32 stack_size, uint32 heap_size,
  257. char *error_buf, uint32 error_buf_size);
  258. /**
  259. * Deinstantiate a AOT module instance, destroy the resources.
  260. *
  261. * @param module_inst the AOT module instance to destroy
  262. */
  263. void
  264. aot_deinstantiate(AOTModuleInstance *module_inst);
  265. /**
  266. * Lookup an exported function in the AOT module instance.
  267. *
  268. * @param module_inst the module instance
  269. * @param name the name of the function
  270. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  271. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  272. *
  273. * @return the function instance found
  274. */
  275. AOTFunctionInstance*
  276. aot_lookup_function(const AOTModuleInstance *module_inst,
  277. const char *name, const char *signature);
  278. /**
  279. * Call the given AOT function of a AOT module instance with
  280. * arguments.
  281. *
  282. * @param exec_env the execution environment
  283. * @param function the function to be called
  284. * @param argc the number of arguments
  285. * @param argv the arguments. If the function method has return value,
  286. * the first (or first two in case 64-bit return value) element of
  287. * argv stores the return value of the called AOT function after this
  288. * function returns.
  289. *
  290. * @return true if success, false otherwise and exception will be thrown,
  291. * the caller can call aot_get_exception to get exception info.
  292. */
  293. bool
  294. aot_call_function(WASMExecEnv *exec_env,
  295. AOTFunctionInstance *function,
  296. unsigned argc, uint32 argv[]);
  297. bool
  298. aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
  299. AOTFunctionInstance *function,
  300. unsigned argc, uint32 argv[]);
  301. /**
  302. * Set AOT module instance exception with exception string
  303. *
  304. * @param module the AOT module instance
  305. *
  306. * @param exception current exception string
  307. */
  308. void
  309. aot_set_exception(AOTModuleInstance *module_inst,
  310. const char *exception);
  311. void
  312. aot_set_exception_with_id(AOTModuleInstance *module_inst,
  313. uint32 id);
  314. /**
  315. * Get exception info of the AOT module instance.
  316. *
  317. * @param module_inst the AOT module instance
  318. *
  319. * @return the exception string
  320. */
  321. const char*
  322. aot_get_exception(AOTModuleInstance *module_inst);
  323. /**
  324. * Clear exception info of the AOT module instance.
  325. *
  326. * @param module_inst the AOT module instance
  327. */
  328. void
  329. aot_clear_exception(AOTModuleInstance *module_inst);
  330. int32
  331. aot_module_malloc(AOTModuleInstance *module_inst, uint32 size);
  332. void
  333. aot_module_free(AOTModuleInstance *module_inst, int32 ptr);
  334. int32
  335. aot_module_dup_data(AOTModuleInstance *module_inst,
  336. const char *src, uint32 size);
  337. bool
  338. aot_validate_app_addr(AOTModuleInstance *module_inst,
  339. int32 app_offset, uint32 size);
  340. bool
  341. aot_validate_native_addr(AOTModuleInstance *module_inst,
  342. void *native_ptr, uint32 size);
  343. void *
  344. aot_addr_app_to_native(AOTModuleInstance *module_inst, int32 app_offset);
  345. int32
  346. aot_addr_native_to_app(AOTModuleInstance *module_inst, void *native_ptr);
  347. bool
  348. aot_get_app_addr_range(AOTModuleInstance *module_inst,
  349. int32 app_offset,
  350. int32 *p_app_start_offset,
  351. int32 *p_app_end_offset);
  352. bool
  353. aot_get_native_addr_range(AOTModuleInstance *module_inst,
  354. uint8 *native_ptr,
  355. uint8 **p_native_start_addr,
  356. uint8 **p_native_end_addr);
  357. bool
  358. aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count);
  359. /**
  360. * Compare whether two wasm types are equal according to the indexs
  361. *
  362. * @param module_inst the AOT module instance
  363. * @param type1_idx index of the first wasm type
  364. * @param type2_idx index of the second wasm type
  365. *
  366. * @return true if equal, false otherwise
  367. */
  368. bool
  369. aot_is_wasm_type_equal(AOTModuleInstance *module_inst,
  370. uint32 type1_idx, uint32 type2_idx);
  371. uint32
  372. aot_get_plt_table_size();
  373. #ifdef __cplusplus
  374. } /* end of extern "C" */
  375. #endif
  376. #endif /* end of _AOT_RUNTIME_H_ */