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 6b63e1e commit 5b12e59
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 157 deletions.
2 changes: 1 addition & 1 deletion runtime/src/calls/jcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///
/// # Parameters
///
/// * `thread` - `&mut JavaThread`
/// * `thread` - `&JavaThread`
/// * `method` - `&'static Method`
/// * `arg`(s) - `Operand`
///
Expand Down
12 changes: 3 additions & 9 deletions runtime/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Frame {
// and a reference to the run-time constant pool (§2.5.5)
constant_pool: ConstantPoolRef,
method: &'static Method,
thread: UnsafeCell<*mut JavaThread>,
thread: UnsafeCell<*const JavaThread>,

// Used to remember the last pc when we return to a frame after a method invocation
cached_pc: AtomicIsize,
Expand All @@ -42,7 +42,7 @@ impl Debug for Frame {
impl Frame {
/// Create a new `Frame` for a [`Method`] invocation
pub fn new(
thread: &mut JavaThread,
thread: &JavaThread,
locals: LocalStack,
max_stack: u2,
constant_pool: ConstantPoolRef,
Expand All @@ -53,7 +53,7 @@ impl Frame {
stack: OperandStack::new(max_stack as usize),
constant_pool,
method,
thread: UnsafeCell::new(&raw mut *thread),
thread: UnsafeCell::new(&raw const *thread),
cached_pc: AtomicIsize::default(),
}
}
Expand All @@ -67,12 +67,6 @@ impl Frame {
unsafe { &**self.thread.get() }
}

/// Get a mutable reference to the associated [`JavaThread`]
#[inline]
pub fn thread_mut(&self) -> &mut JavaThread {
unsafe { &mut **self.thread.get() }
}

/// Get a reference to the constant pool
#[inline]
pub fn constant_pool(&self) -> ConstantPoolRef {
Expand Down
14 changes: 7 additions & 7 deletions runtime/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub fn create_java_vm(args: Option<&JavaVMInitArgs>) -> JavaVm {
JavaThread::set_current_thread(thread);
}

initialize_thread(JavaThread::current_mut());
initialize_thread(JavaThread::current());
unsafe { main_java_vm() }
}

fn initialize_thread(thread: &mut JavaThread) {
fn initialize_thread(thread: &JavaThread) {
// Load some important classes first
load_global_classes();

Expand Down Expand Up @@ -126,7 +126,7 @@ fn load_global_classes() {
)
}

fn initialize_global_classes(thread: &mut JavaThread) {
fn initialize_global_classes(thread: &JavaThread) {
crate::globals::classes::java_lang_Object().initialize(thread);
crate::globals::classes::java_lang_Class().initialize(thread);
crate::globals::classes::java_lang_String().initialize(thread);
Expand All @@ -136,7 +136,7 @@ fn initialize_global_classes(thread: &mut JavaThread) {
crate::globals::classes::java_lang_ref_Finalizer().initialize(thread);
}

fn create_thread_object(thread: &mut JavaThread) {
fn create_thread_object(thread: &JavaThread) {
let thread_group_class = crate::globals::classes::java_lang_ThreadGroup();
let system_thread_group_instance = Reference::class(ClassInstance::new(thread_group_class));

Expand Down Expand Up @@ -174,7 +174,7 @@ fn create_thread_object(thread: &mut JavaThread) {
/// * Signal handlers
/// * OS-specific system settings
/// * Thread group of the main thread
fn init_phase_1(thread: &mut JavaThread) {
fn init_phase_1(thread: &JavaThread) {
let system_class = ClassLoader::Bootstrap.load(sym!(java_lang_System)).unwrap();
let init_phase_1 = system_class
.resolve_method_step_two(sym!(initPhase1_name), sym!(void_method_signature))
Expand All @@ -187,7 +187,7 @@ fn init_phase_1(thread: &mut JavaThread) {
///
/// This is responsible for initializing the module system. Prior to this point, the only module
/// available to us is `java.base`.
fn init_phase_2(thread: &mut JavaThread) {
fn init_phase_2(thread: &JavaThread) {
let system_class = ClassLoader::Bootstrap.load(sym!(java_lang_System)).unwrap();

// TODO: Actually set these arguments accordingly
Expand Down Expand Up @@ -218,7 +218,7 @@ fn init_phase_2(thread: &mut JavaThread) {
/// * Initialization of and setting the security manager
/// * Setting the system class loader
/// * Setting the thread context class loader
fn init_phase_3(thread: &mut JavaThread) {
fn init_phase_3(thread: &JavaThread) {
let system_class = ClassLoader::Bootstrap.load(sym!(java_lang_System)).unwrap();

let init_phase_3 = system_class
Expand Down
14 changes: 7 additions & 7 deletions runtime/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ macro_rules! comparisons {

macro_rules! control_return {
($frame:ident, $instruction:ident) => {{
let thread = $frame.thread_mut();
let thread = $frame.thread();
thread.drop_to_previous_frame(None);
}};
($frame:ident, $instruction:ident, $return_ty:ident) => {{
Expand All @@ -255,7 +255,7 @@ macro_rules! control_return {
);
}

let thread = $frame.thread_mut();
let thread = $frame.thread();
thread.drop_to_previous_frame(Some(value));
}};
}
Expand Down Expand Up @@ -672,7 +672,7 @@ impl Interpreter {
},
OpCode::athrow => {
let object_ref = frame.stack_mut().pop_reference();
let thread = frame.thread_mut();
let thread = frame.thread();
thread.throw_exception(object_ref);
},
OpCode::instanceof => { Self::instanceof_checkcast(frame, opcode) },
Expand Down Expand Up @@ -948,7 +948,7 @@ impl Interpreter {

let ret = class.resolve_field(constant_pool, field_ref_idx);
if ret.is_some() && is_static {
class.initialize(frame.thread_mut());
class.initialize(frame.thread());
}

ret
Expand All @@ -960,10 +960,10 @@ impl Interpreter {
let method = frame.method();
let class = method.class;

let ret = class.resolve_method(frame.thread_mut(), method_ref_idx);
let ret = class.resolve_method(frame.thread(), method_ref_idx);
if ret.is_some() && is_static {
// On successful resolution of the method, the class or interface that declared the resolved method is initialized if that class or interface has not already been initialized
class.initialize(frame.thread_mut());
class.initialize(frame.thread());
}

ret
Expand All @@ -983,7 +983,7 @@ impl Interpreter {
}

// On successful resolution of the class, it is initialized if it has not already been initialized
class.initialize(frame.thread_mut());
class.initialize(frame.thread());

ClassInstance::new(class)
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/method_invoker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl MethodInvoker {
///
/// This will not pop anything off of the stack of the current Frame
pub fn invoke_with_args(
thread: &mut JavaThread,
thread: &JavaThread,
method: &'static Method,
args: Vec<Operand<Reference>>,
) {
Expand Down Expand Up @@ -106,12 +106,12 @@ impl MethodInvoker {
local_stack[0] = this;
}

Self::invoke0_(frame.thread_mut(), method, local_stack);
Self::invoke0_(frame.thread(), method, local_stack);
}

fn invoke0_(thread: &mut JavaThread, method: &'static Method, local_stack: LocalStack) {
fn invoke0_(thread: &JavaThread, method: &'static Method, local_stack: LocalStack) {
trace_method!(method);
JavaThread::invoke_method_with_local_stack(thread, method, local_stack);
thread.invoke_method_with_local_stack(method, local_stack);
}

fn construct_local_stack(
Expand Down
15 changes: 8 additions & 7 deletions runtime/src/native/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a> NativeNameConverter<'a> {

fn lookup_style(
method: &Method,
thread: &mut JavaThread,
thread: &JavaThread,
name_converter: &NativeNameConverter<'_>,
num_args: usize,
include_long: bool,
Expand Down Expand Up @@ -233,8 +233,9 @@ fn lookup_style(
findNative_method,
Operand::Reference(Reference::null()),
Operand::Reference(Reference::class(name_arg))
).unwrap()
.expect_long();
)
.unwrap()
.expect_long();

if address == 0 {
todo!("Agent library search");
Expand All @@ -244,7 +245,7 @@ fn lookup_style(
Some(entry)
}

fn lookup_entry(method: &Method, thread: &mut JavaThread) -> Option<*const c_void> {
fn lookup_entry(method: &Method, thread: &JavaThread) -> Option<*const c_void> {
let mut name_converter = NativeNameConverter::new(method);

// Compute pure name
Expand Down Expand Up @@ -314,11 +315,11 @@ fn lookup_entry(method: &Method, thread: &mut JavaThread) -> Option<*const c_voi
}

/// Check if there are any JVM TI prefixes which have been applied to the native method name.
fn lookup_entry_prefixed(_method: &Method, _thread: &mut JavaThread) -> Option<*const c_void> {
fn lookup_entry_prefixed(_method: &Method, _thread: &JavaThread) -> Option<*const c_void> {
todo!()
}

fn lookup_base(method: &Method, thread: &mut JavaThread) -> *const c_void {
fn lookup_base(method: &Method, thread: &JavaThread) -> *const c_void {
if let Some(entry) = lookup_entry(method, thread) {
return entry;
}
Expand All @@ -331,7 +332,7 @@ fn lookup_base(method: &Method, thread: &mut JavaThread) -> *const c_void {
panic!("UnsatisfiedLinkError")
}

pub fn lookup_native_method(method: &Method, thread: &mut JavaThread) {
pub fn lookup_native_method(method: &Method, thread: &JavaThread) {
let native_method = method.native_method();
if !native_method.is_some() {
return;
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,27 +84,6 @@ 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 @@ -132,3 +111,24 @@ 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;
}
}
}

2 changes: 1 addition & 1 deletion runtime/src/objects/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ impl Class {
/// Attempt to initialize this class
///
/// NOTE: If the class is being initialized by another thread, this will block until it is completed.
pub fn initialize(&self, thread: &mut JavaThread) {
pub fn initialize(&self, thread: &JavaThread) {
if self.is_initialized.get() {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/objects/class/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Class {
#[tracing::instrument(skip_all)]
pub fn resolve_method<'a>(
&'a self,
thread: &mut JavaThread,
thread: &JavaThread,
method_ref_idx: u2,
) -> Option<&'static Method> {
let descriptor = self.unwrap_class_instance();
Expand Down Expand Up @@ -365,7 +365,7 @@ impl Class {
reason = "We have no way of checking of the <clinit> executed successfully yet"
)]
#[tracing::instrument(skip_all)]
pub fn initialization(&self, thread: &mut JavaThread) {
pub fn initialization(&self, thread: &JavaThread) {
// 1. Synchronize on the initialization lock, LC, for C. This involves waiting until the current thread can acquire LC.
let init = self.initialization_lock();
let mut guard = init.lock();
Expand Down Expand Up @@ -527,7 +527,7 @@ impl Class {
#[tracing::instrument(skip_all)]
pub fn construct(
&self,
thread: &mut JavaThread,
thread: &JavaThread,
descriptor: Symbol,
args: Vec<Operand<Reference>>,
) {
Expand Down Expand Up @@ -556,7 +556,7 @@ impl Class {
// https://docs.oracle.com/javase/specs/jvms/se19/html/jvms-2.html#jvms-2.9.2
#[rustfmt::skip]
#[tracing::instrument(skip_all)]
fn clinit(&self, thread: &mut JavaThread) {
fn clinit(&self, thread: &JavaThread) {
// A class or interface has at most one class or interface initialization method and is initialized
// by the Java Virtual Machine invoking that method (§5.5).

Expand Down
Loading

0 comments on commit 5b12e59

Please sign in to comment.