def _post_assignement_operation(self, expression): left = get(expression.expression_left) right = get(expression.expression_right) if isinstance(left, list): # tuple expression: if isinstance(right, list): # unbox assigment assert len(left) == len(right) for idx in range(len(left)): if not left[idx] is None: operation = convert_assignment(left[idx], right[idx], expression.type, expression.expression_return_type) self._result.append(operation) set_val(expression, None) else: assert isinstance(right, TupleVariable) for idx in range(len(left)): if not left[idx] is None: operation = Unpack(left[idx], right, idx) self._result.append(operation) set_val(expression, None) else: # Init of array, like # uint8[2] var = [1,2]; if isinstance(right, list): operation = InitArray(right, left) self._result.append(operation) set_val(expression, left) else: operation = convert_assignment(left, right, expression.type, expression.expression_return_type) self._result.append(operation) # Return left to handle # a = b = 1; set_val(expression, left)
def _post_index_access(self, expression): left = get(expression.expression_left) right = get(expression.expression_right) # Left can be a type for abi.decode(var, uint[2]) if isinstance(left, Type): # Nested type are not yet supported by abi.decode, so the assumption # Is that the right variable must be a constant assert isinstance(right, Constant) t = ArrayType(left, right.value) set_val(expression, t) return val = ReferenceVariable(self._node) # access to anonymous array # such as [0,1][x] if isinstance(left, list): init_array_val = TemporaryVariable(self._node) init_array_right = left left = init_array_val operation = InitArray(init_array_right, init_array_val) operation.set_expression(expression) self._result.append(operation) operation = Index(val, left, right, expression.type) operation.set_expression(expression) self._result.append(operation) set_val(expression, val)
def convert_to_push(ir): """ Convert a call to a PUSH operaiton The funciton assume to receive a correct IR The checks must be done by the caller May necessitate to create an intermediate operation (InitArray) As a result, the function return may return a list """ if isinstance(ir.arguments[0], list): ret = [] val = TemporaryVariable() operation = InitArray(ir.arguments[0], val) ret.append(operation) ir = Push(ir.destination, val) length = Literal(len(operation.init_values)) t = operation.init_values[0].type ir.lvalue.set_type(ArrayType(t, length)) ret.append(ir) return ret ir = Push(ir.destination, ir.arguments[0]) return ir
def _post_assignement_operation(self, expression): left = get(expression.expression_left) right = get(expression.expression_right) if isinstance(left, list): # tuple expression: if isinstance(right, list): # unbox assigment assert len(left) == len(right) for idx, _ in enumerate(left): if not left[idx] is None: operation = convert_assignment( left[idx], right[idx], expression.type, expression.expression_return_type, ) operation.set_expression(expression) self._result.append(operation) set_val(expression, None) else: assert isinstance(right, TupleVariable) for idx, _ in enumerate(left): if not left[idx] is None: index = idx # The following test is probably always true? if (isinstance(left[idx], LocalVariableInitFromTuple) and left[idx].tuple_index is not None): index = left[idx].tuple_index operation = Unpack(left[idx], right, index) operation.set_expression(expression) self._result.append(operation) set_val(expression, None) # Tuple with only one element. We need to convert the assignment to a Unpack # Ex: # (uint a,,) = g() elif (isinstance(left, LocalVariableInitFromTuple) and left.tuple_index is not None and isinstance(right, TupleVariable)): operation = Unpack(left, right, left.tuple_index) operation.set_expression(expression) self._result.append(operation) set_val(expression, None) else: # Init of array, like # uint8[2] var = [1,2]; if isinstance(right, list): operation = InitArray(right, left) operation.set_expression(expression) self._result.append(operation) set_val(expression, left) else: operation = convert_assignment( left, right, expression.type, expression.expression_return_type) operation.set_expression(expression) self._result.append(operation) # Return left to handle # a = b = 1; set_val(expression, left)
def convert_to_push(ir, node): """ Convert a call to a PUSH operaiton The funciton assume to receive a correct IR The checks must be done by the caller May necessitate to create an intermediate operation (InitArray) Necessitate to return the lenght (see push documentation) As a result, the function return may return a list """ # TODO remove Push Operator, and change this to existing operators lvalue = ir.lvalue if isinstance(ir.arguments[0], list): ret = [] val = TemporaryVariable(node) operation = InitArray(ir.arguments[0], val) operation.set_expression(ir.expression) ret.append(operation) prev_ir = ir ir = Push(ir.destination, val) ir.set_expression(prev_ir.expression) length = Literal(len(operation.init_values), 'uint256') t = operation.init_values[0].type ir.lvalue.set_type(ArrayType(t, length)) ret.append(ir) if lvalue: length = Length(ir.array, lvalue) length.set_expression(ir.expression) length.lvalue.points_to = ir.lvalue ret.append(length) return ret prev_ir = ir ir = Push(ir.destination, ir.arguments[0]) ir.set_expression(prev_ir.expression) if lvalue: ret = [] ret.append(ir) length = Length(ir.array, lvalue) length.set_expression(ir.expression) length.lvalue.points_to = ir.lvalue ret.append(length) return ret return ir
def _post_index_access(self, expression): left = get(expression.expression_left) right = get(expression.expression_right) val = ReferenceVariable(self._node) # access to anonymous array # such as [0,1][x] if isinstance(left, list): init_array_val = TemporaryVariable(self._node) init_array_right = left left = init_array_val operation = InitArray(init_array_right, init_array_val) self._result.append(operation) operation = Index(val, left, right, expression.type) self._result.append(operation) set_val(expression, val)
def copy_ir(ir, local_variables_instances, state_variables_instances, temporary_variables_instances, reference_variables_instances, all_local_variables_instances): ''' Args: ir (Operation) local_variables_instances(dict(str -> LocalVariable)) state_variables_instances(dict(str -> StateVariable)) temporary_variables_instances(dict(int -> Variable)) reference_variables_instances(dict(int -> Variable)) Note: temporary and reference can be indexed by int, as they dont need phi functions ''' def get(variable): if isinstance(variable, LocalVariable): if variable.name in local_variables_instances: return local_variables_instances[variable.name] new_var = LocalIRVariable(variable) local_variables_instances[variable.name] = new_var all_local_variables_instances[variable.name] = new_var return new_var if isinstance( variable, StateVariable ) and variable.canonical_name in state_variables_instances: return state_variables_instances[variable.canonical_name] elif isinstance(variable, ReferenceVariable): if not variable.index in reference_variables_instances: new_variable = ReferenceVariable(variable.node, index=variable.index) if variable.points_to: new_variable.points_to = get(variable.points_to) new_variable.set_type(variable.type) reference_variables_instances[variable.index] = new_variable return reference_variables_instances[variable.index] elif isinstance(variable, TemporaryVariable): if not variable.index in temporary_variables_instances: new_variable = TemporaryVariable(variable.node, index=variable.index) new_variable.set_type(variable.type) temporary_variables_instances[variable.index] = new_variable return temporary_variables_instances[variable.index] return variable def get_variable(ir, f): variable = f(ir) variable = get(variable) return variable def get_arguments(ir): arguments = [] for arg in ir.arguments: arg = get(arg) arguments.append(arg) return arguments def get_rec_values(ir, f): # Use by InitArray and NewArray # Potential recursive array(s) ori_init_values = f(ir) def traversal(values): ret = [] for v in values: if isinstance(v, list): v = traversal(v) else: v = get(v) ret.append(v) return ret return traversal(ori_init_values) if isinstance(ir, Assignment): lvalue = get_variable(ir, lambda x: ir.lvalue) rvalue = get_variable(ir, lambda x: ir.rvalue) variable_return_type = ir.variable_return_type return Assignment(lvalue, rvalue, variable_return_type) elif isinstance(ir, Balance): lvalue = get_variable(ir, lambda x: ir.lvalue) value = get_variable(ir, lambda x: ir.value) return Balance(value, lvalue) elif isinstance(ir, Binary): lvalue = get_variable(ir, lambda x: ir.lvalue) variable_left = get_variable(ir, lambda x: ir.variable_left) variable_right = get_variable(ir, lambda x: ir.variable_right) operation_type = ir.type return Binary(lvalue, variable_left, variable_right, operation_type) elif isinstance(ir, Condition): val = get_variable(ir, lambda x: ir.value) return Condition(val) elif isinstance(ir, Delete): lvalue = get_variable(ir, lambda x: ir.lvalue) variable = get_variable(ir, lambda x: ir.variable) return Delete(lvalue, variable) elif isinstance(ir, EventCall): name = ir.name return EventCall(name) elif isinstance(ir, HighLevelCall): # include LibraryCall destination = get_variable(ir, lambda x: ir.destination) function_name = ir.function_name nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: ir.lvalue) type_call = ir.type_call if isinstance(ir, LibraryCall): new_ir = LibraryCall(destination, function_name, nbr_arguments, lvalue, type_call) else: new_ir = HighLevelCall(destination, function_name, nbr_arguments, lvalue, type_call) new_ir.call_id = ir.call_id new_ir.call_value = get_variable(ir, lambda x: ir.call_value) new_ir.call_gas = get_variable(ir, lambda x: ir.call_gas) new_ir.arguments = get_arguments(ir) new_ir.function = ir.function return new_ir elif isinstance(ir, Index): lvalue = get_variable(ir, lambda x: ir.lvalue) variable_left = get_variable(ir, lambda x: ir.variable_left) variable_right = get_variable(ir, lambda x: ir.variable_right) index_type = ir.index_type return Index(lvalue, variable_left, variable_right, index_type) elif isinstance(ir, InitArray): lvalue = get_variable(ir, lambda x: ir.lvalue) init_values = get_rec_values(ir, lambda x: ir.init_values) return InitArray(init_values, lvalue) elif isinstance(ir, InternalCall): function = ir.function nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: ir.lvalue) type_call = ir.type_call new_ir = InternalCall(function, nbr_arguments, lvalue, type_call) new_ir.arguments = get_arguments(ir) return new_ir elif isinstance(ir, InternalDynamicCall): lvalue = get_variable(ir, lambda x: ir.lvalue) function = ir.function function_type = ir.function_type new_ir = InternalDynamicCall(lvalue, function, function_type) new_ir.arguments = get_arguments(ir) return new_ir elif isinstance(ir, LowLevelCall): destination = get_variable(ir, lambda x: x.destination) function_name = ir.function_name nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: ir.lvalue) type_call = ir.type_call new_ir = LowLevelCall(destination, function_name, nbr_arguments, lvalue, type_call) new_ir.call_id = ir.call_id new_ir.call_value = get_variable(ir, lambda x: ir.call_value) new_ir.call_gas = get_variable(ir, lambda x: ir.call_gas) new_ir.arguments = get_arguments(ir) return new_ir elif isinstance(ir, Member): lvalue = get_variable(ir, lambda x: ir.lvalue) variable_left = get_variable(ir, lambda x: ir.variable_left) variable_right = get_variable(ir, lambda x: ir.variable_right) return Member(variable_left, variable_right, lvalue) elif isinstance(ir, NewArray): depth = ir.depth array_type = ir.array_type lvalue = get_variable(ir, lambda x: ir.lvalue) new_ir = NewArray(depth, array_type, lvalue) new_ir.arguments = get_rec_values(ir, lambda x: ir.arguments) return new_ir elif isinstance(ir, NewElementaryType): new_type = ir.type lvalue = get_variable(ir, lambda x: ir.lvalue) new_ir = NewElementaryType(new_type, lvalue) new_ir.arguments = get_arguments(ir) return new_ir elif isinstance(ir, NewContract): contract_name = ir.contract_name lvalue = get_variable(ir, lambda x: ir.lvalue) new_ir = NewContract(contract_name, lvalue) new_ir.arguments = get_arguments(ir) return new_ir elif isinstance(ir, NewStructure): structure = ir.structure lvalue = get_variable(ir, lambda x: ir.lvalue) new_ir = NewStructure(structure, lvalue) new_ir.arguments = get_arguments(ir) return new_ir elif isinstance(ir, Push): array = get_variable(ir, lambda x: ir.array) lvalue = get_variable(ir, lambda x: ir.lvalue) return Push(array, lvalue) elif isinstance(ir, Return): value = [get_variable(x, lambda y: y) for x in ir.values] return Return(value) elif isinstance(ir, Send): destination = get_variable(ir, lambda x: ir.destination) value = get_variable(ir, lambda x: ir.call_value) lvalue = get_variable(ir, lambda x: ir.lvalue) return Send(destination, value, lvalue) elif isinstance(ir, SolidityCall): function = ir.function nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: ir.lvalue) type_call = ir.type_call new_ir = SolidityCall(function, nbr_arguments, lvalue, type_call) new_ir.arguments = get_arguments(ir) return new_ir elif isinstance(ir, Transfer): destination = get_variable(ir, lambda x: ir.destination) value = get_variable(ir, lambda x: ir.call_value) return Transfer(destination, value) elif isinstance(ir, TypeConversion): lvalue = get_variable(ir, lambda x: ir.lvalue) variable = get_variable(ir, lambda x: ir.variable) variable_type = ir.type return TypeConversion(lvalue, variable, variable_type) elif isinstance(ir, Unary): lvalue = get_variable(ir, lambda x: ir.lvalue) rvalue = get_variable(ir, lambda x: ir.rvalue) operation_type = ir.type return Unary(lvalue, rvalue, operation_type) elif isinstance(ir, Unpack): lvalue = get_variable(ir, lambda x: ir.lvalue) tuple_var = ir.tuple idx = ir.index return Unpack(lvalue, tuple_var, idx) elif isinstance(ir, Length): lvalue = get_variable(ir, lambda x: ir.lvalue) value = get_variable(ir, lambda x: ir.value) return Length(value, lvalue) logger.error('Impossible ir copy on {} ({})'.format(ir, type(ir))) exit(-1)
def copy_ir(ir, *instances): ''' Args: ir (Operation) local_variables_instances(dict(str -> LocalVariable)) state_variables_instances(dict(str -> StateVariable)) temporary_variables_instances(dict(int -> Variable)) reference_variables_instances(dict(int -> Variable)) Note: temporary and reference can be indexed by int, as they dont need phi functions ''' if isinstance(ir, Assignment): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) rvalue = get_variable(ir, lambda x: x.rvalue, *instances) variable_return_type = ir.variable_return_type return Assignment(lvalue, rvalue, variable_return_type) elif isinstance(ir, Balance): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) value = get_variable(ir, lambda x: x.value, *instances) return Balance(value, lvalue) elif isinstance(ir, Binary): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) variable_left = get_variable(ir, lambda x: x.variable_left, *instances) variable_right = get_variable(ir, lambda x: x.variable_right, *instances) operation_type = ir.type return Binary(lvalue, variable_left, variable_right, operation_type) elif isinstance(ir, Condition): val = get_variable(ir, lambda x: x.value, *instances) return Condition(val) elif isinstance(ir, Delete): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) variable = get_variable(ir, lambda x: x.variable, *instances) return Delete(lvalue, variable) elif isinstance(ir, EventCall): name = ir.name return EventCall(name) elif isinstance(ir, HighLevelCall): # include LibraryCall destination = get_variable(ir, lambda x: x.destination, *instances) function_name = ir.function_name nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: x.lvalue, *instances) type_call = ir.type_call if isinstance(ir, LibraryCall): new_ir = LibraryCall(destination, function_name, nbr_arguments, lvalue, type_call) else: new_ir = HighLevelCall(destination, function_name, nbr_arguments, lvalue, type_call) new_ir.call_id = ir.call_id new_ir.call_value = get_variable(ir, lambda x: x.call_value, *instances) new_ir.call_gas = get_variable(ir, lambda x: x.call_gas, *instances) new_ir.arguments = get_arguments(ir, *instances) new_ir.function = ir.function return new_ir elif isinstance(ir, Index): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) variable_left = get_variable(ir, lambda x: x.variable_left, *instances) variable_right = get_variable(ir, lambda x: x.variable_right, *instances) index_type = ir.index_type return Index(lvalue, variable_left, variable_right, index_type) elif isinstance(ir, InitArray): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) init_values = get_rec_values(ir, lambda x: x.init_values, *instances) return InitArray(init_values, lvalue) elif isinstance(ir, InternalCall): function = ir.function nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: x.lvalue, *instances) type_call = ir.type_call new_ir = InternalCall(function, nbr_arguments, lvalue, type_call) new_ir.arguments = get_arguments(ir, *instances) return new_ir elif isinstance(ir, InternalDynamicCall): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) function = get_variable(ir, lambda x: x.function, *instances) function_type = ir.function_type new_ir = InternalDynamicCall(lvalue, function, function_type) new_ir.arguments = get_arguments(ir, *instances) return new_ir elif isinstance(ir, LowLevelCall): destination = get_variable(ir, lambda x: x.destination, *instances) function_name = ir.function_name nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: x.lvalue, *instances) type_call = ir.type_call new_ir = LowLevelCall(destination, function_name, nbr_arguments, lvalue, type_call) new_ir.call_id = ir.call_id new_ir.call_value = get_variable(ir, lambda x: x.call_value, *instances) new_ir.call_gas = get_variable(ir, lambda x: x.call_gas, *instances) new_ir.arguments = get_arguments(ir, *instances) return new_ir elif isinstance(ir, Member): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) variable_left = get_variable(ir, lambda x: x.variable_left, *instances) variable_right = get_variable(ir, lambda x: x.variable_right, *instances) return Member(variable_left, variable_right, lvalue) elif isinstance(ir, NewArray): depth = ir.depth array_type = ir.array_type lvalue = get_variable(ir, lambda x: x.lvalue, *instances) new_ir = NewArray(depth, array_type, lvalue) new_ir.arguments = get_rec_values(ir, lambda x: x.arguments, *instances) return new_ir elif isinstance(ir, NewElementaryType): new_type = ir.type lvalue = get_variable(ir, lambda x: x.lvalue, *instances) new_ir = NewElementaryType(new_type, lvalue) new_ir.arguments = get_arguments(ir, *instances) return new_ir elif isinstance(ir, NewContract): contract_name = ir.contract_name lvalue = get_variable(ir, lambda x: x.lvalue, *instances) new_ir = NewContract(contract_name, lvalue) new_ir.arguments = get_arguments(ir, *instances) return new_ir elif isinstance(ir, NewStructure): structure = ir.structure lvalue = get_variable(ir, lambda x: x.lvalue, *instances) new_ir = NewStructure(structure, lvalue) new_ir.arguments = get_arguments(ir, *instances) return new_ir elif isinstance(ir, Push): array = get_variable(ir, lambda x: x.array, *instances) lvalue = get_variable(ir, lambda x: x.lvalue, *instances) return Push(array, lvalue) elif isinstance(ir, Return): values = get_rec_values(ir, lambda x: x.values, *instances) return Return(values) elif isinstance(ir, Send): destination = get_variable(ir, lambda x: x.destination, *instances) value = get_variable(ir, lambda x: x.call_value, *instances) lvalue = get_variable(ir, lambda x: x.lvalue, *instances) return Send(destination, value, lvalue) elif isinstance(ir, SolidityCall): function = ir.function nbr_arguments = ir.nbr_arguments lvalue = get_variable(ir, lambda x: x.lvalue, *instances) type_call = ir.type_call new_ir = SolidityCall(function, nbr_arguments, lvalue, type_call) new_ir.arguments = get_arguments(ir, *instances) return new_ir elif isinstance(ir, Transfer): destination = get_variable(ir, lambda x: x.destination, *instances) value = get_variable(ir, lambda x: x.call_value, *instances) return Transfer(destination, value) elif isinstance(ir, TypeConversion): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) variable = get_variable(ir, lambda x: x.variable, *instances) variable_type = ir.type return TypeConversion(lvalue, variable, variable_type) elif isinstance(ir, Unary): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) rvalue = get_variable(ir, lambda x: x.rvalue, *instances) operation_type = ir.type return Unary(lvalue, rvalue, operation_type) elif isinstance(ir, Unpack): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) tuple_var = get_variable(ir, lambda x: x.tuple, *instances) idx = ir.index return Unpack(lvalue, tuple_var, idx) elif isinstance(ir, Length): lvalue = get_variable(ir, lambda x: x.lvalue, *instances) value = get_variable(ir, lambda x: x.value, *instances) return Length(value, lvalue) logger.error('Impossible ir copy on {} ({})'.format(ir, type(ir))) exit(-1)