Skip to content

Commit

Permalink
Merge branch 'v3_openjdk' into v3_sol
Browse files Browse the repository at this point in the history
  • Loading branch information
SolDev69 committed Jan 8, 2024
2 parents 42e059c + 01fb9ac commit 2c80d42
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 3,758 deletions.
6 changes: 6 additions & 0 deletions app_pojavlauncher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ android {
jniLibs {
useLegacyPackaging = true
}
pickFirst '**/libbytehook.so'
}

buildFeatures {
prefab true
}

buildToolsVersion = '33.0.2'
Expand Down Expand Up @@ -208,6 +213,7 @@ dependencies {
implementation 'org.tukaani:xz:1.8'
implementation 'com.github.PojavLauncherTeam:exp4j:60eaec6f78'
implementation 'net.sourceforge.htmlcleaner:htmlcleaner:2.6.1'
implementation 'com.bytedance:bytehook:1.0.9'

// implementation 'net.sourceforge.streamsupport:streamsupport-cfuture:1.7.0'

Expand Down
31 changes: 3 additions & 28 deletions app_pojavlauncher/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,13 @@ LOCAL_MODULE := angle_gles2
LOCAL_SRC_FILES := tinywrapper/angle-gles/$(TARGET_ARCH_ABI)/libGLESv2_angle.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := angle_gles2_new
LOCAL_SRC_FILES := tinywrapper_new_angle/angle-gles/$(TARGET_ARCH_ABI)/libGLESv2_angle.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := tinywrapper
LOCAL_SHARED_LIBRARIES := angle_gles2
LOCAL_SRC_FILES := tinywrapper/main.c tinywrapper/string_utils.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/tinywrapper
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := tinywrapper_new_angle
LOCAL_SHARED_LIBRARIES := angle_gles2_new
LOCAL_SRC_FILES := tinywrapper_new_angle/main.c tinywrapper_new_angle/string_utils.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/tinywrapper_new_angle
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := xhook
LOCAL_SRC_FILES := xhook/xhook.c \
xhook/xh_core.c \
xhook/xh_elf.c \
xhook/xh_jni.c \
xhook/xh_log.c \
xhook/xh_util.c \
xhook/xh_version.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/xhook
LOCAL_CFLAGS := -Wall -Wextra -Werror -fvisibility=hidden
LOCAL_CONLYFLAGS := -std=c11
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
# Link GLESv2 for test
Expand Down Expand Up @@ -82,13 +56,14 @@ LOCAL_LDFLAGS := -z global
include $(BUILD_SHARED_LIBRARY)
#endif

$(call import-module,prefab/bytehook)
LOCAL_PATH := $(HERE_PATH)

include $(CLEAR_VARS)
LOCAL_MODULE := istdio
LOCAL_SHARED_LIBRARIES := xhook
LOCAL_SHARED_LIBRARIES := bytehook
LOCAL_SRC_FILES := \
stdio_is.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/xhook
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
Expand Down
94 changes: 74 additions & 20 deletions app_pojavlauncher/src/main/jni/stdio_is.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <xhook.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <bytehook.h>

//
// Created by maks on 17.02.21.
Expand All @@ -25,7 +25,7 @@ static pthread_t logger;
static jmethodID logger_onEventLogged;
static volatile jobject logListener = NULL;
static int latestlog_fd = -1;
static int exit_code;
static _Atomic bool exit_tripped = false;


static bool recordBuffer(char* buf, ssize_t len) {
Expand Down Expand Up @@ -104,34 +104,88 @@ Java_net_kdt_pojavlaunch_Logger_begin(JNIEnv *env, __attribute((unused)) jclass
pthread_detach(logger);
}

static void atexit_handler() {
if(exit_code != 0) {
JNIEnv *env;
(*exitTrap_jvm)->AttachCurrentThread(exitTrap_jvm, &env, NULL);
(*env)->CallStaticVoidMethod(env, exitTrap_exitClass, exitTrap_staticMethod, exitTrap_ctx,
exit_code);
(*env)->DeleteGlobalRef(env, exitTrap_ctx);
(*env)->DeleteGlobalRef(env, exitTrap_exitClass);
(*exitTrap_jvm)->DetachCurrentThread(exitTrap_jvm);



typedef void (*exit_func)(int);

_Noreturn static void nominal_exit(int code) {
JNIEnv *env;
jint errorCode = (*exitTrap_jvm)->GetEnv(exitTrap_jvm, (void**)&env, JNI_VERSION_1_6);
if(errorCode == JNI_EDETACHED) {
errorCode = (*exitTrap_jvm)->AttachCurrentThread(exitTrap_jvm, &env, NULL);
}
if(errorCode != JNI_OK) {
// Step on a landmine and die, since we can't invoke the Dalvik exit without attaching to
// Dalvik.
// I mean, if Zygote can do that, why can't I?
killpg(getpgrp(), SIGTERM);
}
if(code != 0) {
// Exit code 0 is pretty established as "eh it's fine"
// so only open the GUI if the code is != 0
(*env)->CallStaticVoidMethod(env, exitTrap_exitClass, exitTrap_staticMethod, exitTrap_ctx, code);
}
// Delete the reference, not gonna need 'em later anyway
(*env)->DeleteGlobalRef(env, exitTrap_ctx);
(*env)->DeleteGlobalRef(env, exitTrap_exitClass);

// A hat trick, if you will
// Call the Android System.exit() to perform Android's shutdown hooks and do a
// fully clean exit.
// After doing this, either of these will happen:
// 1. Runtime calls exit() for real and it will be handled by ByteHook's recurse handler
// and redirected back to the OS
// 2. Zygote sends SIGTERM (no handling necessary, the process perishes)
// 3. A different thread calls exit() and the hook will go through the exit_tripped path
jclass systemClass = (*env)->FindClass(env,"java/lang/System");
jmethodID exitMethod = (*env)->GetStaticMethodID(env, systemClass, "exit", "(I)V");
(*env)->CallStaticVoidMethod(env, systemClass, exitMethod, 0);
// System.exit() should not ever return, but the compiler doesn't know about that
// so put a while loop here
while(1) {}
}

static void (*old_exit)(int code);
static void custom_exit(int code) {
exit_code = code;
old_exit(code);
// If the exit was already done (meaning it is recursive or from a different thread), pass the call through
if(exit_tripped) {
BYTEHOOK_CALL_PREV(custom_exit, exit_func, code);
BYTEHOOK_POP_STACK();
return;
}
exit_tripped = true;
// Perform a nominal exit, as we expect.
nominal_exit(code);
BYTEHOOK_POP_STACK();
}

static void custom_atexit() {
// Same as custom_exit, but without the code or the exit passthrough.
if(exit_tripped) {
return;
}
exit_tripped = true;
nominal_exit(0);
}

JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_utils_JREUtils_setupExitTrap(JNIEnv *env, __attribute((unused)) jclass clazz, jobject context) {
exitTrap_ctx = (*env)->NewGlobalRef(env,context);
(*env)->GetJavaVM(env,&exitTrap_jvm);
exitTrap_exitClass = (*env)->NewGlobalRef(env,(*env)->FindClass(env,"net/kdt/pojavlaunch/ExitActivity"));
exitTrap_staticMethod = (*env)->GetStaticMethodID(env,exitTrap_exitClass,"showExitMessage","(Landroid/content/Context;I)V");
xhook_enable_debug(0);
xhook_register(".*\\.so$", "exit", custom_exit, (void **) &old_exit);
xhook_refresh(1);
// Instead of relying purely on the hook, send off the code in atexit()
// to avoid crashes due to attaching DVM in an unexpected state
atexit(&atexit_handler);

if(bytehook_init(BYTEHOOK_MODE_AUTOMATIC, false) == BYTEHOOK_STATUS_CODE_OK) {
bytehook_hook_all(NULL,
"exit",
&custom_exit,
NULL,
NULL);
}else {
// If we can't hook, register atexit(). This won't report a proper error code,
// but it will prevent a SIGSEGV or a SIGABRT from the depths of Dalvik that happens
// on exit().
atexit(custom_atexit);
}
}

JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_Logger_appendToLog(JNIEnv *env, __attribute((unused)) jclass clazz, jstring text) {
Expand Down
Loading

0 comments on commit 2c80d42

Please sign in to comment.