Enclave.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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_SIGANATURE 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_SIGANATURE) {
  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. set_error_buf(error_buf, error_buf_size,
  214. "WASM module load failed: mmap memory failed.");
  215. *(void **)args_org = NULL;
  216. return;
  217. }
  218. memset(enclave_module, 0, (uint32)total_size);
  219. enclave_module->is_xip_file = true;
  220. enclave_module->total_size_mapped = (uint32)total_size;
  221. }
  222. enclave_module->wasm_file = (uint8 *)enclave_module + sizeof(EnclaveModule);
  223. bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file,
  224. wasm_file_size);
  225. if (!(enclave_module->module =
  226. wasm_runtime_load(enclave_module->wasm_file, wasm_file_size,
  227. error_buf, error_buf_size))) {
  228. if (!enclave_module->is_xip_file)
  229. wasm_runtime_free(enclave_module);
  230. else
  231. os_munmap(enclave_module, (uint32)total_size);
  232. *(void **)args_org = NULL;
  233. return;
  234. }
  235. *(EnclaveModule **)args_org = enclave_module;
  236. #if WASM_ENABLE_LIB_RATS != 0
  237. /* Calculate the module hash */
  238. SHA256_CTX sha256;
  239. SHA256_Init(&sha256);
  240. SHA256_Update(&sha256, wasm_file, wasm_file_size);
  241. SHA256_Final((unsigned char *)enclave_module->module_hash, &sha256);
  242. /* Insert enclave module to enclave module list */
  243. os_mutex_lock(&enclave_module_list_lock);
  244. enclave_module->next = enclave_module_list;
  245. enclave_module_list = enclave_module;
  246. os_mutex_unlock(&enclave_module_list_lock);
  247. #endif
  248. LOG_VERBOSE("Load module success.\n");
  249. }
  250. static void
  251. handle_cmd_unload_module(uint64 *args, uint32 argc)
  252. {
  253. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  254. bh_assert(argc == 1);
  255. if (!runtime_inited) {
  256. return;
  257. }
  258. #if WASM_ENABLE_LIB_RATS != 0
  259. /* Remove enclave module from enclave module list */
  260. os_mutex_lock(&enclave_module_list_lock);
  261. EnclaveModule *node_prev = NULL;
  262. EnclaveModule *node = enclave_module_list;
  263. while (node && node != enclave_module) {
  264. node_prev = node;
  265. node = node->next;
  266. }
  267. bh_assert(node == enclave_module);
  268. if (!node_prev)
  269. enclave_module_list = node->next;
  270. else
  271. node_prev->next = node->next;
  272. os_mutex_unlock(&enclave_module_list_lock);
  273. #endif
  274. /* Destroy enclave module resources */
  275. if (enclave_module->wasi_arg_buf)
  276. wasm_runtime_free(enclave_module->wasi_arg_buf);
  277. wasm_runtime_unload(enclave_module->module);
  278. if (!enclave_module->is_xip_file)
  279. wasm_runtime_free(enclave_module);
  280. else
  281. os_munmap(enclave_module, enclave_module->total_size_mapped);
  282. LOG_VERBOSE("Unload module success.\n");
  283. }
  284. #if WASM_ENABLE_LIB_RATS != 0
  285. char *
  286. wasm_runtime_get_module_hash(wasm_module_t module)
  287. {
  288. EnclaveModule *enclave_module;
  289. char *module_hash = NULL;
  290. os_mutex_lock(&enclave_module_list_lock);
  291. enclave_module = enclave_module_list;
  292. while (enclave_module) {
  293. if (enclave_module->module == module) {
  294. module_hash = enclave_module->module_hash;
  295. break;
  296. }
  297. enclave_module = enclave_module->next;
  298. }
  299. os_mutex_unlock(&enclave_module_list_lock);
  300. return module_hash;
  301. }
  302. #endif
  303. static void
  304. handle_cmd_instantiate_module(uint64 *args, uint32 argc)
  305. {
  306. uint64 *args_org = args;
  307. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  308. uint32 stack_size = *(uint32 *)args++;
  309. uint32 heap_size = *(uint32 *)args++;
  310. char *error_buf = *(char **)args++;
  311. uint32 error_buf_size = *(uint32 *)args++;
  312. wasm_module_inst_t module_inst;
  313. bh_assert(argc == 5);
  314. if (!runtime_inited) {
  315. *(void **)args_org = NULL;
  316. return;
  317. }
  318. if (!(module_inst =
  319. wasm_runtime_instantiate(enclave_module->module, stack_size,
  320. heap_size, error_buf, error_buf_size))) {
  321. *(void **)args_org = NULL;
  322. return;
  323. }
  324. *(wasm_module_inst_t *)args_org = module_inst;
  325. LOG_VERBOSE("Instantiate module success.\n");
  326. }
  327. static void
  328. handle_cmd_deinstantiate_module(uint64 *args, uint32 argc)
  329. {
  330. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  331. bh_assert(argc == 1);
  332. if (!runtime_inited) {
  333. return;
  334. }
  335. wasm_runtime_deinstantiate(module_inst);
  336. LOG_VERBOSE("Deinstantiate module success.\n");
  337. }
  338. static void
  339. handle_cmd_get_exception(uint64 *args, uint32 argc)
  340. {
  341. uint64 *args_org = args;
  342. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  343. char *exception = *(char **)args++;
  344. uint32 exception_size = *(uint32 *)args++;
  345. const char *exception1;
  346. bh_assert(argc == 3);
  347. if (!runtime_inited) {
  348. args_org[0] = false;
  349. return;
  350. }
  351. if ((exception1 = wasm_runtime_get_exception(module_inst))) {
  352. snprintf(exception, exception_size, "%s", exception1);
  353. args_org[0] = true;
  354. }
  355. else {
  356. args_org[0] = false;
  357. }
  358. }
  359. static void
  360. handle_cmd_exec_app_main(uint64 *args, int32 argc)
  361. {
  362. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  363. uint32 app_argc = *(uint32 *)args++;
  364. char **app_argv = NULL;
  365. uint64 total_size;
  366. int32 i;
  367. bh_assert(argc >= 3);
  368. bh_assert(app_argc >= 1);
  369. if (!runtime_inited) {
  370. return;
  371. }
  372. total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2);
  373. if (total_size >= UINT32_MAX
  374. || !(app_argv = (char **)wasm_runtime_malloc(total_size))) {
  375. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  376. return;
  377. }
  378. for (i = 0; i < app_argc; i++) {
  379. app_argv[i] = (char *)(uintptr_t)args[i];
  380. }
  381. wasm_application_execute_main(module_inst, app_argc - 1, app_argv + 1);
  382. wasm_runtime_free(app_argv);
  383. }
  384. static void
  385. handle_cmd_exec_app_func(uint64 *args, int32 argc)
  386. {
  387. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  388. char *func_name = *(char **)args++;
  389. uint32 app_argc = *(uint32 *)args++;
  390. char **app_argv = NULL;
  391. uint64 total_size;
  392. int32 i, func_name_len = strlen(func_name);
  393. bh_assert(argc == app_argc + 3);
  394. if (!runtime_inited) {
  395. return;
  396. }
  397. total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2);
  398. if (total_size >= UINT32_MAX
  399. || !(app_argv = (char **)wasm_runtime_malloc(total_size))) {
  400. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  401. return;
  402. }
  403. for (i = 0; i < app_argc; i++) {
  404. app_argv[i] = (char *)(uintptr_t)args[i];
  405. }
  406. wasm_application_execute_func(module_inst, func_name, app_argc, app_argv);
  407. wasm_runtime_free(app_argv);
  408. }
  409. static void
  410. handle_cmd_set_log_level(uint64 *args, uint32 argc)
  411. {
  412. #if WASM_ENABLE_LOG != 0
  413. LOG_VERBOSE("Set log verbose level to %d.\n", (int)args[0]);
  414. bh_log_set_verbose_level((int)args[0]);
  415. #endif
  416. }
  417. #ifndef SGX_DISABLE_WASI
  418. static void
  419. handle_cmd_set_wasi_args(uint64 *args, int32 argc)
  420. {
  421. uint64 *args_org = args;
  422. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  423. char **dir_list = *(char ***)args++;
  424. uint32 dir_list_size = *(uint32 *)args++;
  425. char **env_list = *(char ***)args++;
  426. uint32 env_list_size = *(uint32 *)args++;
  427. int stdinfd = *(int *)args++;
  428. int stdoutfd = *(int *)args++;
  429. int stderrfd = *(int *)args++;
  430. char **wasi_argv = *(char ***)args++;
  431. char *p, *p1;
  432. uint32 wasi_argc = *(uint32 *)args++;
  433. char **addr_pool_list = *(char ***)args++;
  434. uint32 addr_pool_list_size = *(uint32 *)args++;
  435. uint64 total_size = 0;
  436. int32 i, str_len;
  437. bh_assert(argc == 10);
  438. if (!runtime_inited) {
  439. *args_org = false;
  440. return;
  441. }
  442. total_size += sizeof(char *) * (uint64)dir_list_size
  443. + sizeof(char *) * (uint64)env_list_size
  444. + sizeof(char *) * (uint64)addr_pool_list_size
  445. + sizeof(char *) * (uint64)wasi_argc;
  446. for (i = 0; i < dir_list_size; i++) {
  447. total_size += strlen(dir_list[i]) + 1;
  448. }
  449. for (i = 0; i < env_list_size; i++) {
  450. total_size += strlen(env_list[i]) + 1;
  451. }
  452. for (i = 0; i < addr_pool_list_size; i++) {
  453. total_size += strlen(addr_pool_list[i]) + 1;
  454. }
  455. for (i = 0; i < wasi_argc; i++) {
  456. total_size += strlen(wasi_argv[i]) + 1;
  457. }
  458. if (total_size >= UINT32_MAX
  459. || !(enclave_module->wasi_arg_buf = p =
  460. (char *)wasm_runtime_malloc((uint32)total_size))) {
  461. *args_org = false;
  462. return;
  463. }
  464. p1 = p + sizeof(char *) * dir_list_size + sizeof(char *) * env_list_size
  465. + sizeof(char *) * addr_pool_list_size + sizeof(char *) * wasi_argc;
  466. if (dir_list_size > 0) {
  467. enclave_module->wasi_dir_list = (char **)p;
  468. enclave_module->wasi_dir_list_size = dir_list_size;
  469. for (i = 0; i < dir_list_size; i++) {
  470. enclave_module->wasi_dir_list[i] = p1;
  471. str_len = strlen(dir_list[i]);
  472. bh_memcpy_s(p1, str_len + 1, dir_list[i], str_len + 1);
  473. p1 += str_len + 1;
  474. }
  475. p += sizeof(char *) * dir_list_size;
  476. }
  477. if (env_list_size > 0) {
  478. enclave_module->wasi_env_list = (char **)p;
  479. enclave_module->wasi_env_list_size = env_list_size;
  480. for (i = 0; i < env_list_size; i++) {
  481. enclave_module->wasi_env_list[i] = p1;
  482. str_len = strlen(env_list[i]);
  483. bh_memcpy_s(p1, str_len + 1, env_list[i], str_len + 1);
  484. p1 += str_len + 1;
  485. }
  486. p += sizeof(char *) * env_list_size;
  487. }
  488. if (addr_pool_list_size > 0) {
  489. enclave_module->wasi_addr_pool_list = (char **)p;
  490. enclave_module->wasi_addr_pool_list_size = addr_pool_list_size;
  491. for (i = 0; i < addr_pool_list_size; i++) {
  492. enclave_module->wasi_addr_pool_list[i] = p1;
  493. str_len = strlen(addr_pool_list[i]);
  494. bh_memcpy_s(p1, str_len + 1, addr_pool_list[i], str_len + 1);
  495. p1 += str_len + 1;
  496. }
  497. p += sizeof(char *) * addr_pool_list_size;
  498. }
  499. if (wasi_argc > 0) {
  500. enclave_module->wasi_argv = (char **)p;
  501. enclave_module->wasi_argc = wasi_argc;
  502. for (i = 0; i < wasi_argc; i++) {
  503. enclave_module->wasi_argv[i] = p1;
  504. str_len = strlen(wasi_argv[i]);
  505. bh_memcpy_s(p1, str_len + 1, wasi_argv[i], str_len + 1);
  506. p1 += str_len + 1;
  507. }
  508. p += sizeof(char *) * wasi_argc;
  509. }
  510. wasm_runtime_set_wasi_args_ex(
  511. enclave_module->module, (const char **)enclave_module->wasi_dir_list,
  512. dir_list_size, NULL, 0, (const char **)enclave_module->wasi_env_list,
  513. env_list_size, enclave_module->wasi_argv, enclave_module->wasi_argc,
  514. (stdinfd != -1) ? stdinfd : 0, (stdoutfd != -1) ? stdoutfd : 1,
  515. (stderrfd != -1) ? stderrfd : 2);
  516. wasm_runtime_set_wasi_addr_pool(
  517. enclave_module->module,
  518. (const char **)enclave_module->wasi_addr_pool_list,
  519. addr_pool_list_size);
  520. *args_org = true;
  521. }
  522. #else
  523. static void
  524. handle_cmd_set_wasi_args(uint64 *args, int32 argc)
  525. {
  526. *args = true;
  527. }
  528. #endif /* end of SGX_DISABLE_WASI */
  529. static void
  530. handle_cmd_get_version(uint64 *args, uint32 argc)
  531. {
  532. uint32 major, minor, patch;
  533. bh_assert(argc == 3);
  534. wasm_runtime_get_version(&major, &minor, &patch);
  535. args[0] = major;
  536. args[1] = minor;
  537. args[2] = patch;
  538. }
  539. #if WASM_ENABLE_STATIC_PGO != 0
  540. static void
  541. handle_cmd_get_pgo_prof_buf_size(uint64 *args, int32 argc)
  542. {
  543. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args;
  544. uint32 buf_len;
  545. bh_assert(argc == 1);
  546. if (!runtime_inited) {
  547. args[0] = 0;
  548. return;
  549. }
  550. buf_len = wasm_runtime_get_pgo_prof_data_size(module_inst);
  551. args[0] = buf_len;
  552. }
  553. static void
  554. handle_cmd_get_pro_prof_buf_data(uint64 *args, int32 argc)
  555. {
  556. uint64 *args_org = args;
  557. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  558. char *buf = *(char **)args++;
  559. uint32 len = *(uint32 *)args++;
  560. uint32 bytes_dumped;
  561. bh_assert(argc == 3);
  562. if (!runtime_inited) {
  563. args_org[0] = 0;
  564. return;
  565. }
  566. bytes_dumped =
  567. wasm_runtime_dump_pgo_prof_data_to_buf(module_inst, buf, len);
  568. args_org[0] = bytes_dumped;
  569. }
  570. #endif
  571. void
  572. ecall_handle_command(unsigned cmd, unsigned char *cmd_buf,
  573. unsigned cmd_buf_size)
  574. {
  575. uint64 *args = (uint64 *)cmd_buf;
  576. uint32 argc = cmd_buf_size / sizeof(uint64);
  577. switch (cmd) {
  578. case CMD_INIT_RUNTIME:
  579. handle_cmd_init_runtime(args, argc);
  580. break;
  581. case CMD_LOAD_MODULE:
  582. handle_cmd_load_module(args, argc);
  583. break;
  584. case CMD_SET_WASI_ARGS:
  585. handle_cmd_set_wasi_args(args, argc);
  586. break;
  587. case CMD_INSTANTIATE_MODULE:
  588. handle_cmd_instantiate_module(args, argc);
  589. break;
  590. case CMD_LOOKUP_FUNCTION:
  591. break;
  592. case CMD_CREATE_EXEC_ENV:
  593. break;
  594. case CMD_CALL_WASM:
  595. break;
  596. case CMD_EXEC_APP_FUNC:
  597. handle_cmd_exec_app_func(args, argc);
  598. break;
  599. case CMD_EXEC_APP_MAIN:
  600. handle_cmd_exec_app_main(args, argc);
  601. break;
  602. case CMD_GET_EXCEPTION:
  603. handle_cmd_get_exception(args, argc);
  604. break;
  605. case CMD_DEINSTANTIATE_MODULE:
  606. handle_cmd_deinstantiate_module(args, argc);
  607. break;
  608. case CMD_UNLOAD_MODULE:
  609. handle_cmd_unload_module(args, argc);
  610. break;
  611. case CMD_DESTROY_RUNTIME:
  612. handle_cmd_destroy_runtime();
  613. break;
  614. case CMD_SET_LOG_LEVEL:
  615. handle_cmd_set_log_level(args, argc);
  616. break;
  617. case CMD_GET_VERSION:
  618. handle_cmd_get_version(args, argc);
  619. break;
  620. #if WASM_ENABLE_STATIC_PGO != 0
  621. case CMD_GET_PGO_PROF_BUF_SIZE:
  622. handle_cmd_get_pgo_prof_buf_size(args, argc);
  623. break;
  624. case CMD_DUMP_PGO_PROF_BUF_DATA:
  625. handle_cmd_get_pro_prof_buf_data(args, argc);
  626. break;
  627. #endif
  628. default:
  629. LOG_ERROR("Unknown command %d\n", cmd);
  630. break;
  631. }
  632. }
  633. void
  634. ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
  635. {
  636. wasm_module_t wasm_module = NULL;
  637. wasm_module_inst_t wasm_module_inst = NULL;
  638. RuntimeInitArgs init_args;
  639. char error_buf[128];
  640. const char *exception;
  641. /* avoid duplicated init */
  642. if (runtime_inited) {
  643. return;
  644. }
  645. os_set_print_function(enclave_print);
  646. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  647. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  648. init_args.mem_alloc_type = Alloc_With_Pool;
  649. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  650. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  651. #else
  652. init_args.mem_alloc_type = Alloc_With_System_Allocator;
  653. #endif
  654. /* initialize runtime environment */
  655. if (!wasm_runtime_full_init(&init_args)) {
  656. enclave_print("Init runtime environment failed.");
  657. enclave_print("\n");
  658. return;
  659. }
  660. /* load WASM module */
  661. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  662. error_buf, sizeof(error_buf)))) {
  663. enclave_print(error_buf);
  664. enclave_print("\n");
  665. goto fail1;
  666. }
  667. /* instantiate the module */
  668. if (!(wasm_module_inst =
  669. wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024,
  670. error_buf, sizeof(error_buf)))) {
  671. enclave_print(error_buf);
  672. enclave_print("\n");
  673. goto fail2;
  674. }
  675. /* execute the main function of wasm app */
  676. wasm_application_execute_main(wasm_module_inst, 0, NULL);
  677. if ((exception = wasm_runtime_get_exception(wasm_module_inst))) {
  678. enclave_print(exception);
  679. enclave_print("\n");
  680. }
  681. /* destroy the module instance */
  682. wasm_runtime_deinstantiate(wasm_module_inst);
  683. fail2:
  684. /* unload the module */
  685. wasm_runtime_unload(wasm_module);
  686. fail1:
  687. /* destroy runtime environment */
  688. wasm_runtime_destroy();
  689. }