free_buffer_early.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2024 Amazon.com, Inc. or its affiliates. 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. #include "bh_getopt.h"
  8. void
  9. my_log(uint32 log_level, const char *file, int line, const char *fmt, ...)
  10. {
  11. char buf[200];
  12. snprintf(buf, sizeof(buf), "[WamrLogger] %s\n", fmt);
  13. va_list ap;
  14. va_start(ap, fmt);
  15. vprintf(buf, ap);
  16. va_end(ap);
  17. }
  18. int
  19. my_vprintf(const char *format, va_list ap)
  20. {
  21. return vprintf(format, ap);
  22. }
  23. void
  24. print_usage(void)
  25. {
  26. fprintf(stdout, "Options:\r\n");
  27. fprintf(stdout, " -f [path of wasm file] \n");
  28. }
  29. int
  30. main(int argc, char *argv_main[])
  31. {
  32. static char global_heap_buf[512 * 1024];
  33. char *buffer = NULL, error_buf[128];
  34. int opt;
  35. char *wasm_path = NULL;
  36. bool success;
  37. wasm_module_t module = NULL;
  38. wasm_module_inst_t module_inst = NULL;
  39. uint32 buf_size, stack_size = 8092, heap_size = 8092;
  40. LoadArgs load_args = { 0 };
  41. RuntimeInitArgs init_args;
  42. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  43. while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
  44. switch (opt) {
  45. case 'f':
  46. wasm_path = optarg;
  47. break;
  48. case 'h':
  49. print_usage();
  50. return 0;
  51. case '?':
  52. print_usage();
  53. return 0;
  54. }
  55. }
  56. if (optind == 1) {
  57. print_usage();
  58. return 0;
  59. }
  60. init_args.mem_alloc_type = Alloc_With_Pool;
  61. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  62. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  63. if (!wasm_runtime_full_init(&init_args)) {
  64. printf("Init runtime environment failed.\n");
  65. return -1;
  66. }
  67. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  68. if (!buffer) {
  69. printf("Open wasm app file [%s] failed.\n", wasm_path);
  70. goto fail;
  71. }
  72. load_args.wasm_binary_freeable = true;
  73. module = wasm_runtime_load_ex((uint8 *)buffer, buf_size, &load_args,
  74. error_buf, sizeof(error_buf));
  75. if (!module) {
  76. printf("Load wasm module failed. error: %s\n", error_buf);
  77. goto fail;
  78. }
  79. if (wasm_runtime_is_underlying_binary_freeable(module)) {
  80. printf("Able to free wasm binary buffer.\n");
  81. wasm_runtime_free(buffer);
  82. buffer = NULL;
  83. }
  84. module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
  85. error_buf, sizeof(error_buf));
  86. if (!module_inst) {
  87. printf("Instantiate wasm module failed. error: %s.\n", error_buf);
  88. goto fail;
  89. }
  90. char *args[1] = { "3" };
  91. success = wasm_application_execute_func(module_inst, "mul7", 1, args);
  92. if (!success) {
  93. printf("Unable to execute function.\n");
  94. goto fail;
  95. }
  96. fail:
  97. if (module_inst)
  98. wasm_runtime_deinstantiate(module_inst);
  99. if (module)
  100. wasm_runtime_unload(module);
  101. if (buffer)
  102. wasm_runtime_free(buffer);
  103. wasm_runtime_destroy();
  104. return 0;
  105. }