Enclave.cpp 21 KB

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