Skip to main content

jniNativeInterface

Struct jniNativeInterface 

Source
pub struct jniNativeInterface(/* private fields */);

Implementations§

Source§

impl jniNativeInterface

Source

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.

Source

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.

Source

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);
    }
}
Source

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

Source§

fn clone(&self) -> jniNativeInterface

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for jniNativeInterface

Source§

impl Debug for jniNativeInterface

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<jniNativeInterface> for *mut c_void

Source§

fn from(value: jniNativeInterface) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.