Skip to content

Commit

Permalink
Complex lib target for private member fn hooking
Browse files Browse the repository at this point in the history
This is slightly more complex than the basic target, because the private
member function to be hooked / called makes use of `this`.

That means an actual instance of the class must be constructed and setup
according to the calling convention of the private function before the
call can be made.
  • Loading branch information
Hamled committed May 4, 2019
1 parent 020db63 commit 2fb3d3a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cpp/.gitignore
@@ -1,3 +1,5 @@
/app/app_basic
/app/app_complex
/target/libtarget_basic.so
/target/libtarget_complex.so
/hooks/call_private_basic
6 changes: 6 additions & 0 deletions cpp/app/build.sh
Expand Up @@ -5,3 +5,9 @@ if [[ -f ./app_basic ]]; then
fi

clang++ -I../target/include/ -L../target/ -ltarget_basic ./src/app_basic.cpp -o app_basic

if [[ -f ./app_complex ]]; then
rm ./app_complex
fi

clang++ -I../target/include/ -L../target/ -ltarget_complex ./src/app_complex.cpp -o app_complex
16 changes: 16 additions & 0 deletions cpp/app/src/app_complex.cpp
@@ -0,0 +1,16 @@
#include <iostream>
#include <cstdlib>
#include "target_complex.hpp"

int main(int argc, char **argv) {
if(argc < 2) {
std::cout << "Usage: " << argv[0] << " <integer>" << std::endl;
return EXIT_FAILURE;
}

int input = std::atoi(argv[1]);
Target t = Target();
t.maybe_say(input);

return EXIT_SUCCESS;
}
6 changes: 6 additions & 0 deletions cpp/target/build.sh
Expand Up @@ -5,3 +5,9 @@ if [[ -f ./libtarget_basic.so ]]; then
fi

clang++ -shared -I./include/ ./src/target_basic.cpp -o ./libtarget_basic.so

if [[ -f ./libtarget_complex.so ]]; then
rm ./libtarget_complex.so
fi

clang++ -shared -I./include/ ./src/target_complex.cpp -o ./libtarget_complex.so
16 changes: 16 additions & 0 deletions cpp/target/include/target_complex.hpp
@@ -0,0 +1,16 @@
#pragma once

// Complex target with private member function
// which *does* reference `this` to access private
// member initialized by constructor.

class Target {
public:
void maybe_say(int num);
Target();
~Target();

private:
void say_something();
const char *message;
};
21 changes: 21 additions & 0 deletions cpp/target/src/target_complex.cpp
@@ -0,0 +1,21 @@
#include <iostream>
#include <cstdlib>
#include "target_complex.hpp"

void __attribute__((__noinline__)) Target::say_something() {
std::cout << this->message << std::endl;
}

void Target::maybe_say(int num) {
if(num == std::rand()) {
this->say_something();
}
}

Target::Target() {
this->message = "This is really something!";
}

Target::~Target() {
this->message = NULL;
}

0 comments on commit 2fb3d3a

Please sign in to comment.