Skip to content

Commit

Permalink
Whitespace cleanup (#54)
Browse files Browse the repository at this point in the history
* Whitespace cleanup

* Fix include reordering issues
  • Loading branch information
hainest authored Jan 31, 2024
1 parent d2ae1bc commit 5bbac65
Show file tree
Hide file tree
Showing 55 changed files with 2,452 additions and 2,606 deletions.
53 changes: 25 additions & 28 deletions CFGraph/CFGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "CFG.h"
#include "CodeObject.h"

#include <sstream>
#include <string>
#include <unordered_set>
Expand All @@ -13,14 +14,14 @@
namespace dp = Dyninst::ParseAPI;
namespace st = Dyninst::SymtabAPI;

int main(int argc, char *argv[]) {
if (argc < 2 || argc > 3) {
int main(int argc, char* argv[]) {
if(argc < 2 || argc > 3) {
std::cerr << "Usage: " << argv[0] << " executable\n";
return -1;
}

auto *sts = new dp::SymtabCodeSource(argv[1]);
auto *co = new dp::CodeObject(sts);
auto* sts = new dp::SymtabCodeSource(argv[1]);
auto* co = new dp::CodeObject(sts);

// Parse the binary
co->parse();
Expand All @@ -32,20 +33,19 @@ int main(int argc, char *argv[]) {

// Remove compiler-generated and system functions
{
auto ignore = [&all](dp::Function const *f) {
auto const &name = f->name();
auto ignore = [&all](dp::Function const* f) {
auto const& name = f->name();
bool const starts_with_underscore = name[0] == '_';
bool const ends_with_underscore = name[name.length() - 1] == '_';
bool const is_dummy = name == "frame_dummy";
bool const is_clones = name.find("tm_clones") != std::string::npos;
return starts_with_underscore || ends_with_underscore || is_dummy ||
is_clones;
return starts_with_underscore || ends_with_underscore || is_dummy || is_clones;
};

// 'funclist' is a std::set which has only const iterators
auto i = all.begin();
while (i != all.end()) {
if (ignore(*i)) {
while(i != all.end()) {
if(ignore(*i)) {
i = all.erase(i);
} else {
++i;
Expand All @@ -56,47 +56,44 @@ int main(int argc, char *argv[]) {
std::unordered_set<Dyninst::Address> seen;

int cluster_index = 0;
for (auto const *f : all) {
for(auto const* f : all) {
// Make a cluster for nodes of this function
std::cout << "\t subgraph cluster_" << cluster_index << " { \n\t\t label=\""
<< f->name() << "\"; \n\t\t color=blue;" << '\n';
std::cout << "\t subgraph cluster_" << cluster_index << " { \n\t\t label=\"" << f->name()
<< "\"; \n\t\t color=blue;" << '\n';

std::cout << "\t\t\"" << std::hex << f->addr() << std::dec
<< "\" [shape=box";
std::cout << "\t\t\"" << std::hex << f->addr() << std::dec << "\" [shape=box";

if (f->retstatus() == dp::NORETURN)
if(f->retstatus() == dp::NORETURN)
std::cout << ",color=red";

std::cout << "]" << '\n';

// Label functions by name
std::cout << "\t\t\"" << std::hex << f->addr() << std::dec
<< "\" [label = \"" << f->name() << "\\n"
std::cout << "\t\t\"" << std::hex << f->addr() << std::dec << "\" [label = \"" << f->name() << "\\n"
<< std::hex << f->addr() << std::dec << "\"];" << '\n';

std::stringstream edgeoutput;

for (dp::Block *b : f->blocks()) {
for(dp::Block* b : f->blocks()) {
// Don't revisit blocks in shared code
if (seen.count(b->start()) > 0)
if(seen.count(b->start()) > 0)
continue;
seen.insert(b->start());

std::cout << "\t\t\"" << std::hex << b->start() << std::dec << "\";"
<< '\n';
std::cout << "\t\t\"" << std::hex << b->start() << std::dec << "\";" << '\n';

for (dp::Edge *e : b->targets()) {
if (!e)
for(dp::Edge* e : b->targets()) {
if(!e)
continue;
std::string s;
if (e->type() == dp::CALL)
if(e->type() == dp::CALL)
s = " [color=blue]";
else if (e->type() == dp::RET)
else if(e->type() == dp::RET)
s = " [color=green]";

// Store the edges somewhere to be printed outside of the cluster
edgeoutput << "\t\"" << std::hex << e->src()->start() << "\" -> \""
<< e->trg()->start() << "\"" << s << '\n';
edgeoutput << "\t\"" << std::hex << e->src()->start() << "\" -> \"" << e->trg()->start() << "\"" << s
<< '\n';
}
}
// End cluster
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.13.0)
project(examples)

find_package(Dyninst 12.3.0 REQUIRED COMPONENTS dyninstAPI dynC_API parseAPI symtabAPI patchAPI)
find_package(Dyninst 12.3.0 REQUIRED COMPONENTS dyninstAPI dynC_API parseAPI
symtabAPI patchAPI)

add_executable(InterestingProgram common/mutatees/InterestingProgram.cpp)

Expand Down
3 changes: 2 additions & 1 deletion DynC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_compile_options(dync_mutatee PRIVATE ${EXAMPLES_WARNING_FLAGS})

add_executable(dync_mutator mutator.cpp)
target_compile_options(dync_mutator PRIVATE ${EXAMPLES_WARNING_FLAGS})
target_link_libraries(dync_mutator PRIVATE Dyninst::dynC_API Dyninst::dyninstAPI)
target_link_libraries(dync_mutator PRIVATE Dyninst::dynC_API
Dyninst::dyninstAPI)

install(FILES testStatements testStatements2 DESTINATION ${PROJECT_BINARY_DIR})
14 changes: 7 additions & 7 deletions DynC/mutatee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ int arrayField = array[2];

struct myStructType {
int i;
char const *s;
char const *sa[4];
char const* s;
char const* sa[4];
} mystruct = {3, "house", {"how", "now", "brown", "cow"}};

int count(int i);
Expand All @@ -20,7 +20,7 @@ int main() {

int i = 0;
int r = 0;
while (i < 10) {
while(i < 10) {
i = count(i);
++hi;
r = hi * 10;
Expand All @@ -31,24 +31,24 @@ int main() {
void hello() {}

int count(int i) {
if (i % 2 == 0) {
if(i % 2 == 0) {
hello();
}
array[i % 5]++;
return i + 1;
}

int count(int i, char *n) {
int count(int i, char* n) {
printf("%s", n);
return i + 1;
}

int printfWrapper(char *s) {
int printfWrapper(char* s) {
printf("%s", s);
return 1;
}

int count(char *s) {
int count(char* s) {
printf("%s\n", s);
return 1;
}
72 changes: 36 additions & 36 deletions DynC/mutator.cpp
Original file line number Diff line number Diff line change
@@ -1,111 +1,111 @@
#include <fstream>
#include <iostream>
#include <string>

#include "BPatch.h"
#include "BPatch_function.h"
#include "BPatch_process.h"
#include "BPatch_snippet.h"
#include "dynC.h"

#include <fstream>
#include <iostream>
#include <string>

#define STATEMENT_PATH "testStatements"
#define STATEMENT_PATH_2 "testStatements2"

const char *MUTATEE_PATH = "dync_mutatee";
const char *MUTATEE_ARGS[3];
const char *MODULE_NAME = "mutatee.cpp";
const char* MUTATEE_PATH = "dync_mutatee";
const char* MUTATEE_ARGS[3];
const char* MODULE_NAME = "mutatee.cpp";

BPatch bpatch;

int main() {

std::ifstream myfile(STATEMENT_PATH);
if(!myfile) {
std::cerr << "Unable to open '" << STATEMENT_PATH << "'\n";
return -1;
std::cerr << "Unable to open '" << STATEMENT_PATH << "'\n";
return -1;
}

std::ifstream myfile2(STATEMENT_PATH_2);
if(!myfile2) {
std::cerr << "Unable to open '" << STATEMENT_PATH_2 << "'\n";
return -1;
std::cerr << "Unable to open '" << STATEMENT_PATH_2 << "'\n";
return -1;
}

FILE *myCFILE = fopen(STATEMENT_PATH, "r");
FILE* myCFILE = fopen(STATEMENT_PATH, "r");

BPatch_addressSpace *appProc;
BPatch_addressSpace* appProc;
bool rewrite = false;

if (rewrite) {
if(rewrite) {
appProc = bpatch.openBinary(MUTATEE_PATH, true);
} else {
appProc = bpatch.processCreate(MUTATEE_PATH, MUTATEE_ARGS);
}
if (!appProc) {
std::cerr << "Unable to create an address space\n";
return -1;
if(!appProc) {
std::cerr << "Unable to create an address space\n";
return -1;
}

BPatch_image *appImage = appProc->getImage();
BPatch_module *mutatee = appImage->findModule(MODULE_NAME);
BPatch_image* appImage = appProc->getImage();
BPatch_module* mutatee = appImage->findModule(MODULE_NAME);

if (!mutatee) {
if(!mutatee) {
std::cerr << "Bad Mutatee!\n";
return -1;
}

appProc->malloc(*appImage->findType("long"), std::string("globalVar"));

const std::vector<BPatch_function *> *functions = mutatee->getProcedures();
std::vector<BPatch_point *> *entry_points = (*functions)[0]->findPoint(BPatch_entry);
std::vector<BPatch_point *> *exit_points = (*functions)[0]->findPoint(BPatch_exit);
const std::vector<BPatch_function*>* functions = mutatee->getProcedures();
std::vector<BPatch_point*>* entry_points = (*functions)[0]->findPoint(BPatch_entry);
std::vector<BPatch_point*>* exit_points = (*functions)[0]->findPoint(BPatch_exit);

for (auto *f : *mutatee->getProcedures()) {
for(auto* f : *mutatee->getProcedures()) {
entry_points->push_back((*f->findPoint(BPatch_entry))[0]);
exit_points->push_back((*f->findPoint(BPatch_exit))[0]);
}

/////////////////////////////////////////////////////////
std::map<BPatch_point *, BPatch_snippet *> *entry_snippets, exit_snippets;
std::map<BPatch_point*, BPatch_snippet*>*entry_snippets, exit_snippets;
entry_snippets = dynC_API::createSnippet(myCFILE, *entry_points);

if (!entry_snippets) {
if(!entry_snippets) {
std::cerr << "entry_snippets is null.\n";
return -1;
}

for (auto const &entry : *entry_snippets) {
if (!entry.first) {
for(auto const& entry : *entry_snippets) {
if(!entry.first) {
std::cerr << "Entry point is null.\n";
return -1;
}
if (!entry.second) {
if(!entry.second) {
std::cerr << "Entry snippet is null.\n";
return -1;
}
std::cout << "Snippet inserted\n";
std::cout << "Point's function is " << entry.first->getFunction()->getName() << "\n";
BPatchSnippetHandle *handle = appProc->insertSnippet(*entry.second, *entry.first);
BPatchSnippetHandle* handle = appProc->insertSnippet(*entry.second, *entry.first);
std::cout << "Handle is " << ((!handle) ? "null" : "not null") << "\n";
}

if (!rewrite) {
BPatch_process *aProc = static_cast<BPatch_process *>(appProc);
if(!rewrite) {
BPatch_process* aProc = static_cast<BPatch_process*>(appProc);
aProc->continueExecution();

while (!aProc->isTerminated()) {
while(!aProc->isTerminated()) {
bpatch.waitForStatusChange();
}

if (aProc->terminationStatus() == ExitedNormally) {
if(aProc->terminationStatus() == ExitedNormally) {
printf("Application exited with code %d\n", aProc->getExitCode());
} else if (aProc->terminationStatus() == ExitedViaSignal) {
} else if(aProc->terminationStatus() == ExitedViaSignal) {
printf("!!! Application exited with signal %d\n", aProc->getExitSignal());
} else {
printf("Unknown application exit\n");
}
} else {
BPatch_binaryEdit *aProc = static_cast<BPatch_binaryEdit *>(appProc);
BPatch_binaryEdit* aProc = static_cast<BPatch_binaryEdit*>(appProc);
aProc->writeFile("myMutatee.out");
}
}
Loading

0 comments on commit 5bbac65

Please sign in to comment.