chrome_unwind_info_android_unittest.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/profiler/chrome_unwind_info_android.h"
  5. #include <tuple>
  6. #include "base/test/gtest_util.h"
  7. #include "build/build_config.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace base {
  10. bool operator==(const FunctionTableEntry& e1, const FunctionTableEntry& e2) {
  11. return std::tie(e1.function_start_address_page_instruction_offset,
  12. e1.function_offset_table_byte_index) ==
  13. std::tie(e2.function_start_address_page_instruction_offset,
  14. e2.function_offset_table_byte_index);
  15. }
  16. template <class T, size_t E1, size_t E2>
  17. void ExpectSpanSizeAndContentsEqual(span<T, E1> actual, span<T, E2> expected) {
  18. EXPECT_EQ(actual.size(), expected.size());
  19. if (actual.size() != expected.size()) {
  20. return;
  21. }
  22. for (size_t i = 0; i < actual.size(); i++) {
  23. EXPECT_EQ(actual[i], expected[i]);
  24. }
  25. }
  26. TEST(ChromeUnwindInfoAndroidTest, CreateUnwindInfo) {
  27. ChromeUnwindInfoHeaderAndroid header = {
  28. /* page_table_byte_offset */ 64,
  29. /* page_table_entries */ 1,
  30. /* function_table_byte_offset */ 128,
  31. /* function_table_entries */ 2,
  32. /* function_offset_table_byte_offset */ 192,
  33. /* function_offset_table_size_in_bytes */ 3,
  34. /* unwind_instruction_table_byte_offset */ 256,
  35. /* unwind_instruction_table_size_in_bytes */ 4,
  36. };
  37. uint8_t data[512] = {};
  38. // Note: `CreateChromeUnwindInfoAndroid` is not expected to verify the content
  39. // of each unwind table.
  40. const uint32_t page_table[] = {1};
  41. const FunctionTableEntry function_table[] = {{0, 2}, {0, 3}};
  42. const uint8_t function_offset_table[] = {3, 3, 3};
  43. const uint8_t unwind_instruction_table[] = {4, 4, 4, 4};
  44. ASSERT_LT(sizeof(ChromeUnwindInfoHeaderAndroid), 64ul);
  45. memcpy(&data[0], &header, sizeof(ChromeUnwindInfoHeaderAndroid));
  46. memcpy(&data[header.page_table_byte_offset], page_table, sizeof(page_table));
  47. memcpy(&data[header.function_table_byte_offset], function_table,
  48. sizeof(function_table));
  49. memcpy(&data[header.function_offset_table_byte_offset], function_offset_table,
  50. sizeof(function_offset_table));
  51. memcpy(&data[header.unwind_instruction_table_byte_offset],
  52. unwind_instruction_table, sizeof(unwind_instruction_table));
  53. ChromeUnwindInfoAndroid unwind_info = CreateChromeUnwindInfoAndroid(data);
  54. ASSERT_EQ(&data[64],
  55. reinterpret_cast<const uint8_t*>(&unwind_info.page_table[0]));
  56. ASSERT_EQ(&data[128],
  57. reinterpret_cast<const uint8_t*>(&unwind_info.function_table[0]));
  58. ASSERT_EQ(&data[192], reinterpret_cast<const uint8_t*>(
  59. &unwind_info.function_offset_table[0]));
  60. ASSERT_EQ(&data[256], reinterpret_cast<const uint8_t*>(
  61. &unwind_info.unwind_instruction_table[0]));
  62. ExpectSpanSizeAndContentsEqual(unwind_info.page_table, make_span(page_table));
  63. ExpectSpanSizeAndContentsEqual(unwind_info.function_table,
  64. make_span(function_table));
  65. ExpectSpanSizeAndContentsEqual(unwind_info.function_offset_table,
  66. make_span(function_offset_table));
  67. ExpectSpanSizeAndContentsEqual(unwind_info.unwind_instruction_table,
  68. make_span(unwind_instruction_table));
  69. }
  70. } // namespace base