| 1 |
#include "platform/memory_mapping.hpp" |
| 2 |
|
| 3 |
#include "utils/optional.hpp" |
| 4 |
#include "utils/utils.hpp" |
| 5 |
|
| 6 |
#ifndef _MSC_VER |
| 7 |
|
| 8 |
#if IS_WINDOWS |
| 9 |
#ifndef WIN32_LEAN_AND_MEAN |
| 10 |
#define WIN32_LEAN_AND_MEAN |
| 11 |
#endif |
| 12 |
#include <windows.h> |
| 13 |
#else |
| 14 |
#include <sys/mman.h> |
| 15 |
#include <unistd.h> |
| 16 |
#if IS_APPLE |
| 17 |
#include <mach/mach.h> |
| 18 |
#ifdef CPPTRACE_HAS_MACH_VM |
| 19 |
#include <mach/mach_vm.h> |
| 20 |
#endif |
| 21 |
#else |
| 22 |
#include <fstream> |
| 23 |
#include <ios> |
| 24 |
#endif |
| 25 |
#endif |
| 26 |
|
| 27 |
CPPTRACE_BEGIN_NAMESPACE |
| 28 |
namespace detail { |
| 29 |
#if IS_WINDOWS |
| 30 |
int get_page_size() { |
| 31 |
SYSTEM_INFO info; |
| 32 |
GetSystemInfo(&info); |
| 33 |
return info.dwPageSize; |
| 34 |
} |
| 35 |
int mprotect_page_and_return_old_protections(void* page, int page_size, int protections) { |
| 36 |
DWORD old_protections; |
| 37 |
if(!VirtualProtect(page, page_size, protections, &old_protections)) { |
| 38 |
throw internal_error( |
| 39 |
"VirtualProtect call failed: {}", |
| 40 |
std::system_error(GetLastError(), std::system_category()).what() |
| 41 |
); |
| 42 |
} |
| 43 |
return old_protections; |
| 44 |
} |
| 45 |
void mprotect_page(void* page, int page_size, int protections) { |
| 46 |
mprotect_page_and_return_old_protections(page, page_size, protections); |
| 47 |
} |
| 48 |
void* allocate_page(int page_size) { |
| 49 |
auto page = VirtualAlloc(nullptr, page_size, MEM_COMMIT | MEM_RESERVE, memory_readwrite); |
| 50 |
if(!page) { |
| 51 |
throw internal_error( |
| 52 |
"VirtualAlloc call failed: {}", |
| 53 |
std::system_error(GetLastError(), std::system_category()).what() |
| 54 |
); |
| 55 |
} |
| 56 |
return page; |
| 57 |
} |
| 58 |
#else |
| 59 |
int get_page_size() { |
| 60 |
#if defined(_SC_PAGESIZE) |
| 61 |
return sysconf(_SC_PAGESIZE); |
| 62 |
#else |
| 63 |
return getpagesize(); |
| 64 |
#endif |
| 65 |
} |
| 66 |
#if IS_APPLE |
| 67 |
int get_page_protections(void* page) { |
| 68 |
// https://stackoverflow.com/a/12627784/15675011 |
| 69 |
#ifdef CPPTRACE_HAS_MACH_VM |
| 70 |
mach_vm_size_t vmsize; |
| 71 |
mach_vm_address_t address = (mach_vm_address_t)page; |
| 72 |
#else |
| 73 |
vm_size_t vmsize; |
| 74 |
vm_address_t address = (vm_address_t)page; |
| 75 |
#endif |
| 76 |
vm_region_basic_info_data_t info; |
| 77 |
mach_msg_type_number_t info_count = |
| 78 |
sizeof(size_t) == 8 ? VM_REGION_BASIC_INFO_COUNT_64 : VM_REGION_BASIC_INFO_COUNT; |
| 79 |
memory_object_name_t object; |
| 80 |
kern_return_t status = |
| 81 |
#ifdef CPPTRACE_HAS_MACH_VM |
| 82 |
mach_vm_region |
| 83 |
#else |
| 84 |
vm_region_64 |
| 85 |
#endif |
| 86 |
( |
| 87 |
mach_task_self(), |
| 88 |
&address, |
| 89 |
&vmsize, |
| 90 |
VM_REGION_BASIC_INFO, |
| 91 |
(vm_region_info_t)&info, |
| 92 |
&info_count, |
| 93 |
&object |
| 94 |
); |
| 95 |
if(status == KERN_INVALID_ADDRESS) { |
| 96 |
throw internal_error("vm_region failed with KERN_INVALID_ADDRESS"); |
| 97 |
} |
| 98 |
int perms = 0; |
| 99 |
if(info.protection & VM_PROT_READ) { |
| 100 |
perms |= PROT_READ; |
| 101 |
} |
| 102 |
if(info.protection & VM_PROT_WRITE) { |
| 103 |
perms |= PROT_WRITE; |
| 104 |
} |
| 105 |
if(info.protection & VM_PROT_EXECUTE) { |
| 106 |
perms |= PROT_EXEC; |
| 107 |
} |
| 108 |
return perms; |
| 109 |
} |
| 110 |
#else |
| 111 |
// Code for reading /proc/self/maps |
| 112 |
// Unfortunately this is the canonical and only way to get memory permissions on linux |
| 113 |
// It comes with some surprising behaviors. Because it's a pseudo-file and maps could update at any time, reads of |
| 114 |
// the file can tear. The surprising observable behavior here is overlapping ranges: |
| 115 |
// - https://unix.stackexchange.com/questions/704987/overlapping-address-ranges-in-proc-maps |
| 116 |
// - https://stackoverflow.com/questions/59737950/what-is-the-correct-way-to-get-a-consistent-snapshot-of-proc-pid-smaps |
| 117 |
// Additional info: |
| 118 |
// Note: reading /proc/PID/maps or /proc/PID/smaps is inherently racy (consistent |
| 119 |
// output can be achieved only in the single read call). |
| 120 |
// This typically manifests when doing partial reads of these files while the |
| 121 |
// memory map is being modified. Despite the races, we do provide the following |
| 122 |
// guarantees: |
| 123 |
// |
| 124 |
// 1) The mapped addresses never go backwards, which implies no two |
| 125 |
// regions will ever overlap. |
| 126 |
// 2) If there is something at a given vaddr during the entirety of the |
| 127 |
// life of the smaps/maps walk, there will be some output for it. |
| 128 |
// |
| 129 |
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt |
| 130 |
// Ideally we could do everything as a single read() call but I don't think that's practical, especially given that |
| 131 |
// the kernel has limited buffers internally. While we shouldn't be modifying mapped memory while reading |
| 132 |
// /proc/self/maps here, it's theoretically possible that we could allocate and that could go to the OS for more |
| 133 |
// pages. |
| 134 |
// While reading this is inherently racy, as far as I can tell tears don't happen within a line but they can happen |
| 135 |
// between lines. |
| 136 |
// The code that writes /proc/pid/maps: |
| 137 |
// - https://github.com/torvalds/linux/blob/3d0ebc36b0b3e8486ceb6e08e8ae173aaa6d1221/fs/proc/task_mmu.c#L304-L365 |
| 138 |
|
| 139 |
struct address_range { |
| 140 |
uintptr_t low; |
| 141 |
uintptr_t high; |
| 142 |
int perms; |
| 143 |
bool operator<(const address_range& other) const { |
| 144 |
return low < other.low; |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
// returns nullopt on eof |
| 149 |
optional<address_range> read_map_entry(std::ifstream& stream) { |
| 150 |
uintptr_t start; |
| 151 |
uintptr_t stop; |
| 152 |
stream>>start; |
| 153 |
stream.ignore(1); // dash |
| 154 |
stream>>stop; |
| 155 |
if(stream.eof()) { |
| 156 |
return nullopt; |
| 157 |
} |
| 158 |
if(stream.fail()) { |
| 159 |
throw internal_error("Failure reading /proc/self/maps"); |
| 160 |
} |
| 161 |
stream.ignore(1); // space |
| 162 |
char r, w, x; // there's a private/shared flag after these but we don't need it |
| 163 |
stream>>r>>w>>x; |
| 164 |
if(stream.fail() || stream.eof()) { |
| 165 |
throw internal_error("Failure reading /proc/self/maps"); |
| 166 |
} |
| 167 |
int perms = 0; |
| 168 |
if(r == 'r') { |
| 169 |
perms |= PROT_READ; |
| 170 |
} |
| 171 |
if(w == 'w') { |
| 172 |
perms |= PROT_WRITE; |
| 173 |
} |
| 174 |
if(x == 'x') { |
| 175 |
perms |= PROT_EXEC; |
| 176 |
} |
| 177 |
stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
| 178 |
return address_range{start, stop, perms}; |
| 179 |
} |
| 180 |
|
| 181 |
// returns a vector or nullopt if a tear is detected |
| 182 |
optional<std::vector<address_range>> try_load_mapped_region_info() { |
| 183 |
std::ifstream stream("/proc/self/maps"); |
| 184 |
stream>>std::hex; |
| 185 |
std::vector<address_range> ranges; |
| 186 |
while(auto entry = read_map_entry(stream)) { |
| 187 |
const auto& range = entry.unwrap(); |
| 188 |
VERIFY(range.low <= range.high); |
| 189 |
if(!ranges.empty()) { |
| 190 |
const auto& last_range = ranges.back(); |
| 191 |
if(range.low < last_range.high) { |
| 192 |
return nullopt; |
| 193 |
} |
| 194 |
} |
| 195 |
ranges.push_back(range); |
| 196 |
} |
| 197 |
return ranges; |
| 198 |
} |
| 199 |
|
| 200 |
// we can allocate during try_load_mapped_region_info, in theory that could cause a tear |
| 201 |
optional<std::vector<address_range>> try_load_mapped_region_info_with_retries(int n) { |
| 202 |
VERIFY(n > 0); |
| 203 |
for(int i = 0; i < n; i++) { |
| 204 |
if(auto info = try_load_mapped_region_info()) { |
| 205 |
return info; |
| 206 |
} |
| 207 |
} |
| 208 |
throw internal_error("Couldn't successfully load /proc/self/maps after {} retries", n); |
| 209 |
} |
| 210 |
|
| 211 |
const std::vector<address_range>& load_mapped_region_info() { |
| 212 |
static std::vector<address_range> regions; |
| 213 |
static bool has_loaded = false; |
| 214 |
if(!has_loaded) { |
| 215 |
has_loaded = true; |
| 216 |
if(auto info = try_load_mapped_region_info_with_retries(2)) { |
| 217 |
regions = std::move(info).unwrap(); |
| 218 |
} |
| 219 |
} |
| 220 |
return regions; |
| 221 |
} |
| 222 |
|
| 223 |
int get_page_protections(void* page) { |
| 224 |
const auto& mapped_region_info = load_mapped_region_info(); |
| 225 |
auto it = first_less_than_or_equal( |
| 226 |
mapped_region_info.begin(), |
| 227 |
mapped_region_info.end(), |
| 228 |
reinterpret_cast<uintptr_t>(page), |
| 229 |
[](uintptr_t a, const address_range& b) { |
| 230 |
return a < b.low; |
| 231 |
} |
| 232 |
); |
| 233 |
if(it == mapped_region_info.end()) { |
| 234 |
throw internal_error( |
| 235 |
"Failed to find mapping for {>16:0h} in /proc/self/maps", |
| 236 |
reinterpret_cast<uintptr_t>(page) |
| 237 |
); |
| 238 |
} |
| 239 |
return it->perms; |
| 240 |
} |
| 241 |
#endif |
| 242 |
void mprotect_page(void* page, int page_size, int protections) { |
| 243 |
if(mprotect(page, page_size, protections) != 0) { |
| 244 |
throw internal_error("mprotect call failed: {}", strerror(errno)); |
| 245 |
} |
| 246 |
} |
| 247 |
int mprotect_page_and_return_old_protections(void* page, int page_size, int protections) { |
| 248 |
auto old_protections = get_page_protections(page); |
| 249 |
mprotect_page(page, page_size, protections); |
| 250 |
return old_protections; |
| 251 |
} |
| 252 |
void* allocate_page(int page_size) { |
| 253 |
auto page = mmap(nullptr, page_size, memory_readwrite, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 254 |
if(page == MAP_FAILED) { |
| 255 |
throw internal_error("mmap call failed: {}", strerror(errno)); |
| 256 |
} |
| 257 |
return page; |
| 258 |
} |
| 259 |
#endif |
| 260 |
} |
| 261 |
CPPTRACE_END_NAMESPACE |
| 262 |
|
| 263 |
#endif |