Exemplo n.º 1
0
def ConvertMovzbl(x):
    if IsMemory(x[1]) and Is32Register(x[2]):
        addr = CalculateAddress(x[1])
        dest = x[2]
        tempString = dest + " = zeroext(mem:8[" + addr + "], 24);\n"

        if config.Is64BitArch():
            dest64 = Get64BitRepFromRegister(x[2])[0]
            tempString = tempString + dest64 + " = " + "zeroext(" + dest + ", 32);\n"

    elif Is8Register(x[1]) and Is32Register(x[2]):
        if x[1] in reg8lowRev:
            source = "split(" + reg32[reg8lowRev[x[1]]] + ", 0, 7)"
        elif x[1] in reg8highRev:
            source = "split(" + reg32[reg8highRev[x[1]]] + ", 8, 15)"
        dest = x[2]
        tempString = dest + " = zeroext(" + source + ", 24);\n"

        if config.Is64BitArch():
            dest64 = Get64BitRepFromRegister(x[2])[0]
            tempString = tempString + dest64 + " = zeroext(" + dest + ", 32);\n"

    else:
        sys.exit("Convertmovzbl operand error: %s" % (x))

    return dslparse.dslToAst(tempString)
Exemplo n.º 2
0
def ConvertDecl(x):
    oldDest = "oldDest"
    if Is32Register(x[1]):
        dest = x[1]
        tempString = (oldDest + " = " + dest + ";\n" + dest + " = " + dest +
                      " - 1;\n")

        if config.Is64BitArch():
            dest64 = Get64BitRepFromRegister(x[1])[0]
            tempString = tempString + dest64 + " = " + "zeroext(" + dest + ", 32);\n"

    elif IsMemory(x[1]):
        dest = "tempDest"
        addr = CalculateAddress(x[1])
        tempString = (LoadFromMemoryString(addr, 4, dest) + oldDest + " = " +
                      dest + ";\n" + dest + " = " + dest + " - 1;\n" +
                      StoreToMemoryString(addr, 4, dest))

    else:
        sys.exit("Convertdecl operand error: %s" % (x))

    # Add flags
    tempString = tempString + (
        "zf:1 = " + dest + " == 0 ? 1:1 : 0:1;\n" + "cf:1 = 1 > " + oldDest +
        " ? 1:1 : 0:1;\n" + "of_part1:1 = 0 != split(" + oldDest +
        ", 31, 31) ? 1:1 : 0:1;\n" + "of_part2:1 = split(" + oldDest +
        ", 31, 31) == split(" + dest + ", 31, 31) ? 1:1 : 0:1;\n" +
        "of:1 = of_part1:1 & of_part2:1;\n" + "sf:1 = split(" + dest +
        ", 31, 31);\n")
    return dslparse.dslToAst(tempString)
Exemplo n.º 3
0
def ConvertMovd(x):
    tempString = ""
    # Movd xmm, 32-bit reg
    if Is128Register(x[1]) and Is32Register(x[2]):
        source = [x[1] + "_0", x[1] + "_1", x[1] + "_2", x[1] + "_3"]
        dest = x[2]
        dest64 = Get64BitRepFromRegister(x[2])[0]
        tempString = (dest + " = " + source[0] + ";\n" + dest64 + " = " +
                      "zeroext(" + dest + ", 32);\n")

    # Movd xmm, 32-bit memory
    elif Is128Register(x[1]) and IsMemory(x[2]):
        source = [x[1] + "_0", x[1] + "_1", x[1] + "_2", x[1] + "_3"]
        addr = CalculateAddress(x[2])
        tempString = StoreToMemoryString(addr, 4, source[0])

    # Movd 32-bit reg, xmm
    elif Is32Register(x[1]) and Is128Register(x[2]):
        source = x[1]
        dest = [x[2] + "_0", x[2] + "_1", x[2] + "_2", x[2] + "_3"]
        tempString = (dest[0] + " = " + source + ";\n" + dest[1] + " = 0;\n" +
                      dest[2] + " = 0;\n" + dest[3] + " = 0;\n")

    # Movd 32-bit mem, xmm
    elif IsMemory(x[1]) and Is128Register(x[2]):
        addr = CalculateAddress(x[1])
        dest = [x[2] + "_0", x[2] + "_1", x[2] + "_2", x[2] + "_3"]
        tempString = (LoadFromMemoryString(addr, 4, dest[0]) + dest[1] +
                      " = 0;\n" + dest[2] + " = 0;\n" + dest[3] + " = 0;\n")
    else:
        sys.exit("ConvertMovd operand error: %s" % (x))

    return dslparse.dslToAst(tempString)
