App.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 <stdlib.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <pwd.h>
  11. #include <assert.h>
  12. #include <iostream>
  13. #include <cstdio>
  14. #include <cstring>
  15. #include "Enclave_u.h"
  16. #include "sgx_urts.h"
  17. #ifndef TRUE
  18. #define TRUE 1
  19. #endif
  20. #ifndef FALSE
  21. #define FALSE 0
  22. #endif
  23. #define TOKEN_FILENAME "enclave.token"
  24. #define ENCLAVE_FILENAME "enclave.signed.so"
  25. #define MAX_PATH 1024
  26. #define TEST_OCALL_API 0
  27. static sgx_enclave_id_t g_eid = 0;
  28. void
  29. ocall_print(const char* str)
  30. {
  31. printf("%s", str);
  32. }
  33. static char *
  34. get_exe_path(char *path_buf, unsigned path_buf_size)
  35. {
  36. ssize_t i;
  37. ssize_t size = readlink("/proc/self/exe",
  38. path_buf, path_buf_size - 1);
  39. if (size < 0 || (size >= path_buf_size - 1)) {
  40. return NULL;
  41. }
  42. path_buf[size] = '\0';
  43. for (i = size - 1; i >= 0; i--) {
  44. if (path_buf[i] == '/') {
  45. path_buf[i + 1] = '\0';
  46. break;
  47. }
  48. }
  49. return path_buf;
  50. }
  51. /* Initialize the enclave:
  52. * Step 1: try to retrieve the launch token saved by last transaction
  53. * Step 2: call sgx_create_enclave to initialize an enclave instance
  54. * Step 3: save the launch token if it is updated
  55. */
  56. static int
  57. enclave_init(sgx_enclave_id_t *p_eid)
  58. {
  59. char token_path[MAX_PATH] = { '\0' };
  60. char enclave_path[MAX_PATH] = { '\0' };
  61. const char *home_dir;
  62. sgx_launch_token_t token = { 0 };
  63. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  64. int updated = 0;
  65. size_t write_num, enc_file_len;
  66. FILE *fp;
  67. enc_file_len = strlen(ENCLAVE_FILENAME);
  68. if (!get_exe_path(enclave_path, sizeof(enclave_path) - enc_file_len)) {
  69. printf("Failed to get exec path\n");
  70. return -1;
  71. }
  72. memcpy(enclave_path + strlen(enclave_path), ENCLAVE_FILENAME, enc_file_len);
  73. /* Step 1: try to retrieve the launch token saved by last transaction
  74. * if there is no token, then create a new one.
  75. */
  76. /* try to get the token saved in $HOME */
  77. home_dir = getpwuid(getuid())->pw_dir;
  78. if (home_dir != NULL &&
  79. (strlen(home_dir) + strlen("/") + sizeof(TOKEN_FILENAME) + 1) <= MAX_PATH) {
  80. /* compose the token path */
  81. strncpy(token_path, home_dir, strlen(home_dir));
  82. strncat(token_path, "/", strlen("/"));
  83. strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME) + 1);
  84. }
  85. else {
  86. /* if token path is too long or $HOME is NULL */
  87. strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
  88. }
  89. fp = fopen(token_path, "rb");
  90. if (fp == NULL && (fp = fopen(token_path, "wb")) == NULL) {
  91. printf("Warning: Failed to create/open the launch token file \"%s\".\n",
  92. token_path);
  93. }
  94. if (fp != NULL) {
  95. /* read the token from saved file */
  96. size_t read_num = fread(token, 1, sizeof(sgx_launch_token_t), fp);
  97. if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) {
  98. /* if token is invalid, clear the buffer */
  99. memset(&token, 0x0, sizeof(sgx_launch_token_t));
  100. printf("Warning: Invalid launch token read from \"%s\".\n", token_path);
  101. }
  102. }
  103. /* Step 2: call sgx_create_enclave to initialize an enclave instance */
  104. /* Debug Support: set 2nd parameter to 1 */
  105. ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG,
  106. &token, &updated, p_eid, NULL);
  107. if (ret != SGX_SUCCESS)
  108. /* Try to load enclave.sign.so from the path of exe file */
  109. ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG,
  110. &token, &updated, p_eid, NULL);
  111. if (ret != SGX_SUCCESS) {
  112. printf("Failed to create enclave from %s, error code: %d\n",
  113. ENCLAVE_FILENAME, ret);
  114. if (fp != NULL)
  115. fclose(fp);
  116. return -1;
  117. }
  118. /* Step 3: save the launch token if it is updated */
  119. if (updated == FALSE || fp == NULL) {
  120. /* if the token is not updated, or file handler is invalid, do not perform saving */
  121. if (fp != NULL) fclose(fp);
  122. return 0;
  123. }
  124. /* reopen the file with write capablity */
  125. fp = freopen(token_path, "wb", fp);
  126. if (fp == NULL)
  127. return 0;
  128. write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
  129. if (write_num != sizeof(sgx_launch_token_t))
  130. printf("Warning: Failed to save launch token to \"%s\".\n", token_path);
  131. fclose(fp);
  132. return 0;
  133. }
  134. static unsigned char *
  135. read_file_to_buffer(const char *filename, uint32_t *ret_size)
  136. {
  137. unsigned char *buffer;
  138. FILE *file;
  139. int file_size, read_size;
  140. if (!filename || !ret_size) {
  141. printf("Read file to buffer failed: invalid filename or ret size.\n");
  142. return NULL;
  143. }
  144. if (!(file = fopen(filename, "r"))) {
  145. printf("Read file to buffer failed: open file %s failed.\n",
  146. filename);
  147. return NULL;
  148. }
  149. fseek(file, 0, SEEK_END);
  150. file_size = ftell(file);
  151. fseek(file, 0, SEEK_SET);
  152. if (!(buffer = (unsigned char*)malloc(file_size))) {
  153. printf("Read file to buffer failed: alloc memory failed.\n");
  154. fclose(file);
  155. return NULL;
  156. }
  157. read_size = fread(buffer, 1, file_size, file);
  158. fclose(file);
  159. if (read_size < file_size) {
  160. printf("Read file to buffer failed: read file content failed.\n");
  161. free(buffer);
  162. return NULL;
  163. }
  164. *ret_size = file_size;
  165. return buffer;
  166. }
  167. static int
  168. print_help()
  169. {
  170. printf("Usage: iwasm [-options] wasm_file [args...]\n");
  171. printf("options:\n");
  172. printf(" -f|--function name Specify a function name of the module to run rather\n"
  173. " than main\n");
  174. printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n"
  175. " level with more log\n");
  176. printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n");
  177. printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
  178. printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
  179. " that runs commands in the form of `FUNC ARG...`\n");
  180. printf(" --env=<env> Pass wasi environment variables with \"key=value\"\n");
  181. printf(" to the program, for example:\n");
  182. printf(" --env=\"key1=value1\" --env=\"key2=value2\"\n");
  183. printf(" --dir=<dir> Grant wasi access to the given host directories\n");
  184. printf(" to the program, for example:\n");
  185. printf(" --dir=<dir1> --dir=<dir2>\n");
  186. printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
  187. return 1;
  188. }
  189. /**
  190. * Split a space separated strings into an array of strings
  191. * Returns NULL on failure
  192. * Memory must be freed by caller
  193. * Based on: http://stackoverflow.com/a/11198630/471795
  194. */
  195. static char **
  196. split_string(char *str, int *count)
  197. {
  198. char **res = NULL;
  199. char *p;
  200. int idx = 0;
  201. /* split string and append tokens to 'res' */
  202. do {
  203. p = strtok(str, " ");
  204. str = NULL;
  205. res = (char **)realloc(res, sizeof(char *) * (unsigned)(idx + 1));
  206. if (res == NULL) {
  207. return NULL;
  208. }
  209. res[idx++] = p;
  210. } while (p);
  211. /**
  212. * since the function name,
  213. * res[0] might be contains a '\' to indicate a space
  214. * func\name -> func name
  215. */
  216. p = strchr(res[0], '\\');
  217. while (p) {
  218. *p = ' ';
  219. p = strchr(p, '\\');
  220. }
  221. if (count) {
  222. *count = idx - 1;
  223. }
  224. return res;
  225. }
  226. typedef enum EcallCmd {
  227. CMD_INIT_RUNTIME = 0, /* wasm_runtime_init/full_init() */
  228. CMD_LOAD_MODULE, /* wasm_runtime_load() */
  229. CMD_INSTANTIATE_MODULE, /* wasm_runtime_instantiate() */
  230. CMD_LOOKUP_FUNCTION, /* wasm_runtime_lookup_function() */
  231. CMD_CREATE_EXEC_ENV, /* wasm_runtime_create_exec_env() */
  232. CMD_CALL_WASM, /* wasm_runtime_call_wasm */
  233. CMD_EXEC_APP_FUNC, /* wasm_application_execute_func() */
  234. CMD_EXEC_APP_MAIN, /* wasm_application_execute_main() */
  235. CMD_GET_EXCEPTION, /* wasm_runtime_get_exception() */
  236. CMD_DEINSTANTIATE_MODULE, /* wasm_runtime_deinstantiate() */
  237. CMD_UNLOAD_MODULE, /* wasm_runtime_unload() */
  238. CMD_DESTROY_RUNTIME, /* wasm_runtime_destroy() */
  239. CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */
  240. CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */
  241. } EcallCmd;
  242. static void
  243. app_instance_func(void *wasm_module_inst, const char *func_name,
  244. int app_argc, char **app_argv);
  245. static void *
  246. app_instance_repl(void *module_inst, int app_argc, char **app_argv)
  247. {
  248. char *cmd = NULL;
  249. size_t len = 0;
  250. ssize_t n;
  251. while ((printf("webassembly> "), n = getline(&cmd, &len, stdin)) != -1) {
  252. assert(n > 0);
  253. if (cmd[n - 1] == '\n') {
  254. if (n == 1)
  255. continue;
  256. else
  257. cmd[n - 1] = '\0';
  258. }
  259. if (!strcmp(cmd, "__exit__")) {
  260. printf("exit repl mode\n");
  261. break;
  262. }
  263. app_argv = split_string(cmd, &app_argc);
  264. if (app_argv == NULL) {
  265. printf("Wasm prepare param failed: split string failed.\n");
  266. break;
  267. }
  268. if (app_argc != 0) {
  269. app_instance_func(module_inst, app_argv[0],
  270. app_argc - 1, app_argv + 1);
  271. }
  272. free(app_argv);
  273. }
  274. free(cmd);
  275. return NULL;
  276. }
  277. static bool
  278. validate_env_str(char *env)
  279. {
  280. char *p = env;
  281. int key_len = 0;
  282. while (*p != '\0' && *p != '=') {
  283. key_len++;
  284. p++;
  285. }
  286. if (*p != '=' || key_len == 0)
  287. return false;
  288. return true;
  289. }
  290. static bool
  291. set_log_verbose_level(int log_verbose_level)
  292. {
  293. uint64_t ecall_args[1];
  294. /* Set log verbose level */
  295. if (log_verbose_level != 2) {
  296. ecall_args[0] = log_verbose_level;
  297. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL,
  298. (uint8_t *)ecall_args,
  299. sizeof(uint64_t))) {
  300. printf("Call ecall_handle_command() failed.\n");
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. static bool
  307. init_runtime(bool alloc_with_pool, uint32_t max_thread_num)
  308. {
  309. uint64_t ecall_args[2];
  310. ecall_args[0] = alloc_with_pool;
  311. ecall_args[1] = max_thread_num;
  312. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INIT_RUNTIME,
  313. (uint8_t *)ecall_args,
  314. sizeof(uint64_t) * 2)) {
  315. printf("Call ecall_handle_command() failed.\n");
  316. return false;
  317. }
  318. if (!(bool)ecall_args[0]) {
  319. printf("Init runtime environment failed.\n");
  320. return false;
  321. }
  322. return true;
  323. }
  324. static void
  325. destroy_runtime()
  326. {
  327. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME,
  328. NULL, 0)) {
  329. printf("Call ecall_handle_command() failed.\n");
  330. }
  331. }
  332. static void *
  333. load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size,
  334. char *error_buf, uint32_t error_buf_size)
  335. {
  336. uint64_t ecall_args[4];
  337. ecall_args[0] = (uint64_t)(uintptr_t)wasm_file_buf;
  338. ecall_args[1] = wasm_file_size;
  339. ecall_args[2] = (uint64_t)(uintptr_t)error_buf;
  340. ecall_args[3] = error_buf_size;
  341. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_LOAD_MODULE,
  342. (uint8_t *)ecall_args,
  343. sizeof(uint64_t) * 4)) {
  344. printf("Call ecall_handle_command() failed.\n");
  345. return NULL;
  346. }
  347. return (void *)(uintptr_t)ecall_args[0];
  348. }
  349. static void
  350. unload_module(void *wasm_module)
  351. {
  352. uint64_t ecall_args[1];
  353. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
  354. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE,
  355. (uint8_t *)ecall_args,
  356. sizeof(uint64_t))) {
  357. printf("Call ecall_handle_command() failed.\n");
  358. }
  359. }
  360. static void *
  361. instantiate_module(void *wasm_module,
  362. uint32_t stack_size, uint32_t heap_size,
  363. char *error_buf, uint32_t error_buf_size)
  364. {
  365. uint64_t ecall_args[5];
  366. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
  367. ecall_args[1] = stack_size;
  368. ecall_args[2] = heap_size;
  369. ecall_args[3] = (uint64_t)(uintptr_t)error_buf;
  370. ecall_args[4] = error_buf_size;
  371. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE,
  372. (uint8_t *)ecall_args,
  373. sizeof(uint64_t) * 5)) {
  374. printf("Call ecall_handle_command() failed.\n");
  375. return NULL;
  376. }
  377. return (void *)(uintptr_t)ecall_args[0];
  378. }
  379. static void
  380. deinstantiate_module(void *wasm_module_inst)
  381. {
  382. uint64_t ecall_args[1];
  383. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
  384. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE,
  385. (uint8_t *)ecall_args,
  386. sizeof(uint64_t))) {
  387. printf("Call ecall_handle_command() failed.\n");
  388. }
  389. }
  390. static bool
  391. get_exception(void *wasm_module_inst,
  392. char *exception, uint32_t exception_size)
  393. {
  394. uint64_t ecall_args[3];
  395. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
  396. ecall_args[1] = (uint64_t)(uintptr_t)exception;
  397. ecall_args[2] = exception_size;
  398. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_GET_EXCEPTION,
  399. (uint8_t *)ecall_args,
  400. sizeof(uint64_t) * 3)) {
  401. printf("Call ecall_handle_command() failed.\n");
  402. }
  403. return (bool)ecall_args[0];
  404. }
  405. static void
  406. app_instance_main(void *wasm_module_inst,
  407. int app_argc, char **app_argv)
  408. {
  409. char exception[128];
  410. uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
  411. int i, size;
  412. if (app_argc + 2 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
  413. if (!(ecall_args = (uint64_t *)
  414. malloc(sizeof(uint64_t) * (app_argc + 2)))) {
  415. printf("Allocate memory failed.\n");
  416. return;
  417. }
  418. }
  419. ecall_args[0] = (uintptr_t)wasm_module_inst;
  420. ecall_args[1] = app_argc;
  421. for (i = 0; i < app_argc; i++) {
  422. ecall_args[i + 2] = (uintptr_t)app_argv[i];
  423. }
  424. size = (uint32_t)sizeof(uint64_t) * (app_argc + 2);
  425. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN,
  426. (uint8_t *)ecall_args,
  427. size)) {
  428. printf("Call ecall_handle_command() failed.\n");
  429. }
  430. if (get_exception(wasm_module_inst, exception, sizeof(exception))) {
  431. printf("%s\n", exception);
  432. }
  433. if (ecall_args != ecall_args_buf) {
  434. free(ecall_args);
  435. }
  436. }
  437. static void
  438. app_instance_func(void *wasm_module_inst, const char *func_name,
  439. int app_argc, char **app_argv)
  440. {
  441. uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
  442. int i, size;
  443. if (app_argc + 3 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
  444. if (!(ecall_args = (uint64_t *)
  445. malloc(sizeof(uint64_t) * (app_argc + 3)))) {
  446. printf("Allocate memory failed.\n");
  447. return;
  448. }
  449. }
  450. ecall_args[0] = (uintptr_t)wasm_module_inst;
  451. ecall_args[1] = (uintptr_t)func_name;
  452. ecall_args[2] = (uintptr_t)app_argc;
  453. for (i = 0; i < app_argc; i++) {
  454. ecall_args[i + 3] = (uintptr_t)app_argv[i];
  455. }
  456. size = (uint32_t)sizeof(uint64_t) * (app_argc + 3);
  457. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC,
  458. (uint8_t *)ecall_args,
  459. size)) {
  460. printf("Call ecall_handle_command() failed.\n");
  461. }
  462. if (ecall_args != ecall_args_buf) {
  463. free(ecall_args);
  464. }
  465. }
  466. static bool
  467. set_wasi_args(void *wasm_module,
  468. const char **dir_list, uint32_t dir_list_size,
  469. const char **env_list, uint32_t env_list_size,
  470. char **argv, uint32_t argc)
  471. {
  472. uint64_t ecall_args[7];
  473. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
  474. ecall_args[1] = (uint64_t)(uintptr_t)dir_list;
  475. ecall_args[2] = dir_list_size;
  476. ecall_args[3] = (uint64_t)(uintptr_t)env_list;
  477. ecall_args[4] = env_list_size;
  478. ecall_args[5] = (uint64_t)(uintptr_t)argv;
  479. ecall_args[6] = argc;
  480. if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS,
  481. (uint8_t *)ecall_args,
  482. sizeof(uint64_t) * 7)) {
  483. printf("Call ecall_handle_command() failed.\n");
  484. }
  485. return (bool)ecall_args[0];
  486. }
  487. int
  488. main(int argc, char *argv[])
  489. {
  490. char *wasm_file = NULL;
  491. const char *func_name = NULL;
  492. uint8_t *wasm_file_buf = NULL;
  493. uint32_t wasm_file_size;
  494. uint32_t stack_size = 16 * 1024, heap_size = 16 * 1024;
  495. void *wasm_module = NULL;
  496. void *wasm_module_inst = NULL;
  497. char error_buf[128] = { 0 };
  498. int log_verbose_level = 2;
  499. bool is_repl_mode = false, alloc_with_pool = false;
  500. const char *dir_list[8] = { NULL };
  501. uint32_t dir_list_size = 0;
  502. const char *env_list[8] = { NULL };
  503. uint32_t env_list_size = 0;
  504. uint32_t max_thread_num = 4;
  505. if (enclave_init(&g_eid) < 0) {
  506. std::cout << "Fail to initialize enclave." << std::endl;
  507. return 1;
  508. }
  509. #if TEST_OCALL_API != 0
  510. {
  511. if (!init_runtime(alloc_with_pool, max_thread_num)) {
  512. return -1;
  513. }
  514. ecall_iwasm_test(g_eid);
  515. destroy_runtime();
  516. return 0;
  517. }
  518. #endif
  519. /* Process options. */
  520. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  521. if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
  522. argc--, argv++;
  523. if (argc < 2) {
  524. print_help();
  525. return 0;
  526. }
  527. func_name = argv[0];
  528. }
  529. else if (!strncmp(argv[0], "-v=", 3)) {
  530. log_verbose_level = atoi(argv[0] + 3);
  531. if (log_verbose_level < 0 || log_verbose_level > 5)
  532. return print_help();
  533. }
  534. else if (!strcmp(argv[0], "--repl")) {
  535. is_repl_mode = true;
  536. }
  537. else if (!strncmp(argv[0], "--stack-size=", 13)) {
  538. if (argv[0][13] == '\0')
  539. return print_help();
  540. stack_size = atoi(argv[0] + 13);
  541. }
  542. else if (!strncmp(argv[0], "--heap-size=", 12)) {
  543. if (argv[0][12] == '\0')
  544. return print_help();
  545. heap_size = atoi(argv[0] + 12);
  546. }
  547. else if (!strncmp(argv[0], "--dir=", 6)) {
  548. if (argv[0][6] == '\0')
  549. return print_help();
  550. if (dir_list_size >= sizeof(dir_list) / sizeof(char *)) {
  551. printf("Only allow max dir number %d\n",
  552. (int)(sizeof(dir_list) / sizeof(char *)));
  553. return -1;
  554. }
  555. dir_list[dir_list_size++] = argv[0] + 6;
  556. }
  557. else if (!strncmp(argv[0], "--env=", 6)) {
  558. char *tmp_env;
  559. if (argv[0][6] == '\0')
  560. return print_help();
  561. if (env_list_size >= sizeof(env_list) / sizeof(char *)) {
  562. printf("Only allow max env number %d\n",
  563. (int)(sizeof(env_list) / sizeof(char *)));
  564. return -1;
  565. }
  566. tmp_env = argv[0] + 6;
  567. if (validate_env_str(tmp_env))
  568. env_list[env_list_size++] = tmp_env;
  569. else {
  570. printf("Wasm parse env string failed: expect \"key=value\", "
  571. "got \"%s\"\n",
  572. tmp_env);
  573. return print_help();
  574. }
  575. }
  576. else if (!strncmp(argv[0], "--max-threads=", 14)) {
  577. if (argv[0][14] == '\0')
  578. return print_help();
  579. max_thread_num = atoi(argv[0] + 14);
  580. }
  581. else
  582. return print_help();
  583. }
  584. if (argc == 0)
  585. return print_help();
  586. wasm_file = argv[0];
  587. /* Init runtime */
  588. if (!init_runtime(alloc_with_pool, max_thread_num)) {
  589. return -1;
  590. }
  591. /* Set log verbose level */
  592. if (!set_log_verbose_level(log_verbose_level)) {
  593. goto fail1;
  594. }
  595. /* Load WASM byte buffer from WASM bin file */
  596. if (!(wasm_file_buf =
  597. (uint8_t *)read_file_to_buffer(wasm_file, &wasm_file_size))) {
  598. goto fail1;
  599. }
  600. /* Load module */
  601. if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size,
  602. error_buf, sizeof(error_buf)))) {
  603. printf("%s\n", error_buf);
  604. goto fail2;
  605. }
  606. /* Set wasi arguments */
  607. if (!set_wasi_args(wasm_module, dir_list, dir_list_size,
  608. env_list, env_list_size, argv, argc)) {
  609. printf("%s\n", "set wasi arguments failed.\n");
  610. goto fail3;
  611. }
  612. /* Instantiate module */
  613. if (!(wasm_module_inst = instantiate_module(wasm_module,
  614. stack_size, heap_size,
  615. error_buf,
  616. sizeof(error_buf)))) {
  617. printf("%s\n", error_buf);
  618. goto fail3;
  619. }
  620. if (is_repl_mode)
  621. app_instance_repl(wasm_module_inst, argc, argv);
  622. else if (func_name)
  623. app_instance_func(wasm_module_inst, func_name,
  624. argc - 1, argv + 1);
  625. else
  626. app_instance_main(wasm_module_inst, argc, argv);
  627. /* Deinstantiate module */
  628. deinstantiate_module(wasm_module_inst);
  629. fail3:
  630. /* Unload module */
  631. unload_module(wasm_module);
  632. fail2:
  633. /* Free the file buffer */
  634. free(wasm_file_buf);
  635. fail1:
  636. /* Destroy runtime environment */
  637. destroy_runtime();
  638. return 0;
  639. }