Crate jni_simple

Crate jni_simple 

Source
Expand description

§jni-simple

This crate contains a simple dumb handwritten rust wrapper around the JNI (Java Native Interface) API. It does absolutely no magic around the JNI Calls and lets you just use it as you would in C.

In addition to JNI, this crate also provides a similar simplistic wrapper for the JVMTI (Java VM Tool Interface) API. The JVMTI Api can be used to write, for example, a Java Agent (like a Java debugger) in Rust or perform similar deep instrumentation with the JVM.

If you are looking to start a jvm from rust then the entrypoints in this create are init_dynamic_link, load_jvm_from_library, JNI_CreateJavaVM and JNI_GetCreatedJavaVMs.

If you are looking to write a jni library in rust then the types JNIEnv and jclass, etc. should be sufficient.

Macros§

jtypes
This macro is usefull for constructing jtype arrays. This is often needed when making upcalls into the jvm with many arguments using the ‘A’ type functions:

Structs§

JNIEnv
JNINativeMethod
JVMTIEnv
JavaVM
JavaVMAttachArgs
JavaVMInitArgs
JavaVMOption
jniNativeInterface
jvmtiAddrLocationMap
jvmtiCapabilities
jvmtiClassDefinition
jvmtiError
jvmtiEventCallbacks
jvmtiExtensionEventInfo
jvmtiExtensionFunctionInfo
jvmtiHeapCallbacks
jvmtiHeapReferenceInfoArray
jvmtiHeapReferenceInfoConstantPool
jvmtiHeapReferenceInfoField
jvmtiHeapReferenceInfoJniLocal
jvmtiHeapReferenceInfoReserved
jvmtiHeapReferenceInfoStackLocal
jvmtiLineNumberEntry
jvmtiLocalVariableEntry
jvmtiMonitorStackDepthInfo
jvmtiMonitorUsage
jvmtiParamInfo
jvmtiThreadGroupInfo
jvmtiThreadInfo
jvmtiTimerInfo

Enums§

JNILinkage
Enum of all known jni linkage numbers This is mostly useful for use with jvmti when hooking jvm functions.
JvmtiError
Rust enum that mirrors jvmtiError, however it has a different repr to c_int causing it to be incompatible outside of rust code.
jobjectRefType
jvmtiEvent
jvmtiEventMode
jvmtiHeapObjectFilter
jvmtiHeapReferenceKind
jvmtiIterationControl
jvmtiPrimitiveType
jvmtiVerboseFlag

Constants§

