wasm_running_modes_test.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "platform_common.h"
  8. #include "wasm_runtime_common.h"
  9. #include "bh_read_file.h"
  10. #include "wasm_runtime.h"
  11. #include "bh_platform.h"
  12. #include "wasm_export.h"
  13. #include "aot_runtime.h"
  14. namespace {
  15. std::string CWD;
  16. std::string TEST_WASM1 = "/hello.wasm";
  17. std::string TEST_WASM2 = "/mytest.wasm";
  18. char *WASM_FILE_1;
  19. char *WASM_FILE_2;
  20. std::vector<RunningMode> running_mode_supported = { Mode_Interp,
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. Mode_Fast_JIT,
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. Mode_LLVM_JIT,
  26. #endif
  27. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  28. && WASM_ENABLE_LAZY_JIT != 0
  29. Mode_Multi_Tier_JIT
  30. #endif
  31. };
  32. // To use a test fixture and Value Parameterized Tests,
  33. // derive a class from testing::TestWithParam.
  34. class wasm_running_modes_test_suite : public testing::TestWithParam<RunningMode>
  35. {
  36. private:
  37. std::string get_binary_path()
  38. {
  39. char cwd[1024];
  40. memset(cwd, 0, 1024);
  41. if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
  42. }
  43. char *path_end = strrchr(cwd, '/');
  44. if (path_end != NULL) {
  45. *path_end = '\0';
  46. }
  47. return std::string(cwd);
  48. }
  49. bool load_wasm_file(const char *wasm_file)
  50. {
  51. const char *file;
  52. unsigned char *wasm_file_buf;
  53. uint32 wasm_file_size;
  54. file = wasm_file;
  55. wasm_file_buf =
  56. (unsigned char *)bh_read_file_to_buffer(file, &wasm_file_size);
  57. if (!wasm_file_buf)
  58. goto fail;
  59. if (!(module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  60. error_buf, sizeof(error_buf)))) {
  61. printf("Load wasm module failed. error: %s\n", error_buf);
  62. goto fail;
  63. }
  64. return true;
  65. fail:
  66. if (module)
  67. wasm_runtime_unload(module);
  68. return false;
  69. }
  70. bool init_exec_env()
  71. {
  72. if (!(module_inst =
  73. wasm_runtime_instantiate(module, stack_size, heap_size,
  74. error_buf, sizeof(error_buf)))) {
  75. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  76. goto fail;
  77. }
  78. if (!(exec_env =
  79. wasm_runtime_create_exec_env(module_inst, stack_size))) {
  80. printf("Create wasm execution environment failed.\n");
  81. goto fail;
  82. }
  83. return true;
  84. fail:
  85. if (exec_env)
  86. wasm_runtime_destroy_exec_env(exec_env);
  87. if (module_inst)
  88. wasm_runtime_deinstantiate(module_inst);
  89. if (module)
  90. wasm_runtime_unload(module);
  91. return false;
  92. }
  93. void destroy_exec_env()
  94. {
  95. wasm_runtime_destroy_exec_env(exec_env);
  96. wasm_runtime_deinstantiate(module_inst);
  97. wasm_runtime_unload(module);
  98. }
  99. protected:
  100. void run_wasm_basic(
  101. char *filename, bool in_default_running_mode,
  102. RunningMode running_mode = static_cast<RunningMode>(Mode_Default))
  103. {
  104. bool ret;
  105. uint32_t wasm_argv[2];
  106. ret = load_wasm_file(filename);
  107. ASSERT_TRUE(ret);
  108. ret = init_exec_env();
  109. ASSERT_TRUE(ret);
  110. if (!in_default_running_mode) {
  111. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  112. ASSERT_TRUE(ret);
  113. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  114. }
  115. wasm_function_inst_t echo2xback_func =
  116. wasm_runtime_lookup_function(module_inst, "echo");
  117. ASSERT_TRUE(echo2xback_func != NULL);
  118. wasm_argv[0] = 5;
  119. ret = wasm_runtime_call_wasm(exec_env, echo2xback_func, 1, wasm_argv);
  120. ASSERT_TRUE(ret);
  121. ASSERT_EQ(10, wasm_argv[0]);
  122. destroy_exec_env();
  123. }
  124. void run_wasm_complex(char *filename1, char *filename2,
  125. RunningMode default_running_mode,
  126. RunningMode running_mode)
  127. {
  128. bool ret;
  129. uint32_t wasm_argv[2];
  130. /* run wasm file 1 in default running mode */
  131. wasm_runtime_set_default_running_mode(default_running_mode);
  132. ret = load_wasm_file(filename1);
  133. ASSERT_TRUE(ret);
  134. ret = init_exec_env();
  135. ASSERT_TRUE(ret);
  136. uint8_t *buffer, *buffer2;
  137. wasm_function_inst_t echo2xback_func, main;
  138. ASSERT_EQ(default_running_mode,
  139. wasm_runtime_get_running_mode(module_inst));
  140. echo2xback_func = wasm_runtime_lookup_function(module_inst, "echo");
  141. ASSERT_TRUE(echo2xback_func != NULL);
  142. wasm_argv[0] = 5;
  143. ret = wasm_runtime_call_wasm(exec_env, echo2xback_func, 1, wasm_argv);
  144. ASSERT_TRUE(ret);
  145. ASSERT_EQ(10, wasm_argv[0]);
  146. destroy_exec_env();
  147. /* run wasm file 2 in running_mode */
  148. ret = load_wasm_file(filename2);
  149. ASSERT_TRUE(ret);
  150. ret = init_exec_env();
  151. ASSERT_TRUE(ret);
  152. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  153. ASSERT_TRUE(ret);
  154. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  155. main = wasm_runtime_lookup_function(module_inst, "__main_argc_argv");
  156. ASSERT_TRUE(main != NULL);
  157. ret = wasm_runtime_call_wasm(exec_env, main, 2, wasm_argv);
  158. ASSERT_TRUE(ret);
  159. destroy_exec_env();
  160. }
  161. public:
  162. // If your test fixture defines SetUpTestSuite() or TearDownTestSuite()
  163. // they must be declared public rather than protected in order to use
  164. // TEST_P.
  165. // virtual void SetUp() will be called before each test is run. You
  166. // should define it if you need to initialize the variables.
  167. // Otherwise, this can be skipped.
  168. virtual void SetUp()
  169. {
  170. CWD = get_binary_path();
  171. WASM_FILE_1 = strdup((CWD + TEST_WASM1).c_str());
  172. WASM_FILE_2 = strdup((CWD + TEST_WASM2).c_str());
  173. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  174. init_args.mem_alloc_type = Alloc_With_Pool;
  175. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  176. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  177. ASSERT_EQ(wasm_runtime_full_init(&init_args), true);
  178. cleanup = true;
  179. }
  180. static void SetUpTestCase() {}
  181. // virtual void TearDown() will be called after each test is run.
  182. // You should define it if there is cleanup work to do. Otherwise,
  183. // you don't have to provide it.
  184. //
  185. virtual void TearDown()
  186. {
  187. if (cleanup) {
  188. wasm_runtime_destroy();
  189. cleanup = false;
  190. }
  191. free(WASM_FILE_1);
  192. free(WASM_FILE_2);
  193. }
  194. static void TearDownTestCase() {}
  195. std::string CWD;
  196. RuntimeInitArgs init_args;
  197. wasm_module_t module = NULL;
  198. wasm_module_inst_t module_inst = NULL;
  199. wasm_exec_env_t exec_env = NULL;
  200. char error_buf[128];
  201. char global_heap_buf[512 * 1024];
  202. uint32_t stack_size = 8092, heap_size = 8092;
  203. bool cleanup = true;
  204. };
  205. TEST_F(wasm_running_modes_test_suite, wasm_runtime_is_running_mode_supported)
  206. {
  207. // normal situation
  208. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(
  209. static_cast<RunningMode>(Mode_Default)));
  210. for (auto running_mode : running_mode_supported) {
  211. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(running_mode));
  212. }
  213. // abnormal situation
  214. ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
  215. static_cast<RunningMode>(-1)));
  216. ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
  217. static_cast<RunningMode>(5)));
  218. ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
  219. static_cast<RunningMode>(0xFF)));
  220. }
  221. TEST_F(wasm_running_modes_test_suite, wasm_runtime_set_default_running_mode)
  222. {
  223. // normal situation: only set up
  224. ASSERT_EQ(true, wasm_runtime_set_default_running_mode(
  225. static_cast<RunningMode>(Mode_Default)));
  226. for (auto running_mode : running_mode_supported) {
  227. ASSERT_EQ(true, wasm_runtime_set_default_running_mode(running_mode));
  228. }
  229. // abnormal situation
  230. ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
  231. static_cast<RunningMode>(-1)));
  232. ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
  233. static_cast<RunningMode>(5)));
  234. ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
  235. static_cast<RunningMode>(0xFF)));
  236. }
  237. TEST_P(wasm_running_modes_test_suite,
  238. wasm_runtime_set_default_running_mode_basic)
  239. {
  240. RunningMode running_mode = GetParam();
  241. ASSERT_EQ(true, wasm_runtime_set_default_running_mode(running_mode));
  242. run_wasm_basic(WASM_FILE_1, true);
  243. }
  244. TEST_P(wasm_running_modes_test_suite,
  245. wasm_runtime_set_and_get_running_mode_basic)
  246. {
  247. RunningMode running_mode = GetParam();
  248. run_wasm_basic(WASM_FILE_1, false, running_mode);
  249. }
  250. TEST_P(wasm_running_modes_test_suite,
  251. wasm_runtime_set_and_get_running_mode_complex)
  252. {
  253. RunningMode default_running_mode = GetParam();
  254. for (auto running_mode : running_mode_supported) {
  255. run_wasm_complex(WASM_FILE_1, WASM_FILE_2, default_running_mode,
  256. running_mode);
  257. }
  258. }
  259. INSTANTIATE_TEST_CASE_P(RunningMode, wasm_running_modes_test_suite,
  260. testing::ValuesIn(running_mode_supported));
  261. }