3
3
#include < riru.h>
4
4
#include < malloc.h>
5
5
#include < cstring>
6
+ #include < config.h>
6
7
#include < pthread.h>
7
8
#include " hook.h"
8
9
9
10
static void forkAndSpecializePre (
10
- JNIEnv *env, jclass clazz, jint *_uid , jint *gid, jintArray *gids, jint *runtimeFlags,
11
+ JNIEnv *env, jclass clazz, jint *uid , jint *gid, jintArray *gids, jint *runtimeFlags,
11
12
jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName,
12
13
jintArray *fdsToClose, jintArray *fdsToIgnore, jboolean *is_child_zygote,
13
- jstring *instructionSet, jstring *appDataDir, jboolean *isTopApp,
14
- jobjectArray *pkgDataInfoList,
15
- jobjectArray *whitelistedDataInfoList, jboolean *bindMountAppDataDirs,
16
- jboolean *bindMountAppStorageDirs) {
17
- enable_hack = isGame (env, *appDataDir);
14
+ jstring *instructionSet, jstring *appDataDir, jboolean *isTopApp, jobjectArray *pkgDataInfoList,
15
+ jobjectArray *whitelistedDataInfoList, jboolean *bindMountAppDataDirs, jboolean *bindMountAppStorageDirs) {
16
+ // Called "before" com_android_internal_os_Zygote_nativeForkAndSpecialize in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
17
+ // Parameters are pointers, you can change the value of them if you want
18
+ // Some parameters are not exist is older Android versions, in this case, they are null or 0
19
+ enable_hack = isGame (env, *appDataDir);
18
20
}
19
21
20
22
static void forkAndSpecializePost (JNIEnv *env, jclass clazz, jint res) {
23
+ // Called "after" com_android_internal_os_Zygote_nativeForkAndSpecialize in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
24
+ // "res" is the return value of com_android_internal_os_Zygote_nativeForkAndSpecialize
25
+
21
26
if (res == 0 ) {
22
- // in app process
27
+ // In app process
23
28
if (enable_hack) {
24
29
int ret;
25
30
pthread_t ntid;
26
31
if ((ret = pthread_create (&ntid, nullptr , hack_thread, nullptr ))) {
27
32
LOGE (" can't create thread: %s\n " , strerror (ret));
28
33
}
29
34
}
35
+ // When unload allowed is true, the module will be unloaded (dlclose) by Riru
36
+ // If this modules has hooks installed, DONOT set it to true, or there will be SIGSEGV
37
+ // This value will be automatically reset to false before the "pre" function is called
38
+ riru_set_unload_allowed (false );
30
39
} else {
31
- // in zygote process, res is child pid
32
- // don't print log here, see https://github.com/RikkaApps/Riru/blob/77adfd6a4a6a81bfd20569c910bc4854f2f84f5e/riru-core/jni/main/jni_native_method.cpp#L55-L66
40
+ // In zygote process
33
41
}
34
42
}
35
43
36
44
static void specializeAppProcessPre (
37
- JNIEnv *env, jclass clazz, jint *_uid , jint *gid, jintArray *gids, jint *runtimeFlags,
45
+ JNIEnv *env, jclass clazz, jint *uid , jint *gid, jintArray *gids, jint *runtimeFlags,
38
46
jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName,
39
47
jboolean *startChildZygote, jstring *instructionSet, jstring *appDataDir,
40
48
jboolean *isTopApp, jobjectArray *pkgDataInfoList, jobjectArray *whitelistedDataInfoList,
41
49
jboolean *bindMountAppDataDirs, jboolean *bindMountAppStorageDirs) {
42
- // added from Android 10, but disabled at least in Google Pixel devices
50
+ // Called "before" com_android_internal_os_Zygote_nativeSpecializeAppProcess in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
51
+ // Parameters are pointers, you can change the value of them if you want
52
+ // Some parameters are not exist is older Android versions, in this case, they are null or 0
43
53
}
44
54
45
55
static void specializeAppProcessPost (
46
56
JNIEnv *env, jclass clazz) {
47
- // added from Android 10, but disabled at least in Google Pixel devices
57
+ // Called "after" com_android_internal_os_Zygote_nativeSpecializeAppProcess in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
58
+
59
+ // When unload allowed is true, the module will be unloaded (dlclose) by Riru
60
+ // If this modules has hooks installed, DONOT set it to true, or there will be SIGSEGV
61
+ // This value will be automatically reset to false before the "pre" function is called
62
+ riru_set_unload_allowed (true );
48
63
}
49
64
50
65
static void forkSystemServerPre (
51
66
JNIEnv *env, jclass clazz, uid_t *uid, gid_t *gid, jintArray *gids, jint *runtimeFlags,
52
67
jobjectArray *rlimits, jlong *permittedCapabilities, jlong *effectiveCapabilities) {
53
-
68
+ // Called "before" com_android_internal_os_Zygote_forkSystemServer in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
69
+ // Parameters are pointers, you can change the value of them if you want
70
+ // Some parameters are not exist is older Android versions, in this case, they are null or 0
54
71
}
55
72
56
73
static void forkSystemServerPost (JNIEnv *env, jclass clazz, jint res) {
74
+ // Called "after" com_android_internal_os_Zygote_forkSystemServer in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
75
+
57
76
if (res == 0 ) {
58
- // in system server process
77
+ // In system server process
59
78
} else {
60
- // in zygote process, res is child pid
61
- // don't print log here, see https://github.com/RikkaApps/Riru/blob/77adfd6a4a6a81bfd20569c910bc4854f2f84f5e/riru-core/jni/main/jni_native_method.cpp#L55-L66
79
+ // In zygote process
62
80
}
63
81
}
64
82
65
- static int shouldSkipUid (int uid) {
66
- // by default, Riru only call module functions in "normal app processes" (10000 <= uid % 100000 <= 19999)
67
- // false = don't skip
68
- return false ;
69
- }
70
-
71
83
static void onModuleLoaded () {
72
- // called when the shared library of Riru core is loaded
84
+ // Called when this library is loaded and "hidden" by Riru (see Riru's hide.cpp)
85
+
86
+ // If you want to use threads, start them here rather than the constructors
87
+ // __attribute__((constructor)) or constructors of static variables,
88
+ // or the "hide" will cause SIGSEGV
73
89
}
74
90
75
91
extern " C" {
76
92
77
93
int riru_api_version;
78
- RiruApiV9 *riru_api_v9;
79
-
80
- /*
81
- * Init will be called three times.
82
- *
83
- * The first time:
84
- * Returns the highest version number supported by both Riru and the module.
85
- *
86
- * arg: (int *) Riru's API version
87
- * returns: (int *) the highest possible API version
88
- *
89
- * The second time:
90
- * Returns the RiruModuleX struct created by the module.
91
- * (X is the return of the first call)
92
- *
93
- * arg: (RiruApiVX *) RiruApi strcut, this pointer can be saved for further use
94
- * returns: (RiruModuleX *) RiruModule strcut
95
- *
96
- * The second time:
97
- * Let the module to cleanup (such as RiruModuleX struct created before).
98
- *
99
- * arg: null
100
- * returns: (ignored)
101
- *
102
- */
103
- void *init (void *arg) {
104
- static int step = 0 ;
105
- step += 1 ;
106
-
107
- static void *_module;
108
-
109
- switch (step) {
110
- case 1 : {
111
- auto core_max_api_version = *(int *) arg;
112
- riru_api_version =
113
- core_max_api_version <= RIRU_MODULE_API_VERSION ? core_max_api_version
114
- : RIRU_MODULE_API_VERSION;
115
- return &riru_api_version;
116
- }
117
- case 2 : {
118
- switch (riru_api_version) {
119
- // RiruApiV10 and RiruModuleInfoV10 are equal to V9
120
- case 10 :
121
- case 9 : {
122
- riru_api_v9 = (RiruApiV9 *) arg;
123
-
124
- auto module = (RiruModuleInfoV9 *) malloc (sizeof (RiruModuleInfoV9));
125
- memset (module, 0 , sizeof (RiruModuleInfoV9));
126
- _module = module;
127
-
128
- module->supportHide = true ;
129
-
130
- module->version = RIRU_MODULE_VERSION;
131
- module->versionName = RIRU_MODULE_VERSION_NAME;
132
- module->onModuleLoaded = onModuleLoaded;
133
- module->shouldSkipUid = shouldSkipUid;
134
- module->forkAndSpecializePre = forkAndSpecializePre;
135
- module->forkAndSpecializePost = forkAndSpecializePost;
136
- module->specializeAppProcessPre = specializeAppProcessPre;
137
- module->specializeAppProcessPost = specializeAppProcessPost;
138
- module->forkSystemServerPre = forkSystemServerPre;
139
- module->forkSystemServerPost = forkSystemServerPost;
140
- return module;
141
- }
142
- default : {
143
- return nullptr ;
144
- }
145
- }
146
- }
147
- case 3 : {
148
- free (_module);
149
- return nullptr ;
150
- }
151
- default : {
152
- return nullptr ;
94
+ const char *riru_magisk_module_path = nullptr ;
95
+ int *riru_allow_unload = nullptr ;
96
+
97
+ static auto module = RiruVersionedModuleInfo{
98
+ .moduleApiVersion = riru::moduleApiVersion,
99
+ .moduleInfo = RiruModuleInfo{
100
+ .supportHide = true ,
101
+ .version = riru::moduleVersionCode,
102
+ .versionName = riru::moduleVersionName,
103
+ .onModuleLoaded = onModuleLoaded,
104
+ .forkAndSpecializePre = forkAndSpecializePre,
105
+ .forkAndSpecializePost = forkAndSpecializePost,
106
+ .forkSystemServerPre = forkSystemServerPre,
107
+ .forkSystemServerPost = forkSystemServerPost,
108
+ .specializeAppProcessPre = specializeAppProcessPre,
109
+ .specializeAppProcessPost = specializeAppProcessPost
153
110
}
111
+ };
112
+
113
+ RiruVersionedModuleInfo *init (Riru *riru) {
114
+ auto core_max_api_version = riru->riruApiVersion ;
115
+ riru_api_version = core_max_api_version <= riru::moduleApiVersion ? core_max_api_version : riru::moduleApiVersion;
116
+ module.moduleApiVersion = riru_api_version;
117
+
118
+ riru_magisk_module_path = strdup (riru->magiskModulePath );
119
+ if (riru_api_version >= 25 ) {
120
+ riru_allow_unload = riru->allowUnload ;
154
121
}
122
+ return &module;
123
+ }
155
124
}
156
- }
0 commit comments