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
MachVMRegion(task_t task)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
~MachVMRegion()33 MachVMRegion::~MachVMRegion()
34 {
35 // Restore any original protections and clear our vars
36 Clear();
37 }
38
39 void
Clear()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
SetProtections(mach_vm_address_t addr,mach_vm_size_t size,vm_prot_t prot)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
RestoreProtections()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
GetRegionForAddress(nub_addr_t addr)127 MachVMRegion::GetRegionForAddress(nub_addr_t addr)
128 {
129 // Restore any original protections and clear our vars
130 Clear();
131 m_err.Clear();
132 m_addr = addr;
133 m_start = addr;
134 m_depth = 1024;
135 mach_msg_type_number_t info_size = kRegionInfoSize;
136 assert(sizeof(info_size) == 4);
137 m_err = ::mach_vm_region_recurse (m_task, &m_start, &m_size, &m_depth, (vm_region_recurse_info_t)&m_data, &info_size);
138
139 const bool failed = m_err.Fail();
140 const bool log_protections = DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS);
141
142 if (log_protections || failed)
143 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);
144
145 if (failed)
146 return false;
147 if (log_protections)
148 {
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,
163 m_data.max_protection,
164 m_data.inheritance,
165 (uint64_t)m_data.offset,
166 m_data.user_tag,
167 m_data.ref_count,
168 m_data.shadow_depth,
169 m_data.external_pager,
170 m_data.share_mode,
171 m_data.is_submap,
172 m_data.behavior,
173 m_data.object_id,
174 m_data.user_wired_count);
175 }
176 m_curr_protection = m_data.protection;
177
178 // We make a request for an address and got no error back, but this
179 // doesn't mean that "addr" is in the range. The data in this object will
180 // be valid though, so you could see where the next region begins. So we
181 // return false, yet leave "m_err" with a successfull return code.
182 if ((addr < m_start) || (addr >= (m_start + m_size)))
183 return false;
184
185 return true;
186 }
187
188 uint32_t
GetDNBPermissions() const189 MachVMRegion::GetDNBPermissions () const
190 {
191 if (m_addr == INVALID_NUB_ADDRESS || m_start == INVALID_NUB_ADDRESS || m_size == 0)
192 return 0;
193 uint32_t dnb_permissions = 0;
194
195 if ((m_data.protection & VM_PROT_READ) == VM_PROT_READ)
196 dnb_permissions |= eMemoryPermissionsReadable;
197 if ((m_data.protection & VM_PROT_WRITE) == VM_PROT_WRITE)
198 dnb_permissions |= eMemoryPermissionsWritable;
199 if ((m_data.protection & VM_PROT_EXECUTE) == VM_PROT_EXECUTE)
200 dnb_permissions |= eMemoryPermissionsExecutable;
201 return dnb_permissions;
202 }
203