From 3169e757900064f5d30cbab652c59861fdc46aa8 Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Wed, 22 Feb 2017 14:07:17 +0000
Subject: [PATCH] added INDIRCALL to ICFS to fix coloring-cfi for
 spec2k.gccbench.  put ICFS support in dump_map with DUMP_ICFS env var.

Former-commit-id: 54a31c190e424627232372b753a47e88e5126389
---
 libIRDB/test/fill_in_indtargs.cpp             | 16 ++++-
 .../include/MEDS_IBTAnnotation.hpp            | 11 +++-
 tools/dump_map/SConscript                     |  2 +
 tools/dump_map/dump_map.cpp                   | 27 +++++++-
 tools/selective_cfi/tests/test_spec.sh        | 62 +++++++++----------
 5 files changed, 84 insertions(+), 34 deletions(-)

diff --git a/libIRDB/test/fill_in_indtargs.cpp b/libIRDB/test/fill_in_indtargs.cpp
index e1cca3734..4448238d3 100644
--- a/libIRDB/test/fill_in_indtargs.cpp
+++ b/libIRDB/test/fill_in_indtargs.cpp
@@ -1690,9 +1690,11 @@ void handle_ibt_annot(FileIR_t* firp,Instruction_t* insn, MEDS_IBTAnnotation* p_
 /* meds annotations
  *                typedef enum { SWITCH, RET, DATA, UNREACHABLE, ADDRESSED, UNKNOWN } ibt_reason_code_t;
  */
+	cout<<"at handl_ibt with addr="<<hex<<insn->GetAddress()->GetVirtualOffset()<<" code="<<p_ibt_annotation->GetReason()<<endl;
 	switch(p_ibt_annotation->GetReason())
 	{
 		case MEDS_IBTAnnotation::SWITCH:
+		case MEDS_IBTAnnotation::INDIRCALL:
 		{
 			possible_target((EXEIO::virtual_offset_t)p_ibt_annotation->getVirtualOffset().getOffset(),
 				0,ibt_provenance_t::ibtp_stars_switch);
@@ -1701,11 +1703,13 @@ void handle_ibt_annot(FileIR_t* firp,Instruction_t* insn, MEDS_IBTAnnotation* p_
 			Instruction_t* ibt=lookupInstruction(firp, p_ibt_annotation->getVirtualOffset().getOffset());
 			if(fromib && ibt)
 			{
+				if(getenv("IB_VERBOSE")!=NULL)
+					cout<<hex<<"Adding call/switch icfs: "<<fromib->GetAddress()->GetVirtualOffset()<<"->"<<ibt->GetAddress()->GetVirtualOffset()<<endl;
 				jmptables[fromib].insert(ibt);
 			}
 			else
 			{
-				cout<<"Warning:  cannot find source or dest for switch icfs."<<endl;
+				cout<<"Warning:  cannot find source or dest for call/switch icfs."<<endl;
 			}
 			break;
 		}
@@ -1722,6 +1726,8 @@ void handle_ibt_annot(FileIR_t* firp,Instruction_t* insn, MEDS_IBTAnnotation* p_
 			Instruction_t* ibt=lookupInstruction(firp, toaddr);
 			if(fromib && ibt)
 			{
+				if(getenv("IB_VERBOSE")!=NULL)
+					cout<<hex<<"Adding ret icfs: "<<fromib->GetAddress()->GetVirtualOffset()<<"->"<<ibt->GetAddress()->GetVirtualOffset()<<endl;
 				jmptables[fromib].insert(ibt);
 			}
 			else
@@ -1734,24 +1740,32 @@ void handle_ibt_annot(FileIR_t* firp,Instruction_t* insn, MEDS_IBTAnnotation* p_
 		{
 			possible_target((EXEIO::virtual_offset_t)p_ibt_annotation->getVirtualOffset().getOffset(),
 				0,ibt_provenance_t::ibtp_stars_data);
+			if(getenv("IB_VERBOSE")!=NULL)
+				cout<<hex<<"detected stars data ibt at"<<p_ibt_annotation->getVirtualOffset().getOffset()<<endl;
 			break;
 		}
 		case MEDS_IBTAnnotation::UNREACHABLE:
 		{
 			possible_target((EXEIO::virtual_offset_t)p_ibt_annotation->getVirtualOffset().getOffset(),
 				0,ibt_provenance_t::ibtp_stars_unreachable);
+			if(getenv("IB_VERBOSE")!=NULL)
+				cout<<hex<<"detected stars unreachable ibt at"<<p_ibt_annotation->getVirtualOffset().getOffset()<<endl;
 			break;
 		}
 		case MEDS_IBTAnnotation::ADDRESSED:
 		{
 			possible_target((EXEIO::virtual_offset_t)p_ibt_annotation->getVirtualOffset().getOffset(),
 				0,ibt_provenance_t::ibtp_stars_addressed);
+			if(getenv("IB_VERBOSE")!=NULL)
+				cout<<hex<<"detected stars addresssed ibt at"<<p_ibt_annotation->getVirtualOffset().getOffset()<<endl;
 			break;
 		}
 		case MEDS_IBTAnnotation::UNKNOWN:
 		{
 			possible_target((EXEIO::virtual_offset_t)p_ibt_annotation->getVirtualOffset().getOffset(),
 				0,ibt_provenance_t::ibtp_stars_unknown);
+			if(getenv("IB_VERBOSE")!=NULL)
+				cout<<hex<<"detected stars unknown ibt at"<<p_ibt_annotation->getVirtualOffset().getOffset()<<endl;
 			break;
 		}
 		default:
diff --git a/libMEDSannotation/include/MEDS_IBTAnnotation.hpp b/libMEDSannotation/include/MEDS_IBTAnnotation.hpp
index 0a2913057..7f8877925 100644
--- a/libMEDSannotation/include/MEDS_IBTAnnotation.hpp
+++ b/libMEDSannotation/include/MEDS_IBTAnnotation.hpp
@@ -37,8 +37,9 @@ using namespace MEDS_Annotation;
 class MEDS_IBTAnnotation : public MEDS_AnnotationBase
 {
 	public:
-		typedef enum { SWITCH, RET, DATA, UNREACHABLE, ADDRESSED, UNKNOWN } ibt_reason_code_t;
+		typedef enum { SWITCH, RET, DATA, UNREACHABLE, ADDRESSED, INDIRCALL, UNKNOWN } ibt_reason_code_t;
 
+		MEDS_IBTAnnotation()=delete;
 		MEDS_IBTAnnotation( const string& p_rawLine) 
 			: xref_addr(0), reason(UNKNOWN)
 		{ 
@@ -78,17 +79,20 @@ class MEDS_IBTAnnotation : public MEDS_AnnotationBase
 			if(string("FROMIB") == from_type)
 			{
 				stream >> hex >> xref_addr;
+				cout<<"fromib: '"<<p_rawLine<<"'"<<endl;
 			}
 			else if(string("FROMDATA") == from_type)
 			{
 				stream >> hex >> xref_addr;
 				reason=DATA;
+				cout<<"fromdata: '"<<p_rawLine<<"'"<<endl;
 				return;
 			}
 			else if(string("FROMUNKNOWN") == from_type)
 			{
 				// no other fields for from UNKNOWN
 				xref_addr=0;
+				cout<<"fromunknown: '"<<p_rawLine<<"'"<<endl;
 			}
 
 			string reason_code;
@@ -104,8 +108,13 @@ class MEDS_IBTAnnotation : public MEDS_AnnotationBase
 			{ reason=UNREACHABLE; }
 			else if(string("CODEADDRESSTAKEN") == reason_code)
 			{ reason=ADDRESSED; }
+			else if(string("INDIRCALL") == reason_code)
+			{ reason=INDIRCALL; }
 			else
 			{ reason=UNKNOWN; }
+
+			if(reason==UNKNOWN) 
+				cout<<"unknown reason code: '"<<reason_code<<"'"<<endl;
 				
 		}
 
diff --git a/tools/dump_map/SConscript b/tools/dump_map/SConscript
index e256d4c84..f0ff16c1e 100644
--- a/tools/dump_map/SConscript
+++ b/tools/dump_map/SConscript
@@ -17,6 +17,8 @@ cpppath='''
 
 files=Glob( Dir('.').srcnode().abspath+"/*.cpp")
 
+myenv.Append(CPPFLAGS="-std=c++11")
+
 
 pgm="dump_map.exe"
 
diff --git a/tools/dump_map/dump_map.cpp b/tools/dump_map/dump_map.cpp
index 880fa10eb..d7b1ec5e4 100644
--- a/tools/dump_map/dump_map.cpp
+++ b/tools/dump_map/dump_map.cpp
@@ -23,6 +23,7 @@
 #include <libIRDB-core.hpp>
 #include <libgen.h>
 #include <iomanip>
+#include <algorithm>
 
 
 using namespace std;
@@ -33,6 +34,21 @@ void usage(char* name)
 	cerr<<"Usage: "<<name<<" <variant_id>\n"; 
 }
 
+
+void dump_icfs(Instruction_t* insn)
+{
+	if(insn->GetIBTargets()==NULL)	
+		return;	
+	
+	cout<<"\tComplete: "<<boolalpha<<insn->GetIBTargets()->IsComplete()<<endl;
+	cout<<"\tModComplete: "<<boolalpha<<insn->GetIBTargets()->IsModuleComplete()<<endl;
+	cout<<"\tTargets: "<<boolalpha<<insn->GetIBTargets()->IsModuleComplete()<<endl;
+	for_each(insn->GetIBTargets()->begin(), insn->GetIBTargets()->end(), [&](const Instruction_t* targ)
+	{
+		cout<<"\t"<<targ->GetBaseID()<<":"<<targ->getDisassembly()<<endl;
+	});
+}
+
 int main(int argc, char **argv)
 {
         if(argc != 2)
@@ -41,6 +57,12 @@ int main(int argc, char **argv)
                 exit(1);
         }
 
+	auto dump_icfs_flag=(unsigned long long)-1; 
+	auto dump_icfs_str=getenv("DUMP_ICFS");
+	if(dump_icfs_str)
+		dump_icfs_flag=strtoull(dump_icfs_str,NULL,0);
+		
+
         string programName(argv[0]);
         int variantID = atoi(argv[1]);
 
@@ -53,7 +75,7 @@ int main(int argc, char **argv)
         pidp=new VariantID_t(variantID);
         assert(pidp->IsRegistered()==true);
 
-	cout<<"ret_shadow_stack.exe started\n";
+
 
         bool one_success = false;
         for(set<File_t*>::iterator it=pidp->GetFiles().begin();
@@ -96,6 +118,9 @@ int main(int argc, char **argv)
 					cout<<setw(9)<<"NoFunc";
 					
 				cout<<" "<<insn->getDisassembly()<<endl;
+	
+				if(dump_icfs_flag == insn->GetBaseID())
+					dump_icfs(insn);
 			}
 
 
diff --git a/tools/selective_cfi/tests/test_spec.sh b/tools/selective_cfi/tests/test_spec.sh
index 3f69ac56b..401d0d3d9 100755
--- a/tools/selective_cfi/tests/test_spec.sh
+++ b/tools/selective_cfi/tests/test_spec.sh
@@ -1,37 +1,37 @@
 #!/bin/bash
 
-benchmarks="bzip2"
+benchmarks="gcc"
 # 447.dealII // broken build
-benchmarks="
-	400.perlbench
-	401.bzip2
-	403.gcc
-	410.bwaves
-	416.gamess
-	429.mcf
-	433.milc
-	434.zeusmp
-	435.gromacs
-	436.cactusADM
-	437.leslie3d
-	444.namd
-	445.gobmk
-	450.soplex
-	453.povray
-	454.calculix
-	456.hmmer
-	458.sjeng
-	459.GemsFDTD
-	462.libquantum
-	464.h264ref
-	465.tonto
-	470.lbm
-	471.omnetpp
-	473.astar
-	481.wrf
-	482.sphinx3
-	483.xalancbmk
-	"
+#benchmarks="
+#	400.perlbench
+#	401.bzip2
+#	403.gcc
+#	410.bwaves
+#	416.gamess
+#	429.mcf
+#	433.milc
+#	434.zeusmp
+#	435.gromacs
+#	436.cactusADM
+#	437.leslie3d
+#	444.namd
+#	445.gobmk
+#	450.soplex
+#	453.povray
+#	454.calculix
+#	456.hmmer
+#	458.sjeng
+#	459.GemsFDTD
+#	462.libquantum
+#	464.h264ref
+#	465.tonto
+#	470.lbm
+#	471.omnetpp
+#	473.astar
+#	481.wrf
+#	482.sphinx3
+#	483.xalancbmk
+#	"
 number=1
 
 setup()
-- 
GitLab