aot_llvm_test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_compiler.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. class aot_llvm_test_suite : public testing::Test
  27. {
  28. protected:
  29. // You should make the members protected s.t. they can be
  30. // accessed from sub-classes.
  31. // virtual void SetUp() will be called before each test is run. You
  32. // should define it if you need to initialize the variables.
  33. // Otherwise, this can be skipped.
  34. virtual void SetUp() {}
  35. static void SetUpTestCase()
  36. {
  37. CWD = get_binary_path();
  38. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  39. }
  40. // virtual void TearDown() will be called after each test is run.
  41. // You should define it if there is cleanup work to do. Otherwise,
  42. // you don't have to provide it.
  43. //
  44. virtual void TearDown() {}
  45. static void TearDownTestCase() { free(WASM_FILE); }
  46. WAMRRuntimeRAII<512 * 1024> runtime;
  47. };
  48. TEST_F(aot_llvm_test_suite, aot_functions)
  49. {
  50. const char *wasm_file = WASM_FILE;
  51. unsigned int wasm_file_size = 0;
  52. unsigned char *wasm_file_buf = nullptr;
  53. char error_buf[128] = { 0 };
  54. wasm_module_t wasm_module = nullptr;
  55. struct AOTCompData *comp_data = nullptr;
  56. struct AOTCompContext *comp_ctx = nullptr;
  57. AOTCompOption option = { 0 };
  58. AOTFuncContext *func_ctx = nullptr;
  59. WASMValue wasm_value;
  60. LLVMTypeRef param_types[1];
  61. option.opt_level = 3;
  62. option.size_level = 3;
  63. option.output_format = AOT_FORMAT_FILE;
  64. /* default value, enable or disable depends on the platform */
  65. option.bounds_checks = 2;
  66. option.enable_simd = true;
  67. option.enable_aux_stack_check = true;
  68. option.enable_bulk_memory = true;
  69. option.enable_ref_types = true;
  70. wasm_file_buf =
  71. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  72. EXPECT_NE(wasm_file_buf, nullptr);
  73. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  74. sizeof(error_buf));
  75. EXPECT_NE(wasm_module, nullptr);
  76. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  77. EXPECT_NE(nullptr, comp_data);
  78. comp_ctx = aot_create_comp_context(comp_data, &option);
  79. EXPECT_NE(comp_ctx, nullptr);
  80. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  81. func_ctx = comp_ctx->func_ctxes[1];
  82. param_types[0] = F64_TYPE;
  83. EXPECT_TRUE(aot_call_llvm_intrinsic(comp_ctx, func_ctx, "f32_demote_f64",
  84. F32_TYPE, param_types, 0));
  85. /* Test function aot_get_native_symbol_index. */
  86. AOTNativeSymbol elem_insert_1;
  87. elem_insert_1.index = -1;
  88. bh_list_insert(&comp_ctx->native_symbols, &elem_insert_1);
  89. AOTNativeSymbol elem_insert_2;
  90. strcpy(elem_insert_2.symbol, "f64#_test");
  91. elem_insert_2.index = -1;
  92. bh_list_insert(&comp_ctx->native_symbols, &elem_insert_2);
  93. comp_ctx->pointer_size = sizeof(uint32);
  94. strcpy(comp_ctx->target_arch, "i386");
  95. EXPECT_NE(-1, aot_get_native_symbol_index(comp_ctx, "f64#_test"));
  96. }
  97. TEST_F(aot_llvm_test_suite, wasm_type_to_llvm_type) {}
  98. TEST_F(aot_llvm_test_suite, aot_build_zero_function_ret)
  99. {
  100. const char *wasm_file = WASM_FILE;
  101. unsigned int wasm_file_size = 0;
  102. unsigned char *wasm_file_buf = nullptr;
  103. char error_buf[128] = { 0 };
  104. wasm_module_t wasm_module = nullptr;
  105. struct AOTCompData *comp_data = nullptr;
  106. struct AOTCompContext *comp_ctx = nullptr;
  107. AOTCompOption option = { 0 };
  108. AOTFuncContext *func_ctx = nullptr;
  109. AOTFuncType func_type;
  110. option.opt_level = 3;
  111. option.size_level = 3;
  112. option.output_format = AOT_FORMAT_FILE;
  113. /* default value, enable or disable depends on the platform */
  114. option.bounds_checks = 2;
  115. option.enable_simd = true;
  116. option.enable_aux_stack_check = true;
  117. option.enable_bulk_memory = true;
  118. option.enable_ref_types = false;
  119. wasm_file_buf =
  120. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  121. EXPECT_NE(wasm_file_buf, nullptr);
  122. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  123. sizeof(error_buf));
  124. EXPECT_NE(wasm_module, nullptr);
  125. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  126. EXPECT_NE(nullptr, comp_data);
  127. comp_ctx = aot_create_comp_context(comp_data, &option);
  128. EXPECT_NE(comp_ctx, nullptr);
  129. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  130. func_ctx = comp_ctx->func_ctxes[1];
  131. func_type.result_count = 1;
  132. func_type.param_count = 0;
  133. func_type.types[func_type.param_count] = VALUE_TYPE_I32;
  134. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  135. func_type.types[func_type.param_count] = VALUE_TYPE_I64;
  136. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  137. func_type.types[func_type.param_count] = VALUE_TYPE_F32;
  138. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  139. func_type.types[func_type.param_count] = VALUE_TYPE_F64;
  140. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  141. func_type.types[func_type.param_count] = VALUE_TYPE_V128;
  142. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  143. /* THe current optimization, if not actually use ref_types in wasm module,
  144. * it will set to false, so test false condition */
  145. func_type.types[func_type.param_count] = VALUE_TYPE_FUNCREF;
  146. EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
  147. ".*");
  148. func_type.types[func_type.param_count] = VALUE_TYPE_EXTERNREF;
  149. EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
  150. ".*");
  151. func_type.types[func_type.param_count] = 0xFF;
  152. EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
  153. ".*");
  154. }
  155. TEST_F(aot_llvm_test_suite, aot_destroy_comp_context)
  156. {
  157. const char *wasm_file = WASM_FILE;
  158. unsigned int wasm_file_size = 0;
  159. unsigned char *wasm_file_buf = nullptr;
  160. char error_buf[128] = { 0 };
  161. wasm_module_t wasm_module = nullptr;
  162. struct AOTCompData *comp_data = nullptr;
  163. struct AOTCompContext *comp_ctx = nullptr;
  164. AOTCompOption option = { 0 };
  165. AOTFuncContext *func_ctx = nullptr;
  166. AOTFuncType func_type;
  167. option.opt_level = 3;
  168. option.size_level = 3;
  169. option.output_format = AOT_FORMAT_FILE;
  170. /* default value, enable or disable depends on the platform */
  171. option.bounds_checks = 2;
  172. option.enable_simd = true;
  173. option.enable_aux_stack_check = true;
  174. option.enable_bulk_memory = true;
  175. option.enable_ref_types = true;
  176. wasm_file_buf =
  177. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  178. EXPECT_NE(wasm_file_buf, nullptr);
  179. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  180. sizeof(error_buf));
  181. EXPECT_NE(wasm_module, nullptr);
  182. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  183. EXPECT_NE(nullptr, comp_data);
  184. comp_ctx = aot_create_comp_context(comp_data, &option);
  185. EXPECT_NE(comp_ctx, nullptr);
  186. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  187. AOTNativeSymbol elem_insert_1;
  188. elem_insert_1.index = -1;
  189. bh_list_insert(&comp_ctx->native_symbols, &elem_insert_1);
  190. aot_destroy_comp_context(comp_ctx);
  191. aot_destroy_comp_context(nullptr);
  192. }
  193. TEST_F(aot_llvm_test_suite, aot_create_comp_context)
  194. {
  195. const char *wasm_file = WASM_FILE;
  196. unsigned int wasm_file_size = 0;
  197. unsigned char *wasm_file_buf = nullptr;
  198. char error_buf[128] = { 0 };
  199. wasm_module_t wasm_module = nullptr;
  200. struct AOTCompData *comp_data = nullptr;
  201. struct AOTCompContext *comp_ctx = nullptr;
  202. AOTCompOption option = { 0 };
  203. option.opt_level = 3;
  204. option.size_level = 3;
  205. option.output_format = AOT_FORMAT_FILE;
  206. /* default value, enable or disable depends on the platform */
  207. option.bounds_checks = 2;
  208. option.enable_simd = true;
  209. option.enable_aux_stack_check = true;
  210. option.enable_bulk_memory = true;
  211. option.enable_ref_types = true;
  212. wasm_file_buf =
  213. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  214. EXPECT_NE(wasm_file_buf, nullptr);
  215. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  216. sizeof(error_buf));
  217. EXPECT_NE(wasm_module, nullptr);
  218. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  219. EXPECT_NE(nullptr, comp_data);
  220. option.enable_thread_mgr = true;
  221. option.enable_tail_call = true;
  222. option.is_indirect_mode = true;
  223. option.disable_llvm_intrinsics = true;
  224. option.disable_llvm_lto = true;
  225. option.is_jit_mode = true;
  226. option.target_arch = (char *)"arm";
  227. comp_ctx = aot_create_comp_context(comp_data, &option);
  228. EXPECT_NE(comp_ctx, nullptr);
  229. option.output_format = 100;
  230. comp_ctx = aot_create_comp_context(comp_data, &option);
  231. // Test every target_arch.
  232. option.is_jit_mode = false;
  233. option.target_arch = (char *)"arm";
  234. comp_ctx = aot_create_comp_context(comp_data, &option);
  235. option.target_arch = (char *)"armeb";
  236. comp_ctx = aot_create_comp_context(comp_data, &option);
  237. option.target_arch = (char *)"thumb";
  238. comp_ctx = aot_create_comp_context(comp_data, &option);
  239. option.target_arch = (char *)"thumbeb";
  240. comp_ctx = aot_create_comp_context(comp_data, &option);
  241. option.target_arch = (char *)"aarch64";
  242. comp_ctx = aot_create_comp_context(comp_data, &option);
  243. option.target_arch = (char *)"aarch64_be";
  244. comp_ctx = aot_create_comp_context(comp_data, &option);
  245. option.target_arch = (char *)"help";
  246. comp_ctx = aot_create_comp_context(comp_data, &option);
  247. // Test every target_abi.
  248. option.target_arch = (char *)"arm";
  249. option.target_abi = (char *)"test";
  250. comp_ctx = aot_create_comp_context(comp_data, &option);
  251. option.target_abi = (char *)"help";
  252. comp_ctx = aot_create_comp_context(comp_data, &option);
  253. option.target_abi = (char *)"msvc";
  254. option.target_arch = (char *)"i386";
  255. comp_ctx = aot_create_comp_context(comp_data, &option);
  256. option.cpu_features = (char *)"test";
  257. comp_ctx = aot_create_comp_context(comp_data, &option);
  258. option.is_sgx_platform = true;
  259. comp_ctx = aot_create_comp_context(comp_data, &option);
  260. comp_data->func_count = 0;
  261. comp_ctx = aot_create_comp_context(comp_data, &option);
  262. }