From be65bea8b3e8ef34ed9a3de9bd68c315df89a9c8 Mon Sep 17 00:00:00 2001 From: clc5q <clc5q@git.zephyr-software.com> Date: Thu, 25 Jun 2015 19:42:34 +0000 Subject: [PATCH] Refactor code into base class for STARSProgram. Former-commit-id: 5fb977445b7d9ef731ed9b81d64c7594c2e4a104 --- include/interfaces/abstract/STARSProgram.h | 18 +++++-- include/interfaces/idapro/STARSProgram.h | 7 --- include/interfaces/irdb/STARSProgram.h | 8 --- src/interfaces/abstract/STARSProgram.cpp | 55 +++++++++++++++++++ src/interfaces/idapro/STARSIDAProgram.cpp | 62 ---------------------- src/interfaces/irdb/STARSIRDBProgram.cpp | 31 +---------- 6 files changed, 70 insertions(+), 111 deletions(-) diff --git a/include/interfaces/abstract/STARSProgram.h b/include/interfaces/abstract/STARSProgram.h index 2dd1871a..585f82b2 100644 --- a/include/interfaces/abstract/STARSProgram.h +++ b/include/interfaces/abstract/STARSProgram.h @@ -32,8 +32,8 @@ class STARS_Program_t // Get (accessor) methods std::size_t GetSTARS_ISA_Bitwidth(void) const { return STARS_ISA_Bitwidth; }; std::size_t GetSTARS_ISA_Bytewidth(void) const { return STARS_ISA_Bytewidth; }; - virtual char GetSTARS_ISA_dtyp(void) const = 0; - virtual int GetSTARS_MD_LAST_SAVED_REG_NUM(void) const = 0; + char GetSTARS_ISA_dtyp(void) const { return STARS_ISA_dtyp; }; + int GetSTARS_MD_LAST_SAVED_REG_NUM(void) const { return STARS_MD_LAST_SAVED_REG_NUM; }; int GetOptCategory(uint16_t IDAOpcodeEnum) const { return OptCategory[IDAOpcodeEnum]; }; STARS_sval_t GetStackAlteration(uint16_t IDAOpcodeEnum) const { return StackAlteration[IDAOpcodeEnum]; }; int GetOptCount(std::size_t CategoryIndex) const { return OptCount[CategoryIndex]; }; @@ -102,13 +102,21 @@ class STARS_Program_t std::vector<SMP_bounds_t> FuncBounds; // Mutators - void SetBitwidth32(void) { STARS_ISA_Bytewidth = 4; STARS_ISA_Bitwidth = 32; }; - void SetBitwidth64(void) { STARS_ISA_Bytewidth = 8; STARS_ISA_Bitwidth = 64; }; + void SetBitwidth32(void) { + STARS_ISA_Bytewidth = 4; STARS_ISA_Bitwidth = 32; STARS_ISA_dtyp = STARS_dt_dword; + STARS_MD_LAST_SAVED_REG_NUM = STARS_x86_R_di; + }; + void SetBitwidth64(void) { + STARS_ISA_Bytewidth = 8; STARS_ISA_Bitwidth = 64; STARS_ISA_dtyp = STARS_dt_qword; + STARS_MD_LAST_SAVED_REG_NUM = STARS_x86_R_r15; + }; void SetTotalCodeSize(unsigned long long TotalSize) { STARS_TotalCodeSize = TotalSize; }; void SetReducedProcessingFlag(bool FlagValue) { STARS_PerformReducedAnalysis = FlagValue; }; void IncrementCurrentFileNum(void) { ++CurrentFileNumber; }; // Analysis methods + void MDInitializeCallerSavedRegs(void); + void MDInitializeArgumentRegs(void); // Convert a call type string from the policy file, such as "FILECALLS", to the // corresponding ZST_SysCallType, such as ZST_FILE_CALL. @@ -121,6 +129,8 @@ class STARS_Program_t // Data members. std::size_t STARS_ISA_Bitwidth; std::size_t STARS_ISA_Bytewidth; + char STARS_ISA_dtyp; + int STARS_MD_LAST_SAVED_REG_NUM; // Unique data referent number to use in data annotations. unsigned long DataReferentID; diff --git a/include/interfaces/idapro/STARSProgram.h b/include/interfaces/idapro/STARSProgram.h index 47dfcfbb..834cddc3 100644 --- a/include/interfaces/idapro/STARSProgram.h +++ b/include/interfaces/idapro/STARSProgram.h @@ -23,8 +23,6 @@ public: void CloseFiles(void); // Get (accessor) methods - char GetSTARS_ISA_dtyp(void) const { return STARS_ISA_dtyp; }; - int GetSTARS_MD_LAST_SAVED_REG_NUM(void) const { return STARS_MD_LAST_SAVED_REG_NUM; }; // Set (mutator) methods void Set32BitBinary(void); // Set internal state to handle a 32-bit binary @@ -43,12 +41,7 @@ public: private: // Data - char STARS_ISA_dtyp; - int STARS_MD_LAST_SAVED_REG_NUM; - // Methods - void MDInitializeCallerSavedRegs(void); - void MDInitializeArgumentRegs(void); void ComputeGlobalFieldOffsets(struct GlobalVar &CurrGlobal); }; diff --git a/include/interfaces/irdb/STARSProgram.h b/include/interfaces/irdb/STARSProgram.h index dcdb71e9..b930afd2 100644 --- a/include/interfaces/irdb/STARSProgram.h +++ b/include/interfaces/irdb/STARSProgram.h @@ -23,12 +23,8 @@ public: void CloseFiles(void); // Get (accessor) methods - char GetSTARS_ISA_dtyp(void) const { return STARS_ISA_dtyp; }; - int GetSTARS_MD_LAST_SAVED_REG_NUM(void) const { return STARS_MD_LAST_SAVED_REG_NUM; }; // Set (mutator) methods - void Set32BitBinary(void); // Set internal state to handle a 32-bit binary - void Set64BitBinary(void); // Set internal state to handle a 64-bit binary // Query methods @@ -44,12 +40,8 @@ private: // Data FILE *SMPLogFile; - char STARS_ISA_dtyp; - int STARS_MD_LAST_SAVED_REG_NUM; // Methods - void MDInitializeCallerSavedRegs(void); - void MDInitializeArgumentRegs(void); libIRDB::FileIR_t* m_firp; diff --git a/src/interfaces/abstract/STARSProgram.cpp b/src/interfaces/abstract/STARSProgram.cpp index a74c687d..3c2f4333 100644 --- a/src/interfaces/abstract/STARSProgram.cpp +++ b/src/interfaces/abstract/STARSProgram.cpp @@ -12,6 +12,58 @@ using namespace std; // Data initialization + + +void STARS_Program_t::MDInitializeCallerSavedRegs(void) { + this->STARS_MDCallerSavedRegs.clear(); + bool x86_64_ISA_flag = false; +#ifdef __EA64__ + x86_64_ISA_flag = (this->GetSTARS_ISA_Bitwidth() == 64); +#endif + if (!x86_64_ISA_flag) { + // 32-bit x86 uses EAX, ECX, EDX as caller-saved. + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_ax); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_cx); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_dx); + } + else { + // 64-bit x86 uses EDI, ESI, EDX, ECX, R8 and R9 + // in that order. After six arguments that fit into + // these regs, arguments are passed on the stack. + // In addition, registers EAX, R10 and R11 are caller-saved + // but are not used to pass arguments. + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_ax); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_cx); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_dx); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_si); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_di); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_r8); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_r9); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_r10); + this->STARS_MDCallerSavedRegs.push_back(STARS_x86_R_r11); + } + return; +} // end of STARS_IDA_Program_t::MDInitializeCallerSavedRegs() + +void STARS_Program_t::MDInitializeArgumentRegs(void) { + bool x86_64_ISA_flag = false; +#ifdef __EA64__ + x86_64_ISA_flag = (this->GetSTARS_ISA_Bitwidth() == 64); +#endif + if (x86_64_ISA_flag) { + this->STARS_MDArgumentRegs.push_back(STARS_x86_R_di); + this->STARS_MDArgumentRegs.push_back(STARS_x86_R_si); + this->STARS_MDArgumentRegs.push_back(STARS_x86_R_dx); + this->STARS_MDArgumentRegs.push_back(STARS_x86_R_cx); + this->STARS_MDArgumentRegs.push_back(STARS_x86_R_r8); + this->STARS_MDArgumentRegs.push_back(STARS_x86_R_r9); + } + else { + this->STARS_MDArgumentRegs.clear(); + } + return; +} // end of STARS_IDA_Program_t::MDInitializeArgumentRegs() + bool STARS_Program_t::OpenFiles(void) { // Open the output files. assert(0 < this->RootFileName.size()); // SetRootFileName() must be called previously. @@ -121,6 +173,9 @@ void STARS_Program_t::InitData(void) { this->STARS_CallReturnFile = NULL; this->STARS_XrefsFile = NULL; + this->MDInitializeArgumentRegs(); + this->MDInitializeCallerSavedRegs(); + // Initialize global counters for statistics-gathering purposes. STARS_SPARK_IndentCount = 1; UnusedStructCount = 0; diff --git a/src/interfaces/idapro/STARSIDAProgram.cpp b/src/interfaces/idapro/STARSIDAProgram.cpp index 12eca099..f58affbc 100644 --- a/src/interfaces/idapro/STARSIDAProgram.cpp +++ b/src/interfaces/idapro/STARSIDAProgram.cpp @@ -33,21 +33,15 @@ 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 = 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 = STARS_dt_qword; - this->STARS_MD_LAST_SAVED_REG_NUM = STARS_x86_R_r15; return; } @@ -72,9 +66,6 @@ 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(); // 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(); @@ -101,59 +92,6 @@ void STARS_IDA_Program_t::DetermineRootFileName(void) { this->SetRootFileName(TempRootString); return; } - -// TODO: Move to base class. -void STARS_IDA_Program_t::MDInitializeCallerSavedRegs(void) { - this->STARS_MDCallerSavedRegs.clear(); - bool x86_64_ISA_flag = false; -#ifdef __EA64__ - x86_64_ISA_flag = (this->GetSTARS_ISA_Bitwidth() == 64); -#endif - if (!x86_64_ISA_flag) { - // 32-bit x86 uses EAX, ECX, EDX as caller-saved. - this->STARS_MDCallerSavedRegs.push_back(R_ax); - this->STARS_MDCallerSavedRegs.push_back(R_cx); - this->STARS_MDCallerSavedRegs.push_back(R_dx); - } - else { - // 64-bit x86 uses EDI, ESI, EDX, ECX, R8 and R9 - // in that order. After six arguments that fit into - // these regs, arguments are passed on the stack. - // In addition, registers EAX, R10 and R11 are caller-saved - // but are not used to pass arguments. - this->STARS_MDCallerSavedRegs.push_back(R_ax); - this->STARS_MDCallerSavedRegs.push_back(R_cx); - this->STARS_MDCallerSavedRegs.push_back(R_dx); - this->STARS_MDCallerSavedRegs.push_back(R_si); - this->STARS_MDCallerSavedRegs.push_back(R_di); - this->STARS_MDCallerSavedRegs.push_back(R_r8); - this->STARS_MDCallerSavedRegs.push_back(R_r9); - this->STARS_MDCallerSavedRegs.push_back(R_r10); - this->STARS_MDCallerSavedRegs.push_back(R_r11); - } - 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__ - x86_64_ISA_flag = (this->GetSTARS_ISA_Bitwidth() == 64); -#endif - if (x86_64_ISA_flag) { - this->STARS_MDArgumentRegs.push_back(R_di); - this->STARS_MDArgumentRegs.push_back(R_si); - this->STARS_MDArgumentRegs.push_back(R_dx); - this->STARS_MDArgumentRegs.push_back(R_cx); - this->STARS_MDArgumentRegs.push_back(R_r8); - this->STARS_MDArgumentRegs.push_back(R_r9); - } - else { - this->STARS_MDArgumentRegs.clear(); - } - return; -} // end of STARS_IDA_Program_t::MDInitializeArgumentRegs() - // Does the instruction at InstAddr access the global data offset in GlobalAddr // using an index register? bool MDIsIndexedAccess(STARS_ea_t InstAddr, STARS_ea_t GlobalAddr) { diff --git a/src/interfaces/irdb/STARSIRDBProgram.cpp b/src/interfaces/irdb/STARSIRDBProgram.cpp index 7ea8fc5a..3c0304bd 100644 --- a/src/interfaces/irdb/STARSIRDBProgram.cpp +++ b/src/interfaces/irdb/STARSIRDBProgram.cpp @@ -17,16 +17,12 @@ using namespace std; // Set internal state to handle a 32-bit binary void STARS_IRDB_Program_t::Set32BitBinary(void) { STARS_Program_t::SetBitwidth32(); - // this->STARS_ISA_dtyp = dt_dword; - // this->STARS_MD_LAST_SAVED_REG_NUM = R_di; return; } // Set internal state to handle a 64-bit binary void STARS_IRDB_Program_t::Set64BitBinary(void) { STARS_Program_t::SetBitwidth64(); - // this->STARS_ISA_dtyp = dt_qword; - // this->STARS_MD_LAST_SAVED_REG_NUM = R_r15; return; } @@ -38,7 +34,7 @@ void STARS_IRDB_Program_t::ReportTotalCodeSize(unsigned long long TotalCodeSize) } bool STARS_IRDB_Program_t::OpenFiles(void) { -#if 0 // Need IRDB version of GetRootFileName() +#if 1 // Need IRDB version of GetRootFileName() string ZSTLogFileName(global_STARS_program->GetRootFileName()); string LogFileSuffix(".STARSlog"); ZSTLogFileName += LogFileSuffix; @@ -60,31 +56,6 @@ void STARS_IRDB_Program_t::CloseFiles(void) { return; } // end of STARS_IRDB_Program_t::CloseFiles() -void STARS_IRDB_Program_t::InitData(void) { - SMP_msg("INFO: Reach STARS_IDA_Program_t::InitData method.\n"); - - // this->MDInitializeArgumentRegs(); - // this->MDInitializeCallerSavedRegs(); - - // Init everything else through the base class. - STARS_Program_t::InitData(); - -#if 0 - // Record the start and end addresses for all function entry - // chunks in the program. - this->FuncBounds.reserve(10 + SMP_get_func_qty()); - for (std::size_t FuncIndex = 0; FuncIndex < SMP_get_func_qty(); ++FuncIndex) { - STARS_Function_t *FuncInfo = SMP_getn_func(FuncIndex); - SMP_bounds_t temp; - temp.startEA = FuncInfo->get_startEA(); - temp.endEA = FuncInfo->get_endEA(); - FuncBounds.push_back(temp); - } -#endif - - return; -} // end of STARS_IRDB_Program_t::InitData() - void STARS_IRDB_Program_t::DetermineRootFileName(void) { #if 0 SMP_msg("INFO: Reached DetermineRootFileName method.\n"); -- GitLab