1 //===-- IRMemoryMap.cpp -----------------------------------------*- C++ -*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Expression/IRMemoryMap.h"
10 #include "lldb/Target/MemoryRegionInfo.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/Target.h"
13 #include "lldb/Utility/DataBufferHeap.h"
14 #include "lldb/Utility/DataExtractor.h"
15 #include "lldb/Utility/LLDBAssert.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/Scalar.h"
18 #include "lldb/Utility/Status.h"
20 using namespace lldb_private;
22 IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
24 m_process_wp = target_sp->GetProcessSP();
27 IRMemoryMap::~IRMemoryMap() {
28 lldb::ProcessSP process_sp = m_process_wp.lock();
31 AllocationMap::iterator iter;
35 while ((iter = m_allocations.begin()) != m_allocations.end()) {
37 if (iter->second.m_leak)
38 m_allocations.erase(iter);
40 Free(iter->first, err);
45 lldb::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.
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
51 // expressions processing real values from the process do not use the wrong
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.
58 lldb::TargetSP target_sp = m_target_wp.lock();
59 lldb::ProcessSP process_sp = m_process_wp.lock();
61 const bool process_is_alive = process_sp && process_sp->IsAlive();
63 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
67 if (process_is_alive && process_sp->CanJIT()) {
70 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
71 lldb::ePermissionsWritable,
74 if (!alloc_error.Success())
75 return LLDB_INVALID_ADDRESS;
80 // At this point we know that we need to hunt.
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.
85 if (m_allocations.empty()) {
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);
94 // Now, if it's possible to use the GetMemoryRegionInfo API to detect mapped
95 // regions, walk forward through memory until a region is found that has
96 // adequate space for our allocation.
97 if (process_is_alive) {
98 const uint64_t end_of_memory = process_sp->GetAddressByteSize() == 8
99 ? 0xffffffffffffffffull
102 lldbassert(process_sp->GetAddressByteSize() == 4 ||
103 end_of_memory != 0xffffffffull);
105 MemoryRegionInfo region_info;
106 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
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;
117 ret = region_info.GetRange().GetRangeEnd();
119 } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
122 // ret stays the same. We just need to walk a bit further.
125 err = process_sp->GetMemoryRegionInfo(
126 region_info.GetRange().GetRangeEnd(), region_info);
128 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
129 ret = LLDB_INVALID_ADDRESS;
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.
140 if (m_allocations.empty()) {
141 uint32_t address_byte_size = GetAddressByteSize();
142 if (address_byte_size != UINT32_MAX) {
143 switch (address_byte_size) {
145 ret = 0xffffffff00000000ull;
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);
164 IRMemoryMap::AllocationMap::iterator
165 IRMemoryMap::FindAllocation(lldb::addr_t addr, size_t size) {
166 if (addr == LLDB_INVALID_ADDRESS)
167 return m_allocations.end();
169 AllocationMap::iterator iter = m_allocations.lower_bound(addr);
171 if (iter == m_allocations.end() || iter->first > addr) {
172 if (iter == m_allocations.begin())
173 return m_allocations.end();
177 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
180 return m_allocations.end();
183 bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
184 if (addr == LLDB_INVALID_ADDRESS)
187 AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
189 // Since we only know that the returned interval begins at a location greater
190 // 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
196 if (iter != m_allocations.end()) {
197 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
198 iter->second.m_size))
202 if (iter != m_allocations.begin()) {
204 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
205 iter->second.m_size))
212 bool 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
215 // that satisfy A<B and X<Y are the following:
217 // A X B Y (intersects)
218 // A X Y B (intersects)
219 // X A B Y (intersects)
220 // X A Y B (intersects)
222 // 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)
224 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
227 lldb::ByteOrder IRMemoryMap::GetByteOrder() {
228 lldb::ProcessSP process_sp = m_process_wp.lock();
231 return process_sp->GetByteOrder();
233 lldb::TargetSP target_sp = m_target_wp.lock();
236 return target_sp->GetArchitecture().GetByteOrder();
238 return lldb::eByteOrderInvalid;
241 uint32_t IRMemoryMap::GetAddressByteSize() {
242 lldb::ProcessSP process_sp = m_process_wp.lock();
245 return process_sp->GetAddressByteSize();
247 lldb::TargetSP target_sp = m_target_wp.lock();
250 return target_sp->GetArchitecture().GetAddressByteSize();
255 ExecutionContextScope *IRMemoryMap::GetBestExecutionContextScope() const {
256 lldb::ProcessSP process_sp = m_process_wp.lock();
259 return process_sp.get();
261 lldb::TargetSP target_sp = m_target_wp.lock();
264 return target_sp.get();
269 IRMemoryMap::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),
274 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
275 m_alignment(alignment) {
278 llvm_unreachable("Invalid AllocationPolicy");
279 case eAllocationPolicyHostOnly:
280 case eAllocationPolicyMirror:
281 m_data.SetByteSize(size);
283 case eAllocationPolicyProcessOnly:
288 lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
289 uint32_t permissions, AllocationPolicy policy,
290 bool zero_memory, Status &error) {
291 lldb_private::Log *log(
292 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
295 lldb::ProcessSP process_sp;
296 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
297 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
299 size_t allocation_size;
302 // FIXME: Malloc(0) should either return an invalid address or assert, in
303 // order to cut down on unnecessary allocations.
304 allocation_size = alignment;
306 // Round up the requested size to an aligned value.
307 allocation_size = llvm::alignTo(size, alignment);
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;
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;
328 case eAllocationPolicyMirror:
329 process_sp = m_process_wp.lock();
331 "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()) {
339 process_sp->AllocateMemory(allocation_size, permissions, error);
342 process_sp->CallocateMemory(allocation_size, permissions, error);
344 if (!error.Success())
345 return LLDB_INVALID_ADDRESS;
348 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
349 "due to failed condition (see previous expr log message)",
351 policy = eAllocationPolicyHostOnly;
352 allocation_address = FindSpace(allocation_size);
353 if (allocation_address == LLDB_INVALID_ADDRESS) {
354 error.SetErrorToGenericError();
355 error.SetErrorString("Couldn't malloc: address space is full");
356 return LLDB_INVALID_ADDRESS;
360 case eAllocationPolicyProcessOnly:
361 process_sp = m_process_wp.lock();
363 if (process_sp->CanJIT() && process_sp->IsAlive()) {
366 process_sp->AllocateMemory(allocation_size, permissions, error);
369 process_sp->CallocateMemory(allocation_size, permissions, error);
371 if (!error.Success())
372 return LLDB_INVALID_ADDRESS;
374 error.SetErrorToGenericError();
375 error.SetErrorString(
376 "Couldn't malloc: process doesn't support allocating memory");
377 return LLDB_INVALID_ADDRESS;
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;
388 lldb::addr_t mask = alignment - 1;
389 aligned_address = (allocation_address + mask) & (~mask);
391 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));
398 std::vector<uint8_t> zero_buf(size, 0);
399 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
403 const char *policy_string;
407 policy_string = "<invalid policy>";
409 case eAllocationPolicyHostOnly:
410 policy_string = "eAllocationPolicyHostOnly";
412 case eAllocationPolicyProcessOnly:
413 policy_string = "eAllocationPolicyProcessOnly";
415 case eAllocationPolicyMirror:
416 policy_string = "eAllocationPolicyMirror";
421 "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);
427 return aligned_address;
430 void IRMemoryMap::Leak(lldb::addr_t process_address, Status &error) {
433 AllocationMap::iterator iter = m_allocations.find(process_address);
435 if (iter == m_allocations.end()) {
436 error.SetErrorToGenericError();
437 error.SetErrorString("Couldn't leak: allocation doesn't exist");
441 Allocation &allocation = iter->second;
443 allocation.m_leak = true;
446 void IRMemoryMap::Free(lldb::addr_t process_address, Status &error) {
449 AllocationMap::iterator iter = m_allocations.find(process_address);
451 if (iter == m_allocations.end()) {
452 error.SetErrorToGenericError();
453 error.SetErrorString("Couldn't free: allocation doesn't exist");
457 Allocation &allocation = iter->second;
459 switch (allocation.m_policy) {
461 case eAllocationPolicyHostOnly: {
462 lldb::ProcessSP process_sp = m_process_wp.lock();
464 if (process_sp->CanJIT() && process_sp->IsAlive())
465 process_sp->DeallocateMemory(
466 allocation.m_process_alloc); // FindSpace allocated this for real
471 case eAllocationPolicyMirror:
472 case eAllocationPolicyProcessOnly: {
473 lldb::ProcessSP process_sp = m_process_wp.lock();
475 process_sp->DeallocateMemory(allocation.m_process_alloc);
479 if (lldb_private::Log *log =
480 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
482 "IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
484 (uint64_t)process_address, iter->second.m_process_start,
485 iter->second.m_process_start + iter->second.m_size);
488 m_allocations.erase(iter);
491 bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
492 AllocationMap::iterator iter = FindAllocation(address, size);
493 if (iter == m_allocations.end())
496 Allocation &al = iter->second;
498 if (address > (al.m_process_start + al.m_size)) {
503 if (address > al.m_process_start) {
504 int dif = address - al.m_process_start;
505 size = al.m_size - dif;
513 void IRMemoryMap::WriteMemory(lldb::addr_t process_address,
514 const uint8_t *bytes, size_t size,
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)) {
580 "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)) {
711 "IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64
712 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
713 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
714 (uint64_t)allocation.m_process_start,
715 (uint64_t)allocation.m_process_start +
716 (uint64_t)allocation.m_size);
720 void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
721 lldb::addr_t process_address,
722 size_t size, Status &error) {
726 DataBufferHeap buf(size, 0);
727 ReadMemory(buf.GetBytes(), process_address, size, error);
729 if (!error.Success())
732 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
733 GetAddressByteSize());
735 lldb::offset_t offset = 0;
739 error.SetErrorToGenericError();
740 error.SetErrorStringWithFormat(
741 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
744 scalar = extractor.GetU8(&offset);
747 scalar = extractor.GetU16(&offset);
750 scalar = extractor.GetU32(&offset);
753 scalar = extractor.GetU64(&offset);
757 error.SetErrorToGenericError();
758 error.SetErrorString("Couldn't read scalar: its size was zero");
763 void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
764 lldb::addr_t process_address,
768 Scalar pointer_scalar;
769 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
772 if (!error.Success())
775 *address = pointer_scalar.ULongLong();
780 void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
781 lldb::addr_t process_address, size_t size,
786 AllocationMap::iterator iter = FindAllocation(process_address, size);
788 if (iter == m_allocations.end()) {
789 error.SetErrorToGenericError();
790 error.SetErrorStringWithFormat(
791 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
793 process_address, process_address + size);
797 Allocation &allocation = iter->second;
799 switch (allocation.m_policy) {
801 error.SetErrorToGenericError();
802 error.SetErrorString(
803 "Couldn't get memory data: invalid allocation policy");
805 case eAllocationPolicyProcessOnly:
806 error.SetErrorToGenericError();
807 error.SetErrorString(
808 "Couldn't get memory data: memory is only in the target");
810 case eAllocationPolicyMirror: {
811 lldb::ProcessSP process_sp = m_process_wp.lock();
813 if (!allocation.m_data.GetByteSize()) {
814 error.SetErrorToGenericError();
815 error.SetErrorString("Couldn't get memory data: data buffer is empty");
819 process_sp->ReadMemory(allocation.m_process_start,
820 allocation.m_data.GetBytes(),
821 allocation.m_data.GetByteSize(), error);
822 if (!error.Success())
824 uint64_t offset = process_address - allocation.m_process_start;
825 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
826 GetByteOrder(), GetAddressByteSize());
830 case eAllocationPolicyHostOnly:
831 if (!allocation.m_data.GetByteSize()) {
832 error.SetErrorToGenericError();
833 error.SetErrorString("Couldn't get memory data: data buffer is empty");
836 uint64_t offset = process_address - allocation.m_process_start;
837 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
838 GetByteOrder(), GetAddressByteSize());
842 error.SetErrorToGenericError();
843 error.SetErrorString("Couldn't get memory data: its size was zero");