aot_emit_control_test.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. class aot_emit_control_test_suite : public testing::Test
  15. {
  16. protected:
  17. // You should make the members protected s.t. they can be
  18. // accessed from sub-classes.
  19. // virtual void SetUp() will be called before each test is run. You
  20. // should define it if you need to initialize the variables.
  21. // Otherwise, this can be skipped.
  22. virtual void SetUp() {}
  23. static void SetUpTestCase()
  24. {
  25. CWD = get_test_binary_dir();
  26. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  27. }
  28. // virtual void TearDown() will be called after each test is run.
  29. // You should define it if there is cleanup work to do. Otherwise,
  30. // you don't have to provide it.
  31. //
  32. virtual void TearDown() {}
  33. static void TearDownTestCase() { free(WASM_FILE); }
  34. WAMRRuntimeRAII<512 * 1024> runtime;
  35. };
  36. TEST_F(aot_emit_control_test_suite, check_suspend_flags)
  37. {
  38. const char *wasm_file = WASM_FILE;
  39. unsigned int wasm_file_size = 0;
  40. unsigned char *wasm_file_buf = nullptr;
  41. char error_buf[128] = { 0 };
  42. wasm_module_t wasm_module = nullptr;
  43. struct AOTCompData *comp_data = nullptr;
  44. struct AOTCompContext *comp_ctx = nullptr;
  45. AOTFuncContext *func_ctx = nullptr;
  46. AOTCompOption option = { 0 };
  47. char out_file_name[] = "out_file_name_test";
  48. option.opt_level = 3;
  49. option.size_level = 3;
  50. option.output_format = AOT_FORMAT_FILE;
  51. /* default value, enable or disable depends on the platform */
  52. option.bounds_checks = 2;
  53. option.enable_simd = true;
  54. option.enable_aux_stack_check = true;
  55. option.enable_bulk_memory = true;
  56. option.enable_ref_types = true;
  57. wasm_file_buf =
  58. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  59. EXPECT_NE(wasm_file_buf, nullptr);
  60. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  61. sizeof(error_buf));
  62. EXPECT_NE(wasm_module, nullptr);
  63. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  64. EXPECT_NE(nullptr, comp_data);
  65. comp_ctx = aot_create_comp_context(comp_data, &option);
  66. EXPECT_NE(comp_ctx, nullptr);
  67. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  68. func_ctx = comp_ctx->func_ctxes[1];
  69. EXPECT_EQ(true, check_suspend_flags(comp_ctx, func_ctx, false));
  70. }
  71. TEST_F(aot_emit_control_test_suite, aot_compile_op_block)
  72. {
  73. const char *wasm_file = WASM_FILE;
  74. unsigned int wasm_file_size = 0;
  75. unsigned char *wasm_file_buf = nullptr;
  76. char error_buf[128] = { 0 };
  77. wasm_module_t wasm_module = nullptr;
  78. struct AOTCompData *comp_data = nullptr;
  79. struct AOTCompContext *comp_ctx = nullptr;
  80. AOTFuncContext *func_ctx = nullptr;
  81. AOTCompOption option = { 0 };
  82. char out_file_name[] = "out_file_name_test";
  83. option.opt_level = 3;
  84. option.size_level = 3;
  85. option.output_format = AOT_FORMAT_FILE;
  86. /* default value, enable or disable depends on the platform */
  87. option.bounds_checks = 2;
  88. option.enable_simd = true;
  89. option.enable_aux_stack_check = true;
  90. option.enable_bulk_memory = true;
  91. option.enable_ref_types = true;
  92. wasm_file_buf =
  93. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  94. EXPECT_NE(wasm_file_buf, nullptr);
  95. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  96. sizeof(error_buf));
  97. EXPECT_NE(wasm_module, nullptr);
  98. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  99. EXPECT_NE(nullptr, comp_data);
  100. comp_ctx = aot_create_comp_context(comp_data, &option);
  101. EXPECT_NE(comp_ctx, nullptr);
  102. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  103. func_ctx = comp_ctx->func_ctxes[1];
  104. func_ctx->block_stack.block_list_end = nullptr;
  105. EXPECT_EQ(false, aot_compile_op_block(comp_ctx, func_ctx, nullptr, nullptr,
  106. 0, 0, nullptr, 0, nullptr));
  107. }
  108. TEST_F(aot_emit_control_test_suite, aot_compile_op_else)
  109. {
  110. const char *wasm_file = WASM_FILE;
  111. unsigned int wasm_file_size = 0;
  112. unsigned char *wasm_file_buf = nullptr;
  113. char error_buf[128] = { 0 };
  114. wasm_module_t wasm_module = nullptr;
  115. struct AOTCompData *comp_data = nullptr;
  116. struct AOTCompContext *comp_ctx = nullptr;
  117. AOTFuncContext *func_ctx = nullptr;
  118. AOTCompOption option = { 0 };
  119. char out_file_name[] = "out_file_name_test";
  120. option.opt_level = 3;
  121. option.size_level = 3;
  122. option.output_format = AOT_FORMAT_FILE;
  123. /* default value, enable or disable depends on the platform */
  124. option.bounds_checks = 2;
  125. option.enable_simd = true;
  126. option.enable_aux_stack_check = true;
  127. option.enable_bulk_memory = true;
  128. option.enable_ref_types = true;
  129. wasm_file_buf =
  130. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  131. EXPECT_NE(wasm_file_buf, nullptr);
  132. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  133. sizeof(error_buf));
  134. EXPECT_NE(wasm_module, nullptr);
  135. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  136. EXPECT_NE(nullptr, comp_data);
  137. comp_ctx = aot_create_comp_context(comp_data, &option);
  138. EXPECT_NE(comp_ctx, nullptr);
  139. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  140. func_ctx = comp_ctx->func_ctxes[1];
  141. func_ctx->block_stack.block_list_end = nullptr;
  142. EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
  143. AOTBlock block_list_end_test;
  144. block_list_end_test.label_type = LABEL_TYPE_FUNCTION;
  145. func_ctx->block_stack.block_list_end = &block_list_end_test;
  146. EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
  147. block_list_end_test.label_type = LABEL_TYPE_IF;
  148. block_list_end_test.llvm_else_block = nullptr;
  149. EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
  150. }