Example #1
0
    def __insertToDict(self,
                       tarCmdNum,
                       fromStr,
                       refCmdNum,
                       toStr,
                       dict2insert=None):
        if dict2insert == None:
            dict2insert = self.rewriteDict

        for typeDict in [
                x for x in dict2insert.keys()
                if dict2insert[x]['isArgument'] == True
        ]:
            res = myutils.iff(dict2insert[typeDict]['testerFunction'], fromStr,
                              toStr)
            if res < 0:
                return

            if res == 1:
                self.insertToDictWithType(tarCmdNum, fromStr, refCmdNum, toStr,
                                          typeDict, dict2insert)
                return

            if res == 0:
                continue
def getMatchedCmdsWithGrade(refCode,tarCode):
    res = myutils.iff(lambda x: x=="",refCode,tarCode)
    if res==1:
        return {'matchedCmds':[],'deletedCmds':[],'insertedCmds':[],'gradesDict':{'ratio':100,'contain':100},'refCode':refCode,'tarCode':tarCode}
    if res==0:
        refCmds = refCode.split(";")
        tarCmds = tarCode.split(";")
        (matchedCmds,deletedCmds,insertedCmds,grade) = __doGetMatchedCmds(refCmds,tarCmds)
        return {'matchedCmds':matchedCmds,'deletedCmds':deletedCmds,'insertedCmds':insertedCmds,'gradesDict':__getGradesDict(grade, refCmds, tarCmds),'refCode':refCode,'tarCode':tarCode}
    else:
        return {'matchedCmds':[],'deletedCmds':[],'insertedCmds':[],'gradesDict':{'ratio':0,'contain':0},'refCode':refCode,'tarCode':tarCode}
Example #3
0
def compareDisAsmCode(refCode, tarCode):
    res = myutils.iff(lambda x: x == "", refCode, tarCode)
    if res == 1:
        return {'ratio': 100, 'contain': 100}

    if res == 0:
        refCmds = refCode.split(";")
        tarCmds = tarCode.split(";")
        grade = __doGetMatchedCmds(refCmds, tarCmds, False)
        return __getGradesDict(grade, refCmds, tarCmds)
    else:
        return {'ratio': 0, 'contain': 0}
def compareDisAsmCode(refCode,tarCode):
    res = myutils.iff(lambda x: x=="",refCode,tarCode)
    if res==1:
        return {'ratio':100,'contain':100}
    
    if res==0:
        refCmds = refCode.split(";")
        tarCmds = tarCode.split(";")
        grade = __doGetMatchedCmds(refCmds,tarCmds,False)
        return __getGradesDict(grade, refCmds, tarCmds)
    else:
        return {'ratio':0,'contain':0}
Example #5
0
    def __insertToDict(self,tarCmdNum,fromStr,refCmdNum,toStr,dict2insert=None):
        if dict2insert == None:
            dict2insert = self.rewriteDict

        for typeDict in [x for x in dict2insert.keys() if dict2insert[x]['isArgument']==True ]:
            res = myutils.iff(dict2insert[typeDict]['testerFunction'],fromStr,toStr)
            if res < 0:
                return

            if res == 1:
                self.insertToDictWithType(tarCmdNum,fromStr,refCmdNum,toStr,typeDict,dict2insert)
                return

            if res == 0:
                continue
Example #6
0
def __gradeCmdMatch(refCmd,tarCmd):

    if refCmd == "" or tarCmd == "":
        return 0

    totalGrade = 0

    refFields = refCmd.split(" ")
    tarFields = tarCmd.split(" ")

    refParams = refFields[-1].split(",")
    tarParams = tarFields[-1].split(",")


    #sanity - no cmd with 3 params
    # TODO - remove
    res = myutils.iff(lambda x: len(x) < 3,refParams,tarParams)
    if res != 1:
        print "REF=" + str(refCmd)
        print "TAR=" + str(tarCmd)
        raise hell
        

    # if we dont have the same ammount of params this means we dont have the same opcode.. 
    # we just give this 0!
    if len(refParams) == len(tarParams):
        #refParams = [refFields[-1]]
        #tarParams = [tarFields[-1]]

        for i in range(0,len(refParams)):
            if refParams[i] == tarParams[i]:
                totalGrade += 1

        if refFields[0] == tarFields[0]:
            totalGrade += 1

        if refFields[1:-1] == tarFields[1:-1] and refFields[1:-1] != []:
            totalGrade += 1

    return totalGrade
