def mload_(self, global_state): state = global_state.mstate op0 = state.stack.pop() logging.debug("MLOAD[" + str(op0) + "]") try: offset = util.get_concrete_int(op0) except AttributeError: logging.debug("Can't MLOAD from symbolic index") data = BitVec("mem_" + str(op0), 256) state.stack.append(data) return [global_state] try: data = util.concrete_int_from_bytes(state.memory, offset) except IndexError: # Memory slot not allocated data = BitVec("mem_" + str(offset), 256) except TypeError: # Symbolic memory data = state.memory[offset] logging.debug("Load from memory[" + str(offset) + "]: " + str(data)) state.stack.append(data) return [global_state]
def get_word_at(self, index: int) -> Union[int, BitVec]: """Access a word from a specified memory index. :param index: integer representing the index to access :return: 32 byte word at the specified index """ try: return symbol_factory.BitVecVal( util.concrete_int_from_bytes( bytes([ util.get_concrete_int(b) for b in self[index:index + 32] ]), 0, ), 256, ) except TypeError: result = simplify( Concat([ b if isinstance(b, BitVec) else symbol_factory.BitVecVal( b, 8) for b in cast(List[Union[int, BitVec]], self[index:index + 32]) ])) assert result.size() == 256 return result
def sha3_(self, global_state): global keccak_function_manager state = global_state.mstate environment = global_state.environment op0, op1 = state.stack.pop(), state.stack.pop() try: index, length = util.get_concrete_int(op0), util.get_concrete_int(op1) # FIXME: broad exception catch except: # Can't access symbolic memory offsets if is_expr(op0): op0 = simplify(op0) state.stack.append(BitVec("KECCAC_mem[" + str(op0) + "]", 256)) return [global_state] try: state.mem_extend(index, length) data = b''.join([util.get_concrete_int(i).to_bytes(1, byteorder='big') for i in state.memory[index: index + length]]) except AttributeError: argument = str(state.memory[index]).replace(" ", "_") result = BitVec("KECCAC[{}]".format(argument), 256) keccak_function_manager.add_keccak(result, state.memory[index]) state.stack.append(result) return [global_state] keccak = utils.sha3(utils.bytearray_to_bytestr(data)) logging.debug("Computed SHA3 Hash: " + str(binascii.hexlify(keccak))) state.stack.append(BitVecVal(util.concrete_int_from_bytes(keccak, 0), 256)) return [global_state]
def may_write_to(self, z3_index, z3_value, storage, constraint_list, consider_length): if consider_length: may_write_to, added_constraints = ConcretSlot.may_write_to(self, z3_index, z3_value, storage, constraint_list, consider_length) if may_write_to: return may_write_to, added_constraints slot_hash = utils.sha3(utils.bytearray_to_bytestr(util.concrete_int_to_bytes(self.slot_counter))) slot_hash = str(BitVecVal(util.concrete_int_from_bytes(slot_hash, 0), 256)) z3_index_str = str(z3_index).replace("\n", "") if z3_index_str.startswith(slot_hash): if z3_index_str.endswith(slot_hash) or z3_index_str[len(slot_hash):].startswith(" +"): return True, [] if z3_index_str in keccak_map: unresolved_index = keccak_map[str(z3_index).replace("\n" , "")] unresolved_index = str(unresolved_index).replace("\n", "") if unresolved_index.startswith(str(self.slot_counter)): if unresolved_index.endswith(str(self.slot_counter)) or unresolved_index[len(str(self.slot_counter)):].startswith(" +"): return True, [] return False, []
def sha3_(self, global_state): state = global_state.mstate environment = global_state.environment op0, op1 = state.stack.pop(), state.stack.pop() try: index, length = util.get_concrete_int(op0), util.get_concrete_int( op1) # FIXME: broad exception catch except: # Can't access symbolic memory offsets if is_expr(op0): op0 = simplify(op0) state.stack.append(BitVec("KECCAC_mem[" + str(op0) + "]", 256)) return [global_state] try: data = b'' for i in range(index, index + length): data += util.get_concrete_int(state.memory[i]).to_bytes( 1, byteorder='big') i += 1 # FIXME: broad exception catch except: svar = str(state.memory[index]) svar = svar.replace(" ", "_") state.stack.append(BitVec("keccac_" + svar, 256)) return [global_state] keccac = utils.sha3(utils.bytearray_to_bytestr(data)) logging.debug("Computed SHA3 Hash: " + str(binascii.hexlify(keccac))) state.stack.append( BitVecVal(util.concrete_int_from_bytes(keccac, 0), 256)) return [global_state]