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 "DNBLog.h"
16 #include <assert.h>
17 #include <mach/mach_vm.h>
18 
19 MachVMRegion::MachVMRegion(task_t task)
20     : m_task(task), m_addr(INVALID_NUB_ADDRESS), m_err(),
21       m_start(INVALID_NUB_ADDRESS), m_size(0), m_depth(-1),
22       m_curr_protection(0), m_protection_addr(INVALID_NUB_ADDRESS),
23       m_protection_size(0) {
24   memset(&m_data, 0, sizeof(m_data));
25 }
26 
27 MachVMRegion::~MachVMRegion() {
28   // Restore any original protections and clear our vars
29   Clear();
30 }
31 
32 void MachVMRegion::Clear() {
33   RestoreProtections();
34   m_addr = INVALID_NUB_ADDRESS;
35   m_err.Clear();
36   m_start = INVALID_NUB_ADDRESS;
37   m_size = 0;
38   m_depth = -1;
39   memset(&m_data, 0, sizeof(m_data));
40   m_curr_protection = 0;
41   m_protection_addr = INVALID_NUB_ADDRESS;
42   m_protection_size = 0;
43 }
44 
45 bool MachVMRegion::SetProtections(mach_vm_address_t addr, mach_vm_size_t size,
46                                   vm_prot_t prot) {
47   if (ContainsAddress(addr)) {
48     mach_vm_size_t prot_size = size;
49     mach_vm_address_t end_addr = EndAddress();
50     if (prot_size > (end_addr - addr))
51       prot_size = end_addr - addr;
52 
53     if (prot_size > 0) {
54       if (prot == (m_curr_protection & VM_PROT_ALL)) {
55         DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS | LOG_VERBOSE,
56                          "MachVMRegion::%s: protections (%u) already "
57                          "sufficient for task 0x%4.4x at address 0x%8.8llx) ",
58                          __FUNCTION__, prot, m_task, (uint64_t)addr);
59         // Protections are already set as requested...
60         return true;
61       } else {
62         m_err = ::mach_vm_protect(m_task, addr, prot_size, 0, prot);
63         if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS))
64           m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = "
65                             "0x%8.8llx, size = %llu, set_max = %i, prot = %u )",
66                             m_task, (uint64_t)addr, (uint64_t)prot_size, 0,
67                             prot);
68         if (m_err.Fail()) {
69           // Try again with the ability to create a copy on write region
70           m_err = ::mach_vm_protect(m_task, addr, prot_size, 0,
71                                     prot | VM_PROT_COPY);
72           if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS) || m_err.Fail())
73             m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = "
74                               "0x%8.8llx, size = %llu, set_max = %i, prot = %u "
75                               ")",
76                               m_task, (uint64_t)addr, (uint64_t)prot_size, 0,
77                               prot | VM_PROT_COPY);
78         }
79         if (m_err.Success()) {
80           m_curr_protection = prot;
81           m_protection_addr = addr;
82           m_protection_size = prot_size;
83           return true;
84         }
85       }
86     } else {
87       DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS | LOG_VERBOSE,
88                        "%s: Zero size for task 0x%4.4x at address 0x%8.8llx) ",
89                        __FUNCTION__, m_task, (uint64_t)addr);
90     }
91   }
92   return false;
93 }
94 
95 bool MachVMRegion::RestoreProtections() {
96   if (m_curr_protection != m_data.protection && m_protection_size > 0) {
97     m_err = ::mach_vm_protect(m_task, m_protection_addr, m_protection_size, 0,
98                               m_data.protection);
99     if (DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS) || m_err.Fail())
100       m_err.LogThreaded("::mach_vm_protect ( task = 0x%4.4x, addr = 0x%8.8llx, "
101                         "size = %llu, set_max = %i, prot = %u )",
102                         m_task, (uint64_t)m_protection_addr,
103                         (uint64_t)m_protection_size, 0, m_data.protection);
104     if (m_err.Success()) {
105       m_protection_size = 0;
106       m_protection_addr = INVALID_NUB_ADDRESS;
107       m_curr_protection = m_data.protection;
108       return true;
109     }
110   } else {
111     m_err.Clear();
112     return true;
113   }
114 
115   return false;
116 }
117 
118 bool MachVMRegion::GetRegionForAddress(nub_addr_t addr) {
119   // Restore any original protections and clear our vars
120   Clear();
121   m_err.Clear();
122   m_addr = addr;
123   m_start = addr;
124   m_depth = 1024;
125   mach_msg_type_number_t info_size = kRegionInfoSize;
126   assert(sizeof(info_size) == 4);
127   m_err =
128       ::mach_vm_region_recurse(m_task, &m_start, &m_size, &m_depth,
129                                (vm_region_recurse_info_t)&m_data, &info_size);
130 
131   const bool failed = m_err.Fail();
132   const bool log_protections = DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS);
133 
134   if (log_protections || failed)
135     m_err.LogThreaded("::mach_vm_region_recurse ( task = 0x%4.4x, address => "
136                       "0x%8.8llx, size => %llu, nesting_depth => %d, info => "
137                       "%p, infoCnt => %d) addr = 0x%8.8llx ",
138                       m_task, (uint64_t)m_start, (uint64_t)m_size, m_depth,
139                       &m_data, info_size, (uint64_t)addr);
140 
141   if (failed)
142     return false;
143   if (log_protections) {
144     DNBLogThreaded("info = { prot = %u, "
145                    "max_prot = %u, "
146                    "inheritance = 0x%8.8x, "
147                    "offset = 0x%8.8llx, "
148                    "user_tag = 0x%8.8x, "
149                    "ref_count = %u, "
150                    "shadow_depth = %u, "
151                    "ext_pager = %u, "
152                    "share_mode = %u, "
153                    "is_submap = %d, "
154                    "behavior = %d, "
155                    "object_id = 0x%8.8x, "
156                    "user_wired_count = 0x%4.4x }",
157                    m_data.protection, m_data.max_protection, m_data.inheritance,
158                    (uint64_t)m_data.offset, m_data.user_tag, m_data.ref_count,
159                    m_data.shadow_depth, m_data.external_pager,
160                    m_data.share_mode, m_data.is_submap, m_data.behavior,
161                    m_data.object_id, m_data.user_wired_count);
162   }
163   m_curr_protection = m_data.protection;
164 
165   // We make a request for an address and got no error back, but this
166   // doesn't mean that "addr" is in the range. The data in this object will
167   // be valid though, so you could see where the next region begins. So we
168   // return false, yet leave "m_err" with a successfull return code.
169   if ((addr < m_start) || (addr >= (m_start + m_size)))
170     return false;
171 
172   return true;
173 }
174 
175 uint32_t MachVMRegion::GetDNBPermissions() const {
176   if (m_addr == INVALID_NUB_ADDRESS || m_start == INVALID_NUB_ADDRESS ||
177       m_size == 0)
178     return 0;
179   uint32_t dnb_permissions = 0;
180 
181   if ((m_data.protection & VM_PROT_READ) == VM_PROT_READ)
182     dnb_permissions |= eMemoryPermissionsReadable;
183   if ((m_data.protection & VM_PROT_WRITE) == VM_PROT_WRITE)
184     dnb_permissions |= eMemoryPermissionsWritable;
185   if ((m_data.protection & VM_PROT_EXECUTE) == VM_PROT_EXECUTE)
186     dnb_permissions |= eMemoryPermissionsExecutable;
187   return dnb_permissions;
188 }
189