JNI_ABORT
JNI_COMMIT
JNI_EDETACHED
JNI_EEXIST
JNI_EINVAL
JNI_ENOMEM
JNI_ERR
JNI_EVERSION
JNI_FALSE
JNI_OK
JNI_TRUE
JNI_VERSION_9
JNI_VERSION_1_1
JNI_VERSION_1_2
JNI_VERSION_1_4
JNI_VERSION_1_6
JNI_VERSION_1_8
JNI_VERSION_10
JNI_VERSION_19
JNI_VERSION_20
JNI_VERSION_21
JNI_VERSION_24
JVMTI_CLASS_STATUS_ARRAY
Class is an array. If set, all other bits are zero.
JVMTI_CLASS_STATUS_ERROR
Error during initialization makes class unusable
JVMTI_CLASS_STATUS_INITIALIZED
Class initialization is complete. Static initializer has been run.
JVMTI_CLASS_STATUS_PREPARED
Class preparation is complete
JVMTI_CLASS_STATUS_PRIMITIVE
Class is a primitive class (for example, java.lang.Integer.TYPE). If set, all other bits are zero.
JVMTI_CLASS_STATUS_VERIFIED
Class bytecodes have been verified
JVMTI_ERROR_ABSENT_INFORMATION
JVMTI_ERROR_ACCESS_DENIED
JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION
JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED
JVMTI_ERROR_CLASS_NOT_PREPARED
JVMTI_ERROR_DUPLICATE
JVMTI_ERROR_FAILS_VERIFICATION
JVMTI_ERROR_ILLEGAL_ARGUMENT
JVMTI_ERROR_INTERNAL
JVMTI_ERROR_INTERRUPT
JVMTI_ERROR_INVALID_CLASS
JVMTI_ERROR_INVALID_CLASS_FORMAT
JVMTI_ERROR_INVALID_ENVIRONMENT
JVMTI_ERROR_INVALID_EVENT_TYPE
JVMTI_ERROR_INVALID_FIELDID
JVMTI_ERROR_INVALID_LOCATION
JVMTI_ERROR_INVALID_METHODID
JVMTI_ERROR_INVALID_MODULE
JVMTI_ERROR_INVALID_MONITOR
JVMTI_ERROR_INVALID_OBJECT
JVMTI_ERROR_INVALID_PRIORITY
JVMTI_ERROR_INVALID_SLOT
JVMTI_ERROR_INVALID_THREAD
JVMTI_ERROR_INVALID_THREAD_GROUP
JVMTI_ERROR_INVALID_TYPESTATE
JVMTI_ERROR_MAX
JVMTI_ERROR_MUST_POSSESS_CAPABILITY
JVMTI_ERROR_NAMES_DONT_MATCH
JVMTI_ERROR_NATIVE_METHOD
JVMTI_ERROR_NONE
JVMTI_ERROR_NOT_AVAILABLE
JVMTI_ERROR_NOT_FOUND
JVMTI_ERROR_NOT_MONITOR_OWNER
JVMTI_ERROR_NO_MORE_FRAMES
JVMTI_ERROR_NULL_POINTER
JVMTI_ERROR_OPAQUE_FRAME
JVMTI_ERROR_OUT_OF_MEMORY
JVMTI_ERROR_THREAD_NOT_ALIVE
JVMTI_ERROR_THREAD_NOT_SUSPENDED
JVMTI_ERROR_THREAD_SUSPENDED
JVMTI_ERROR_TYPE_MISMATCH
JVMTI_ERROR_UNATTACHED_THREAD
JVMTI_ERROR_UNMODIFIABLE_CLASS
JVMTI_ERROR_UNMODIFIABLE_MODULE
JVMTI_ERROR_UNSUPPORTED_OPERATION
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED
JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED
JVMTI_ERROR_UNSUPPORTED_VERSION
JVMTI_ERROR_WRONG_PHASE
JVMTI_HEAP_FILTER_CLASS_TAGGED
JVMTI_HEAP_FILTER_CLASS_UNTAGGED
JVMTI_HEAP_FILTER_TAGGED
JVMTI_HEAP_FILTER_UNTAGGED
JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT
JVMTI_HEAP_REFERENCE_CLASS
JVMTI_HEAP_REFERENCE_CLASS_LOADER
JVMTI_HEAP_REFERENCE_CONSTANT_POOL
JVMTI_HEAP_REFERENCE_FIELD
JVMTI_HEAP_REFERENCE_INTERFACE
JVMTI_HEAP_REFERENCE_JNI_GLOBAL
JVMTI_HEAP_REFERENCE_JNI_LOCAL
JVMTI_HEAP_REFERENCE_MONITOR
JVMTI_HEAP_REFERENCE_OTHER
JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN
JVMTI_HEAP_REFERENCE_SIGNERS
JVMTI_HEAP_REFERENCE_STACK_LOCAL
JVMTI_HEAP_REFERENCE_STATIC_FIELD
JVMTI_HEAP_REFERENCE_SUPERCLASS
JVMTI_HEAP_REFERENCE_SYSTEM_CLASS
JVMTI_HEAP_REFERENCE_THREAD
JVMTI_HEAP_ROOT_JNI_GLOBAL
JVMTI_HEAP_ROOT_JNI_LOCAL
JVMTI_HEAP_ROOT_MONITOR
JVMTI_HEAP_ROOT_OTHER
JVMTI_HEAP_ROOT_STACK_LOCAL
JVMTI_HEAP_ROOT_SYSTEM_CLASS
JVMTI_HEAP_ROOT_THREAD
JVMTI_JLOCATION_JVMBCI
jlocation values represent virtual machine bytecode indices–that is, offsets into the virtual machine code for a method.
JVMTI_JLOCATION_MACHINEPC
jlocation values represent native machine program counter values.
JVMTI_JLOCATION_OTHER
jlocation values have some other representation.
JVMTI_KIND_ALLOC_ALLOC_BUF
Outgoing allocated array of allocated arrays argument - foo***. Free with Deallocate.
JVMTI_KIND_ALLOC_BUF
Outgoing allocated array argument - foo**. Free with Deallocate.
JVMTI_KIND_IN
Ingoing argument - foo.
JVMTI_KIND_IN_BUF
Ingoing array argument - const foo*.
JVMTI_KIND_IN_PTR
Ingoing pointer argument - const foo*.
JVMTI_KIND_OUT
Outgoing argument - foo*.
JVMTI_KIND_OUT_BUF
Outgoing array argument (pre-allocated by agent) - foo*. Do not Deallocate.
JVMTI_PHASE_DEAD
JVMTI_PHASE_LIVE
JVMTI_PHASE_ONLOAD
JVMTI_PHASE_PRIMORDIAL
JVMTI_PHASE_START
JVMTI_PRIMITIVE_TYPE_BOOLEAN
JVMTI_PRIMITIVE_TYPE_BYTE
JVMTI_PRIMITIVE_TYPE_CHAR
JVMTI_PRIMITIVE_TYPE_DOUBLE
JVMTI_PRIMITIVE_TYPE_FLOAT
JVMTI_PRIMITIVE_TYPE_INT
JVMTI_PRIMITIVE_TYPE_LONG
JVMTI_PRIMITIVE_TYPE_SHORT
JVMTI_REFERENCE_ARRAY_ELEMENT
JVMTI_REFERENCE_CLASS
JVMTI_REFERENCE_CLASS_LOADER
JVMTI_REFERENCE_CONSTANT_POOL
JVMTI_REFERENCE_FIELD
JVMTI_REFERENCE_INTERFACE
JVMTI_REFERENCE_PROTECTION_DOMAIN
JVMTI_REFERENCE_SIGNERS
JVMTI_REFERENCE_STATIC_FIELD
JVMTI_THREAD_STATE_ALIVE
Thread is alive. Zero if thread is new (not started) or terminated.
JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER
Thread is waiting to enter a synchronized block/method or, after an Object.wait(), waiting to re-enter a synchronized block/method.
JVMTI_THREAD_STATE_INTERRUPTED
Thread has been interrupted.
JVMTI_THREAD_STATE_IN_NATIVE
Thread is in native code–that is, a native method is running which has not called back into the VM or Java programming language code.
JVMTI_THREAD_STATE_IN_OBJECT_WAIT
Thread is waiting on an object monitor – Object.wait.
JVMTI_THREAD_STATE_PARKED
Thread is parked, for example: LockSupport.park, LockSupport.parkUtil and LockSupport.parkNanos. A virtual thread that is sleeping, in Thread.sleep, may have this state flag set instead of JVMTI_THREAD_STATE_SLEEPING.
JVMTI_THREAD_STATE_RUNNABLE
Thread is runnable.
JVMTI_THREAD_STATE_SLEEPING
Thread is sleeping – Thread.sleep.
JVMTI_THREAD_STATE_SUSPENDED
Thread is suspended by a suspend function (such as SuspendThread). If this bit is set, the other bits refer to the thread state before suspension.
JVMTI_THREAD_STATE_TERMINATED
Thread has completed execution.
JVMTI_THREAD_STATE_VENDOR_1
Defined by VM vendor.
JVMTI_THREAD_STATE_VENDOR_2
Defined by VM vendor.
JVMTI_THREAD_STATE_VENDOR_3
Defined by VM vendor.
JVMTI_THREAD_STATE_WAITING
Thread is waiting.
JVMTI_THREAD_STATE_WAITING_INDEFINITELY
Thread is waiting without a timeout. For example, Object.wait().
JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT
Thread is waiting with a maximum time to wait specified. For example, Object.wait(long).
JVMTI_TIMER_ELAPSED
JVMTI_TIMER_TOTAL_CPU
JVMTI_TIMER_USER_CPU
JVMTI_TYPE_CCHAR
C programming language type - char.
JVMTI_TYPE_CVOID
C programming language type - void.
JVMTI_TYPE_JBOOLEAN
Java programming language primitive type - boolean. JNI type jboolean.
JVMTI_TYPE_JBYTE
Java programming language primitive type - byte. JNI type jbyte.
JVMTI_TYPE_JCHAR
Java programming language primitive type - char. JNI type jchar.
JVMTI_TYPE_JCLASS
Java programming language object type - java.lang.Class. JNI type jclass. Returned values are JNI local references and must be managed.
JVMTI_TYPE_JDOUBLE
Java programming language primitive type - double. JNI type jdouble.
JVMTI_TYPE_JFIELDID
Java programming language field identifier - JNI type jfieldID.
JVMTI_TYPE_JFLOAT
Java programming language primitive type - float. JNI type jfloat.
JVMTI_TYPE_JINT
Java programming language primitive type - int. JNI type jint.
JVMTI_TYPE_JLONG
Java programming language primitive type - long. JNI type jlong.
JVMTI_TYPE_JMETHODID
Java programming language method identifier - JNI type jmethodID.
JVMTI_TYPE_JNIENV
JNI environment - JNIEnv. Should be used with the correct jvmtiParamKind to make it a pointer type.
JVMTI_TYPE_JOBJECT
Java programming language object type - java.lang.Object. JNI type jobject. Returned values are JNI local references and must be managed.
JVMTI_TYPE_JSHORT
Java programming language primitive type - short. JNI type jshort.
JVMTI_TYPE_JTHREAD
Java programming language object type - java.lang.Thread. JVM TI type jthread. Returned values are JNI local references and must be managed.
JVMTI_TYPE_JVALUE
Union of all Java programming language primitive and object types - JNI type jvalue. Returned values which represent object types are JNI local references and must be managed.
JVMTI_VERSION_1
JVMTI_VERSION_9
JVMTI_VERSION_1_0
JVMTI_VERSION_1_1
JVMTI_VERSION_1_2
JVMTI_VERSION_11
JVMTI_VERSION_19
JVMTI_VERSION_21
JVMTI_VISIT_ABORT
JVMTI_VISIT_OBJECTS

