aot_emit_control_test.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.h"
  9. #include "aot_llvm.h"
  10. #include "aot_emit_control.h"
  11. static std::string CWD;
  12. static std::string MAIN_WASM = "/main.wasm";
  13. static char *WASM_FILE;
  14. static std::string
  15. get_binary_path()
  16. {
  17. char cwd[1024];
  18. memset(cwd, 0, 1024);
  19. if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
  20. }
  21. char *path_end = strrchr(cwd, '/');
  22. if (path_end != NULL) {
  23. *path_end = '\0';
  24. }
  25. return std::string(cwd);
  26. }
  27. class aot_emit_control_test_suite : public testing::Test
  28. {
  29. protected:
  30. // You should make the members protected s.t. they can be
  31. // accessed from sub-classes.
  32. // virtual void SetUp() will be called before each test is run. You
  33. // should define it if you need to initialize the variables.
  34. // Otherwise, this can be skipped.
  35. virtual void SetUp() {}
  36. static void SetUpTestCase()
  37. {
  38. CWD = get_binary_path();
  39. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  40. }
  41. // virtual void TearDown() will be called after each test is run.
  42. // You should define it if there is cleanup work to do. Otherwise,
  43. // you don't have to provide it.
  44. //
  45. virtual void TearDown() {}
  46. static void TearDownTestCase() { free(WASM_FILE); }
  47. WAMRRuntimeRAII<512 * 1024> runtime;
  48. };
  49. TEST_F(aot_emit_control_test_suite, check_suspend_flags)
  50. {
  51. const char *wasm_file = WASM_FILE;
  52. unsigned int wasm_file_size = 0;
  53. unsigned char *wasm_file_buf = nullptr;
  54. char error_buf[128] = { 0 };
  55. wasm_module_t wasm_module = nullptr;
  56. struct AOTCompData *comp_data = nullptr;
  57. struct AOTCompContext *comp_ctx = nullptr;
  58. AOTFuncContext *func_ctx = nullptr;
  59. AOTCompOption option = { 0 };
  60. char out_file_name[] = "out_file_name_test";
  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. EXPECT_EQ(true, check_suspend_flags(comp_ctx, func_ctx, false));
  83. }
  84. TEST_F(aot_emit_control_test_suite, aot_compile_op_block)
  85. {
  86. const char *wasm_file = WASM_FILE;
  87. unsigned int wasm_file_size = 0;
  88. unsigned char *wasm_file_buf = nullptr;
  89. char error_buf[128] = { 0 };
  90. wasm_module_t wasm_module = nullptr;
  91. struct AOTCompData *comp_data = nullptr;
  92. struct AOTCompContext *comp_ctx = nullptr;
  93. AOTFuncContext *func_ctx = nullptr;
  94. AOTCompOption option = { 0 };
  95. char out_file_name[] = "out_file_name_test";
  96. option.opt_level = 3;
  97. option.size_level = 3;
  98. option.output_format = AOT_FORMAT_FILE;
  99. /* default value, enable or disable depends on the platform */
  100. option.bounds_checks = 2;
  101. option.enable_simd = true;
  102. option.enable_aux_stack_check = true;
  103. option.enable_bulk_memory = true;
  104. option.enable_ref_types = true;
  105. wasm_file_buf =
  106. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  107. EXPECT_NE(wasm_file_buf, nullptr);
  108. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  109. sizeof(error_buf));
  110. EXPECT_NE(wasm_module, nullptr);
  111. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  112. EXPECT_NE(nullptr, comp_data);
  113. comp_ctx = aot_create_comp_context(comp_data, &option);
  114. EXPECT_NE(comp_ctx, nullptr);
  115. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  116. func_ctx = comp_ctx->func_ctxes[1];
  117. func_ctx->block_stack.block_list_end = nullptr;
  118. EXPECT_EQ(false, aot_compile_op_block(comp_ctx, func_ctx, nullptr, nullptr,
  119. 0, 0, nullptr, 0, nullptr));
  120. }
  121. TEST_F(aot_emit_control_test_suite, aot_compile_op_else)
  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. struct AOTCompData *comp_data = nullptr;
  129. struct AOTCompContext *comp_ctx = nullptr;
  130. AOTFuncContext *func_ctx = nullptr;
  131. AOTCompOption option = { 0 };
  132. char out_file_name[] = "out_file_name_test";
  133. option.opt_level = 3;
  134. option.size_level = 3;
  135. option.output_format = AOT_FORMAT_FILE;
  136. /* default value, enable or disable depends on the platform */
  137. option.bounds_checks = 2;
  138. option.enable_simd = true;
  139. option.enable_aux_stack_check = true;
  140. option.enable_bulk_memory = true;
  141. option.enable_ref_types = true;
  142. wasm_file_buf =
  143. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  144. EXPECT_NE(wasm_file_buf, nullptr);
  145. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  146. sizeof(error_buf));
  147. EXPECT_NE(wasm_module, nullptr);
  148. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  149. EXPECT_NE(nullptr, comp_data);
  150. comp_ctx = aot_create_comp_context(comp_data, &option);
  151. EXPECT_NE(comp_ctx, nullptr);
  152. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  153. func_ctx = comp_ctx->func_ctxes[1];
  154. func_ctx->block_stack.block_list_end = nullptr;
  155. EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
  156. AOTBlock block_list_end_test;
  157. block_list_end_test.label_type = LABEL_TYPE_FUNCTION;
  158. func_ctx->block_stack.block_list_end = &block_list_end_test;
  159. EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
  160. block_list_end_test.label_type = LABEL_TYPE_IF;
  161. block_list_end_test.llvm_else_block = nullptr;
  162. EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
  163. }