aot_emit_table_test.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "bh_read_file.h"
  8. #include "aot_llvm.h"
  9. #include "aot_emit_table.h"
  10. static std::string CWD;
  11. static std::string MAIN_WASM = "/main.wasm";
  12. static char *WASM_FILE;
  13. class aot_emit_table_test_suite : public testing::Test
  14. {
  15. protected:
  16. // You should make the members protected s.t. they can be
  17. // accessed from sub-classes.
  18. // virtual void SetUp() will be called before each test is run. You
  19. // should define it if you need to initialize the variables.
  20. // Otherwise, this can be skipped.
  21. virtual void SetUp() {}
  22. static void SetUpTestCase()
  23. {
  24. CWD = get_test_binary_dir();
  25. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  26. }
  27. // virtual void TearDown() will be called after each test is run.
  28. // You should define it if there is cleanup work to do. Otherwise,
  29. // you don't have to provide it.
  30. //
  31. virtual void TearDown() {}
  32. static void TearDownTestCase() { free(WASM_FILE); }
  33. WAMRRuntimeRAII<512 * 1024> runtime;
  34. };
  35. TEST_F(aot_emit_table_test_suite, get_tbl_inst_offset)
  36. {
  37. const char *wasm_file = WASM_FILE;
  38. unsigned int wasm_file_size = 0;
  39. unsigned char *wasm_file_buf = nullptr;
  40. char error_buf[128] = { 0 };
  41. wasm_module_t wasm_module = nullptr;
  42. struct AOTCompData *comp_data = nullptr;
  43. struct AOTCompContext *comp_ctx = nullptr;
  44. AOTFuncContext *func_ctx = nullptr;
  45. AOTCompOption option = { 0 };
  46. option.opt_level = 3;
  47. option.size_level = 3;
  48. option.output_format = AOT_FORMAT_FILE;
  49. /* default value, enable or disable depends on the platform */
  50. option.bounds_checks = 2;
  51. option.enable_simd = true;
  52. option.enable_aux_stack_check = true;
  53. option.enable_bulk_memory = true;
  54. option.enable_ref_types = true;
  55. wasm_file_buf =
  56. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  57. EXPECT_NE(wasm_file_buf, nullptr);
  58. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  59. sizeof(error_buf));
  60. EXPECT_NE(wasm_module, nullptr);
  61. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  62. EXPECT_NE(nullptr, comp_data);
  63. comp_ctx = aot_create_comp_context(comp_data, &option);
  64. EXPECT_NE(comp_ctx, nullptr);
  65. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  66. func_ctx = comp_ctx->func_ctxes[1];
  67. EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 6));
  68. EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 1));
  69. EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 0));
  70. ((AOTCompData *)comp_ctx->comp_data)->import_table_count = 1;
  71. AOTImportTable import_tables_test;
  72. ((AOTCompData *)comp_ctx->comp_data)->import_tables = &import_tables_test;
  73. EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 6));
  74. }