Skip to content

Commit

Permalink
Prepare memalloc pintool for integration (#15)
Browse files Browse the repository at this point in the history
Add a new knob letting the user choose between "classic" or "couple-based"
memalloc pintool.
Reintegrate the old code corresponding to "classic" memalloc pintool

Also, a few indentation fix.
  • Loading branch information
NicsTr authored and Frky committed Jun 5, 2017
1 parent 59a3d26 commit b9a5bbc
Showing 1 changed file with 114 additions and 78 deletions.
192 changes: 114 additions & 78 deletions src/pintool/memalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@
#define COUPLE_THRESHOLD 0.9

ifstream ifile;
KNOB<string> KnobInputFile(KNOB_MODE_WRITEONCE, "pintool", "i", "stdin", "Specify an intput file");
KNOB<string> KnobInputFile(KNOB_MODE_WRITEONCE, "pintool", "i", "stdin",
"Specify an intput file");
ofstream ofile;
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "stdout", "Specify an output file");
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "stdout",
"Specify an output file");
bool couple_mode = false;
KNOB<BOOL> KnobCoupleMode(KNOB_MODE_WRITEONCE, "pintool", "couple", "false",
"Base memalloc inference on couple results instead of types");


/* Call stack */
HollowStack<MAX_DEPTH, FID> call_stack;
Expand Down Expand Up @@ -63,7 +69,7 @@ string read_part(char* c) {
string str = "";

ifile.read(&m, 1);
while (ifile && m != ':' && m != '\n') {
while (ifile && m != ':' && m != '\n' && m != ',') {
str += m;
ifile.read(&m, 1);
}
Expand All @@ -78,32 +84,32 @@ ADDRINT val_from_reg(CONTEXT *ctxt, unsigned int pid) {

REG reg;
switch (pid) {
case 0:
reg = REG_RAX;
break;
case 1:
reg = REG_RDI;
break;
case 2:
reg = REG_RSI;
break;
case 3:
reg = REG_RDX;
break;
case 4:
reg = REG_RCX;
break;
case 5:
reg = REG_R8;
break;
case 6:
reg = REG_R9;
break;
default:

trace_leave();

return 0;
case 0:
reg = REG_RAX;
break;
case 1:
reg = REG_RDI;
break;
case 2:
reg = REG_RSI;
break;
case 3:
reg = REG_RDX;
break;
case 4:
reg = REG_RCX;
break;
case 5:
reg = REG_R8;
break;
case 6:
reg = REG_R9;
break;
default:

trace_leave();

return 0;
}

trace_leave();
Expand All @@ -113,7 +119,7 @@ ADDRINT val_from_reg(CONTEXT *ctxt, unsigned int pid) {


VOID fn_call(CONTEXT *ctxt, FID fid, bool is_jump, ADDRINT inst_addr) {

trace_enter();

FID caller = call_stack.top();
Expand All @@ -138,7 +144,7 @@ VOID fn_call(CONTEXT *ctxt, FID fid, bool is_jump, ADDRINT inst_addr) {
new_param->pos = i;
if (IMG_Valid(IMG_FindByAddress(inst_addr))) {
if (IMG_IsMainExecutable(IMG_FindByAddress(inst_addr)))
new_param->from_main = IMG_IsMainExecutable(IMG_FindByAddress(inst_addr));
new_param->from_main = IMG_IsMainExecutable(IMG_FindByAddress(inst_addr));
} else {
new_param->from_main = 1;
}
Expand Down Expand Up @@ -170,7 +176,7 @@ VOID fn_call(CONTEXT *ctxt, FID fid, bool is_jump, ADDRINT inst_addr) {
}

VOID fn_icall(CONTEXT* ctxt, ADDRINT target, bool is_jump, ADDRINT inst_addr) {

trace_enter();

// Indirect call, we have to look up the function each time
Expand All @@ -193,7 +199,7 @@ VOID fn_icall(CONTEXT* ctxt, ADDRINT target, bool is_jump, ADDRINT inst_addr) {

VOID fn_ret(CONTEXT *ctxt, UINT32 fid) {
trace_enter();

counter += 1;

if (!call_stack.is_top_forgotten()) {
Expand Down Expand Up @@ -236,10 +242,10 @@ VOID fn_ret(CONTEXT *ctxt, UINT32 fid) {


void fn_registered(
FID fid,
unsigned int nb_param,
vector<bool> type_param
) {
FID fid,
unsigned int nb_param,
vector<bool> type_param
) {

trace_enter();

Expand Down Expand Up @@ -283,23 +289,23 @@ VOID Instruction(INS ins, VOID *v) {
ADDRINT addr = INS_DirectBranchOrCallTargetAddress(ins);
FID fid = fn_lookup_by_address(addr);
INS_InsertCall(ins,
IPOINT_BEFORE,
(AFUNPTR) fn_call,
IARG_CONST_CONTEXT,
IARG_UINT32, fid,
IARG_BOOL, false,
IARG_ADDRINT, inst_addr,
IARG_END);
IPOINT_BEFORE,
(AFUNPTR) fn_call,
IARG_CONST_CONTEXT,
IARG_UINT32, fid,
IARG_BOOL, false,
IARG_ADDRINT, inst_addr,
IARG_END);
}
else {
INS_InsertCall(ins,
IPOINT_BEFORE,
(AFUNPTR) fn_icall,
IARG_CONST_CONTEXT,
IARG_BRANCH_TARGET_ADDR,
IARG_BOOL, false,
IARG_ADDRINT, inst_addr,
IARG_END);
IPOINT_BEFORE,
(AFUNPTR) fn_icall,
IARG_CONST_CONTEXT,
IARG_BRANCH_TARGET_ADDR,
IARG_BOOL, false,
IARG_ADDRINT, inst_addr,
IARG_END);
}
}

Expand All @@ -318,10 +324,10 @@ VOID Instruction(INS ins, VOID *v) {

if (INS_IsRet(ins)) {
INS_InsertCall(ins,
IPOINT_BEFORE,
(AFUNPTR) fn_ret,
IARG_CONST_CONTEXT,
IARG_END);
IPOINT_BEFORE,
(AFUNPTR) fn_ret,
IARG_CONST_CONTEXT,
IARG_END);
}

return;
Expand All @@ -339,40 +345,69 @@ VOID Commence() {
if (ifile.is_open()) {
while (ifile) {
char m;
unsigned int nb_param = 0;
vector<bool> type_param;
string img_name = read_part(&m);
float rho = 1;
FID fid;

if (img_name.empty()) {
continue;
}
ADDRINT img_addr = atol(read_part(&m).c_str());
string name = read_part(&m);
type_param.push_back(true);
FID fid = fn_register(img_name, img_addr, name);
if (fid != FID_UNKNOWN) {
fn_registered(fid, 0, type_param);
}

type_param.clear();
img_name = read_part(&m);
if (img_name.empty()) {
continue;
}
img_addr = atol(read_part(&m).c_str());
name = read_part(&m);
if ( couple_mode ) {
type_param.push_back(true);

float rho = atof(read_part(&m).c_str());
unsigned char param_pos = atoi(read_part(&m).c_str());
fid = fn_register(img_name, img_addr, name);
if (fid != FID_UNKNOWN) {
fn_registered(fid, 0, type_param);
}

for (unsigned char pos = 0 ; pos < param_pos ; pos++) {
type_param.push_back(false);
}
type_param.push_back(true);
type_param.clear();
img_name = read_part(&m);
if (img_name.empty()) {
continue;
}
img_addr = atol(read_part(&m).c_str());
name = read_part(&m);

rho = atof(read_part(&m).c_str());
unsigned char param_pos = atoi(read_part(&m).c_str());

for (unsigned char pos = 0 ; pos < param_pos ; pos++) {
type_param.push_back(false);
}
type_param.push_back(true);

nb_param = param_pos + 1;
} else {
/* Read parameters */
while (ifile && m != '\n') {
string part = read_part(&m);
switch (part[0]) {
case 'A':
type_param.push_back(true);
break;
case 'I':
case 'V':
type_param.push_back(false);
break;
case 'F':
type_param.push_back(false);
break;
default:
type_param.push_back(false);
}
nb_param += 1;
}
}
if (rho > COUPLE_THRESHOLD) {
fid = fn_register(img_name, img_addr, name);

if (fid != FID_UNKNOWN) {
fn_registered(fid, param_pos, type_param);
fn_registered(fid, nb_param - 1, type_param);
}
}

Expand Down Expand Up @@ -425,7 +460,7 @@ VOID Fini(INT32 code, VOID *v) {
ofile.write((char *) &(p->pos), sizeof(p->pos));
ofile.write((char *) &(p->counter), sizeof(p->counter));
ofile.write((char *) &(p->from_main), sizeof(p->from_main));
// std::cerr << p->val << ":" << p->fid << ":" << p->pos << ":" << p->counter << endl;
// std::cerr << p->val << ":" << p->fid << ":" << p->pos << ":" << p->counter << endl;
}
ofile.close();

Expand All @@ -436,7 +471,7 @@ VOID Fini(INT32 code, VOID *v) {


int main(int argc, char * argv[]) {

param_addr = (bool **) malloc(NB_FN_MAX * sizeof(bool *));
is_instrumented = (bool *) calloc(NB_FN_MAX, sizeof(bool));
nb_p = (unsigned int *) calloc(NB_FN_MAX, sizeof(unsigned int));
Expand All @@ -451,7 +486,8 @@ int main(int argc, char * argv[]) {

ifile.open(KnobInputFile.Value().c_str());
ofile.open(KnobOutputFile.Value().c_str());

couple_mode = KnobCoupleMode.Value();

// INS_AddInstrumentFunction(Instruction, 0);
INS_AddInstrumentFunction(Instruction, 0);

Expand All @@ -464,7 +500,7 @@ int main(int argc, char * argv[]) {
PIN_AddFiniFunction(Fini, 0);

PIN_StartProgram();

return 0;
}

Expand Down

0 comments on commit b9a5bbc

Please sign in to comment.