App.cpp 27 KB

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