Example #7
0
def getMatchedCmdsWithGrade(refCode, tarCode):
    res = myutils.iff(lambda x: x == "", refCode, tarCode)
    if res == 1:
        return {
            'matchedCmds': [],
            'deletedCmds': [],
            'insertedCmds': [],
            'gradesDict': {
                'ratio': 100,
                'contain': 100
            },
            'refCode': refCode,
            'tarCode': tarCode
        }
    if res == 0:
        refCmds = refCode.split(";")
        tarCmds = tarCode.split(";")
        (matchedCmds, deletedCmds, insertedCmds,
         grade) = __doGetMatchedCmds(refCmds, tarCmds)
        return {
            'matchedCmds': matchedCmds,
            'deletedCmds': deletedCmds,
            'insertedCmds': insertedCmds,
            'gradesDict': __getGradesDict(grade, refCmds, tarCmds),
            'refCode': refCode,
            'tarCode': tarCode
        }
    else:
        return {
            'matchedCmds': [],
            'deletedCmds': [],
            'insertedCmds': [],
            'gradesDict': {
                'ratio': 0,
                'contain': 0
            },
            'refCode': refCode,
            'tarCode': tarCode
        }
def __gradeCmdMatch(refCmd, tarCmd):

    if refCmd == "" or tarCmd == "":
        return 0

    totalGrade = 0

    refFields = refCmd.split(" ")
    tarFields = tarCmd.split(" ")

    refParams = refFields[-1].split(",")
    tarParams = tarFields[-1].split(",")

    #sanity - no cmd with 3 params
    # TODO - remove
    res = myutils.iff(lambda x: len(x) < 3, refParams, tarParams)
    if res != 1:
        print "REF=" + str(refCmd)
        print "TAR=" + str(tarCmd)
        raise hell

    # if we dont have the same ammount of params this means we dont have the same opcode..
    # we just give this 0!
    if len(refParams) == len(tarParams):
        #refParams = [refFields[-1]]
        #tarParams = [tarFields[-1]]

        for i in range(0, len(refParams)):
            if refParams[i] == tarParams[i]:
                totalGrade += 1

        if refFields[0] == tarFields[0]:
            totalGrade += 1

        if refFields[1:-1] == tarFields[1:-1] and refFields[1:-1] != []:
            totalGrade += 1

    return totalGrade
Example #9
0
def __doGetMatchedCmds(refCmds, tarCmds, fillArrays=True):

    res = myutils.iff(lambda x: x == [], refCmds, tarCmds)
    if res == 1:
        return ([], [], [], 0)
    elif res < 0:
        return ([], tarCmds, [], 0) if res == -1 else ([], [], refCmds, 0)

    # we have tarCmds rows, and refCmds columns
    # where ar[0][1] is row 0 col 1
    array = []
    for i in range(0, len(tarCmds)):
        array.append([0] * len(refCmds))

    #fill last row
    for i in range(0, len(refCmds)):
        grade = __gradeCmdMatch(refCmds[i], tarCmds[-1])
        array[-1][i] = {'my': grade, 'best': grade}

    # fill last column
    for i in range(0, len(tarCmds)):
        grade = __gradeCmdMatch(refCmds[-1], tarCmds[i])
        array[i][-1] = {'my': grade, 'best': grade}

    # work on the rest of them
    for i in range(len(tarCmds) - 2, -1, -1):
        for j in range(len(refCmds) - 2, -1, -1):
            grade = __gradeCmdMatch(refCmds[j],
                                    tarCmds[i]) + array[i + 1][j + 1]['best']
            myMax = max(grade, array[i][j + 1]['best'],
                        array[i + 1][j]['best'])
            array[i][j] = {'my': grade, 'best': myMax}
    """
    print "ARRAY:"
    
    for row in array:
        for cell in row:
            print cell,
        print ";"

    print "DONE"
    """

    if fillArrays == False:
        return array[0][0]['best']

    #now we must walk on the array and find the used sequence
    curI = 0
    curJ = 0
    matchedCmds = []
    insertedCmds = []
    deletedCmds = []

    Iok = curI < len(tarCmds)
    Jok = curJ < len(refCmds)

    while Iok and Jok:
        if Iok and Jok and (array[curI][curJ]['best']
                            == array[curI][curJ]['my']):
            # the current one was chosen , +1 cuz here we are 0 based but i want to be 1 based

            if array[curI][curJ]['best'] == 0:
                if tarCmds[curI] == "":
                    deletedCmds.append(tarCmds[curI])
                elif refCmds[curJ] == "":
                    insertedCmds.append(refCmds[curJ])
            else:
                matchedCmds.append({
                    'ref':
                    refCmds[curJ],
                    'tar':
                    tarCmds[curI],
                    'tarCmdNum':
                    curI + 1,
                    'refCmdNum':
                    curJ + 1,
                    'operationMatch':
                    array[curI][curJ]['best'] > 0
                })

            curI += 1
            Iok = curI < len(tarCmds)
            curJ += 1
            Jok = curJ < len(refCmds)

        elif Jok and array[curI][curJ]['best'] == array[curI][curJ +
                                                              1]['best']:
            insertedCmds.append(refCmds[curJ])
            curJ += 1
            Jok = curJ < len(refCmds) - 1

        elif Iok:
            deletedCmds.append(tarCmds[curI])
            curI += 1
            Iok = curI < len(tarCmds) - 1

    while Iok:
        deletedCmds.append(tarCmds[curI])
        curI += 1
        Iok = curI < len(tarCmds) - 1

    while Jok:
        insertedCmds.append(refCmds[curJ])
        curJ += 1
        Jok = curJ < len(refCmds) - 1

    return (matchedCmds, deletedCmds, insertedCmds, array[0][0]['best'])
