Enclave.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. extern "C" {
  13. typedef void (*os_print_function_t)(const char *message);
  14. extern void
  15. os_set_print_function(os_print_function_t pf);
  16. void
  17. enclave_print(const char *message)
  18. {
  19. ocall_print(message);
  20. }
  21. }
  22. typedef enum EcallCmd {
  23. CMD_INIT_RUNTIME = 0, /* wasm_runtime_init/full_init() */
  24. CMD_LOAD_MODULE, /* wasm_runtime_load() */
  25. CMD_INSTANTIATE_MODULE, /* wasm_runtime_instantiate() */
  26. CMD_LOOKUP_FUNCTION, /* wasm_runtime_lookup_function() */
  27. CMD_CREATE_EXEC_ENV, /* wasm_runtime_create_exec_env() */
  28. CMD_CALL_WASM, /* wasm_runtime_call_wasm */
  29. CMD_EXEC_APP_FUNC, /* wasm_application_execute_func() */
  30. CMD_EXEC_APP_MAIN, /* wasm_application_execute_main() */
  31. CMD_GET_EXCEPTION, /* wasm_runtime_get_exception() */
  32. CMD_DEINSTANTIATE_MODULE, /* wasm_runtime_deinstantiate() */
  33. CMD_UNLOAD_MODULE, /* wasm_runtime_unload() */
  34. CMD_DESTROY_RUNTIME, /* wasm_runtime_destroy() */
  35. CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */
  36. CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */
  37. } EcallCmd;
  38. typedef struct EnclaveModule {
  39. wasm_module_t module;
  40. uint8 *wasm_file;
  41. uint32 wasm_file_size;
  42. char *wasi_arg_buf;
  43. char **wasi_dir_list;
  44. uint32 wasi_dir_list_size;
  45. char **wasi_env_list;
  46. uint32 wasi_env_list_size;
  47. char **wasi_argv;
  48. uint32 wasi_argc;
  49. } EnclaveModule;
  50. #if WASM_ENABLE_SPEC_TEST == 0
  51. static char global_heap_buf[10 * 1024 * 1024] = { 0 };
  52. #else
  53. static char global_heap_buf[100 * 1024 * 1024] = { 0 };
  54. #endif
  55. static void
  56. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  57. {
  58. if (error_buf != NULL)
  59. snprintf(error_buf, error_buf_size, "%s", string);
  60. }
  61. static void
  62. handle_cmd_init_runtime(uint64 *args, uint32 argc)
  63. {
  64. bool alloc_with_pool;
  65. uint32 max_thread_num;
  66. RuntimeInitArgs init_args;
  67. bh_assert(argc == 2);
  68. os_set_print_function(enclave_print);
  69. #if WASM_ENABLE_SPEC_TEST == 0
  70. alloc_with_pool = (bool)args[0];
  71. #else
  72. alloc_with_pool = true;
  73. #endif
  74. max_thread_num = (uint32)args[1];
  75. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  76. init_args.max_thread_num = max_thread_num;
  77. if (alloc_with_pool) {
  78. init_args.mem_alloc_type = Alloc_With_Pool;
  79. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  80. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  81. }
  82. else {
  83. init_args.mem_alloc_type = Alloc_With_System_Allocator;
  84. }
  85. /* initialize runtime environment */
  86. if (!wasm_runtime_full_init(&init_args)) {
  87. LOG_ERROR("Init runtime environment failed.\n");
  88. args[0] = false;
  89. return;
  90. }
  91. args[0] = true;
  92. LOG_VERBOSE("Init runtime environment success.\n");
  93. }
  94. static void
  95. handle_cmd_destroy_runtime()
  96. {
  97. wasm_runtime_destroy();
  98. LOG_VERBOSE("Destroy runtime success.\n");
  99. }
  100. static void
  101. handle_cmd_load_module(uint64 *args, uint32 argc)
  102. {
  103. uint64 *args_org = args;
  104. char *wasm_file = *(char **)args++;
  105. uint32 wasm_file_size = *(uint32 *)args++;
  106. char *error_buf = *(char **)args++;
  107. uint32 error_buf_size = *(uint32 *)args++;
  108. uint64 total_size = sizeof(EnclaveModule) + (uint64)wasm_file_size;
  109. EnclaveModule *enclave_module;
  110. bh_assert(argc == 4);
  111. if (total_size >= UINT32_MAX
  112. || !(enclave_module =
  113. (EnclaveModule *)wasm_runtime_malloc((uint32)total_size))) {
  114. set_error_buf(error_buf, error_buf_size,
  115. "WASM module load failed: "
  116. "allocate memory failed.");
  117. *(void **)args_org = NULL;
  118. return;
  119. }
  120. memset(enclave_module, 0, (uint32)total_size);
  121. enclave_module->wasm_file = (uint8 *)enclave_module + sizeof(EnclaveModule);
  122. bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file,
  123. wasm_file_size);
  124. if (!(enclave_module->module =
  125. wasm_runtime_load(enclave_module->wasm_file, wasm_file_size,
  126. error_buf, error_buf_size))) {
  127. wasm_runtime_free(enclave_module);
  128. *(void **)args_org = NULL;
  129. return;
  130. }
  131. *(EnclaveModule **)args_org = enclave_module;
  132. LOG_VERBOSE("Load module success.\n");
  133. }
  134. static void
  135. handle_cmd_unload_module(uint64 *args, uint32 argc)
  136. {
  137. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  138. uint32 i;
  139. bh_assert(argc == 1);
  140. if (enclave_module->wasi_arg_buf)
  141. wasm_runtime_free(enclave_module->wasi_arg_buf);
  142. wasm_runtime_unload(enclave_module->module);
  143. wasm_runtime_free(enclave_module);
  144. LOG_VERBOSE("Unload module success.\n");
  145. }
  146. static void
  147. handle_cmd_instantiate_module(uint64 *args, uint32 argc)
  148. {
  149. uint64 *args_org = args;
  150. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  151. uint32 stack_size = *(uint32 *)args++;
  152. uint32 heap_size = *(uint32 *)args++;
  153. char *error_buf = *(char **)args++;
  154. uint32 error_buf_size = *(uint32 *)args++;
  155. wasm_module_inst_t module_inst;
  156. bh_assert(argc == 5);
  157. if (!(module_inst =
  158. wasm_runtime_instantiate(enclave_module->module, stack_size,
  159. heap_size, error_buf, error_buf_size))) {
  160. *(void **)args_org = NULL;
  161. return;
  162. }
  163. *(wasm_module_inst_t *)args_org = module_inst;
  164. LOG_VERBOSE("Instantiate module success.\n");
  165. }
  166. static void
  167. handle_cmd_deinstantiate_module(uint64 *args, uint32 argc)
  168. {
  169. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  170. bh_assert(argc == 1);
  171. wasm_runtime_deinstantiate(module_inst);
  172. LOG_VERBOSE("Deinstantiate module success.\n");
  173. }
  174. static void
  175. handle_cmd_get_exception(uint64 *args, uint32 argc)
  176. {
  177. uint64 *args_org = args;
  178. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  179. char *exception = *(char **)args++;
  180. uint32 exception_size = *(uint32 *)args++;
  181. const char *exception1;
  182. bh_assert(argc == 3);
  183. if ((exception1 = wasm_runtime_get_exception(module_inst))) {
  184. snprintf(exception, exception_size, "%s", exception1);
  185. args_org[0] = true;
  186. }
  187. else {
  188. args_org[0] = false;
  189. }
  190. }
  191. static void
  192. handle_cmd_exec_app_main(uint64 *args, int32 argc)
  193. {
  194. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  195. uint32 app_argc = *(uint32 *)args++;
  196. char **app_argv = NULL;
  197. uint64 total_size;
  198. int32 i;
  199. bh_assert(argc >= 3);
  200. bh_assert(app_argc >= 1);
  201. total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2);
  202. if (total_size >= UINT32_MAX
  203. || !(app_argv = (char **)wasm_runtime_malloc(total_size))) {
  204. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  205. return;
  206. }
  207. for (i = 0; i < app_argc; i++) {
  208. app_argv[i] = (char *)(uintptr_t)args[i];
  209. }
  210. wasm_application_execute_main(module_inst, app_argc - 1, app_argv + 1);
  211. wasm_runtime_free(app_argv);
  212. }
  213. static void
  214. handle_cmd_exec_app_func(uint64 *args, int32 argc)
  215. {
  216. wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
  217. char *func_name = *(char **)args++;
  218. uint32 app_argc = *(uint32 *)args++;
  219. char **app_argv = NULL;
  220. uint64 total_size;
  221. int32 i, func_name_len = strlen(func_name);
  222. bh_assert(argc == app_argc + 3);
  223. total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2);
  224. if (total_size >= UINT32_MAX
  225. || !(app_argv = (char **)wasm_runtime_malloc(total_size))) {
  226. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  227. return;
  228. }
  229. for (i = 0; i < app_argc; i++) {
  230. app_argv[i] = (char *)(uintptr_t)args[i];
  231. }
  232. wasm_application_execute_func(module_inst, func_name, app_argc, app_argv);
  233. wasm_runtime_free(app_argv);
  234. }
  235. static void
  236. handle_cmd_set_log_level(uint64 *args, uint32 argc)
  237. {
  238. #if WASM_ENABLE_LOG != 0
  239. LOG_VERBOSE("Set log verbose level to %d.\n", (int)args[0]);
  240. bh_log_set_verbose_level((int)args[0]);
  241. #endif
  242. }
  243. #ifndef SGX_DISABLE_WASI
  244. static void
  245. handle_cmd_set_wasi_args(uint64 *args, int32 argc)
  246. {
  247. uint64 *args_org = args;
  248. EnclaveModule *enclave_module = *(EnclaveModule **)args++;
  249. char **dir_list = *(char ***)args++;
  250. uint32 dir_list_size = *(uint32 *)args++;
  251. char **env_list = *(char ***)args++;
  252. uint32 env_list_size = *(uint32 *)args++;
  253. int stdinfd = *(int *)args++;
  254. int stdoutfd = *(int *)args++;
  255. int stderrfd = *(int *)args++;
  256. char **wasi_argv = *(char ***)args++;
  257. char *p, *p1;
  258. uint32 wasi_argc = *(uint32 *)args++;
  259. uint64 total_size = 0;
  260. int32 i, str_len;
  261. bh_assert(argc == 10);
  262. total_size += sizeof(char *) * (uint64)dir_list_size
  263. + sizeof(char *) * (uint64)env_list_size
  264. + sizeof(char *) * (uint64)wasi_argc;
  265. for (i = 0; i < dir_list_size; i++) {
  266. total_size += strlen(dir_list[i]) + 1;
  267. }
  268. for (i = 0; i < env_list_size; i++) {
  269. total_size += strlen(env_list[i]) + 1;
  270. }
  271. for (i = 0; i < wasi_argc; i++) {
  272. total_size += strlen(wasi_argv[i]) + 1;
  273. }
  274. if (total_size >= UINT32_MAX
  275. || !(enclave_module->wasi_arg_buf = p =
  276. (char *)wasm_runtime_malloc((uint32)total_size))) {
  277. *args_org = false;
  278. return;
  279. }
  280. p1 = p + sizeof(char *) * dir_list_size + sizeof(char *) * env_list_size
  281. + sizeof(char *) * wasi_argc;
  282. if (dir_list_size > 0) {
  283. enclave_module->wasi_dir_list = (char **)p;
  284. enclave_module->wasi_dir_list_size = dir_list_size;
  285. for (i = 0; i < dir_list_size; i++) {
  286. enclave_module->wasi_dir_list[i] = p1;
  287. str_len = strlen(dir_list[i]);
  288. bh_memcpy_s(p1, str_len + 1, dir_list[i], str_len + 1);
  289. p1 += str_len + 1;
  290. }
  291. p += sizeof(char *) * dir_list_size;
  292. }
  293. if (env_list_size > 0) {
  294. enclave_module->wasi_env_list = (char **)p;
  295. enclave_module->wasi_env_list_size = env_list_size;
  296. for (i = 0; i < env_list_size; i++) {
  297. enclave_module->wasi_env_list[i] = p1;
  298. str_len = strlen(env_list[i]);
  299. bh_memcpy_s(p1, str_len + 1, env_list[i], str_len + 1);
  300. p1 += str_len + 1;
  301. }
  302. p += sizeof(char *) * env_list_size;
  303. }
  304. if (wasi_argc > 0) {
  305. enclave_module->wasi_argv = (char **)p;
  306. enclave_module->wasi_argc = wasi_argc;
  307. for (i = 0; i < wasi_argc; i++) {
  308. enclave_module->wasi_argv[i] = p1;
  309. str_len = strlen(wasi_argv[i]);
  310. bh_memcpy_s(p1, str_len + 1, wasi_argv[i], str_len + 1);
  311. p1 += str_len + 1;
  312. }
  313. p += sizeof(char *) * wasi_argc;
  314. }
  315. wasm_runtime_set_wasi_args_ex(
  316. enclave_module->module, (const char **)enclave_module->wasi_dir_list,
  317. dir_list_size, NULL, 0, (const char **)enclave_module->wasi_env_list,
  318. env_list_size, enclave_module->wasi_argv, enclave_module->wasi_argc,
  319. (stdinfd != -1) ? stdinfd : 0, (stdoutfd != -1) ? stdoutfd : 1,
  320. (stderrfd != -1) ? stderrfd : 2);
  321. *args_org = true;
  322. }
  323. #else
  324. static void
  325. handle_cmd_set_wasi_args(uint64 *args, int32 argc)
  326. {
  327. *args = true;
  328. }
  329. #endif /* end of SGX_DISABLE_WASI */
  330. void
  331. ecall_handle_command(unsigned cmd, unsigned char *cmd_buf,
  332. unsigned cmd_buf_size)
  333. {
  334. uint64 *args = (uint64 *)cmd_buf;
  335. uint32 argc = cmd_buf_size / sizeof(uint64);
  336. switch (cmd) {
  337. case CMD_INIT_RUNTIME:
  338. handle_cmd_init_runtime(args, argc);
  339. break;
  340. case CMD_LOAD_MODULE:
  341. handle_cmd_load_module(args, argc);
  342. break;
  343. case CMD_SET_WASI_ARGS:
  344. handle_cmd_set_wasi_args(args, argc);
  345. break;
  346. case CMD_INSTANTIATE_MODULE:
  347. handle_cmd_instantiate_module(args, argc);
  348. break;
  349. case CMD_LOOKUP_FUNCTION:
  350. break;
  351. case CMD_CREATE_EXEC_ENV:
  352. break;
  353. case CMD_CALL_WASM:
  354. break;
  355. case CMD_EXEC_APP_FUNC:
  356. handle_cmd_exec_app_func(args, argc);
  357. break;
  358. case CMD_EXEC_APP_MAIN:
  359. handle_cmd_exec_app_main(args, argc);
  360. break;
  361. case CMD_GET_EXCEPTION:
  362. handle_cmd_get_exception(args, argc);
  363. break;
  364. case CMD_DEINSTANTIATE_MODULE:
  365. handle_cmd_deinstantiate_module(args, argc);
  366. break;
  367. case CMD_UNLOAD_MODULE:
  368. handle_cmd_unload_module(args, argc);
  369. break;
  370. case CMD_DESTROY_RUNTIME:
  371. handle_cmd_destroy_runtime();
  372. break;
  373. case CMD_SET_LOG_LEVEL:
  374. handle_cmd_set_log_level(args, argc);
  375. break;
  376. default:
  377. LOG_ERROR("Unknown command %d\n", cmd);
  378. break;
  379. }
  380. }
  381. void
  382. ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
  383. {
  384. wasm_module_t wasm_module = NULL;
  385. wasm_module_inst_t wasm_module_inst = NULL;
  386. RuntimeInitArgs init_args;
  387. char error_buf[128];
  388. const char *exception;
  389. os_set_print_function(enclave_print);
  390. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  391. init_args.mem_alloc_type = Alloc_With_Pool;
  392. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  393. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  394. /* initialize runtime environment */
  395. if (!wasm_runtime_full_init(&init_args)) {
  396. ocall_print("Init runtime environment failed.");
  397. ocall_print("\n");
  398. return;
  399. }
  400. /* load WASM module */
  401. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  402. error_buf, sizeof(error_buf)))) {
  403. ocall_print(error_buf);
  404. ocall_print("\n");
  405. goto fail1;
  406. }
  407. /* instantiate the module */
  408. if (!(wasm_module_inst =
  409. wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024,
  410. error_buf, sizeof(error_buf)))) {
  411. ocall_print(error_buf);
  412. ocall_print("\n");
  413. goto fail2;
  414. }
  415. /* execute the main function of wasm app */
  416. wasm_application_execute_main(wasm_module_inst, 0, NULL);
  417. if ((exception = wasm_runtime_get_exception(wasm_module_inst))) {
  418. ocall_print(exception);
  419. ocall_print("\n");
  420. }
  421. /* destroy the module instance */
  422. wasm_runtime_deinstantiate(wasm_module_inst);
  423. fail2:
  424. /* unload the module */
  425. wasm_runtime_unload(wasm_module);
  426. fail1:
  427. /* destroy runtime environment */
  428. wasm_runtime_destroy();
  429. }