Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
adapt to Riru v8
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Oct 30, 2018
1 parent 04410e2 commit 590cbb5
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ android {

task zip(type: Exec) {
workingDir '..'
commandLine 'sh', 'build.sh', project.name, 'v5'
commandLine 'sh', 'build.sh', project.name, 'v6'
}
2 changes: 1 addition & 1 deletion jni/main/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ LOCAL_STATIC_LIBRARIES := xhook
LOCAL_LDLIBS += -ldl -llog
LOCAL_LDFLAGS := -Wl

LOCAL_SRC_FILES:= main.cpp hook.cpp misc.cpp
LOCAL_SRC_FILES:= main.cpp hook.cpp misc.cpp riru.c

include $(BUILD_SHARED_LIBRARY)
18 changes: 17 additions & 1 deletion jni/main/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sys/system_properties.h>
#include <xhook/xhook.h>

#include "riru.h"
#include "logging.h"

static const char *sim_operator_numeric = "310030";
Expand Down Expand Up @@ -65,11 +66,26 @@ void install_hook(const char *package_name, int user) {
XHOOK_REGISTER(__system_property_get);

char sdk[PROP_VALUE_MAX + 1];
if (__system_property_get("ro.build.version.sdk", sdk) > 0 && atoi(sdk) >= 28)
int sdkLevel = 0;
if (__system_property_get("ro.build.version.sdk", sdk) > 0 && (sdkLevel = atoi(sdk)) >= 28)
XHOOK_REGISTER(_ZN7android4base11GetPropertyERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_);

if (xhook_refresh(0) == 0)
xhook_clear();
else
LOGE("failed to refresh hook");

if (riru_get_version() >= 8) {
void *f = riru_get_func("__system_property_get");
if (f != nullptr) old___system_property_get = (int (*)(const char *, char *)) f;
riru_set_func("__system_property_get", (void *) new___system_property_get);

if (sdkLevel >= 28) {
f = riru_get_func("_ZN7android4base11GetPropertyERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_");
if (f != nullptr) old__ZN7android4base11GetPropertyERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_ = (std::string (*)(const std::string &, const std::string &)) f;

riru_set_func("_ZN7android4base11GetPropertyERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_",
(void *) new__ZN7android4base11GetPropertyERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_);
}
}
}
1 change: 1 addition & 0 deletions jni/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <utility>
#include <string>
#include <sys/system_properties.h>
#include <riru.h>

#include "logging.h"
#include "hook.h"
Expand Down
76 changes: 76 additions & 0 deletions jni/main/riru.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <dlfcn.h>
#include <memory.h>
#include <logging.h>

#ifdef __LP64__
#define LIB "/system/lib64/libmemtrack.so"
#else
#define LIB "/system/lib/libmemtrack.so"
#endif

static void *riru_handle;
static char *riru_module_name;

static void *get_handle() {
if (riru_handle == NULL)
riru_handle = dlopen(LIB, RTLD_NOW | RTLD_GLOBAL);

return riru_handle;
}

const char *riru_get_module_name() {
return riru_module_name;
}

void riru_set_module_name(const char *name) {
riru_module_name = strdup(name);
}

int riru_get_version() {
static void **sym;
void *handle;
if ((handle = get_handle()) == NULL) return -1;
if (sym == NULL) sym = dlsym(handle, "riru_get_version");
if (sym) return ((int (*)()) sym)();
return -1;
}

void *riru_get_func(const char *name) {
static void **sym;
void *handle;
if ((handle = get_handle()) == NULL) return NULL;
if (sym == NULL) sym = dlsym(handle, "riru_get_func");
if (sym) return ((void *(*)(const char *, const char *)) sym)(riru_get_module_name(), name);
return NULL;
}

void *riru_get_native_method_func(const char *className, const char *name, const char *signature) {
static void **sym;
void *handle;
if ((handle = get_handle()) == NULL) return NULL;
if (sym == NULL) sym = dlsym(handle, "riru_get_native_method_func");
if (sym)
return ((void *(*)(const char *, const char *, const char *, const char *)) sym)(
riru_get_module_name(), className, name, signature);
return NULL;
}

void riru_set_func(const char *name, void *func) {
static void **sym;
void *handle;
if ((handle = get_handle()) == NULL) return;
if (sym == NULL) sym = dlsym(handle, "riru_set_func");
if (sym)
((void *(*)(const char *, const char *, void *)) sym)(riru_get_module_name(), name, func);
}

void riru_set_native_method_func(const char *className, const char *name, const char *signature,
void *func) {
static void **sym;
void *handle;
if ((handle = get_handle()) == NULL) return;
if (sym == NULL) sym = dlsym(handle, "riru_set_native_method_func");
if (sym)
((void *(*)(const char *, const char *, const char *, const char *, void *)) sym)(
riru_get_module_name(), className, name, signature, func);
}
57 changes: 57 additions & 0 deletions jni/main/riru.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef RIRU_H
#define RIRU_H

#ifdef __cplusplus
extern "C" {
#endif
__attribute__((visibility("default"))) void riru_set_module_name(const char *name);

/**
* Get Riru version.
*
* @return Riru version
*/
int riru_get_version();

/*
* Get new_func address from last module which hook func.
* Use this as your old_func if you want to hook func.
*
* @param name a unique name
* @return new_func from last module or null
*/
void *riru_get_func(const char *name);

/*
* Java native version of riru_get_func.
*
* @param className class name
* @param name method name
* @param signature method signature
* @return new_func address from last module or original address
*/
void *riru_get_native_method_func(const char *className, const char *name, const char *signature);

/*
* Set new_func address for next module which wants to hook func.
*
* @param name a unique name
* @param func your new_func address
*/
void riru_set_func(const char *name, void *func);

/*
* Java native method version of riru_set_func.
*
* @param className class name
* @param name method name
* @param signature method signature
* @param func your new_func address
*/
void riru_set_native_method_func(const char *className, const char *name, const char *signature,
void *func);
#ifdef __cplusplus
}
#endif

#endif
4 changes: 2 additions & 2 deletions template_override/module.prop
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
id=riru_location_report_enabler
name=Riru - Location Report Enabler
version=v5
versionCode=5
version=v6
versionCode=6
author=Rikka
description=Enable location report by hook system_property_get. Require Riru - Core installed.
minMagisk=17000
4 changes: 2 additions & 2 deletions template_override/riru_module.prop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Location Report Enabler
version=v5
versionCode=5
version=v6
versionCode=6
author=Rikka
description=Enable location report by hook system_property_get. Require Riru - Core installed.

0 comments on commit 590cbb5

Please sign in to comment.