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 | |
| 5 | #include "base/memory/protected_memory.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | |
Jeffrey Gour | e63ec7f | 2024-08-28 01:12:53 | [diff] [blame] | 11 | #include "base/bits.h" |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 12 | #include "base/memory/page_size.h" |
| 13 | #include "base/process/process_metrics.h" |
| 14 | #include "base/synchronization/lock.h" |
| 15 | #include "build/build_config.h" |
| 16 | |
| 17 | namespace base { |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 18 | |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 19 | #if BUILDFLAG(PROTECTED_MEMORY_ENABLED) |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | bool SetMemory(void* start, void* end, DWORD prot) { |
| 23 | CHECK(end > start); |
Jeffrey Gour | e63ec7f | 2024-08-28 01:12:53 | [diff] [blame] | 24 | const uintptr_t page_start = |
| 25 | bits::AlignDown(reinterpret_cast<uintptr_t>(start), GetPageSize()); |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 26 | DWORD old_prot; |
| 27 | return VirtualProtect(reinterpret_cast<void*>(page_start), |
| 28 | reinterpret_cast<uintptr_t>(end) - page_start, prot, |
| 29 | &old_prot) != 0; |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 34 | namespace internal { |
Jeffrey Gour | e63ec7f | 2024-08-28 01:12:53 | [diff] [blame] | 35 | void CheckMemoryReadOnly(const void* ptr) { |
| 36 | const uintptr_t page_start = |
| 37 | bits::AlignDown(reinterpret_cast<uintptr_t>(ptr), GetPageSize()); |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 38 | |
| 39 | MEMORY_BASIC_INFORMATION info; |
| 40 | SIZE_T result = |
| 41 | VirtualQuery(reinterpret_cast<LPCVOID>(page_start), &info, sizeof(info)); |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 42 | |
Jeffrey Gour | e63ec7f | 2024-08-28 01:12:53 | [diff] [blame] | 43 | CHECK((result > 0U) && (info.Protect == PAGE_READONLY)); |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 44 | } |
| 45 | } // namespace internal |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 46 | |
| 47 | bool AutoWritableMemoryBase::SetMemoryReadWrite(void* start, void* end) { |
| 48 | return SetMemory(start, end, PAGE_READWRITE); |
| 49 | } |
| 50 | |
| 51 | bool AutoWritableMemoryBase::SetMemoryReadOnly(void* start, void* end) { |
| 52 | return SetMemory(start, end, PAGE_READONLY); |
| 53 | } |
Jeffrey Gour | 6f946aad | 2024-07-11 16:07:54 | [diff] [blame] | 54 | #endif // BUILDFLAG(PROTECTED_MEMORY_ENABLED) |
André Kempe | 5adc4d5a | 2024-03-15 12:23:33 | [diff] [blame] | 55 | |
Jeff Gour | a3e4ebd | 2024-01-23 21:04:49 | [diff] [blame] | 56 | } // namespace base |