1 //===-- MachVMRegion.cpp ----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Created by Greg Clayton on 6/26/07. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MachVMRegion.h" 15 #include <mach/mach_vm.h> 16 #include "DNBLog.h" 17 #include <assert.h> 18 19 MachVMRegion::MachVMRegion(task_t task) : 20 m_task(task), 21 m_addr(INVALID_NUB_ADDRESS), 22 m_err(), 23 m_start(INVALID_NUB_ADDRESS), 24 m_size(0), 25 m_depth(-1), 26 m_curr_protection(0), 27 m_protection_addr(INVALID_NUB_ADDRESS), 28 m_protection_size(0) 29 { 30 memset(&m_data, 0, sizeof(m_data)); 31 } 32 33 MachVMRegion::~MachVMRegion() 34 { 35 // Restore any original protections and clear our vars 36 Clear(); 37 } 38 39 void 40 MachVMRegion::Clear() 41 { 42 RestoreProtections(); 43 m_addr = INVALID_NUB_ADDRESS; 44 m_err.Clear(); 45 m_start = INVALID_NUB_ADDRESS; 46 m_size = 0; 47 m_depth = -1; 48 memset(&m_data, 0, sizeof(m_data)); 49 m_curr_protection = 0; 50 m_protection_addr = INVALID_NUB_ADDRESS; 51 m_protection_size = 0; 52 } 53 54 bool 55 MachVMRegion::SetProtections(mach_vm_address_t addr, mach_vm_size_t size, vm_prot_t prot) 56 { 57 if (ContainsAddress(addr)) 58 { 59 mach_vm_size_t prot_size = size; 60 mach_vm_address_t end_addr = EndAddress(); 61 if (prot_size > (end_addr - addr)) 62 prot_size = end_addr - addr; 63 64 if (prot_size > 0) 65 { 66 if (prot == (m_curr_protection & VM_PROT_ALL)) 67 { 68 DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS | LOG_VERBOSE, "MachVMRegion::%s: protections (%u) already sufficient for task 0x%4.4x at address 0x%8.8llx) ", __FUNCTION__, prot, m_task, (uint64_t)addr); 69 // Protections are already set as requested... 70 return true; 71 } 72 else 73 { 74 m_err = ::mach_vm_protect (m_task, addr, prot_size, 0, prot); 75 if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS)) 76 m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = 0x%8.8llx, size = %llu, set_max = %i, prot = %u )", m_task, (uint64_t)addr, (uint64_t)prot_size, 0, prot); 77 if (m_err.Fail()) 78 { 79 // Try again with the ability to create a copy on write region 80 m_err = ::mach_vm_protect (m_task, addr, prot_size, 0, prot | VM_PROT_COPY); 81 if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS) || m_err.Fail()) 82 m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = 0x%8.8llx, size = %llu, set_max = %i, prot = %u )", m_task, (uint64_t)addr, (uint64_t)prot_size, 0, prot | VM_PROT_COPY); 83 } 84 if (m_err.Success()) 85 { 86 m_curr_protection = prot; 87 m_protection_addr = addr; 88 m_protection_size = prot_size; 89 return true; 90 } 91 } 92 } 93 else 94 { 95 DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS | LOG_VERBOSE, "%s: Zero size for task 0x%4.4x at address 0x%8.8llx) ", __FUNCTION__, m_task, (uint64_t)addr); 96 } 97 } 98 return false; 99 } 100 101 bool 102 MachVMRegion::RestoreProtections() 103 { 104 if (m_curr_protection != m_data.protection && m_protection_size > 0) 105 { 106 m_err = ::mach_vm_protect (m_task, m_protection_addr, m_protection_size, 0, m_data.protection); 107 if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS) || m_err.Fail()) 108 m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = 0x%8.8llx, size = %llu, set_max = %i, prot = %u )", m_task, (uint64_t)m_protection_addr, (uint64_t)m_protection_size, 0, m_data.protection); 109 if (m_err.Success()) 110 { 111 m_protection_size = 0; 112 m_protection_addr = INVALID_NUB_ADDRESS; 113 m_curr_protection = m_data.protection; 114 return true; 115 } 116 } 117 else 118 { 119 m_err.Clear(); 120 return true; 121 } 122 123 return false; 124 } 125 126 bool 127 MachVMRegion::GetRegionForAddress(nub_addr_t addr) 128 { 129 // Restore any original protections and clear our vars 130 Clear(); 131 m_addr = addr; 132 m_start = addr; 133 m_depth = 1024; 134 mach_msg_type_number_t info_size = kRegionInfoSize; 135 assert(sizeof(info_size) == 4); 136 m_err = ::mach_vm_region_recurse (m_task, &m_start, &m_size, &m_depth, (vm_region_recurse_info_t)&m_data, &info_size); 137 const bool log_protections = DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS); 138 if (m_err.Success()) 139 { 140 if ((addr < m_start) || (addr >= (m_start + m_size))) 141 { 142 m_err.SetErrorString("no region for address"); 143 m_err.SetError(-1, DNBError::Generic); 144 if (log_protections) 145 m_err.LogThreaded("::mach_vm_region_recurse ( task = 0x%4.4x, address => 0x%8.8llx, size => %llu, nesting_depth => %d, info => %p, infoCnt => %d) addr = 0x%8.8llx not in range [0x%8.8llx - 0x%8.8llx)", 146 m_task, (uint64_t)m_start, (uint64_t)m_size, m_depth, &m_data, info_size, (uint64_t)addr, (uint64_t)m_start, (uint64_t)m_start + m_size); 147 return false; 148 } 149 } 150 151 if (log_protections || m_err.Fail()) 152 m_err.LogThreaded("::mach_vm_region_recurse ( task = 0x%4.4x, address => 0x%8.8llx, size => %llu, nesting_depth => %d, info => %p, infoCnt => %d) addr = 0x%8.8llx ", m_task, (uint64_t)m_start, (uint64_t)m_size, m_depth, &m_data, info_size, (uint64_t)addr); 153 if (m_err.Fail()) 154 { 155 return false; 156 } 157 else 158 { 159 if (log_protections) 160 { 161 DNBLogThreaded("info = { prot = %u, " 162 "max_prot = %u, " 163 "inheritance = 0x%8.8x, " 164 "offset = 0x%8.8llx, " 165 "user_tag = 0x%8.8x, " 166 "ref_count = %u, " 167 "shadow_depth = %u, " 168 "ext_pager = %u, " 169 "share_mode = %u, " 170 "is_submap = %d, " 171 "behavior = %d, " 172 "object_id = 0x%8.8x, " 173 "user_wired_count = 0x%4.4x }", 174 m_data.protection, 175 m_data.max_protection, 176 m_data.inheritance, 177 (uint64_t)m_data.offset, 178 m_data.user_tag, 179 m_data.ref_count, 180 m_data.shadow_depth, 181 m_data.external_pager, 182 m_data.share_mode, 183 m_data.is_submap, 184 m_data.behavior, 185 m_data.object_id, 186 m_data.user_wired_count); 187 } 188 } 189 190 m_curr_protection = m_data.protection; 191 192 return true; 193 } 194 195 uint32_t 196 MachVMRegion::GetDNBPermissions () const 197 { 198 if (m_addr == INVALID_NUB_ADDRESS || m_start == INVALID_NUB_ADDRESS || m_size == 0) 199 return 0; 200 uint32_t dnb_permissions = 0; 201 202 if ((m_data.protection & VM_PROT_READ) == VM_PROT_READ) 203 dnb_permissions |= eMemoryPermissionsReadable; 204 if ((m_data.protection & VM_PROT_WRITE) == VM_PROT_WRITE) 205 dnb_permissions |= eMemoryPermissionsWritable; 206 if ((m_data.protection & VM_PROT_EXECUTE) == VM_PROT_EXECUTE) 207 dnb_permissions |= eMemoryPermissionsExecutable; 208 return dnb_permissions; 209 } 210