32 AllocationMap::iterator iter;
38 if (iter->second.m_leak)
41 Free(iter->first, err);
62 const bool process_is_alive = process_sp && process_sp->IsAlive();
68 if (process_is_alive && process_sp->CanJIT()) {
71 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
72 lldb::ePermissionsWritable,
91 size_t alloc_size = back->second.m_size;
92 ret = llvm::alignTo(addr + alloc_size, 4096);
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);
107 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
110 if (region_info.
GetReadable() != MemoryRegionInfo::OptionalBool::eNo ||
111 region_info.
GetWritable() != MemoryRegionInfo::OptionalBool::eNo ||
113 MemoryRegionInfo::OptionalBool::eNo) {
126 err = process_sp->GetMemoryRegionInfo(
129 lldbassert(0 &&
"GetMemoryRegionInfo() succeeded, then failed");
144 switch (address_byte_size) {
146 ret = 0xffffffff00000000ull;
158 size_t alloc_size = back->second.m_size;
159 ret = llvm::alignTo(addr + alloc_size, 4096);
165 IRMemoryMap::AllocationMap::iterator
170 AllocationMap::iterator iter =
m_allocations.lower_bound(addr);
178 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
188 AllocationMap::const_iterator iter =
m_allocations.lower_bound(addr);
199 iter->second.m_size))
206 iter->second.m_size))
225 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
232 return process_sp->GetByteOrder();
237 return target_sp->GetArchitecture().GetByteOrder();
246 return process_sp->GetAddressByteSize();
251 return target_sp->GetArchitecture().GetAddressByteSize();
260 return process_sp.get();
265 return target_sp.get();
272 uint32_t permissions, uint8_t alignment,
274 : m_process_alloc(process_alloc), m_process_start(process_start),
275 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
276 m_alignment(alignment) {
279 llvm_unreachable(
"Invalid AllocationPolicy");
295 lldb::ProcessSP process_sp;
299 size_t allocation_size;
304 allocation_size = alignment;
307 allocation_size = llvm::alignTo(size, alignment);
312 allocation_size += alignment - 1;
317 error.SetErrorToGenericError();
318 error.SetErrorString(
"Couldn't malloc: invalid allocation policy");
321 allocation_address =
FindSpace(allocation_size);
323 error.SetErrorToGenericError();
324 error.SetErrorString(
"Couldn't malloc: address space is full");
331 "IRMemoryMap::%s process_sp=0x%" PRIxPTR
332 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
333 __FUNCTION__,
reinterpret_cast<uintptr_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())
348 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
349 "due to failed condition (see previous expr log message)",
352 allocation_address =
FindSpace(allocation_size);
354 error.SetErrorToGenericError();
355 error.SetErrorString(
"Couldn't malloc: address space is full");
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())
374 error.SetErrorToGenericError();
375 error.SetErrorString(
376 "Couldn't malloc: process doesn't support allocating memory");
380 error.SetErrorToGenericError();
381 error.SetErrorString(
"Couldn't malloc: process doesn't exist, and this "
382 "memory must be in the process");
389 aligned_address = (allocation_address + mask) & (~mask);
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>";
410 policy_string =
"eAllocationPolicyHostOnly";
413 policy_string =
"eAllocationPolicyProcessOnly";
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;
433 AllocationMap::iterator iter =
m_allocations.find(process_address);
436 error.SetErrorToGenericError();
437 error.SetErrorString(
"Couldn't leak: allocation doesn't exist");
449 AllocationMap::iterator iter =
m_allocations.find(process_address);
452 error.SetErrorToGenericError();
453 error.SetErrorString(
"Couldn't free: allocation doesn't exist");
464 if (process_sp->CanJIT() && process_sp->IsAlive())
465 process_sp->DeallocateMemory(
481 "IRMemoryMap::Free (0x%" PRIx64
") freed [0x%" PRIx64
483 (uint64_t)process_address, iter->second.m_process_start,
484 iter->second.m_process_start + iter->second.m_size);
513 const uint8_t *bytes,
size_t size,
517 AllocationMap::iterator iter =
FindAllocation(process_address, size);
523 process_sp->WriteMemory(process_address, bytes, size,
error);
527 error.SetErrorToGenericError();
528 error.SetErrorString(
"Couldn't write: no allocation contains the target "
529 "range and the process doesn't exist");
537 lldb::ProcessSP process_sp;
541 error.SetErrorToGenericError();
542 error.SetErrorString(
"Couldn't write: invalid allocation policy");
546 error.SetErrorToGenericError();
547 error.SetErrorString(
"Couldn't write: data buffer is empty");
550 ::memcpy(allocation.
m_data.GetBytes() + offset, bytes, size);
554 error.SetErrorToGenericError();
555 error.SetErrorString(
"Couldn't write: data buffer is empty");
558 ::memcpy(allocation.
m_data.GetBytes() + offset, bytes, size);
561 process_sp->WriteMemory(process_address, bytes, size,
error);
562 if (!
error.Success())
569 process_sp->WriteMemory(process_address, bytes, size,
error);
570 if (!
error.Success())
578 "IRMemoryMap::WriteMemory (0x%" PRIx64
", 0x%" PRIxPTR
579 ", 0x%" PRId64
") went to [0x%" PRIx64
"..0x%" PRIx64
")",
580 (uint64_t)process_address,
reinterpret_cast<uintptr_t
>(bytes), (uint64_t)size,
583 (uint64_t)allocation.
m_size);
588 Scalar &scalar,
size_t size,
597 const size_t mem_size =
602 error.SetErrorToGenericError();
603 error.SetErrorString(
604 "Couldn't write scalar: failed to get scalar as memory data");
607 error.SetErrorToGenericError();
608 error.SetErrorString(
"Couldn't write scalar: its size was zero");
625 AllocationMap::iterator iter =
FindAllocation(process_address, size);
631 process_sp->ReadMemory(process_address, bytes, size,
error);
638 Address absolute_address(process_address);
639 target_sp->ReadMemory(absolute_address, bytes, size,
error,
true);
643 error.SetErrorToGenericError();
644 error.SetErrorString(
"Couldn't read: no allocation contains the target "
645 "range, and neither the process nor the target exist");
653 if (offset > allocation.
m_size) {
654 error.SetErrorToGenericError();
655 error.SetErrorString(
"Couldn't read: data is not in the allocation");
659 lldb::ProcessSP process_sp;
663 error.SetErrorToGenericError();
664 error.SetErrorString(
"Couldn't read: invalid allocation policy");
668 error.SetErrorToGenericError();
669 error.SetErrorString(
"Couldn't read: data buffer is empty");
673 error.SetErrorToGenericError();
674 error.SetErrorString(
"Couldn't read: not enough underlying data");
678 ::memcpy(bytes, allocation.
m_data.GetBytes() + offset, size);
683 process_sp->ReadMemory(process_address, bytes, size,
error);
684 if (!
error.Success())
688 error.SetErrorToGenericError();
689 error.SetErrorString(
"Couldn't read: data buffer is empty");
692 ::memcpy(bytes, allocation.
m_data.GetBytes() + offset, size);
698 process_sp->ReadMemory(process_address, bytes, size,
error);
699 if (!
error.Success())
707 "IRMemoryMap::ReadMemory (0x%" PRIx64
", 0x%" PRIxPTR
708 ", 0x%" PRId64
") came from [0x%" PRIx64
"..0x%" PRIx64
")",
709 (uint64_t)process_address,
reinterpret_cast<uintptr_t
>(bytes), (uint64_t)size,
712 (uint64_t)allocation.
m_size);
725 if (!
error.Success())
735 error.SetErrorToGenericError();
736 error.SetErrorStringWithFormat(
737 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
740 scalar = extractor.
GetU8(&offset);
743 scalar = extractor.
GetU16(&offset);
746 scalar = extractor.
GetU32(&offset);
749 scalar = extractor.
GetU64(&offset);
753 error.SetErrorToGenericError();
754 error.SetErrorString(
"Couldn't read scalar: its size was zero");
767 if (!
error.Success())
779 AllocationMap::iterator iter =
FindAllocation(process_address, size);
782 error.SetErrorToGenericError();
783 error.SetErrorStringWithFormat(
784 "Couldn't find an allocation containing [0x%" PRIx64
"..0x%" PRIx64
786 process_address, process_address + size);
794 error.SetErrorToGenericError();
795 error.SetErrorString(
796 "Couldn't get memory data: invalid allocation policy");
799 error.SetErrorToGenericError();
800 error.SetErrorString(
801 "Couldn't get memory data: memory is only in the target");
807 error.SetErrorToGenericError();
808 error.SetErrorString(
"Couldn't get memory data: data buffer is empty");
813 allocation.
m_data.GetBytes(),
815 if (!
error.Success())
825 error.SetErrorToGenericError();
826 error.SetErrorString(
"Couldn't get memory data: data buffer is empty");
835 error.SetErrorToGenericError();
836 error.SetErrorString(
"Couldn't get memory data: its size was zero");