Exemplo n.º 4
0
def ConvertBswapl(x):
    dest = x[1]
    dest64 = Get64BitRepFromRegister(x[1])[0]
    tempString = (dest + " = merge(split(" + dest + ", 0, 7), merge(split(" +
                  dest + ", 8, 15), merge(split(" + dest +
                  ", 16, 23), split(" + dest + ", 24, 31))));\n" + dest64 +
                  " = zeroext(" + dest + ", 32);\n")
    return dslparse.dslToAst(tempString)
Exemplo n.º 5
0
def ConvertPopq(x):
    if Is64Register(x[1]):
        addr = "rsp:64"
        dest = x[1] + ":64"
        tempString = (LoadFromMemoryString(addr, 8, dest) + addr + " = " +
                      addr + " + 8:64" + ";\n")
    else:
        sys.exit("Convertpopq operand error: %s" % (x))

    return dslparse.dslToAst(tempString)
Exemplo n.º 6
0
def ConvertPushq(x):
    if Is64Register(x[1]):
        dest = x[1] + ":64"
        addr = "rsp:64"
        tempString = (addr + " = " + addr + " - 8:64" + ";\n" +
                      StoreToMemoryString(addr, 8, dest))
    else:
        sys.exit("Convertpushq operand error: %s" % (x))

    return dslparse.dslToAst(tempString)
Exemplo n.º 7
0
def ConvertLeaq(x):
    if IsMemory(x[1]) and Is64Register(x[2]):
        dest = x[2] + ":64"
        addr = CalculateAddress(x[1])
        dest32 = Get32BitRepFromRegister(x[2])[0]

        tempString = (dest + " = " + addr + ";\n" + dest32 + " = " + "split(" +
                      dest + ", 0, 31);\n")
        return dslparse.dslToAst(tempString)
    else:
        sys.exit("Convertleaq operand error: %s" % (x))
Exemplo n.º 8
0
def ConvertAndl(x):
    source, dest, oldDest, addr, tempString = SetupOperand32Bit(x)
    tempString = tempString + (
        dest + " = " + dest + " & " + source + ";\n" +
        SaveToOperand32Bit(x, source, dest, oldDest, addr))

    # Add flags
    tempString = tempString + ("zf:1 = " + dest + " == 0 ? 1:1 : 0:1;\n" +
                               "cf:1 = 0:1;\n" + "of:1 = 0:1;\n" +
                               "sf:1 = split(" + dest + ", 31, 31);\n")
    return dslparse.dslToAst(tempString)
Exemplo n.º 9
0
def ConvertXorq(x):
    source, dest, oldDest, addr, tempString = SetupOperand64Bit(x)
    tempString = tempString + (
        dest + " = " + dest + " ^ " + source + ";\n" +
        SaveToOperand64Bit(x, source, dest, oldDest, addr))

    # Add flags
    tempString = tempString + ("zf:1 = " + dest + " == 0:64 ? 1:1 : 0:1;\n" +
                               "cf:1 = 0:1;\n" + "of:1 = 0:1;\n" +
                               "sf:1 = split(" + dest + ", 63, 63);\n")
    return dslparse.dslToAst(tempString)
