Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS) || m_err.Fail()) |
| 138 | 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); |
| 139 | if (m_err.Fail()) |
| 140 | { |
| 141 | return false; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS)) |
| 146 | { |
| 147 | DNBLogThreaded("info = { prot = %u, " |
| 148 | "max_prot = %u, " |
| 149 | "inheritance = 0x%8.8x, " |
| 150 | "offset = 0x%8.8llx, " |
| 151 | "user_tag = 0x%8.8x, " |
| 152 | "ref_count = %u, " |
| 153 | "shadow_depth = %u, " |
| 154 | "ext_pager = %u, " |
| 155 | "share_mode = %u, " |
| 156 | "is_submap = %d, " |
| 157 | "behavior = %d, " |
| 158 | "object_id = 0x%8.8x, " |
| 159 | "user_wired_count = 0x%4.4x }", |
| 160 | m_data.protection, |
| 161 | m_data.max_protection, |
| 162 | m_data.inheritance, |
| 163 | (uint64_t)m_data.offset, |
| 164 | m_data.user_tag, |
| 165 | m_data.ref_count, |
| 166 | m_data.shadow_depth, |
| 167 | m_data.external_pager, |
| 168 | m_data.share_mode, |
| 169 | m_data.is_submap, |
| 170 | m_data.behavior, |
| 171 | m_data.object_id, |
| 172 | m_data.user_wired_count); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | m_curr_protection = m_data.protection; |
| 177 | |
| 178 | return true; |
| 179 | } |
Jason Molenda | 3dc8583 | 2011-11-09 08:03:56 +0000 | [diff] [blame^] | 180 | |
| 181 | bool |
| 182 | MachVMRegion::GetRegionDescription (char *outbuf, nub_size_t outbufsize) |
| 183 | { |
| 184 | if (m_addr == INVALID_NUB_ADDRESS || m_start == INVALID_NUB_ADDRESS || m_size == 0) |
| 185 | return false; |
| 186 | snprintf (outbuf, outbufsize, "start:%llx,size:%llx", m_start, m_size); |
| 187 | outbuf[outbufsize - 1] = '\0'; |
| 188 | |
| 189 | char tmpbuf[128]; |
| 190 | strcpy (tmpbuf, ",permissions:"); |
| 191 | if ((m_data.protection & VM_PROT_READ) == VM_PROT_READ) |
| 192 | strcat (tmpbuf, "r"); |
| 193 | if ((m_data.protection & VM_PROT_WRITE) == VM_PROT_WRITE) |
| 194 | strcat (tmpbuf, "w"); |
| 195 | if ((m_data.protection & VM_PROT_EXECUTE) == VM_PROT_EXECUTE) |
| 196 | strcat (tmpbuf, "x"); |
| 197 | strlcat (outbuf, tmpbuf, outbufsize); |
| 198 | |
| 199 | // It would be nice if we could figure out whether the memory region is stack memory or jitted code memory as well |
| 200 | |
| 201 | outbuf[outbufsize - 1] = '\0'; |
| 202 | return true; |
| 203 | } |