From f0659c611afe3030a4b462eda7bd6a81845d9bdd Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Thu, 25 Jun 2015 19:12:02 +0000
Subject: [PATCH] Partial update: move more code into base class OpenFiles()
 and CloseFiles().

Former-commit-id: 4c6c93ed43d6e0abb3104842c76b1326ab385301
---
 include/interfaces/abstract/STARSProgram.h | 16 ++++--
 src/drivers/idapro/SMPStaticAnalyzer.cpp   | 57 ++++++----------------
 src/interfaces/abstract/STARSProgram.cpp   | 30 ++++++++++++
 src/interfaces/idapro/STARSIDAProgram.cpp  | 18 ++++---
 4 files changed, 68 insertions(+), 53 deletions(-)

diff --git a/include/interfaces/abstract/STARSProgram.h b/include/interfaces/abstract/STARSProgram.h
index 48404aad..2dd1871a 100644
--- a/include/interfaces/abstract/STARSProgram.h
+++ b/include/interfaces/abstract/STARSProgram.h
@@ -5,7 +5,6 @@
 #include <map>
 #include <set>
 #include <string>
-#include <vector>
 
 #include <cstdint>
 #include <cstdio>
@@ -41,7 +40,11 @@ class STARS_Program_t
 		int GetAnnotationCount(std::size_t CategoryIndex) const { return AnnotationCount[CategoryIndex]; };
 		unsigned long GetDataReferentID(void) const { return DataReferentID; }; // Unique ID for data annotations
 		uint16_t GetCurrentFileNum(void) const { return CurrentFileNumber; };
-		std::string GetRootFileName(void) const { return RootFileName; }; // e.g. "foo" when analyzing file "foo.exe"
+		std::string GetRootFileName(void) const { return RootFileName; }; // e.g. "foo.exe" when analyzing file "/usr/foo.exe"
+		std::string GetAnnotFileName(void) const { return AnnotFileName; }; // e.g. "foo.exe.annot"
+		std::string GetInfoAnnotFileName(void) const { return InfoAnnotFileName; }; // e.g. "foo.exe.infoannot"
+		FILE *GetAnnotFile(void) const { return STARS_AnnotFile; };
+		FILE *GetInfoAnnotFile(void) const { return STARS_InfoAnnotFile; };
 		FILE *GetAlarmFile(void) const { return ZST_AlarmFile; };
 		FILE *GetXrefsFile(void) const { return STARS_XrefsFile; };
 		FILE *GetCallReturnFile(void) const { return STARS_CallReturnFile; };
@@ -59,6 +62,7 @@ class STARS_Program_t
 		// Set (mutator) methods
 		virtual void Set32BitBinary(void) = 0; // Set internal state to handle a 32-bit binary
 		virtual void Set64BitBinary(void) = 0; // Set internal state to handle a 64-bit binary
+		void SetRootFileName(std::string NewName) { RootFileName = NewName; AnnotFileName = NewName + ".annot"; InfoAnnotFileName = NewName + ".infoannot"; };
 		void IncrementOptCount(std::size_t OptCategory) { ++OptCount[OptCategory]; }; // increment optimizing annotation count
 		void IncrementAnnotationCount(std::size_t OptCategory) { ++AnnotationCount[OptCategory]; }; // increment total annotation count
 		void IncrementDataReferentID(void) { ++DataReferentID; }; // increment unique data annotations referent ID
@@ -103,7 +107,6 @@ class STARS_Program_t
 		void SetTotalCodeSize(unsigned long long TotalSize) { STARS_TotalCodeSize = TotalSize; };
 		void SetReducedProcessingFlag(bool FlagValue) { STARS_PerformReducedAnalysis = FlagValue; };
 		void IncrementCurrentFileNum(void) { ++CurrentFileNumber; };
-		void SetRootFileName(std::string NewName) { RootFileName = NewName; };
 
 		// Analysis methods
 
@@ -133,6 +136,13 @@ class STARS_Program_t
 		// Filename (not including path) of executable being analyzed.
 		std::string RootFileName;
 
+		// Annotation file names.
+		std::string AnnotFileName, InfoAnnotFileName;
+
+		// Files for annotations.
+		FILE *STARS_AnnotFile;
+		FILE *STARS_InfoAnnotFile;
+
 		// File to print security alert messages to, e.g. foo.exe.alarms.
 		FILE *ZST_AlarmFile;
 
