wasm_runtime_init_test.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. class wasm_runtime_init_test_suite : public testing::Test
  42. {
  43. protected:
  44. // You should make the members protected s.t. they can be
  45. // accessed from sub-classes.
  46. // virtual void SetUp() will be called before each test is run. You
  47. // should define it if you need to initialize the variables.
  48. // Otherwise, this can be skipped.
  49. virtual void SetUp() {}
  50. static void SetUpTestCase()
  51. {
  52. CWD = get_test_binary_dir();
  53. WASM_FILE_1 = strdup((CWD + MAIN_WASM).c_str());
  54. AOT_FILE_1 = strdup((CWD + MAIN_AOT).c_str());
  55. }
  56. // virtual void TearDown() will be called after each test is run.
  57. // You should define it if there is cleanup work to do. Otherwise,
  58. // you don't have to provide it.
  59. //
  60. virtual void TearDown() {}
  61. static void TearDownTestCase()
  62. {
  63. free(WASM_FILE_1);
  64. free(AOT_FILE_1);
  65. }
  66. };
  67. TEST_F(wasm_runtime_init_test_suite, init_and_register_natives)
  68. {
  69. EXPECT_EQ(true, wasm_runtime_init());
  70. int n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
  71. EXPECT_EQ(true, wasm_runtime_register_natives("env", native_symbols,
  72. n_native_symbols));
  73. EXPECT_EQ(true, wasm_runtime_register_natives_raw("env", native_symbols,
  74. n_native_symbols));
  75. wasm_runtime_destroy();
  76. }
  77. TEST_F(wasm_runtime_init_test_suite, init_thread_env_destroy_thread_env)
  78. {
  79. EXPECT_EQ(true, wasm_runtime_init_thread_env());
  80. wasm_runtime_destroy_thread_env();
  81. }
  82. TEST_F(wasm_runtime_init_test_suite, wasm_runtime_full_init)
  83. {
  84. RuntimeInitArgs init_args;
  85. unsigned char *wasm_file_buf;
  86. uint32 wasm_file_size;
  87. wasm_module_t module = nullptr;
  88. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  89. init_args.mem_alloc_type = Alloc_With_Pool;
  90. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  91. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  92. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  93. wasm_runtime_destroy();
  94. init_args.n_native_symbols = 1;
  95. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  96. wasm_runtime_destroy();
  97. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  98. init_args.mem_alloc_type = Alloc_With_Allocator;
  99. init_args.mem_alloc_option.allocator.malloc_func = (void *)malloc;
  100. init_args.mem_alloc_option.allocator.realloc_func = (void *)realloc;
  101. init_args.mem_alloc_option.allocator.free_func = (void *)free;
  102. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  103. wasm_runtime_destroy();
  104. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  105. init_args.mem_alloc_type = Alloc_With_Allocator;
  106. init_args.mem_alloc_option.allocator.malloc_func = (void *)os_malloc;
  107. init_args.mem_alloc_option.allocator.realloc_func = (void *)os_realloc;
  108. init_args.mem_alloc_option.allocator.free_func = (void *)os_free;
  109. EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
  110. /* Use valid module, and runtime need to be proper inited */
  111. wasm_file_buf =
  112. (unsigned char *)bh_read_file_to_buffer(WASM_FILE_1, &wasm_file_size);
  113. EXPECT_NE(nullptr, wasm_file_buf);
  114. module = wasm_runtime_load(wasm_file_buf, wasm_file_size, nullptr, 0);
  115. EXPECT_NE(nullptr, module);
  116. EXPECT_EQ(true, wasm_runtime_register_module_internal(
  117. "module", module, wasm_file_buf, wasm_file_size, nullptr, 0));
  118. wasm_runtime_destroy();
  119. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  120. init_args.mem_alloc_type = Alloc_With_Pool;
  121. init_args.mem_alloc_option.pool.heap_buf = NULL;
  122. init_args.mem_alloc_option.pool.heap_size = 0;
  123. EXPECT_EQ(false, wasm_runtime_full_init(&init_args));
  124. }