1 //===-- IRMemoryMap.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Expression/IRMemoryMap.h"
11 #include "lldb/Core/DataBufferHeap.h"
12 #include "lldb/Core/DataExtractor.h"
13 #include "lldb/Core/Error.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Scalar.h"
16 #include "lldb/Target/MemoryRegionInfo.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Utility/LLDBAssert.h"
21 using namespace lldb_private;
23 IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
25 m_process_wp = target_sp->GetProcessSP();
28 IRMemoryMap::~IRMemoryMap() {
29 lldb::ProcessSP process_sp = m_process_wp.lock();
32 AllocationMap::iterator iter;
36 while ((iter = m_allocations.begin()) != m_allocations.end()) {
38 if (iter->second.m_leak)
39 m_allocations.erase(iter);
41 Free(iter->first, err);
46 lldb::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.
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
52 // expressions processing real values from the process do not use the
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.
59 lldb::TargetSP target_sp = m_target_wp.lock();
60 lldb::ProcessSP process_sp = m_process_wp.lock();
62 const bool process_is_alive = process_sp && process_sp->IsAlive();
64 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
68 if (process_is_alive && process_sp->CanJIT()) {
71 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
72 lldb::ePermissionsWritable,
75 if (!alloc_error.Success())
76 return LLDB_INVALID_ADDRESS;
81 // At this point we know that we need to hunt.
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.
86 if (m_allocations.empty()) {
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);
95 // Now, if it's possible to use the GetMemoryRegionInfo API to detect mapped
96 // regions, walk forward through memory until a region is found that
97 // has adequate space for our allocation.
98 if (process_is_alive) {
99 const uint64_t end_of_memory = process_sp->GetAddressByteSize() == 8
100 ? 0xffffffffffffffffull
103 lldbassert(process_sp->GetAddressByteSize() == 4 ||
104 end_of_memory != 0xffffffffull);
106 MemoryRegionInfo region_info;
107 Error err = process_sp->GetMemoryRegionInfo(ret, region_info);
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;
118 ret = region_info.GetRange().GetRangeEnd();
120 } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
123 // ret stays the same. We just need to walk a bit further.
126 err = process_sp->GetMemoryRegionInfo(
127 region_info.GetRange().GetRangeEnd(), region_info);
129 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
130 ret = LLDB_INVALID_ADDRESS;
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.
141 if (m_allocations.empty()) {
142 uint32_t address_byte_size = GetAddressByteSize();
143 if (address_byte_size != UINT32_MAX) {
144 switch (address_byte_size) {
146 ret = 0xffffffff00000000ull;
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);
165 IRMemoryMap::AllocationMap::iterator
166 IRMemoryMap::FindAllocation(lldb::addr_t addr, size_t size) {
167 if (addr == LLDB_INVALID_ADDRESS)
168 return m_allocations.end();
170 AllocationMap::iterator iter = m_allocations.lower_bound(addr);
172 if (iter == m_allocations.end() || iter->first > addr) {
173 if (iter == m_allocations.begin())
174 return m_allocations.end();
178 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
181 return m_allocations.end();
184 bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
185 if (addr == LLDB_INVALID_ADDRESS)
188 AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
190 // Since we only know that the returned interval begins at a location greater
192 // equal to where the given interval begins, it's possible that the given
194 // intersects either the returned interval or the previous interval. Thus, we
196 // check both. Note that we only need to check these two intervals. Since all
198 // are disjoint it is not possible that an adjacent interval does not
200 // non-adjacent interval does intersect.
201 if (iter != m_allocations.end()) {
202 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
203 iter->second.m_size))
207 if (iter != m_allocations.begin()) {
209 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
210 iter->second.m_size))
217 bool IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1,
218 lldb::addr_t addr2, size_t size2) {
219 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
221 // A<B and X<Y are the following:
223 // A X B Y (intersects)
224 // A X Y B (intersects)
225 // X A B Y (intersects)
226 // X A Y B (intersects)
228 // The first is B <= X, and the last is Y <= A.
229 // So the condition is !(B <= X || Y <= A)), or (X < B && A < Y)
230 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
233 lldb::ByteOrder IRMemoryMap::GetByteOrder() {
234 lldb::ProcessSP process_sp = m_process_wp.lock();
237 return process_sp->GetByteOrder();
239 lldb::TargetSP target_sp = m_target_wp.lock();
242 return target_sp->GetArchitecture().GetByteOrder();
244 return lldb::eByteOrderInvalid;
247 uint32_t IRMemoryMap::GetAddressByteSize() {
248 lldb::ProcessSP process_sp = m_process_wp.lock();
251 return process_sp->GetAddressByteSize();
253 lldb::TargetSP target_sp = m_target_wp.lock();
256 return target_sp->GetArchitecture().GetAddressByteSize();
261 ExecutionContextScope *IRMemoryMap::GetBestExecutionContextScope() const {
262 lldb::ProcessSP process_sp = m_process_wp.lock();
265 return process_sp.get();
267 lldb::TargetSP target_sp = m_target_wp.lock();
270 return target_sp.get();
275 IRMemoryMap::Allocation::Allocation(lldb::addr_t process_alloc,
276 lldb::addr_t process_start, size_t size,
277 uint32_t permissions, uint8_t alignment,
278 AllocationPolicy policy)
279 : m_process_alloc(process_alloc), m_process_start(process_start),
280 m_size(size), m_permissions(permissions), m_alignment(alignment),
281 m_policy(policy), m_leak(false) {
284 assert(0 && "We cannot reach this!");
285 case eAllocationPolicyHostOnly:
286 m_data.SetByteSize(size);
287 memset(m_data.GetBytes(), 0, size);
289 case eAllocationPolicyProcessOnly:
291 case eAllocationPolicyMirror:
292 m_data.SetByteSize(size);
293 memset(m_data.GetBytes(), 0, size);
298 lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
299 uint32_t permissions, AllocationPolicy policy,
300 bool zero_memory, Error &error) {
301 lldb_private::Log *log(
302 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
305 lldb::ProcessSP process_sp;
306 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
307 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
309 size_t alignment_mask = alignment - 1;
310 size_t allocation_size;
313 allocation_size = alignment;
315 allocation_size = (size & alignment_mask)
316 ? ((size + alignment) & (~alignment_mask))
321 error.SetErrorToGenericError();
322 error.SetErrorString("Couldn't malloc: invalid allocation policy");
323 return LLDB_INVALID_ADDRESS;
324 case eAllocationPolicyHostOnly:
325 allocation_address = FindSpace(allocation_size);
326 if (allocation_address == LLDB_INVALID_ADDRESS) {
327 error.SetErrorToGenericError();
328 error.SetErrorString("Couldn't malloc: address space is full");
329 return LLDB_INVALID_ADDRESS;
332 case eAllocationPolicyMirror:
333 process_sp = m_process_wp.lock();
335 log->Printf("IRMemoryMap::%s process_sp=0x%" PRIx64
336 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
337 __FUNCTION__, (lldb::addr_t)process_sp.get(),
338 process_sp && process_sp->CanJIT() ? "true" : "false",
339 process_sp && process_sp->IsAlive() ? "true" : "false");
340 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
343 process_sp->AllocateMemory(allocation_size, permissions, error);
346 process_sp->CallocateMemory(allocation_size, permissions, error);
348 if (!error.Success())
349 return LLDB_INVALID_ADDRESS;
352 log->Printf("IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
353 "due to failed condition (see previous expr log message)",
355 policy = eAllocationPolicyHostOnly;
356 allocation_address = FindSpace(allocation_size);
357 if (allocation_address == LLDB_INVALID_ADDRESS) {
358 error.SetErrorToGenericError();
359 error.SetErrorString("Couldn't malloc: address space is full");
360 return LLDB_INVALID_ADDRESS;
364 case eAllocationPolicyProcessOnly:
365 process_sp = m_process_wp.lock();
367 if (process_sp->CanJIT() && process_sp->IsAlive()) {
370 process_sp->AllocateMemory(allocation_size, permissions, error);
373 process_sp->CallocateMemory(allocation_size, permissions, error);
375 if (!error.Success())
376 return LLDB_INVALID_ADDRESS;
378 error.SetErrorToGenericError();
379 error.SetErrorString(
380 "Couldn't malloc: process doesn't support allocating memory");
381 return LLDB_INVALID_ADDRESS;
384 error.SetErrorToGenericError();
385 error.SetErrorString("Couldn't malloc: process doesn't exist, and this "
386 "memory must be in the process");
387 return LLDB_INVALID_ADDRESS;
392 lldb::addr_t mask = alignment - 1;
393 aligned_address = (allocation_address + mask) & (~mask);
395 m_allocations[aligned_address] =
396 Allocation(allocation_address, aligned_address, allocation_size,
397 permissions, alignment, policy);
401 std::vector<uint8_t> zero_buf(size, 0);
402 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
406 const char *policy_string;
410 policy_string = "<invalid policy>";
412 case eAllocationPolicyHostOnly:
413 policy_string = "eAllocationPolicyHostOnly";
415 case eAllocationPolicyProcessOnly:
416 policy_string = "eAllocationPolicyProcessOnly";
418 case eAllocationPolicyMirror:
419 policy_string = "eAllocationPolicyMirror";
423 log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64
424 ", %s) -> 0x%" PRIx64,
425 (uint64_t)allocation_size, (uint64_t)alignment,
426 (uint64_t)permissions, policy_string, aligned_address);
429 return aligned_address;
432 void IRMemoryMap::Leak(lldb::addr_t process_address, Error &error) {
435 AllocationMap::iterator iter = m_allocations.find(process_address);
437 if (iter == m_allocations.end()) {
438 error.SetErrorToGenericError();
439 error.SetErrorString("Couldn't leak: allocation doesn't exist");
443 Allocation &allocation = iter->second;
445 allocation.m_leak = true;
448 void IRMemoryMap::Free(lldb::addr_t process_address, Error &error) {
451 AllocationMap::iterator iter = m_allocations.find(process_address);
453 if (iter == m_allocations.end()) {
454 error.SetErrorToGenericError();
455 error.SetErrorString("Couldn't free: allocation doesn't exist");
459 Allocation &allocation = iter->second;
461 switch (allocation.m_policy) {
463 case eAllocationPolicyHostOnly: {
464 lldb::ProcessSP process_sp = m_process_wp.lock();
466 if (process_sp->CanJIT() && process_sp->IsAlive())
467 process_sp->DeallocateMemory(
468 allocation.m_process_alloc); // FindSpace allocated this for real
473 case eAllocationPolicyMirror:
474 case eAllocationPolicyProcessOnly: {
475 lldb::ProcessSP process_sp = m_process_wp.lock();
477 process_sp->DeallocateMemory(allocation.m_process_alloc);
481 if (lldb_private::Log *log =
482 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
483 log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
485 (uint64_t)process_address, iter->second.m_process_start,
486 iter->second.m_process_start + iter->second.m_size);
489 m_allocations.erase(iter);
492 bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
493 AllocationMap::iterator iter = FindAllocation(address, size);
494 if (iter == m_allocations.end())
497 Allocation &al = iter->second;
499 if (address > (al.m_process_start + al.m_size)) {
504 if (address > al.m_process_start) {
505 int dif = address - al.m_process_start;
506 size = al.m_size - dif;
514 void IRMemoryMap::WriteMemory(lldb::addr_t process_address,
515 const uint8_t *bytes, size_t size, Error &error) {
518 AllocationMap::iterator iter = FindAllocation(process_address, size);
520 if (iter == m_allocations.end()) {
521 lldb::ProcessSP process_sp = m_process_wp.lock();
524 process_sp->WriteMemory(process_address, bytes, size, error);
528 error.SetErrorToGenericError();
529 error.SetErrorString("Couldn't write: no allocation contains the target "
530 "range and the process doesn't exist");
534 Allocation &allocation = iter->second;
536 uint64_t offset = process_address - allocation.m_process_start;
538 lldb::ProcessSP process_sp;
540 switch (allocation.m_policy) {
542 error.SetErrorToGenericError();
543 error.SetErrorString("Couldn't write: invalid allocation policy");
545 case eAllocationPolicyHostOnly:
546 if (!allocation.m_data.GetByteSize()) {
547 error.SetErrorToGenericError();
548 error.SetErrorString("Couldn't write: data buffer is empty");
551 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
553 case eAllocationPolicyMirror:
554 if (!allocation.m_data.GetByteSize()) {
555 error.SetErrorToGenericError();
556 error.SetErrorString("Couldn't write: data buffer is empty");
559 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
560 process_sp = m_process_wp.lock();
562 process_sp->WriteMemory(process_address, bytes, size, error);
563 if (!error.Success())
567 case eAllocationPolicyProcessOnly:
568 process_sp = m_process_wp.lock();
570 process_sp->WriteMemory(process_address, bytes, size, error);
571 if (!error.Success())
577 if (lldb_private::Log *log =
578 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
579 log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64
580 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
581 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
582 (uint64_t)allocation.m_process_start,
583 (uint64_t)allocation.m_process_start +
584 (uint64_t)allocation.m_size);
588 void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
589 Scalar &scalar, size_t size,
593 if (size == UINT32_MAX)
594 size = scalar.GetByteSize();
598 const size_t mem_size =
599 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
601 return WriteMemory(process_address, buf, mem_size, error);
603 error.SetErrorToGenericError();
604 error.SetErrorString(
605 "Couldn't write scalar: failed to get scalar as memory data");
608 error.SetErrorToGenericError();
609 error.SetErrorString("Couldn't write scalar: its size was zero");
614 void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
615 lldb::addr_t address, Error &error) {
618 Scalar scalar(address);
620 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
623 void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
624 size_t size, Error &error) {
627 AllocationMap::iterator iter = FindAllocation(process_address, size);
629 if (iter == m_allocations.end()) {
630 lldb::ProcessSP process_sp = m_process_wp.lock();
633 process_sp->ReadMemory(process_address, bytes, size, error);
637 lldb::TargetSP target_sp = m_target_wp.lock();
640 Address absolute_address(process_address);
641 target_sp->ReadMemory(absolute_address, false, bytes, size, error);
645 error.SetErrorToGenericError();
646 error.SetErrorString("Couldn't read: no allocation contains the target "
647 "range, and neither the process nor the target exist");
651 Allocation &allocation = iter->second;
653 uint64_t offset = process_address - allocation.m_process_start;
655 if (offset > allocation.m_size) {
656 error.SetErrorToGenericError();
657 error.SetErrorString("Couldn't read: data is not in the allocation");
661 lldb::ProcessSP process_sp;
663 switch (allocation.m_policy) {
665 error.SetErrorToGenericError();
666 error.SetErrorString("Couldn't read: invalid allocation policy");
668 case eAllocationPolicyHostOnly:
669 if (!allocation.m_data.GetByteSize()) {
670 error.SetErrorToGenericError();
671 error.SetErrorString("Couldn't read: data buffer is empty");
674 if (allocation.m_data.GetByteSize() < offset + size) {
675 error.SetErrorToGenericError();
676 error.SetErrorString("Couldn't read: not enough underlying data");
680 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
682 case eAllocationPolicyMirror:
683 process_sp = m_process_wp.lock();
685 process_sp->ReadMemory(process_address, bytes, size, error);
686 if (!error.Success())
689 if (!allocation.m_data.GetByteSize()) {
690 error.SetErrorToGenericError();
691 error.SetErrorString("Couldn't read: data buffer is empty");
694 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
697 case eAllocationPolicyProcessOnly:
698 process_sp = m_process_wp.lock();
700 process_sp->ReadMemory(process_address, bytes, size, error);
701 if (!error.Success())
707 if (lldb_private::Log *log =
708 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
709 log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64
710 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
711 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
712 (uint64_t)allocation.m_process_start,
713 (uint64_t)allocation.m_process_start +
714 (uint64_t)allocation.m_size);
718 void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
719 lldb::addr_t process_address,
720 size_t size, Error &error) {
724 DataBufferHeap buf(size, 0);
725 ReadMemory(buf.GetBytes(), process_address, size, error);
727 if (!error.Success())
730 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
731 GetAddressByteSize());
733 lldb::offset_t offset = 0;
737 error.SetErrorToGenericError();
738 error.SetErrorStringWithFormat(
739 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
742 scalar = extractor.GetU8(&offset);
745 scalar = extractor.GetU16(&offset);
748 scalar = extractor.GetU32(&offset);
751 scalar = extractor.GetU64(&offset);
755 error.SetErrorToGenericError();
756 error.SetErrorString("Couldn't read scalar: its size was zero");
761 void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
762 lldb::addr_t process_address,
766 Scalar pointer_scalar;
767 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
770 if (!error.Success())
773 *address = pointer_scalar.ULongLong();
778 void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
779 lldb::addr_t process_address, size_t size,
784 AllocationMap::iterator iter = FindAllocation(process_address, size);
786 if (iter == m_allocations.end()) {
787 error.SetErrorToGenericError();
788 error.SetErrorStringWithFormat(
789 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
791 process_address, process_address + size);
795 Allocation &allocation = iter->second;
797 switch (allocation.m_policy) {
799 error.SetErrorToGenericError();
800 error.SetErrorString(
801 "Couldn't get memory data: invalid allocation policy");
803 case eAllocationPolicyProcessOnly:
804 error.SetErrorToGenericError();
805 error.SetErrorString(
806 "Couldn't get memory data: memory is only in the target");
808 case eAllocationPolicyMirror: {
809 lldb::ProcessSP process_sp = m_process_wp.lock();
811 if (!allocation.m_data.GetByteSize()) {
812 error.SetErrorToGenericError();
813 error.SetErrorString("Couldn't get memory data: data buffer is empty");
817 process_sp->ReadMemory(allocation.m_process_start,
818 allocation.m_data.GetBytes(),
819 allocation.m_data.GetByteSize(), error);
820 if (!error.Success())
822 uint64_t offset = process_address - allocation.m_process_start;
823 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
824 GetByteOrder(), GetAddressByteSize());
828 case eAllocationPolicyHostOnly:
829 if (!allocation.m_data.GetByteSize()) {
830 error.SetErrorToGenericError();
831 error.SetErrorString("Couldn't get memory data: data buffer is empty");
834 uint64_t offset = process_address - allocation.m_process_start;
835 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
836 GetByteOrder(), GetAddressByteSize());
840 error.SetErrorToGenericError();
841 error.SetErrorString("Couldn't get memory data: its size was zero");