1 //===-- IRMemoryMap.cpp -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
20 
21 using namespace lldb_private;
22 
23 IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
24   if (target_sp)
25     m_process_wp = target_sp->GetProcessSP();
26 }
27 
28 IRMemoryMap::~IRMemoryMap() {
29   lldb::ProcessSP process_sp = m_process_wp.lock();
30 
31   if (process_sp) {
32     AllocationMap::iterator iter;
33 
34     Status err;
35 
36     while ((iter = m_allocations.begin()) != m_allocations.end()) {
37       err.Clear();
38       if (iter->second.m_leak)
39         m_allocations.erase(iter);
40       else
41         Free(iter->first, err);
42     }
43   }
44 }
45 
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.
49   //
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 wrong
53   // data.
54   //
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.
58 
59   lldb::TargetSP target_sp = m_target_wp.lock();
60   lldb::ProcessSP process_sp = m_process_wp.lock();
61 
62   const bool process_is_alive = process_sp && process_sp->IsAlive();
63 
64   lldb::addr_t ret = LLDB_INVALID_ADDRESS;
65   if (size == 0)
66     return ret;
67 
68   if (process_is_alive && process_sp->CanJIT()) {
69     Status alloc_error;
70 
71     ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
72                                                lldb::ePermissionsWritable,
73                                      alloc_error);
74 
75     if (!alloc_error.Success())
76       return LLDB_INVALID_ADDRESS;
77     else
78       return ret;
79   }
80 
81   // At this point we know that we need to hunt.
82   //
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.
85 
86   if (m_allocations.empty()) {
87     ret = 0x0;
88   } else {
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);
93   }
94 
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 has
97   // adequate space for our allocation.
98   if (process_is_alive) {
99     const uint64_t end_of_memory = process_sp->GetAddressByteSize() == 8
100                                        ? 0xffffffffffffffffull
101                                        : 0xffffffffull;
102 
103     lldbassert(process_sp->GetAddressByteSize() == 4 ||
104                end_of_memory != 0xffffffffull);
105 
106     MemoryRegionInfo region_info;
107     Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
108     if (err.Success()) {
109       while (true) {
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;
116             break;
117           } else {
118             ret = region_info.GetRange().GetRangeEnd();
119           }
120         } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
121           return ret;
122         } else {
123           // ret stays the same.  We just need to walk a bit further.
124         }
125 
126         err = process_sp->GetMemoryRegionInfo(
127             region_info.GetRange().GetRangeEnd(), region_info);
128         if (err.Fail()) {
129           lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
130           ret = LLDB_INVALID_ADDRESS;
131           break;
132         }
133       }
134     }
135   }
136 
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.
140 
141   if (m_allocations.empty()) {
142     uint32_t address_byte_size = GetAddressByteSize();
143     if (address_byte_size != UINT32_MAX) {
144       switch (address_byte_size) {
145       case 8:
146         ret = 0xffffffff00000000ull;
147         break;
148       case 4:
149         ret = 0xee000000ull;
150         break;
151       default:
152         break;
153       }
154     }
155   } else {
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);
160   }
161 
162   return ret;
163 }
164 
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();
169 
170   AllocationMap::iterator iter = m_allocations.lower_bound(addr);
171 
172   if (iter == m_allocations.end() || iter->first > addr) {
173     if (iter == m_allocations.begin())
174       return m_allocations.end();
175     iter--;
176   }
177 
178   if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
179     return iter;
180 
181   return m_allocations.end();
182 }
183 
184 bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
185   if (addr == LLDB_INVALID_ADDRESS)
186     return false;
187 
188   AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
189 
190   // Since we only know that the returned interval begins at a location greater
191   // than or equal to where the given interval begins, it's possible that the
192   // given interval intersects either the returned interval or the previous
193   // interval.  Thus, we need to check both. Note that we only need to check
194   // these two intervals.  Since all intervals are disjoint it is not possible
195   // that an adjacent interval does not intersect, but a non-adjacent interval
196   // does intersect.
197   if (iter != m_allocations.end()) {
198     if (AllocationsIntersect(addr, size, iter->second.m_process_start,
199                              iter->second.m_size))
200       return true;
201   }
202 
203   if (iter != m_allocations.begin()) {
204     --iter;
205     if (AllocationsIntersect(addr, size, iter->second.m_process_start,
206                              iter->second.m_size))
207       return true;
208   }
209 
210   return false;
211 }
212 
213 bool IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1,
214                                        lldb::addr_t addr2, size_t size2) {
215   // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
216   // that satisfy A<B and X<Y are the following:
217   // A B X Y
218   // A X B Y  (intersects)
219   // A X Y B  (intersects)
220   // X A B Y  (intersects)
221   // X A Y B  (intersects)
222   // X Y A B
223   // The first is B <= X, and the last is Y <= A. So the condition is !(B <= X
224   // || Y <= A)), or (X < B && A < Y)
225   return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
226 }
227 
228 lldb::ByteOrder IRMemoryMap::GetByteOrder() {
229   lldb::ProcessSP process_sp = m_process_wp.lock();
230 
231   if (process_sp)
232     return process_sp->GetByteOrder();
233 
234   lldb::TargetSP target_sp = m_target_wp.lock();
235 
236   if (target_sp)
237     return target_sp->GetArchitecture().GetByteOrder();
238 
239   return lldb::eByteOrderInvalid;
240 }
241 
242 uint32_t IRMemoryMap::GetAddressByteSize() {
243   lldb::ProcessSP process_sp = m_process_wp.lock();
244 
245   if (process_sp)
246     return process_sp->GetAddressByteSize();
247 
248   lldb::TargetSP target_sp = m_target_wp.lock();
249 
250   if (target_sp)
251     return target_sp->GetArchitecture().GetAddressByteSize();
252 
253   return UINT32_MAX;
254 }
255 
256 ExecutionContextScope *IRMemoryMap::GetBestExecutionContextScope() const {
257   lldb::ProcessSP process_sp = m_process_wp.lock();
258 
259   if (process_sp)
260     return process_sp.get();
261 
262   lldb::TargetSP target_sp = m_target_wp.lock();
263 
264   if (target_sp)
265     return target_sp.get();
266 
267   return NULL;
268 }
269 
270 IRMemoryMap::Allocation::Allocation(lldb::addr_t process_alloc,
271                                     lldb::addr_t process_start, size_t size,
272                                     uint32_t permissions, uint8_t alignment,
273                                     AllocationPolicy policy)
274     : m_process_alloc(process_alloc), m_process_start(process_start),
275       m_size(size), m_permissions(permissions), m_alignment(alignment),
276       m_policy(policy), m_leak(false) {
277   switch (policy) {
278   default:
279     assert(0 && "We cannot reach this!");
280   case eAllocationPolicyHostOnly:
281   case eAllocationPolicyMirror:
282     m_data.SetByteSize(size);
283     break;
284   case eAllocationPolicyProcessOnly:
285     break;
286   }
287 }
288 
289 lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
290                                  uint32_t permissions, AllocationPolicy policy,
291                                  bool zero_memory, Status &error) {
292   lldb_private::Log *log(
293       lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
294   error.Clear();
295 
296   lldb::ProcessSP process_sp;
297   lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
298   lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
299 
300   size_t allocation_size;
301 
302   if (size == 0) {
303     // FIXME: Malloc(0) should either return an invalid address or assert, in
304     // order to cut down on unnecessary allocations.
305     allocation_size = alignment;
306   } else {
307     // Round up the requested size to an aligned value.
308     allocation_size = llvm::alignTo(size, alignment);
309 
310     // The process page cache does not see the requested alignment. We can't
311     // assume its result will be any more than 1-byte aligned. To work around
312     // this, request `alignment - 1` additional bytes.
313     allocation_size += alignment - 1;
314   }
315 
316   switch (policy) {
317   default:
318     error.SetErrorToGenericError();
319     error.SetErrorString("Couldn't malloc: invalid allocation policy");
320     return LLDB_INVALID_ADDRESS;
321   case eAllocationPolicyHostOnly:
322     allocation_address = FindSpace(allocation_size);
323     if (allocation_address == LLDB_INVALID_ADDRESS) {
324       error.SetErrorToGenericError();
325       error.SetErrorString("Couldn't malloc: address space is full");
326       return LLDB_INVALID_ADDRESS;
327     }
328     break;
329   case eAllocationPolicyMirror:
330     process_sp = m_process_wp.lock();
331     if (log)
332       log->Printf("IRMemoryMap::%s process_sp=0x%" PRIx64
333                   ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
334                   __FUNCTION__, (lldb::addr_t)process_sp.get(),
335                   process_sp && process_sp->CanJIT() ? "true" : "false",
336                   process_sp && process_sp->IsAlive() ? "true" : "false");
337     if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
338       if (!zero_memory)
339         allocation_address =
340             process_sp->AllocateMemory(allocation_size, permissions, error);
341       else
342         allocation_address =
343             process_sp->CallocateMemory(allocation_size, permissions, error);
344 
345       if (!error.Success())
346         return LLDB_INVALID_ADDRESS;
347     } else {
348       if (log)
349         log->Printf("IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
350                     "due to failed condition (see previous expr log message)",
351                     __FUNCTION__);
352       policy = eAllocationPolicyHostOnly;
353       allocation_address = FindSpace(allocation_size);
354       if (allocation_address == LLDB_INVALID_ADDRESS) {
355         error.SetErrorToGenericError();
356         error.SetErrorString("Couldn't malloc: address space is full");
357         return LLDB_INVALID_ADDRESS;
358       }
359     }
360     break;
361   case eAllocationPolicyProcessOnly:
362     process_sp = m_process_wp.lock();
363     if (process_sp) {
364       if (process_sp->CanJIT() && process_sp->IsAlive()) {
365         if (!zero_memory)
366           allocation_address =
367               process_sp->AllocateMemory(allocation_size, permissions, error);
368         else
369           allocation_address =
370               process_sp->CallocateMemory(allocation_size, permissions, error);
371 
372         if (!error.Success())
373           return LLDB_INVALID_ADDRESS;
374       } else {
375         error.SetErrorToGenericError();
376         error.SetErrorString(
377             "Couldn't malloc: process doesn't support allocating memory");
378         return LLDB_INVALID_ADDRESS;
379       }
380     } else {
381       error.SetErrorToGenericError();
382       error.SetErrorString("Couldn't malloc: process doesn't exist, and this "
383                            "memory must be in the process");
384       return LLDB_INVALID_ADDRESS;
385     }
386     break;
387   }
388 
389   lldb::addr_t mask = alignment - 1;
390   aligned_address = (allocation_address + mask) & (~mask);
391 
392   m_allocations[aligned_address] =
393       Allocation(allocation_address, aligned_address, allocation_size,
394                  permissions, alignment, policy);
395 
396   if (zero_memory) {
397     Status write_error;
398     std::vector<uint8_t> zero_buf(size, 0);
399     WriteMemory(aligned_address, zero_buf.data(), size, write_error);
400   }
401 
402   if (log) {
403     const char *policy_string;
404 
405     switch (policy) {
406     default:
407       policy_string = "<invalid policy>";
408       break;
409     case eAllocationPolicyHostOnly:
410       policy_string = "eAllocationPolicyHostOnly";
411       break;
412     case eAllocationPolicyProcessOnly:
413       policy_string = "eAllocationPolicyProcessOnly";
414       break;
415     case eAllocationPolicyMirror:
416       policy_string = "eAllocationPolicyMirror";
417       break;
418     }
419 
420     log->Printf("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 =
479           lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
480     log->Printf("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 =
576           lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
577     log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64
578                 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
579                 (uint64_t)process_address, (uint64_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   return;
610 }
611 
612 void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
613                                        lldb::addr_t address, Status &error) {
614   error.Clear();
615 
616   Scalar scalar(address);
617 
618   WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
619 }
620 
621 void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
622                              size_t size, Status &error) {
623   error.Clear();
624 
625   AllocationMap::iterator iter = FindAllocation(process_address, size);
626 
627   if (iter == m_allocations.end()) {
628     lldb::ProcessSP process_sp = m_process_wp.lock();
629 
630     if (process_sp) {
631       process_sp->ReadMemory(process_address, bytes, size, error);
632       return;
633     }
634 
635     lldb::TargetSP target_sp = m_target_wp.lock();
636 
637     if (target_sp) {
638       Address absolute_address(process_address);
639       target_sp->ReadMemory(absolute_address, false, bytes, size, error);
640       return;
641     }
642 
643     error.SetErrorToGenericError();
644     error.SetErrorString("Couldn't read: no allocation contains the target "
645                          "range, and neither the process nor the target exist");
646     return;
647   }
648 
649   Allocation &allocation = iter->second;
650 
651   uint64_t offset = process_address - allocation.m_process_start;
652 
653   if (offset > allocation.m_size) {
654     error.SetErrorToGenericError();
655     error.SetErrorString("Couldn't read: data is not in the allocation");
656     return;
657   }
658 
659   lldb::ProcessSP process_sp;
660 
661   switch (allocation.m_policy) {
662   default:
663     error.SetErrorToGenericError();
664     error.SetErrorString("Couldn't read: invalid allocation policy");
665     return;
666   case eAllocationPolicyHostOnly:
667     if (!allocation.m_data.GetByteSize()) {
668       error.SetErrorToGenericError();
669       error.SetErrorString("Couldn't read: data buffer is empty");
670       return;
671     }
672     if (allocation.m_data.GetByteSize() < offset + size) {
673       error.SetErrorToGenericError();
674       error.SetErrorString("Couldn't read: not enough underlying data");
675       return;
676     }
677 
678     ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
679     break;
680   case eAllocationPolicyMirror:
681     process_sp = m_process_wp.lock();
682     if (process_sp) {
683       process_sp->ReadMemory(process_address, bytes, size, error);
684       if (!error.Success())
685         return;
686     } else {
687       if (!allocation.m_data.GetByteSize()) {
688         error.SetErrorToGenericError();
689         error.SetErrorString("Couldn't read: data buffer is empty");
690         return;
691       }
692       ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
693     }
694     break;
695   case eAllocationPolicyProcessOnly:
696     process_sp = m_process_wp.lock();
697     if (process_sp) {
698       process_sp->ReadMemory(process_address, bytes, size, error);
699       if (!error.Success())
700         return;
701     }
702     break;
703   }
704 
705   if (lldb_private::Log *log =
706           lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
707     log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64
708                 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
709                 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
710                 (uint64_t)allocation.m_process_start,
711                 (uint64_t)allocation.m_process_start +
712                     (uint64_t)allocation.m_size);
713   }
714 }
715 
716 void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
717                                        lldb::addr_t process_address,
718                                        size_t size, Status &error) {
719   error.Clear();
720 
721   if (size > 0) {
722     DataBufferHeap buf(size, 0);
723     ReadMemory(buf.GetBytes(), process_address, size, error);
724 
725     if (!error.Success())
726       return;
727 
728     DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
729                             GetAddressByteSize());
730 
731     lldb::offset_t offset = 0;
732 
733     switch (size) {
734     default:
735       error.SetErrorToGenericError();
736       error.SetErrorStringWithFormat(
737           "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
738       return;
739     case 1:
740       scalar = extractor.GetU8(&offset);
741       break;
742     case 2:
743       scalar = extractor.GetU16(&offset);
744       break;
745     case 4:
746       scalar = extractor.GetU32(&offset);
747       break;
748     case 8:
749       scalar = extractor.GetU64(&offset);
750       break;
751     }
752   } else {
753     error.SetErrorToGenericError();
754     error.SetErrorString("Couldn't read scalar: its size was zero");
755   }
756   return;
757 }
758 
759 void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
760                                         lldb::addr_t process_address,
761                                         Status &error) {
762   error.Clear();
763 
764   Scalar pointer_scalar;
765   ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
766                        error);
767 
768   if (!error.Success())
769     return;
770 
771   *address = pointer_scalar.ULongLong();
772 
773   return;
774 }
775 
776 void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
777                                 lldb::addr_t process_address, size_t size,
778                                 Status &error) {
779   error.Clear();
780 
781   if (size > 0) {
782     AllocationMap::iterator iter = FindAllocation(process_address, size);
783 
784     if (iter == m_allocations.end()) {
785       error.SetErrorToGenericError();
786       error.SetErrorStringWithFormat(
787           "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
788           ")",
789           process_address, process_address + size);
790       return;
791     }
792 
793     Allocation &allocation = iter->second;
794 
795     switch (allocation.m_policy) {
796     default:
797       error.SetErrorToGenericError();
798       error.SetErrorString(
799           "Couldn't get memory data: invalid allocation policy");
800       return;
801     case eAllocationPolicyProcessOnly:
802       error.SetErrorToGenericError();
803       error.SetErrorString(
804           "Couldn't get memory data: memory is only in the target");
805       return;
806     case eAllocationPolicyMirror: {
807       lldb::ProcessSP process_sp = m_process_wp.lock();
808 
809       if (!allocation.m_data.GetByteSize()) {
810         error.SetErrorToGenericError();
811         error.SetErrorString("Couldn't get memory data: data buffer is empty");
812         return;
813       }
814       if (process_sp) {
815         process_sp->ReadMemory(allocation.m_process_start,
816                                allocation.m_data.GetBytes(),
817                                allocation.m_data.GetByteSize(), error);
818         if (!error.Success())
819           return;
820         uint64_t offset = process_address - allocation.m_process_start;
821         extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
822                                   GetByteOrder(), GetAddressByteSize());
823         return;
824       }
825     } break;
826     case eAllocationPolicyHostOnly:
827       if (!allocation.m_data.GetByteSize()) {
828         error.SetErrorToGenericError();
829         error.SetErrorString("Couldn't get memory data: data buffer is empty");
830         return;
831       }
832       uint64_t offset = process_address - allocation.m_process_start;
833       extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
834                                 GetByteOrder(), GetAddressByteSize());
835       return;
836     }
837   } else {
838     error.SetErrorToGenericError();
839     error.SetErrorString("Couldn't get memory data: its size was zero");
840     return;
841   }
842 }
843