App.cpp 26 KB

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