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. /**
  137. * Load WASM module instance from AOT file.
  138. *
  139. * @param aot_file the AOT file of a WASM module
  140. * @param aot_file_size the AOT file size
  141. * @param heap_size the default heap size of the module instance, a heap will
  142. * be created besides the app memory space. Both wasm app and native
  143. * function can allocate memory from the heap. If heap_size is 0, the
  144. * default heap size will be used.
  145. * @param error_buf buffer to output the error info if failed
  146. * @param error_buf_size the size of the error buffer
  147. *
  148. * @return the instantiated WASM module instance, NULL if failed
  149. */
  150. wasm_module_inst_t
  151. wasm_runtime_load_aot(uint8_t *aot_file, uint32_t aot_file_size,
  152. uint32_t heap_size,
  153. char *error_buf, uint32_t error_buf_size);
  154. /**
  155. * Lookup an exported function in the WASM module instance.
  156. *
  157. * @param module_inst the module instance
  158. * @param name the name of the function
  159. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  160. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  161. *
  162. * @return the function instance found, if the module instance is loaded from
  163. * the AOT file, the return value is the function pointer
  164. */
  165. wasm_function_inst_t
  166. wasm_runtime_lookup_function(const wasm_module_inst_t module_inst,
  167. const char *name, const char *signature);
  168. /**
  169. * Create execution environment.
  170. *
  171. * @param stack_size the stack size to execute a WASM function
  172. *
  173. * @return the execution environment
  174. */
  175. wasm_exec_env_t
  176. wasm_runtime_create_exec_env(uint32_t stack_size);
  177. /**
  178. * Destroy the execution environment.
  179. *
  180. * @param env the execution environment to destroy
  181. */
  182. void
  183. wasm_runtime_destory_exec_env(wasm_exec_env_t env);
  184. /**
  185. * Call the given WASM function of a WASM module instance with
  186. * arguments (bytecode and AoT).
  187. *
  188. * @param module_inst the WASM module instance which the function belongs to
  189. * @param exec_env the execution environment to call the function. If the module
  190. * instance is created by AoT mode, it is ignored and just set it to NULL.
  191. * If the module instance is created by bytecode mode and it is NULL,
  192. * a temporary env object will be created
  193. * @param function the function to be called
  194. * @param argc the number of arguments
  195. * @param argv the arguments. If the function method has return value,
  196. * the first (or first two in case 64-bit return value) element of
  197. * argv stores the return value of the called WASM function after this
  198. * function returns.
  199. *
  200. * @return true if success, false otherwise and exception will be thrown,
  201. * the caller can call wasm_runtime_get_exception to get exception info.
  202. */
  203. bool
  204. wasm_runtime_call_wasm(wasm_module_inst_t module_inst,
  205. wasm_exec_env_t exec_env,
  206. wasm_function_inst_t function,
  207. uint32_t argc, uint32_t argv[]);
  208. /**
  209. * Get exception info of the WASM module instance.
  210. *
  211. * @param module_inst the WASM module instance
  212. *
  213. * @return the exception string
  214. */
  215. const char*
  216. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  217. /**
  218. * Clear exception info of the WASM module instance.
  219. *
  220. * @param module_inst the WASM module instance
  221. */
  222. void
  223. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  224. /**
  225. * Attach the current native thread to a WASM module instance.
  226. * A native thread cannot be attached simultaneously to two WASM module
  227. * instances. The WASM module instance will be attached to the native
  228. * thread which it is instantiated in by default.
  229. *
  230. * @param module_inst the WASM module instance to attach
  231. * @param thread_data the thread data that current native thread requires
  232. * the WASM module instance to store
  233. *
  234. * @return true if SUCCESS, false otherwise
  235. */
  236. bool
  237. wasm_runtime_attach_current_thread(wasm_module_inst_t module_inst,
  238. void *thread_data);
  239. /**
  240. * Detach the current native thread from a WASM module instance.
  241. *
  242. * @param module_inst the WASM module instance to detach
  243. */
  244. void
  245. wasm_runtime_detach_current_thread(wasm_module_inst_t module_inst);
  246. /**
  247. * Get the thread data that the current native thread requires the WASM
  248. * module instance to store when attaching.
  249. *
  250. * @return the thread data stored when attaching
  251. */
  252. void*
  253. wasm_runtime_get_current_thread_data();
  254. /**
  255. * Get current WASM module instance of the current native thread
  256. *
  257. * @return current WASM module instance of the current native thread, NULL
  258. * if not found
  259. */
  260. wasm_module_inst_t
  261. wasm_runtime_get_current_module_inst();
  262. /**
  263. * Allocate memory from the heap of WASM module instance
  264. *
  265. * @param module_inst the WASM module instance which contains heap
  266. * @param size the size bytes to allocate
  267. *
  268. * @return the allocated memory address, which is a relative offset to the
  269. * base address of the module instance's memory space, the value range
  270. * is (-heap_size, 0). Note that it is not an absolute address.
  271. * Return non-zero if success, zero if failed.
  272. */
  273. int32_t
  274. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size);
  275. /**
  276. * Free memory to the heap of WASM module instance
  277. *
  278. * @param module_inst the WASM module instance which contains heap
  279. * @param ptr the pointer to free
  280. */
  281. void
  282. wasm_runtime_module_free(wasm_module_inst_t module_inst, int32_t ptr);
  283. /**
  284. * Allocate memory from the heap of WASM module instance and initialize
  285. * the memory with src
  286. *
  287. * @param module_inst the WASM module instance which contains heap
  288. * @param src the source data to copy
  289. * @param size the size of the source data
  290. *
  291. * @return the allocated memory address, which is a relative offset to the
  292. * base address of the module instance's memory space, the value range
  293. * is (-heap_size, 0). Note that it is not an absolute address.
  294. * Return non-zero if success, zero if failed.
  295. */
  296. int32_t
  297. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  298. const char *src, uint32_t size);
  299. /**
  300. * Validate the app address, check whether it belongs to WASM module
  301. * instance's address space, or in its heap space or memory space.
  302. *
  303. * @param module_inst the WASM module instance
  304. * @param app_offset the app address to validate, which is a relative address
  305. * @param size the size bytes of the app address
  306. *
  307. * @return true if success, false otherwise. If failed, an exception will
  308. * be thrown.
  309. */
  310. bool
  311. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  312. int32_t app_offset, uint32_t size);
  313. /**
  314. * Validate the native address, check whether it belongs to WASM module
  315. * instance's address space, or in its heap space or memory space.
  316. *
  317. * @param module_inst the WASM module instance
  318. * @param native_ptr the native address to validate, which is an absolute
  319. * address
  320. * @param size the size bytes of the app address
  321. *
  322. * @return true if success, false otherwise. If failed, an exception will
  323. * be thrown.
  324. */
  325. bool
  326. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  327. void *native_ptr, uint32_t size);
  328. /**
  329. * Convert app address(relative address) to native address(absolute address)
  330. *
  331. * @param module_inst the WASM module instance
  332. * @param app_offset the app adress
  333. *
  334. * @return the native address converted
  335. */
  336. void *
  337. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  338. int32_t app_offset);
  339. /**
  340. * Convert native address(absolute address) to app address(relative address)
  341. *
  342. * @param module_inst the WASM module instance
  343. * @param native_ptr the native address
  344. *
  345. * @return the app address converted
  346. */
  347. int32_t
  348. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  349. void *native_ptr);
  350. /**
  351. * Find the unique main function from a WASM module instance
  352. * and execute that function.
  353. *
  354. * @param module_inst the WASM module instance
  355. * @param argc the number of arguments
  356. * @param argv the arguments array
  357. *
  358. * @return true if the main function is called, false otherwise.
  359. */
  360. bool
  361. wasm_application_execute_main(wasm_module_inst_t module_inst,
  362. int argc, char *argv[]);
  363. /**
  364. * Find the specified function in argv[0] from WASM module of current instance
  365. * and execute that function.
  366. *
  367. * @param module_inst the WASM module instance
  368. * @param name the name of the function to execute
  369. * @param argc the number of arguments
  370. * @param argv the arguments array
  371. *
  372. * @return true if the specified function is called, false otherwise.
  373. */
  374. bool
  375. wasm_application_execute_func(wasm_module_inst_t module_inst,
  376. const char *name, int argc, char *argv[]);
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380. #endif /* end of _WASM_EXPORT_H */