diff --git a/lifter/OperandUtils.cpp b/lifter/OperandUtils.cpp index fe50b55..5dfa8cc 100644 --- a/lifter/OperandUtils.cpp +++ b/lifter/OperandUtils.cpp @@ -1147,8 +1147,7 @@ Value* lifterClass::setFlag(const Flag flag, Value* newValue) { LLVMContext& context = builder.getContext(); newValue = createTruncFolder(newValue, Type::getInt1Ty(context)); printvalue2((int32_t)flag) printvalue(newValue); - if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF || - flag == FLAG_DF) + if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF) return nullptr; FlagList[flag].set(newValue); // Set the new value directly @@ -1158,8 +1157,7 @@ Value* lifterClass::setFlag(const Flag flag, Value* newValue) { void lifterClass::setFlag(const Flag flag, std::function calculation) { // If the flag is one of the reserved ones, do not modify - if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF || - flag == FLAG_DF) + if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF) return; // lazy calculation diff --git a/lifter/Semantics.cpp b/lifter/Semantics.cpp index 623b93c..ff8aed9 100644 --- a/lifter/Semantics.cpp +++ b/lifter/Semantics.cpp @@ -259,76 +259,60 @@ void lifterClass::branchHelper(Value* condition, const string& instname, // cout << "pathInfo:" << pathInfo << " dest: " << destination << // "\n"; } - -void lifterClass::lift_movsb() { +void lifterClass::lift_movs_X() { + LLVMContext& context = builder.getContext(); + // Get the size based on the operand + int size = operands[1].size; // DEST := SRC; - // [esi] = [edi] + // Parameterize size and direction // sign = DF (-1/+1) - // incdecv = size*sign (sb means size is 1) - // esi += incdecv - // edi += incdecv - // - - // Value* SRCptrvalue = - // GetOperandValue(operands[0],operands[0].size); - - Value* DSTptrvalue = GetOperandValue(operands[1], operands[1].size); + // incdecv = size*sign + Value* DSTptrvalue = GetOperandValue(operands[1], size); SetOperandValue(operands[0], DSTptrvalue); bool isREP = (instruction.attributes & ZYDIS_ATTRIB_HAS_REP) != 0; Value* DF = getFlag(FLAG_DF); - auto one = ConstantInt::get(DF->getType(), 1); - // sign = (x*(x+1)) - 1 - // v = sign * bytesize ; bytesize is 1 - Value* Direction = - createSubFolder(createMulFolder(DF, createAddFolder(DF, one)), one); + // sign = (DF*(DF+1)) - 1 + // v = sign * byteSize + + auto byteSizeValue = size; auto SRCop = operands[2 + isREP]; auto DSTop = operands[3 + isREP]; - + printvalue(DF); + Value* Direction = createSelectFolder(DF, ConstantInt::get(Type::getIntNTy(context, SRCop.size),1 * byteSizeValue), + ConstantInt::get(Type::getIntNTy(context, SRCop.size),-1 * byteSizeValue)); + printvalue(Direction); + Value* SRCvalue = GetOperandValue(SRCop, SRCop.size); Value* DSTvalue = GetOperandValue(DSTop, DSTop.size); if (isREP) { - // if REP, operands[1] will be e/rax - // in that case, repeat and decrement e/rax until its 0 - - // we can create a loop but I dont know how that would effect our - // optimizations Value* count = GetOperandValue(operands[2], operands[2].size); if (auto countci = dyn_cast(count)) { Value* UpdateSRCvalue = SRCvalue; Value* UpdateDSTvalue = DSTvalue; uint64_t looptime = countci->getZExtValue(); - printvalue2(looptime); for (int i = looptime; i > 0; i--) { - // TODO: fix this loop - - // Value* SRCptrvalue = GetOperandValue( - // operands[0], - // operands[0].size); - DSTptrvalue = GetOperandValue(operands[1], operands[1].size); - + DSTptrvalue = GetOperandValue(operands[1], size); SetOperandValue(operands[0], DSTptrvalue); UpdateSRCvalue = createAddFolder(UpdateSRCvalue, Direction); UpdateDSTvalue = createAddFolder(UpdateDSTvalue, Direction); - printvalue(UpdateDSTvalue) printvalue(UpdateSRCvalue); SetOperandValue(SRCop, UpdateSRCvalue); SetOperandValue(DSTop, UpdateDSTvalue); - // bad cheat + if (i > 1) debugging::increaseInstCounter(); } SetOperandValue(operands[2], ConstantInt::get(count->getType(), 0)); - return; } else { UNREACHABLE("fix rep"); @@ -341,6 +325,7 @@ void lifterClass::lift_movsb() { SetOperandValue(SRCop, UpdateSRCvalue); SetOperandValue(DSTop, UpdateDSTvalue); } + void lifterClass::lift_movaps() { auto dest = operands[0]; auto src = operands[1]; @@ -3669,11 +3654,13 @@ void lifterClass::liftInstructionSemantics() { lift_mov(); break; } - case ZYDIS_MNEMONIC_MOVSB: { - lift_movsb(); + case ZYDIS_MNEMONIC_MOVSB: + case ZYDIS_MNEMONIC_MOVSW: + case ZYDIS_MNEMONIC_MOVSD: + case ZYDIS_MNEMONIC_MOVSQ: { + lift_movs_X(); break; } - // cmov case ZYDIS_MNEMONIC_CMOVZ: { lift_cmovz(); diff --git a/lifter/lifterClass.h b/lifter/lifterClass.h index 761f257..0d5bfbf 100644 --- a/lifter/lifterClass.h +++ b/lifter/lifterClass.h @@ -408,7 +408,7 @@ class lifterClass { // end folders // semantics definition - DEFINE_FUNCTION(movsb); + DEFINE_FUNCTION(movs_X); DEFINE_FUNCTION(movaps); DEFINE_FUNCTION(mov); DEFINE_FUNCTION(cmovbz);