Skip to content

Commit da3d4b9

Browse files
authored
Merge pull request #5 from vadage/develop
Merge develop into main
2 parents 3feb7a3 + b87d74e commit da3d4b9

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ on:
1010

1111
jobs:
1212
build:
13-
runs-on: windows-latest
13+
strategy:
14+
matrix:
15+
os: [windows-latest, ubuntu-latest, macos-latest]
16+
17+
runs-on: ${{ matrix.os }}
1418

1519
steps:
1620
- name: Checkout code

.github/workflows/release.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ on:
66

77
jobs:
88
build:
9-
runs-on: windows-latest
9+
strategy:
10+
matrix:
11+
target: [
12+
{ os: windows-latest, extension: dll },
13+
{ os: ubuntu-latest, extension: so },
14+
{ os: macos-latest, extension: dylib }
15+
]
16+
17+
runs-on: ${{ matrix.target.os }}
1018

1119
steps:
1220
- name: Checkout code
@@ -31,5 +39,5 @@ jobs:
3139
env:
3240
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3341
with:
34-
file: "target/release/*.dll"
42+
file: target/release/*.${{ matrix.target.extension }}
3543
update_latest_release: true

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ crate-type = ["cdylib"]
1111

1212
[dependencies]
1313
jni = "0.21.1"
14+
libloading = "0.8.1"
1415
retour = { version = "0.3.1", features = ["static-detour"] }
15-
winapi = { version = "0.3.9", features = ["libloaderapi"] }
1616

1717
[profile.release]
1818
panic = "abort"
1919
codegen-units = 1
2020
lto = true
2121
opt-level = 2
22-
strip = true
22+
strip = true

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ Keep this in mind as you use this feature in your projects.
1616
All Java versions should be supported, as `jni` is only used for the structs and no method calls or such.<br>
1717
Thus far only `Java 8`, `Java 20` and `Java 21` were explicitly tested.
1818

19-
### ⚠️ Operating system
20-
The build targets only windows, as the library heavily relies on functionality of `winapi`.
19+
### ✅ Operating system
20+
The build targets all major architectures
21+
- Linux
22+
- Windows
23+
- MacOS
2124

2225
# How to build
2326
1. [Install rust](https://www.rust-lang.org/tools/install)

src/class_loader.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::ffi::CString;
21
use std::mem;
2+
use std::os::raw::c_char;
33
use std::slice::from_raw_parts_mut;
44

55
use retour::static_detour;
66
use jni::sys::{jbyte, jclass, JNIEnv, jobject, jsize};
7-
use winapi::ctypes::c_char;
8-
use winapi::um::libloaderapi::{GetModuleHandleA, GetProcAddress};
7+
use libloading::Library;
98

109
use crate::jvm::DefineClassCommon;
1110

@@ -25,17 +24,12 @@ impl ClassLoader {
2524
const JAVA_MAGIC_VALUE: [jbyte; 4] = convert_magic_number(0xCAFEBABE);
2625

2726
pub unsafe fn setup_hook() {
28-
let module = CString::new("jvm.dll").unwrap();
29-
let handle = GetModuleHandleA(module.as_ptr());
30-
31-
let method_name = CString::new("JVM_DefineClassWithSource").unwrap();
32-
let method = GetProcAddress(handle, method_name.as_ptr());
27+
let handle = Library::new("jvm").expect("Could not find jvm library.");
28+
let method = handle.get::<DefineClassCommon>(b"JVM_DefineClassWithSource").expect("Could not find exported function.");
3329
let target: DefineClassCommon = mem::transmute(method);
3430

35-
DefineClassCommonHook.initialize(target, ClassLoader::hooked_define_class_common)
36-
.unwrap()
37-
.enable()
38-
.expect("Unable to hook into class loading.");
31+
let hook = DefineClassCommonHook.initialize(target, ClassLoader::hooked_define_class_common).expect("Could not initialize hook for class loading.");
32+
hook.enable().expect("Could not to hook into class loading.");
3933
}
4034

4135
fn hooked_define_class_common(env: JNIEnv, name: *const c_char, loader: jobject, buf: *const jbyte, len: jsize, pd: jobject, source: *const c_char) -> jclass {

src/jvm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
use std::os::raw::c_char;
12
use jni::sys::{JNIEnv, jbyte, jsize, jobject, jclass};
2-
use winapi::ctypes::c_char;
33

44
pub type DefineClassCommon = fn(JNIEnv, *const c_char, jobject, *const jbyte, jsize, jobject, *const c_char) -> jclass;

src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
use winapi::um::winnt::DLL_PROCESS_ATTACH;
2-
31
mod class_loader;
42
mod jvm;
53

4+
use std::os::raw::c_void;
5+
use jni::JavaVM;
6+
use jni::sys::{jint, JNI_VERSION_1_1};
67
use class_loader::ClassLoader;
78

89
#[no_mangle]
9-
pub unsafe extern "stdcall" fn DllMain(_instance: u32, reason: u32, _reserved: *mut u8) -> bool {
10-
11-
if reason == DLL_PROCESS_ATTACH {
12-
ClassLoader::setup_hook();
13-
}
14-
15-
return true;
10+
pub unsafe extern "system" fn JNI_OnLoad(_: JavaVM, _: *mut c_void) -> jint {
11+
ClassLoader::setup_hook();
12+
return JNI_VERSION_1_1;
1613
}

0 commit comments

Comments
 (0)