improve: zygisk-ptrace's event parsing

This commit improves ptrace's event parsing by converting more C++ code to C.
This commit is contained in:
ThePedroo
2024-10-04 04:07:53 -03:00
parent e31f9b3d85
commit 6cc01fb548

View File

@@ -2,7 +2,6 @@
#include <sys/system_properties.h> #include <sys/system_properties.h>
#include <unistd.h> #include <unistd.h>
#include <map>
#include <set> #include <set>
#include <sys/signalfd.h> #include <sys/signalfd.h>
#include <err.h> #include <err.h>
@@ -165,13 +164,10 @@ struct SocketHandler : public EventHandler {
}; };
while (1) { while (1) {
std::vector<uint8_t> buf; struct MsgHead *msg = (struct MsgHead *)malloc(sizeof(struct MsgHead));
buf.resize(sizeof(MsgHead), 0);
MsgHead &msg = *((MsgHead *)buf.data());
ssize_t real_size; ssize_t real_size;
ssize_t nread = recv(sock_fd_, &msg, sizeof(msg), MSG_PEEK); ssize_t nread = recv(sock_fd_, msg, sizeof(struct MsgHead), MSG_PEEK);
if (nread == -1) { if (nread == -1) {
if (errno == EAGAIN) break; if (errno == EAGAIN) break;
@@ -183,17 +179,17 @@ struct SocketHandler : public EventHandler {
continue; continue;
} }
if (msg.cmd >= Command::DAEMON64_SET_INFO && msg.cmd != Command::SYSTEM_SERVER_STARTED) { if (msg->cmd >= Command::DAEMON64_SET_INFO && msg->cmd != Command::SYSTEM_SERVER_STARTED) {
if (nread != sizeof(msg)) { if (nread != sizeof(msg)) {
LOGE("cmd %d size %zu != %zu", msg.cmd, nread, sizeof(MsgHead)); LOGE("cmd %d size %zu != %zu", msg->cmd, nread, sizeof(MsgHead));
continue; continue;
} }
real_size = sizeof(MsgHead) + msg.length; real_size = sizeof(MsgHead) + msg->length;
} else { } else {
if (nread != sizeof(Command)) { if (nread != sizeof(Command)) {
LOGE("cmd %d size %zu != %zu", msg.cmd, nread, sizeof(Command)); LOGE("cmd %d size %zu != %zu", msg->cmd, nread, sizeof(Command));
continue; continue;
} }
@@ -201,8 +197,8 @@ struct SocketHandler : public EventHandler {
real_size = sizeof(Command); real_size = sizeof(Command);
} }
buf.resize(real_size); msg = (struct MsgHead *)realloc(msg, real_size);
nread = recv(sock_fd_, &msg, real_size, 0); nread = recv(sock_fd_, msg, real_size, 0);
if (nread == -1) { if (nread == -1) {
if (errno == EAGAIN) break; if (errno == EAGAIN) break;
@@ -217,7 +213,7 @@ struct SocketHandler : public EventHandler {
continue; continue;
} }
switch (msg.cmd) { switch (msg->cmd) {
case START: { case START: {
if (tracing_state == STOPPING) tracing_state = TRACING; if (tracing_state == STOPPING) tracing_state = TRACING;
else if (tracing_state == STOPPED) { else if (tracing_state == STOPPED) {
@@ -271,7 +267,7 @@ struct SocketHandler : public EventHandler {
break; break;
} }
case DAEMON64_SET_INFO: { case DAEMON64_SET_INFO: {
LOGD("received daemon64 info %s", msg.data); LOGD("received daemon64 info %s", msg->data);
/* Will only happen if somehow the daemon restarts */ /* Will only happen if somehow the daemon restarts */
if (status64.daemon_info != NULL) { if (status64.daemon_info != NULL) {
@@ -279,32 +275,42 @@ struct SocketHandler : public EventHandler {
status64.daemon_info = NULL; status64.daemon_info = NULL;
} }
status64.daemon_info = (char *)malloc(msg.length); status64.daemon_info = (char *)malloc(msg->length);
memcpy(status64.daemon_info, msg.data, msg.length - 1); if (status64.daemon_info == NULL) {
status64.daemon_info[msg.length - 1] = '\0'; PLOGE("malloc daemon64 info");
break;
}
strcpy(status64.daemon_info, msg->data);
updateStatus(); updateStatus();
break; break;
} }
case DAEMON32_SET_INFO: { case DAEMON32_SET_INFO: {
LOGD("received daemon32 info %s", msg.data); LOGD("received daemon32 info %s", msg->data);
if (status32.daemon_info != NULL) { if (status32.daemon_info != NULL) {
free(status32.daemon_info); free(status32.daemon_info);
status32.daemon_info = NULL; status32.daemon_info = NULL;
} }
status32.daemon_info = (char *)malloc(msg.length); status32.daemon_info = (char *)malloc(msg->length);
memcpy(status32.daemon_info, msg.data, msg.length - 1); if (status32.daemon_info == NULL) {
status32.daemon_info[msg.length - 1] = '\0'; PLOGE("malloc daemon32 info");
break;
}
strcpy(status32.daemon_info, msg->data);
updateStatus(); updateStatus();
break; break;
} }
case DAEMON64_SET_ERROR_INFO: { case DAEMON64_SET_ERROR_INFO: {
LOGD("received daemon64 error info %s", msg.data); LOGD("received daemon64 error info %s", msg->data);
status64.daemon_running = false; status64.daemon_running = false;
@@ -313,16 +319,21 @@ struct SocketHandler : public EventHandler {
status64.daemon_error_info = NULL; status64.daemon_error_info = NULL;
} }
status64.daemon_error_info = (char *)malloc(msg.length); status64.daemon_error_info = (char *)malloc(msg->length);
memcpy(status64.daemon_error_info, msg.data, msg.length - 1); if (status64.daemon_error_info == NULL) {
status64.daemon_error_info[msg.length - 1] = '\0'; PLOGE("malloc daemon64 error info");
break;
}
strcpy(status64.daemon_error_info, msg->data);
updateStatus(); updateStatus();
break; break;
} }
case DAEMON32_SET_ERROR_INFO: { case DAEMON32_SET_ERROR_INFO: {
LOGD("received daemon32 error info %s", msg.data); LOGD("received daemon32 error info %s", msg->data);
status32.daemon_running = false; status32.daemon_running = false;
@@ -331,9 +342,14 @@ struct SocketHandler : public EventHandler {
status32.daemon_error_info = NULL; status32.daemon_error_info = NULL;
} }
status32.daemon_error_info = (char *)malloc(msg.length); status32.daemon_error_info = (char *)malloc(msg->length);
memcpy(status32.daemon_error_info, msg.data, msg.length - 1); if (status32.daemon_error_info == NULL) {
status32.daemon_error_info[msg.length - 1] = '\0'; PLOGE("malloc daemon32 error info");
break;
}
strcpy(status32.daemon_error_info, msg->data);
updateStatus(); updateStatus();
@@ -349,6 +365,8 @@ struct SocketHandler : public EventHandler {
break; break;
} }
} }
free(msg);
} }
} }