Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 51d26a4 | 2024-04-25 14:23:56 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 10 | #include "base/memory/protected_memory.h" |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 11 | |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | |
| 15 | #include <climits> |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 16 | #include <type_traits> |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 17 | |
| 18 | #include "base/memory/protected_memory_buildflags.h" |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
| 20 | #include "base/test/gtest_util.h" |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 21 | #include "build/build_config.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
| 24 | namespace base { |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | struct Data { |
| 28 | Data() = default; |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 29 | constexpr Data(int16_t f, int32_t b) : foo(f), bar(b) {} |
| 30 | int16_t foo = 0; |
| 31 | int32_t bar = -1; |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 32 | }; |
| 33 | |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 34 | struct DataWithNonTrivialConstructor { |
| 35 | explicit DataWithNonTrivialConstructor(int f) : foo(f) {} |
| 36 | int foo; |
| 37 | }; |
| 38 | |
| 39 | static_assert( |
| 40 | !std::is_trivially_constructible_v<DataWithNonTrivialConstructor>); |
| 41 | |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 42 | #if BUILDFLAG(PROTECTED_MEMORY_ENABLED) |
| 43 | void VerifyByteSequenceIsNotWriteable(unsigned char* const byte_pattern, |
| 44 | const size_t number_of_bits, |
| 45 | const size_t bit_increment) { |
| 46 | const auto check_bit_not_writeable = [=](const size_t bit_index) { |
| 47 | const size_t byte_index = bit_index / CHAR_BIT; |
| 48 | const size_t local_bit_index = bit_index % CHAR_BIT; |
| 49 | |
| 50 | EXPECT_CHECK_DEATH_WITH( |
| 51 | byte_pattern[byte_index] ^= (0x1 << local_bit_index), "") |
| 52 | << " at bit " << bit_index << " of " << number_of_bits; |
| 53 | }; |
| 54 | |
| 55 | // Check the boundary bits explicitly to ensure we cover these. |
| 56 | if (number_of_bits >= 1) { |
| 57 | check_bit_not_writeable(0); |
| 58 | } |
| 59 | |
| 60 | if (number_of_bits >= 2) { |
| 61 | check_bit_not_writeable(number_of_bits - 1); |
| 62 | } |
| 63 | |
| 64 | // Now check the bits in between at the requested increment. |
| 65 | for (size_t bit_index = bit_increment; bit_index < (number_of_bits - 1); |
| 66 | bit_index += bit_increment) { |
| 67 | check_bit_not_writeable(bit_index); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | void VerifyInstanceIsNotWriteable(T& instance, const size_t bit_increment = 3) { |
| 73 | VerifyByteSequenceIsNotWriteable( |
| 74 | reinterpret_cast<unsigned char*>(std::addressof(instance)), |
| 75 | sizeof(T) * CHAR_BIT, bit_increment); |
| 76 | } |
| 77 | #endif // BUILDFLAG(PROTECTED_MEMORY_ENABLED) |
| 78 | |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 79 | DEFINE_PROTECTED_DATA ProtectedMemory<int> g_explicit_initialization; |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 80 | |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 81 | TEST(ProtectedMemoryTest, ExplicitInitializationWithExplicitValue) { |
| 82 | static ProtectedMemoryInitializer initializer_explicit_value( |
| 83 | g_explicit_initialization, 4); |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 84 | |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 85 | EXPECT_EQ(*g_explicit_initialization, 4); |
| 86 | } |
| 87 | |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 88 | DEFINE_PROTECTED_DATA ProtectedMemory<int> |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 89 | g_explicit_initialization_with_default_value; |
| 90 | |
| 91 | TEST(ProtectedMemoryTest, VerifyExplicitInitializationWithDefaultValue) { |
| 92 | static ProtectedMemoryInitializer initializer_explicit_value( |
| 93 | g_explicit_initialization_with_default_value); |
| 94 | |
| 95 | EXPECT_EQ(*g_explicit_initialization_with_default_value, int()); |
| 96 | } |
| 97 | |
| 98 | DEFINE_PROTECTED_DATA |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 99 | ProtectedMemory<DataWithNonTrivialConstructor> |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 100 | g_lazily_initialized_with_explicit_initialization; |
| 101 | |
| 102 | TEST(ProtectedMemoryTest, ExplicitLazyInitializationWithExplicitValue) { |
| 103 | static ProtectedMemoryInitializer initializer_explicit_value( |
| 104 | g_lazily_initialized_with_explicit_initialization, 4); |
| 105 | |
| 106 | EXPECT_EQ(g_lazily_initialized_with_explicit_initialization->foo, 4); |
| 107 | } |
| 108 | |
| 109 | DEFINE_PROTECTED_DATA |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 110 | ProtectedMemory<DataWithNonTrivialConstructor> g_uninitialized; |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 111 | |
| 112 | TEST(ProtectedMemoryDeathTest, AccessWithoutInitialization) { |
| 113 | EXPECT_CHECK_DEATH_WITH(g_uninitialized.operator*(), ""); |
| 114 | EXPECT_CHECK_DEATH_WITH(g_uninitialized.operator->(), ""); |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | #if BUILDFLAG(PROTECTED_MEMORY_ENABLED) |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 118 | DEFINE_PROTECTED_DATA ProtectedMemory<Data> g_initialized; |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 119 | |
| 120 | TEST(ProtectedMemoryTest, VerifySetValue) { |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 121 | static ProtectedMemoryInitializer initializer_explicit_value(g_initialized); |
| 122 | ASSERT_NE(g_initialized->foo, 5); |
| 123 | EXPECT_EQ(g_initialized->bar, -1); |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 124 | { |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 125 | base::AutoWritableMemory writer(g_initialized); |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 126 | writer.GetProtectedDataPtr()->foo = 5; |
| 127 | } |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 128 | EXPECT_EQ(g_initialized->foo, 5); |
| 129 | EXPECT_EQ(g_initialized->bar, -1); |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 130 | } |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 131 | |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 132 | DEFINE_PROTECTED_DATA ProtectedMemory<Data> g_not_writable; |
| 133 | |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 134 | TEST(ProtectedMemoryDeathTest, AccessWithoutWriteAccessCrashes) { |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 135 | static ProtectedMemoryInitializer initializer_explicit_value(g_not_writable); |
| 136 | VerifyInstanceIsNotWriteable(g_not_writable); |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 137 | } |
| 138 | |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 139 | TEST(ProtectedMemoryDeathTest, FailsIfDefinedOutsideOfProtectMemoryRegion) { |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 140 | ProtectedMemory<Data> data; |
| 141 | EXPECT_CHECK_DEATH({ AutoWritableMemory writer(data); }); |
| 142 | } |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 143 | |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 144 | #endif // BUILDFLAG(PROTECTED_MEMORY_ENABLED) |
| 145 | |
André Kempe | 0f7da88a | 2024-03-22 19:12:47 | [diff] [blame] | 146 | } // namespace |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 147 | } // namespace base |