| 1 | //===-- MachVMRegion.cpp ----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Created by Greg Clayton on 6/26/07. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "MachVMRegion.h" |
| 14 | #include "DNBLog.h" |
| 15 | #include <cassert> |
| 16 | #include <mach/mach_vm.h> |
| 17 | #include <mach/vm_statistics.h> |
| 18 | |
| 19 | // From <mach/vm_statistics.h>, but not on older OSs. |
| 20 | #ifndef VM_MEMORY_SANITIZER |
| 21 | #define VM_MEMORY_SANITIZER 99 |
| 22 | #endif |
| 23 | |
| 24 | MachVMRegion::MachVMRegion(task_t task) |
| 25 | : m_task(task), m_addr(INVALID_NUB_ADDRESS), m_err(), |
| 26 | m_start(INVALID_NUB_ADDRESS), m_size(0), m_depth(-1), |
| 27 | m_curr_protection(0), m_protection_addr(INVALID_NUB_ADDRESS), |
| 28 | m_protection_size(0) { |
| 29 | memset(&m_data, 0, sizeof(m_data)); |
| 30 | } |
| 31 | |
| 32 | MachVMRegion::~MachVMRegion() { |
| 33 | // Restore any original protections and clear our vars |
| 34 | Clear(); |
| 35 | } |
| 36 | |
| 37 | void MachVMRegion::Clear() { |
| 38 | RestoreProtections(); |
| 39 | m_addr = INVALID_NUB_ADDRESS; |
| 40 | m_err.Clear(); |
| 41 | m_start = INVALID_NUB_ADDRESS; |
| 42 | m_size = 0; |
| 43 | m_depth = -1; |
| 44 | memset(&m_data, 0, sizeof(m_data)); |
| 45 | m_curr_protection = 0; |
| 46 | m_protection_addr = INVALID_NUB_ADDRESS; |
| 47 | m_protection_size = 0; |
| 48 | } |
| 49 | |
| 50 | bool MachVMRegion::SetProtections(mach_vm_address_t addr, mach_vm_size_t size, |
| 51 | vm_prot_t prot) { |
| 52 | if (ContainsAddress(addr)) { |
| 53 | mach_vm_size_t prot_size = size; |
| 54 | mach_vm_address_t end_addr = EndAddress(); |
| 55 | if (prot_size > (end_addr - addr)) |
| 56 | prot_size = end_addr - addr; |
| 57 | |
| 58 | if (prot_size > 0) { |
| 59 | if (prot == (m_curr_protection & VM_PROT_ALL)) { |
| 60 | DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS | LOG_VERBOSE, |
| 61 | "MachVMRegion::%s: protections (%u) already " |
| 62 | "sufficient for task 0x%4.4x at address 0x%8.8llx) " , |
| 63 | __FUNCTION__, prot, m_task, (uint64_t)addr); |
| 64 | // Protections are already set as requested... |
| 65 | return true; |
| 66 | } else { |
| 67 | m_err = ::mach_vm_protect(m_task, addr, prot_size, 0, prot); |
| 68 | if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS)) |
| 69 | m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = " |
| 70 | "0x%8.8llx, size = %llu, set_max = %i, prot = %u )" , |
| 71 | m_task, (uint64_t)addr, (uint64_t)prot_size, 0, |
| 72 | prot); |
| 73 | if (m_err.Fail()) { |
| 74 | // Try again with the ability to create a copy on write region |
| 75 | m_err = ::mach_vm_protect(m_task, addr, prot_size, 0, |
| 76 | prot | VM_PROT_COPY); |
| 77 | if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS) || m_err.Fail()) |
| 78 | m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = " |
| 79 | "0x%8.8llx, size = %llu, set_max = %i, prot = %u " |
| 80 | ")" , |
| 81 | m_task, (uint64_t)addr, (uint64_t)prot_size, 0, |
| 82 | prot | VM_PROT_COPY); |
| 83 | } |
| 84 | if (m_err.Success()) { |
| 85 | m_curr_protection = prot; |
| 86 | m_protection_addr = addr; |
| 87 | m_protection_size = prot_size; |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | } else { |
| 92 | DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS | LOG_VERBOSE, |
| 93 | "%s: Zero size for task 0x%4.4x at address 0x%8.8llx) " , |
| 94 | __FUNCTION__, m_task, (uint64_t)addr); |
| 95 | } |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | bool MachVMRegion::RestoreProtections() { |
| 101 | if (m_curr_protection != m_data.protection && m_protection_size > 0) { |
| 102 | m_err = ::mach_vm_protect(m_task, m_protection_addr, m_protection_size, 0, |
| 103 | m_data.protection); |
| 104 | if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS) || m_err.Fail()) |
| 105 | m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = 0x%8.8llx, " |
| 106 | "size = %llu, set_max = %i, prot = %u )" , |
| 107 | m_task, (uint64_t)m_protection_addr, |
| 108 | (uint64_t)m_protection_size, 0, m_data.protection); |
| 109 | if (m_err.Success()) { |
| 110 | m_protection_size = 0; |
| 111 | m_protection_addr = INVALID_NUB_ADDRESS; |
| 112 | m_curr_protection = m_data.protection; |
| 113 | return true; |
| 114 | } |
| 115 | } else { |
| 116 | m_err.Clear(); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | bool MachVMRegion::GetRegionForAddress(nub_addr_t addr) { |
| 124 | // Restore any original protections and clear our vars |
| 125 | Clear(); |
| 126 | m_err.Clear(); |
| 127 | m_addr = addr; |
| 128 | m_start = addr; |
| 129 | m_depth = 1024; |
| 130 | mach_msg_type_number_t info_size = kRegionInfoSize; |
| 131 | static_assert(sizeof(info_size) == 4); |
| 132 | m_err = |
| 133 | ::mach_vm_region_recurse(m_task, &m_start, &m_size, &m_depth, |
| 134 | (vm_region_recurse_info_t)&m_data, &info_size); |
| 135 | |
| 136 | const bool failed = m_err.Fail(); |
| 137 | const bool log_protections = DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS); |
| 138 | |
| 139 | if (log_protections || failed) |
| 140 | m_err.LogThreaded("::mach_vm_region_recurse ( task = 0x%4.4x, address => " |
| 141 | "0x%8.8llx, size => %llu, nesting_depth => %d, info => " |
| 142 | "%p, infoCnt => %d) addr = 0x%8.8llx " , |
| 143 | m_task, (uint64_t)m_start, (uint64_t)m_size, m_depth, |
| 144 | &m_data, info_size, (uint64_t)addr); |
| 145 | |
| 146 | if (failed) |
| 147 | return false; |
| 148 | if (log_protections) { |
| 149 | DNBLogThreaded("info = { prot = %u, " |
| 150 | "max_prot = %u, " |
| 151 | "inheritance = 0x%8.8x, " |
| 152 | "offset = 0x%8.8llx, " |
| 153 | "user_tag = 0x%8.8x, " |
| 154 | "ref_count = %u, " |
| 155 | "shadow_depth = %u, " |
| 156 | "ext_pager = %u, " |
| 157 | "share_mode = %u, " |
| 158 | "is_submap = %d, " |
| 159 | "behavior = %d, " |
| 160 | "object_id = 0x%8.8x, " |
| 161 | "user_wired_count = 0x%4.4x }" , |
| 162 | m_data.protection, m_data.max_protection, m_data.inheritance, |
| 163 | (uint64_t)m_data.offset, m_data.user_tag, m_data.ref_count, |
| 164 | m_data.shadow_depth, m_data.external_pager, |
| 165 | m_data.share_mode, m_data.is_submap, m_data.behavior, |
| 166 | m_data.object_id, m_data.user_wired_count); |
| 167 | } |
| 168 | m_curr_protection = m_data.protection; |
| 169 | |
| 170 | // We make a request for an address and got no error back, but this |
| 171 | // doesn't mean that "addr" is in the range. The data in this object will |
| 172 | // be valid though, so you could see where the next region begins. So we |
| 173 | // return false, yet leave "m_err" with a successfull return code. |
| 174 | return !((addr < m_start) || (addr >= (m_start + m_size))); |
| 175 | } |
| 176 | |
| 177 | uint32_t MachVMRegion::GetDNBPermissions() const { |
| 178 | if (m_addr == INVALID_NUB_ADDRESS || m_start == INVALID_NUB_ADDRESS || |
| 179 | m_size == 0) |
| 180 | return 0; |
| 181 | uint32_t dnb_permissions = 0; |
| 182 | |
| 183 | if ((m_data.protection & VM_PROT_READ) == VM_PROT_READ) |
| 184 | dnb_permissions |= eMemoryPermissionsReadable; |
| 185 | if ((m_data.protection & VM_PROT_WRITE) == VM_PROT_WRITE) |
| 186 | dnb_permissions |= eMemoryPermissionsWritable; |
| 187 | if ((m_data.protection & VM_PROT_EXECUTE) == VM_PROT_EXECUTE) |
| 188 | dnb_permissions |= eMemoryPermissionsExecutable; |
| 189 | return dnb_permissions; |
| 190 | } |
| 191 | |
| 192 | std::vector<std::string> MachVMRegion::GetMemoryTypes() const { |
| 193 | std::vector<std::string> types; |
| 194 | if (m_data.user_tag == VM_MEMORY_STACK) { |
| 195 | if (m_data.protection == VM_PROT_NONE) { |
| 196 | types.push_back("stack-guard" ); |
| 197 | } else { |
| 198 | types.push_back("stack" ); |
| 199 | } |
| 200 | } |
| 201 | if (m_data.user_tag == VM_MEMORY_MALLOC) { |
| 202 | if (m_data.protection == VM_PROT_NONE) |
| 203 | types.push_back("malloc-guard" ); |
| 204 | else if (m_data.share_mode == SM_EMPTY) |
| 205 | types.push_back("malloc-reserved" ); |
| 206 | else |
| 207 | types.push_back("malloc-metadata" ); |
| 208 | } |
| 209 | if (m_data.user_tag == VM_MEMORY_MALLOC_NANO || |
| 210 | m_data.user_tag == VM_MEMORY_MALLOC_TINY || |
| 211 | m_data.user_tag == VM_MEMORY_MALLOC_SMALL || |
| 212 | m_data.user_tag == VM_MEMORY_MALLOC_LARGE || |
| 213 | m_data.user_tag == VM_MEMORY_MALLOC_LARGE_REUSED || |
| 214 | m_data.user_tag == VM_MEMORY_MALLOC_LARGE_REUSABLE || |
| 215 | m_data.user_tag == VM_MEMORY_MALLOC_HUGE || |
| 216 | m_data.user_tag == VM_MEMORY_REALLOC || |
| 217 | m_data.user_tag == VM_MEMORY_SBRK || |
| 218 | m_data.user_tag == VM_MEMORY_SANITIZER) { |
| 219 | types.push_back("heap" ); |
| 220 | if (m_data.user_tag == VM_MEMORY_MALLOC_TINY) { |
| 221 | types.push_back("malloc-tiny" ); |
| 222 | } |
| 223 | if (m_data.user_tag == VM_MEMORY_MALLOC_LARGE) { |
| 224 | types.push_back("malloc-large" ); |
| 225 | } |
| 226 | if (m_data.user_tag == VM_MEMORY_MALLOC_SMALL) { |
| 227 | types.push_back("malloc-small" ); |
| 228 | } |
| 229 | } |
| 230 | return types; |
| 231 | } |
| 232 | |