Skip to content

Commit

Permalink
Add stubs for jdk.internal.misc.Unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Oct 17, 2024
1 parent 073edd7 commit c133c57
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
9 changes: 9 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ fn should_do_trivial_reflection_with_primitives() {
assert_eq!(9369, get_int(last_frame_value))
}

#[test]
fn should_do_trivial_unsafe_things() {
let mut vm = VM::new("std");
let last_frame_value = vm
.run("samples.jdkinternal.unsafe.trivial.UnsafeUsage")
.unwrap();
assert_eq!(1, get_int(last_frame_value))
}

fn get_int(locals: Option<Vec<i32>>) -> i32 {
*locals.unwrap().last().unwrap()
}
Expand Down
15 changes: 15 additions & 0 deletions tests/test_data/UnsafeUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// javac --add-exports java.base/jdk.internal.misc=ALL-UNNAMED -d . UnsafeUsage.java

package samples.jdkinternal.unsafe.trivial;

import jdk.internal.misc.Unsafe;

public class UnsafeUsage {

public static void main(String[] args) {
Unsafe unsafe = Unsafe.getUnsafe();

int isBigEndian = !unsafe.isBigEndian() ? 1 : 0;
}

}
Binary file not shown.
12 changes: 12 additions & 0 deletions vm/src/execution_engine/system_native_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ static SYSTEM_NATIVE_TABLE: Lazy<
"jdk/internal/misc/Unsafe:registerNatives:()V",
void_stub as fn(&[i32]) -> crate::error::Result<Vec<i32>>,
);
table.insert(
"jdk/internal/misc/Unsafe:arrayBaseOffset0:(Ljava/lang/Class;)I",
int_stub as fn(&[i32]) -> crate::error::Result<Vec<i32>>,
);
table.insert(
"jdk/internal/misc/Unsafe:arrayIndexScale0:(Ljava/lang/Class;)I",
int_stub as fn(&[i32]) -> crate::error::Result<Vec<i32>>,
);
table.insert(
"java/lang/String:intern:()Ljava/lang/String;",
intern_wrp as fn(&[i32]) -> crate::error::Result<Vec<i32>>,
Expand Down Expand Up @@ -61,3 +69,7 @@ fn void_stub(_args: &[i32]) -> crate::error::Result<Vec<i32>> {
fn bool_stub(_args: &[i32]) -> crate::error::Result<Vec<i32>> {
Ok(vec![false as i32])
}

fn int_stub(_args: &[i32]) -> crate::error::Result<Vec<i32>> {
Ok(vec![0])
}
37 changes: 28 additions & 9 deletions vm/src/method_area/method_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,34 @@ impl MethodArea {
}

fn generate_synthetic_classes() -> HashMap<String, Arc<JavaClass>> {
["I", "J", "F", "D", "B", "C", "S", "Z", "V"]
.into_iter()
.map(|class_name| {
(
class_name.to_string(),
Self::generate_synthetic_class(class_name),
)
})
.collect()
[
"I",
"J",
"F",
"D",
"B",
"C",
"S",
"Z",
"V",
"[I",
"[J",
"[F",
"[D",
"[B",
"[C",
"[S",
"[Z", //todo implement real Classes for arrays
"[Ljava/lang/Object;",
] //todo implement real Classes for array of objects
.into_iter()
.map(|class_name| {
(
class_name.to_string(),
Self::generate_synthetic_class(class_name),
)
})
.collect()
}

fn generate_synthetic_class(class_name: &str) -> Arc<JavaClass> {
Expand Down

0 comments on commit c133c57

Please sign in to comment.