Example #10
0
    def createRewrite(self):

        tarBase = 0
        refBase = 0

        for nodeInfo in self.nodeGradesInfos:
            for cmd in nodeInfo['matchedCmds']:

                tarCmdNum = tarBase + cmd['tarCmdNum']
                refCmdNum = refBase + cmd['refCmdNum']

                #ref = self.refCode
                #tar = self.tarCode

                res = myutils.iff(isCall, cmd['ref'], cmd['tar'])
                if res < 0:
                    if self.printCmdMatchErrors:
                        print "bad command match" + "(refcmd=" + cmd[
                            'ref'] + ",tarcmd=" + cmd['tar'] + ")"
                    continue
                elif res == 1:
                    self.insertToDictWithType(tarCmdNum,
                                              cmd['tar'][len("call "):],
                                              refCmdNum,
                                              cmd['ref'][len("call "):],
                                              self.FUNCNAME, self.rewriteDict)
                    continue

                res = myutils.iff(lambda x: x.startswith("<"), cmd['ref'],
                                  cmd['tar'])
                if res < 0:
                    if self.printCmdMatchErrors:
                        print "bad command match"
                    continue
                elif res == 1:
                    continue  # we do nothing with this now...
                else:

                    tmpDict = self.getEmptyDict()

                    cmdPartsRef = cmd['ref'].split(" ")
                    cmdPartsTar = cmd['tar'].split(" ")

                    if cmdPartsRef[0] != cmdPartsTar[0]:
                        if self.printCmdMatchErrors:
                            print "bad command match" + "(ref=" + cmd[
                                'ref'] + ",tar=" + cmd['tar'] + ")"
                        continue

                    if len(cmdPartsRef) != len(cmdPartsTar):
                        if self.printCmdMatchErrors:
                            print "bad command match in cmd parts" + "(ref=" + cmd[
                                'ref'] + ",tar=" + cmd['tar'] + ")"
                        continue

                    for i in range(1, len(cmdPartsRef)):

                        res = myutils.iff(lambda x: x != "", cmdPartsRef[i],
                                          cmdPartsTar[i])
                        if res < 0:
                            if self.printCmdMatchErrors:
                                print "bad command match in cmd parts one empty one not" + "(ref=" + cmd[
                                    'ref'] + ",tar=" + cmd['tar'] + ")"
                            continue
                            #no args, nothing to learn here.

                        if res == 0:
                            if self.printCmdMatchErrors:
                                print "two empty commands" + "(ref=" + cmd[
                                    'ref'] + ",tar=" + cmd['tar'] + ")"
                            continue

                        #else they are both not empty

                        argsRef = cmdPartsRef[i].split(",")
                        argsTar = cmdPartsTar[i].split(",")

                        if len(argsRef) != len(argsTar):
                            if self.printCmdMatchErrors:
                                print "bad command match in args" + "(ref=" + cmd[
                                    'ref'] + ",tar=" + cmd['tar'] + ")"
                            continue

                        for j in range(0, len(argsRef)):
                            res = myutils.iff(
                                lambda x: x.startswith("[") and x.endswith(
                                    "]"), argsRef[j], argsTar[j])
                            if res < 0:
                                if self.printCmdMatchErrors:
                                    print "bad command match in args one mem one not" + "(ref=" + cmd[
                                        'ref'] + ",tar=" + cmd['tar'] + ")"
                                continue

                            elif res == 1:
                                #both mem operations
                                singleArgRef = argsRef[j][1:-1]
                                singleArgTar = argsTar[j][1:-1]

                                paramsRef = singleArgRef.split("+")
                                paramsTar = singleArgTar.split("+")

                                if len(paramsRef) == len(paramsTar):
                                    for k in range(0, len(paramsRef)):
                                        self.__insertToDict(
                                            tarCmdNum, paramsTar[k], refCmdNum,
                                            paramsRef[k], tmpDict)

                                else:
                                    if self.printCmdMatchErrors:
                                        print "bad command match - number of params is not the same" + "(ref=" + cmd[
                                            'ref'] + ",tar=" + cmd['tar'] + ")"
                                    continue

                            else:
                                #both without mem
                                self.__insertToDict(tarCmdNum, argsTar[j],
                                                    refCmdNum, argsRef[j],
                                                    tmpDict)

                    # we passed the parameted\argument compare and the cmds have the same structure. we commit the changes.
                    self.commitChanges(tmpDict)

            tarBase += nodeInfo['tarCode'].count(";")
            refBase += nodeInfo['refCode'].count(";")
