From 1805a9edec384bd9976e7f14dcfaf668a58ab37e Mon Sep 17 00:00:00 2001 From: "BITDEFENDER\\vlutas" Date: Thu, 14 Jul 2022 13:42:37 +0300 Subject: [PATCH] Fixed flag setting for ADC, SBB, SAR and IMUL instructions. --- bdshemu/bdshemu.c | 47 +++++++++++++++++++++------------- bdshemu_test/bdshemu_test.zip | Bin 74758 -> 78024 bytes bindings/pybddisasm/setup.py | 2 +- inc/version.h | 2 +- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/bdshemu/bdshemu.c b/bdshemu/bdshemu.c index c69d00e..2f30283 100644 --- a/bdshemu/bdshemu.c +++ b/bdshemu/bdshemu.c @@ -422,8 +422,9 @@ ShemuSetFlags( } else if (FM_SAR == FlagsMode) { - // CF is the last bit shifted out of the destination. - if (ND_GET_BIT(Src2 - 1, Src1)) + // CF is the last bit shifted out of the destination. In case of SAR, if the shift ammount exceeds the operand + // size, CF will be 1 if the result is -1, or 0 if the result is 0. + if (ND_GET_BIT(Src2 - 1, Src1) || ((Src2 >= (ND_UINT64)Size * 8) && Dst != 0)) { Context->Registers.RegFlags |= NDR_RFLAG_CF; } @@ -437,11 +438,11 @@ ShemuSetFlags( else { // Set CF. - if ((FM_SUB == FlagsMode) && (Src1 < Src2)) + if ((FM_SUB == FlagsMode) && ((Src1 < Src2) || (Src1 == Src2 && Dst != 0))) { Context->Registers.RegFlags |= NDR_RFLAG_CF; } - else if ((FM_ADD == FlagsMode) && (Dst < Src1)) + else if ((FM_ADD == FlagsMode) && ((Dst < Src1) || (Dst == Src1 && Src2 != 0))) { Context->Registers.RegFlags |= NDR_RFLAG_CF; } @@ -1107,7 +1108,7 @@ ShemuSetMemValue( // -// IntWinShcSetOperandValue +// ShemuGetOperandValue // static SHEMU_STATUS ShemuGetOperandValue( @@ -1293,7 +1294,7 @@ done_gla:; // -// IntWinShcSetOperandValue +// ShemuSetOperandValue // static SHEMU_STATUS ShemuSetOperandValue( @@ -1945,14 +1946,14 @@ ShemuEmulate( GET_OP(Context, 0, &dst); GET_OP(Context, 1, &src); + res.Size = src.Size; + res.Value.Qwords[0] = dst.Value.Qwords[0] + src.Value.Qwords[0]; + if (ND_INS_ADC == Context->Instruction.Instruction) { - src.Value.Qwords[0] += GET_FLAG(Context, NDR_RFLAG_CF); + res.Value.Qwords[0] += GET_FLAG(Context, NDR_RFLAG_CF); } - res.Size = src.Size; - res.Value.Qwords[0] = dst.Value.Qwords[0] + src.Value.Qwords[0]; - SET_FLAGS(Context, res, dst, src, FM_ADD); SET_OP(Context, 0, &res); @@ -1964,14 +1965,14 @@ ShemuEmulate( GET_OP(Context, 0, &dst); GET_OP(Context, 1, &src); + res.Size = src.Size; + res.Value.Qwords[0] = dst.Value.Qwords[0] - src.Value.Qwords[0]; + if (ND_INS_SBB == Context->Instruction.Instruction) { - src.Value.Qwords[0] += GET_FLAG(Context, NDR_RFLAG_CF); + res.Value.Qwords[0] -= GET_FLAG(Context, NDR_RFLAG_CF); } - res.Size = src.Size; - res.Value.Qwords[0] = dst.Value.Qwords[0] - src.Value.Qwords[0]; - SET_FLAGS(Context, res, dst, src, FM_SUB); if (ND_INS_CMP != Context->Instruction.Instruction) @@ -2521,20 +2522,29 @@ check_far_branch: case ND_INS_MUL: case ND_INS_IMUL: - if (Context->Instruction.ExpOperandsCount < 3) + if (Context->Instruction.ExpOperandsCount == 1) { - // MUL or IMUL with a single explicit operand or IMUL with 2 explicit operands. + // MUL or IMUL with a single explicit operand. GET_OP(Context, 0, &dst); GET_OP(Context, 1, &src); + res.Size = dst.Size * 2; + } + else if (Context->Instruction.ExpOperandsCount == 2) + { + // IMUL with 2 explicit operands. + GET_OP(Context, 0, &dst); + GET_OP(Context, 1, &src); + res.Size = dst.Size; } else { // IMUL with 3 operands. The first operand is the write-only destination. - GET_OP(Context, 0, &res); GET_OP(Context, 1, &dst); GET_OP(Context, 2, &src); + res.Size = dst.Size; } + if (dst.Size == 1) { if (ND_INS_MUL == Context->Instruction.Instruction) @@ -2610,6 +2620,7 @@ check_far_branch: SET_OP(Context, 0, &res); } + // Set the flags. if (ND_INS_MUL == Context->Instruction.Instruction) { ND_UINT8 cfof = 0; @@ -2640,7 +2651,7 @@ check_far_branch: // the sign extended operand - size - truncated product, otherwise the CF and OF flags are cleared. ND_UINT8 cfof = 0, sign = 0; - sign = ND_MSB(res.Size, res.Value.Qwords[0]); + sign = ND_MSB(dst.Size, res.Value.Qwords[0]); switch (dst.Size) { diff --git a/bdshemu_test/bdshemu_test.zip b/bdshemu_test/bdshemu_test.zip index 31cdd817ca3ee35735faf77341fe015399dc2dba..4eeeaa0b9953e27ba2568ab5cee4ee0aafd270c1 100644 GIT binary patch delta 3257 zcmZ{m2{_bw7{^D>97&m)am5Oad)(v59kv`5iQMD9HH@4ya^GhpM{>mo6Dkrj7{??j z6xL113Pm&|b~L+7ZTp|+Kfh<5XMUgO`+cAHit3{2nxervt)r-E*a6f420)DWk};{A zu`>Vw09aE204K?(oh`xzF5&Bd@U@ncv9`5`OG3qM5gv5g3YiRUY*F*Wj_ZM-F(@aP zNsbz18?=#^b)u~3@mIstdGuLjk*T_&p!8LZOYBs`H_UHtud^%*>s?qwYVC2VlI#P=e6y! z;lzdJi>vzxc`{-HuRHRQ>{d!3U44liMX`9@0paKF+aVnNZm0T!?=E;#1z zCvj+^FL<3pL|{h>QXSVL|LqyEuU&s`CWpRZF>~m&U5KQp?S%QUG6K^@Yp)z57<;xL zz~f8y7D5l|;KVn0hkm-sRQPJppF$T4jf{y!ZOeCFb^;v!kd9bEY#X|~MlF888ORN) zDRwnWIJoNg!i^dElYE4c1oP0qs+V8@)41pykHDH%TxLntO#S3r5YbEk1OFQM4c!mA zDbi?rW97P@IcHMM66%QgK+9>9>&@02mOa^pYo&iOerc5uHN!pGskb=J*3=^FrMh@X z-2OH-b@I)~CFaVL(t2hKU=(pf`>vabwJMk9*|8awhEz+UIsR^FNv5JuEqt>$a%H{f zJ*c}&3SQVA^|6B`Gu0Zi=F-#QlEH_#MLMnub}%$^H>dXujP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9`Wu3P{9Y-!2&826x5KtZq z%RQ4hZTpI|$%^|0Y?Xz{O3#pA_5+G3!<-`7x$7zC^Ab`v?W?SFJ_)xliu$+-v8)=& zEf2>TX0(UwN1m7UH6gQio+I|zg=BW3cBLSW9El5D001WdwI7ayaB)gkcup0Q6f-N;o4 zIltnRWizo-fv!Xu{?W$7=GbRUb9A70fuQcWTBx#8i_d^@`#ZM^CY+e;v+7gK_B5yu z`w+$Fb|;vqWZ$3WiOe`tdQU1<=5D;9`X$4m(QB&q7Nlfan)+s0&qdN>6U}-?ko9?Q zrHY5Ao-w#usk##L{II=zh=H&m#o?q;-ND^H(p8t#K6XWGAz-e?$6A-inHm_LMybW% z1aa=1*a9x(NUVX?YS*{UO;d%zp1Cs?(k-GON$B#UoWZ+t6A~JAgoj~TCk=t);VFYm z{chVrgsG}9q*tz?egaM*5YxDeEIaxsQ&gnZVLeeCMRkasoJaq+Dv}cnKn36>e><%I zoDTmkjz;JAAwMi|mcRl7J}OAA(V%gd^mplHRd4Vzny||pAy*C{~`SAh?Z{e?=|EPNfH2{ Jd%C;#^Di2!Tn+#L delta 35 mcmX@{kfrSe%ZBu?oA>xYC!~*~eB@4Fz diff --git a/bindings/pybddisasm/setup.py b/bindings/pybddisasm/setup.py index 934ef73..61cbd4e 100644 --- a/bindings/pybddisasm/setup.py +++ b/bindings/pybddisasm/setup.py @@ -12,7 +12,7 @@ from setuptools import find_packages, setup, Command, Extension, Distribution from codecs import open VERSION = (0, 1, 3) -LIBRARY_VERSION = (1, 34, 10) +LIBRARY_VERSION = (1, 34, 15) LIBRARY_INSTRUX_SIZE = 856 packages = ['pybddisasm'] diff --git a/inc/version.h b/inc/version.h index f91a7a2..b4d6980 100644 --- a/inc/version.h +++ b/inc/version.h @@ -7,6 +7,6 @@ #define DISASM_VERSION_MAJOR 1 #define DISASM_VERSION_MINOR 34 -#define DISASM_VERSION_REVISION 10 +#define DISASM_VERSION_REVISION 15 #endif // DISASM_VER_H