App.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. int
  35. ocall_print(const char *str)
  36. {
  37. return 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("/") + 1);
  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 64 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(" --addr-pool= Grant wasi access to the given network addresses in\n");
  197. printf(" CIRD notation to the program, seperated with ',',\n");
  198. printf(" for example:\n");
  199. printf(" --addr-pool=1.2.3.4/15,2.3.4.5/16\n");
  200. printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
  201. printf(" --version Show version information\n");
  202. return 1;
  203. }
  204. /* clang-format on */
  205. /**
  206. * Split a space separated strings into an array of strings
  207. * Returns NULL on failure
  208. * Memory must be freed by caller
  209. * Based on: http://stackoverflow.com/a/11198630/471795
  210. */
  211. static char **
  212. split_string(char *str, int *count)
  213. {
  214. char **res = NULL;
  215. char *p;
  216. int idx = 0;
  217. /* split string and append tokens to 'res' */
  218. do {
  219. p = strtok(str, " ");
  220. str = NULL;
  221. res = (char **)realloc(res, sizeof(char *) * (unsigned)(idx + 1));
  222. if (res == NULL) {
  223. return NULL;
  224. }
  225. res[idx++] = p;
  226. } while (p);
  227. /**
  228. * since the function name,
  229. * res[0] might be contains a '\' to indicate a space
  230. * func\name -> func name
  231. */
  232. p = strchr(res[0], '\\');
  233. while (p) {
  234. *p = ' ';
  235. p = strchr(p, '\\');
  236. }
  237. if (count) {
  238. *count = idx - 1;
  239. }
  240. return res;
  241. }
  242. typedef enum EcallCmd {
  243. CMD_INIT_RUNTIME = 0, /* wasm_runtime_init/full_init() */
  244. CMD_LOAD_MODULE, /* wasm_runtime_load() */
  245. CMD_INSTANTIATE_MODULE, /* wasm_runtime_instantiate() */
  246. CMD_LOOKUP_FUNCTION, /* wasm_runtime_lookup_function() */
  247. CMD_CREATE_EXEC_ENV, /* wasm_runtime_create_exec_env() */
  248. CMD_CALL_WASM, /* wasm_runtime_call_wasm */
  249. CMD_EXEC_APP_FUNC, /* wasm_application_execute_func() */
  250. CMD_EXEC_APP_MAIN, /* wasm_application_execute_main() */
  251. CMD_GET_EXCEPTION, /* wasm_runtime_get_exception() */
  252. CMD_DEINSTANTIATE_MODULE, /* wasm_runtime_deinstantiate() */
  253. CMD_UNLOAD_MODULE, /* wasm_runtime_unload() */
  254. CMD_DESTROY_RUNTIME, /* wasm_runtime_destroy() */
  255. CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */
  256. CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */
  257. CMD_GET_VERSION, /* wasm_runtime_get_version() */
  258. } EcallCmd;
  259. static void
  260. app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc,
  261. char **app_argv);
  262. static void *
  263. app_instance_repl(void *module_inst, int app_argc, char **app_argv)
  264. {
  265. char *cmd = NULL;
  266. size_t len = 0;
  267. ssize_t n;
  268. while ((printf("webassembly> "), n = getline(&cmd, &len, stdin)) != -1) {
  269. assert(n > 0);
  270. if (cmd[n - 1] == '\n') {
  271. if (n == 1)
  272. continue;
  273. else
  274. cmd[n - 1] = '\0';
  275. }
  276. if (!strcmp(cmd, "__exit__")) {
  277. printf("exit repl mode\n");
  278. break;
  279. }
  280. app_argv = split_string(cmd, &app_argc);
  281. if (app_argv == NULL) {
  282. printf("Wasm prepare param failed: split string failed.\n");
  283. break;
  284. }
  285. if (app_argc != 0) {
  286. app_instance_func(module_inst, app_argv[0], app_argc - 1,
  287. app_argv + 1);
  288. }
  289. free(app_argv);
  290. }
  291. free(cmd);
  292. return NULL;
  293. }
  294. static bool
  295. validate_env_str(char *env)
  296. {
  297. char *p = env;
  298. int key_len = 0;
  299. while (*p != '\0' && *p != '=') {
  300. key_len++;
  301. p++;
  302. }
  303. if (*p != '=' || key_len == 0)
  304. return false;
  305. return true;
  306. }
  307. static bool
  308. set_log_verbose_level(int log_verbose_level)
  309. {
  310. uint64_t ecall_args[1];
  311. /* Set log verbose level */
  312. if (log_verbose_level != 2) {
  313. ecall_args[0] = log_verbose_level;
  314. if (SGX_SUCCESS
  315. != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL,
  316. (uint8_t *)ecall_args, sizeof(uint64_t))) {
  317. printf("Call ecall_handle_command() failed.\n");
  318. return false;
  319. }
  320. }
  321. return true;
  322. }
  323. static bool
  324. init_runtime(uint32_t max_thread_num)
  325. {
  326. uint64_t ecall_args[1];
  327. ecall_args[0] = max_thread_num;
  328. if (SGX_SUCCESS
  329. != ecall_handle_command(g_eid, CMD_INIT_RUNTIME, (uint8_t *)ecall_args,
  330. sizeof(ecall_args))) {
  331. printf("Call ecall_handle_command() failed.\n");
  332. return false;
  333. }
  334. if (!(bool)ecall_args[0]) {
  335. printf("Init runtime environment failed.\n");
  336. return false;
  337. }
  338. return true;
  339. }
  340. static void
  341. destroy_runtime()
  342. {
  343. if (SGX_SUCCESS
  344. != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME, NULL, 0)) {
  345. printf("Call ecall_handle_command() failed.\n");
  346. }
  347. }
  348. static void *
  349. load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size, char *error_buf,
  350. uint32_t error_buf_size)
  351. {
  352. uint64_t ecall_args[4];
  353. ecall_args[0] = (uint64_t)(uintptr_t)wasm_file_buf;
  354. ecall_args[1] = wasm_file_size;
  355. ecall_args[2] = (uint64_t)(uintptr_t)error_buf;
  356. ecall_args[3] = error_buf_size;
  357. if (SGX_SUCCESS
  358. != ecall_handle_command(g_eid, CMD_LOAD_MODULE, (uint8_t *)ecall_args,
  359. sizeof(uint64_t) * 4)) {
  360. printf("Call ecall_handle_command() failed.\n");
  361. return NULL;
  362. }
  363. return (void *)(uintptr_t)ecall_args[0];
  364. }
  365. static void
  366. unload_module(void *wasm_module)
  367. {
  368. uint64_t ecall_args[1];
  369. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
  370. if (SGX_SUCCESS
  371. != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE, (uint8_t *)ecall_args,
  372. sizeof(uint64_t))) {
  373. printf("Call ecall_handle_command() failed.\n");
  374. }
  375. }
  376. static void *
  377. instantiate_module(void *wasm_module, uint32_t stack_size, uint32_t heap_size,
  378. char *error_buf, uint32_t error_buf_size)
  379. {
  380. uint64_t ecall_args[5];
  381. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
  382. ecall_args[1] = stack_size;
  383. ecall_args[2] = heap_size;
  384. ecall_args[3] = (uint64_t)(uintptr_t)error_buf;
  385. ecall_args[4] = error_buf_size;
  386. if (SGX_SUCCESS
  387. != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE,
  388. (uint8_t *)ecall_args, sizeof(uint64_t) * 5)) {
  389. printf("Call ecall_handle_command() failed.\n");
  390. return NULL;
  391. }
  392. return (void *)(uintptr_t)ecall_args[0];
  393. }
  394. static void
  395. deinstantiate_module(void *wasm_module_inst)
  396. {
  397. uint64_t ecall_args[1];
  398. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
  399. if (SGX_SUCCESS
  400. != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE,
  401. (uint8_t *)ecall_args, sizeof(uint64_t))) {
  402. printf("Call ecall_handle_command() failed.\n");
  403. }
  404. }
  405. static bool
  406. get_exception(void *wasm_module_inst, char *exception, uint32_t exception_size)
  407. {
  408. uint64_t ecall_args[3];
  409. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
  410. ecall_args[1] = (uint64_t)(uintptr_t)exception;
  411. ecall_args[2] = exception_size;
  412. if (SGX_SUCCESS
  413. != ecall_handle_command(g_eid, CMD_GET_EXCEPTION, (uint8_t *)ecall_args,
  414. sizeof(uint64_t) * 3)) {
  415. printf("Call ecall_handle_command() failed.\n");
  416. }
  417. return (bool)ecall_args[0];
  418. }
  419. static void
  420. app_instance_main(void *wasm_module_inst, int app_argc, char **app_argv)
  421. {
  422. char exception[128];
  423. uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
  424. int i, size;
  425. if (app_argc + 2 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
  426. if (!(ecall_args =
  427. (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 2)))) {
  428. printf("Allocate memory failed.\n");
  429. return;
  430. }
  431. }
  432. ecall_args[0] = (uintptr_t)wasm_module_inst;
  433. ecall_args[1] = app_argc;
  434. for (i = 0; i < app_argc; i++) {
  435. ecall_args[i + 2] = (uintptr_t)app_argv[i];
  436. }
  437. size = (uint32_t)sizeof(uint64_t) * (app_argc + 2);
  438. if (SGX_SUCCESS
  439. != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN, (uint8_t *)ecall_args,
  440. size)) {
  441. printf("Call ecall_handle_command() failed.\n");
  442. }
  443. if (get_exception(wasm_module_inst, exception, sizeof(exception))) {
  444. printf("%s\n", exception);
  445. }
  446. if (ecall_args != ecall_args_buf) {
  447. free(ecall_args);
  448. }
  449. }
  450. static void
  451. app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc,
  452. char **app_argv)
  453. {
  454. uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
  455. int i, size;
  456. if (app_argc + 3 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
  457. if (!(ecall_args =
  458. (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 3)))) {
  459. printf("Allocate memory failed.\n");
  460. return;
  461. }
  462. }
  463. ecall_args[0] = (uintptr_t)wasm_module_inst;
  464. ecall_args[1] = (uintptr_t)func_name;
  465. ecall_args[2] = (uintptr_t)app_argc;
  466. for (i = 0; i < app_argc; i++) {
  467. ecall_args[i + 3] = (uintptr_t)app_argv[i];
  468. }
  469. size = (uint32_t)sizeof(uint64_t) * (app_argc + 3);
  470. if (SGX_SUCCESS
  471. != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC, (uint8_t *)ecall_args,
  472. size)) {
  473. printf("Call ecall_handle_command() failed.\n");
  474. }
  475. if (ecall_args != ecall_args_buf) {
  476. free(ecall_args);
  477. }
  478. }
  479. static bool
  480. set_wasi_args(void *wasm_module, const char **dir_list, uint32_t dir_list_size,
  481. const char **env_list, uint32_t env_list_size, int stdinfd,
  482. int stdoutfd, int stderrfd, char **argv, uint32_t argc,
  483. const char **addr_pool, uint32_t addr_pool_size)
  484. {
  485. uint64_t ecall_args[12];
  486. ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
  487. ecall_args[1] = (uint64_t)(uintptr_t)dir_list;
  488. ecall_args[2] = dir_list_size;
  489. ecall_args[3] = (uint64_t)(uintptr_t)env_list;
  490. ecall_args[4] = env_list_size;
  491. ecall_args[5] = stdinfd;
  492. ecall_args[6] = stdoutfd;
  493. ecall_args[7] = stderrfd;
  494. ecall_args[8] = (uint64_t)(uintptr_t)argv;
  495. ecall_args[9] = argc;
  496. ecall_args[10] = (uint64_t)(uintptr_t)addr_pool;
  497. ecall_args[11] = addr_pool_size;
  498. if (SGX_SUCCESS
  499. != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS, (uint8_t *)ecall_args,
  500. sizeof(uint64_t) * 12)) {
  501. printf("Call ecall_handle_command() failed.\n");
  502. }
  503. return (bool)ecall_args[0];
  504. }
  505. static void
  506. get_version(uint64_t *major, uint64_t *minor, uint64_t *patch)
  507. {
  508. uint64_t ecall_args[3] = { 0 };
  509. if (SGX_SUCCESS
  510. != ecall_handle_command(g_eid, CMD_GET_VERSION, (uint8_t *)ecall_args,
  511. sizeof(ecall_args))) {
  512. printf("Call ecall_handle_command() failed.\n");
  513. return;
  514. }
  515. *major = ecall_args[0];
  516. *minor = ecall_args[1];
  517. *patch = ecall_args[2];
  518. }
  519. int
  520. main(int argc, char *argv[])
  521. {
  522. int32_t ret = -1;
  523. char *wasm_file = NULL;
  524. const char *func_name = NULL;
  525. uint8_t *wasm_file_buf = NULL;
  526. uint32_t wasm_file_size;
  527. uint32_t stack_size = 64 * 1024, heap_size = 16 * 1024;
  528. void *wasm_module = NULL;
  529. void *wasm_module_inst = NULL;
  530. char error_buf[128] = { 0 };
  531. int log_verbose_level = 2;
  532. bool is_repl_mode = false;
  533. const char *dir_list[8] = { NULL };
  534. uint32_t dir_list_size = 0;
  535. const char *env_list[8] = { NULL };
  536. uint32_t env_list_size = 0;
  537. const char *addr_pool[8] = { NULL };
  538. uint32_t addr_pool_size = 0;
  539. uint32_t max_thread_num = 4;
  540. if (enclave_init(&g_eid) < 0) {
  541. std::cout << "Fail to initialize enclave." << std::endl;
  542. return 1;
  543. }
  544. #if TEST_OCALL_API != 0
  545. {
  546. if (!init_runtime(max_thread_num)) {
  547. return -1;
  548. }
  549. ecall_iwasm_test(g_eid);
  550. destroy_runtime();
  551. return 0;
  552. }
  553. #endif
  554. /* Process options. */
  555. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  556. if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
  557. argc--, argv++;
  558. if (argc < 2) {
  559. return print_help();
  560. }
  561. func_name = argv[0];
  562. }
  563. else if (!strncmp(argv[0], "-v=", 3)) {
  564. log_verbose_level = atoi(argv[0] + 3);
  565. if (log_verbose_level < 0 || log_verbose_level > 5)
  566. return print_help();
  567. }
  568. else if (!strcmp(argv[0], "--repl")) {
  569. is_repl_mode = true;
  570. }
  571. else if (!strncmp(argv[0], "--stack-size=", 13)) {
  572. if (argv[0][13] == '\0')
  573. return print_help();
  574. stack_size = atoi(argv[0] + 13);
  575. }
  576. else if (!strncmp(argv[0], "--heap-size=", 12)) {
  577. if (argv[0][12] == '\0')
  578. return print_help();
  579. heap_size = atoi(argv[0] + 12);
  580. }
  581. else if (!strncmp(argv[0], "--dir=", 6)) {
  582. if (argv[0][6] == '\0')
  583. return print_help();
  584. if (dir_list_size >= sizeof(dir_list) / sizeof(char *)) {
  585. printf("Only allow max dir number %d\n",
  586. (int)(sizeof(dir_list) / sizeof(char *)));
  587. return 1;
  588. }
  589. dir_list[dir_list_size++] = argv[0] + 6;
  590. }
  591. else if (!strncmp(argv[0], "--env=", 6)) {
  592. char *tmp_env;
  593. if (argv[0][6] == '\0')
  594. return print_help();
  595. if (env_list_size >= sizeof(env_list) / sizeof(char *)) {
  596. printf("Only allow max env number %d\n",
  597. (int)(sizeof(env_list) / sizeof(char *)));
  598. return 1;
  599. }
  600. tmp_env = argv[0] + 6;
  601. if (validate_env_str(tmp_env))
  602. env_list[env_list_size++] = tmp_env;
  603. else {
  604. printf("Wasm parse env string failed: expect \"key=value\", "
  605. "got \"%s\"\n",
  606. tmp_env);
  607. return print_help();
  608. }
  609. }
  610. /* TODO: parse the configuration file via --addr-pool-file */
  611. else if (!strncmp(argv[0], "--addr-pool=", strlen("--addr-pool="))) {
  612. /* like: --addr-pool=100.200.244.255/30 */
  613. char *token = NULL;
  614. if ('\0' == argv[0][12])
  615. return print_help();
  616. token = strtok(argv[0] + strlen("--addr-pool="), ",");
  617. while (token) {
  618. if (addr_pool_size >= sizeof(addr_pool) / sizeof(char *)) {
  619. printf("Only allow max address number %d\n",
  620. (int)(sizeof(addr_pool) / sizeof(char *)));
  621. return 1;
  622. }
  623. addr_pool[addr_pool_size++] = token;
  624. token = strtok(NULL, ";");
  625. }
  626. }
  627. else if (!strncmp(argv[0], "--max-threads=", 14)) {
  628. if (argv[0][14] == '\0')
  629. return print_help();
  630. max_thread_num = atoi(argv[0] + 14);
  631. }
  632. else if (!strncmp(argv[0], "--version", 9)) {
  633. uint64_t major = 0, minor = 0, patch = 0;
  634. get_version(&major, &minor, &patch);
  635. printf("iwasm %lu.%lu.%lu\n", major, minor, patch);
  636. return 0;
  637. }
  638. else
  639. return print_help();
  640. }
  641. if (argc == 0)
  642. return print_help();
  643. wasm_file = argv[0];
  644. /* Init runtime */
  645. if (!init_runtime(max_thread_num)) {
  646. return -1;
  647. }
  648. /* Set log verbose level */
  649. if (!set_log_verbose_level(log_verbose_level)) {
  650. goto fail1;
  651. }
  652. /* Load WASM byte buffer from WASM bin file */
  653. if (!(wasm_file_buf =
  654. (uint8_t *)read_file_to_buffer(wasm_file, &wasm_file_size))) {
  655. goto fail1;
  656. }
  657. /* Load module */
  658. if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, error_buf,
  659. sizeof(error_buf)))) {
  660. printf("%s\n", error_buf);
  661. goto fail2;
  662. }
  663. /* Set wasi arguments */
  664. if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list,
  665. env_list_size, 0, 1, 2, argv, argc, addr_pool,
  666. addr_pool_size)) {
  667. printf("%s\n", "set wasi arguments failed.\n");
  668. goto fail3;
  669. }
  670. /* Instantiate module */
  671. if (!(wasm_module_inst =
  672. instantiate_module(wasm_module, stack_size, heap_size, error_buf,
  673. sizeof(error_buf)))) {
  674. printf("%s\n", error_buf);
  675. goto fail3;
  676. }
  677. if (is_repl_mode)
  678. app_instance_repl(wasm_module_inst, argc, argv);
  679. else if (func_name)
  680. app_instance_func(wasm_module_inst, func_name, argc - 1, argv + 1);
  681. else
  682. app_instance_main(wasm_module_inst, argc, argv);
  683. ret = 0;
  684. /* Deinstantiate module */
  685. deinstantiate_module(wasm_module_inst);
  686. fail3:
  687. /* Unload module */
  688. unload_module(wasm_module);
  689. fail2:
  690. /* Free the file buffer */
  691. free(wasm_file_buf);
  692. fail1:
  693. /* Destroy runtime environment */
  694. destroy_runtime();
  695. return ret;
  696. }
  697. int
  698. wamr_pal_get_version(void)
  699. {
  700. return WAMR_PAL_VERSION;
  701. }
  702. int
  703. wamr_pal_init(const struct wamr_pal_attr *args)
  704. {
  705. sgx_enclave_id_t *p_eid = &g_eid;
  706. if (enclave_init(&g_eid) < 0) {
  707. std::cout << "Fail to initialize enclave." << std::endl;
  708. return 1;
  709. }
  710. return 0;
  711. }
  712. int
  713. wamr_pal_create_process(struct wamr_pal_create_process_args *args)
  714. {
  715. uint32_t stack_size = 64 * 1024, heap_size = 16 * 1024;
  716. int log_verbose_level = 2;
  717. bool is_repl_mode = false;
  718. const char *dir_list[8] = { NULL };
  719. uint32_t dir_list_size = 0;
  720. const char *env_list[8] = { NULL };
  721. uint32_t env_list_size = 0;
  722. const char *addr_pool[8] = { NULL };
  723. uint32_t addr_pool_size = 0;
  724. uint32_t max_thread_num = 4;
  725. char *wasm_files[16];
  726. void *wasm_module_inst[16];
  727. int stdinfd = -1;
  728. int stdoutfd = -1;
  729. int stderrfd = -1;
  730. int argc = 2;
  731. char *argv[argc] = { (char *)"./iwasm", (char *)args->argv[0] };
  732. uint8_t *wasm_files_buf = NULL;
  733. void *wasm_modules = NULL;
  734. int len = 0, i;
  735. char *temp = (char *)args->argv[0];
  736. while (temp) {
  737. len++;
  738. temp = (char *)args->argv[len];
  739. }
  740. if (len > sizeof(wasm_files) / sizeof(char *)) {
  741. printf("Number of input files is out of range\n");
  742. return -1;
  743. }
  744. for (i = 0; i < len; ++i) {
  745. wasm_files[i] = (char *)args->argv[i];
  746. }
  747. if (args->stdio != NULL) {
  748. stdinfd = args->stdio->stdin_fd;
  749. stdoutfd = args->stdio->stdout_fd;
  750. stderrfd = args->stdio->stderr_fd;
  751. }
  752. /* Init runtime */
  753. if (!init_runtime(max_thread_num)) {
  754. printf("Failed to init runtime\n");
  755. return -1;
  756. }
  757. /* Set log verbose level */
  758. if (!set_log_verbose_level(log_verbose_level)) {
  759. printf("Failed to set log level\n");
  760. destroy_runtime();
  761. return -1;
  762. }
  763. for (i = 0; i < len; ++i) {
  764. uint8_t *wasm_file_buf = NULL;
  765. uint32_t wasm_file_size;
  766. void *wasm_module = NULL;
  767. char error_buf[128] = { 0 };
  768. /* Load WASM byte buffer from WASM bin file */
  769. if (!(wasm_file_buf = (uint8_t *)read_file_to_buffer(
  770. wasm_files[i], &wasm_file_size))) {
  771. printf("Failed to read file to buffer\n");
  772. destroy_runtime();
  773. return -1;
  774. }
  775. /* Load module */
  776. if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size,
  777. error_buf, sizeof(error_buf)))) {
  778. printf("%s\n", error_buf);
  779. free(wasm_file_buf);
  780. destroy_runtime();
  781. return -1;
  782. }
  783. /* Set wasi arguments */
  784. if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list,
  785. env_list_size, stdinfd, stdoutfd, stderrfd, argv,
  786. argc, addr_pool, addr_pool_size)) {
  787. printf("%s\n", "set wasi arguments failed.\n");
  788. unload_module(wasm_module);
  789. free(wasm_file_buf);
  790. destroy_runtime();
  791. return -1;
  792. }
  793. /* Instantiate module */
  794. if (!(wasm_module_inst[i] =
  795. instantiate_module(wasm_module, stack_size, heap_size,
  796. error_buf, sizeof(error_buf)))) {
  797. printf("%s\n", error_buf);
  798. unload_module(wasm_module);
  799. free(wasm_file_buf);
  800. destroy_runtime();
  801. return -1;
  802. }
  803. app_instance_main(wasm_module_inst[i], argc, argv);
  804. /* Deinstantiate module */
  805. deinstantiate_module(wasm_module_inst[i]);
  806. unload_module(wasm_module);
  807. free(wasm_file_buf);
  808. }
  809. destroy_runtime();
  810. return 0;
  811. }
  812. int
  813. wamr_pal_destroy(void)
  814. {
  815. // sgx_destroy_enclave(g_eid);
  816. return 0;
  817. }
  818. int
  819. wamr_pal_exec(struct wamr_pal_exec_args *args)
  820. {
  821. // app_instance_main(wasm_module_inst[i], argc, argv);
  822. return 0;
  823. }
  824. int
  825. wamr_pal_kill(int pid, int sig)
  826. {
  827. // deinstantiate_module(wasm_module_inst[i]);
  828. // unload_module(wasm_module);
  829. // free(wasm_file_buf);
  830. return 0;
  831. }
  832. int
  833. pal_get_version(void) __attribute__((weak, alias("wamr_pal_get_version")));
  834. int
  835. pal_init(const struct wamr_pal_attr *attr)
  836. __attribute__((weak, alias("wamr_pal_init")));
  837. int
  838. pal_create_process(struct wamr_pal_create_process_args *args)
  839. __attribute__((weak, alias("wamr_pal_create_process")));
  840. int
  841. pal_exec(struct wamr_pal_exec_args *args)
  842. __attribute__((weak, alias("wamr_pal_exec")));
  843. int
  844. pal_kill(int pid, int sig) __attribute__((weak, alias("wamr_pal_kill")));
  845. int
  846. pal_destroy(void) __attribute__((weak, alias("wamr_pal_destroy")));