Enclave.cpp 23 KB

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