From 12d167a21ab80797f71db062f035c58af008ffe4 Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Mon, 19 Feb 2018 12:47:36 +0000
Subject: [PATCH] added getAddress and hasRepPrefix support to capstone

Former-commit-id: 939c01951a64c43cb0c46ef19ee2361a9269568c
---
 include/interfaces/irdb/STARSInstruction.h | 8 ++++----
 src/base/SMPFunction.cpp                   | 2 +-
 src/base/SMPInstr.cpp                      | 6 +++---
 tests/scripts/SMP-analyze32.sh             | 6 +++++-
 tests/scripts/SMP-analyze64.sh             | 7 +++++--
 5 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/include/interfaces/irdb/STARSInstruction.h b/include/interfaces/irdb/STARSInstruction.h
index 3394c643..f91f5c47 100644
--- a/include/interfaces/irdb/STARSInstruction.h
+++ b/include/interfaces/irdb/STARSInstruction.h
@@ -95,14 +95,14 @@ class STARS_IRDB_Instruction_t : public STARS_Instruction_t
 
 		// Query methods
 		virtual bool HasRepeatIfEqualPrefix(void) const 
-		{ return disasm.hasRepnePrefix(); /*disasm.Prefix.RepnePrefix!=NotUsedPrefix;*/ }
+		{ return disasm.hasRelevantRepnePrefix(); /*disasm.Prefix.RepnePrefix!=NotUsedPrefix;*/ }
 		virtual bool HasRepeatIfNotEqualPrefix(void) const 
-		{ return disasm.hasRepPrefix(); /* disasm.Prefix.RepPrefix!=NotUsedPrefix; */ }
+		{ return disasm.hasRelevantRepPrefix(); /* disasm.Prefix.RepPrefix!=NotUsedPrefix; */ }
 		virtual bool HasAnyRepeatPrefix(void) const 
 		{ return HasRepeatIfEqualPrefix() || HasRepeatIfNotEqualPrefix(); }
 
 		virtual bool HasOperandSizePrefix(void) const 
-		{ return disasm.hasOperandSizePrefix(); /* Prefix.OperandSize!=NotUsedPrefix; */}
+		{ return disasm.hasRelevantOperandSizePrefix(); /* Prefix.OperandSize!=NotUsedPrefix; */}
 
 		virtual bool HasREXWPrefix(void) const 
 		{ return disasm.hasRexWPrefix(); /*disasm.Prefix.REX.W_!=0; */}
@@ -131,7 +131,7 @@ class STARS_IRDB_Instruction_t : public STARS_Instruction_t
 				return true; 
 		
 			// no size prefix and opcode defaults to 64-bit 
-			if (!this->HasOperandSizePrefix() && this->OpcodeDefaultsTo64BitOperands() )  
+			if ( this->OpcodeDefaultsTo64BitOperands() && !this->HasOperandSizePrefix() )  
 				return true;
 		
 			// size prefix or non-64 bit operand
diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index 54488ec6..797f1ca6 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -4796,7 +4796,7 @@ void SMPFunction::AnalyzeFunc(void) {
 			InstAddr = CurrInst->GetAddr();
 			pair<STARS_ea_t, SMPBasicBlock *> MapItem(InstAddr, CurrBlock);
 			this->InstBlockMap.insert(MapItem);
-			if (CurrInst->MDHasAnyRepeatPrefix() && CurrInst->MDIsPossibleStringLoopingOpcode()) {
+			if ( CurrInst->MDIsPossibleStringLoopingOpcode() && CurrInst->MDHasAnyRepeatPrefix() ) {
 				CurrBlock->SetHasLoopingStringOpcode();
 			}
 		}
diff --git a/src/base/SMPInstr.cpp b/src/base/SMPInstr.cpp
index a7d1a7c7..8a93b4c0 100644
--- a/src/base/SMPInstr.cpp
+++ b/src/base/SMPInstr.cpp
@@ -19174,7 +19174,6 @@ bool SMPInstr::BuildMoveRTL(SMPoperator GuardOp) {
 	bool SourceFound = false;
 	bool MemSrc = this->HasSourceMemoryOperand();
 	bool MemDest = this->HasDestMemoryOperand();
-	bool HasRepeatPrefix = this->STARSInstPtr->HasAnyRepeatPrefix();
 	bool AltersRSI = false; // for string operations
 	bool AltersRDI = false; // for string operations
 	unsigned short opcode = this->GetIDAOpcode();
@@ -19363,6 +19362,7 @@ bool SMPInstr::BuildMoveRTL(SMPoperator GuardOp) {
 		STARSOpndTypePtr FirstOpnd = this->GetOperand(0);
 		uint16_t ByteWidth = FirstOpnd->GetByteWidth();
 		STARSOpndTypePtr IncrementOp = this->MakeImmediateOpnd((STARS_uval_t) ByteWidth);
+		bool HasRepeatPrefix = this->STARSInstPtr->HasAnyRepeatPrefix();
 		if (HasRepeatPrefix) { // Must be MOVS or STOS or INS or OUTS
 			// The repeat causes USE and DEF of ECX as a counter
 			STARSOpndTypePtr CountOp = this->STARSInstPtr->MakeRegOpnd(STARS_x86_R_cx);
@@ -19522,7 +19522,6 @@ bool SMPInstr::BuildCompareStringRTL(void) {
 	std::size_t OpNum;
 	bool Src1Found = false;
 	bool Src2Found = false;
-	bool HasRepeatPrefix = this->STARSInstPtr->HasAnyRepeatPrefix();
 
 	STARSOpndTypePtr FlagsOp = this->STARSInstPtr->MakeRegOpnd(X86_FLAGS_REG);
 
@@ -19578,6 +19577,7 @@ bool SMPInstr::BuildCompareStringRTL(void) {
 	else {
 		this->RTL.push_back(TempRT);
 		// Now, create the repeat prefix effects
+		bool HasRepeatPrefix = this->STARSInstPtr->HasAnyRepeatPrefix();
 		if (HasRepeatPrefix) {
 			// The repeat causes USE and DEF of ECX as a counter
 			SMPRegTransfer *CounterRT = new SMPRegTransfer;
@@ -19871,7 +19871,6 @@ bool SMPInstr::BuildFlagsDestBinaryRTL(SMPoperator BinaryOp) {
 	bool Source1Found = false;
 	bool Source2Found = false;
 	bool NoOperandsRequired = ((STARS_NN_scas == opcode) || (STARS_NN_cmps == opcode));
-	bool HasRepeatPrefix = this->STARSInstPtr->HasAnyRepeatPrefix();
 
 	SMPRegTransfer *TempRT = new SMPRegTransfer;
 	TempRT->SetParentInst(this);
@@ -19950,6 +19949,7 @@ bool SMPInstr::BuildFlagsDestBinaryRTL(SMPoperator BinaryOp) {
 	else {
 		this->RTL.push_back(TempRT);
 		// Now, create the repeat prefix effects
+		bool HasRepeatPrefix = this->STARSInstPtr->HasAnyRepeatPrefix();
 		if (HasRepeatPrefix) { // Must be CMPS or SCAS
 			// The repeat causes USE and DEF of ECX as a counter
 			SMPRegTransfer *CounterRT = new SMPRegTransfer;
diff --git a/tests/scripts/SMP-analyze32.sh b/tests/scripts/SMP-analyze32.sh
index 39fbfa63..6d24db74 100755
--- a/tests/scripts/SMP-analyze32.sh
+++ b/tests/scripts/SMP-analyze32.sh
@@ -34,7 +34,11 @@ old_stty=`stty -g`
 # jdh - added -B for batch mode, avoids any prompts.
 # jdh - note:  can't use -B, as the plugin doesn't run!
 # time ${IDAROOT}/idal  -A -Telf -OSMPStaticAnalyzer:run $1 2>&1 >/dev/null
-TVHEADLESS=1 time ${IDAROOT}/idal  -A -SSMP.idc $1 > $1.idaoutput
+if [[ -f ${IDAROOT}/idal ]] ; then 
+	TVHEADLESS=1 time ${IDAROOT}/idal  -A -SSMP.idc $1 > $1.idaoutput
+else
+	TVHEADLESS=1 time ${IDAROOT}/idat  -A -SSMP.idc $1 > $1.idaoutput
+fi
 # valgrind -v --leak-check=full --show-reachable=yes --track-origins=yes --log-file=$1.valgrind.log ${IDAROOT}/idal  -A -SSMP.idc $1 
 
 
diff --git a/tests/scripts/SMP-analyze64.sh b/tests/scripts/SMP-analyze64.sh
index cc241b9a..cdb3a1bb 100755
--- a/tests/scripts/SMP-analyze64.sh
+++ b/tests/scripts/SMP-analyze64.sh
@@ -34,8 +34,11 @@ old_stty=`stty -g`
 # jdh - added -B for batch mode, avoids any prompts.
 # jdh - note:  can't use -B, as the plugin doesn't run!
 # TVHEADLESS=1 time ${IDAROOT}/idal64  -A -Telf -OSMPStaticAnalyzer:run $1 2>&1 >/dev/null
-echo "TVHEADLESS=1 time ${IDAROOT}/idal64  -A -SSMP.idc $1 > $1.idaoutput"
-TVHEADLESS=1 time ${IDAROOT}/idal64  -A -SSMP.idc $1 > $1.idaoutput
+if [[ -f ${IDAROOT}/idal64 ]]; then
+	(set -x;  TVHEADLESS=1 time ${IDAROOT}/idal64  -A -SSMP.idc $1 > $1.idaoutput )
+else
+	(set -x ; TVHEADLESS=1 time ${IDAROOT}/idat64  -A -SSMP.idc $1 > $1.idaoutput )
+fi
 # TVHEADLESS=1 valgrind -v --leak-check=full --show-reachable=yes --track-origins=yes --log-file=$1.valgrind.log ${IDAROOT}/idal64  -A -SSMP.idc $1 
 
 
-- 
GitLab