wasm_running_modes_test.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_supportted = { 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_unload(module);
  89. return false;
  90. }
  91. void destory_exec_env()
  92. {
  93. wasm_runtime_destroy_exec_env(exec_env);
  94. wasm_runtime_deinstantiate(module_inst);
  95. wasm_runtime_unload(module);
  96. }
  97. protected:
  98. void run_wasm_basic(
  99. char *filename, bool in_default_running_mode,
  100. RunningMode running_mode = static_cast<RunningMode>(Mode_Default))
  101. {
  102. bool ret;
  103. uint32_t wasm_argv[2];
  104. ret = load_wasm_file(filename);
  105. ASSERT_TRUE(ret);
  106. ret = init_exec_env();
  107. ASSERT_TRUE(ret);
  108. if (!in_default_running_mode) {
  109. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  110. ASSERT_TRUE(ret);
  111. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  112. }
  113. wasm_function_inst_t echo2xback_func =
  114. wasm_runtime_lookup_function(module_inst, "echo");
  115. ASSERT_TRUE(echo2xback_func != NULL);
  116. wasm_argv[0] = 5;
  117. ret = wasm_runtime_call_wasm(exec_env, echo2xback_func, 1, wasm_argv);
  118. ASSERT_TRUE(ret);
  119. ASSERT_EQ(10, wasm_argv[0]);
  120. destory_exec_env();
  121. }
  122. void run_wasm_complex(char *filename1, char *filename2,
  123. RunningMode default_running_mode,
  124. RunningMode running_mode)
  125. {
  126. bool ret;
  127. uint32_t wasm_argv[2];
  128. /* run wasm file 1 in default running mode */
  129. wasm_runtime_set_default_running_mode(default_running_mode);
  130. ret = load_wasm_file(filename1);
  131. ASSERT_TRUE(ret);
  132. ret = init_exec_env();
  133. ASSERT_TRUE(ret);
  134. uint8_t *buffer, *buffer2;
  135. wasm_function_inst_t echo2xback_func, main;
  136. ASSERT_EQ(default_running_mode,
  137. wasm_runtime_get_running_mode(module_inst));
  138. echo2xback_func = wasm_runtime_lookup_function(module_inst, "echo");
  139. ASSERT_TRUE(echo2xback_func != NULL);
  140. wasm_argv[0] = 5;
  141. ret = wasm_runtime_call_wasm(exec_env, echo2xback_func, 1, wasm_argv);
  142. ASSERT_TRUE(ret);
  143. ASSERT_EQ(10, wasm_argv[0]);
  144. destory_exec_env();
  145. /* run wasm file 2 in running_mode */
  146. ret = load_wasm_file(filename2);
  147. ASSERT_TRUE(ret);
  148. ret = init_exec_env();
  149. ASSERT_TRUE(ret);
  150. ret = wasm_runtime_set_running_mode(module_inst, running_mode);
  151. ASSERT_TRUE(ret);
  152. ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
  153. main = wasm_runtime_lookup_function(module_inst, "__main_argc_argv");
  154. ASSERT_TRUE(main != NULL);
  155. ret = wasm_runtime_call_wasm(exec_env, main, 2, wasm_argv);
  156. ASSERT_TRUE(ret);
  157. destory_exec_env();
  158. }
  159. public:
  160. // If your test fixture defines SetUpTestSuite() or TearDownTestSuite()
  161. // they must be declared public rather than protected in order to use
  162. // TEST_P.
  163. // virtual void SetUp() will be called before each test is run. You
  164. // should define it if you need to initialize the variables.
  165. // Otherwise, this can be skipped.
  166. virtual void SetUp()
  167. {
  168. CWD = get_binary_path();
  169. WASM_FILE_1 = strdup((CWD + TEST_WASM1).c_str());
  170. WASM_FILE_2 = strdup((CWD + TEST_WASM2).c_str());
  171. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  172. init_args.mem_alloc_type = Alloc_With_Pool;
  173. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  174. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  175. ASSERT_EQ(wasm_runtime_full_init(&init_args), true);
  176. cleanup = true;
  177. }
  178. static void SetUpTestCase() {}
  179. // virtual void TearDown() will be called after each test is run.
  180. // You should define it if there is cleanup work to do. Otherwise,
  181. // you don't have to provide it.
  182. //
  183. virtual void TearDown()
  184. {
  185. if (cleanup) {
  186. wasm_runtime_destroy();
  187. cleanup = false;
  188. }
  189. free(WASM_FILE_1);
  190. free(WASM_FILE_2);
  191. }
  192. static void TearDownTestCase() {}
  193. std::string CWD;
  194. RuntimeInitArgs init_args;
  195. wasm_module_t module = NULL;
  196. wasm_module_inst_t module_inst = NULL;
  197. wasm_exec_env_t exec_env = NULL;
  198. char error_buf[128];
  199. char global_heap_buf[512 * 1024];
  200. uint32_t stack_size = 8092, heap_size = 8092;
  201. bool cleanup = true;
  202. };
  203. TEST_F(wasm_running_modes_test_suite, wasm_runtime_is_running_mode_supported)
  204. {
  205. // normal situation
  206. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(
  207. static_cast<RunningMode>(Mode_Default)));
  208. for (auto running_mode : running_mode_supportted) {
  209. ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(running_mode));
  210. }
  211. // abnormal situation
  212. ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
  213. static_cast<RunningMode>(-1)));
  214. ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
  215. static_cast<RunningMode>(5)));
  216. ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
  217. static_cast<RunningMode>(0xFF)));
  218. }
  219. TEST_F(wasm_running_modes_test_suite, wasm_runtime_set_default_running_mode)
  220. {
  221. // normal situation: only set up
  222. ASSERT_EQ(true, wasm_runtime_set_default_running_mode(
  223. static_cast<RunningMode>(Mode_Default)));
  224. for (auto running_mode : running_mode_supportted) {
  225. ASSERT_EQ(true, wasm_runtime_set_default_running_mode(running_mode));
  226. }
  227. // abnormal situation
  228. ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
  229. static_cast<RunningMode>(-1)));
  230. ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
  231. static_cast<RunningMode>(5)));
  232. ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
  233. static_cast<RunningMode>(0xFF)));
  234. }
  235. TEST_P(wasm_running_modes_test_suite,
  236. wasm_runtime_set_default_running_mode_basic)
  237. {
  238. RunningMode running_mode = GetParam();
  239. ASSERT_EQ(true, wasm_runtime_set_default_running_mode(running_mode));
  240. run_wasm_basic(WASM_FILE_1, true);
  241. }
  242. TEST_P(wasm_running_modes_test_suite,
  243. wasm_runtime_set_and_get_running_mode_basic)
  244. {
  245. RunningMode running_mode = GetParam();
  246. run_wasm_basic(WASM_FILE_1, false, running_mode);
  247. }
  248. TEST_P(wasm_running_modes_test_suite,
  249. wasm_runtime_set_and_get_running_mode_complex)
  250. {
  251. RunningMode default_running_mode = GetParam();
  252. for (auto running_mode : running_mode_supportted) {
  253. run_wasm_complex(WASM_FILE_1, WASM_FILE_2, default_running_mode,
  254. running_mode);
  255. }
  256. }
  257. INSTANTIATE_TEST_CASE_P(RunningMode, wasm_running_modes_test_suite,
  258. testing::ValuesIn(running_mode_supportted));
  259. }