From 338bf26d8c8e2f5da4f8c1c3ec52ecdae511c6b9 Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Sat, 4 Jul 2015 21:46:44 +0000
Subject: [PATCH] Fix phase ordering issue with ProfilerInformation annotation
 file processing.

Former-commit-id: 564f919d6d2418b79d5fc92aabe9b2330346093b
---
 include/interfaces/abstract/STARSProgram.h |  1 +
 include/interfaces/idapro/STARSInterface.h |  6 +++---
 src/base/ProfilerInformation.cpp           |  5 +++--
 src/base/SMPInstr.cpp                      |  3 +++
 src/drivers/idapro/SMPStaticAnalyzer.cpp   |  7 ++++---
 src/interfaces/abstract/STARSProgram.cpp   | 10 ++++++++++
 6 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/include/interfaces/abstract/STARSProgram.h b/include/interfaces/abstract/STARSProgram.h
index eaf68937..d13481e4 100644
--- a/include/interfaces/abstract/STARSProgram.h
+++ b/include/interfaces/abstract/STARSProgram.h
@@ -27,6 +27,7 @@ class STARS_Program_t
 		virtual void DetermineRootFileName(void) = 0;
 		virtual bool OpenFiles(void);
 		virtual void CloseFiles(void);
+		virtual bool ReopenAnnotFile(const char *mode);
 		void ZST_InitPolicies(void);
 
 		// Get (accessor) methods
diff --git a/include/interfaces/idapro/STARSInterface.h b/include/interfaces/idapro/STARSInterface.h
index 490d0015..0dba26fa 100644
--- a/include/interfaces/idapro/STARSInterface.h
+++ b/include/interfaces/idapro/STARSInterface.h
@@ -45,10 +45,10 @@ public:
 
 	// Function accessors
 
-    // find out how many functions there are
+	// find out how many functions there are
 	virtual std::size_t get_func_qty() { return ::get_func_qty(); };
 
-    // get the index-th function
+	// get the index-th function
 	virtual STARS_Function_t *getn_func(int index) {
 		func_t* Func = ::getn_func(index);
 		return func2Func(Func);
@@ -105,7 +105,7 @@ public:
 
 	// Miscellaneous IDA-only methods.
 	virtual void AuditTailChunkOwnership(void);
-    virtual void AuditCodeTargets(void);
+	virtual void AuditCodeTargets(void);
 
 	virtual bool STARS_patch_byte(STARS_ea_t InstAddr, uint32_t ByteValue); // Patch IDA Pro database.
 
diff --git a/src/base/ProfilerInformation.cpp b/src/base/ProfilerInformation.cpp
index b571653c..461be192 100644
--- a/src/base/ProfilerInformation.cpp
+++ b/src/base/ProfilerInformation.cpp
@@ -116,7 +116,6 @@ ProfilerInformation::ProfilerInformation(const char* fn, SMPProgram *CurrProg)
 		return;
 
 	fin = SMP_fopen(fn, "r");
-
 	if (!fin)
 	{
 		SMP_msg("WARNING: Cannot open strata annotation file %s\n", fn);
@@ -258,9 +257,11 @@ ProfilerInformation::ProfilerInformation(const char* fn, SMPProgram *CurrProg)
 		} while ((TempChar != '\n') && (TempChar != EOF));
 		line++;
 	} while (!SMP_feof(fin));
+
 	int ReturnCode = SMP_fclose(fin);
 	if (0 != ReturnCode) {
-		SMP_msg("ERROR: Failed to close annotation file %s ReturnCode: %d\n", fn, ReturnCode);
+		SMP_msg("FATAL ERROR: Failed to close annotation file %s in ProfilerInformation. ReturnCode: %d\n", fn, ReturnCode);
+		assert(0 == ReturnCode);
 	}
 
 	SMP_msg("Successfully loaded annotation file %s\n", fn);
diff --git a/src/base/SMPInstr.cpp b/src/base/SMPInstr.cpp
index 9423951e..e8d251a4 100644
--- a/src/base/SMPInstr.cpp
+++ b/src/base/SMPInstr.cpp
@@ -2813,6 +2813,9 @@ int SMPInstr::operator<=(const SMPInstr &rhs) const {
 // Is this instruction one that allocates space on the
 //  stack for the local variables?
 bool SMPInstr::MDIsFrameAllocInstr(void) {
+	if (this->IsAllocaCall())
+		return true;
+
 	// The frame allocating instruction should look like:
 	//   sub esp,48   or   add esp,-64   etc.
 	STARSOpndTypePtr ESPOp = this->STARSInstPtr->MakeRegOpnd(STARS_x86_R_sp);
diff --git a/src/drivers/idapro/SMPStaticAnalyzer.cpp b/src/drivers/idapro/SMPStaticAnalyzer.cpp
index 77cc9c65..a79c5559 100644
--- a/src/drivers/idapro/SMPStaticAnalyzer.cpp
+++ b/src/drivers/idapro/SMPStaticAnalyzer.cpp
@@ -348,8 +348,11 @@ void IDAP_run(int arg) {
 
 	// read the Profiler generated information into a new prof_info class 
 	CurrProg = new SMPProgram();
+
+	CurrProg->AnalyzeData(); // Analyze static data in the executable
+
+	// Note: ProfilerInformation must come after the call above to AnalyzeData().
 	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");
@@ -390,8 +393,6 @@ void IDAP_run(int arg) {
 	}
 #endif
 
-	CurrProg->AnalyzeData(); // Analyze static data in the executable
-
 	// Read the Zephyr Security Toolkit system call security policies, if available.
 	global_STARS_program->ZST_InitPolicies();
 
diff --git a/src/interfaces/abstract/STARSProgram.cpp b/src/interfaces/abstract/STARSProgram.cpp
index 3c2f4333..4d6f5fd7 100644
--- a/src/interfaces/abstract/STARSProgram.cpp
+++ b/src/interfaces/abstract/STARSProgram.cpp
@@ -167,6 +167,16 @@ void STARS_Program_t::CloseFiles(void) {
 	return;
 } // end of STARS_Program_t::CloseFiles()
 
+// Apply new mode to the AnnotFile.
+bool STARS_Program_t::ReopenAnnotFile(const char *mode) {
+	FILE *TempHandle = freopen(NULL, mode, this->GetAnnotFile());
+	bool success = (NULL != TempHandle);
+	if  (success) {
+		this->STARS_AnnotFile = TempHandle;
+	}
+	return success;
+}
+
 void STARS_Program_t::InitData(void) {
 	this->CurrentFileNumber = 0;
 	this->ZST_AlarmFile = NULL;
-- 
GitLab