tagging_unittest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2021 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/allocator/partition_allocator/tagging.h"
  5. #include <cstdint>
  6. #include "base/allocator/partition_allocator/page_allocator.h"
  7. #include "base/allocator/partition_allocator/partition_alloc_base/cpu.h"
  8. #include "base/allocator/partition_allocator/partition_alloc_config.h"
  9. #include "build/build_config.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace partition_alloc::internal {
  12. // Check whether we can call the tagging intrinsics safely on all architectures.
  13. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeRandomlySafe) {
  14. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  15. uintptr_t buffer = AllocPages(
  16. PageAllocationGranularity(), PageAllocationGranularity(),
  17. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  18. EXPECT_TRUE(buffer);
  19. void* bufferp = TagMemoryRangeRandomly(buffer, 4 * kMemTagGranuleSize, 0u);
  20. EXPECT_TRUE(bufferp);
  21. int* buffer0 = static_cast<int*>(bufferp);
  22. *buffer0 = 42;
  23. EXPECT_EQ(42, *buffer0);
  24. FreePages(buffer, PageAllocationGranularity());
  25. }
  26. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeIncrementSafe) {
  27. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  28. base::CPU cpu;
  29. uintptr_t buffer = AllocPages(
  30. PageAllocationGranularity(), PageAllocationGranularity(),
  31. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  32. EXPECT_TRUE(buffer);
  33. void* bufferp = TagMemoryRangeIncrement(buffer, 4 * kMemTagGranuleSize);
  34. EXPECT_TRUE(bufferp);
  35. int* buffer0 = static_cast<int*>(bufferp);
  36. *buffer0 = 42;
  37. EXPECT_EQ(42, *buffer0);
  38. if (cpu.has_mte()) {
  39. EXPECT_NE(bufferp, reinterpret_cast<void*>(buffer));
  40. }
  41. FreePages(buffer, PageAllocationGranularity());
  42. }
  43. #if defined(ARCH_CPU_64_BITS)
  44. // Size / alignment constraints are only enforced on 64-bit architectures.
  45. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeBadSz) {
  46. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  47. base::CPU cpu;
  48. uintptr_t buffer = AllocPages(
  49. PageAllocationGranularity(), PageAllocationGranularity(),
  50. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  51. EXPECT_TRUE(buffer);
  52. void* bufferp =
  53. TagMemoryRangeRandomly(buffer, 4 * kMemTagGranuleSize - 1, 0u);
  54. if (cpu.has_mte()) {
  55. EXPECT_FALSE(bufferp);
  56. }
  57. FreePages(buffer, PageAllocationGranularity());
  58. }
  59. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeRandomlyNoSz) {
  60. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  61. base::CPU cpu;
  62. uintptr_t buffer = AllocPages(
  63. PageAllocationGranularity(), PageAllocationGranularity(),
  64. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  65. EXPECT_TRUE(buffer);
  66. void* bufferp = TagMemoryRangeRandomly(buffer, 0, 0u);
  67. if (cpu.has_mte()) {
  68. EXPECT_FALSE(bufferp);
  69. }
  70. FreePages(buffer, PageAllocationGranularity());
  71. }
  72. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeRandomlyBadAlign) {
  73. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  74. base::CPU cpu;
  75. uintptr_t buffer = AllocPages(
  76. PageAllocationGranularity(), PageAllocationGranularity(),
  77. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  78. EXPECT_TRUE(buffer);
  79. void* bufferp =
  80. TagMemoryRangeRandomly(buffer - 1, 4 * kMemTagGranuleSize, 0u);
  81. if (cpu.has_mte()) {
  82. EXPECT_FALSE(bufferp);
  83. }
  84. FreePages(buffer, PageAllocationGranularity());
  85. }
  86. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeIncrementBadSz) {
  87. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  88. base::CPU cpu;
  89. uintptr_t buffer = AllocPages(
  90. PageAllocationGranularity(), PageAllocationGranularity(),
  91. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  92. EXPECT_TRUE(buffer);
  93. void* bufferp = TagMemoryRangeIncrement(buffer, 4 * kMemTagGranuleSize - 1);
  94. if (cpu.has_mte()) {
  95. EXPECT_FALSE(bufferp);
  96. }
  97. FreePages(buffer, PageAllocationGranularity());
  98. }
  99. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeIncrementNoSz) {
  100. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  101. base::CPU cpu;
  102. uintptr_t buffer = AllocPages(
  103. PageAllocationGranularity(), PageAllocationGranularity(),
  104. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  105. EXPECT_TRUE(buffer);
  106. void* bufferp = TagMemoryRangeIncrement(buffer, 0);
  107. if (cpu.has_mte()) {
  108. EXPECT_FALSE(bufferp);
  109. }
  110. FreePages(buffer, PageAllocationGranularity());
  111. }
  112. TEST(PartitionAllocMemoryTaggingTest, TagMemoryRangeIncrementBadAlign) {
  113. ::partition_alloc::internal::InitializeMTESupportIfNeeded();
  114. base::CPU cpu;
  115. uintptr_t buffer = AllocPages(
  116. PageAllocationGranularity(), PageAllocationGranularity(),
  117. PageAccessibilityConfiguration::kReadWriteTagged, PageTag::kChromium);
  118. EXPECT_TRUE(buffer);
  119. void* bufferp = TagMemoryRangeIncrement(buffer - 1, 4 * kMemTagGranuleSize);
  120. if (cpu.has_mte()) {
  121. EXPECT_FALSE(bufferp);
  122. }
  123. FreePages(buffer, PageAllocationGranularity());
  124. }
  125. #endif // defined(ARCH_CPU_64_BITS)
  126. } // namespace partition_alloc::internal