From 1937bc5ab305f4d5ae5cecf4d27e244420349a5b Mon Sep 17 00:00:00 2001 From: Clark Coleman <clc@zephyr-software.com> Date: Thu, 12 Dec 2019 22:08:10 -0500 Subject: [PATCH] Don't try to find code addresses in shared data for shared objects, DLLs and archives for STARS/IDA Pro. --- include/interfaces/abstract/STARSProgram.h | 3 ++ src/drivers/idapro/SMPStaticAnalyzer.cpp | 4 +- src/interfaces/idapro/STARSIDAProgram.cpp | 59 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/include/interfaces/abstract/STARSProgram.h b/include/interfaces/abstract/STARSProgram.h index eeb935ab..dde17704 100644 --- a/include/interfaces/abstract/STARSProgram.h +++ b/include/interfaces/abstract/STARSProgram.h @@ -112,6 +112,7 @@ class STARS_Program_t bool ShouldSTARSPerformConstantPropagation(void) const { return STARS_PerformConstantPropagation; }; bool ShouldSTARSTranslateToSPARKAda(void) const { return STARS_TranslateToSPARKAda; }; bool IsIDAProDriverMode(void) const { return IDAProDriver; }; + bool IsSharedObject(void) const { return SharedObjectFlag; }; bool IsLocationWhitelisted(ZST_SysCallType CallType, std::string LocationName) const; bool IsLocationBlacklisted(ZST_SysCallType CallType, std::string LocationName) const; // Given a called function name, does it produce only benign numeric errors when @@ -184,6 +185,7 @@ class STARS_Program_t }; void SetTotalCodeSize(unsigned long long TotalSize) { STARS_TotalCodeSize = TotalSize; }; void IncrementCurrentFileNum(void) { ++CurrentFileNumber; }; + void SetSharedObjectFlag(const bool flag) { SharedObjectFlag = flag; }; // Analysis methods void MDInitializeCallerSavedRegs(void); @@ -204,6 +206,7 @@ class STARS_Program_t STARS_ProcessorType_t processor_type; char STARS_ISA_dtyp; bool IDAProDriver; // program is compiled in IDA Pro mode + bool SharedObjectFlag; int STARS_MD_LAST_SAVED_REG_NUM; // Unique data referent number to use in data annotations. diff --git a/src/drivers/idapro/SMPStaticAnalyzer.cpp b/src/drivers/idapro/SMPStaticAnalyzer.cpp index 5d6fc8bd..e19a70fd 100644 --- a/src/drivers/idapro/SMPStaticAnalyzer.cpp +++ b/src/drivers/idapro/SMPStaticAnalyzer.cpp @@ -477,7 +477,9 @@ bool IDAP_run(size_t arg) { time_t EndTime, Time1, Time2; CurrProg->AnalyzeData(); // Analyze static data in the executable Time1 = time(nullptr); - global_STARS_program->FindCodeAddressesTaken(CurrProg); // find code addresses in read-only data segments + if (!global_STARS_program->IsSharedObject()) { + global_STARS_program->FindCodeAddressesTaken(CurrProg); // find code addresses in read-only data segments + } Time2 = time(nullptr); // Note: ProfilerInformation must come after the call above to AnalyzeData(). diff --git a/src/interfaces/idapro/STARSIDAProgram.cpp b/src/interfaces/idapro/STARSIDAProgram.cpp index 80824166..7a65485d 100644 --- a/src/interfaces/idapro/STARSIDAProgram.cpp +++ b/src/interfaces/idapro/STARSIDAProgram.cpp @@ -8,6 +8,10 @@ #include <sys/utsname.h> +#include <iostream> +#include <string> +#include <sstream> + #include "interfaces/idapro/all.h" #include "interfaces/STARSTypes.h" #include "interfaces/STARSIDATypes.h" @@ -152,6 +156,61 @@ void STARS_IDA_Program_t::DetermineRootFileName(void) { #endif string TempRootString(TempRootName); this->SetRootFileName(TempRootString); + + // Try to find ".so" or ".dll" or ".a" in file name, indicating shared object. + // This code below only works for non-renamed file, e.g. STARS/IDA Pro operation, + // and not files renamed with new extensions, e.g. a.ncexe. Replace with info from + // file header soon. + bool sharedObjectFlag = false; + char token[210]; + std::size_t tokencount = 0; + istringstream FileNameStr(TempRootString); + this->SetSharedObjectFlag(false); + char delim = '.'; + FileNameStr.getline(token, 200, delim); + while (FileNameStr.rdstate() == std::istringstream::goodbit) { + ++tokencount; + if (1 < tokencount) { // Not looking for file extension before first "." + sharedObjectFlag = ((0 == strcmp(token, "so")) || (0 == strcmp(token, "dll")) + || (0 == strcmp(token, "a"))); + if (sharedObjectFlag) { + this->SetSharedObjectFlag(sharedObjectFlag); + SMP_msg("INFO: Determined shared object from file extension %s\n", token); + break; + } + } + FileNameStr.getline(token, 200, delim); + } + +#if 1 + // See if shared object extension is at the very end, with no more "." delimiter after. + // Find file name extension. + if (!sharedObjectFlag) { + string delimiter("."); + std::size_t found = TempRootString.rfind(delimiter, TempRootString.length()); + if (found != std::string::npos) { // found last instance of delimiter + string extension = TempRootString.substr(found, std::string::npos); + if (!extension.empty()) { + // This code below only works for non-renamed files, e.g. STARS/IDA Pro operation, + // and not files renamed with new extensions, e.g. a.ncexe. Replace with info from + // file header soon. + SMP_msg("INFO: Found extension %s\n", extension.c_str()); + sharedObjectFlag = ((0 == extension.compare(".so")) || (0 == extension.compare(".dll")) + || (0 == extension.compare(".a"))); + this->SetSharedObjectFlag(sharedObjectFlag); + if (sharedObjectFlag) + SMP_msg("INFO: Determined shared object from file extension %s\n", extension.c_str()); + } + else { + SMP_msg("ERROR: Empty file name extension.\n"); + } + } + else { + SMP_msg("ERROR: Could not reverse-find the dot delimiter in file name.\n"); + } + } +#endif + return; } // Does the instruction at InstAddr access the global data offset in GlobalAddr -- GitLab