Enclave.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <inttypes.h>
  8. #include <stdbool.h>
  9. #include "Enclave_t.h"
  10. #include "wasm_export.h"
  11. #include "bh_platform.h"
  12. #if WASM_ENABLE_LIB_RATS != 0
  13. #include <openssl/sha.h>
  14. char wasm_module_hash[SHA256_DIGEST_LENGTH];
  15. #endif
  16. extern "C" {
  17. typedef int (*os_print_function_t)(const char *message);
  18. extern void
  19. os_set_print_function(os_print_function_t pf);
  20. int
  21. enclave_print(const char *message)
  22. {
  23. int bytes_written = 0;
  24. if (SGX_SUCCESS != ocall_print(&bytes_written, message))
  25. return 0;
  26. return bytes_written;
  27. }
  28. }
  29. typedef enum EcallCmd {
  30. CMD_INIT_RUNTIME = 0, /* wasm_runtime_init/full_init() */
  31. CMD_LOAD_MODULE, /* wasm_runtime_load() */
  32. CMD_INSTANTIATE_MODULE, /* wasm_runtime_instantiate() */
  33. CMD_LOOKUP_FUNCTION, /* wasm_runtime_lookup_function() */
  34. CMD_CREATE_EXEC_ENV, /* wasm_runtime_create_exec_env() */
  35. CMD_CALL_WASM, /* wasm_runtime_call_wasm */
  36. CMD_EXEC_APP_FUNC, /* wasm_application_execute_func() */
  37. CMD_EXEC_APP_MAIN, /* wasm_application_execute_main() */
  38. CMD_GET_EXCEPTION, /* wasm_runtime_get_exception() */
  39. CMD_DEINSTANTIATE_MODULE, /* wasm_runtime_deinstantiate() */
  40. CMD_UNLOAD_MODULE, /* wasm_runtime_unload() */
  41. CMD_DESTROY_RUNTIME, /* wasm_runtime_destroy() */
  42. CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */
  43. CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */
  44. CMD_GET_VERSION, /* wasm_runtime_get_version() */
  45. } EcallCmd;
  46. typedef struct EnclaveModule {
  47. wasm_module_t module;
  48. uint8 *wasm_file;
  49. uint32 wasm_file_size;
  50. char *wasi_arg_buf;
  51. char **wasi_dir_list;
  52. uint32 wasi_dir_list_size;
  53. char **wasi_env_list;
  54. uint32 wasi_env_list_size;
  55. char **wasi_addr_pool_list;
  56. uint32 wasi_addr_pool_list_size;
  57. char **wasi_argv;
  58. uint32 wasi_argc;
  59. bool is_xip_file;
  60. uint32 total_size_mapped;
  61. } EnclaveModule;
  62. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  63. static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
  64. #endif
  65. static void
  66. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  67. {
  68. if (error_buf != NULL)
  69. snprintf(error_buf, error_buf_size, "%s", string);
  70. }
  71. static void
  72. handle_cmd_init_runtime(uint64 *args, uint32 argc)
  73. {
  74. uint32 max_thread_num;
  75. RuntimeInitArgs init_args;
  76. bh_assert(argc == 1);
  77. os_set_print_function(enclave_print);
  78. max_thread_num = (uint32)args[0];
  79. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  80. init_args.max_thread_num = max_thread_num;
  81. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  82. init_args.mem_alloc_type = Alloc_With_Pool;
  83. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  84. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  85. #else
  86. init_args.mem_alloc_type = Alloc_With_System_Allocator;
  87. #endif
  88. /* initialize runtime environment */
  89. if (!wasm_runtime_full_init(&init_args)) {
  90. LOG_ERROR("Init runtime environment failed.\n");
  91. args[0] = false;
  92. return;
  93. }
  94. args[0] = true;
  95. LOG_VERBOSE("Init runtime environment success.\n");
  96. }
  97. static void
  98. handle_cmd_destroy_runtime()
  99. {
  100. wasm_runtime_destroy();
  101. LOG_VERBOSE("Destroy runtime success.\n");
  102. }
  103. static uint8 *
  104. align_ptr(const uint8 *p, uint32 b)
  105. {
  106. uintptr_t v = (uintptr_t)p;
  107. uintptr_t m = b - 1;
  108. return (uint8 *)((v + m) & ~m);
  109. }
  110. #define AOT_SECTION_TYPE_TARGET_INFO 0
  111. #define AOT_SECTION_TYPE_SIGANATURE 6
  112. #define E_TYPE_XIP 4
  113. #define CHECK_BUF(buf, buf_end, length) \
  114. do { \
  115. if ((uintptr_t)buf + length < (uintptr_t)buf \
  116. || (uintptr_t)buf + length > (uintptr_t)buf_end) \
  117. return false; \
  118. } while (0)
  119. #define read_uint16(p, p_end, res) \
  120. do { \
  121. p = (uint8 *)align_ptr(p, sizeof(uint16)); \
  122. CHECK_BUF(p, p_end, sizeof(uint16)); \
  123. res = *(uint16 *)p; \
  124. p += sizeof(uint16); \
  125. } while (0)
  126. #define read_uint32(p, p_end, res) \
  127. do { \
  128. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  129. CHECK_BUF(p, p_end, sizeof(uint32)); \
  130. res = *(uint32 *)p; \
  131. p += sizeof(uint32); \
  132. } while (0)
  133. static bool
  134. is_xip_file(const uint8 *buf, uint32 size)
  135. {
  136. const uint8 *p = buf, *p_end = buf + size;
  137. uint32 section_type, section_size;
  138. uint16 e_type;
  139. if (get_package_type(buf, size) != Wasm_Module_AoT)
  140. return false;
  141. CHECK_BUF(p, p_end, 8);
  142. p += 8;
  143. while (p < p_end) {
  144. read_uint32(p, p_end, section_type);
  145. read_uint32(p, p_end, section_size);
  146. CHECK_BUF(p, p_end, section_size);
  147. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  148. p += 4;
  149. read_uint16(p, p_end, e_type);
  150. return (e_type == E_TYPE_XIP) ? true : false;
  151. }
  152. else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
  153. return false;
  154. }
  155. p += section_size;
  156. }
  157. return false;
  158. }
  159. static void
  160. handle_cmd_load_module(uint64 *args, uint32 argc)
  161. {
  162. uint64 *args_org = args;
  163. char *wasm_file = *(char **)args++;
  164. uint32 wasm_file_size = *(uint32 *)args++;
  165. char *error_buf = *(char **)args++;
  166. uint32 error_buf_size = *(uint32 *)args++;
  167. uint64 total_size = sizeof(EnclaveModule) + (uint64)wasm_file_size;
  168. EnclaveModule *enclave_module;
  169. bh_assert(argc == 4);
  170. if (!is_xip_file((uint8 *)wasm_file, wasm_file_size)) {
  171. if (total_size >= UINT32_MAX
  172. || !(enclave_module = (EnclaveModule *)wasm_runtime_malloc(
  173. (uint32)total_size))) {
  174. set_error_buf(error_buf, error_buf_size,
  175. "WASM module load failed: "
  176. "allocate memory failed.");
  177. *(void **)args_org = NULL;
  178. return;
  179. }
  180. memset(enclave_module, 0, (uint32)total_size);
  181. }
  182. else {
  183. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  184. int map_flags = MMAP_MAP_NONE;
  185. if (total_size >= UINT32_MAX
  186. || !(enclave_module = (EnclaveModule *)os_mmap(
  187. NULL, (uint32)total_size, map_prot, map_flags))) {
  188. set_error_buf(error_buf, error_buf_size,
  189. "WASM module load failed: mmap memory failed.");
  190. *(void **)args_org = NULL;
  191. return;
  192. }
  193. memset(enclave_module, 0, (uint32)total_size);
  194. enclave_module->is_xip_file = true;
  195. enclave_module->total_size_mapped = (uint32)total_size;
  196. }
  197. enclave_module->wasm_file = (uint8 *)enclave_module + sizeof(EnclaveModule);
  198. bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file,
  199. wasm_file_size);
  200. if (!(enclave_module->module =
  201. wasm_runtime_load(enclave_module->wasm_file, wasm_file_size,
  202. error_buf, error_buf_size))) {
  203. if (!enclave_module->is_xip_file)
  204. wasm_runtime_free(enclave_module);
  205. else
  206. os_munmap(enclave_module, (uint32)total_size);
  207. *(void **)args_org = NULL;
  208. return;
  209. }
  210. *(EnclaveModule **)args_org = enclave_module;
  211. #if WASM_ENABLE_LIB_RATS != 0
  212. SHA256_CTX sha256;
  213. SHA256_Init(&sha256);
  214. SHA256_Update(&sha256, wasm_file, wasm_file_size);
  215. SHA256_Final((unsigned char *)wasm_module_hash, &sha256);
  216. #endif
  217. LOG_VERBOSE("Load module success.\n");
  218. }
  219. static void
  220. handle_cmd_unload_module(uint64 *args, uint32 argc)
  221. {
  222. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  223. uint32 i;
  224. bh_assert(argc == 1);
  225. if (enclave_module->wasi_arg_buf)
  226. wasm_runtime_free(enclave_module->wasi_arg_buf);
  227. wasm_runtime_unload(enclave_module->module);
  228. if (!enclave_module->is_xip_file)
  229. wasm_runtime_free(enclave_module);
  230. else
  231. os_munmap(enclave_module, enclave_module->total_size_mapped);
  232. LOG_VERBOSE("Unload module success.\n");
  233. }
  234. static void
  235. handle_cmd_instantiate_module(uint64 *args, uint32 argc)
  236. {
  237. uint64 *args_org = args;
  238. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  239. uint32 stack_size = *(uint32 *)args++;
  240. uint32 heap_size = *(uint32 *)args++;
  241. char *error_buf = *(char **)args++;
  242. uint32 error_buf_size = *(uint32 *)args++;
  243. wasm_module_inst_t module_inst;
  244. bh_assert(argc == 5);
  245. if (!(module_inst =
  246. wasm_runtime_instantiate(enclave_module->module, stack_size,
  247. heap_size, error_buf, error_buf_size))) {
  248. *(void **)args_org = NULL;
  249. return;
  250. }
  251. *(wasm_module_inst_t *)args_org = module_inst;
  252. LOG_VERBOSE("Instantiate module success.\n");
  253. }
  254. static void
  255. handle_cmd_deinstantiate_module(uint64 *args, uint32 argc)
  256. {
  257. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  258. bh_assert(argc == 1);
  259. wasm_runtime_deinstantiate(module_inst);
  260. LOG_VERBOSE("Deinstantiate module success.\n");
  261. }
  262. static void
  263. handle_cmd_get_exception(uint64 *args, uint32 argc)
  264. {
  265. uint64 *args_org = args;
  266. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  267. char *exception = *(char **)args++;
  268. uint32 exception_size = *(uint32 *)args++;
  269. const char *exception1;
  270. bh_assert(argc == 3);
  271. if ((exception1 = wasm_runtime_get_exception(module_inst))) {
  272. snprintf(exception, exception_size, "%s", exception1);
  273. args_org[0] = true;
  274. }
  275. else {
  276. args_org[0] = false;
  277. }
  278. }
  279. static void
  280. handle_cmd_exec_app_main(uint64 *args, int32 argc)
  281. {
  282. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  283. uint32 app_argc = *(uint32 *)args++;
  284. char **app_argv = NULL;
  285. uint64 total_size;
  286. int32 i;
  287. bh_assert(argc >= 3);
  288. bh_assert(app_argc >= 1);
  289. total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2);
  290. if (total_size >= UINT32_MAX
  291. || !(app_argv = (char **)wasm_runtime_malloc(total_size))) {
  292. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  293. return;
  294. }
  295. for (i = 0; i < app_argc; i++) {
  296. app_argv[i] = (char *)(uintptr_t)args[i];
  297. }
  298. wasm_application_execute_main(module_inst, app_argc - 1, app_argv + 1);
  299. wasm_runtime_free(app_argv);
  300. }
  301. static void
  302. handle_cmd_exec_app_func(uint64 *args, int32 argc)
  303. {
  304. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  305. char *func_name = *(char **)args++;
  306. uint32 app_argc = *(uint32 *)args++;
  307. char **app_argv = NULL;
  308. uint64 total_size;
  309. int32 i, func_name_len = strlen(func_name);
  310. bh_assert(argc == app_argc + 3);
  311. total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2);
  312. if (total_size >= UINT32_MAX
  313. || !(app_argv = (char **)wasm_runtime_malloc(total_size))) {
  314. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  315. return;
  316. }
  317. for (i = 0; i < app_argc; i++) {
  318. app_argv[i] = (char *)(uintptr_t)args[i];
  319. }
  320. wasm_application_execute_func(module_inst, func_name, app_argc, app_argv);
  321. wasm_runtime_free(app_argv);
  322. }
  323. static void
  324. handle_cmd_set_log_level(uint64 *args, uint32 argc)
  325. {
  326. #if WASM_ENABLE_LOG != 0
  327. LOG_VERBOSE("Set log verbose level to %d.\n", (int)args[0]);
  328. bh_log_set_verbose_level((int)args[0]);
  329. #endif
  330. }
  331. #ifndef SGX_DISABLE_WASI
  332. static void
  333. handle_cmd_set_wasi_args(uint64 *args, int32 argc)
  334. {
  335. uint64 *args_org = args;
  336. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  337. char **dir_list = *(char ***)args++;
  338. uint32 dir_list_size = *(uint32 *)args++;
  339. char **env_list = *(char ***)args++;
  340. uint32 env_list_size = *(uint32 *)args++;
  341. int stdinfd = *(int *)args++;
  342. int stdoutfd = *(int *)args++;
  343. int stderrfd = *(int *)args++;
  344. char **wasi_argv = *(char ***)args++;
  345. char *p, *p1;
  346. uint32 wasi_argc = *(uint32 *)args++;
  347. char **addr_pool_list = *(char ***)args++;
  348. uint32 addr_pool_list_size = *(uint32 *)args++;
  349. uint64 total_size = 0;
  350. int32 i, str_len;
  351. bh_assert(argc == 10);
  352. total_size += sizeof(char *) * (uint64)dir_list_size
  353. + sizeof(char *) * (uint64)env_list_size
  354. + sizeof(char *) * (uint64)addr_pool_list_size
  355. + sizeof(char *) * (uint64)wasi_argc;
  356. for (i = 0; i < dir_list_size; i++) {
  357. total_size += strlen(dir_list[i]) + 1;
  358. }
  359. for (i = 0; i < env_list_size; i++) {
  360. total_size += strlen(env_list[i]) + 1;
  361. }
  362. for (i = 0; i < addr_pool_list_size; i++) {
  363. total_size += strlen(addr_pool_list[i]) + 1;
  364. }
  365. for (i = 0; i < wasi_argc; i++) {
  366. total_size += strlen(wasi_argv[i]) + 1;
  367. }
  368. if (total_size >= UINT32_MAX
  369. || !(enclave_module->wasi_arg_buf = p =
  370. (char *)wasm_runtime_malloc((uint32)total_size))) {
  371. *args_org = false;
  372. return;
  373. }
  374. p1 = p + sizeof(char *) * dir_list_size + sizeof(char *) * env_list_size
  375. + sizeof(char *) * addr_pool_list_size + sizeof(char *) * wasi_argc;
  376. if (dir_list_size > 0) {
  377. enclave_module->wasi_dir_list = (char **)p;
  378. enclave_module->wasi_dir_list_size = dir_list_size;
  379. for (i = 0; i < dir_list_size; i++) {
  380. enclave_module->wasi_dir_list[i] = p1;
  381. str_len = strlen(dir_list[i]);
  382. bh_memcpy_s(p1, str_len + 1, dir_list[i], str_len + 1);
  383. p1 += str_len + 1;
  384. }
  385. p += sizeof(char *) * dir_list_size;
  386. }
  387. if (env_list_size > 0) {
  388. enclave_module->wasi_env_list = (char **)p;
  389. enclave_module->wasi_env_list_size = env_list_size;
  390. for (i = 0; i < env_list_size; i++) {
  391. enclave_module->wasi_env_list[i] = p1;
  392. str_len = strlen(env_list[i]);
  393. bh_memcpy_s(p1, str_len + 1, env_list[i], str_len + 1);
  394. p1 += str_len + 1;
  395. }
  396. p += sizeof(char *) * env_list_size;
  397. }
  398. if (addr_pool_list_size > 0) {
  399. enclave_module->wasi_addr_pool_list = (char **)p;
  400. enclave_module->wasi_addr_pool_list_size = addr_pool_list_size;
  401. for (i = 0; i < addr_pool_list_size; i++) {
  402. enclave_module->wasi_addr_pool_list[i] = p1;
  403. str_len = strlen(addr_pool_list[i]);
  404. bh_memcpy_s(p1, str_len + 1, addr_pool_list[i], str_len + 1);
  405. p1 += str_len + 1;
  406. }
  407. p += sizeof(char *) * addr_pool_list_size;
  408. }
  409. if (wasi_argc > 0) {
  410. enclave_module->wasi_argv = (char **)p;
  411. enclave_module->wasi_argc = wasi_argc;
  412. for (i = 0; i < wasi_argc; i++) {
  413. enclave_module->wasi_argv[i] = p1;
  414. str_len = strlen(wasi_argv[i]);
  415. bh_memcpy_s(p1, str_len + 1, wasi_argv[i], str_len + 1);
  416. p1 += str_len + 1;
  417. }
  418. p += sizeof(char *) * wasi_argc;
  419. }
  420. wasm_runtime_set_wasi_args_ex(
  421. enclave_module->module, (const char **)enclave_module->wasi_dir_list,
  422. dir_list_size, NULL, 0, (const char **)enclave_module->wasi_env_list,
  423. env_list_size, enclave_module->wasi_argv, enclave_module->wasi_argc,
  424. (stdinfd != -1) ? stdinfd : 0, (stdoutfd != -1) ? stdoutfd : 1,
  425. (stderrfd != -1) ? stderrfd : 2);
  426. wasm_runtime_set_wasi_addr_pool(
  427. enclave_module->module,
  428. (const char **)enclave_module->wasi_addr_pool_list,
  429. addr_pool_list_size);
  430. *args_org = true;
  431. }
  432. #else
  433. static void
  434. handle_cmd_set_wasi_args(uint64 *args, int32 argc)
  435. {
  436. *args = true;
  437. }
  438. #endif /* end of SGX_DISABLE_WASI */
  439. static void
  440. handle_cmd_get_version(uint64 *args, uint32 argc)
  441. {
  442. uint32 major, minor, patch;
  443. bh_assert(argc == 3);
  444. wasm_runtime_get_version(&major, &minor, &patch);
  445. args[0] = major;
  446. args[1] = minor;
  447. args[2] = patch;
  448. }
  449. void
  450. ecall_handle_command(unsigned cmd, unsigned char *cmd_buf,
  451. unsigned cmd_buf_size)
  452. {
  453. uint64 *args = (uint64 *)cmd_buf;
  454. uint32 argc = cmd_buf_size / sizeof(uint64);
  455. switch (cmd) {
  456. case CMD_INIT_RUNTIME:
  457. handle_cmd_init_runtime(args, argc);
  458. break;
  459. case CMD_LOAD_MODULE:
  460. handle_cmd_load_module(args, argc);
  461. break;
  462. case CMD_SET_WASI_ARGS:
  463. handle_cmd_set_wasi_args(args, argc);
  464. break;
  465. case CMD_INSTANTIATE_MODULE:
  466. handle_cmd_instantiate_module(args, argc);
  467. break;
  468. case CMD_LOOKUP_FUNCTION:
  469. break;
  470. case CMD_CREATE_EXEC_ENV:
  471. break;
  472. case CMD_CALL_WASM:
  473. break;
  474. case CMD_EXEC_APP_FUNC:
  475. handle_cmd_exec_app_func(args, argc);
  476. break;
  477. case CMD_EXEC_APP_MAIN:
  478. handle_cmd_exec_app_main(args, argc);
  479. break;
  480. case CMD_GET_EXCEPTION:
  481. handle_cmd_get_exception(args, argc);
  482. break;
  483. case CMD_DEINSTANTIATE_MODULE:
  484. handle_cmd_deinstantiate_module(args, argc);
  485. break;
  486. case CMD_UNLOAD_MODULE:
  487. handle_cmd_unload_module(args, argc);
  488. break;
  489. case CMD_DESTROY_RUNTIME:
  490. handle_cmd_destroy_runtime();
  491. break;
  492. case CMD_SET_LOG_LEVEL:
  493. handle_cmd_set_log_level(args, argc);
  494. break;
  495. case CMD_GET_VERSION:
  496. handle_cmd_get_version(args, argc);
  497. break;
  498. default:
  499. LOG_ERROR("Unknown command %d\n", cmd);
  500. break;
  501. }
  502. }
  503. void
  504. ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
  505. {
  506. wasm_module_t wasm_module = NULL;
  507. wasm_module_inst_t wasm_module_inst = NULL;
  508. RuntimeInitArgs init_args;
  509. char error_buf[128];
  510. const char *exception;
  511. os_set_print_function(enclave_print);
  512. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  513. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  514. init_args.mem_alloc_type = Alloc_With_Pool;
  515. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  516. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  517. #else
  518. init_args.mem_alloc_type = Alloc_With_System_Allocator;
  519. #endif
  520. /* initialize runtime environment */
  521. if (!wasm_runtime_full_init(&init_args)) {
  522. enclave_print("Init runtime environment failed.");
  523. enclave_print("\n");
  524. return;
  525. }
  526. /* load WASM module */
  527. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  528. error_buf, sizeof(error_buf)))) {
  529. enclave_print(error_buf);
  530. enclave_print("\n");
  531. goto fail1;
  532. }
  533. /* instantiate the module */
  534. if (!(wasm_module_inst =
  535. wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024,
  536. error_buf, sizeof(error_buf)))) {
  537. enclave_print(error_buf);
  538. enclave_print("\n");
  539. goto fail2;
  540. }
  541. /* execute the main function of wasm app */
  542. wasm_application_execute_main(wasm_module_inst, 0, NULL);
  543. if ((exception = wasm_runtime_get_exception(wasm_module_inst))) {
  544. enclave_print(exception);
  545. enclave_print("\n");
  546. }
  547. /* destroy the module instance */
  548. wasm_runtime_deinstantiate(wasm_module_inst);
  549. fail2:
  550. /* unload the module */
  551. wasm_runtime_unload(wasm_module);
  552. fail1:
  553. /* destroy runtime environment */
  554. wasm_runtime_destroy();
  555. }