blob: e4c85d6ce727144092ca993aedc039cae229b6d2 [file] [log] [blame]
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001//===-- IRMemoryMap.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
Kate Stoneb9c1b512016-09-06 20:57:50 +000010#include "lldb/Expression/IRMemoryMap.h"
Sean Callananf3df7e82016-06-09 20:22:25 +000011#include "lldb/Target/MemoryRegionInfo.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000012#include "lldb/Target/Process.h"
Sean Callanan35005f72013-04-12 18:10:34 +000013#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000014#include "lldb/Utility/DataBufferHeap.h"
15#include "lldb/Utility/DataExtractor.h"
Sean Callananf3df7e82016-06-09 20:22:25 +000016#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000017#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000018#include "lldb/Utility/Scalar.h"
Zachary Turner97206d52017-05-12 04:51:55 +000019#include "lldb/Utility/Status.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000020
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
24 if (target_sp)
25 m_process_wp = target_sp->GetProcessSP();
Sean Callanan5a1af4e2013-04-05 02:22:57 +000026}
27
Kate Stoneb9c1b512016-09-06 20:57:50 +000028IRMemoryMap::~IRMemoryMap() {
29 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 if (process_sp) {
32 AllocationMap::iterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000033
Zachary Turner97206d52017-05-12 04:51:55 +000034 Status err;
Sean Callananbb777042013-05-16 17:30:37 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 while ((iter = m_allocations.begin()) != m_allocations.end()) {
37 err.Clear();
38 if (iter->second.m_leak)
39 m_allocations.erase(iter);
40 else
41 Free(iter->first, err);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000042 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046lldb::addr_t IRMemoryMap::FindSpace(size_t size) {
47 // The FindSpace algorithm's job is to find a region of memory that the
48 // underlying process is unlikely to be using.
49 //
50 // The memory returned by this function will never be written to. The only
51 // point is that it should not shadow process memory if possible, so that
Adrian Prantl05097242018-04-30 16:49:04 +000052 // expressions processing real values from the process do not use the wrong
53 // data.
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 //
55 // If the process can in fact allocate memory (CanJIT() lets us know this)
56 // then this can be accomplished just be allocating memory in the inferior.
57 // Then no guessing is required.
Sean Callananf3df7e82016-06-09 20:22:25 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 lldb::TargetSP target_sp = m_target_wp.lock();
60 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 const bool process_is_alive = process_sp && process_sp->IsAlive();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
65 if (size == 0)
Sean Callananbb9945f2013-04-19 01:51:24 +000066 return ret;
Kate Stoneb9c1b512016-09-06 20:57:50 +000067
68 if (process_is_alive && process_sp->CanJIT()) {
Zachary Turner97206d52017-05-12 04:51:55 +000069 Status alloc_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000070
71 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
72 lldb::ePermissionsWritable,
73 alloc_error);
74
75 if (!alloc_error.Success())
76 return LLDB_INVALID_ADDRESS;
77 else
78 return ret;
79 }
80
81 // At this point we know that we need to hunt.
82 //
83 // First, go to the end of the existing allocations we've made if there are
84 // any allocations. Otherwise start at the beginning of memory.
85
86 if (m_allocations.empty()) {
87 ret = 0x0;
88 } else {
89 auto back = m_allocations.rbegin();
90 lldb::addr_t addr = back->first;
91 size_t alloc_size = back->second.m_size;
92 ret = llvm::alignTo(addr + alloc_size, 4096);
93 }
94
95 // Now, if it's possible to use the GetMemoryRegionInfo API to detect mapped
Adrian Prantl05097242018-04-30 16:49:04 +000096 // regions, walk forward through memory until a region is found that has
97 // adequate space for our allocation.
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 if (process_is_alive) {
99 const uint64_t end_of_memory = process_sp->GetAddressByteSize() == 8
100 ? 0xffffffffffffffffull
101 : 0xffffffffull;
102
103 lldbassert(process_sp->GetAddressByteSize() == 4 ||
104 end_of_memory != 0xffffffffull);
105
106 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +0000107 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (err.Success()) {
109 while (true) {
110 if (region_info.GetReadable() != MemoryRegionInfo::OptionalBool::eNo ||
111 region_info.GetWritable() != MemoryRegionInfo::OptionalBool::eNo ||
112 region_info.GetExecutable() !=
113 MemoryRegionInfo::OptionalBool::eNo) {
114 if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory) {
115 ret = LLDB_INVALID_ADDRESS;
116 break;
117 } else {
118 ret = region_info.GetRange().GetRangeEnd();
119 }
120 } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
121 return ret;
122 } else {
123 // ret stays the same. We just need to walk a bit further.
124 }
125
126 err = process_sp->GetMemoryRegionInfo(
127 region_info.GetRange().GetRangeEnd(), region_info);
128 if (err.Fail()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +0000129 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 ret = LLDB_INVALID_ADDRESS;
131 break;
132 }
133 }
134 }
135 }
136
137 // We've tried our algorithm, and it didn't work. Now we have to reset back
138 // to the end of the allocations we've already reported, or use a 'sensible'
139 // default if this is our first allocation.
140
141 if (m_allocations.empty()) {
142 uint32_t address_byte_size = GetAddressByteSize();
143 if (address_byte_size != UINT32_MAX) {
144 switch (address_byte_size) {
145 case 8:
146 ret = 0xffffffff00000000ull;
147 break;
148 case 4:
149 ret = 0xee000000ull;
150 break;
151 default:
152 break;
153 }
154 }
155 } else {
156 auto back = m_allocations.rbegin();
157 lldb::addr_t addr = back->first;
158 size_t alloc_size = back->second.m_size;
159 ret = llvm::alignTo(addr + alloc_size, 4096);
160 }
161
162 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000163}
164
165IRMemoryMap::AllocationMap::iterator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166IRMemoryMap::FindAllocation(lldb::addr_t addr, size_t size) {
167 if (addr == LLDB_INVALID_ADDRESS)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000168 return m_allocations.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169
170 AllocationMap::iterator iter = m_allocations.lower_bound(addr);
171
172 if (iter == m_allocations.end() || iter->first > addr) {
173 if (iter == m_allocations.begin())
174 return m_allocations.end();
175 iter--;
176 }
177
178 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
179 return iter;
180
181 return m_allocations.end();
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
185 if (addr == LLDB_INVALID_ADDRESS)
Sean Callananbb9945f2013-04-19 01:51:24 +0000186 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187
188 AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
189
190 // Since we only know that the returned interval begins at a location greater
Adrian Prantl05097242018-04-30 16:49:04 +0000191 // than or equal to where the given interval begins, it's possible that the
192 // given interval intersects either the returned interval or the previous
193 // interval. Thus, we need to check both. Note that we only need to check
194 // these two intervals. Since all intervals are disjoint it is not possible
195 // that an adjacent interval does not intersect, but a non-adjacent interval
196 // does intersect.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 if (iter != m_allocations.end()) {
198 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
199 iter->second.m_size))
200 return true;
201 }
202
203 if (iter != m_allocations.begin()) {
204 --iter;
205 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
206 iter->second.m_size))
207 return true;
208 }
209
210 return false;
Sean Callananbb9945f2013-04-19 01:51:24 +0000211}
212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213bool IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1,
214 lldb::addr_t addr2, size_t size2) {
215 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
Adrian Prantl05097242018-04-30 16:49:04 +0000216 // that satisfy A<B and X<Y are the following:
Zachary Turner15362442014-06-25 18:37:19 +0000217 // A B X Y
218 // A X B Y (intersects)
219 // A X Y B (intersects)
220 // X A B Y (intersects)
221 // X A Y B (intersects)
222 // X Y A B
Adrian Prantl05097242018-04-30 16:49:04 +0000223 // The first is B <= X, and the last is Y <= A. So the condition is !(B <= X
224 // || Y <= A)), or (X < B && A < Y)
Zachary Turner15362442014-06-25 18:37:19 +0000225 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
226}
227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228lldb::ByteOrder IRMemoryMap::GetByteOrder() {
229 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 if (process_sp)
232 return process_sp->GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 if (target_sp)
237 return target_sp->GetArchitecture().GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 return lldb::eByteOrderInvalid;
Sean Callanan35005f72013-04-12 18:10:34 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242uint32_t IRMemoryMap::GetAddressByteSize() {
243 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 if (process_sp)
246 return process_sp->GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 if (target_sp)
251 return target_sp->GetArchitecture().GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 return UINT32_MAX;
Sean Callanan35005f72013-04-12 18:10:34 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256ExecutionContextScope *IRMemoryMap::GetBestExecutionContextScope() const {
257 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 if (process_sp)
260 return process_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 if (target_sp)
265 return target_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 return NULL;
Sean Callanan35005f72013-04-12 18:10:34 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270IRMemoryMap::Allocation::Allocation(lldb::addr_t process_alloc,
271 lldb::addr_t process_start, size_t size,
272 uint32_t permissions, uint8_t alignment,
273 AllocationPolicy policy)
274 : m_process_alloc(process_alloc), m_process_start(process_start),
Vedant Kumarf71dd342018-08-08 21:26:49 +0000275 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
276 m_alignment(alignment) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 switch (policy) {
278 default:
279 assert(0 && "We cannot reach this!");
280 case eAllocationPolicyHostOnly:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 case eAllocationPolicyMirror:
282 m_data.SetByteSize(size);
Vedant Kumar94523592018-08-06 20:13:52 +0000283 break;
284 case eAllocationPolicyProcessOnly:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 break;
286 }
287}
288
289lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
290 uint32_t permissions, AllocationPolicy policy,
Zachary Turner97206d52017-05-12 04:51:55 +0000291 bool zero_memory, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 lldb_private::Log *log(
293 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
294 error.Clear();
295
296 lldb::ProcessSP process_sp;
297 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
298 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 size_t allocation_size;
301
Vedant Kumar5b71e752018-05-31 22:08:59 +0000302 if (size == 0) {
303 // FIXME: Malloc(0) should either return an invalid address or assert, in
304 // order to cut down on unnecessary allocations.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 allocation_size = alignment;
Vedant Kumar5b71e752018-05-31 22:08:59 +0000306 } else {
307 // Round up the requested size to an aligned value.
308 allocation_size = llvm::alignTo(size, alignment);
309
310 // The process page cache does not see the requested alignment. We can't
311 // assume its result will be any more than 1-byte aligned. To work around
312 // this, request `alignment - 1` additional bytes.
313 allocation_size += alignment - 1;
314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315
316 switch (policy) {
317 default:
318 error.SetErrorToGenericError();
319 error.SetErrorString("Couldn't malloc: invalid allocation policy");
320 return LLDB_INVALID_ADDRESS;
321 case eAllocationPolicyHostOnly:
322 allocation_address = FindSpace(allocation_size);
323 if (allocation_address == LLDB_INVALID_ADDRESS) {
324 error.SetErrorToGenericError();
325 error.SetErrorString("Couldn't malloc: address space is full");
326 return LLDB_INVALID_ADDRESS;
Sean Callanand2562502013-04-19 17:44:40 +0000327 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 break;
329 case eAllocationPolicyMirror:
330 process_sp = m_process_wp.lock();
Todd Fialaaf245d12014-06-30 21:05:18 +0000331 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 log->Printf("IRMemoryMap::%s process_sp=0x%" PRIx64
333 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
334 __FUNCTION__, (lldb::addr_t)process_sp.get(),
335 process_sp && process_sp->CanJIT() ? "true" : "false",
336 process_sp && process_sp->IsAlive() ? "true" : "false");
337 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
338 if (!zero_memory)
339 allocation_address =
340 process_sp->AllocateMemory(allocation_size, permissions, error);
341 else
342 allocation_address =
343 process_sp->CallocateMemory(allocation_size, permissions, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 if (!error.Success())
346 return LLDB_INVALID_ADDRESS;
347 } else {
348 if (log)
349 log->Printf("IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
350 "due to failed condition (see previous expr log message)",
351 __FUNCTION__);
352 policy = eAllocationPolicyHostOnly;
353 allocation_address = FindSpace(allocation_size);
354 if (allocation_address == LLDB_INVALID_ADDRESS) {
Sean Callananfbf5c682013-05-22 22:49:06 +0000355 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 error.SetErrorString("Couldn't malloc: address space is full");
357 return LLDB_INVALID_ADDRESS;
358 }
Sean Callananfbf5c682013-05-22 22:49:06 +0000359 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 break;
361 case eAllocationPolicyProcessOnly:
362 process_sp = m_process_wp.lock();
363 if (process_sp) {
364 if (process_sp->CanJIT() && process_sp->IsAlive()) {
365 if (!zero_memory)
366 allocation_address =
367 process_sp->AllocateMemory(allocation_size, permissions, error);
Sean Callanan35005f72013-04-12 18:10:34 +0000368 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 allocation_address =
370 process_sp->CallocateMemory(allocation_size, permissions, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Sean Callanan35005f72013-04-12 18:10:34 +0000372 if (!error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 return LLDB_INVALID_ADDRESS;
374 } else {
Sean Callanan35005f72013-04-12 18:10:34 +0000375 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 error.SetErrorString(
377 "Couldn't malloc: process doesn't support allocating memory");
378 return LLDB_INVALID_ADDRESS;
379 }
380 } else {
381 error.SetErrorToGenericError();
382 error.SetErrorString("Couldn't malloc: process doesn't exist, and this "
383 "memory must be in the process");
384 return LLDB_INVALID_ADDRESS;
Sean Callanan35005f72013-04-12 18:10:34 +0000385 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 break;
387 }
388
389 lldb::addr_t mask = alignment - 1;
390 aligned_address = (allocation_address + mask) & (~mask);
391
Vedant Kumarf71dd342018-08-08 21:26:49 +0000392 m_allocations.emplace(
393 std::piecewise_construct, std::forward_as_tuple(aligned_address),
394 std::forward_as_tuple(allocation_address, aligned_address,
395 allocation_size, permissions, alignment, policy));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396
397 if (zero_memory) {
Zachary Turner97206d52017-05-12 04:51:55 +0000398 Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 std::vector<uint8_t> zero_buf(size, 0);
400 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
401 }
402
403 if (log) {
404 const char *policy_string;
405
406 switch (policy) {
407 default:
408 policy_string = "<invalid policy>";
409 break;
410 case eAllocationPolicyHostOnly:
411 policy_string = "eAllocationPolicyHostOnly";
412 break;
413 case eAllocationPolicyProcessOnly:
414 policy_string = "eAllocationPolicyProcessOnly";
415 break;
416 case eAllocationPolicyMirror:
417 policy_string = "eAllocationPolicyMirror";
418 break;
419 }
420
421 log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64
422 ", %s) -> 0x%" PRIx64,
423 (uint64_t)allocation_size, (uint64_t)alignment,
424 (uint64_t)permissions, policy_string, aligned_address);
425 }
426
427 return aligned_address;
Sean Callanan35005f72013-04-12 18:10:34 +0000428}
429
Zachary Turner97206d52017-05-12 04:51:55 +0000430void IRMemoryMap::Leak(lldb::addr_t process_address, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000432
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 AllocationMap::iterator iter = m_allocations.find(process_address);
434
435 if (iter == m_allocations.end()) {
436 error.SetErrorToGenericError();
437 error.SetErrorString("Couldn't leak: allocation doesn't exist");
438 return;
439 }
440
441 Allocation &allocation = iter->second;
442
443 allocation.m_leak = true;
444}
445
Zachary Turner97206d52017-05-12 04:51:55 +0000446void IRMemoryMap::Free(lldb::addr_t process_address, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 error.Clear();
448
449 AllocationMap::iterator iter = m_allocations.find(process_address);
450
451 if (iter == m_allocations.end()) {
452 error.SetErrorToGenericError();
453 error.SetErrorString("Couldn't free: allocation doesn't exist");
454 return;
455 }
456
457 Allocation &allocation = iter->second;
458
459 switch (allocation.m_policy) {
460 default:
461 case eAllocationPolicyHostOnly: {
462 lldb::ProcessSP process_sp = m_process_wp.lock();
463 if (process_sp) {
464 if (process_sp->CanJIT() && process_sp->IsAlive())
465 process_sp->DeallocateMemory(
466 allocation.m_process_alloc); // FindSpace allocated this for real
467 }
468
469 break;
470 }
471 case eAllocationPolicyMirror:
472 case eAllocationPolicyProcessOnly: {
473 lldb::ProcessSP process_sp = m_process_wp.lock();
474 if (process_sp)
475 process_sp->DeallocateMemory(allocation.m_process_alloc);
476 }
477 }
478
479 if (lldb_private::Log *log =
480 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
481 log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
482 "..0x%" PRIx64 ")",
483 (uint64_t)process_address, iter->second.m_process_start,
484 iter->second.m_process_start + iter->second.m_size);
485 }
486
487 m_allocations.erase(iter);
488}
489
490bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
491 AllocationMap::iterator iter = FindAllocation(address, size);
492 if (iter == m_allocations.end())
493 return false;
494
495 Allocation &al = iter->second;
496
497 if (address > (al.m_process_start + al.m_size)) {
498 size = 0;
499 return false;
500 }
501
502 if (address > al.m_process_start) {
503 int dif = address - al.m_process_start;
504 size = al.m_size - dif;
505 return true;
506 }
507
508 size = al.m_size;
509 return true;
510}
511
512void IRMemoryMap::WriteMemory(lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000513 const uint8_t *bytes, size_t size,
514 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 error.Clear();
516
517 AllocationMap::iterator iter = FindAllocation(process_address, size);
518
519 if (iter == m_allocations.end()) {
520 lldb::ProcessSP process_sp = m_process_wp.lock();
521
522 if (process_sp) {
523 process_sp->WriteMemory(process_address, bytes, size, error);
524 return;
525 }
526
527 error.SetErrorToGenericError();
528 error.SetErrorString("Couldn't write: no allocation contains the target "
529 "range and the process doesn't exist");
530 return;
531 }
532
533 Allocation &allocation = iter->second;
534
535 uint64_t offset = process_address - allocation.m_process_start;
536
537 lldb::ProcessSP process_sp;
538
539 switch (allocation.m_policy) {
540 default:
541 error.SetErrorToGenericError();
542 error.SetErrorString("Couldn't write: invalid allocation policy");
543 return;
544 case eAllocationPolicyHostOnly:
545 if (!allocation.m_data.GetByteSize()) {
546 error.SetErrorToGenericError();
547 error.SetErrorString("Couldn't write: data buffer is empty");
548 return;
549 }
550 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
551 break;
552 case eAllocationPolicyMirror:
553 if (!allocation.m_data.GetByteSize()) {
554 error.SetErrorToGenericError();
555 error.SetErrorString("Couldn't write: data buffer is empty");
556 return;
557 }
558 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
559 process_sp = m_process_wp.lock();
560 if (process_sp) {
561 process_sp->WriteMemory(process_address, bytes, size, error);
562 if (!error.Success())
563 return;
564 }
565 break;
566 case eAllocationPolicyProcessOnly:
567 process_sp = m_process_wp.lock();
568 if (process_sp) {
569 process_sp->WriteMemory(process_address, bytes, size, error);
570 if (!error.Success())
571 return;
572 }
573 break;
574 }
575
576 if (lldb_private::Log *log =
577 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
578 log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64
579 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
580 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
581 (uint64_t)allocation.m_process_start,
582 (uint64_t)allocation.m_process_start +
583 (uint64_t)allocation.m_size);
584 }
585}
586
587void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
588 Scalar &scalar, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +0000589 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 error.Clear();
591
592 if (size == UINT32_MAX)
593 size = scalar.GetByteSize();
594
595 if (size > 0) {
596 uint8_t buf[32];
597 const size_t mem_size =
598 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
599 if (mem_size > 0) {
600 return WriteMemory(process_address, buf, mem_size, error);
601 } else {
602 error.SetErrorToGenericError();
603 error.SetErrorString(
604 "Couldn't write scalar: failed to get scalar as memory data");
605 }
606 } else {
607 error.SetErrorToGenericError();
608 error.SetErrorString("Couldn't write scalar: its size was zero");
609 }
610 return;
611}
612
613void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000614 lldb::addr_t address, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 error.Clear();
616
617 Scalar scalar(address);
618
619 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
620}
621
622void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000623 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 error.Clear();
625
626 AllocationMap::iterator iter = FindAllocation(process_address, size);
627
628 if (iter == m_allocations.end()) {
629 lldb::ProcessSP process_sp = m_process_wp.lock();
630
631 if (process_sp) {
632 process_sp->ReadMemory(process_address, bytes, size, error);
633 return;
634 }
635
636 lldb::TargetSP target_sp = m_target_wp.lock();
637
638 if (target_sp) {
639 Address absolute_address(process_address);
640 target_sp->ReadMemory(absolute_address, false, bytes, size, error);
641 return;
642 }
643
644 error.SetErrorToGenericError();
645 error.SetErrorString("Couldn't read: no allocation contains the target "
646 "range, and neither the process nor the target exist");
647 return;
648 }
649
650 Allocation &allocation = iter->second;
651
652 uint64_t offset = process_address - allocation.m_process_start;
653
654 if (offset > allocation.m_size) {
655 error.SetErrorToGenericError();
656 error.SetErrorString("Couldn't read: data is not in the allocation");
657 return;
658 }
659
660 lldb::ProcessSP process_sp;
661
662 switch (allocation.m_policy) {
663 default:
664 error.SetErrorToGenericError();
665 error.SetErrorString("Couldn't read: invalid allocation policy");
666 return;
667 case eAllocationPolicyHostOnly:
668 if (!allocation.m_data.GetByteSize()) {
669 error.SetErrorToGenericError();
670 error.SetErrorString("Couldn't read: data buffer is empty");
671 return;
672 }
673 if (allocation.m_data.GetByteSize() < offset + size) {
674 error.SetErrorToGenericError();
675 error.SetErrorString("Couldn't read: not enough underlying data");
676 return;
677 }
678
679 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
680 break;
681 case eAllocationPolicyMirror:
682 process_sp = m_process_wp.lock();
683 if (process_sp) {
684 process_sp->ReadMemory(process_address, bytes, size, error);
685 if (!error.Success())
686 return;
687 } else {
688 if (!allocation.m_data.GetByteSize()) {
689 error.SetErrorToGenericError();
690 error.SetErrorString("Couldn't read: data buffer is empty");
691 return;
692 }
693 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
694 }
695 break;
696 case eAllocationPolicyProcessOnly:
697 process_sp = m_process_wp.lock();
698 if (process_sp) {
699 process_sp->ReadMemory(process_address, bytes, size, error);
700 if (!error.Success())
701 return;
702 }
703 break;
704 }
705
706 if (lldb_private::Log *log =
707 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
708 log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64
709 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
710 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
711 (uint64_t)allocation.m_process_start,
712 (uint64_t)allocation.m_process_start +
713 (uint64_t)allocation.m_size);
714 }
715}
716
717void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
718 lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000719 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 error.Clear();
721
722 if (size > 0) {
723 DataBufferHeap buf(size, 0);
724 ReadMemory(buf.GetBytes(), process_address, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000725
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000726 if (!error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000728
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
730 GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000731
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 lldb::offset_t offset = 0;
733
734 switch (size) {
735 default:
736 error.SetErrorToGenericError();
737 error.SetErrorStringWithFormat(
738 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
739 return;
740 case 1:
741 scalar = extractor.GetU8(&offset);
742 break;
743 case 2:
744 scalar = extractor.GetU16(&offset);
745 break;
746 case 4:
747 scalar = extractor.GetU32(&offset);
748 break;
749 case 8:
750 scalar = extractor.GetU64(&offset);
751 break;
752 }
753 } else {
754 error.SetErrorToGenericError();
755 error.SetErrorString("Couldn't read scalar: its size was zero");
756 }
757 return;
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000758}
759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
761 lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000762 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000764
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 Scalar pointer_scalar;
766 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
767 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000768
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 if (!error.Success())
770 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000771
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 *address = pointer_scalar.ULongLong();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000773
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 return;
775}
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000776
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
778 lldb::addr_t process_address, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +0000779 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780 error.Clear();
781
782 if (size > 0) {
783 AllocationMap::iterator iter = FindAllocation(process_address, size);
784
785 if (iter == m_allocations.end()) {
786 error.SetErrorToGenericError();
787 error.SetErrorStringWithFormat(
788 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
789 ")",
790 process_address, process_address + size);
791 return;
Sean Callanan458ae1c2013-04-13 02:06:42 +0000792 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793
794 Allocation &allocation = iter->second;
795
796 switch (allocation.m_policy) {
797 default:
798 error.SetErrorToGenericError();
799 error.SetErrorString(
800 "Couldn't get memory data: invalid allocation policy");
801 return;
802 case eAllocationPolicyProcessOnly:
803 error.SetErrorToGenericError();
804 error.SetErrorString(
805 "Couldn't get memory data: memory is only in the target");
806 return;
807 case eAllocationPolicyMirror: {
808 lldb::ProcessSP process_sp = m_process_wp.lock();
809
810 if (!allocation.m_data.GetByteSize()) {
Sean Callanan458ae1c2013-04-13 02:06:42 +0000811 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 error.SetErrorString("Couldn't get memory data: data buffer is empty");
Sean Callanan458ae1c2013-04-13 02:06:42 +0000813 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 }
815 if (process_sp) {
816 process_sp->ReadMemory(allocation.m_process_start,
817 allocation.m_data.GetBytes(),
818 allocation.m_data.GetByteSize(), error);
819 if (!error.Success())
820 return;
821 uint64_t offset = process_address - allocation.m_process_start;
822 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
823 GetByteOrder(), GetAddressByteSize());
824 return;
825 }
826 } break;
827 case eAllocationPolicyHostOnly:
828 if (!allocation.m_data.GetByteSize()) {
829 error.SetErrorToGenericError();
830 error.SetErrorString("Couldn't get memory data: data buffer is empty");
831 return;
832 }
833 uint64_t offset = process_address - allocation.m_process_start;
834 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
835 GetByteOrder(), GetAddressByteSize());
836 return;
Sean Callanan458ae1c2013-04-13 02:06:42 +0000837 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 } else {
839 error.SetErrorToGenericError();
840 error.SetErrorString("Couldn't get memory data: its size was zero");
841 return;
842 }
Sean Callanan458ae1c2013-04-13 02:06:42 +0000843}