[docs]classHeapAllocator:""" A simple modelisation to help represent heap memory management during a <ReachingDefinitionsAnalysis>: - Act as if allocations were always done in consecutive memory segments; - Take care of the size not to screw potential pointer arithmetic (avoid overlapping segments). The content of the heap itself is modeled using a <KeyedRegion> attribute in the <LiveDefinitions> state; This class serves to generate consistent heap addresses to be used by the aforementionned. *Note:* This has **NOT** been made to help detect heap vulnerabilities. """
[docs]def__init__(self,canonical_size:int):""" :param canonical_size: The concrete size an <UNKNOWN_SIZE> defaults to. """self._next_heap_address:HeapAddress=HeapAddress(0)self._allocated_addresses:List[HeapAddress]=[self._next_heap_address]self._canonical_size:int=canonical_size
[docs]defallocate(self,size:Union[int,UnknownSize])->HeapAddress:""" Gives an address for a new memory chunck of <size> bytes. :param size: The requested size for the chunck, in number of bytes. :return: The address of the chunck. """address=self._next_heap_addresssize=self._canonical_sizeifisinstance(size,UnknownSize)elsesizeself._next_heap_address+=sizeself._allocated_addresses+=[self._next_heap_address]returnaddress
[docs]deffree(self,address:Union[Undefined,HeapAddress]):""" Mark the chunck pointed by <address> as freed. :param address: The address of the chunck to free. """ifisinstance(address,Undefined):_l.debug("free(), Undefined address provided")elifisinstance(address,HeapAddress):try:self._allocated_addresses.remove(address)exceptValueError:_l.warning("free(), address %s had not been allocated",address)else:_l.warning("free(), expected HeapAddress, or Undefined, got %s",type(address).__name__)
@propertydefallocated_addresses(self):""" :return: The list of addresses that are currently allocated on the heap. """returnself._allocated_addresses