Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Dec 14, 2024
1 parent 5b12e59 commit 98a637f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 59 deletions.
15 changes: 15 additions & 0 deletions runtime/src/globals/field_offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,18 @@ pub fn field_holder_thread_status_field_offset() -> usize {
pub unsafe fn set_field_holder_thread_status_field_offset(value: usize) {
FIELDHOLDER_THREAD_STATUS_FIELD_OFFSET = value;
}

static mut THREAD_EETOP_FIELD_OFFSET: usize = 0;

/// `java.lang.Thread#eetop` field offset
///
/// This will not change for the lifetime of the program.
///
/// Expected type: `jlong`
pub fn thread_eetop_field_offset() -> usize {
unsafe { THREAD_EETOP_FIELD_OFFSET }
}

pub unsafe fn set_thread_eetop_field_offset(value: usize) {
THREAD_EETOP_FIELD_OFFSET = value;
}
2 changes: 1 addition & 1 deletion runtime/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn initialize_thread(thread: &JavaThread) {
}

// Grab the java.lang.Thread field offsets
JavaThread::set_field_holder_offsets();
JavaThread::set_field_offsets();

// Init some important classes
initialize_global_classes(thread);
Expand Down
8 changes: 6 additions & 2 deletions runtime/src/native/jdk/internal/misc/Unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ where
}

#[doc(hidden)]
unsafe fn __put_raw(&self, _value: T) {
unimplemented!("put at raw pointer offsets");
unsafe fn __put_raw(&self, value: T) {
let offset = self.offset;
let ptr = offset as *mut T;
unsafe {
*ptr = value;
}
}

#[doc(hidden)]
Expand Down
42 changes: 21 additions & 21 deletions runtime/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ pub(self) fn insert_method((def, ptr): (NativeMethodDef, NativeMethodPtr)) {

// Module marker, do not remove

pub(crate) mod jdk {
pub(crate) mod internal {
pub(crate) mod misc {
pub(crate) mod ScopedMemoryAccess;
pub(crate) mod CDS;
pub(crate) mod VM;
pub(crate) mod Unsafe;
pub(crate) mod Signal;
}
pub(crate) mod util {
pub(crate) mod SystemProps;
}
pub(crate) mod loader {
pub(crate) mod NativeLibraries;
}
pub(crate) mod reflect {
pub(crate) mod Reflection;
}
}
}

pub(crate) mod java {
pub(crate) mod io {
pub(crate) mod FileInputStream;
Expand Down Expand Up @@ -111,24 +132,3 @@ pub(crate) mod java {
}
}

pub(crate) mod jdk {
pub(crate) mod internal {
pub(crate) mod misc {
pub(crate) mod ScopedMemoryAccess;
pub(crate) mod CDS;
pub(crate) mod VM;
pub(crate) mod Unsafe;
pub(crate) mod Signal;
}
pub(crate) mod util {
pub(crate) mod SystemProps;
}
pub(crate) mod loader {
pub(crate) mod NativeLibraries;
}
pub(crate) mod reflect {
pub(crate) mod Reflection;
}
}
}

94 changes: 59 additions & 35 deletions runtime/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,57 +223,70 @@ pub enum ThreadStatus {

/// `java.lang.Thread$FieldHolder` accessors
impl JavaThread {
pub fn set_field_holder_offsets() {
// java.lang.Thread#holder
pub fn set_field_offsets() {
// java.lang.Thread fields
{
let class = crate::globals::classes::java_lang_Thread();
for (index, field) in class
.fields()
.filter(|field| !field.is_static())
.enumerate()
{

let mut field_set = 0;
for (index, field) in class.instance_fields().enumerate() {
if field.name == sym!(holder) {
unsafe {
crate::globals::field_offsets::set_thread_holder_field_offset(index);
}

break;
field_set |= 1;
continue;
}
}
}

// java.lang.Thread$FieldHolder fields
{
let class = crate::globals::classes::java_lang_Thread_FieldHolder();
if field.name == sym!(eetop) {
unsafe {
crate::globals::field_offsets::set_thread_eetop_field_offset(index);
}

let mut field_set = 0;
for (index, field) in class.fields().enumerate() {
match field.name.as_str() {
"priority" => unsafe {
crate::globals::field_offsets::set_field_holder_priority_field_offset(
index,
);
field_set |= 1;
},
"daemon" => unsafe {
crate::globals::field_offsets::set_field_holder_daemon_field_offset(index);
field_set |= 1 << 1;
},
"threadStatus" => unsafe {
crate::globals::field_offsets::set_field_holder_thread_status_field_offset(
index,
);
field_set |= 1 << 2;
},
_ => {},
field_set |= 1 << 1;
continue;
}
}

assert_eq!(
field_set, 0b111,
"Not all fields were found in java/lang/Thread$FieldHolder"
field_set, 0b11,
"Not all fields were found in java/lang/Thread"
);
}

Self::set_field_holder_offsets();
}

// java.lang.Thread$FieldHolder fields
fn set_field_holder_offsets() {
let class = crate::globals::classes::java_lang_Thread_FieldHolder();

let mut field_set = 0;
for (index, field) in class.fields().enumerate() {
match field.name.as_str() {
"priority" => unsafe {
crate::globals::field_offsets::set_field_holder_priority_field_offset(index);
field_set |= 1;
},
"daemon" => unsafe {
crate::globals::field_offsets::set_field_holder_daemon_field_offset(index);
field_set |= 1 << 1;
},
"threadStatus" => unsafe {
crate::globals::field_offsets::set_field_holder_thread_status_field_offset(
index,
);
field_set |= 1 << 2;
},
_ => {},
}
}

assert_eq!(
field_set, 0b111,
"Not all fields were found in java/lang/Thread$FieldHolder"
);
}

fn set_field_holder_field(&self, offset: usize, value: Operand<Reference>) {
Expand Down Expand Up @@ -367,6 +380,7 @@ impl JavaThread {

// Set the obj early, since the java.lang.Thread constructor calls Thread#current.
self.set_obj(Reference::class(ClassInstanceRef::clone(&thread_instance)));
self.set_eetop();

let init_method = thread_class
.vtable()
Expand Down Expand Up @@ -409,6 +423,16 @@ impl JavaThread {
let obj_opt = unsafe { &*obj_ptr };
obj_opt.as_ref().map(Reference::clone)
}

fn set_eetop(&self) {
let offset = crate::globals::field_offsets::thread_eetop_field_offset();

let obj = self.obj().unwrap();
obj.extract_class().get_mut().put_field_value0(
offset,
Operand::Long(JavaThread::current_ptr() as jni::sys::jlong),
);
}
}

impl JavaThread {
Expand Down
1 change: 1 addition & 0 deletions symbols/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ vm_symbols::define_symbols! {

// Fields
holder: "holder",
eetop: "eetop",
value: "value",
coder: "coder",
fd: "fd",
Expand Down

0 comments on commit 98a637f

Please sign in to comment.