wasm_export.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _WASM_EXPORT_H
  17. #define _WASM_EXPORT_H
  18. #include <inttypes.h>
  19. #include <stdbool.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Uninstantiated WASM module loaded from WASM binary file */
  24. struct WASMModule;
  25. typedef struct WASMModule *wasm_module_t;
  26. /* Instantiated WASM module */
  27. struct WASMModuleInstance;
  28. typedef struct WASMModuleInstance *wasm_module_inst_t;
  29. /* Function instance */
  30. struct WASMFunctionInstance;
  31. typedef struct WASMFunctionInstance *wasm_function_inst_t;
  32. /* WASM section */
  33. typedef struct wasm_section {
  34. struct wasm_section *next;
  35. /* section type */
  36. int section_type;
  37. /* section body, not include type and size */
  38. uint8_t *section_body;
  39. /* section body size */
  40. uint32_t section_body_size;
  41. } wasm_section_t, *wasm_section_list_t;
  42. /* Execution environment, e.g. stack info */
  43. typedef struct WASMExecEnv {
  44. uint8_t *stack;
  45. uint32_t stack_size;
  46. } *wasm_exec_env_t;
  47. /* Package Type */
  48. typedef enum {
  49. Wasm_Module_Bytecode = 0,
  50. Wasm_Module_AoT,
  51. Package_Type_Unknown = 0xFFFF
  52. } package_type_t;
  53. /**
  54. * Initialize the WASM runtime environment.
  55. *
  56. * @return true if success, false otherwise
  57. */
  58. bool
  59. wasm_runtime_init();
  60. /**
  61. * Destroy the WASM runtime environment.
  62. */
  63. void
  64. wasm_runtime_destroy();
  65. /**
  66. * Get the package type of a buffer.
  67. *
  68. * @param buf the package buffer
  69. * @param size the package buffer size
  70. *
  71. * @return the package type, return Package_Type_Unknown if the type is unknown
  72. */
  73. package_type_t
  74. get_package_type(const uint8_t *buf, uint32_t size);
  75. /**
  76. * Load a WASM module from a specified byte buffer.
  77. *
  78. * @param buf the byte buffer which contains the WASM binary data
  79. * @param size the size of the buffer
  80. * @param error_buf output of the exception info
  81. * @param error_buf_size the size of the exception string
  82. *
  83. * @return return WASM module loaded, NULL if failed
  84. */
  85. wasm_module_t
  86. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  87. char *error_buf, uint32_t error_buf_size);
  88. /**
  89. * Load a WASM module from a specified WASM section list.
  90. *
  91. * @param section_list the section list which contains each section data
  92. * @param error_buf output of the exception info
  93. * @param error_buf_size the size of the exception string
  94. *
  95. * @return return WASM module loaded, NULL if failed
  96. */
  97. wasm_module_t
  98. wasm_runtime_load_from_sections(wasm_section_list_t section_list,
  99. char *error_buf, uint32_t error_buf_size);
  100. /**
  101. * Unload a WASM module.
  102. *
  103. * @param module the module to be unloaded
  104. */
  105. void
  106. wasm_runtime_unload(wasm_module_t module);
  107. /**
  108. * Instantiate a WASM module.
  109. *
  110. * @param module the WASM module to instantiate
  111. * @param stack_size the default stack size of the module instance, a stack
  112. * will be created when function wasm_runtime_call_wasm() is called
  113. * to run WASM function and the exec_env argument passed to
  114. * wasm_runtime_call_wasm() is NULL. That means this parameter is
  115. * ignored if exec_env is not NULL.
  116. * @param heap_size the default heap size of the module instance, a heap will
  117. * be created besides the app memory space. Both wasm app and native
  118. * function can allocate memory from the heap. If heap_size is 0, the
  119. * default heap size will be used.
  120. * @param error_buf buffer to output the error info if failed
  121. * @param error_buf_size the size of the error buffer
  122. *
  123. * @return return the instantiated WASM module instance, NULL if failed
  124. */
  125. wasm_module_inst_t
  126. wasm_runtime_instantiate(const wasm_module_t module,
  127. uint32_t stack_size, uint32_t heap_size,
  128. char *error_buf, uint32_t error_buf_size);
  129. /**
  130. * Deinstantiate a WASM module instance, destroy the resources.
  131. *
  132. * @param module_inst the WASM module instance to destroy
  133. */
  134. void
  135. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  136. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  137. bool
  138. wasm_runtime_set_ext_memory(wasm_module_inst_t module_inst,
  139. uint8_t *ext_mem_data, uint32_t ext_mem_size,
  140. char *error_buf, uint32_t error_buf_size);
  141. #endif
  142. /**
  143. * Load WASM module instance from AOT file.
  144. *
  145. * @param aot_file the AOT file of a WASM module
  146. * @param aot_file_size the AOT file size
  147. * @param heap_size the default heap size of the module instance, a heap will
  148. * be created besides the app memory space. Both wasm app and native
  149. * function can allocate memory from the heap. If heap_size is 0, the
  150. * default heap size will be used.
  151. * @param error_buf buffer to output the error info if failed
  152. * @param error_buf_size the size of the error buffer
  153. *
  154. * @return the instantiated WASM module instance, NULL if failed
  155. */
  156. wasm_module_inst_t
  157. wasm_runtime_load_aot(uint8_t *aot_file, uint32_t aot_file_size,
  158. uint32_t heap_size,
  159. char *error_buf, uint32_t error_buf_size);
  160. /**
  161. * Lookup an exported function in the WASM module instance.
  162. *
  163. * @param module_inst the module instance
  164. * @param name the name of the function
  165. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  166. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  167. *
  168. * @return the function instance found, if the module instance is loaded from
  169. * the AOT file, the return value is the function pointer
  170. */
  171. wasm_function_inst_t
  172. wasm_runtime_lookup_function(const wasm_module_inst_t module_inst,
  173. const char *name, const char *signature);
  174. /**
  175. * Create execution environment.
  176. *
  177. * @param stack_size the stack size to execute a WASM function
  178. *
  179. * @return the execution environment
  180. */
  181. wasm_exec_env_t
  182. wasm_runtime_create_exec_env(uint32_t stack_size);
  183. /**
  184. * Destroy the execution environment.
  185. *
  186. * @param env the execution environment to destroy
  187. */
  188. void
  189. wasm_runtime_destroy_exec_env(wasm_exec_env_t env);
  190. /**
  191. * Call the given WASM function of a WASM module instance with
  192. * arguments (bytecode and AoT).
  193. *
  194. * @param module_inst the WASM module instance which the function belongs to
  195. * @param exec_env the execution environment to call the function. If the module
  196. * instance is created by AoT mode, it is ignored and just set it to NULL.
  197. * If the module instance is created by bytecode mode and it is NULL,
  198. * a temporary env object will be created
  199. * @param function the function to be called
  200. * @param argc the number of arguments
  201. * @param argv the arguments. If the function method has return value,
  202. * the first (or first two in case 64-bit return value) element of
  203. * argv stores the return value of the called WASM function after this
  204. * function returns.
  205. *
  206. * @return true if success, false otherwise and exception will be thrown,
  207. * the caller can call wasm_runtime_get_exception to get exception info.
  208. */
  209. bool
  210. wasm_runtime_call_wasm(wasm_module_inst_t module_inst,
  211. wasm_exec_env_t exec_env,
  212. wasm_function_inst_t function,
  213. uint32_t argc, uint32_t argv[]);
  214. /**
  215. * Get exception info of the WASM module instance.
  216. *
  217. * @param module_inst the WASM module instance
  218. *
  219. * @return the exception string
  220. */
  221. const char*
  222. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  223. /**
  224. * Clear exception info of the WASM module instance.
  225. *
  226. * @param module_inst the WASM module instance
  227. */
  228. void
  229. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  230. /**
  231. * Attach the current native thread to a WASM module instance.
  232. * A native thread cannot be attached simultaneously to two WASM module
  233. * instances. The WASM module instance will be attached to the native
  234. * thread which it is instantiated in by default.
  235. *
  236. * @param module_inst the WASM module instance to attach
  237. * @param thread_data the thread data that current native thread requires
  238. * the WASM module instance to store
  239. *
  240. * @return true if SUCCESS, false otherwise
  241. */
  242. bool
  243. wasm_runtime_attach_current_thread(wasm_module_inst_t module_inst,
  244. void *thread_data);
  245. /**
  246. * Detach the current native thread from a WASM module instance.
  247. *
  248. * @param module_inst the WASM module instance to detach
  249. */
  250. void
  251. wasm_runtime_detach_current_thread(wasm_module_inst_t module_inst);
  252. /**
  253. * Get the thread data that the current native thread requires the WASM
  254. * module instance to store when attaching.
  255. *
  256. * @return the thread data stored when attaching
  257. */
  258. void*
  259. wasm_runtime_get_current_thread_data();
  260. /**
  261. * Allocate memory from the heap of WASM module instance
  262. *
  263. * @param module_inst the WASM module instance which contains heap
  264. * @param size the size bytes to allocate
  265. *
  266. * @return the allocated memory address, which is a relative offset to the
  267. * base address of the module instance's memory space, the value range
  268. * is (-heap_size, 0). Note that it is not an absolute address.
  269. * Return non-zero if success, zero if failed.
  270. */
  271. int32_t
  272. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size);
  273. /**
  274. * Free memory to the heap of WASM module instance
  275. *
  276. * @param module_inst the WASM module instance which contains heap
  277. * @param ptr the pointer to free
  278. */
  279. void
  280. wasm_runtime_module_free(wasm_module_inst_t module_inst, int32_t ptr);
  281. /**
  282. * Allocate memory from the heap of WASM module instance and initialize
  283. * the memory with src
  284. *
  285. * @param module_inst the WASM module instance which contains heap
  286. * @param src the source data to copy
  287. * @param size the size of the source data
  288. *
  289. * @return the allocated memory address, which is a relative offset to the
  290. * base address of the module instance's memory space, the value range
  291. * is (-heap_size, 0). Note that it is not an absolute address.
  292. * Return non-zero if success, zero if failed.
  293. */
  294. int32_t
  295. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  296. const char *src, uint32_t size);
  297. /**
  298. * Validate the app address, check whether it belongs to WASM module
  299. * instance's address space, or in its heap space or memory space.
  300. *
  301. * @param module_inst the WASM module instance
  302. * @param app_offset the app address to validate, which is a relative address
  303. * @param size the size bytes of the app address
  304. *
  305. * @return true if success, false otherwise. If failed, an exception will
  306. * be thrown.
  307. */
  308. bool
  309. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  310. int32_t app_offset, uint32_t size);
  311. /**
  312. * Validate the native address, check whether it belongs to WASM module
  313. * instance's address space, or in its heap space or memory space.
  314. *
  315. * @param module_inst the WASM module instance
  316. * @param native_ptr the native address to validate, which is an absolute
  317. * address
  318. * @param size the size bytes of the app address
  319. *
  320. * @return true if success, false otherwise. If failed, an exception will
  321. * be thrown.
  322. */
  323. bool
  324. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  325. void *native_ptr, uint32_t size);
  326. /**
  327. * Convert app address(relative address) to native address(absolute address)
  328. *
  329. * @param module_inst the WASM module instance
  330. * @param app_offset the app adress
  331. *
  332. * @return the native address converted
  333. */
  334. void *
  335. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  336. int32_t app_offset);
  337. /**
  338. * Convert native address(absolute address) to app address(relative address)
  339. *
  340. * @param module_inst the WASM module instance
  341. * @param native_ptr the native address
  342. *
  343. * @return the app address converted
  344. */
  345. int32_t
  346. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  347. void *native_ptr);
  348. /**
  349. * Get the app address range (relative address) that a app address belongs to
  350. *
  351. * @param module_inst the WASM module instance
  352. * @param app_offset the app address to retrieve
  353. * @param p_app_start_offset buffer to output the app start offset if not NULL
  354. * @param p_app_end_offset buffer to output the app end offset if not NULL
  355. *
  356. * @return true if success, false otherwise.
  357. */
  358. bool
  359. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  360. int32_t app_offset,
  361. int32_t *p_app_start_offset,
  362. int32_t *p_app_end_offset);
  363. /**
  364. * Get the native address range (absolute address) that a native address belongs to
  365. *
  366. * @param module_inst the WASM module instance
  367. * @param native_ptr the native address to retrieve
  368. * @param p_native_start_addr buffer to output the native start address if not NULL
  369. * @param p_native_end_addr buffer to output the native end address if not NULL
  370. *
  371. * @return true if success, false otherwise.
  372. */
  373. bool
  374. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  375. uint8_t *native_ptr,
  376. uint8_t **p_native_start_addr,
  377. uint8_t **p_native_end_addr);
  378. #ifdef __cplusplus
  379. }
  380. #endif
  381. #endif /* end of _WASM_EXPORT_H */