App.cpp 31 KB

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