Commit 28a98ebc authored by Jason Hiser's avatar Jason Hiser 🚜

doing code cleanups

parent 409fb094
......@@ -31,10 +31,9 @@ using namespace std;
using namespace IRDB_SDK;
using namespace InitStack;
InitStack_t::InitStack_t(IRDB_SDK::pqxxDB_t &p_dbinterface, IRDB_SDK::FileIR_t *p_variantIR, const string& p_functionsFilename, int p_initValue, bool p_verbose)
InitStack_t::InitStack_t(FileIR_t *p_variantIR, const string& p_functionsFilename, int p_initValue, bool p_verbose)
:
Transform(p_variantIR),
m_dbinterface(p_dbinterface),
m_initValue(p_initValue),
m_verbose(p_verbose),
m_numTransformed(0)
......@@ -58,22 +57,21 @@ InitStack_t::InitStack_t(IRDB_SDK::pqxxDB_t &p_dbinterface, IRDB_SDK::FileIR_t *
*/
void InitStack_t::readFunctionsFromFile(const string &p_filename)
{
std::ifstream functionsFile(p_filename);
if (!functionsFile.is_open())
throw;
std::string line;
ifstream functionsFile(p_filename);
if (!functionsFile.is_open()) throw runtime_error("Cannot open "+p_filename);
auto line = string();
while(functionsFile >> line)
{
for_each(getFileIR()->getFunctions().begin(), getFileIR()->getFunctions().end(), [&](Function_t* f)
for(auto f : getFileIR()->getFunctions())
{
if (f && f->getName() == line)
{
m_funcsToInit.insert(f);
cout <<"Adding " << line << " to function list" << endl;
}
});
};
}
functionsFile.close();
}
/*
......@@ -85,15 +83,13 @@ void InitStack_t::readFunctionsFromFile(const string &p_filename)
*/
int InitStack_t::execute()
{
for_each(m_funcsToInit.begin(), m_funcsToInit.end(), [&](Function_t* f)
for(auto f : m_funcsToInit)
{
assert(f);
if (f->getName().substr(0, 1) == ".")
return;
if (f == nullptr ) continue;
if (f->getName().substr(0, 1) == ".") continue; /* anh: ??? */
initStack(f);
});
};
cout << "#ATTRIBUTE InitStack::numTransformed=" << m_numTransformed << endl;
......@@ -106,31 +102,37 @@ int InitStack_t::execute()
*/
void InitStack_t::initStack(Function_t* f)
{
/* preconditions */
assert(f!=nullptr);
/* check that the frame size is in the area we care about */
const auto frame_size = f->getStackFrameSize();
if (frame_size <= MIN_STACK_FRAME_SIZE || frame_size >= MAX_STACK_FRAME_SIZE)
return;
if (frame_size <= MIN_STACK_FRAME_SIZE || frame_size >= MAX_STACK_FRAME_SIZE) return;
/* debug output */
cout << "Function: " << f->getName() << " frame size: " << f->getStackFrameSize() << endl;
const auto num_locs = static_cast<uint64_t> (ceil(frame_size / 4.0));
/* sanity check the frame size: anh, can we safely remove this? */
const auto num_locs = static_cast<uint64_t> (ceil(frame_size / 4.0));
assert (num_locs >= 0 && num_locs <= 4000000); // sanity check
/* not all functions have an entry point */
const auto entry = f->getEntryPoint();
if (!entry)
return;
if (!entry) return;
cout << "Function: " << f->getName() << " auto-initialize " << dec << num_locs << " stack memory locations (4 bytes at a time) with value = " << hex << m_initValue << endl;
/* determine the registers to use on x86-32 or x86-64 */
const auto sp_reg= getFileIR()->getArchitectureBitWidth()==64 ? "rsp" : "esp";
const auto scratch_reg= getFileIR()->getArchitectureBitWidth()==64 ? "r11" : "ecx";
// assume: flags dead
// assume: flags dead at function entry.
// and insert these instructions at the start of the function
auto i=entry;
insertAssemblyBefore (i, string()+"mov ["+sp_reg+"+"+to_string(-f->getStackFrameSize()-100)+"], "+scratch_reg);
i = insertAssemblyAfter (i, string()+"mov "+scratch_reg+", -" + std::to_string(num_locs));
const auto L1 = i = insertAssemblyAfter (i, string()+"mov dword ["+sp_reg+"+"+scratch_reg+"*4-4], " + std::to_string(m_initValue));
i = insertAssemblyAfter (i, string()+"mov "+scratch_reg+", -" + to_string(num_locs));
const auto L1 = i = insertAssemblyAfter (i, string()+"mov dword ["+sp_reg+"+"+scratch_reg+"*4-4], " + to_string(m_initValue));
i = insertAssemblyAfter (i, string()+"inc "+scratch_reg);
i = insertAssemblyAfter (i, string()+"jnz 0", L1);
i = insertAssemblyAfter (i, string()+"mov "+scratch_reg+", ["+sp_reg+"+"+to_string(-f->getStackFrameSize()-100)+"] ");
......
......@@ -18,8 +18,8 @@
*
*/
#ifndef _LIBTRANSFORM_INIT_STACK_H
#define _LIBTRANSFORM_INIT_STACK_H
#ifndef _INIT_STACK_H
#define _INIT_STACK_H
#include <irdb-core>
#include <irdb-transform>
......@@ -27,32 +27,32 @@
namespace InitStack
{
using namespace std;
using namespace IRDB_SDK;
#define DEFAULT_INIT_VALUE 0
// the actual transform.
class InitStack_t : public Transform
{
public:
// explicitly disable default and copy constructors
InitStack_t() = delete;
InitStack_t(const InitStack::InitStack_t&) = delete;
InitStack_t(IRDB_SDK::pqxxDB_t &p_dbinterface, IRDB_SDK::FileIR_t *p_variantIR, const std::string& p_functionFilename, int initValue = DEFAULT_INIT_VALUE, bool p_verbose = false);
int execute();
private:
void readFunctionsFromFile(const string &p_filename);
void initStack(Function_t* f);
private:
IRDB_SDK::pqxxDB_t &m_dbinterface;
set<Function_t*> m_funcsToInit;
int m_initValue;
bool m_verbose;
int m_numTransformed;
};
using namespace std;
using namespace IRDB_SDK;
// the actual transform.
class InitStack_t : public Transform
{
public:
InitStack_t(
FileIR_t *p_variantIR,
const string& p_functionFilename,
int initValue = 0,
bool p_verbose = false
);
int execute();
private:
void readFunctionsFromFile(const string &p_filename);
void initStack(Function_t* f);
private:
set<Function_t*> m_funcsToInit;
int m_initValue;
bool m_verbose;
int m_numTransformed;
};
}
......
......@@ -44,16 +44,18 @@ int main(int argc, char **argv)
exit(1);
}
string programName(argv[0]);
int variantID = atoi(argv[1]);
// constant parameters read from argv
const auto programName = string(argv[0]);
const auto variantID = atoi(argv[1]);
bool verbose=false;
string funcsFilename;
int initValue = 0;
// initial values of parameters to parse
auto verbose = false;
auto funcsFilename= string();
auto initValue = 0;
// Parse some options for the transform
static struct option long_options[] = {
struct option long_options[] = {
{"functions", required_argument, 0, 'f'},
{"initvalue", required_argument, 0, 'i'},
{"verbose", no_argument, 0, 'v'},
......@@ -71,24 +73,24 @@ int main(int argc, char **argv)
break;
switch(c)
{
case 'f':
funcsFilename=optarg;
cout<<"Reading file with function specifiers: "<<funcsFilename<<endl;
break;
case 'i':
initValue=strtoll(optarg, NULL, 0);
cout<<" Stack initialization value: "<<hex<<initValue<<endl;
break;
case 'v':
verbose=true;
break;
case '?':
case 'h':
usage(argv[0]);
exit(1);
break;
default:
break;
case 'f':
funcsFilename=optarg;
cout<<"Reading file with function specifiers: "<<funcsFilename<<endl;
break;
case 'i':
initValue=strtoll(optarg, NULL, 0);
cout<<" Stack initialization value: "<<hex<<initValue<<endl;
break;
case 'v':
verbose=true;
break;
case '?':
case 'h':
usage(argv[0]);
exit(1);
break;
default:
break;
}
}
......@@ -100,51 +102,48 @@ int main(int argc, char **argv)
auto pidp=VariantID_t::factory(variantID);
assert(pidp->isRegistered()==true);
bool one_success = false;
for(set<File_t*>::iterator it=pidp->getFiles().begin();
it!=pidp->getFiles().end();
++it)
{
File_t* this_file = *it;
auto firp = FileIR_t::factory(pidp.get(), this_file);
auto success = false;
cout<<"Transforming "<<this_file->getURL()<<endl;
auto this_file = pidp->getMainFile();
auto firp = FileIR_t::factory(pidp.get(), this_file);
assert(firp && pidp);
cout<<"Transforming "<<this_file->getURL()<<endl;
try
{
InitStack_t is(*pqxx_interface,firp.get(), funcsFilename, initValue, verbose);
int success=is.execute();
if (success)
{
cout<<"Writing changes for "<<this_file->getURL()<<endl;
one_success = true;
firp->writeToDB();
}
else
{
cout<<"Skipping (no changes) "<<this_file->getURL()<<endl;
}
}
catch (DatabaseError_t pnide)
assert(firp && pidp);
try
{
InitStack_t is(firp.get(), funcsFilename, initValue, verbose);
auto success=is.execute();
if (success)
{
cerr << programName << ": Unexpected database error: " << pnide << "file url: " << this_file->getURL() << endl;
cout<<"Writing changes for "<<this_file->getURL()<<endl;
firp->writeToDB();
success = true;
}
catch (...)
else
{
cerr << programName << ": Unexpected error file url: " << this_file->getURL() << endl;
cout<<"Skipping (no changes) "<<this_file->getURL()<<endl;
}
} // end file iterator
}
catch (DatabaseError_t pnide)
{
cerr << programName << ": Unexpected database error: " << pnide << "file url: " << this_file->getURL() << endl;
}
catch (...)
{
cerr << programName << ": Unexpected error file url: " << this_file->getURL() << endl;
}
// if any transforms for any files succeeded, we commit
if (one_success)
if (success)
{
cout<<"Commiting changes...\n";
pqxx_interface->commit();
}
return one_success ? 0 : 2;
// Return success code to driver. 0=success, 1=warnings, 2=errors.
return success ? 0 : 2;
}
......@@ -24,38 +24,6 @@
using namespace IRDB_SDK;
using namespace std;
#if 0
static RegisterSet_t get_dead_regs(Instruction_t* insn, MEDS_AnnotationParser &meds_ap_param)
{
auto meds_ap=&meds_ap_param;
assert(meds_ap);
/* find it in the annotations */
auto ret = meds_ap->getAnnotations().equal_range(insn->getBaseID());
MEDS_DeadRegAnnotation* p_annotation;
/* for each annotation for this instruction */
for (auto it = ret.first; it != ret.second; ++it)
{
/* is this annotation a funcSafe annotation? */
p_annotation=dynamic_cast<MEDS_DeadRegAnnotation*>(it->second);
if(p_annotation==NULL)
continue;
/* bad annotation? */
if(!p_annotation->isValid())
continue;
/* that marks the function safe? */
return p_annotation->getRegisterSet();
}
/* couldn't find the annation, return an empty set.*/
return RegisterSet_t();
}
#endif
KillDeads::KillDeads(FileIR_t *p_variantIR)
:
......@@ -71,11 +39,7 @@ int KillDeads::execute()
srand(time(0));
int kill_val=rand();
/*
STARS::IRDB_Interface_t stars_analysis_engine(dbinterface);
stars_analysis_engine.do_STARS(getFileIR());
auto &annotations=stars_analysis_engine.getAnnotations();
*/
auto de=DeepAnalysis_t::factory(getFileIR());
auto reg_mapp=de->getDeadRegisters();
auto &reg_map=*reg_mapp;
......@@ -111,7 +75,6 @@ int KillDeads::execute()
assert(getenv("SELF_VALIDATE")==nullptr || killed_flags > 5);
assert(getenv("SELF_VALIDATE")==nullptr || killed_regs > 15);
// assert(getenv("SELF_VALIDATE")==nullptr || annotations.getAnnotations().size() > 15);
cout<<"#ATTRIBUTE killed_flags="<<dec<<killed_flags<<endl;
cout<<"#ATTRIBUTE killed_regs="<<dec<<killed_regs<<endl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment