Beispiel #1
0
def repair_strict_balance(ast, charno):
    for num in charno:
        try:
            defectNode = BFSGetNode(ast, num)
            # find this.balance

            thisNode = None
            nodeQ = Queue()
            nodeQ.put(defectNode)  # root
            while (not nodeQ.empty()):
                tmpNode = nodeQ.get()
                if 'value' in tmpNode.attributes.keys(
                ) and tmpNode.attributes['value'] == 'this':
                    thisNode = tmpNode
                for childNode in tmpNode.children:
                    nodeQ.put(childNode)

            binaryOpNode = thisNode.father.father
            binaryOpNode1 = copy.deepcopy(binaryOpNode)
            binaryOpNode2 = copy.deepcopy(binaryOpNode)
            # >=
            binaryOpNode1.attributes['operator'] = '>='
            binaryOpNode2.attributes['operator'] = '<'

            origvalNode = binaryOpNode2.children[
                1]  # maybe behind is an expression
            tmpBinaryNode = TreeNode('BinaryOperation')
            tmpBinaryNode.beginPoint = -1
            tmpBinaryNode.attributes = {}
            tmpBinaryNode.attributes['operator'] = '+'
            oneNode = TreeNode('Literal')
            oneNode.attributes = {}
            oneNode.attributes['value'] = '1'
            oneNode.attributes['token'] = 'number'
            tmpBinaryNode.children.append(origvalNode)
            tmpBinaryNode.children.append(oneNode)
            secTuple = TreeNode('TupleExpression')
            secTuple.attributes = {'isInlineArray': False}
            secTuple.children.append(tmpBinaryNode)
            binaryOpNode2.children[1] = secTuple

            binaryOpNodeTotal = TreeNode('BinaryOperation')
            binaryOpNodeTotal.attributes = {}
            binaryOpNodeTotal.attributes['operator'] = '&&'

            tupleNode1 = TreeNode('TupleExpression')
            tupleNode1.attributes = {'isInlineArray': False}
            tupleNode1.children.append(binaryOpNode1)
            tupleNode2 = TreeNode('TupleExpression')
            tupleNode2.attributes = {'isInlineArray': False}
            tupleNode2.children.append(binaryOpNode2)

            binaryOpNodeTotal.children.append(tupleNode1)
            binaryOpNodeTotal.children.append(tupleNode2)

            upNode = binaryOpNode.father
            idx = upNode.children.index(binaryOpNode)
            upNode.children[idx] = binaryOpNodeTotal

        except:
            print('failing to repair the strict balance defect in charnum:' +
                  str(num))
            continue
Beispiel #2
0
def add_require_protect(defectNode):
    # find the contract node
    contractNode = defectNode
    while contractNode.nodeType != 'ContractDefinition':
        contractNode = contractNode.father
    
    state_var = []
    for item in contractNode.children:
        if item.nodeType == 'VariableDeclaration':
            if len(item.children) > 0 and item.children[0].nodeType == 'ElementaryTypeName' and item.children[0].attributes['name'] == 'address':
                state_var.append(item.attributes['name'])
    owner_var = ''
    
    for var in state_var:
        if 'owner' in var or 'create' in var or 'admin' in var:
            owner_var = var
            break
    
    if owner_var == '' and len(state_var) > 0:
        owner_var = state_var[0]
    
    if len(state_var) == 0:
        # add a variable declaration and insert into constructor
        owner_idennode = TreeNode('VariableDeclaration')
        owner_idennode.attributes = {'name':'contractOwner'}
        ele_node = TreeNode('ElementaryTypeName')
        ele_node.attributes = {'name':'address', 'value':'address'}
        owner_idennode.children.append(ele_node)

        idx = 0
        while idx < len(contractNode.children):
            if contractNode.children[idx].nodeType != 'InheritanceSpecifier':
                break
            idx+=1

        constructorNode = ''
        for child in contractNode.children:
            if child.nodeType == 'FunctionDefinition' and child.attributes['isConstructor'] == True:
                constructorNode = child
                break
        
        param_node = copy.deepcopy(owner_idennode)
        param_node.attributes['name'] = '_owner'
        # constructorNode.children[0].children.append(param_node)
        for ele in constructorNode.children:
            if ele.nodeType == 'ParameterList':
                ele.children.append(param_node)
                break

        expNode = TreeNode('ExpressionStatement')
        curbinaryOpNode = TreeNode('BinaryOperation')
        curbinaryOpNode.attributes = {}
        curbinaryOpNode.attributes['operator'] = '='

        left_node = TreeNode('Identifier')
        left_node.attributes = {'value':'contractOwner'}
        right_node = TreeNode('Identifier')
        right_node.attributes = {'value':'_owner'}

        curbinaryOpNode.children.append(left_node)
        curbinaryOpNode.children.append(right_node)
        expNode.children.append(curbinaryOpNode)
        # constructorNode.children[2].children.insert(0, expNode)
        for ele in constructorNode.children:
            if ele.nodeType == 'Block':
                ele.children.append(expNode)
                break

        contractNode.children.insert(idx, owner_idennode)
        owner_var = 'contractOwner'

    # add a require(xxx == msg.sender)
    fatherNode = defectNode.father
    idx = fatherNode.children.index(defectNode)
    # msg.sender
    memaccNode = TreeNode('MemberAccess')
    memaccNode.attributes = {}
    memaccNode.attributes['member_name'] = 'sender'
    idenNode = TreeNode('Identifier')
    idenNode.attributes = {}
    idenNode.attributes['value'] = 'msg'
    memaccNode.children.append(idenNode)

    reqexpNode = TreeNode('ExpressionStatement')
    requireNode = TreeNode('FunctionCall')
    nameNode = TreeNode('Identifier')
    nameNode.attributes = {}
    nameNode.attributes['value'] = 'require'
    binaryOpNode = TreeNode('BinaryOperation')
    binaryOpNode.beginPoint = -1
    binaryOpNode.attributes = {}
    binaryOpNode.attributes['operator'] = '=='
    userdefNode = TreeNode('Identifier')
    userdefNode.attributes = {}
    userdefNode.attributes['value'] = owner_var
    binaryOpNode.children.append(userdefNode)
    binaryOpNode.children.append(memaccNode)
    requireNode.children.append(nameNode)
    requireNode.children.append(binaryOpNode)
    reqexpNode.children.append(requireNode)

    fatherNode.children.insert(idx, reqexpNode)
Beispiel #3
0
def generateExpStatement():
    node = TreeNode('ExpressionStatement')
    node.beginPoint = -1
    return node