Exemplo n.º 10
0
def ConvertCmpb(x):
    temp = "tempCmpb:8"
    source, dest, oldDest, addr, tempString = SetupOperand8Bit(x)
    tempString = tempString + temp + " = " + dest + " - " + source + ";\n"

    # Add flags
    tempString = tempString + (
        "zf:1 = " + temp + " == 0:8 ? 1:1 : 0:1;\n" + "of_part1:1 = split(" +
        source + ", 7, 7) != split(" + dest + ", 7, 7) ? 1:1 : 0:1;\n" +
        "of_part2:1 = split(" + dest + ", 7, 7) == split(" + temp +
        ", 7, 7) ? 1:1 : 0:1;\n" + "of:1 = of_part1:1 & of_part2:1;\n" +
        "cf:1 = " + source + " > " + dest + " ? 1:1 : 0:1;\n" +
        "sf:1 = split(" + temp + ", 7, 7);\n")
    return dslparse.dslToAst(tempString)
Exemplo n.º 11
0
def ConvertSubl(x):
    source, dest, oldDest, addr, tempString = SetupOperand32Bit(x)
    tempString = tempString + (
        oldDest + " = " + dest + ";\n" + dest + " = " + dest + " - " + source +
        ";\n" + SaveToOperand32Bit(x, source, dest, oldDest, addr))

    # Add flags
    tempString = tempString + (
        "zf:1 = " + dest + " == 0 ? 1:1 : 0:1;\n" + "of_part1:1 = split(" +
        oldDest + ", 31, 31) != split(" + source + ", 31, 31) ? 1:1 : 0:1;\n" +
        "of_part2:1 = split(" + source + ", 31, 31) == split(" + dest +
        ", 31, 31) ? 1:1 : 0:1;\n" + "of:1 = of_part1:1 & of_part2:1;\n" +
        "cf:1 = " + source + " > " + oldDest + " ? 1:1 : 0:1;\n" +
        "sf:1 = split(" + dest + ", 31, 31);\n")
    return dslparse.dslToAst(tempString)
Exemplo n.º 12
0
def ConvertShll(x):
    source, dest, oldDest, addr, tempString = SetupOperandForShifts32Bit(x)
    tempString = tempString + (
        oldDest + " = " + dest + ";\n" + dest + " = " + dest + " << " + source
        + ";\n" + SaveToOperandForShifts32Bit(x, source, dest, oldDest, addr))

    # Add flags
    tempString = tempString + "cf:1 = split(" + dest + ", 31, 31);\n"
    if int(x[1], 0) == 1:
        tempString = tempString + "of:1 = split(" + dest + ", 31, 31) ^ cf:1;\n"
    else:
        tempString = tempString + "of:1 = builtin_undef;\n"
    tempString = tempString + ("zf:1 = " + dest + " == 0 ? 1:1 : 0:1;\n" +
                               "sf:1 = split(" + dest + ", 31, 31);\n")
    return dslparse.dslToAst(tempString)
Exemplo n.º 13
0
def ConvertSbbq(x):
    source, dest, oldDest, addr, tempString = SetupOperand64Bit(x)
    tempString = tempString + (
        oldDest + " = " + dest + ";\n" + dest + " = " + dest + " - " + source +
        " - zeroext(cf:1, 63);\n" +
        SaveToOperand64Bit(x, source, dest, oldDest, addr))

    # Add flags
    tempString = tempString + (
        "zf:1 = " + dest + " == 0:64 ? 1:1 : 0:1;\n" + "of_part1:1 = split(" +
        oldDest + ", 63, 63) != split(" + source + ", 63, 63) ? 1:1 : 0:1;\n" +
        "of_part2:1 = split(" + source + ", 63, 63) == split(" + dest +
        ", 63, 63) ? 1:1 : 0:1;\n" + "of:1 = of_part1:1 & of_part2:1;\n" +
        "cf:1 = " + source + " > " + oldDest + " ? 1:1 : 0:1;\n" +
        "sf:1 = split(" + dest + ", 63, 63);\n")

    return dslparse.dslToAst(tempString)
