Add Linux and macOS support, batch class transformations with a disk cache, and add cross-platform release packaging. Also improve runtime namespace handling, keybind editing, and startup behavior.
This commit is contained in:
bbldrizzy committed 2026-10-27 20:30:09 +00:00
1 parent 910a126f04
commit 38ff6d387c
29 files changed
+2234 -491

No files matched your search

+139 -30
View File
@@ -1,14 +1,16 @@
//! JVMTI state and the two natives of dev.zprestige.prestige.inject.NativeBridge.
use crate::jni::*;
use std::ffi::{c_char, c_void};
use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicPtr, Ordering};
use std::sync::Mutex;
static JVMTI: AtomicPtr<*const *const c_void> = AtomicPtr::new(std::ptr::null_mut());
/// 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.
/// jclass references (as addresses) whose bytes the ClassFileLoadHook captures during the current RetransformClasses call.
/// Never held across the JVMTI call: the hook runs on the calling thread and locks it again.
static CAPTURE_TARGETS: Mutex<Vec<usize>> = Mutex::new(Vec::new());
/// Captured bytes, index-aligned with CAPTURE_TARGETS.
static CAPTURED: Mutex<Vec<Option<Vec<u8>>>> = Mutex::new(Vec::new());
/// Serialises classBytes calls: CAPTURE_TARGETS holds thread-local jclass references, 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.
@@ -22,7 +24,7 @@ pub unsafe fn init_jvmti(vm: VmPtr) -> Result<(), String> {
if rc != 0 {
return Err(format!("AddCapabilities failed ({rc})"));
}
enable_class_file_load_hook(jvmti, class_file_load_hook)?;
install_class_file_load_hook(jvmti, class_file_load_hook)?;
JVMTI.store(jvmti, Ordering::SeqCst);
Ok(())
}
@@ -40,14 +42,22 @@ unsafe extern "system" fn class_file_load_hook(
_new_len: *mut JInt,
_new_data: *mut *mut u8,
) {
let target = CAPTURE_TARGET.load(Ordering::SeqCst);
if target == 0 || class_being_redefined.is_null() || data.is_null() {
if class_being_redefined.is_null() || data.is_null() {
return;
}
let targets: Vec<usize> = match CAPTURE_TARGETS.lock() {
Ok(t) if !t.is_empty() => t.clone(),
_ => return,
};
let jni = Jni::new(env);
if jni.is_same_object(class_being_redefined, target as JObject) {
if let Ok(mut slot) = CAPTURED.lock() {
*slot = Some(std::slice::from_raw_parts(data, data_len as usize).to_vec());
for (i, &target) in targets.iter().enumerate() {
if jni.is_same_object(class_being_redefined, target as JObject) {
if let Ok(mut slots) = CAPTURED.lock() {
if let Some(slot) = slots.get_mut(i) {
*slot = Some(std::slice::from_raw_parts(data, data_len as usize).to_vec());
}
}
return;
}
}
}
@@ -58,22 +68,39 @@ 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;
match capture(jvmti, &[target]).and_then(|mut v| v.pop().flatten()) {
Some(bytes) => to_byte_array(&Jni::new(env), &bytes),
None => std::ptr::null_mut(),
}
}
/// Retransforms `classes` in one JVMTI call and returns the captured bytes, index-aligned (None = not captured).
unsafe fn capture(jvmti: JvmtiPtr, classes: &[JClass]) -> Option<Vec<Option<Vec<u8>>>> {
let _serial = CAPTURE_LOCK.lock();
if let Ok(mut c) = CAPTURED.lock() {
*c = vec![None; classes.len()];
}
if let Ok(mut t) = CAPTURE_TARGETS.lock() {
*t = classes.iter().map(|&c| c as usize).collect();
}
let enabled = set_class_file_load_hook_enabled(jvmti, true);
let rc = if enabled.is_ok() { retransform_classes(jvmti, classes) } else { -1 };
let _ = set_class_file_load_hook_enabled(jvmti, false);
if let Ok(mut t) = CAPTURE_TARGETS.lock() {
t.clear();
}
if let Err(e) = enabled {
crate::log!("classBytes: {e}");
return None;
}
CAPTURE_TARGET.store(target as usize, Ordering::SeqCst);
let rc = retransform_class(jvmti, target);
CAPTURE_TARGET.store(0, Ordering::SeqCst);
if rc != 0 {
crate::log!("RetransformClasses failed ({rc})");
return std::ptr::null_mut();
return None;
}
let bytes = CAPTURED.lock().ok().and_then(|mut slot| slot.take());
let Some(bytes) = bytes else {
return std::ptr::null_mut();
};
let jni = Jni::new(env);
CAPTURED.lock().ok().map(|mut c| std::mem::take(&mut *c))
}
unsafe fn to_byte_array(jni: &Jni, bytes: &[u8]) -> JObject {
let array = jni.new_byte_array(bytes.len() as JInt);
if !array.is_null() {
jni.set_byte_array_region(array, 0, bytes.len() as JInt, bytes.as_ptr());
@@ -81,6 +108,80 @@ pub unsafe extern "system" fn native_class_bytes(env: EnvPtr, _cls: JClass, targ
array
}
/// byte[][] classBytesBatch(Class[]): classBytes for many classes with a single RetransformClasses call (one safepoint, one pass
/// over the other agents' transformers). Entries are null for classes that were not captured; the result is null on failure.
pub unsafe extern "system" fn native_class_bytes_batch(env: EnvPtr, _cls: JClass, targets: JObject) -> JObject {
let jvmti = JVMTI.load(Ordering::SeqCst);
if jvmti.is_null() || targets.is_null() {
return std::ptr::null_mut();
}
let jni = Jni::new(env);
let n = jni.get_array_length(targets);
if n <= 0 {
return std::ptr::null_mut();
}
jni.ensure_local_capacity(2 * n + 16);
let classes: Vec<JClass> = (0..n).map(|i| jni.get_object_array_element(targets, i)).collect();
let Some(captured) = capture(jvmti, &classes) else {
return std::ptr::null_mut();
};
let byte_array_class = jni.find_class("[B");
let result = jni.new_object_array(n, byte_array_class, std::ptr::null_mut());
if result.is_null() {
return std::ptr::null_mut();
}
for (i, bytes) in captured.iter().enumerate() {
if let Some(bytes) = bytes {
let array = to_byte_array(&jni, bytes);
if !array.is_null() {
jni.set_object_array_element(result, i as JInt, array);
}
}
}
result
}
/// int redefineBatch(Class[], byte[][]): JVMTI RedefineClasses of many classes in one call (one safepoint). It is all or nothing:
/// the JVMTI error code is returned (0 = success) and no class is changed on failure.
pub unsafe extern "system" fn native_redefine_batch(env: EnvPtr, _cls: JClass, targets: JObject, bytes: JObject) -> JInt {
let jvmti = JVMTI.load(Ordering::SeqCst);
if jvmti.is_null() || targets.is_null() || bytes.is_null() {
return -1;
}
let jni = Jni::new(env);
let n = jni.get_array_length(targets);
if n <= 0 || jni.get_array_length(bytes) != n {
return -1;
}
jni.ensure_local_capacity(2 * n + 16);
let mut buffers: Vec<Vec<u8>> = Vec::with_capacity(n as usize);
let mut classes: Vec<JClass> = Vec::with_capacity(n as usize);
for i in 0..n {
let array = jni.get_object_array_element(bytes, i);
if array.is_null() {
return -1;
}
let len = jni.get_array_length(array);
if len < 0 {
return -1;
}
let mut buf = vec![0u8; len as usize];
jni.get_byte_array_region(array, 0, len, buf.as_mut_ptr());
buffers.push(buf);
classes.push(jni.get_object_array_element(targets, i));
}
let defs: Vec<ClassDefinition> = classes
.iter()
.zip(&buffers)
.map(|(&klass, buf)| ClassDefinition { klass, class_byte_count: buf.len() as JInt, class_bytes: buf.as_ptr() })
.collect();
let rc = redefine_classes(jvmti, &defs);
if rc != 0 {
crate::log!("RedefineClasses (batch of {n}) failed ({rc})");
}
rc
}
/// int redefine(Class, byte[]): JVMTI RedefineClasses of a single class; returns the JVMTI error code (0 = success).
pub unsafe extern "system" fn native_redefine(env: EnvPtr, _cls: JClass, target: JClass, bytes: JObject) -> JInt {
let jvmti = JVMTI.load(Ordering::SeqCst);
@@ -111,6 +212,7 @@ pub unsafe extern "system" fn native_log(env: EnvPtr, _cls: JClass, message: JOb
crate::log!("[java] {text}");
}
#[cfg(windows)]
extern "system" {
fn FreeLibraryAndExitThread(module: *mut c_void, exit_code: u32) -> !;
}
@@ -129,11 +231,18 @@ pub unsafe extern "system" fn native_release(env: EnvPtr, cls: JClass) {
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);
});
#[cfg(windows)]
{
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);
});
}
// On unix the library is deliberately NOT dlclose'd: a thread of ours would return into unmapped code (and the
// JVM may still hold the agent handle). The hook and natives are gone; the code just stays resident.
#[cfg(unix)]
crate::log!("[java] released (library stays resident on this platform)");
}