wasm_runtime_init_test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. using namespace std;
  14. extern "C" {
  15. uint32
  16. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
  17. uint32 size, void **p_native_addr);
  18. bool
  19. wasm_runtime_create_exec_env_and_call_wasm(
  20. WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function,
  21. uint32 argc, uint32 argv[]);
  22. }
  23. static char global_heap_buf[100 * 1024 * 1024] = { 0 };
  24. static std::string CWD;
  25. static std::string MAIN_WASM = "/main.wasm";
  26. static std::string MAIN_AOT = "/main.aot";
  27. static char *WASM_FILE_1;
  28. static char *AOT_FILE_1;
  29. static int
  30. foo(int a, int b);
  31. static int
  32. foo_native(wasm_exec_env_t exec_env, int a, int b)
  33. {
  34. return a + b;
  35. }
  36. static NativeSymbol native_symbols[] = { {
  37. "foo", // the name of WASM function name
  38. (void *)foo_native, // the native function pointer
  39. "(ii)i" // the function prototype signature
  40. } };
  41. static std::string
  42. get_binary_path()
  43. {
  44. char cwd[1024];
  45. memset(cwd, 0, 1024);
  46. if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
  47. }
  48. char *path_end = strrchr(cwd, '/');
  49. if (path_end != NULL) {
  50. *path_end = '\0';
  51. }
  52. return std::string(cwd);
  53. }
  54. class wasm_runtime_init_test_suite : public testing::Test
  55. {
  56. protected:
  57. // You should make the members protected s.t. they can be
  58. // accessed from sub-classes.
  59. // virtual void SetUp() will be called before each test is run. You
  60. // should define it if you need to initialize the varaibles.
  61. // Otherwise, this can be skipped.
  62. virtual void SetUp() {}
  63. static void SetUpTestCase()
  64. {
  65. CWD = get_binary_path();
  66. WASM_FILE_1 = strdup((CWD + MAIN_WASM).c_str());
  67. AOT_FILE_1 = strdup((CWD + MAIN_AOT).c_str());
  68. }
  69. // virtual void TearDown() will be called after each test is run.
  70. // You should define it if there is cleanup work to do. Otherwise,
  71. // you don't have to provide it.
  72. //
  73. virtual void TearDown() {}
  74. static void TearDownTestCase()
  75. {
  76. free(WASM_FILE_1);
  77. free(AOT_FILE_1);
  78. }
  79. };
  80. TEST_F(wasm_runtime_init_test_suite, init_and_register_natives)
  81. {
  82. EXPECT_EQ(true, wasm_runtime_init());
  83. int n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
  84. EXPECT_EQ(true, wasm_runtime_register_natives("env", native_symbols,
  85. n_native_symbols));
  86. EXPECT_EQ(true, wasm_runtime_register_natives_raw("env", native_symbols,
  87. n_native_symbols));
  88. wasm_runtime_destroy();
  89. }
  90. TEST_F(wasm_runtime_init_test_suite, init_thread_env_destroy_thread_env)
  91. {
  92. EXPECT_EQ(true, wasm_runtime_init_thread_env());
  93. wasm_runtime_destroy_thread_env();
  94. }
  95. TEST_F(wasm_runtime_init_test_suite, wasm_runtime_full_init)
  96. {
  97. RuntimeInitArgs init_args;
  98. unsigned char *wasm_file_buf;
  99. uint32 wasm_file_size;
  100. wasm_module_t module = nullptr;
  101. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  102. init_args.mem_alloc_type = Alloc_With_Pool;
  103. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  104. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  105. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  106. wasm_runtime_destroy();
  107. init_args.n_native_symbols = 1;
  108. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  109. wasm_runtime_destroy();
  110. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  111. init_args.mem_alloc_type = Alloc_With_Allocator;
  112. init_args.mem_alloc_option.allocator.malloc_func = (void *)malloc;
  113. init_args.mem_alloc_option.allocator.realloc_func = (void *)realloc;
  114. init_args.mem_alloc_option.allocator.free_func = (void *)free;
  115. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  116. wasm_runtime_destroy();
  117. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  118. init_args.mem_alloc_type = Alloc_With_Allocator;
  119. init_args.mem_alloc_option.allocator.malloc_func = (void *)os_malloc;
  120. init_args.mem_alloc_option.allocator.realloc_func = (void *)os_realloc;
  121. init_args.mem_alloc_option.allocator.free_func = (void *)os_free;
  122. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  123. /* Use valid module, and runtime need to be proper inited */
  124. wasm_file_buf =
  125. (unsigned char *)bh_read_file_to_buffer(WASM_FILE_1, &wasm_file_size);
  126. EXPECT_NE(nullptr, wasm_file_buf);
  127. module = wasm_runtime_load(wasm_file_buf, wasm_file_size, nullptr, 0);
  128. EXPECT_NE(nullptr, module);
  129. EXPECT_EQ(true, wasm_runtime_register_module_internal(
  130. "module", module, wasm_file_buf, wasm_file_size, nullptr, 0));
  131. wasm_runtime_destroy();
  132. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  133. init_args.mem_alloc_type = Alloc_With_Pool;
  134. init_args.mem_alloc_option.pool.heap_buf = NULL;
  135. init_args.mem_alloc_option.pool.heap_size = 0;
  136. EXPECT_EQ(false, wasm_runtime_full_init(&init_args));
  137. }