free_buffer_early.c 3.1 KB

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