[docs]classChunk:""" The sort of chunk as would typically be found in a freelist-style heap implementation. Provides a representation of a chunk via a view into the memory plugin. Chunks may be adjacent, in different senses, to as many as four other chunks. For any given chunk, two of these chunks are adjacent to it in memory, and are referred to as the "previous" and "next" chunks throughout this implementation. For any given free chunk, there may also be two significant chunks that are adjacent to it in some linked list of free chunks. These chunks are referred to the "backward" and "foward" chunks relative to the chunk in question. :ivar base: the location of the base of the chunk in memory :ivar state: the program state that the chunk is resident in """
[docs]def__init__(self,base,sim_state):self.state=sim_statedefsym_chunk_handler(chunk):l.warning("A pointer to a chunk is symbolic; maximizing it")returnself.state.solver.max_int(chunk)self.base=concretize(base,self.state.solver,sym_chunk_handler)
[docs]defget_size(self):""" Returns the actual size of a chunk (as opposed to the entire size field, which may include some flags). """raiseNotImplementedError(f"{self.get_size.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defget_data_size(self):""" Returns the size of the data portion of a chunk. """raiseNotImplementedError(f"{self.get_size.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defset_size(self,size):""" Sets the size of the chunk, preserving any flags. """raiseNotImplementedError(f"{self.set_size.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defdata_ptr(self):""" Returns the address of the payload of the chunk. """raiseNotImplementedError(f"{self.data_ptr.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defis_free(self):""" Returns a concrete determination as to whether the chunk is free. """raiseNotImplementedError(f"{self.is_free.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defnext_chunk(self):""" Returns the chunk immediately following (and adjacent to) this one. """raiseNotImplementedError(f"{self.next_chunk.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defprev_chunk(self):""" Returns the chunk immediately prior (and adjacent) to this one. """raiseNotImplementedError(f"{self.prev_chunk.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]deffwd_chunk(self):""" Returns the chunk following this chunk in the list of free chunks. """raiseNotImplementedError(f"{self.fwd_chunk.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defset_fwd_chunk(self,fwd):""" Sets the chunk following this chunk in the list of free chunks. :param fwd: the chunk to follow this chunk in the list of free chunks """raiseNotImplementedError(f"{self.set_fwd_chunk.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defbck_chunk(self):""" Returns the chunk backward from this chunk in the list of free chunks. """raiseNotImplementedError(f"{self.bck_chunk.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defset_bck_chunk(self,bck):""" Sets the chunk backward from this chunk in the list of free chunks. :param bck: the chunk to precede this chunk in the list of free chunks """raiseNotImplementedError(f"{self.set_bck_chunk.__func__.__name__} not implemented for {self.__class__.__name__}")
def_compare(self,other,comparison):ifself.stateisother.state:returncomparisonelse:raiseSimHeapError("Chunks must originate from the same simulation state to be compared!")def__lt__(self,other):""" Compares the base of this chunk with another chunk. """returnself._compare(other,self.base<other.base)def__le__(self,other):""" Compares the base of this chunk with another chunk. """returnself._compare(other,self.base<=other.base)def__eq__(self,other):""" Compares the base of this chunk with another chunk. """returnself._compare(other,self.base==other.base)def__ne__(self,other):""" Compares the base of this chunk with another chunk. """returnself._compare(other,self.base!=other.base)def__gt__(self,other):""" Compares the base of this chunk with another chunk. """returnself._compare(other,self.base>other.base)def__ge__(self,other):""" Compares the base of this chunk with another chunk. """returnself._compare(other,self.base>=other.base)def__repr__(self):return"<{} ({} @ 0x{:x})>".format(self.__class__.__name__,"free"ifself.is_free()else"used",self.base)
[docs]classSimHeapFreelist(SimHeapLibc):""" A freelist-style heap implementation. Distinguishing features of such heaps include chunks containing heap metadata in addition to user data and at least (but often more than) one linked list of free chunks. """def__iter__(self):returnself.chunks()
[docs]defchunks(self):""" Returns an iterator over all the chunks in the heap. """raiseNotImplementedError(f"{self.chunks.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defallocated_chunks(self):""" Returns an iterator over all the allocated chunks in the heap. """raiseNotImplementedError(f"{self.allocated_chunks.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]deffree_chunks(self):""" Returns an iterator over all the free chunks in the heap. """raiseNotImplementedError(f"{self.free_chunks.__func__.__name__} not implemented for {self.__class__.__name__}")
[docs]defchunk_from_mem(self,ptr):""" Given a pointer to a user payload, return the chunk associated with that payload. :param ptr: a pointer to the base of a user payload in the heap :returns: the associated heap chunk """raiseNotImplementedError(f"{self.chunk_from_mem.__func__.__name__} not implemented for {self.__class__.__name__}")