def __doGetMatchedCmds(refCmds,tarCmds,fillArrays=True):

    res = myutils.iff(lambda x: x == [],refCmds, tarCmds) 
    if res == 1:
        return ([],[],[],0)
    elif res < 0:
        return ([],tarCmds,[],0) if res == -1 else ([],[],refCmds,0)

    # we have tarCmds rows, and refCmds columns
    # where ar[0][1] is row 0 col 1
    array = []
    for i in range(0,len(tarCmds)):
        array.append([0] * len(refCmds))

    #fill last row
    for i in range(0,len(refCmds)):
        grade = __gradeCmdMatch(refCmds[i],tarCmds[-1])
        array[-1][i] = {'my':grade,'best':grade}

    # fill last column
    for i in range(0,len(tarCmds)):
        grade = __gradeCmdMatch(refCmds[-1],tarCmds[i])
        array[i][-1] = {'my':grade,'best':grade}

    # work on the rest of them
    for i in range(len(tarCmds)-2,-1,-1):
        for j in range(len(refCmds)-2,-1,-1):
            grade = __gradeCmdMatch(refCmds[j],tarCmds[i]) + array[i+1][j+1]['best']
            myMax = max(grade,array[i][j+1]['best'],array[i+1][j]['best'])
            array[i][j] = {'my':grade,'best':myMax}

    """
    print "ARRAY:"
    
    for row in array:
        for cell in row:
            print cell,
        print ";"

    print "DONE"
    """

    if fillArrays == False:
        return array[0][0]['best']

    #now we must walk on the array and find the used sequence
    curI = 0
    curJ = 0
    matchedCmds = []
    insertedCmds = []
    deletedCmds = []

    Iok = curI < len(tarCmds)
    Jok = curJ < len(refCmds)

    while Iok and Jok:
        if Iok and Jok and (array[curI][curJ]['best'] == array[curI][curJ]['my']):
            # the current one was chosen , +1 cuz here we are 0 based but i want to be 1 based
            
            if array[curI][curJ]['best'] == 0:
                if tarCmds[curI] == "":
                    deletedCmds.append(tarCmds[curI])
                elif refCmds[curJ] == "":
                    insertedCmds.append(refCmds[curJ])
            else:
                matchedCmds.append({'ref':refCmds[curJ],'tar':tarCmds[curI],'tarCmdNum':curI+1,'refCmdNum':curJ+1,'operationMatch':array[curI][curJ]['best'] > 0})
            
            curI+=1
            Iok = curI < len(tarCmds)
            curJ+=1
            Jok = curJ < len(refCmds)

        elif Jok and array[curI][curJ]['best'] == array[curI][curJ+1]['best']:
            insertedCmds.append(refCmds[curJ])
            curJ+=1
            Jok = curJ < len(refCmds)-1

        elif Iok:
            deletedCmds.append(tarCmds[curI])
            curI+=1
            Iok = curI < len(tarCmds)-1

    while Iok:
        deletedCmds.append(tarCmds[curI])
        curI+=1
        Iok = curI < len(tarCmds)-1

    while Jok:
        insertedCmds.append(refCmds[curJ])
        curJ+=1
        Jok = curJ < len(refCmds)-1


    return (matchedCmds,deletedCmds,insertedCmds,array[0][0]['best'])
