@@ -102,6 +102,13 @@ def add_mapinfo(self, mem_s: int, mem_e: int, mem_p: int, mem_info: str):
102102
103103
104104 def del_mapinfo (self , mem_s : int , mem_e : int ):
105+ """Subtract a memory range from map.
106+
107+ Args:
108+ mem_s: memory range start
109+ mem_e: memory range end
110+ """
111+
105112 tmp_map_info = []
106113
107114 for s , e , p , info in self .map_info :
@@ -129,6 +136,8 @@ def del_mapinfo(self, mem_s: int, mem_e: int):
129136
130137
131138 def show_mapinfo (self ):
139+ """Emit memory map info in a nicely formatted table.
140+ """
132141 def _perms_mapping (ps ):
133142 perms_d = {1 : "r" , 2 : "w" , 4 : "x" }
134143 perms_sym = []
@@ -155,6 +164,13 @@ def get_lib_base(self, filename: str) -> int:
155164 return - 1
156165
157166 def align (self , addr : int , alignment : int = 0x1000 ) -> int :
167+ """Round up to nearest alignment.
168+
169+ Args:
170+ addr: address to align
171+ alignment: alignment granularity, must be a power of 2
172+ """
173+
158174 # rounds up to nearest alignment
159175 mask = ((1 << self .ql .archbit ) - 1 ) & - alignment
160176 return (addr + (alignment - 1 )) & mask
@@ -187,9 +203,27 @@ def restore(self, mem_dict):
187203 self .write (start , mem_read )
188204
189205 def read (self , addr : int , size : int ) -> bytearray :
206+ """Read bytes from memory.
207+
208+ Args:
209+ addr: source address
210+ size: amount of bytes to read
211+
212+ Returns: bytes located at the specified address
213+ """
214+
190215 return self .ql .uc .mem_read (addr , size )
191216
192217 def read_ptr (self , addr : int , size : int = None ) -> int :
218+ """Read an integer value from a memory address.
219+
220+ Args:
221+ addr: memory address to read
222+ size: pointer size (in bytes): either 1, 2, 4, 8, or None for arch native size
223+
224+ Returns: integer value stored at the specified memory address
225+ """
226+
193227 if not size :
194228 size = self .ql .pointersize
195229
@@ -205,6 +239,13 @@ def read_ptr(self, addr: int, size: int=None) -> int:
205239 raise QlErrorStructConversion (f"Unsupported pointer size: { size } " )
206240
207241 def write (self , addr : int , data : bytes ) -> None :
242+ """Write bytes to a memory.
243+
244+ Args:
245+ addr: destination address
246+ data: bytes to write
247+ """
248+
208249 try :
209250 self .ql .uc .mem_write (addr , data )
210251 except :
@@ -239,17 +280,21 @@ def search(self, needle: bytes, begin: int = None, end: int = None):
239280 return addrs
240281
241282 def unmap (self , addr : int , size : int ) -> None :
242- '''
243- The main function of mem_unmap is to reclaim memory.
244- This function will reclaim the memory starting with addr and length of size.
245- Upon successful completion, munmap() shall return 0;
246- otherwise, it shall return -1 and set errno to indicate the error.
247- '''
283+ """Reclaim a memory range.
284+
285+ Args:
286+ addr: range base address
287+ size: range size (in bytes)
288+ """
289+
248290 self .del_mapinfo (addr , addr + size )
249291 self .ql .uc .mem_unmap (addr , size )
250292
251293
252294 def unmap_all (self ):
295+ """Reclaim the entire memory space.
296+ """
297+
253298 for region in list (self .ql .uc .mem_regions ()):
254299 if region [0 ] and region [1 ]:
255300 return self .unmap (region [0 ], ((region [1 ] - region [0 ])+ 0x1 ))
@@ -502,12 +547,28 @@ def alloc(self, size: int):
502547 return chunk .address
503548
504549 def size (self , addr : int ) -> int :
550+ """Get the size of allocated memory chunk starting at a specific address.
551+
552+ Args:
553+ addr: chunk starting address
554+
555+ Returns: chunk size (in bytes), or 0 if no chunk starts at that address
556+ """
557+
505558 for chunk in self .chunks :
506559 if addr == chunk .address and chunk .inuse :
507560 return chunk .size
508561 return 0
509562
510563 def free (self , addr : int ) -> bool :
564+ """Free up memory at a specific address.
565+
566+ Args:
567+ addr: address of memory to free
568+
569+ Returns: True iff memory was freed successfully, False otherwise
570+ """
571+
511572 for chunk in self .chunks :
512573 if addr == chunk .address and chunk .inuse :
513574 chunk .inuse = False
@@ -528,6 +589,16 @@ def clear(self):
528589 self .current_use = 0
529590
530591 def _find (self , addr : int , inuse : bool = None ) -> Optional [Chunk ]:
592+ """Find a chunk starting at a specified address.
593+
594+ Args:
595+ addr: starting address of the requested chunk
596+ inuse: whether the chunk should be in-use; None if dont care
597+
598+ Returns: chunk instance starting at specified address whose in-use status is set
599+ as required (if required), None if no such chunk was found
600+ """
601+
531602 for chunk in self .chunks :
532603 if addr == chunk .address :
533604 return chunk
0 commit comments