pub struct jniNativeInterface(/* private fields */);Implementations§
Source§impl jniNativeInterface
impl jniNativeInterface
Sourcepub const fn new_uninit() -> Self
pub const fn new_uninit() -> Self
Returns uninitialized jniNativeInterface.
The interface must be initialized with a call to JVMTIEnv::GetJNIFunctionTable
before it can be used in any way.
§Undefined behavior of uninitialized jniNativeInterface
Calling any jvmti fn is ub. Calling any unsafe fn is ub.
Sourcepub const unsafe fn from_raw_ptr(ptr: *mut c_void) -> Self
pub const unsafe fn from_raw_ptr(ptr: *mut c_void) -> Self
Constructs a new jniNativeInterface from a raw pointer.
§Safety
Unless the raw pointer was constructed by an invocation on JVMTIEnv::GetJNIFunctionTable
then the using the resulting jniNativeInterface in any way is UB.
Sourcepub unsafe fn set(&self, linkage: impl AsJNILinkage, value: *mut c_void)
pub unsafe fn set(&self, linkage: impl AsJNILinkage, value: *mut c_void)
Overwrites function in this jniNativeInterface
§Safety
if value is not a function with a matching signature/calling convention
then putting the jniNativeInterface into use will trigger UB once that linkage is later used.
§Example
use jni_simple::*;
extern "system" fn hooked_get_version(_env: JNIEnv) -> jint {
println!("JNIEnv GetVersion was called!");
JNI_VERSION_1_8
}
fn install_hook(env: JVMTIEnv) {
unsafe {
let mut iface = jniNativeInterface::new_uninit();
assert_eq!(env.GetJNIFunctionTable(&mut iface), JVMTI_ERROR_NONE);
iface.set(JNILinkage::GetVersion, hooked_get_version as _);
assert_eq!(env.SetJNIFunctionTable(iface), JVMTI_ERROR_NONE);
}
}Sourcepub unsafe fn get<X>(&self, linkage: impl AsJNILinkage) -> X
pub unsafe fn get<X>(&self, linkage: impl AsJNILinkage) -> X
Returns a function in this jniNativeInterface
This is usually used to retrieve the unhooked original function from a jniNativeInterface
§Safety
The size of X must be usize (a function pointer).
§Example
This example illustrates hooking of the GetVersion function.
The hooked function calls the original function and prints the result to stdout.
use std::ffi::c_void;
use std::ops::DerefMut;
use std::sync::OnceLock;
use jni_simple::*;
static ORIGINAL_FUNCTIONS: OnceLock<jniNativeInterface> = OnceLock::new();
extern "system" fn hooked_get_version(env: JNIEnv) -> jint {
println!("JNIEnv GetVersion will be called!");
let guard = ORIGINAL_FUNCTIONS.get().unwrap();
let result = unsafe {
guard.get::<extern "system" fn(*mut c_void) -> jint>(JNILinkage::GetVersion)(env.vtable())
};
println!("JNIEnv GetVersion returned {result}!");
result
}
fn install_hook(env: JVMTIEnv) {
unsafe {
_= ORIGINAL_FUNCTIONS.get_or_init(|| {
let mut iface = jniNativeInterface::new_uninit();
assert_eq!(env.GetJNIFunctionTable(&mut iface), JVMTI_ERROR_NONE);
iface
});
let mut iface = jniNativeInterface::new_uninit();
assert_eq!(env.GetJNIFunctionTable(&mut iface), JVMTI_ERROR_NONE);
iface.set(JNILinkage::GetVersion, hooked_get_version as _);
assert_eq!(env.SetJNIFunctionTable(iface), JVMTI_ERROR_NONE);
}
}Trait Implementations§
Source§impl Clone for jniNativeInterface
impl Clone for jniNativeInterface
Source§fn clone(&self) -> jniNativeInterface
fn clone(&self) -> jniNativeInterface
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more