Example #12
0
 def createRewrite(self):
     
     tarBase = 0
     refBase = 0
     
     for nodeInfo in self.nodeGradesInfos:
         for cmd in nodeInfo['matchedCmds']:
             
             tarCmdNum = tarBase + cmd['tarCmdNum']
             refCmdNum = refBase + cmd['refCmdNum']
             
             #ref = self.refCode
             #tar = self.tarCode
             
             res = myutils.iff(isCall,cmd['ref'],cmd['tar'])
             if res < 0:
                 if self.printCmdMatchErrors:
                     print "bad command match" + "(refcmd=" + cmd['ref'] + ",tarcmd=" + cmd['tar'] + ")"
                 continue
             elif res == 1:
                 self.insertToDictWithType(tarCmdNum,cmd['tar'][len("call "):],refCmdNum,cmd['ref'][len("call "):],self.FUNCNAME,self.rewriteDict)
                 continue
 
             res = myutils.iff(lambda x: x.startswith("<"),cmd['ref'],cmd['tar'])
             if res < 0:
                 if self.printCmdMatchErrors:
                     print "bad command match"
                 continue
             elif res == 1:
                 continue # we do nothing with this now...
             else:
 
                 tmpDict = self.getEmptyDict()
 
                 cmdPartsRef = cmd['ref'].split(" ")
                 cmdPartsTar = cmd['tar'].split(" ")
 
                 if cmdPartsRef[0] != cmdPartsTar[0]:
                     if self.printCmdMatchErrors:
                         print "bad command match" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
                     continue
 
                 if len(cmdPartsRef) != len(cmdPartsTar):
                     if self.printCmdMatchErrors:
                         print "bad command match in cmd parts" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
                     continue
 
                 for i in range(1,len(cmdPartsRef)):
 
                     res = myutils.iff(lambda x: x != "",cmdPartsRef[i],cmdPartsTar[i])
                     if res < 0:
                         if self.printCmdMatchErrors:
                             print "bad command match in cmd parts one empty one not" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
                         continue
                         #no args, nothing to learn here.
 
                     if res==0:
                         if self.printCmdMatchErrors:
                             print "two empty commands" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
                         continue
 
                     #else they are both not empty
 
                     argsRef = cmdPartsRef[i].split(",")
                     argsTar = cmdPartsTar[i].split(",")
 
                     if len(argsRef) != len(argsTar):
                         if self.printCmdMatchErrors:
                             print "bad command match in args" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
                         continue
 
                     for j in range(0,len(argsRef)):
                         res = myutils.iff(lambda x: x.startswith("[") and x.endswith("]"),argsRef[j],argsTar[j])
                         if res < 0 :
                             if self.printCmdMatchErrors:
                                 print "bad command match in args one mem one not" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
                             continue
 
                         elif res == 1:
                             #both mem operations
                             singleArgRef = argsRef[j][1:-1]
                             singleArgTar = argsTar[j][1:-1]
 
                             paramsRef = singleArgRef.split("+")
                             paramsTar = singleArgTar.split("+")
 
                             if len(paramsRef) == len(paramsTar):
                                 for k in range(0,len(paramsRef)):
                                     self.__insertToDict(tarCmdNum,paramsTar[k],refCmdNum,paramsRef[k],tmpDict)
 
                             else:
                                 if self.printCmdMatchErrors:
                                     print "bad command match - number of params is not the same" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
                                 continue
 
 
                         else:
                             #both without mem
                             self.__insertToDict(tarCmdNum,argsTar[j],refCmdNum,argsRef[j],tmpDict)
 
                 # we passed the parameted\argument compare and the cmds have the same structure. we commit the changes.
                 self.commitChanges(tmpDict)
                 
         tarBase += nodeInfo['tarCode'].count(";")
         refBase += nodeInfo['refCode'].count(";")