Enclave.cpp 22 KB

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