custom_section_test.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "gtest/gtest.h"
  6. #include "bh_platform.h"
  7. #include <fstream>
  8. #include "test_helper.h"
  9. #include "aot_export.h"
  10. class CustomSectionTest : public testing::Test
  11. {
  12. protected:
  13. // You should make the members protected s.t. they can be
  14. // accessed from sub-classes.
  15. // virtual void SetUp() will be called before each test is run. You
  16. // should define it if you need to initialize the variables.
  17. // Otherwise, this can be skipped.
  18. virtual void SetUp() {}
  19. // virtual void TearDown() will be called after each test is run.
  20. // You should define it if there is cleanup work to do. Otherwise,
  21. // you don't have to provide it.
  22. //
  23. virtual void TearDown() {}
  24. public:
  25. WAMRRuntimeRAII<1 * 1024 * 1024> runtime;
  26. };
  27. TEST_F(CustomSectionTest, get_custom_section_from_wasm_module_t)
  28. {
  29. uint32_t length, len_from_aot;
  30. const uint8_t *content, *content_from_aot;
  31. std::ifstream wasm_file("wasm-apps/app.wasm", std::ios::binary);
  32. std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(wasm_file),
  33. {});
  34. {
  35. WAMRModule module(buffer.data(), buffer.size());
  36. aot_comp_data_t comp_data = NULL;
  37. aot_comp_context_t comp_ctx = NULL;
  38. std::vector<const char *> sections_to_emit{
  39. "name",
  40. ".debug_info",
  41. ".debug_abbrev",
  42. /* skip ".debug_line" section in AoT module */
  43. ".debug_str",
  44. "producers",
  45. };
  46. AOTCompOption option = { 0 };
  47. option.custom_sections = (char **)sections_to_emit.data();
  48. option.custom_sections_count = 5;
  49. {
  50. /* Compile an AoT module */
  51. comp_data = aot_create_comp_data(module.get(), NULL, false);
  52. EXPECT_NE(comp_data, nullptr);
  53. comp_ctx = aot_create_comp_context(comp_data, &option);
  54. EXPECT_NE(comp_ctx, nullptr);
  55. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  56. EXPECT_TRUE(aot_emit_aot_file(comp_ctx, comp_data, "temp.aot"));
  57. }
  58. std::ifstream aot_file("temp.aot", std::ios::binary);
  59. std::vector<unsigned char> aot_buffer(
  60. std::istreambuf_iterator<char>(aot_file), {});
  61. WAMRModule aot_module(aot_buffer.data(), aot_buffer.size());
  62. /* name */
  63. content =
  64. wasm_runtime_get_custom_section(module.get(), "name", &length);
  65. EXPECT_NE(content, nullptr);
  66. EXPECT_GT(length, 0);
  67. /* TODO: aot_emit_name_section don't
  68. EMIT_U32(AOT_CUSTOM_SECTION_RAW);*
  69. EMIT_STR("name");
  70. but instead
  71. EMIT_U32(AOT_CUSTOM_SECTION_NAME);
  72. can't use get_custom_section to get it
  73. */
  74. // content_from_aot = wasm_runtime_get_custom_section(
  75. // aot_module.get(), "name", &len_from_aot);
  76. // EXPECT_NE(content_from_aot, nullptr);
  77. // EXPECT_EQ(len_from_aot, length);
  78. // EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
  79. /* .debug_info */
  80. content = wasm_runtime_get_custom_section(module.get(), ".debug_info",
  81. &length);
  82. EXPECT_NE(content, nullptr);
  83. EXPECT_GT(length, 0);
  84. content_from_aot = wasm_runtime_get_custom_section(
  85. aot_module.get(), ".debug_info", &len_from_aot);
  86. EXPECT_NE(content_from_aot, nullptr);
  87. EXPECT_EQ(len_from_aot, length);
  88. EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
  89. /* .debug_abbrev */
  90. content = wasm_runtime_get_custom_section(module.get(), ".debug_abbrev",
  91. &length);
  92. EXPECT_NE(content, nullptr);
  93. EXPECT_GT(length, 0);
  94. content_from_aot = wasm_runtime_get_custom_section(
  95. aot_module.get(), ".debug_abbrev", &len_from_aot);
  96. EXPECT_NE(content_from_aot, nullptr);
  97. EXPECT_EQ(len_from_aot, length);
  98. EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
  99. /* .debug_line */
  100. content = wasm_runtime_get_custom_section(module.get(), ".debug_line",
  101. &length);
  102. EXPECT_NE(content, nullptr);
  103. EXPECT_GT(length, 0);
  104. content_from_aot = wasm_runtime_get_custom_section(
  105. aot_module.get(), ".debug_line", &len_from_aot);
  106. EXPECT_EQ(content_from_aot, nullptr);
  107. /* .debug_str */
  108. content = wasm_runtime_get_custom_section(module.get(), ".debug_str",
  109. &length);
  110. EXPECT_NE(content, nullptr);
  111. EXPECT_GT(length, 0);
  112. content_from_aot = wasm_runtime_get_custom_section(
  113. aot_module.get(), ".debug_str", &len_from_aot);
  114. EXPECT_NE(content_from_aot, nullptr);
  115. EXPECT_EQ(len_from_aot, length);
  116. EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
  117. /* producers */
  118. content =
  119. wasm_runtime_get_custom_section(module.get(), "producers", &length);
  120. EXPECT_NE(content, nullptr);
  121. EXPECT_GT(length, 0);
  122. content_from_aot = wasm_runtime_get_custom_section(
  123. aot_module.get(), "producers", &len_from_aot);
  124. EXPECT_NE(content_from_aot, nullptr);
  125. EXPECT_EQ(len_from_aot, length);
  126. EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
  127. /* Not exist */
  128. content = wasm_runtime_get_custom_section(module.get(), "producers1",
  129. &length);
  130. EXPECT_EQ(content, nullptr);
  131. content_from_aot = wasm_runtime_get_custom_section(
  132. aot_module.get(), "producers1", &len_from_aot);
  133. EXPECT_EQ(content_from_aot, nullptr);
  134. }
  135. }