Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update:提供c++ api,使得Raphael能够在native c++命令行程序或者so中使用 #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/src/main/cpp/HookProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ void registerPltGotProxy(JNIEnv *env, jstring regex) {
}
}

void registerInlineProxy(JNIEnv *env) {
void registerInlineProxy() {
invoke_je_free();

const int PROXY_MAPPING_LENGTH = sizeof(sInline) / sizeof(sInline[0]);
Expand Down
5 changes: 2 additions & 3 deletions library/src/main/cpp/PltGotHookProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,12 @@ static void try_pltgot_hook_on_soload(const char *filename) {
}
}

int registerSoLoadProxy(JNIEnv *env, jstring focused) {
int registerSoLoadProxy(const char* focused) {
api_level = android_get_device_api_level();

if (focused != NULL) {
const char *focused_reg = (char *) env->GetStringUTFChars(focused, 0);
const char *focused_reg = focused;
use_regex = regcomp(&focused_regex, focused_reg, REG_EXTENDED|REG_NOSUB) == 0;
env->ReleaseStringUTFChars(focused, focused_reg);
}

if (api_level >= __ANDROID_API_N__ && api_level < __ANDROID_API_O__) {
Expand Down
55 changes: 23 additions & 32 deletions library/src/main/cpp/Raphael.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,92 +16,83 @@

#include <cstring>
#include <dirent.h>

#include "Cache.h"
#include "Raphael.h"
#include "HookProxy.h"
#include "MemoryCache.h"
#include "PltGotHookProxy.h"

//**************************************************************************************************
void Raphael::start(JNIEnv *env, jobject obj, jint configs, jstring space, jstring regex) {
const char *string = (char *) env->GetStringUTFChars(space, 0);
size_t length = strlen(string);
mSpace = (char *) malloc(length+1);
memset((void *)mSpace, 0, length+1);
memcpy((void *) mSpace, string, length);
env->ReleaseStringUTFChars(space, string);

mCache = new MemoryCache(mSpace);
void Raphael::start(int configs, const char* space, const char* regex) {
mSpace = space;
mCache = new MemoryCache(mSpace.c_str());
update_configs(mCache, 0);

if (regex != nullptr) {
registerSoLoadProxy(env, regex);
registerSoLoadProxy(regex);
} else {
registerInlineProxy(env);
registerInlineProxy();
}

mCache->reset();
pthread_key_create(&guard, nullptr);
LOGGER("start >>> %#x, %s", (uint) configs, mSpace);
LOGGER("start >>> %#x, %s", (uint) configs, mSpace.c_str());
update_configs(mCache, configs);
}

void Raphael::stop(JNIEnv *env, jobject obj) {
void Raphael::stop() {
update_configs(nullptr, 0);
print(env, obj);
print();

delete mCache;
mCache = nullptr;

xh_core_clear();
pthread_key_delete(guard);
LOGGER("stop >>> %s", mSpace);

delete mSpace;
mSpace = nullptr;
LOGGER("stop >>> %s", mSpace.c_str());
}

void Raphael::print(JNIEnv *env, jobject obj) {
void Raphael::print() {
pthread_setspecific(guard, (void *) 1);

clean_cache(env);
clean_cache();
mCache->print();
dump_system(env);
dump_system();

LOGGER("print >>> %s", mSpace);
LOGGER("print >>> %s", mSpace.c_str());
pthread_setspecific(guard, (void *) 0);
}

void Raphael::clean_cache(JNIEnv *env) {
void Raphael::clean_cache() {
DIR *pDir;
struct dirent *pDirent;

char path[MAX_BUFFER_SIZE];
if ((pDir = opendir(mSpace)) != NULL) {
if ((pDir = opendir(mSpace.c_str())) != NULL) {
while ((pDirent = readdir(pDir)) != NULL) {
if (strcmp(pDirent->d_name, ".") != 0 && strcmp(pDirent->d_name, "..") != 0) {
if (snprintf(path, MAX_BUFFER_SIZE, "%s/%s", mSpace, pDirent->d_name) < MAX_BUFFER_SIZE) {
if (snprintf(path, MAX_BUFFER_SIZE, "%s/%s", mSpace.c_str(), pDirent->d_name) < MAX_BUFFER_SIZE) {
remove(path);
}
}
}
closedir(pDir);
} else if (mkdir(mSpace, 777) != 0) {
LOGGER("create %s failed, please check permissions", mSpace);
} else if (mkdir(mSpace.c_str(), 777) != 0) {
LOGGER("create %s failed, please check permissions", mSpace.c_str());
} else {
LOGGER("create %s success", mSpace);
LOGGER("create %s success", mSpace.c_str());
}
}

void Raphael::dump_system(JNIEnv *env) {
void Raphael::dump_system() {
char path[MAX_BUFFER_SIZE];
if (snprintf(path, MAX_BUFFER_SIZE, "%s/maps", mSpace) >= MAX_BUFFER_SIZE) {
if (snprintf(path, MAX_BUFFER_SIZE, "%s/maps", mSpace.c_str()) >= MAX_BUFFER_SIZE) {
return;
}

FILE *target = fopen(path, "w");
if (target == nullptr) {
LOGGER("dump maps failed, can't open %s/maps", mSpace);
LOGGER("dump maps failed, can't open %s/maps", mSpace.c_str());
return;
}

Expand Down
23 changes: 13 additions & 10 deletions library/src/main/cpp/Raphael.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@
#ifndef RAPHAEL_H
#define RAPHAEL_H

#include <jni.h>
#include "Cache.h"
#include <string>


#define RAPHAEL_API __attribute__((visibility("default")))


#define MAP64_MODE 0x00800000
#define ALLOC_MODE 0x00400000
#define DEPTH_MASK 0x001F0000
#define LIMIT_MASK 0x0000FFFF

class Raphael {
class Cache;
class RAPHAEL_API Raphael {
public:
void start(JNIEnv *env, jobject obj, jint configs, jstring space, jstring regex);
void stop(JNIEnv *env, jobject obj);
void print(JNIEnv *env, jobject obj);
void start(int configs, const char* space, const char* regex);
void stop();
void print();
private:
void clean_cache(JNIEnv *env);
void dump_system(JNIEnv *env);
void clean_cache();
void dump_system();
private:
char *mSpace;
std::string mSpace;
Cache *mCache;
};

Expand Down
13 changes: 10 additions & 3 deletions library/src/main/cpp/xloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@
Raphael* sRaphael = new Raphael();

void start(JNIEnv *env, jobject obj, jint configs, jstring space, jstring regex) {
sRaphael->start(env, obj, configs, space, regex);
const char *spacestring = (char *) env->GetStringUTFChars(space, 0);

const char *regexstring = (char *) env->GetStringUTFChars(regex, 0);

sRaphael->start(configs, spacestring, regexstring);

env->ReleaseStringUTFChars(space, spacestring);
env->ReleaseStringUTFChars(regex, regexstring);
}

void stop(JNIEnv *env, jobject obj) {
sRaphael->stop(env, obj);
sRaphael->stop();
}

void print(JNIEnv *env, jobject obj) {
sRaphael->print(env, obj);
sRaphael->print();
}

static const JNINativeMethod sMethods[] = {
Expand Down