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/Scalar.h"
12 #include "lldb/Target/MemoryRegionInfo.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Utility/DataBufferHeap.h"
16 #include "lldb/Utility/DataExtractor.h"
17 #include "lldb/Utility/LLDBAssert.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Status.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 Status 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, Status &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, Status &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, Status &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,
519 AllocationMap::iterator iter = FindAllocation(process_address, size);
521 if (iter == m_allocations.end()) {
522 lldb::ProcessSP process_sp = m_process_wp.lock();
525 process_sp->WriteMemory(process_address, bytes, size, error);
529 error.SetErrorToGenericError();
530 error.SetErrorString("Couldn't write: no allocation contains the target "
531 "range and the process doesn't exist");
535 Allocation &allocation = iter->second;
537 uint64_t offset = process_address - allocation.m_process_start;
539 lldb::ProcessSP process_sp;
541 switch (allocation.m_policy) {
543 error.SetErrorToGenericError();
544 error.SetErrorString("Couldn't write: invalid allocation policy");
546 case eAllocationPolicyHostOnly:
547 if (!allocation.m_data.GetByteSize()) {
548 error.SetErrorToGenericError();
549 error.SetErrorString("Couldn't write: data buffer is empty");
552 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
554 case eAllocationPolicyMirror:
555 if (!allocation.m_data.GetByteSize()) {
556 error.SetErrorToGenericError();
557 error.SetErrorString("Couldn't write: data buffer is empty");
560 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
561 process_sp = m_process_wp.lock();
563 process_sp->WriteMemory(process_address, bytes, size, error);
564 if (!error.Success())
568 case eAllocationPolicyProcessOnly:
569 process_sp = m_process_wp.lock();
571 process_sp->WriteMemory(process_address, bytes, size, error);
572 if (!error.Success())
578 if (lldb_private::Log *log =
579 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
580 log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64
581 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
582 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
583 (uint64_t)allocation.m_process_start,
584 (uint64_t)allocation.m_process_start +
585 (uint64_t)allocation.m_size);
589 void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
590 Scalar &scalar, size_t size,
594 if (size == UINT32_MAX)
595 size = scalar.GetByteSize();
599 const size_t mem_size =
600 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
602 return WriteMemory(process_address, buf, mem_size, error);
604 error.SetErrorToGenericError();
605 error.SetErrorString(
606 "Couldn't write scalar: failed to get scalar as memory data");
609 error.SetErrorToGenericError();
610 error.SetErrorString("Couldn't write scalar: its size was zero");
615 void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
616 lldb::addr_t address, Status &error) {
619 Scalar scalar(address);
621 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
624 void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
625 size_t size, Status &error) {
628 AllocationMap::iterator iter = FindAllocation(process_address, size);
630 if (iter == m_allocations.end()) {
631 lldb::ProcessSP process_sp = m_process_wp.lock();
634 process_sp->ReadMemory(process_address, bytes, size, error);
638 lldb::TargetSP target_sp = m_target_wp.lock();
641 Address absolute_address(process_address);
642 target_sp->ReadMemory(absolute_address, false, bytes, size, error);
646 error.SetErrorToGenericError();
647 error.SetErrorString("Couldn't read: no allocation contains the target "
648 "range, and neither the process nor the target exist");
652 Allocation &allocation = iter->second;
654 uint64_t offset = process_address - allocation.m_process_start;
656 if (offset > allocation.m_size) {
657 error.SetErrorToGenericError();
658 error.SetErrorString("Couldn't read: data is not in the allocation");
662 lldb::ProcessSP process_sp;
664 switch (allocation.m_policy) {
666 error.SetErrorToGenericError();
667 error.SetErrorString("Couldn't read: invalid allocation policy");
669 case eAllocationPolicyHostOnly:
670 if (!allocation.m_data.GetByteSize()) {
671 error.SetErrorToGenericError();
672 error.SetErrorString("Couldn't read: data buffer is empty");
675 if (allocation.m_data.GetByteSize() < offset + size) {
676 error.SetErrorToGenericError();
677 error.SetErrorString("Couldn't read: not enough underlying data");
681 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
683 case eAllocationPolicyMirror:
684 process_sp = m_process_wp.lock();
686 process_sp->ReadMemory(process_address, bytes, size, error);
687 if (!error.Success())
690 if (!allocation.m_data.GetByteSize()) {
691 error.SetErrorToGenericError();
692 error.SetErrorString("Couldn't read: data buffer is empty");
695 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
698 case eAllocationPolicyProcessOnly:
699 process_sp = m_process_wp.lock();
701 process_sp->ReadMemory(process_address, bytes, size, error);
702 if (!error.Success())
708 if (lldb_private::Log *log =
709 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
710 log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64
711 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
712 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
713 (uint64_t)allocation.m_process_start,
714 (uint64_t)allocation.m_process_start +
715 (uint64_t)allocation.m_size);
719 void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
720 lldb::addr_t process_address,
721 size_t size, Status &error) {
725 DataBufferHeap buf(size, 0);
726 ReadMemory(buf.GetBytes(), process_address, size, error);
728 if (!error.Success())
731 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
732 GetAddressByteSize());
734 lldb::offset_t offset = 0;
738 error.SetErrorToGenericError();
739 error.SetErrorStringWithFormat(
740 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
743 scalar = extractor.GetU8(&offset);
746 scalar = extractor.GetU16(&offset);
749 scalar = extractor.GetU32(&offset);
752 scalar = extractor.GetU64(&offset);
756 error.SetErrorToGenericError();
757 error.SetErrorString("Couldn't read scalar: its size was zero");
762 void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
763 lldb::addr_t process_address,
767 Scalar pointer_scalar;
768 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
771 if (!error.Success())
774 *address = pointer_scalar.ULongLong();
779 void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
780 lldb::addr_t process_address, size_t size,
785 AllocationMap::iterator iter = FindAllocation(process_address, size);
787 if (iter == m_allocations.end()) {
788 error.SetErrorToGenericError();
789 error.SetErrorStringWithFormat(
790 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
792 process_address, process_address + size);
796 Allocation &allocation = iter->second;
798 switch (allocation.m_policy) {
800 error.SetErrorToGenericError();
801 error.SetErrorString(
802 "Couldn't get memory data: invalid allocation policy");
804 case eAllocationPolicyProcessOnly:
805 error.SetErrorToGenericError();
806 error.SetErrorString(
807 "Couldn't get memory data: memory is only in the target");
809 case eAllocationPolicyMirror: {
810 lldb::ProcessSP process_sp = m_process_wp.lock();
812 if (!allocation.m_data.GetByteSize()) {
813 error.SetErrorToGenericError();
814 error.SetErrorString("Couldn't get memory data: data buffer is empty");
818 process_sp->ReadMemory(allocation.m_process_start,
819 allocation.m_data.GetBytes(),
820 allocation.m_data.GetByteSize(), error);
821 if (!error.Success())
823 uint64_t offset = process_address - allocation.m_process_start;
824 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
825 GetByteOrder(), GetAddressByteSize());
829 case eAllocationPolicyHostOnly:
830 if (!allocation.m_data.GetByteSize()) {
831 error.SetErrorToGenericError();
832 error.SetErrorString("Couldn't get memory data: data buffer is empty");
835 uint64_t offset = process_address - allocation.m_process_start;
836 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
837 GetByteOrder(), GetAddressByteSize());
841 error.SetErrorToGenericError();
842 error.SetErrorString("Couldn't get memory data: its size was zero");