Exemplo n.º 14
0
def ConvertMovq(x):
    tempString = ""
    # 64-bit reg to 64-bit reg
    if (isImmediate(x[1]) or Is64Register(x[1])) and Is64Register(x[2]):
        source = x[1] + ":64"
        dest = x[2] + ":64"
        dest32 = Get32BitRepFromRegister(x[2])[0]
        tempString = (dest + " = " + source + ";\n" + dest32 + " = " +
                      "split(" + dest + ", 0, 31);\n")

    # 64-bit reg to 64-bit mem
    elif (isImmediate(x[1]) or Is64Register(x[1])) and IsMemory(x[2]):
        source = x[1] + ":64"
        addr = CalculateAddress(x[2])
        tempString = StoreToMemoryString(addr, 8, source)

    # 64-bit mem to 64-bit reg
    elif IsMemory(x[1]) and Is64Register(x[2]):
        addr = CalculateAddress(x[1])
        dest = x[2] + ":64"
        dest32 = Get32BitRepFromRegister(x[2])[0]
        tempString = (LoadFromMemoryString(addr, 8, dest) + dest32 +
                      " = split(" + dest + ", 0, 31);\n")

    # 128-bit reg to 64-bit reg
    elif Is128Register(x[1]) and Is64Register(x[2]):
        source = [x[1] + "_0", x[1] + "_1", x[1] + "_2", x[1] + "_3"]
        dest = x[2] + ":64"
        dest32 = Get32BitRepFromRegister(x[2])[0]
        tempString = (dest + " = merge(" + source[1] + ", " + source[0] +
                      ");\n" + dest32 + " = split(" + dest + ", 0, 31);\n")

    # 64-bit reg to 128-bit reg
    elif Is64Register(x[1]) and Is128Register(x[2]):
        source = x[1] + ":64"
        dest = [x[2] + "_0", x[2] + "_1", x[2] + "_2", x[2] + "_3"]
        tempString = (dest[0] + " = split(" + source + ", 0, 31);\n" +
                      dest[1] + " = split(" + source + ", 32, 63);\n" +
                      dest[2] + " = 0;\n" + dest[3] + " = 0;\n")
    else:
        sys.exit("ConvertMovd operand error: %s" % (x))

    return dslparse.dslToAst(tempString)
Exemplo n.º 15
0
def ParseSpecStringToDsl(specString, progOrig):
    # Parse dsl program string
    specAst = dslparse.dslToAst(specString)
    # Separate out function definitions and other parts.
    functions, specAst = ProcessFunctions(specAst)
    # Inline the function calls with function definition.
    replaceFunctions(functions, specAst)
    # Unroll Loops
    specAst = UnrollLoops(specAst)

    # Once replaced, see if it's a "memory" store or load.
    for sa in specAst:
        if isinstance(sa.lhs, di.ArrayCall) and sa.comparator == "=":
            sa.comparator = "<-"

    # Set programOrigin.
    for sa in specAst:
        sa.SetProgramOrigin(progOrig, False)

    return specAst
Exemplo n.º 16
0
#        p1Ast : AST of p1
#        p2Ast : AST of p2
#        postAst : AST of the postcondition
#
####################################################################################################
# Read from file
ansiCode.PrintOnThisLineBold("Reading Files")
preString = readFile(args.pre)
p1String = readFile(args.p1)
p2String = readFile(args.p2)
postString = readFile(args.post)

# Turn everything into dslinstruction
ansiCode.PrintOnThisLineBold("ParsingFiles: ")
ansiCode.Print("pre condition")
preAst = dslparse.dslToAst(preString)



ansiCode.PrintFromLeft("p1", 13)
p1Ast = ParseProgramToDsl(p1String, config.p1lang, "P1")
ansiCode.PrintFromLeft("p2", 2)
p2Ast = ParseProgramToDsl(p2String, config.p2lang, "P2")
ansiCode.PrintFromLeft("post condition", 2)
postAst = dslparse.dslToAst(postString)

# Constant propagate some numbers
ansiCode.PrintFromLeft("Constant propagating", 14)
ConstantPropagate(preAst)
ConstantPropagate(p1Ast)
ConstantPropagate(p2Ast)
Exemplo n.º 17
0
def ConvertCmovccl(x, cc):
    source, dest, oldDest, addr, tempString = SetupOperand32Bit(x)
    tempString = tempString + (
        dest + " = " + cc + " ? " + source + " : " + dest + ";\n" +
        SaveToOperand32Bit(x, source, dest, oldDest, addr))
    return dslparse.dslToAst(tempString)