diff --git a/CMakeLists.txt b/CMakeLists.txt index 35ebc8d..635799d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.15) project(JuiceAgent LANGUAGES C CXX) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) # output directories diff --git a/src/libagent/callback/ClassFileLoadHook.cpp b/src/libagent/callback/ClassFileLoadHook.cpp index 19013f3..a2ec4d8 100644 --- a/src/libagent/callback/ClassFileLoadHook.cpp +++ b/src/libagent/callback/ClassFileLoadHook.cpp @@ -14,5 +14,32 @@ void JNICALL ClassFileLoadHook( unsigned char** new_classbytes) { // Delete this log when the hook is stable, or it will cause performance issues std::cout << "ClassFileLoadHook: " << name << std::endl; + if (!name) { + PLOGW << "name is null"; + return; + } + /* ========================= + * 1. Capture original bytes + * ========================= */ + std::lock_guard lock(classDataMutex); + if (classToCapture.contains(name)) { + if (!classFileDataMap.contains(name)) { + ClassFileData data; + + data.className = name; + data.bytecode.assign(classbytes, classbytes + class_data_len); + // Experimental: class/classloader/protection_domain references (to be used in future features, e.g. dynamic patching) + data.clazz = class_being_redefined; + data.classloader = loader; + data.protection_domain = protection_domain; + + + classFileDataMap[name] = std::move(data); + + classToCapture.erase(name); + + PLOGI << "Captured class: " << name << " (length: " << class_data_len << ")"; + } + } } \ No newline at end of file diff --git a/src/libagent/includes/global.hpp b/src/libagent/includes/global.hpp index 40107ae..527e97d 100644 --- a/src/libagent/includes/global.hpp +++ b/src/libagent/includes/global.hpp @@ -16,7 +16,7 @@ struct ClassFileData { // ======== Global references to prevent GC ======== // Remember to release these references when they are no longer needed, otherwise it will cause memory leaks jclass clazz; - jclass classloader; + jobject classloader; jobject protection_domain; }; diff --git a/src/libagent/includes/jni_impl.hpp b/src/libagent/includes/jni_impl.hpp index 440bbec..2e73d74 100644 --- a/src/libagent/includes/jni_impl.hpp +++ b/src/libagent/includes/jni_impl.hpp @@ -5,6 +5,7 @@ #include #include #include +#include // Generated by JuiceAgent-API #include