-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
106 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include "jni.h" | ||
|
||
#include <dlfcn.h> | ||
|
||
#include <string> | ||
#include <stdexcept> | ||
|
||
// TODO(meme) make this not gross | ||
struct AgentOptions { | ||
void* (*dlopen_)(const char *filename, int flags); | ||
void* (*dlsym_)(void *handle, const char *symbol); | ||
int (*dlclose_)(void *handle); | ||
|
||
AgentOptions(void* (*dlopen_)(const char*, int), void* (*dlsym_)(void*, const char*), int (*dlclose_)(void*)) | ||
: dlopen_(dlopen_), dlsym_(dlsym_), dlclose_(dlclose_) { } | ||
}; | ||
|
||
class Agent { | ||
private: | ||
std::string path_; | ||
AgentOptions const& options_; | ||
|
||
void *handle_; | ||
jint (*JNI_OnLoad_)(JavaVM *vm, void *reserved); | ||
void (*JNI_OnUnload_)(JavaVM *vm, void *reserved); | ||
|
||
public: | ||
Agent(std::string path, AgentOptions const& options); | ||
|
||
bool open(); | ||
|
||
bool close(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "jni.h" | ||
#include "agent.h" | ||
|
||
#include <vector> | ||
|
||
class FakeJVM { | ||
private: | ||
JavaVM vm_; | ||
std::vector<Agent*> agents_; | ||
|
||
public: | ||
static FakeJVM instance; | ||
|
||
FakeJVM(); | ||
|
||
JavaVM* getFakeJVM() { return &vm_; } | ||
|
||
void addAgent(Agent* agent) { | ||
agents_.push_back(agent); | ||
agent->open(); | ||
} | ||
|
||
void close() { | ||
for (auto& agent : agents_) { | ||
agent->close(); | ||
} | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# All sources for the JNI target (only STATIC is supported) | ||
add_library(jni STATIC | ||
fake_jvm.cpp | ||
fake_jni.cpp | ||
agent.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "agent.h" | ||
#include "fake_jni.h" | ||
|
||
#include <iostream> | ||
|
||
Agent::Agent(std::string path, AgentOptions const& options) : path_(path), options_(options) { | ||
FakeJVM::instance.addAgent(this); | ||
} | ||
|
||
bool Agent::open() { | ||
handle_ = options_.dlopen_(path_.c_str(), RTLD_LAZY); | ||
|
||
if (!handle_) { | ||
throw std::runtime_error("Agent failed to load using `dlopen`"); | ||
} | ||
|
||
(void*&) JNI_OnLoad_ = options_.dlsym_(handle_, "JNI_OnLoad"); | ||
(void*&) JNI_OnUnload_ = options_.dlsym_(handle_, "JNI_OnUnload"); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
if (!JNI_OnLoad_ || !JNI_OnUnload_) { | ||
throw std::runtime_error("JNI_OnLoad or JNI_OnUnload not found"); | ||
} | ||
|
||
return JNI_OnLoad_(FakeJVM::instance.getFakeJVM(), nullptr) > JNI_VERSION_1_8; | ||
} | ||
|
||
bool Agent::close() { | ||
JNI_OnUnload_(FakeJVM::instance.getFakeJVM(), nullptr); | ||
return options_.dlclose_(handle_) ? false : true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "fake_jvm.h" | ||
#include "fake_jni.h" | ||
|
||
FakeJVM FakeJVM::instance; | ||
|
||
|
Not all binaries have JNI_OnUnload -- this one should be optional!