fixed bugs, memory leaks, etc

This commit is contained in:
bbldrizzy committed 2026-10-23 08:36:42 +01:00
1 parent aa4f6b6ddc
commit c3f8f23c1d
124 files changed
+1060 -420

No files matched your search

+33
View File
@@ -8,6 +8,8 @@ static JVMTI: AtomicPtr<*const *const c_void> = AtomicPtr::new(std::ptr::null_mu
/// jclass whose bytes the ClassFileLoadHook should capture during the current RetransformClasses call (0 = none).
static CAPTURE_TARGET: AtomicUsize = AtomicUsize::new(0);
static CAPTURED: Mutex<Option<Vec<u8>>> = Mutex::new(None);
/// Serialises classBytes calls: CAPTURE_TARGET holds a thread-local jclass reference, so only one thread may capture at a time.
static CAPTURE_LOCK: Mutex<()> = Mutex::new(());
/// Acquires the JVMTI capabilities and enables the class file load hook. Called once from the worker thread.
pub unsafe fn init_jvmti(vm: VmPtr) -> Result<(), String> {
@@ -56,6 +58,7 @@ pub unsafe extern "system" fn native_class_bytes(env: EnvPtr, _cls: JClass, targ
if jvmti.is_null() {
return std::ptr::null_mut();
}
let _serial = CAPTURE_LOCK.lock();
if let Ok(mut slot) = CAPTURED.lock() {
*slot = None;
}
@@ -86,6 +89,9 @@ pub unsafe extern "system" fn native_redefine(env: EnvPtr, _cls: JClass, target:
}
let jni = Jni::new(env);
let len = jni.get_array_length(bytes);
if len < 0 {
return -1;
}
let mut buf = vec![0u8; len as usize];
jni.get_byte_array_region(bytes, 0, len, buf.as_mut_ptr());
let def = ClassDefinition { klass: target, class_byte_count: len, class_bytes: buf.as_ptr() };
@@ -104,3 +110,30 @@ pub unsafe extern "system" fn native_log(env: EnvPtr, _cls: JClass, message: JOb
let text = Jni::new(env).get_string_utf(message);
crate::log!("[java] {text}");
}
extern "system" {
fn FreeLibraryAndExitThread(module: *mut c_void, exit_code: u32) -> !;
}
/// void release(): stops the JVMTI hook and unloads this DLL after a short delay (self destruct).
/// Java must not call any native of NativeBridge afterwards.
pub unsafe extern "system" fn native_release(env: EnvPtr, cls: JClass) {
let jvmti = JVMTI.swap(std::ptr::null_mut(), Ordering::SeqCst);
if !jvmti.is_null() {
if let Err(e) = disable_class_file_load_hook(jvmti) {
crate::log!("release: {e}");
}
}
// Late calls from Java then throw UnsatisfiedLinkError instead of jumping into the unmapped DLL.
let rc = Jni::new(env).unregister_natives(cls);
if rc != JNI_OK {
crate::log!("release: UnregisterNatives failed ({rc})");
}
crate::log!("[java] released, unloading the loader");
let module = crate::MODULE as usize;
// The thread below runs code from this DLL until FreeLibraryAndExitThread; the delay lets in-flight native calls return.
let _ = std::thread::Builder::new().name("prestige-unload".into()).spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(2000));
FreeLibraryAndExitThread(module as *mut c_void, 0);
});
}