Skip to content

Commit b94ea0c

Browse files
committed
improve: port ptracer to C
This commit ports the C++ to C99 from zygisk-ptracer code, allowing a ~3x size reduce in its binary.
1 parent 1a3f497 commit b94ea0c

21 files changed

Lines changed: 1739 additions & 1690 deletions

loader/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ val ccachePath by lazy {
2626
}
2727

2828
val defaultCFlags = arrayOf(
29+
"-D_GNU_SOURCE",
30+
2931
"-Wall", "-Wextra",
3032
"-fno-rtti", "-fno-exceptions",
3133
"-fno-stack-protector", "-fomit-frame-pointer",

loader/src/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ add_definitions(-DZKSU_VERSION=\"${ZKSU_VERSION}\")
1010
aux_source_directory(common COMMON_SRC_LIST)
1111
add_library(common STATIC ${COMMON_SRC_LIST})
1212
target_include_directories(common PRIVATE include)
13-
target_link_libraries(common cxx::cxx log)
13+
target_link_libraries(common log)
1414
1515
aux_source_directory(injector INJECTOR_SRC_LIST)
1616
add_library(zygisk SHARED ${INJECTOR_SRC_LIST})
@@ -20,6 +20,5 @@ target_link_libraries(zygisk cxx::cxx log common lsplt_static phmap)
2020
aux_source_directory(ptracer PTRACER_SRC_LIST)
2121
add_executable(libzygisk_ptrace.so ${PTRACER_SRC_LIST})
2222
target_include_directories(libzygisk_ptrace.so PRIVATE include)
23-
target_link_libraries(libzygisk_ptrace.so cxx::cxx log common)
24-
23+
target_link_libraries(libzygisk_ptrace.so log common)
2524
add_subdirectory(external)

loader/src/common/daemon.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// #include <unistd.h>
2-
// #include <sys/types.h>
3-
// #include <sys/stat.h>
4-
// #include <dirent.h>
5-
// #include <fcntl.h>
61
#include <stdio.h>
72
#include <stdlib.h>
83
#include <stdbool.h>
@@ -15,19 +10,6 @@
1510

1611
#include "daemon.h"
1712

18-
char daemon_path[PATH_MAX];
19-
20-
void rezygiskd_init(const char *path) {
21-
snprintf(daemon_path, sizeof(daemon_path), "%s/%s", path, SOCKET_FILE_NAME);
22-
}
23-
24-
void rezygiskd_get_path(char *buf, size_t buf_size) {
25-
size_t fileless_daemon_path = strlen(daemon_path) - strlen("/") - strlen(SOCKET_FILE_NAME);
26-
27-
strncpy(buf, daemon_path, buf_size > fileless_daemon_path ? fileless_daemon_path : buf_size);
28-
buf[fileless_daemon_path] = '\0';
29-
}
30-
3113
int rezygiskd_connect(uint8_t retry) {
3214
retry++;
3315

@@ -49,7 +31,7 @@ int rezygiskd_connect(uint8_t retry) {
4931
Sources:
5032
- https://pubs.opengroup.org/onlinepubs/009696699/basedefs/sys/un.h.html
5133
*/
52-
strcpy(addr.sun_path, daemon_path);
34+
strcpy(addr.sun_path, TMP_PATH "/" SOCKET_FILE_NAME);
5335
socklen_t socklen = sizeof(addr);
5436

5537
while (--retry) {
@@ -343,15 +325,18 @@ bool rezygiskd_update_mns(enum mount_namespace_state nms_state, char *buf, size_
343325
write_uint8_t(fd, (uint8_t)nms_state);
344326

345327
uint32_t target_pid = 0;
346-
read_uint32_t(fd, &target_pid);
347-
if (target_pid == 0) {
328+
if (read_uint32_t(fd, &target_pid) < 0) {
329+
PLOGE("Failed to read target pid");
330+
348331
close(fd);
349332

350333
return false;
351334
}
352335

353-
int target_fd = read_fd(fd);
354-
if (target_fd == -1) {
336+
uint32_t target_fd = 0;
337+
if (read_uint32_t(fd, &target_fd) < 0) {
338+
PLOGE("Failed to read target fd");
339+
355340
close(fd);
356341

357342
return false;

loader/src/common/elf_util.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* INFO: This file is written in C99 */
2-
31
#include <stdlib.h>
42
#include <stdint.h>
53
#include <stdbool.h>

loader/src/common/misc.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
int parse_int(const char *str) {
2-
int val = 0;
2+
int val = 0;
33

4-
char *c = (char *)str;
5-
while (*c) {
6-
if (*c > '9' || *c < '0')
7-
return -1;
4+
char *c = (char *)str;
5+
while (*c) {
6+
if (*c > '9' || *c < '0')
7+
return -1;
88

9-
val = val * 10 + *c - '0';
10-
c++;
11-
}
9+
val = val * 10 + *c - '0';
10+
c++;
11+
}
1212

13-
return val;
13+
return val;
1414
}

loader/src/include/daemon.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#ifndef DAEMON_H
22
#define DAEMON_H
3+
34
#ifdef __cplusplus
45
extern "C" {
56
#endif /* __cplusplus */
67

8+
#include <stdbool.h>
9+
710
#include <unistd.h>
811

912
#ifdef __LP64__
@@ -51,9 +54,11 @@ enum mount_namespace_state {
5154
Module
5255
};
5356

54-
void rezygiskd_init(const char *path);
57+
#define TMP_PATH "/data/adb/rezygisk"
5558

56-
void rezygiskd_get_path(char *buf, size_t buf_size);
59+
static inline const char *rezygiskd_get_path() {
60+
return TMP_PATH;
61+
}
5762

5863
int rezygiskd_connect(uint8_t retry);
5964

loader/src/include/native_bridge_callbacks.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

loader/src/injector/entry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ size_t block_size = 0;
1010
extern "C" [[gnu::visibility("default")]]
1111
void entry(void* addr, size_t size, const char* path) {
1212
LOGD("Zygisk library injected, version %s", ZKSU_VERSION);
13+
1314
start_addr = addr;
1415
block_size = size;
15-
rezygiskd_init(path);
1616

1717
if (!rezygiskd_ping()) {
1818
LOGE("Zygisk daemon is not running");

loader/src/injector/hook.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ DCL_HOOK_FUNC(int, unshare, int flags) {
181181
update_mnt_ns(Rooted, false);
182182
} else if (!(g_ctx->flags[DO_REVERT_UNMOUNT])) {
183183
update_mnt_ns(Module, false);
184-
} else {
185-
LOGI("Process [%s] is on denylist, skipping unmount", g_ctx->process);
186184
}
187185

188186
old_unshare(CLONE_NEWNS);
@@ -636,6 +634,7 @@ void ZygiskContext::run_modules_pre() {
636634

637635
for (auto &m : modules) {
638636
m.onLoad(env);
637+
639638
if (flags[APP_SPECIALIZE]) m.preAppSpecialize(args.app);
640639
else if (flags[SERVER_FORK_AND_SPECIALIZE]) m.preServerSpecialize(args.server);
641640
}
@@ -674,6 +673,12 @@ void ZygiskContext::app_specialize_pre() {
674673
if ((info_flags & (PROCESS_IS_MANAGER | PROCESS_ROOT_IS_MAGISK)) == (PROCESS_IS_MANAGER | PROCESS_ROOT_IS_MAGISK)) {
675674
LOGD("Manager process detected. Notifying that Zygisk has been enabled.");
676675

676+
/* INFO: This environment variable is related to Magisk Zygisk/Manager. It
677+
it used by Magisk's Zygisk to communicate to Magisk Manager whether
678+
Zygisk is working or not.
679+
680+
To allow Zygisk modules to both work properly and for the manager to
681+
identify Zygisk, being it not built-in, as working, we also set it. */
677682
setenv("ZYGISK_ENABLED", "1", 1);
678683
} else {
679684
run_modules_pre();
@@ -818,7 +823,7 @@ void clean_trace(const char* path, size_t load, size_t unload, bool spoof_maps)
818823

819824
if (load > 0 || unload > 0) solist_reset_counters(load, unload);
820825

821-
LOGI("Dropping solist record for %s", path);
826+
LOGD("Dropping solist record for %s", path);
822827

823828
bool path_found = solist_drop_so_path(path);
824829
if (!path_found || !spoof_maps) return;

loader/src/injector/solist.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* INFO: This file is written in C99 */
2-
31
#include <stdio.h>
42
#include <stdbool.h>
53
#include <string.h>

0 commit comments

Comments
 (0)