Enclave.cpp 15 KB

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