Traits§

AsJNILinkage
JType
Marker trait for all types that are valid to use to make variadic JNI Up-calls with.
UseCString
Helper trait that converts rusts various strings into a zero terminated c string for use with a JNI method.

Functions§

JNI_CreateJavaVM
Directly calls JNI_CreateJavaVM with the provided arguments.
JNI_CreateJavaVM_with_string_args
Convenience function to call JNI_CreateJavaVM with a simple list of String arguments.
JNI_GetCreatedJavaVMs
Returns the created JavaVMs in the given vms slice. All remaining elements in the slice are set to None. The count of returned JavaVMs is returned in the result.
JNI_GetCreatedJavaVMs_first
Returns the first created JavaVM or None in the result.
init_dynamic_link
Call this function to initialize the dynamic linking to the jvm to use the provided function pointers to create the jvm.
is_jvm_loaded
Returns true if the jvm was loaded by either calling load_jvm_from_library or init_dynamic_link.
load_jvm_from_java_home
Convenience method to load the jvm from the JAVA_HOME environment variable that is commonly set on Windows by End-User Java Setups, or on linux by distribution package installers.
load_jvm_from_java_home_folder
Convinience method to load the jvm from a given path to a java installation. Info: The java_home parameter should refer to a path of a folder, which directly contains the “bin” or “jre” folder.
load_jvm_from_library
Convenience method to load the jvm from a path to libjvm.so or jvm.dll.

