blob: 70e62ac12b0e827cece4cdf021b87819e41d7ca8 [file] [log] [blame]
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001//===-- IRMemoryMap.cpp -----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sean Callanan5a1af4e2013-04-05 02:22:57 +00006//
7//===----------------------------------------------------------------------===//
8
Kate Stoneb9c1b512016-09-06 20:57:50 +00009#include "lldb/Expression/IRMemoryMap.h"
Sean Callananf3df7e82016-06-09 20:22:25 +000010#include "lldb/Target/MemoryRegionInfo.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000011#include "lldb/Target/Process.h"
Sean Callanan35005f72013-04-12 18:10:34 +000012#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000013#include "lldb/Utility/DataBufferHeap.h"
14#include "lldb/Utility/DataExtractor.h"
Sean Callananf3df7e82016-06-09 20:22:25 +000015#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000016#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000017#include "lldb/Utility/Scalar.h"
Zachary Turner97206d52017-05-12 04:51:55 +000018#include "lldb/Utility/Status.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000019
20using namespace lldb_private;
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
23 if (target_sp)
24 m_process_wp = target_sp->GetProcessSP();
Sean Callanan5a1af4e2013-04-05 02:22:57 +000025}
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027IRMemoryMap::~IRMemoryMap() {
28 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000029
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 if (process_sp) {
31 AllocationMap::iterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000032
Zachary Turner97206d52017-05-12 04:51:55 +000033 Status err;
Sean Callananbb777042013-05-16 17:30:37 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 while ((iter = m_allocations.begin()) != m_allocations.end()) {
36 err.Clear();
37 if (iter->second.m_leak)
38 m_allocations.erase(iter);
39 else
40 Free(iter->first, err);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000041 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045lldb::addr_t IRMemoryMap::FindSpace(size_t size) {
46 // The FindSpace algorithm's job is to find a region of memory that the
47 // underlying process is unlikely to be using.
48 //
49 // The memory returned by this function will never be written to. The only
50 // point is that it should not shadow process memory if possible, so that
Adrian Prantl05097242018-04-30 16:49:04 +000051 // expressions processing real values from the process do not use the wrong
52 // data.
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 //
54 // If the process can in fact allocate memory (CanJIT() lets us know this)
55 // then this can be accomplished just be allocating memory in the inferior.
56 // Then no guessing is required.
Sean Callananf3df7e82016-06-09 20:22:25 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 lldb::TargetSP target_sp = m_target_wp.lock();
59 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 const bool process_is_alive = process_sp && process_sp->IsAlive();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
64 if (size == 0)
Sean Callananbb9945f2013-04-19 01:51:24 +000065 return ret;
Kate Stoneb9c1b512016-09-06 20:57:50 +000066
67 if (process_is_alive && process_sp->CanJIT()) {
Zachary Turner97206d52017-05-12 04:51:55 +000068 Status alloc_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000069
70 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
71 lldb::ePermissionsWritable,
72 alloc_error);
73
74 if (!alloc_error.Success())
75 return LLDB_INVALID_ADDRESS;
76 else
77 return ret;
78 }
79
80 // At this point we know that we need to hunt.
81 //
82 // First, go to the end of the existing allocations we've made if there are
83 // any allocations. Otherwise start at the beginning of memory.
84
85 if (m_allocations.empty()) {
86 ret = 0x0;
87 } else {
88 auto back = m_allocations.rbegin();
89 lldb::addr_t addr = back->first;
90 size_t alloc_size = back->second.m_size;
91 ret = llvm::alignTo(addr + alloc_size, 4096);
92 }
93
94 // Now, if it's possible to use the GetMemoryRegionInfo API to detect mapped
Adrian Prantl05097242018-04-30 16:49:04 +000095 // regions, walk forward through memory until a region is found that has
96 // adequate space for our allocation.
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 if (process_is_alive) {
98 const uint64_t end_of_memory = process_sp->GetAddressByteSize() == 8
99 ? 0xffffffffffffffffull
100 : 0xffffffffull;
101
102 lldbassert(process_sp->GetAddressByteSize() == 4 ||
103 end_of_memory != 0xffffffffull);
104
105 MemoryRegionInfo region_info;
Zachary Turner97206d52017-05-12 04:51:55 +0000106 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 if (err.Success()) {
108 while (true) {
109 if (region_info.GetReadable() != MemoryRegionInfo::OptionalBool::eNo ||
110 region_info.GetWritable() != MemoryRegionInfo::OptionalBool::eNo ||
111 region_info.GetExecutable() !=
112 MemoryRegionInfo::OptionalBool::eNo) {
113 if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory) {
114 ret = LLDB_INVALID_ADDRESS;
115 break;
116 } else {
117 ret = region_info.GetRange().GetRangeEnd();
118 }
119 } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
120 return ret;
121 } else {
122 // ret stays the same. We just need to walk a bit further.
123 }
124
125 err = process_sp->GetMemoryRegionInfo(
126 region_info.GetRange().GetRangeEnd(), region_info);
127 if (err.Fail()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +0000128 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 ret = LLDB_INVALID_ADDRESS;
130 break;
131 }
132 }
133 }
134 }
135
136 // We've tried our algorithm, and it didn't work. Now we have to reset back
137 // to the end of the allocations we've already reported, or use a 'sensible'
138 // default if this is our first allocation.
139
140 if (m_allocations.empty()) {
141 uint32_t address_byte_size = GetAddressByteSize();
142 if (address_byte_size != UINT32_MAX) {
143 switch (address_byte_size) {
144 case 8:
145 ret = 0xffffffff00000000ull;
146 break;
147 case 4:
148 ret = 0xee000000ull;
149 break;
150 default:
151 break;
152 }
153 }
154 } else {
155 auto back = m_allocations.rbegin();
156 lldb::addr_t addr = back->first;
157 size_t alloc_size = back->second.m_size;
158 ret = llvm::alignTo(addr + alloc_size, 4096);
159 }
160
161 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000162}
163
164IRMemoryMap::AllocationMap::iterator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165IRMemoryMap::FindAllocation(lldb::addr_t addr, size_t size) {
166 if (addr == LLDB_INVALID_ADDRESS)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000167 return m_allocations.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168
169 AllocationMap::iterator iter = m_allocations.lower_bound(addr);
170
171 if (iter == m_allocations.end() || iter->first > addr) {
172 if (iter == m_allocations.begin())
173 return m_allocations.end();
174 iter--;
175 }
176
177 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
178 return iter;
179
180 return m_allocations.end();
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
184 if (addr == LLDB_INVALID_ADDRESS)
Sean Callananbb9945f2013-04-19 01:51:24 +0000185 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186
187 AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
188
189 // Since we only know that the returned interval begins at a location greater
Adrian Prantl05097242018-04-30 16:49:04 +0000190 // than or equal to where the given interval begins, it's possible that the
191 // given interval intersects either the returned interval or the previous
192 // interval. Thus, we need to check both. Note that we only need to check
193 // these two intervals. Since all intervals are disjoint it is not possible
194 // that an adjacent interval does not intersect, but a non-adjacent interval
195 // does intersect.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 if (iter != m_allocations.end()) {
197 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
198 iter->second.m_size))
199 return true;
200 }
201
202 if (iter != m_allocations.begin()) {
203 --iter;
204 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
205 iter->second.m_size))
206 return true;
207 }
208
209 return false;
Sean Callananbb9945f2013-04-19 01:51:24 +0000210}
211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212bool IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1,
213 lldb::addr_t addr2, size_t size2) {
214 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
Adrian Prantl05097242018-04-30 16:49:04 +0000215 // that satisfy A<B and X<Y are the following:
Zachary Turner15362442014-06-25 18:37:19 +0000216 // A B X Y
217 // A X B Y (intersects)
218 // A X Y B (intersects)
219 // X A B Y (intersects)
220 // X A Y B (intersects)
221 // X Y A B
Adrian Prantl05097242018-04-30 16:49:04 +0000222 // The first is B <= X, and the last is Y <= A. So the condition is !(B <= X
223 // || Y <= A)), or (X < B && A < Y)
Zachary Turner15362442014-06-25 18:37:19 +0000224 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
225}
226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227lldb::ByteOrder IRMemoryMap::GetByteOrder() {
228 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 if (process_sp)
231 return process_sp->GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 if (target_sp)
236 return target_sp->GetArchitecture().GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 return lldb::eByteOrderInvalid;
Sean Callanan35005f72013-04-12 18:10:34 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241uint32_t IRMemoryMap::GetAddressByteSize() {
242 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (process_sp)
245 return process_sp->GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 if (target_sp)
250 return target_sp->GetArchitecture().GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 return UINT32_MAX;
Sean Callanan35005f72013-04-12 18:10:34 +0000253}
254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255ExecutionContextScope *IRMemoryMap::GetBestExecutionContextScope() const {
256 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 if (process_sp)
259 return process_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 if (target_sp)
264 return target_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000265
Konrad Kleine248a1302019-05-23 11:14:47 +0000266 return nullptr;
Sean Callanan35005f72013-04-12 18:10:34 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269IRMemoryMap::Allocation::Allocation(lldb::addr_t process_alloc,
270 lldb::addr_t process_start, size_t size,
271 uint32_t permissions, uint8_t alignment,
272 AllocationPolicy policy)
273 : m_process_alloc(process_alloc), m_process_start(process_start),
Vedant Kumarf71dd342018-08-08 21:26:49 +0000274 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
275 m_alignment(alignment) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 switch (policy) {
277 default:
Fangrui Song35861f22019-04-12 02:38:17 +0000278 llvm_unreachable("Invalid AllocationPolicy");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 case eAllocationPolicyHostOnly:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 case eAllocationPolicyMirror:
281 m_data.SetByteSize(size);
Vedant Kumar94523592018-08-06 20:13:52 +0000282 break;
283 case eAllocationPolicyProcessOnly:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 break;
285 }
286}
287
288lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
289 uint32_t permissions, AllocationPolicy policy,
Zachary Turner97206d52017-05-12 04:51:55 +0000290 bool zero_memory, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 lldb_private::Log *log(
292 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
293 error.Clear();
294
295 lldb::ProcessSP process_sp;
296 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
297 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 size_t allocation_size;
300
Vedant Kumar5b71e752018-05-31 22:08:59 +0000301 if (size == 0) {
302 // FIXME: Malloc(0) should either return an invalid address or assert, in
303 // order to cut down on unnecessary allocations.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 allocation_size = alignment;
Vedant Kumar5b71e752018-05-31 22:08:59 +0000305 } else {
306 // Round up the requested size to an aligned value.
307 allocation_size = llvm::alignTo(size, alignment);
308
309 // The process page cache does not see the requested alignment. We can't
310 // assume its result will be any more than 1-byte aligned. To work around
311 // this, request `alignment - 1` additional bytes.
312 allocation_size += alignment - 1;
313 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314
315 switch (policy) {
316 default:
317 error.SetErrorToGenericError();
318 error.SetErrorString("Couldn't malloc: invalid allocation policy");
319 return LLDB_INVALID_ADDRESS;
320 case eAllocationPolicyHostOnly:
321 allocation_address = FindSpace(allocation_size);
322 if (allocation_address == LLDB_INVALID_ADDRESS) {
323 error.SetErrorToGenericError();
324 error.SetErrorString("Couldn't malloc: address space is full");
325 return LLDB_INVALID_ADDRESS;
Sean Callanand2562502013-04-19 17:44:40 +0000326 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 break;
328 case eAllocationPolicyMirror:
329 process_sp = m_process_wp.lock();
Todd Fialaaf245d12014-06-30 21:05:18 +0000330 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 log->Printf("IRMemoryMap::%s process_sp=0x%" PRIx64
332 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
333 __FUNCTION__, (lldb::addr_t)process_sp.get(),
334 process_sp && process_sp->CanJIT() ? "true" : "false",
335 process_sp && process_sp->IsAlive() ? "true" : "false");
336 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
337 if (!zero_memory)
338 allocation_address =
339 process_sp->AllocateMemory(allocation_size, permissions, error);
340 else
341 allocation_address =
342 process_sp->CallocateMemory(allocation_size, permissions, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 if (!error.Success())
345 return LLDB_INVALID_ADDRESS;
346 } else {
347 if (log)
348 log->Printf("IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
349 "due to failed condition (see previous expr log message)",
350 __FUNCTION__);
351 policy = eAllocationPolicyHostOnly;
352 allocation_address = FindSpace(allocation_size);
353 if (allocation_address == LLDB_INVALID_ADDRESS) {
Sean Callananfbf5c682013-05-22 22:49:06 +0000354 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 error.SetErrorString("Couldn't malloc: address space is full");
356 return LLDB_INVALID_ADDRESS;
357 }
Sean Callananfbf5c682013-05-22 22:49:06 +0000358 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 break;
360 case eAllocationPolicyProcessOnly:
361 process_sp = m_process_wp.lock();
362 if (process_sp) {
363 if (process_sp->CanJIT() && process_sp->IsAlive()) {
364 if (!zero_memory)
365 allocation_address =
366 process_sp->AllocateMemory(allocation_size, permissions, error);
Sean Callanan35005f72013-04-12 18:10:34 +0000367 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 allocation_address =
369 process_sp->CallocateMemory(allocation_size, permissions, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000370
Sean Callanan35005f72013-04-12 18:10:34 +0000371 if (!error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 return LLDB_INVALID_ADDRESS;
373 } else {
Sean Callanan35005f72013-04-12 18:10:34 +0000374 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 error.SetErrorString(
376 "Couldn't malloc: process doesn't support allocating memory");
377 return LLDB_INVALID_ADDRESS;
378 }
379 } else {
380 error.SetErrorToGenericError();
381 error.SetErrorString("Couldn't malloc: process doesn't exist, and this "
382 "memory must be in the process");
383 return LLDB_INVALID_ADDRESS;
Sean Callanan35005f72013-04-12 18:10:34 +0000384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 break;
386 }
387
388 lldb::addr_t mask = alignment - 1;
389 aligned_address = (allocation_address + mask) & (~mask);
390
Vedant Kumarf71dd342018-08-08 21:26:49 +0000391 m_allocations.emplace(
392 std::piecewise_construct, std::forward_as_tuple(aligned_address),
393 std::forward_as_tuple(allocation_address, aligned_address,
394 allocation_size, permissions, alignment, policy));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395
396 if (zero_memory) {
Zachary Turner97206d52017-05-12 04:51:55 +0000397 Status write_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 std::vector<uint8_t> zero_buf(size, 0);
399 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
400 }
401
402 if (log) {
403 const char *policy_string;
404
405 switch (policy) {
406 default:
407 policy_string = "<invalid policy>";
408 break;
409 case eAllocationPolicyHostOnly:
410 policy_string = "eAllocationPolicyHostOnly";
411 break;
412 case eAllocationPolicyProcessOnly:
413 policy_string = "eAllocationPolicyProcessOnly";
414 break;
415 case eAllocationPolicyMirror:
416 policy_string = "eAllocationPolicyMirror";
417 break;
418 }
419
420 log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64
421 ", %s) -> 0x%" PRIx64,
422 (uint64_t)allocation_size, (uint64_t)alignment,
423 (uint64_t)permissions, policy_string, aligned_address);
424 }
425
426 return aligned_address;
Sean Callanan35005f72013-04-12 18:10:34 +0000427}
428
Zachary Turner97206d52017-05-12 04:51:55 +0000429void IRMemoryMap::Leak(lldb::addr_t process_address, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 AllocationMap::iterator iter = m_allocations.find(process_address);
433
434 if (iter == m_allocations.end()) {
435 error.SetErrorToGenericError();
436 error.SetErrorString("Couldn't leak: allocation doesn't exist");
437 return;
438 }
439
440 Allocation &allocation = iter->second;
441
442 allocation.m_leak = true;
443}
444
Zachary Turner97206d52017-05-12 04:51:55 +0000445void IRMemoryMap::Free(lldb::addr_t process_address, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 error.Clear();
447
448 AllocationMap::iterator iter = m_allocations.find(process_address);
449
450 if (iter == m_allocations.end()) {
451 error.SetErrorToGenericError();
452 error.SetErrorString("Couldn't free: allocation doesn't exist");
453 return;
454 }
455
456 Allocation &allocation = iter->second;
457
458 switch (allocation.m_policy) {
459 default:
460 case eAllocationPolicyHostOnly: {
461 lldb::ProcessSP process_sp = m_process_wp.lock();
462 if (process_sp) {
463 if (process_sp->CanJIT() && process_sp->IsAlive())
464 process_sp->DeallocateMemory(
465 allocation.m_process_alloc); // FindSpace allocated this for real
466 }
467
468 break;
469 }
470 case eAllocationPolicyMirror:
471 case eAllocationPolicyProcessOnly: {
472 lldb::ProcessSP process_sp = m_process_wp.lock();
473 if (process_sp)
474 process_sp->DeallocateMemory(allocation.m_process_alloc);
475 }
476 }
477
478 if (lldb_private::Log *log =
479 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
480 log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
481 "..0x%" PRIx64 ")",
482 (uint64_t)process_address, iter->second.m_process_start,
483 iter->second.m_process_start + iter->second.m_size);
484 }
485
486 m_allocations.erase(iter);
487}
488
489bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
490 AllocationMap::iterator iter = FindAllocation(address, size);
491 if (iter == m_allocations.end())
492 return false;
493
494 Allocation &al = iter->second;
495
496 if (address > (al.m_process_start + al.m_size)) {
497 size = 0;
498 return false;
499 }
500
501 if (address > al.m_process_start) {
502 int dif = address - al.m_process_start;
503 size = al.m_size - dif;
504 return true;
505 }
506
507 size = al.m_size;
508 return true;
509}
510
511void IRMemoryMap::WriteMemory(lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000512 const uint8_t *bytes, size_t size,
513 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 error.Clear();
515
516 AllocationMap::iterator iter = FindAllocation(process_address, size);
517
518 if (iter == m_allocations.end()) {
519 lldb::ProcessSP process_sp = m_process_wp.lock();
520
521 if (process_sp) {
522 process_sp->WriteMemory(process_address, bytes, size, error);
523 return;
524 }
525
526 error.SetErrorToGenericError();
527 error.SetErrorString("Couldn't write: no allocation contains the target "
528 "range and the process doesn't exist");
529 return;
530 }
531
532 Allocation &allocation = iter->second;
533
534 uint64_t offset = process_address - allocation.m_process_start;
535
536 lldb::ProcessSP process_sp;
537
538 switch (allocation.m_policy) {
539 default:
540 error.SetErrorToGenericError();
541 error.SetErrorString("Couldn't write: invalid allocation policy");
542 return;
543 case eAllocationPolicyHostOnly:
544 if (!allocation.m_data.GetByteSize()) {
545 error.SetErrorToGenericError();
546 error.SetErrorString("Couldn't write: data buffer is empty");
547 return;
548 }
549 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
550 break;
551 case eAllocationPolicyMirror:
552 if (!allocation.m_data.GetByteSize()) {
553 error.SetErrorToGenericError();
554 error.SetErrorString("Couldn't write: data buffer is empty");
555 return;
556 }
557 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
558 process_sp = m_process_wp.lock();
559 if (process_sp) {
560 process_sp->WriteMemory(process_address, bytes, size, error);
561 if (!error.Success())
562 return;
563 }
564 break;
565 case eAllocationPolicyProcessOnly:
566 process_sp = m_process_wp.lock();
567 if (process_sp) {
568 process_sp->WriteMemory(process_address, bytes, size, error);
569 if (!error.Success())
570 return;
571 }
572 break;
573 }
574
575 if (lldb_private::Log *log =
576 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
577 log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64
578 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
579 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
580 (uint64_t)allocation.m_process_start,
581 (uint64_t)allocation.m_process_start +
582 (uint64_t)allocation.m_size);
583 }
584}
585
586void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
587 Scalar &scalar, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +0000588 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 error.Clear();
590
591 if (size == UINT32_MAX)
592 size = scalar.GetByteSize();
593
594 if (size > 0) {
595 uint8_t buf[32];
596 const size_t mem_size =
597 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
598 if (mem_size > 0) {
599 return WriteMemory(process_address, buf, mem_size, error);
600 } else {
601 error.SetErrorToGenericError();
602 error.SetErrorString(
603 "Couldn't write scalar: failed to get scalar as memory data");
604 }
605 } else {
606 error.SetErrorToGenericError();
607 error.SetErrorString("Couldn't write scalar: its size was zero");
608 }
609 return;
610}
611
612void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000613 lldb::addr_t address, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 error.Clear();
615
616 Scalar scalar(address);
617
618 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
619}
620
621void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000622 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 error.Clear();
624
625 AllocationMap::iterator iter = FindAllocation(process_address, size);
626
627 if (iter == m_allocations.end()) {
628 lldb::ProcessSP process_sp = m_process_wp.lock();
629
630 if (process_sp) {
631 process_sp->ReadMemory(process_address, bytes, size, error);
632 return;
633 }
634
635 lldb::TargetSP target_sp = m_target_wp.lock();
636
637 if (target_sp) {
638 Address absolute_address(process_address);
639 target_sp->ReadMemory(absolute_address, false, bytes, size, error);
640 return;
641 }
642
643 error.SetErrorToGenericError();
644 error.SetErrorString("Couldn't read: no allocation contains the target "
645 "range, and neither the process nor the target exist");
646 return;
647 }
648
649 Allocation &allocation = iter->second;
650
651 uint64_t offset = process_address - allocation.m_process_start;
652
653 if (offset > allocation.m_size) {
654 error.SetErrorToGenericError();
655 error.SetErrorString("Couldn't read: data is not in the allocation");
656 return;
657 }
658
659 lldb::ProcessSP process_sp;
660
661 switch (allocation.m_policy) {
662 default:
663 error.SetErrorToGenericError();
664 error.SetErrorString("Couldn't read: invalid allocation policy");
665 return;
666 case eAllocationPolicyHostOnly:
667 if (!allocation.m_data.GetByteSize()) {
668 error.SetErrorToGenericError();
669 error.SetErrorString("Couldn't read: data buffer is empty");
670 return;
671 }
672 if (allocation.m_data.GetByteSize() < offset + size) {
673 error.SetErrorToGenericError();
674 error.SetErrorString("Couldn't read: not enough underlying data");
675 return;
676 }
677
678 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
679 break;
680 case eAllocationPolicyMirror:
681 process_sp = m_process_wp.lock();
682 if (process_sp) {
683 process_sp->ReadMemory(process_address, bytes, size, error);
684 if (!error.Success())
685 return;
686 } else {
687 if (!allocation.m_data.GetByteSize()) {
688 error.SetErrorToGenericError();
689 error.SetErrorString("Couldn't read: data buffer is empty");
690 return;
691 }
692 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
693 }
694 break;
695 case eAllocationPolicyProcessOnly:
696 process_sp = m_process_wp.lock();
697 if (process_sp) {
698 process_sp->ReadMemory(process_address, bytes, size, error);
699 if (!error.Success())
700 return;
701 }
702 break;
703 }
704
705 if (lldb_private::Log *log =
706 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
707 log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64
708 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
709 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
710 (uint64_t)allocation.m_process_start,
711 (uint64_t)allocation.m_process_start +
712 (uint64_t)allocation.m_size);
713 }
714}
715
716void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
717 lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000718 size_t size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 error.Clear();
720
721 if (size > 0) {
722 DataBufferHeap buf(size, 0);
723 ReadMemory(buf.GetBytes(), process_address, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000724
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000725 if (!error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000727
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
729 GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000730
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 lldb::offset_t offset = 0;
732
733 switch (size) {
734 default:
735 error.SetErrorToGenericError();
736 error.SetErrorStringWithFormat(
737 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
738 return;
739 case 1:
740 scalar = extractor.GetU8(&offset);
741 break;
742 case 2:
743 scalar = extractor.GetU16(&offset);
744 break;
745 case 4:
746 scalar = extractor.GetU32(&offset);
747 break;
748 case 8:
749 scalar = extractor.GetU64(&offset);
750 break;
751 }
752 } else {
753 error.SetErrorToGenericError();
754 error.SetErrorString("Couldn't read scalar: its size was zero");
755 }
756 return;
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000757}
758
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
760 lldb::addr_t process_address,
Zachary Turner97206d52017-05-12 04:51:55 +0000761 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 Scalar pointer_scalar;
765 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
766 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000767
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 if (!error.Success())
769 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000770
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771 *address = pointer_scalar.ULongLong();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 return;
774}
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000775
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
777 lldb::addr_t process_address, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +0000778 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 error.Clear();
780
781 if (size > 0) {
782 AllocationMap::iterator iter = FindAllocation(process_address, size);
783
784 if (iter == m_allocations.end()) {
785 error.SetErrorToGenericError();
786 error.SetErrorStringWithFormat(
787 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
788 ")",
789 process_address, process_address + size);
790 return;
Sean Callanan458ae1c2013-04-13 02:06:42 +0000791 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792
793 Allocation &allocation = iter->second;
794
795 switch (allocation.m_policy) {
796 default:
797 error.SetErrorToGenericError();
798 error.SetErrorString(
799 "Couldn't get memory data: invalid allocation policy");
800 return;
801 case eAllocationPolicyProcessOnly:
802 error.SetErrorToGenericError();
803 error.SetErrorString(
804 "Couldn't get memory data: memory is only in the target");
805 return;
806 case eAllocationPolicyMirror: {
807 lldb::ProcessSP process_sp = m_process_wp.lock();
808
809 if (!allocation.m_data.GetByteSize()) {
Sean Callanan458ae1c2013-04-13 02:06:42 +0000810 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811 error.SetErrorString("Couldn't get memory data: data buffer is empty");
Sean Callanan458ae1c2013-04-13 02:06:42 +0000812 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813 }
814 if (process_sp) {
815 process_sp->ReadMemory(allocation.m_process_start,
816 allocation.m_data.GetBytes(),
817 allocation.m_data.GetByteSize(), error);
818 if (!error.Success())
819 return;
820 uint64_t offset = process_address - allocation.m_process_start;
821 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
822 GetByteOrder(), GetAddressByteSize());
823 return;
824 }
825 } break;
826 case eAllocationPolicyHostOnly:
827 if (!allocation.m_data.GetByteSize()) {
828 error.SetErrorToGenericError();
829 error.SetErrorString("Couldn't get memory data: data buffer is empty");
830 return;
831 }
832 uint64_t offset = process_address - allocation.m_process_start;
833 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
834 GetByteOrder(), GetAddressByteSize());
835 return;
Sean Callanan458ae1c2013-04-13 02:06:42 +0000836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 } else {
838 error.SetErrorToGenericError();
839 error.SetErrorString("Couldn't get memory data: its size was zero");
840 return;
841 }
Sean Callanan458ae1c2013-04-13 02:06:42 +0000842}