main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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;
  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. uint32_t wasm_buffer = 0;
  26. RuntimeInitArgs init_args;
  27. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  28. while ((opt = getopt(argc, argv_main, "hf:d:")) != -1) {
  29. switch (opt) {
  30. case 'f':
  31. wasm_path = optarg;
  32. break;
  33. case 'd':
  34. wasi_dir = optarg;
  35. break;
  36. case 'h':
  37. print_usage();
  38. return 0;
  39. case '?':
  40. print_usage();
  41. return 0;
  42. }
  43. }
  44. if (wasm_path == NULL || wasi_dir == NULL) {
  45. print_usage();
  46. return 0;
  47. }
  48. init_args.mem_alloc_type = Alloc_With_Pool;
  49. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  50. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  51. if (!wasm_runtime_full_init(&init_args)) {
  52. printf("Init runtime environment failed.\n");
  53. return -1;
  54. }
  55. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  56. if (!buffer) {
  57. printf("Open wasm app file [%s] failed.\n", wasm_path);
  58. goto fail;
  59. }
  60. module = wasm_runtime_load(buffer, buf_size, error_buf, sizeof(error_buf));
  61. if (!module) {
  62. printf("Load wasm module failed. error: %s\n", error_buf);
  63. goto fail;
  64. }
  65. wasm_runtime_set_wasi_args_ex(module, &wasi_dir, 1, NULL, 0, NULL, 0, NULL,
  66. 0, 0, 1, 2);
  67. module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
  68. error_buf, sizeof(error_buf));
  69. if (!module_inst) {
  70. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  71. goto fail;
  72. }
  73. exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
  74. if (!exec_env) {
  75. printf("Create wasm execution environment failed.\n");
  76. goto fail;
  77. }
  78. if (wasm_application_execute_main(module_inst, 0, NULL)) {
  79. printf("Main wasm function successfully finished.\n");
  80. }
  81. else {
  82. printf("call wasm function main failed. error: %s\n",
  83. wasm_runtime_get_exception(module_inst));
  84. goto fail;
  85. }
  86. fail:
  87. if (exec_env)
  88. wasm_runtime_destroy_exec_env(exec_env);
  89. if (module_inst) {
  90. if (wasm_buffer)
  91. wasm_runtime_module_free(module_inst, wasm_buffer);
  92. wasm_runtime_deinstantiate(module_inst);
  93. }
  94. if (module)
  95. wasm_runtime_unload(module);
  96. if (buffer)
  97. BH_FREE(buffer);
  98. wasm_runtime_destroy();
  99. return 0;
  100. }