aot_runtime.h 14 KB

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