shared_heap_test.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2024 Xiaomi 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 "wasm_runtime_common.h"
  9. class shared_heap_test : public testing::Test
  10. {
  11. protected:
  12. virtual void SetUp() {}
  13. static void SetUpTestCase() {}
  14. virtual void TearDown() {}
  15. WAMRRuntimeRAII<512 * 1024> runtime;
  16. };
  17. struct ret_env {
  18. wasm_exec_env_t exec_env;
  19. wasm_module_t wasm_module;
  20. wasm_module_inst_t wasm_module_inst;
  21. unsigned char *wasm_file_buf;
  22. char error_buf[128];
  23. };
  24. struct ret_env
  25. load_wasm(char *wasm_file_tested, unsigned int app_heap_size)
  26. {
  27. std::string wasm_mem_page = wasm_file_tested;
  28. const char *wasm_file = strdup(wasm_mem_page.c_str());
  29. wasm_module_inst_t wasm_module_inst = nullptr;
  30. wasm_module_t wasm_module = nullptr;
  31. wasm_exec_env_t exec_env = nullptr;
  32. unsigned char *wasm_file_buf = nullptr;
  33. unsigned int wasm_file_size = 0;
  34. unsigned int stack_size = 16 * 1024, heap_size = app_heap_size;
  35. char error_buf[128] = { 0 };
  36. struct ret_env ret_module_env;
  37. memset(ret_module_env.error_buf, 0, 128);
  38. wasm_file_buf =
  39. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  40. if (!wasm_file_buf) {
  41. goto fail;
  42. }
  43. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  44. sizeof(error_buf));
  45. if (!wasm_module) {
  46. memcpy(ret_module_env.error_buf, error_buf, 128);
  47. goto fail;
  48. }
  49. wasm_module_inst = wasm_runtime_instantiate(
  50. wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf));
  51. if (!wasm_module_inst) {
  52. memcpy(ret_module_env.error_buf, error_buf, 128);
  53. goto fail;
  54. }
  55. exec_env = wasm_runtime_create_exec_env(wasm_module_inst, stack_size);
  56. fail:
  57. ret_module_env.exec_env = exec_env;
  58. ret_module_env.wasm_module = wasm_module;
  59. ret_module_env.wasm_module_inst = wasm_module_inst;
  60. ret_module_env.wasm_file_buf = wasm_file_buf;
  61. return ret_module_env;
  62. }
  63. void
  64. destroy_module_env(struct ret_env module_env)
  65. {
  66. if (module_env.exec_env) {
  67. wasm_runtime_destroy_exec_env(module_env.exec_env);
  68. }
  69. if (module_env.wasm_module_inst) {
  70. wasm_runtime_deinstantiate(module_env.wasm_module_inst);
  71. }
  72. if (module_env.wasm_module) {
  73. wasm_runtime_unload(module_env.wasm_module);
  74. }
  75. if (module_env.wasm_file_buf) {
  76. wasm_runtime_free(module_env.wasm_file_buf);
  77. }
  78. }
  79. TEST_F(shared_heap_test, test_shared_heap)
  80. {
  81. struct ret_env tmp_module_env;
  82. WASMFunctionInstanceCommon *func_test = nullptr;
  83. bool ret = false;
  84. uint32 argv[1] = { 65535 };
  85. const char *exception = nullptr;
  86. SharedHeapInitArgs args;
  87. WASMSharedHeap *shared_heap = nullptr;
  88. args.size = 1024;
  89. shared_heap = wasm_runtime_create_shared_heap(&args);
  90. tmp_module_env = load_wasm((char *)"test.wasm", 0);
  91. if (!shared_heap) {
  92. printf("Failed to create shared heap\n");
  93. goto test_failed;
  94. }
  95. if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst, shared_heap)) {
  96. printf("Failed to attach shared heap\n");
  97. goto test_failed;
  98. }
  99. func_test = wasm_runtime_lookup_function(
  100. tmp_module_env.wasm_module_inst, "test");
  101. if (!func_test) {
  102. printf("\nFailed to wasm_runtime_lookup_function!\n");
  103. goto test_failed;
  104. }
  105. ret =
  106. wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, 1, argv);
  107. if (!ret) {
  108. printf("\nFailed to wasm_runtime_call_wasm!\n");
  109. const char *s = wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
  110. printf("exception: %s\n", s);
  111. goto test_failed;
  112. }
  113. wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
  114. EXPECT_EQ(10, argv[0]);
  115. destroy_module_env(tmp_module_env);
  116. return;
  117. test_failed:
  118. destroy_module_env(tmp_module_env);
  119. EXPECT_EQ(1, 0);
  120. }