App.cpp 28 KB

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