aot_compiler_test.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. char *
  28. aot_generate_tempfile_name(const char *prefix, const char *extension,
  29. char *buffer, uint32 len);
  30. }
  31. class aot_compiler_test_suit : 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 varaibles.
  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. void
  54. test_aot_emit_object_file_with_option(AOTCompOption *option_ptr)
  55. {
  56. const char *wasm_file = WASM_FILE;
  57. unsigned int wasm_file_size = 0;
  58. unsigned char *wasm_file_buf = nullptr;
  59. char error_buf[128] = { 0 };
  60. wasm_module_t wasm_module = nullptr;
  61. aot_comp_data_t comp_data = nullptr;
  62. aot_comp_context_t comp_ctx = nullptr;
  63. char out_file_name[] = "test.aot";
  64. wasm_file_buf =
  65. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  66. EXPECT_NE(wasm_file_buf, nullptr);
  67. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  68. sizeof(error_buf));
  69. EXPECT_NE(wasm_module, nullptr);
  70. comp_data = aot_create_comp_data(wasm_module, NULL, false);
  71. EXPECT_NE(nullptr, comp_data);
  72. comp_ctx = aot_create_comp_context(comp_data, option_ptr);
  73. EXPECT_NE(comp_ctx, nullptr);
  74. EXPECT_STREQ(aot_get_last_error(), "");
  75. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  76. EXPECT_TRUE(aot_emit_object_file(comp_ctx, out_file_name));
  77. }
  78. TEST_F(aot_compiler_test_suit, aot_emit_object_file)
  79. {
  80. AOTCompOption option = { 0 };
  81. uint32_t i = 0;
  82. option.opt_level = 3;
  83. option.size_level = 3;
  84. option.output_format = AOT_FORMAT_FILE;
  85. option.bounds_checks = 2;
  86. option.enable_simd = true;
  87. option.enable_aux_stack_check = true;
  88. option.enable_bulk_memory = true;
  89. option.enable_ref_types = true;
  90. // Test opt_level in range from 0 to 3.
  91. for (i = 0; i <= 3; i++) {
  92. option.opt_level = i;
  93. test_aot_emit_object_file_with_option(&option);
  94. }
  95. // Test size_level in range from 0 to 3.
  96. option.opt_level = 3;
  97. for (i = 0; i <= 3; i++) {
  98. option.size_level = i;
  99. test_aot_emit_object_file_with_option(&option);
  100. }
  101. // Test output_format in range from AOT_FORMAT_FILE to AOT_LLVMIR_OPT_FILE.
  102. option.size_level = 3;
  103. for (i = AOT_FORMAT_FILE; i <= AOT_LLVMIR_OPT_FILE; i++) {
  104. option.output_format = i;
  105. test_aot_emit_object_file_with_option(&option);
  106. }
  107. // Test bounds_checks in range 0 to 2.
  108. option.output_format = AOT_FORMAT_FILE;
  109. for (i = 0; i <= 2; i++) {
  110. option.bounds_checks = i;
  111. test_aot_emit_object_file_with_option(&option);
  112. }
  113. // Test all enable option is false.
  114. option.bounds_checks = 2;
  115. option.enable_simd = false;
  116. option.enable_aux_stack_check = false;
  117. option.enable_bulk_memory = false;
  118. option.enable_ref_types = false;
  119. test_aot_emit_object_file_with_option(&option);
  120. }
  121. TEST_F(aot_compiler_test_suit, aot_emit_llvm_file)
  122. {
  123. const char *wasm_file = WASM_FILE;
  124. unsigned int wasm_file_size = 0;
  125. unsigned char *wasm_file_buf = nullptr;
  126. char error_buf[128] = { 0 };
  127. wasm_module_t wasm_module = nullptr;
  128. aot_comp_data_t comp_data = nullptr;
  129. aot_comp_context_t comp_ctx = nullptr;
  130. AOTCompOption option = { 0 };
  131. char out_file_name[] = "out_file_name_test";
  132. option.opt_level = 3;
  133. option.size_level = 3;
  134. option.output_format = AOT_FORMAT_FILE;
  135. /* default value, enable or disable depends on the platform */
  136. option.bounds_checks = 2;
  137. option.enable_simd = true;
  138. option.enable_aux_stack_check = true;
  139. option.enable_bulk_memory = true;
  140. option.enable_ref_types = true;
  141. wasm_file_buf =
  142. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  143. EXPECT_NE(wasm_file_buf, nullptr);
  144. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  145. sizeof(error_buf));
  146. EXPECT_NE(wasm_module, nullptr);
  147. comp_data = aot_create_comp_data(wasm_module, NULL, false);
  148. EXPECT_NE(nullptr, comp_data);
  149. comp_ctx = aot_create_comp_context(comp_data, &option);
  150. EXPECT_NE(comp_ctx, nullptr);
  151. EXPECT_STREQ(aot_get_last_error(), "");
  152. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  153. EXPECT_EQ(true, aot_emit_llvm_file(comp_ctx, out_file_name));
  154. }
  155. TEST_F(aot_compiler_test_suit, aot_generate_tempfile_name)
  156. {
  157. char obj_file_name[64];
  158. // Test common case.
  159. aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name,
  160. sizeof(obj_file_name));
  161. EXPECT_NE(nullptr, strstr(obj_file_name, ".o"));
  162. // Test abnormal cases.
  163. EXPECT_EQ(nullptr,
  164. aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name, 0));
  165. char obj_file_name_1[20];
  166. EXPECT_EQ(nullptr, aot_generate_tempfile_name(
  167. "wamrc-obj", "12345678901234567890", obj_file_name_1,
  168. sizeof(obj_file_name_1)));
  169. }