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/Core/DataBufferHeap.h"
11 #include "lldb/Core/DataExtractor.h"
12 #include "lldb/Core/Error.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Core/Scalar.h"
15 #include "lldb/Expression/IRMemoryMap.h"
16 #include "lldb/Target/MemoryRegionInfo.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Utility/LLDBAssert.h"
20 
21 using namespace lldb_private;
22 
23 IRMemoryMap::IRMemoryMap (lldb::TargetSP target_sp) :
24     m_target_wp(target_sp)
25 {
26     if (target_sp)
27         m_process_wp = target_sp->GetProcessSP();
28 }
29 
30 IRMemoryMap::~IRMemoryMap ()
31 {
32     lldb::ProcessSP process_sp = m_process_wp.lock();
33 
34     if (process_sp)
35     {
36         AllocationMap::iterator iter;
37 
38         Error err;
39 
40         while ((iter = m_allocations.begin()) != m_allocations.end())
41         {
42             err.Clear();
43             if (iter->second.m_leak)
44                 m_allocations.erase(iter);
45             else
46                 Free(iter->first, err);
47         }
48     }
49 }
50 
51 lldb::addr_t
52 IRMemoryMap::FindSpace (size_t size)
53 {
54     // The FindSpace algorithm's job is to find a region of memory that the
55     // underlying process is unlikely to be using.
56     //
57     // The memory returned by this function will never be written to.  The only
58     // point is that it should not shadow process memory if possible, so that
59     // expressions processing real values from the process do not use the
60     // wrong data.
61     //
62     // If the process can in fact allocate memory (CanJIT() lets us know this)
63     // then this can be accomplished just be allocating memory in the inferior.
64     // Then no guessing is required.
65 
66     lldb::TargetSP target_sp = m_target_wp.lock();
67     lldb::ProcessSP process_sp = m_process_wp.lock();
68 
69     const bool process_is_alive = process_sp && process_sp->IsAlive();
70 
71     lldb::addr_t ret = LLDB_INVALID_ADDRESS;
72     if (size == 0)
73         return ret;
74 
75     if (process_is_alive && process_sp->CanJIT())
76     {
77         Error alloc_error;
78 
79         ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable | lldb::ePermissionsWritable, alloc_error);
80 
81         if (!alloc_error.Success())
82             return LLDB_INVALID_ADDRESS;
83         else
84             return ret;
85     }
86 
87     // At this point we know that we need to hunt.
88     //
89     // First, go to the end of the existing allocations we've made if there are
90     // any allocations.  Otherwise start at the beginning of memory.
91 
92     if (m_allocations.empty())
93     {
94         ret = 0x0;
95     }
96     else
97     {
98         auto back = m_allocations.rbegin();
99         lldb::addr_t addr = back->first;
100         size_t alloc_size = back->second.m_size;
101         ret = llvm::alignTo(addr+alloc_size, 4096);
102     }
103 
104     // Now, if it's possible to use the GetMemoryRegionInfo API to detect mapped
105     // regions, walk forward through memory until a region is found that
106     // has adequate space for our allocation.
107     if (process_is_alive)
108     {
109         const uint64_t end_of_memory = process_sp->GetAddressByteSize() == 8 ?
110             0xffffffffffffffffull : 0xffffffffull;
111 
112         lldbassert(process_sp->GetAddressByteSize() == 4 || end_of_memory != 0xffffffffull);
113 
114         MemoryRegionInfo region_info;
115         Error err = process_sp->GetMemoryRegionInfo(ret, region_info);
116         if (err.Success())
117         {
118             while (true)
119             {
120                 if (region_info.GetReadable() != MemoryRegionInfo::OptionalBool::eNo ||
121                     region_info.GetWritable() != MemoryRegionInfo::OptionalBool::eNo ||
122                     region_info.GetExecutable() != MemoryRegionInfo::OptionalBool::eNo)
123                 {
124                     if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory)
125                     {
126                         ret = LLDB_INVALID_ADDRESS;
127                         break;
128                     }
129                     else
130                     {
131                         ret = region_info.GetRange().GetRangeEnd();
132                     }
133                 }
134                 else if (ret + size < region_info.GetRange().GetRangeEnd())
135                 {
136                     return ret;
137                 }
138                 else
139                 {
140                     // ret stays the same.  We just need to walk a bit further.
141                 }
142 
143                 err = process_sp->GetMemoryRegionInfo(region_info.GetRange().GetRangeEnd(), region_info);
144                 if (err.Fail())
145                 {
146                     lldbassert(!"GetMemoryRegionInfo() succeeded, then failed");
147                     ret = LLDB_INVALID_ADDRESS;
148                     break;
149                 }
150             }
151         }
152     }
153 
154     // We've tried our algorithm, and it didn't work.  Now we have to reset back
155     // to the end of the allocations we've already reported, or use a 'sensible'
156     // default if this is our first allocation.
157 
158     if (m_allocations.empty())
159     {
160         uint32_t address_byte_size = GetAddressByteSize();
161         if (address_byte_size != UINT32_MAX)
162         {
163             switch (address_byte_size)
164             {
165                 case 8:
166                     ret = 0xffffffff00000000ull;
167                     break;
168                 case 4:
169                     ret = 0xee000000ull;
170                     break;
171                 default:
172                     break;
173             }
174         }
175     }
176     else
177     {
178         auto back = m_allocations.rbegin();
179         lldb::addr_t addr = back->first;
180         size_t alloc_size = back->second.m_size;
181         ret = llvm::alignTo(addr+alloc_size, 4096);
182     }
183 
184     return ret;
185 }
186 
187 IRMemoryMap::AllocationMap::iterator
188 IRMemoryMap::FindAllocation (lldb::addr_t addr, size_t size)
189 {
190     if (addr == LLDB_INVALID_ADDRESS)
191         return m_allocations.end();
192 
193     AllocationMap::iterator iter = m_allocations.lower_bound (addr);
194 
195     if (iter == m_allocations.end() ||
196         iter->first > addr)
197     {
198         if (iter == m_allocations.begin())
199             return m_allocations.end();
200         iter--;
201     }
202 
203     if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
204         return iter;
205 
206     return m_allocations.end();
207 }
208 
209 bool
210 IRMemoryMap::IntersectsAllocation (lldb::addr_t addr, size_t size) const
211 {
212     if (addr == LLDB_INVALID_ADDRESS)
213         return false;
214 
215     AllocationMap::const_iterator iter = m_allocations.lower_bound (addr);
216 
217     // Since we only know that the returned interval begins at a location greater than or
218     // equal to where the given interval begins, it's possible that the given interval
219     // intersects either the returned interval or the previous interval.  Thus, we need to
220     // check both. Note that we only need to check these two intervals.  Since all intervals
221     // are disjoint it is not possible that an adjacent interval does not intersect, but a
222     // non-adjacent interval does intersect.
223     if (iter != m_allocations.end()) {
224         if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size))
225             return true;
226     }
227 
228     if (iter != m_allocations.begin()) {
229         --iter;
230         if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size))
231             return true;
232     }
233 
234     return false;
235 }
236 
237 bool
238 IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1, lldb::addr_t addr2, size_t size2) {
239   // Given two half open intervals [A, B) and [X, Y), the only 6 permutations that satisfy
240   // A<B and X<Y are the following:
241   // A B X Y
242   // A X B Y  (intersects)
243   // A X Y B  (intersects)
244   // X A B Y  (intersects)
245   // X A Y B  (intersects)
246   // X Y A B
247   // The first is B <= X, and the last is Y <= A.
248   // So the condition is !(B <= X || Y <= A)), or (X < B && A < Y)
249   return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
250 }
251 
252 lldb::ByteOrder
253 IRMemoryMap::GetByteOrder()
254 {
255     lldb::ProcessSP process_sp = m_process_wp.lock();
256 
257     if (process_sp)
258         return process_sp->GetByteOrder();
259 
260     lldb::TargetSP target_sp = m_target_wp.lock();
261 
262     if (target_sp)
263         return target_sp->GetArchitecture().GetByteOrder();
264 
265     return lldb::eByteOrderInvalid;
266 }
267 
268 uint32_t
269 IRMemoryMap::GetAddressByteSize()
270 {
271     lldb::ProcessSP process_sp = m_process_wp.lock();
272 
273     if (process_sp)
274         return process_sp->GetAddressByteSize();
275 
276     lldb::TargetSP target_sp = m_target_wp.lock();
277 
278     if (target_sp)
279         return target_sp->GetArchitecture().GetAddressByteSize();
280 
281     return UINT32_MAX;
282 }
283 
284 ExecutionContextScope *
285 IRMemoryMap::GetBestExecutionContextScope() const
286 {
287     lldb::ProcessSP process_sp = m_process_wp.lock();
288 
289     if (process_sp)
290         return process_sp.get();
291 
292     lldb::TargetSP target_sp = m_target_wp.lock();
293 
294     if (target_sp)
295         return target_sp.get();
296 
297     return NULL;
298 }
299 
300 IRMemoryMap::Allocation::Allocation (lldb::addr_t process_alloc,
301                                      lldb::addr_t process_start,
302                                      size_t size,
303                                      uint32_t permissions,
304                                      uint8_t alignment,
305                                      AllocationPolicy policy) :
306     m_process_alloc (process_alloc),
307     m_process_start (process_start),
308     m_size (size),
309     m_permissions (permissions),
310     m_alignment (alignment),
311     m_policy (policy),
312     m_leak (false)
313 {
314     switch (policy)
315     {
316         default:
317             assert (0 && "We cannot reach this!");
318         case eAllocationPolicyHostOnly:
319             m_data.SetByteSize(size);
320             memset(m_data.GetBytes(), 0, size);
321             break;
322         case eAllocationPolicyProcessOnly:
323             break;
324         case eAllocationPolicyMirror:
325             m_data.SetByteSize(size);
326             memset(m_data.GetBytes(), 0, size);
327             break;
328     }
329 }
330 
331 lldb::addr_t
332 IRMemoryMap::Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, Error &error)
333 {
334     lldb_private::Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
335     error.Clear();
336 
337     lldb::ProcessSP process_sp;
338     lldb::addr_t    allocation_address  = LLDB_INVALID_ADDRESS;
339     lldb::addr_t    aligned_address     = LLDB_INVALID_ADDRESS;
340 
341     size_t          alignment_mask = alignment - 1;
342     size_t          allocation_size;
343 
344     if (size == 0)
345         allocation_size = alignment;
346     else
347         allocation_size = (size & alignment_mask) ? ((size + alignment) & (~alignment_mask)) : size;
348 
349     switch (policy)
350     {
351     default:
352         error.SetErrorToGenericError();
353         error.SetErrorString("Couldn't malloc: invalid allocation policy");
354         return LLDB_INVALID_ADDRESS;
355     case eAllocationPolicyHostOnly:
356         allocation_address = FindSpace(allocation_size);
357         if (allocation_address == LLDB_INVALID_ADDRESS)
358         {
359             error.SetErrorToGenericError();
360             error.SetErrorString("Couldn't malloc: address space is full");
361             return LLDB_INVALID_ADDRESS;
362         }
363         break;
364     case eAllocationPolicyMirror:
365         process_sp = m_process_wp.lock();
366         if (log)
367             log->Printf ("IRMemoryMap::%s process_sp=0x%" PRIx64 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s", __FUNCTION__, (lldb::addr_t) process_sp.get (), process_sp && process_sp->CanJIT () ? "true" : "false", process_sp && process_sp->IsAlive () ? "true" : "false");
368         if (process_sp && process_sp->CanJIT() && process_sp->IsAlive())
369         {
370             if (!zero_memory)
371               allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error);
372             else
373               allocation_address = process_sp->CallocateMemory(allocation_size, permissions, error);
374 
375             if (!error.Success())
376                 return LLDB_INVALID_ADDRESS;
377         }
378         else
379         {
380             if (log)
381                 log->Printf ("IRMemoryMap::%s switching to eAllocationPolicyHostOnly due to failed condition (see previous expr log message)", __FUNCTION__);
382             policy = eAllocationPolicyHostOnly;
383             allocation_address = FindSpace(allocation_size);
384             if (allocation_address == LLDB_INVALID_ADDRESS)
385             {
386                 error.SetErrorToGenericError();
387                 error.SetErrorString("Couldn't malloc: address space is full");
388                 return LLDB_INVALID_ADDRESS;
389             }
390         }
391         break;
392     case eAllocationPolicyProcessOnly:
393         process_sp = m_process_wp.lock();
394         if (process_sp)
395         {
396             if (process_sp->CanJIT() && process_sp->IsAlive())
397             {
398                 if (!zero_memory)
399                   allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error);
400                 else
401                   allocation_address = process_sp->CallocateMemory(allocation_size, permissions, error);
402 
403                 if (!error.Success())
404                     return LLDB_INVALID_ADDRESS;
405             }
406             else
407             {
408                 error.SetErrorToGenericError();
409                 error.SetErrorString("Couldn't malloc: process doesn't support allocating memory");
410                 return LLDB_INVALID_ADDRESS;
411             }
412         }
413         else
414         {
415             error.SetErrorToGenericError();
416             error.SetErrorString("Couldn't malloc: process doesn't exist, and this memory must be in the process");
417             return LLDB_INVALID_ADDRESS;
418         }
419         break;
420     }
421 
422 
423     lldb::addr_t mask = alignment - 1;
424     aligned_address = (allocation_address + mask) & (~mask);
425 
426     m_allocations[aligned_address] = Allocation(allocation_address,
427                                                 aligned_address,
428                                                 allocation_size,
429                                                 permissions,
430                                                 alignment,
431                                                 policy);
432 
433     if (zero_memory)
434     {
435         Error write_error;
436         std::vector<uint8_t> zero_buf(size, 0);
437         WriteMemory(aligned_address, zero_buf.data(), size, write_error);
438     }
439 
440     if (log)
441     {
442         const char * policy_string;
443 
444         switch (policy)
445         {
446         default:
447             policy_string = "<invalid policy>";
448             break;
449         case eAllocationPolicyHostOnly:
450             policy_string = "eAllocationPolicyHostOnly";
451             break;
452         case eAllocationPolicyProcessOnly:
453             policy_string = "eAllocationPolicyProcessOnly";
454             break;
455         case eAllocationPolicyMirror:
456             policy_string = "eAllocationPolicyMirror";
457             break;
458         }
459 
460         log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", %s) -> 0x%" PRIx64,
461                     (uint64_t)allocation_size,
462                     (uint64_t)alignment,
463                     (uint64_t)permissions,
464                     policy_string,
465                     aligned_address);
466     }
467 
468     return aligned_address;
469 }
470 
471 void
472 IRMemoryMap::Leak (lldb::addr_t process_address, Error &error)
473 {
474     error.Clear();
475 
476     AllocationMap::iterator iter = m_allocations.find(process_address);
477 
478     if (iter == m_allocations.end())
479     {
480         error.SetErrorToGenericError();
481         error.SetErrorString("Couldn't leak: allocation doesn't exist");
482         return;
483     }
484 
485     Allocation &allocation = iter->second;
486 
487     allocation.m_leak = true;
488 }
489 
490 void
491 IRMemoryMap::Free (lldb::addr_t process_address, Error &error)
492 {
493     error.Clear();
494 
495     AllocationMap::iterator iter = m_allocations.find(process_address);
496 
497     if (iter == m_allocations.end())
498     {
499         error.SetErrorToGenericError();
500         error.SetErrorString("Couldn't free: allocation doesn't exist");
501         return;
502     }
503 
504     Allocation &allocation = iter->second;
505 
506     switch (allocation.m_policy)
507     {
508     default:
509     case eAllocationPolicyHostOnly:
510         {
511             lldb::ProcessSP process_sp = m_process_wp.lock();
512             if (process_sp)
513             {
514                 if (process_sp->CanJIT() && process_sp->IsAlive())
515                     process_sp->DeallocateMemory(allocation.m_process_alloc); // FindSpace allocated this for real
516             }
517 
518             break;
519         }
520     case eAllocationPolicyMirror:
521     case eAllocationPolicyProcessOnly:
522         {
523             lldb::ProcessSP process_sp = m_process_wp.lock();
524             if (process_sp)
525                 process_sp->DeallocateMemory(allocation.m_process_alloc);
526         }
527     }
528 
529     if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
530     {
531         log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64 "..0x%" PRIx64 ")",
532                     (uint64_t)process_address,
533                     iter->second.m_process_start,
534                     iter->second.m_process_start + iter->second.m_size);
535     }
536 
537     m_allocations.erase(iter);
538 }
539 
540 bool
541 IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size)
542 {
543     AllocationMap::iterator iter = FindAllocation(address, size);
544     if (iter == m_allocations.end())
545         return false;
546 
547     Allocation &al = iter->second;
548 
549     if (address > (al.m_process_start + al.m_size))
550     {
551         size = 0;
552         return false;
553     }
554 
555     if (address > al.m_process_start)
556     {
557         int dif = address - al.m_process_start;
558         size = al.m_size - dif;
559         return true;
560     }
561 
562     size = al.m_size;
563     return true;
564 }
565 
566 void
567 IRMemoryMap::WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error)
568 {
569     error.Clear();
570 
571     AllocationMap::iterator iter = FindAllocation(process_address, size);
572 
573     if (iter == m_allocations.end())
574     {
575         lldb::ProcessSP process_sp = m_process_wp.lock();
576 
577         if (process_sp)
578         {
579             process_sp->WriteMemory(process_address, bytes, size, error);
580             return;
581         }
582 
583         error.SetErrorToGenericError();
584         error.SetErrorString("Couldn't write: no allocation contains the target range and the process doesn't exist");
585         return;
586     }
587 
588     Allocation &allocation = iter->second;
589 
590     uint64_t offset = process_address - allocation.m_process_start;
591 
592     lldb::ProcessSP process_sp;
593 
594     switch (allocation.m_policy)
595     {
596     default:
597         error.SetErrorToGenericError();
598         error.SetErrorString("Couldn't write: invalid allocation policy");
599         return;
600     case eAllocationPolicyHostOnly:
601         if (!allocation.m_data.GetByteSize())
602         {
603             error.SetErrorToGenericError();
604             error.SetErrorString("Couldn't write: data buffer is empty");
605             return;
606         }
607         ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size);
608         break;
609     case eAllocationPolicyMirror:
610         if (!allocation.m_data.GetByteSize())
611         {
612             error.SetErrorToGenericError();
613             error.SetErrorString("Couldn't write: data buffer is empty");
614             return;
615         }
616         ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size);
617         process_sp = m_process_wp.lock();
618         if (process_sp)
619         {
620             process_sp->WriteMemory(process_address, bytes, size, error);
621             if (!error.Success())
622                 return;
623         }
624         break;
625     case eAllocationPolicyProcessOnly:
626         process_sp = m_process_wp.lock();
627         if (process_sp)
628         {
629             process_sp->WriteMemory(process_address, bytes, size, error);
630             if (!error.Success())
631                 return;
632         }
633         break;
634     }
635 
636     if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
637     {
638         log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
639                     (uint64_t)process_address,
640                     (uint64_t)bytes,
641                     (uint64_t)size,
642                     (uint64_t)allocation.m_process_start,
643                     (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size);
644     }
645 }
646 
647 void
648 IRMemoryMap::WriteScalarToMemory (lldb::addr_t process_address, Scalar &scalar, size_t size, Error &error)
649 {
650     error.Clear();
651 
652     if (size == UINT32_MAX)
653         size = scalar.GetByteSize();
654 
655     if (size > 0)
656     {
657         uint8_t buf[32];
658         const size_t mem_size = scalar.GetAsMemoryData (buf, size, GetByteOrder(), error);
659         if (mem_size > 0)
660         {
661             return WriteMemory(process_address, buf, mem_size, error);
662         }
663         else
664         {
665             error.SetErrorToGenericError();
666             error.SetErrorString ("Couldn't write scalar: failed to get scalar as memory data");
667         }
668     }
669     else
670     {
671         error.SetErrorToGenericError();
672         error.SetErrorString ("Couldn't write scalar: its size was zero");
673     }
674     return;
675 }
676 
677 void
678 IRMemoryMap::WritePointerToMemory (lldb::addr_t process_address, lldb::addr_t address, Error &error)
679 {
680     error.Clear();
681 
682     Scalar scalar(address);
683 
684     WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
685 }
686 
687 void
688 IRMemoryMap::ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error)
689 {
690     error.Clear();
691 
692     AllocationMap::iterator iter = FindAllocation(process_address, size);
693 
694     if (iter == m_allocations.end())
695     {
696         lldb::ProcessSP process_sp = m_process_wp.lock();
697 
698         if (process_sp)
699         {
700             process_sp->ReadMemory(process_address, bytes, size, error);
701             return;
702         }
703 
704         lldb::TargetSP target_sp = m_target_wp.lock();
705 
706         if (target_sp)
707         {
708             Address absolute_address(process_address);
709             target_sp->ReadMemory(absolute_address, false, bytes, size, error);
710             return;
711         }
712 
713         error.SetErrorToGenericError();
714         error.SetErrorString("Couldn't read: no allocation contains the target range, and neither the process nor the target exist");
715         return;
716     }
717 
718     Allocation &allocation = iter->second;
719 
720     uint64_t offset = process_address - allocation.m_process_start;
721 
722     if (offset > allocation.m_size)
723     {
724         error.SetErrorToGenericError();
725         error.SetErrorString("Couldn't read: data is not in the allocation");
726         return;
727     }
728 
729     lldb::ProcessSP process_sp;
730 
731     switch (allocation.m_policy)
732     {
733     default:
734         error.SetErrorToGenericError();
735         error.SetErrorString("Couldn't read: invalid allocation policy");
736         return;
737     case eAllocationPolicyHostOnly:
738         if (!allocation.m_data.GetByteSize())
739         {
740             error.SetErrorToGenericError();
741             error.SetErrorString("Couldn't read: data buffer is empty");
742             return;
743         }
744         if (allocation.m_data.GetByteSize() < offset + size)
745         {
746             error.SetErrorToGenericError();
747             error.SetErrorString("Couldn't read: not enough underlying data");
748             return;
749         }
750 
751         ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size);
752         break;
753     case eAllocationPolicyMirror:
754         process_sp = m_process_wp.lock();
755         if (process_sp)
756         {
757             process_sp->ReadMemory(process_address, bytes, size, error);
758             if (!error.Success())
759                 return;
760         }
761         else
762         {
763             if (!allocation.m_data.GetByteSize())
764             {
765                 error.SetErrorToGenericError();
766                 error.SetErrorString("Couldn't read: data buffer is empty");
767                 return;
768             }
769             ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size);
770         }
771         break;
772     case eAllocationPolicyProcessOnly:
773         process_sp = m_process_wp.lock();
774         if (process_sp)
775         {
776             process_sp->ReadMemory(process_address, bytes, size, error);
777             if (!error.Success())
778                 return;
779         }
780         break;
781     }
782 
783     if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
784     {
785         log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
786                     (uint64_t)process_address,
787                     (uint64_t)bytes,
788                     (uint64_t)size,
789                     (uint64_t)allocation.m_process_start,
790                     (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size);
791     }
792 }
793 
794 void
795 IRMemoryMap::ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error)
796 {
797     error.Clear();
798 
799     if (size > 0)
800     {
801         DataBufferHeap buf(size, 0);
802         ReadMemory(buf.GetBytes(), process_address, size, error);
803 
804         if (!error.Success())
805             return;
806 
807         DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(), GetAddressByteSize());
808 
809         lldb::offset_t offset = 0;
810 
811         switch (size)
812         {
813         default:
814             error.SetErrorToGenericError();
815             error.SetErrorStringWithFormat("Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
816             return;
817         case 1: scalar = extractor.GetU8(&offset);  break;
818         case 2: scalar = extractor.GetU16(&offset); break;
819         case 4: scalar = extractor.GetU32(&offset); break;
820         case 8: scalar = extractor.GetU64(&offset); break;
821         }
822     }
823     else
824     {
825         error.SetErrorToGenericError();
826         error.SetErrorString ("Couldn't read scalar: its size was zero");
827     }
828     return;
829 }
830 
831 void
832 IRMemoryMap::ReadPointerFromMemory (lldb::addr_t *address, lldb::addr_t process_address, Error &error)
833 {
834     error.Clear();
835 
836     Scalar pointer_scalar;
837     ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(), error);
838 
839     if (!error.Success())
840         return;
841 
842     *address = pointer_scalar.ULongLong();
843 
844     return;
845 }
846 
847 void
848 IRMemoryMap::GetMemoryData (DataExtractor &extractor, lldb::addr_t process_address, size_t size, Error &error)
849 {
850     error.Clear();
851 
852     if (size > 0)
853     {
854         AllocationMap::iterator iter = FindAllocation(process_address, size);
855 
856         if (iter == m_allocations.end())
857         {
858             error.SetErrorToGenericError();
859             error.SetErrorStringWithFormat("Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64 ")", process_address, process_address + size);
860             return;
861         }
862 
863         Allocation &allocation = iter->second;
864 
865         switch (allocation.m_policy)
866         {
867         default:
868             error.SetErrorToGenericError();
869             error.SetErrorString("Couldn't get memory data: invalid allocation policy");
870             return;
871         case eAllocationPolicyProcessOnly:
872             error.SetErrorToGenericError();
873             error.SetErrorString("Couldn't get memory data: memory is only in the target");
874             return;
875         case eAllocationPolicyMirror:
876             {
877                 lldb::ProcessSP process_sp = m_process_wp.lock();
878 
879                 if (!allocation.m_data.GetByteSize())
880                 {
881                     error.SetErrorToGenericError();
882                     error.SetErrorString("Couldn't get memory data: data buffer is empty");
883                     return;
884                 }
885                 if (process_sp)
886                 {
887                     process_sp->ReadMemory(allocation.m_process_start, allocation.m_data.GetBytes(), allocation.m_data.GetByteSize(), error);
888                     if (!error.Success())
889                         return;
890                     uint64_t offset = process_address - allocation.m_process_start;
891                     extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize());
892                     return;
893                 }
894             }
895             break;
896         case eAllocationPolicyHostOnly:
897             if (!allocation.m_data.GetByteSize())
898             {
899                 error.SetErrorToGenericError();
900                 error.SetErrorString("Couldn't get memory data: data buffer is empty");
901                 return;
902             }
903             uint64_t offset = process_address - allocation.m_process_start;
904             extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize());
905             return;
906         }
907     }
908     else
909     {
910         error.SetErrorToGenericError();
911         error.SetErrorString ("Couldn't get memory data: its size was zero");
912         return;
913     }
914 }
915