def __init__(self, simstate: "SimulationState", base: str, initialized: bool = False, buffersize: Optional[int] = None) -> None: SimMemory.__init__(self, simstate, initialized, base) self._buffersize = buffersize self._status = "valid"
def __init__(self, simstate: "SimulationState", elfheader: ELFHeader, initialized: bool = False): SimMemory.__init__(self, simstate, initialized, "global") self._elfheader = elfheader self._accesses: Dict[str, List[str]] = {} self._patched_globals: Mapping[str, str] = {}
def __init__( self, simstate: "SimulationState", shmid: int, key: str, # hex value buffersize: int = 4096) -> None: SimMemory.__init__(self, simstate, True, "shared:" + str(shmid)) self._shmid = shmid self._buffersize = buffersize # segments may be mapped in multiple locations self._baseoffsets: List[int] = []
def set(self, iaddr: str, address: SSV.SimAddress, srcval: SV.SimValue) -> None: for base in self.baseoffsets: if ( address.offsetvalue >= base and address.offsetvalue < base + self.buffersize): address = address.add_offset(-base) SimMemory.set(self, iaddr, address, srcval) break else: raise SU.CHBSimError( self.simstate, iaddr, "Invalid shared memory address: " + str(address))
def get(self, iaddr: str, address: SSV.SimAddress, size: int) -> SV.SimValue: try: for base in self.baseoffsets: if ( address.offsetvalue >= base and address.offsetvalue < base + self.buffersize): address = address.add_offset(-base) try: memval = SimMemory.get(self, iaddr, address, size) except SU.CHBSimError: memval = SV.mk_simvalue(0, size=size) return memval else: raise SU.CHBSimError( self.simstate, iaddr, "invalid shared memory address: " + str(address)) except SU.CHBSimError as e: print("Error in shared memory: " + str(e)) name = (self.name + '[' + str(address.offsetvalue) + ']' + ' (value not retrieved: ' + str(e) + ')') return SSV.SimSymbol(name)
def get(self, iaddr: str, address: SSV.SimAddress, size: int) -> SV.SimValue: try: result = SimMemory.get(self, iaddr, address, size) if result.is_defined: return result else: return self.get_from_section(iaddr, address, size) except SU.CHBSimError: return self.get_from_section(iaddr, address, size)
def get(self, iaddr: str, address: SSV.SimAddress, size: int) -> SV.SimValue: try: memval = SimMemory.get(self, iaddr, address, size) except SU.CHBSimError as e: print("Error in basemem: " + str(e)) name = (self.name + '[' + str(address.offsetvalue) + ']' + ' (value not retrieved: ' + str(e) + ')') return SSV.SimSymbol(name) else: return memval
def __str__(self) -> str: lines: List[str] = [] if self.has_offset: try: for a in range(0, self.buffersize, 4): if a in self._mem: address = self.mk_address(a) try: charstring = self.char_string("", address, 4) except UF.CHBError: charstring = "?" memval = SimMemory.get(self, "0", address, 4) lines.append(str(hex(a)).rjust(12) + " " + str(a).rjust(12) + " " + str(memval) + " " + str(charstring)) except Exception: pass return "\n".join(lines)
def get_from_section(self, iaddr: str, address: SSV.SimAddress, size: int) -> SV.SimValue: if address.is_defined: offset = address.offsetvalue # address has been patched by user if self.has_patched_global(address): return self.get_patched_global(address) # try to get the value from the file image else: sectionindex = self.elfheader.get_elf_section_index(offset) if sectionindex is None: # address is not in file image self.simstate.add_logmsg("global memory", str(address) + " uninitialized") return SV.mk_simvalue(0, size=size) # store in global memory data structure for i in range(offset, offset + size): byteval = self.elfheader.get_memory_value(sectionindex, i) if byteval is not None: self.set_byte(iaddr, i, SV.SimByteValue(byteval)) else: raise UF.CHBError("No value found for " + hex(i) + " in section " + str(sectionindex)) # retrieve from global memory data structure memval = SimMemory.get(self, iaddr, address, size) if not memval.is_defined: memval = SV.mk_simvalue(0, size=size) self.simstate.add_logmsg('global memory', str(address) + ' uninitialized') self._accesses.setdefault(str(address), []) self._accesses[str(address)].append( str(iaddr) + ':' + str(memval)) return memval else: raise UF.CHBError("Address is not defined: " + str(address))
def __init__(self, simstate: "SimulationState", initialized: bool = False) -> None: SimMemory.__init__(self, simstate, initialized, "stack")
def initialize(self, iaddr: str): addr = SSV.mk_global_address(0, "shared") for i in range(0, self.buffersize): SimMemory.set(self, iaddr, addr.add_offset(i), SV.simZerobyte)