binary_file.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "binary_file.h"
  6. #include <cctype>
  7. #include <cstring>
  8. #include "analyzer_error.h"
  9. #if HAVE_ALLOCA
  10. #include <alloca.h>
  11. #endif
  12. namespace analyzer {
  13. BinaryFile::BinaryFile(const char *file_name)
  14. : file_name_(file_name)
  15. , file_data_(NULL)
  16. , file_size_(0)
  17. , current_pos_(0)
  18. , module_(NULL)
  19. {
  20. memset(&mem_conspn_, 0, sizeof(WASMModuleMemConsumption));
  21. }
  22. BinaryFile::~BinaryFile()
  23. {
  24. if (module_) {
  25. wasm_runtime_unload(module_);
  26. wasm_runtime_free(file_data_);
  27. wasm_runtime_destroy();
  28. }
  29. }
  30. Result
  31. BinaryFile::ReadModule()
  32. {
  33. char error_buf[128];
  34. #if WASM_ENABLE_GC != 0
  35. uint32_t gc_heap_size = GC_HEAP_SIZE_DEFAULT;
  36. #endif
  37. uint32_t buf_size, stack_size = DEFAULT_WASM_STACK_SIZE,
  38. heap_size = GC_HEAP_SIZE_DEFAULT;
  39. RuntimeInitArgs init_args;
  40. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  41. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  42. static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
  43. init_args.mem_alloc_type = Alloc_With_Pool;
  44. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  45. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  46. #else
  47. init_args.mem_alloc_type = Alloc_With_Allocator;
  48. init_args.mem_alloc_option.allocator.malloc_func = (void *)malloc;
  49. init_args.mem_alloc_option.allocator.realloc_func = (void *)realloc;
  50. init_args.mem_alloc_option.allocator.free_func = (void *)free;
  51. #endif
  52. /* initialize runtime environment */
  53. if (!wasm_runtime_full_init(&init_args)) {
  54. printf("Init runtime environment failed.\n");
  55. return Result::Error;
  56. }
  57. file_data_ = (uint8 *)bh_read_file_to_buffer(file_name_, &file_size_);
  58. if (!file_data_) {
  59. printf("Open Binary file [%s] failed.\n", file_name_);
  60. wasm_runtime_destroy();
  61. return Result::Error;
  62. }
  63. module_ =
  64. wasm_runtime_load(file_data_, file_size_, error_buf, sizeof(error_buf));
  65. if (!module_) {
  66. printf("Load Binary module failed. error: %s\n", error_buf);
  67. wasm_runtime_free(file_data_);
  68. wasm_runtime_destroy();
  69. return Result::Error;
  70. }
  71. return Result::Ok;
  72. }
  73. Result
  74. BinaryFile::Scan()
  75. {
  76. return Result::Ok;
  77. }
  78. void ANALYZER_PRINTF_FORMAT(2, 3) BinaryFile::PrintError(const char *format,
  79. ...)
  80. {
  81. ErrorLevel error_level = ErrorLevel::Error;
  82. ANALYZER_SNPRINTF_ALLOCA(buffer, length, format);
  83. Error error(error_level, buffer);
  84. fprintf(stderr, "%07" PRIzx ": %s: %s\n", current_pos_,
  85. GetErrorLevelName(error_level), buffer);
  86. }
  87. Result
  88. BinaryFile::UpdateCurrentPos(uint32_t steps)
  89. {
  90. if (current_pos_ + steps > file_size_) {
  91. return Result::Error;
  92. }
  93. current_pos_ += steps;
  94. return Result::Ok;
  95. }
  96. } // namespace analyzer