[docs]classDataNormalizationMixin(MemoryMixin):""" Normalizes the data field for a store and the fallback field for a load to be BVs. """
[docs]defstore(self,addr,data,size=None,**kwargs):data_bv=self._convert_to_ast(data,size,self.state.arch.byte_width)# zero extend if size is greater than len(data_e)# TODO move this to the register resolver# bit_width = size*self.state.arch.byte_width if isinstance(size, int) else self.state.arch.bits# if size is not None and self.category == 'reg' and len(data_bv) < bit_width:# data_bv = data_bv.zero_extend(bit_width - len(data_bv))iflen(data_bv)%self.state.arch.byte_width!=0:raiseSimMemoryError("Attempting to store non-byte data to memory")super().store(addr,data_bv,size=size,**kwargs)
def_convert_to_ast(self,thing,size,byte_width):""" :param thing: The thing to convert to an AST :param size: The size of the thing in bytes :param byte_width: The size of a byte in bits """iftype(thing)isclaripy.ast.BV:returnthingiftype(size)isint:bits=size*byte_widthelifgetattr(size,"op",None)=="BVV":bits=size.args[0]*byte_widthelse:bits=Noneifisinstance(thing,str):l.warning("Encoding unicode string for memory as utf-8. Did you mean to use a bytestring?")thing=thing.encode("utf-8")iftype(thing)in(bytes,bytearray,memoryview):returnclaripy.BVV(thing)eliftype(thing)isint:ifbitsisNone:l.warning("Unknown size for memory data %#x. Default to arch.bits.",thing)bits=self.state.arch.bitsreturnclaripy.BVV(thing,bits)eliftype(thing)isfloat:ifbits==32:returnclaripy.FPV(thing,claripy.FSORT_FLOAT).raw_to_bv()elifbits==64:returnclaripy.FPV(thing,claripy.FSORT_DOUBLE).raw_to_bv()else:raiseTypeError("Passed float size which is not a float or a double",size)else:try:raw_to_bv=thing.raw_to_bvexceptAttributeError:raiseTypeError("Bad value passed to memory",thing)fromNoneelse:returnraw_to_bv()