main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2022 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_export.h"
  6. #include "bh_read_file.h"
  7. void
  8. print_usage(void)
  9. {
  10. fprintf(stdout, "Required arguments:\r\n");
  11. fprintf(stdout, " -f [path of wasm file] \n");
  12. fprintf(stdout, " -d [path of host directory] \n");
  13. }
  14. int
  15. main(int argc, char *argv_main[])
  16. {
  17. static char global_heap_buf[512 * 1024];
  18. char *buffer, error_buf[128];
  19. const char *wasm_path = NULL, *wasi_dir = NULL;
  20. int opt, main_result = 1;
  21. wasm_module_t module = NULL;
  22. wasm_module_inst_t module_inst = NULL;
  23. wasm_exec_env_t exec_env = NULL;
  24. uint32 buf_size, stack_size = 8092, heap_size = 8092;
  25. RuntimeInitArgs init_args;
  26. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  27. while ((opt = getopt(argc, argv_main, "hf:d:")) != -1) {
  28. switch (opt) {
  29. case 'f':
  30. wasm_path = optarg;
  31. break;
  32. case 'd':
  33. wasi_dir = optarg;
  34. break;
  35. case 'h':
  36. print_usage();
  37. return 0;
  38. case '?':
  39. print_usage();
  40. return 0;
  41. }
  42. }
  43. if (wasm_path == NULL || wasi_dir == NULL) {
  44. print_usage();
  45. return 0;
  46. }
  47. init_args.mem_alloc_type = Alloc_With_Pool;
  48. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  49. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  50. if (!wasm_runtime_full_init(&init_args)) {
  51. printf("Init runtime environment failed.\n");
  52. return -1;
  53. }
  54. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  55. if (!buffer) {
  56. printf("Open wasm app file [%s] failed.\n", wasm_path);
  57. goto fail;
  58. }
  59. module = wasm_runtime_load(buffer, buf_size, error_buf, sizeof(error_buf));
  60. if (!module) {
  61. printf("Load wasm module failed. error: %s\n", error_buf);
  62. goto fail;
  63. }
  64. wasm_runtime_set_wasi_args_ex(module, &wasi_dir, 1, NULL, 0, NULL, 0, NULL,
  65. 0, 0, 1, 2);
  66. module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
  67. error_buf, sizeof(error_buf));
  68. if (!module_inst) {
  69. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  70. goto fail;
  71. }
  72. exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
  73. if (!exec_env) {
  74. printf("Create wasm execution environment failed.\n");
  75. goto fail;
  76. }
  77. if (wasm_application_execute_main(module_inst, 0, NULL)) {
  78. main_result = wasm_runtime_get_wasi_exit_code(module_inst);
  79. }
  80. else {
  81. printf("call wasm function main failed. error: %s\n",
  82. wasm_runtime_get_exception(module_inst));
  83. goto fail;
  84. }
  85. fail:
  86. if (exec_env)
  87. wasm_runtime_destroy_exec_env(exec_env);
  88. if (module_inst)
  89. wasm_runtime_deinstantiate(module_inst);
  90. if (module)
  91. wasm_runtime_unload(module);
  92. if (buffer)
  93. BH_FREE(buffer);
  94. wasm_runtime_destroy();
  95. return main_result;
  96. }