1 //===-- IRMemoryMap.cpp ---------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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"
19 
20 using namespace lldb_private;
21 
22 IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
23   if (target_sp)
24     m_process_wp = target_sp->GetProcessSP();
25 }
26 
27 IRMemoryMap::~IRMemoryMap() {
28   lldb::ProcessSP process_sp = m_process_wp.lock();
29 
30   if (process_sp) {
31     AllocationMap::iterator iter;
32 
33     Status err;
34 
35     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);
41     }
42   }
43 }
44 
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.
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
51   // expressions processing real values from the process do not use the wrong
52   // data.
53   //
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.
57 
58   lldb::TargetSP target_sp = m_target_wp.lock();
59   lldb::ProcessSP process_sp = m_process_wp.lock();
60 
61   const bool process_is_alive = process_sp && process_sp->IsAlive();
62 
63   lldb::addr_t ret = LLDB_INVALID_ADDRESS;
64   if (size == 0)
65     return ret;
66 
67   if (process_is_alive && process_sp->CanJIT()) {
68     Status alloc_error;
69 
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
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
100                                        : 0xffffffffull;
101 
102     lldbassert(process_sp->GetAddressByteSize() == 4 ||
103                end_of_memory != 0xffffffffull);
104 
105     MemoryRegionInfo region_info;
106     Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
107     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()) {
128           lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
129           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;
162 }
163 
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();
168 
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();
181 }
182 
183 bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
184   if (addr == LLDB_INVALID_ADDRESS)
185     return false;
186 
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
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
195   // does intersect.
196   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;
210 }
211 
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:
216   // 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
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));
225 }
226 
227 lldb::ByteOrder IRMemoryMap::GetByteOrder() {
228   lldb::ProcessSP process_sp = m_process_wp.lock();
229 
230   if (process_sp)
231     return process_sp->GetByteOrder();
232 
233   lldb::TargetSP target_sp = m_target_wp.lock();
234 
235   if (target_sp)
236     return target_sp->GetArchitecture().GetByteOrder();
237 
238   return lldb::eByteOrderInvalid;
239 }
240 
241 uint32_t IRMemoryMap::GetAddressByteSize() {
242   lldb::ProcessSP process_sp = m_process_wp.lock();
243 
244   if (process_sp)
245     return process_sp->GetAddressByteSize();
246 
247   lldb::TargetSP target_sp = m_target_wp.lock();
248 
249   if (target_sp)
250     return target_sp->GetArchitecture().GetAddressByteSize();
251 
252   return UINT32_MAX;
253 }
254 
255 ExecutionContextScope *IRMemoryMap::GetBestExecutionContextScope() const {
256   lldb::ProcessSP process_sp = m_process_wp.lock();
257 
258   if (process_sp)
259     return process_sp.get();
260 
261   lldb::TargetSP target_sp = m_target_wp.lock();
262 
263   if (target_sp)
264     return target_sp.get();
265 
266   return nullptr;
267 }
268 
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) {
276   switch (policy) {
277   default:
278     llvm_unreachable("Invalid AllocationPolicy");
279   case eAllocationPolicyHostOnly:
280   case eAllocationPolicyMirror:
281     m_data.SetByteSize(size);
282     break;
283   case eAllocationPolicyProcessOnly:
284     break;
285   }
286 }
287 
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(GetLog(LLDBLog::Expressions));
292   error.Clear();
293 
294   lldb::ProcessSP process_sp;
295   lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
296   lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
297 
298   size_t allocation_size;
299 
300   if (size == 0) {
301     // FIXME: Malloc(0) should either return an invalid address or assert, in
302     // order to cut down on unnecessary allocations.
303     allocation_size = alignment;
304   } else {
305     // Round up the requested size to an aligned value.
306     allocation_size = llvm::alignTo(size, alignment);
307 
308     // The process page cache does not see the requested alignment. We can't
309     // assume its result will be any more than 1-byte aligned. To work around
310     // this, request `alignment - 1` additional bytes.
311     allocation_size += alignment - 1;
312   }
313 
314   switch (policy) {
315   default:
316     error.SetErrorToGenericError();
317     error.SetErrorString("Couldn't malloc: invalid allocation policy");
318     return LLDB_INVALID_ADDRESS;
319   case eAllocationPolicyHostOnly:
320     allocation_address = FindSpace(allocation_size);
321     if (allocation_address == LLDB_INVALID_ADDRESS) {
322       error.SetErrorToGenericError();
323       error.SetErrorString("Couldn't malloc: address space is full");
324       return LLDB_INVALID_ADDRESS;
325     }
326     break;
327   case eAllocationPolicyMirror:
328     process_sp = m_process_wp.lock();
329     LLDB_LOGF(log,
330               "IRMemoryMap::%s process_sp=0x%" PRIxPTR
331               ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
332               __FUNCTION__, reinterpret_cast<uintptr_t>(process_sp.get()),
333               process_sp && process_sp->CanJIT() ? "true" : "false",
334               process_sp && process_sp->IsAlive() ? "true" : "false");
335     if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
336       if (!zero_memory)
337         allocation_address =
338             process_sp->AllocateMemory(allocation_size, permissions, error);
339       else
340         allocation_address =
341             process_sp->CallocateMemory(allocation_size, permissions, error);
342 
343       if (!error.Success())
344         return LLDB_INVALID_ADDRESS;
345     } else {
346       LLDB_LOGF(log,
347                 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
348                 "due to failed condition (see previous expr log message)",
349                 __FUNCTION__);
350       policy = eAllocationPolicyHostOnly;
351       allocation_address = FindSpace(allocation_size);
352       if (allocation_address == LLDB_INVALID_ADDRESS) {
353         error.SetErrorToGenericError();
354         error.SetErrorString("Couldn't malloc: address space is full");
355         return LLDB_INVALID_ADDRESS;
356       }
357     }
358     break;
359   case eAllocationPolicyProcessOnly:
360     process_sp = m_process_wp.lock();
361     if (process_sp) {
362       if (process_sp->CanJIT() && process_sp->IsAlive()) {
363         if (!zero_memory)
364           allocation_address =
365               process_sp->AllocateMemory(allocation_size, permissions, error);
366         else
367           allocation_address =
368               process_sp->CallocateMemory(allocation_size, permissions, error);
369 
370         if (!error.Success())
371           return LLDB_INVALID_ADDRESS;
372       } else {
373         error.SetErrorToGenericError();
374         error.SetErrorString(
375             "Couldn't malloc: process doesn't support allocating memory");
376         return LLDB_INVALID_ADDRESS;
377       }
378     } else {
379       error.SetErrorToGenericError();
380       error.SetErrorString("Couldn't malloc: process doesn't exist, and this "
381                            "memory must be in the process");
382       return LLDB_INVALID_ADDRESS;
383     }
384     break;
385   }
386 
387   lldb::addr_t mask = alignment - 1;
388   aligned_address = (allocation_address + mask) & (~mask);
389 
390   m_allocations.emplace(
391       std::piecewise_construct, std::forward_as_tuple(aligned_address),
392       std::forward_as_tuple(allocation_address, aligned_address,
393                             allocation_size, permissions, alignment, policy));
394 
395   if (zero_memory) {
396     Status write_error;
397     std::vector<uint8_t> zero_buf(size, 0);
398     WriteMemory(aligned_address, zero_buf.data(), size, write_error);
399   }
400 
401   if (log) {
402     const char *policy_string;
403 
404     switch (policy) {
405     default:
406       policy_string = "<invalid policy>";
407       break;
408     case eAllocationPolicyHostOnly:
409       policy_string = "eAllocationPolicyHostOnly";
410       break;
411     case eAllocationPolicyProcessOnly:
412       policy_string = "eAllocationPolicyProcessOnly";
413       break;
414     case eAllocationPolicyMirror:
415       policy_string = "eAllocationPolicyMirror";
416       break;
417     }
418 
419     LLDB_LOGF(log,
420               "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;
427 }
428 
429 void IRMemoryMap::Leak(lldb::addr_t process_address, Status &error) {
430   error.Clear();
431 
432   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 
445 void IRMemoryMap::Free(lldb::addr_t process_address, Status &error) {
446   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 = GetLog(LLDBLog::Expressions)) {
479     LLDB_LOGF(log,
480               "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 
489 bool 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 
511 void IRMemoryMap::WriteMemory(lldb::addr_t process_address,
512                               const uint8_t *bytes, size_t size,
513                               Status &error) {
514   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 = GetLog(LLDBLog::Expressions)) {
576     LLDB_LOGF(log,
577               "IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIxPTR
578               ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
579               (uint64_t)process_address, reinterpret_cast<uintptr_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 
586 void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
587                                       Scalar &scalar, size_t size,
588                                       Status &error) {
589   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 }
610 
611 void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
612                                        lldb::addr_t address, Status &error) {
613   error.Clear();
614 
615   Scalar scalar(address);
616 
617   WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
618 }
619 
620 void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
621                              size_t size, Status &error) {
622   error.Clear();
623 
624   AllocationMap::iterator iter = FindAllocation(process_address, size);
625 
626   if (iter == m_allocations.end()) {
627     lldb::ProcessSP process_sp = m_process_wp.lock();
628 
629     if (process_sp) {
630       process_sp->ReadMemory(process_address, bytes, size, error);
631       return;
632     }
633 
634     lldb::TargetSP target_sp = m_target_wp.lock();
635 
636     if (target_sp) {
637       Address absolute_address(process_address);
638       target_sp->ReadMemory(absolute_address, bytes, size, error, true);
639       return;
640     }
641 
642     error.SetErrorToGenericError();
643     error.SetErrorString("Couldn't read: no allocation contains the target "
644                          "range, and neither the process nor the target exist");
645     return;
646   }
647 
648   Allocation &allocation = iter->second;
649 
650   uint64_t offset = process_address - allocation.m_process_start;
651 
652   if (offset > allocation.m_size) {
653     error.SetErrorToGenericError();
654     error.SetErrorString("Couldn't read: data is not in the allocation");
655     return;
656   }
657 
658   lldb::ProcessSP process_sp;
659 
660   switch (allocation.m_policy) {
661   default:
662     error.SetErrorToGenericError();
663     error.SetErrorString("Couldn't read: invalid allocation policy");
664     return;
665   case eAllocationPolicyHostOnly:
666     if (!allocation.m_data.GetByteSize()) {
667       error.SetErrorToGenericError();
668       error.SetErrorString("Couldn't read: data buffer is empty");
669       return;
670     }
671     if (allocation.m_data.GetByteSize() < offset + size) {
672       error.SetErrorToGenericError();
673       error.SetErrorString("Couldn't read: not enough underlying data");
674       return;
675     }
676 
677     ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
678     break;
679   case eAllocationPolicyMirror:
680     process_sp = m_process_wp.lock();
681     if (process_sp) {
682       process_sp->ReadMemory(process_address, bytes, size, error);
683       if (!error.Success())
684         return;
685     } else {
686       if (!allocation.m_data.GetByteSize()) {
687         error.SetErrorToGenericError();
688         error.SetErrorString("Couldn't read: data buffer is empty");
689         return;
690       }
691       ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
692     }
693     break;
694   case eAllocationPolicyProcessOnly:
695     process_sp = m_process_wp.lock();
696     if (process_sp) {
697       process_sp->ReadMemory(process_address, bytes, size, error);
698       if (!error.Success())
699         return;
700     }
701     break;
702   }
703 
704   if (lldb_private::Log *log = GetLog(LLDBLog::Expressions)) {
705     LLDB_LOGF(log,
706               "IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIxPTR
707               ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
708               (uint64_t)process_address, reinterpret_cast<uintptr_t>(bytes), (uint64_t)size,
709               (uint64_t)allocation.m_process_start,
710               (uint64_t)allocation.m_process_start +
711                   (uint64_t)allocation.m_size);
712   }
713 }
714 
715 void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
716                                        lldb::addr_t process_address,
717                                        size_t size, Status &error) {
718   error.Clear();
719 
720   if (size > 0) {
721     DataBufferHeap buf(size, 0);
722     ReadMemory(buf.GetBytes(), process_address, size, error);
723 
724     if (!error.Success())
725       return;
726 
727     DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
728                             GetAddressByteSize());
729 
730     lldb::offset_t offset = 0;
731 
732     switch (size) {
733     default:
734       error.SetErrorToGenericError();
735       error.SetErrorStringWithFormat(
736           "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
737       return;
738     case 1:
739       scalar = extractor.GetU8(&offset);
740       break;
741     case 2:
742       scalar = extractor.GetU16(&offset);
743       break;
744     case 4:
745       scalar = extractor.GetU32(&offset);
746       break;
747     case 8:
748       scalar = extractor.GetU64(&offset);
749       break;
750     }
751   } else {
752     error.SetErrorToGenericError();
753     error.SetErrorString("Couldn't read scalar: its size was zero");
754   }
755 }
756 
757 void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
758                                         lldb::addr_t process_address,
759                                         Status &error) {
760   error.Clear();
761 
762   Scalar pointer_scalar;
763   ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
764                        error);
765 
766   if (!error.Success())
767     return;
768 
769   *address = pointer_scalar.ULongLong();
770 }
771 
772 void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
773                                 lldb::addr_t process_address, size_t size,
774                                 Status &error) {
775   error.Clear();
776 
777   if (size > 0) {
778     AllocationMap::iterator iter = FindAllocation(process_address, size);
779 
780     if (iter == m_allocations.end()) {
781       error.SetErrorToGenericError();
782       error.SetErrorStringWithFormat(
783           "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
784           ")",
785           process_address, process_address + size);
786       return;
787     }
788 
789     Allocation &allocation = iter->second;
790 
791     switch (allocation.m_policy) {
792     default:
793       error.SetErrorToGenericError();
794       error.SetErrorString(
795           "Couldn't get memory data: invalid allocation policy");
796       return;
797     case eAllocationPolicyProcessOnly:
798       error.SetErrorToGenericError();
799       error.SetErrorString(
800           "Couldn't get memory data: memory is only in the target");
801       return;
802     case eAllocationPolicyMirror: {
803       lldb::ProcessSP process_sp = m_process_wp.lock();
804 
805       if (!allocation.m_data.GetByteSize()) {
806         error.SetErrorToGenericError();
807         error.SetErrorString("Couldn't get memory data: data buffer is empty");
808         return;
809       }
810       if (process_sp) {
811         process_sp->ReadMemory(allocation.m_process_start,
812                                allocation.m_data.GetBytes(),
813                                allocation.m_data.GetByteSize(), error);
814         if (!error.Success())
815           return;
816         uint64_t offset = process_address - allocation.m_process_start;
817         extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
818                                   GetByteOrder(), GetAddressByteSize());
819         return;
820       }
821     } break;
822     case eAllocationPolicyHostOnly:
823       if (!allocation.m_data.GetByteSize()) {
824         error.SetErrorToGenericError();
825         error.SetErrorString("Couldn't get memory data: data buffer is empty");
826         return;
827       }
828       uint64_t offset = process_address - allocation.m_process_start;
829       extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
830                                 GetByteOrder(), GetAddressByteSize());
831       return;
832     }
833   } else {
834     error.SetErrorToGenericError();
835     error.SetErrorString("Couldn't get memory data: its size was zero");
836     return;
837   }
838 }
839