aot_emit_aot_file_test.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "test_helper.h"
  6. #include "gtest/gtest.h"
  7. #include "wasm_export.h"
  8. #include "aot_export.h"
  9. #include "bh_read_file.h"
  10. static std::string CWD;
  11. static std::string MAIN_WASM = "/main.wasm";
  12. static char *WASM_FILE;
  13. static std::string
  14. get_binary_path()
  15. {
  16. char cwd[1024];
  17. memset(cwd, 0, 1024);
  18. if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
  19. }
  20. char *path_end = strrchr(cwd, '/');
  21. if (path_end != NULL) {
  22. *path_end = '\0';
  23. }
  24. return std::string(cwd);
  25. }
  26. extern "C" {
  27. uint8 *
  28. aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  29. uint32 *p_aot_file_size);
  30. }
  31. class aot_emit_aot_file_test_suite : public testing::Test
  32. {
  33. protected:
  34. // You should make the members protected s.t. they can be
  35. // accessed from sub-classes.
  36. // virtual void SetUp() will be called before each test is run. You
  37. // should define it if you need to initialize the variables.
  38. // Otherwise, this can be skipped.
  39. virtual void SetUp() {}
  40. static void SetUpTestCase()
  41. {
  42. CWD = get_binary_path();
  43. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  44. }
  45. // virtual void TearDown() will be called after each test is run.
  46. // You should define it if there is cleanup work to do. Otherwise,
  47. // you don't have to provide it.
  48. //
  49. virtual void TearDown() {}
  50. static void TearDownTestCase() { free(WASM_FILE); }
  51. WAMRRuntimeRAII<512 * 1024> runtime;
  52. };
  53. TEST_F(aot_emit_aot_file_test_suite, aot_emit_aot_file)
  54. {
  55. const char *wasm_file = WASM_FILE;
  56. unsigned int wasm_file_size = 0;
  57. unsigned char *wasm_file_buf = nullptr;
  58. char error_buf[128] = { 0 };
  59. wasm_module_t wasm_module = nullptr;
  60. aot_comp_data_t comp_data = nullptr;
  61. aot_comp_context_t comp_ctx = nullptr;
  62. AOTCompOption option = { 0 };
  63. char out_file_name[] = "test.aot";
  64. option.opt_level = 3;
  65. option.size_level = 3;
  66. option.output_format = AOT_FORMAT_FILE;
  67. /* default value, enable or disable depends on the platform */
  68. option.bounds_checks = 2;
  69. option.enable_simd = false;
  70. option.enable_aux_stack_check = false;
  71. option.enable_bulk_memory = false;
  72. option.enable_ref_types = false;
  73. wasm_file_buf =
  74. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  75. EXPECT_NE(wasm_file_buf, nullptr);
  76. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  77. sizeof(error_buf));
  78. EXPECT_NE(wasm_module, nullptr);
  79. comp_data = aot_create_comp_data(wasm_module, NULL, false);
  80. EXPECT_NE(nullptr, comp_data);
  81. comp_ctx = aot_create_comp_context(comp_data, &option);
  82. EXPECT_NE(comp_ctx, nullptr);
  83. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  84. EXPECT_EQ(false, aot_emit_aot_file(comp_ctx, comp_data, nullptr));
  85. }