Type Aliases§

jarray
jboolean
jbooleanArray
jbyte
jbyteArray
jchar
jcharArray
jclass
jdouble
jdoubleArray
jfieldID
jfloat
jfloatArray
jint
jintArray
jlocation
jlong
jlongArray
jmethodID
jobject
jobjectArray
jrawMonitorID
jshort
jshortArray
jsize
jstring
jthread
jthreadGroup
jthrowable
jvalue
jvmtiArrayPrimitiveValueCallback
jvmtiEventBreakpoint
jvmtiEventClassFileLoadHook
jvmtiEventClassLoad
jvmtiEventClassPrepare
jvmtiEventCompiledMethodLoad
jvmtiEventCompiledMethodUnload
jvmtiEventDataDumpRequest
jvmtiEventDynamicCodeGenerated
jvmtiEventException
jvmtiEventExceptionCatch
jvmtiEventFieldAccess
jvmtiEventFieldModification
jvmtiEventFramePop
jvmtiEventGarbageCollectionFinish
jvmtiEventGarbageCollectionStart
jvmtiEventMethodEntry
jvmtiEventMethodExit
jvmtiEventMonitorContendedEnter
jvmtiEventMonitorContendedEntered
jvmtiEventMonitorWait
jvmtiEventMonitorWaited
jvmtiEventNativeMethodBind
jvmtiEventObjectFree
jvmtiEventReserved
jvmtiEventResourceExhausted
jvmtiEventSampledObjectAlloc
jvmtiEventSingleStep
jvmtiEventThreadEnd
jvmtiEventThreadStart
jvmtiEventVMDeath
jvmtiEventVMInit
jvmtiEventVMObjectAlloc
jvmtiEventVMStart
jvmtiEventVirtualThreadEnd
jvmtiEventVirtualThreadStart
jvmtiExtensionEvent
jvmtiExtensionFunction
jvmtiHeapIterationCallback
jvmtiHeapObjectCallback
jvmtiHeapReferenceCallback
jvmtiHeapRootCallback
jvmtiHeapRootKind
jvmtiHeapRootKind cant enum this because we are called with it, making addition in a future version of JVMTI UB in rust.
jvmtiJlocationFormat
jvmtiObjectReferenceCallback
jvmtiObjectReferenceKind
jvmtiHeapRootKind cant enum this because we are called with it, making addition in a future version of JVMTI UB in rust.
jvmtiParamKind
jvmtiParamTypes
jvmtiPhase
jvmtiPrimitiveFieldCallback
jvmtiReservedCallback
jvmtiStackReferenceCallback
jvmtiStartFunction
jvmtiStringPrimitiveValueCallback
jvmtiTimerKind
jweak

Unions§

jtype
jvmtiHeapReferenceInfo