diff --git a/src/drivers/idapro/SMPStaticAnalyzer.cpp b/src/drivers/idapro/SMPStaticAnalyzer.cpp
index 633624cf..77cc9c65 100644
--- a/src/drivers/idapro/SMPStaticAnalyzer.cpp
+++ b/src/drivers/idapro/SMPStaticAnalyzer.cpp
@@ -287,7 +287,7 @@ int IDAP_init(void) {
 	// Ensure correct working environment.
 	if (ph.id != PLFM_386) {
 		error("Processor must be x86.");
- 		return PLUGIN_SKIP;
+		return PLUGIN_SKIP;
 	}
 
 	hook_to_notification_point(HT_IDP, idp_callback, NULL);
@@ -329,7 +329,7 @@ void IDAP_run(int arg) {
 
 	DefOrUse DummyRef;
 	STARSOpndType DummyOperand;
-	size_t RefObjectSize = sizeof(DummyRef), OpndSize = sizeof(DummyOperand);
+	std::size_t RefObjectSize = sizeof(DummyRef), OpndSize = sizeof(DummyOperand);
 	SMP_msg("INFO: Size of DefOrUse: %zu Size of op_t: %zu \n", RefObjectSize, OpndSize);
 	SMP_msg("INFO: Size of STARS_ea_t: %zu Size of uintptr_t: %zu \n", sizeof(STARS_ea_t), sizeof(uintptr_t));
 
@@ -345,6 +345,12 @@ void IDAP_run(int arg) {
 	global_STARS_program->InitData();
 
 	global_STARS_program->DetermineRootFileName();
+
+	// read the Profiler generated information into a new prof_info class 
+	CurrProg = new SMPProgram();
+	ProfilerInformation *prof_info = new ProfilerInformation(global_STARS_program->GetAnnotFileName().c_str(), CurrProg);
+	// NOTE: ProfilerInformation fopen's the AnnotFile, reads it, then closes it. Then we re-open for writing below.
+
 	if (!(global_STARS_program->OpenFiles())) {
 		SMP_msg("FATAL ERROR: At least one file could not be opened.\n");
 		error("FATAL ERROR: At least one file could not be opened.\n");
@@ -353,13 +359,6 @@ void IDAP_run(int arg) {
 		return;
 	}
 
-	// Open the output file.
-	string AnnotFileName(global_STARS_program->GetRootFileName());
-	string FileSuffix(".annot");
-	AnnotFileName += FileSuffix;
-	string InfoAnnotFileName(global_STARS_program->GetRootFileName());
-	string InfoFileSuffix(".infoannot");
-	InfoAnnotFileName += InfoFileSuffix;
 	string AsmFileName(global_STARS_program->GetRootFileName());
 	string AsmFileSuffix(".asm");
 	AsmFileName += AsmFileSuffix;
@@ -391,35 +390,8 @@ void IDAP_run(int arg) {
 	}
 #endif
 
-	CurrProg = new SMPProgram();
 	CurrProg->AnalyzeData(); // Analyze static data in the executable
 
-	// read the Profiler generated information into a new prof_info class 
-	ProfilerInformation *prof_info = new ProfilerInformation(AnnotFileName.c_str(), CurrProg);
-	// NOTE: ProfilerInformation fopen's the AnnotFile, reads it, then closes it. Then we re-open for writing below.
-
-	AnnotFile = SMP_fopen(AnnotFileName.c_str(), "w");
-	if (NULL == AnnotFile) {
-		error("FATAL ERROR: Cannot open output file %s\n", AnnotFileName.c_str());
-		global_STARS_program->CloseFiles();
-		delete prof_info;
-		delete CurrProg;
-		delete global_STARS_program;
-		delete global_stars_interface;
-		return;
-	}
-	InfoAnnotFile = SMP_fopen(InfoAnnotFileName.c_str(), "w");
-	if (NULL == InfoAnnotFile) {
-		error("FATAL ERROR: Cannot open output file %s\n", InfoAnnotFileName.c_str());
-		global_STARS_program->CloseFiles();
-		SMP_fclose(AnnotFile);
-		delete prof_info;
-		delete CurrProg;
-		delete global_STARS_program;
-		delete global_stars_interface;
-		return;
-	}
-
 	// Read the Zephyr Security Toolkit system call security policies, if available.
 	global_STARS_program->ZST_InitPolicies();
 
@@ -449,10 +421,11 @@ void IDAP_run(int arg) {
 		prof_info->GetMemoryAccessInfo()->InferDataGranularity();
 		SMP_msg("Returned from InferDataGranularity\n");
 	}
-	CurrProg->ProfGranularityFinished(AnnotFile, InfoAnnotFile);
-	CurrProg->Analyze(prof_info, AnnotFile, InfoAnnotFile);
+	
+	CurrProg->ProfGranularityFinished(global_STARS_program->GetAnnotFile(), global_STARS_program->GetInfoAnnotFile());
+	CurrProg->Analyze(prof_info, global_STARS_program->GetAnnotFile(), global_STARS_program->GetInfoAnnotFile());
 	if (!global_STARS_program->ShouldSTARSPerformReducedAnalysis()) {
-		CurrProg->EmitAnnotations(AnnotFile, InfoAnnotFile);
+		CurrProg->EmitAnnotations(global_STARS_program->GetAnnotFile(), global_STARS_program->GetInfoAnnotFile());
 	}
 
 	// Process the instructions that are not in functions (generally, an IDA problem, or just no-ops for
@@ -471,7 +444,7 @@ void IDAP_run(int arg) {
 	for (STARS_Segment_t *seg = SMP_get_first_seg(); NULL != seg; seg = SMP_get_next_seg(RecentAddr)) {
 		RecentAddr = seg->get_startEA();
 		if (seg->IsCodeSegment())
-			FindOrphanedCode(seg, AnnotFile, InfoAnnotFile);
+			FindOrphanedCode(seg, global_STARS_program->GetAnnotFile(), global_STARS_program->GetInfoAnnotFile());
 	}
 #endif
 
@@ -481,10 +454,8 @@ void IDAP_run(int arg) {
 			OptType, global_STARS_program->GetOptCount(OptType), global_STARS_program->GetAnnotationCount(OptType));
 	}
 
+	SMP_fprintf(global_STARS_program->GetInfoAnnotFile(), "   8000000      2 SUCCESS ANALYSISCOMPLETED\n");
 	global_STARS_program->CloseFiles();
-	SMP_fclose(AnnotFile);
-	SMP_fprintf(InfoAnnotFile, "   8000000      2 SUCCESS ANALYSISCOMPLETED\n");
-	SMP_fclose(InfoAnnotFile);
 
 	if (!global_STARS_program->ShouldSTARSPerformReducedAnalysis()) {
 #if STARS_GENERATE_ASM_FILE
diff --git a/src/interfaces/abstract/STARSProgram.cpp b/src/interfaces/abstract/STARSProgram.cpp
index a1686632..a74c687d 100644
--- a/src/interfaces/abstract/STARSProgram.cpp
+++ b/src/interfaces/abstract/STARSProgram.cpp
@@ -13,6 +13,9 @@ using namespace std;
 
 // Data initialization
 bool STARS_Program_t::OpenFiles(void) {
+	// Open the output files.
+	assert(0 < this->RootFileName.size());  // SetRootFileName() must be called previously.
+
 	string ZSTAlarmFileName(this->GetRootFileName());
 	string AlarmFileSuffix(".alarms");
 	ZSTAlarmFileName += AlarmFileSuffix;
@@ -52,6 +55,25 @@ bool STARS_Program_t::OpenFiles(void) {
 		return false;
 	}
 
+	this->STARS_AnnotFile = SMP_fopen(this->AnnotFileName.c_str(), "w");
+	if (NULL == this->STARS_AnnotFile) {
+		SMP_msg("FATAL ERROR: Cannot open annotations file %s\n", this->AnnotFileName.c_str());
+		SMP_fclose(this->STARS_XrefsFile);
+		SMP_fclose(this->STARS_CallReturnFile);
+		SMP_fclose(this->ZST_AlarmFile);
+		return false;
+	}
+
+	this->STARS_InfoAnnotFile = SMP_fopen(this->InfoAnnotFileName.c_str(), "w");
+	if (NULL == this->STARS_InfoAnnotFile) {
+		SMP_msg("FATAL ERROR: Cannot open annotations file %s\n", this->InfoAnnotFileName.c_str());
+		SMP_fclose(this->STARS_XrefsFile);
+		SMP_fclose(this->STARS_CallReturnFile);
+		SMP_fclose(this->ZST_AlarmFile);
+		SMP_fclose(this->STARS_AnnotFile);
+		return false;
+	}
+
 #if ZST_EMIT_SPARK_ADA_TRANSLATION
 	this->ZST_SPARKSourceFile = SMP_fopen(SPARKSourceFileName.c_str(), "w");
 	if (NULL == this->ZST_SPARKSourceFile) {
@@ -59,6 +81,8 @@ bool STARS_Program_t::OpenFiles(void) {
 		SMP_fclose(this->STARS_XrefsFile);
 		SMP_fclose(this->STARS_CallReturnFile);
 		SMP_fclose(this->ZST_AlarmFile);
+		SMP_fclose(this->STARS_AnnotFile);
+		SMP_fclose(this->STARS_InfoAnnotFile);
 		return false;
 	}
 	this->ZST_SPARKHeaderFile = SMP_fopen(SPARKHeaderFileName.c_str(), "w");
@@ -67,14 +91,20 @@ bool STARS_Program_t::OpenFiles(void) {
 		SMP_fclose(this->STARS_XrefsFile);
 		SMP_fclose(this->STARS_CallReturnFile);
 		SMP_fclose(this->ZST_AlarmFile);
+		SMP_fclose(this->STARS_AnnotFile);
+		SMP_fclose(this->STARS_InfoAnnotFile);
 		SMP_fclose(this->ZST_SPARKSourceFile);
 		return false;
 	}
 #endif
+
 	return true;
 } // end of STARS_Program_t::OpenFiles()
 
 void STARS_Program_t::CloseFiles(void) {
+	(void) SMP_fclose(this->STARS_AnnotFile);
+	(void) SMP_fclose(this->STARS_InfoAnnotFile);
+
 #if ZST_EMIT_SPARK_ADA_TRANSLATION
 	(void) SMP_fclose(this->ZST_SPARKSourceFile);
 	(void) SMP_fclose(this->ZST_SPARKHeaderFile);
diff --git a/src/interfaces/idapro/STARSIDAProgram.cpp b/src/interfaces/idapro/STARSIDAProgram.cpp
index 77c9b83c..12eca099 100644
--- a/src/interfaces/idapro/STARSIDAProgram.cpp
+++ b/src/interfaces/idapro/STARSIDAProgram.cpp
@@ -9,7 +9,7 @@
 #include <ua.hpp>
 #include <bytes.hpp>
 #include <kernwin.hpp>
-#include <nalt.hpp>
+#include <nalt.hpp>   // for get_root_filename()
 #include <intel.hpp>
 #include <name.hpp>  // for get_true_name()
 #include <struct.hpp>  // for get_member_name()
@@ -33,19 +33,21 @@ using namespace std;
 // Set to zero until we can do more precise analyses of indexed accesses.
 #define SMP_DETECT_INDEXED_ACCESSES 0
 
+// TODO: Move to base class.
 // Set internal state to handle a 32-bit binary
 void STARS_IDA_Program_t::Set32BitBinary(void) {
 	STARS_Program_t::SetBitwidth32();
-	this->STARS_ISA_dtyp = dt_dword;
-	this->STARS_MD_LAST_SAVED_REG_NUM = R_di;
+	this->STARS_ISA_dtyp = STARS_dt_dword;
+	this->STARS_MD_LAST_SAVED_REG_NUM = STARS_x86_R_di;
 	return;
 }
 
+// TODO: Move to base class.
 // Set internal state to handle a 64-bit binary
 void STARS_IDA_Program_t::Set64BitBinary(void) {
 	STARS_Program_t::SetBitwidth64();
-	this->STARS_ISA_dtyp = dt_qword;
-	this->STARS_MD_LAST_SAVED_REG_NUM = R_r15;
+	this->STARS_ISA_dtyp = STARS_dt_qword;
+	this->STARS_MD_LAST_SAVED_REG_NUM = STARS_x86_R_r15;
 	return;
 }
 
@@ -70,8 +72,8 @@ void STARS_IDA_Program_t::CloseFiles(void) {
 void STARS_IDA_Program_t::InitData(void) {
 	SMP_msg("INFO: Reach STARS_IDA_Program_t::InitData method.\n");
 
-	this->MDInitializeArgumentRegs();
-	this->MDInitializeCallerSavedRegs();
+	this->MDInitializeArgumentRegs();  // TODO: Move to base class InitData
+	this->MDInitializeCallerSavedRegs(); // TODO: Move to base class InitData
 
 	// Init everything else through the base class.
 	STARS_Program_t::InitData();
@@ -100,6 +102,7 @@ void STARS_IDA_Program_t::DetermineRootFileName(void) {
 	return;
 }
 
+// TODO: Move to base class.
 void STARS_IDA_Program_t::MDInitializeCallerSavedRegs(void) {
 	this->STARS_MDCallerSavedRegs.clear();
 	bool x86_64_ISA_flag = false;
@@ -131,6 +134,7 @@ void STARS_IDA_Program_t::MDInitializeCallerSavedRegs(void) {
 	return;
 } // end of STARS_IDA_Program_t::MDInitializeCallerSavedRegs()
 
+// TODO: Move to base class.
 void STARS_IDA_Program_t::MDInitializeArgumentRegs(void) {
 	bool x86_64_ISA_flag = false;
 #ifdef __EA64__
-- 
GitLab