Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Dec 1, 2024
1 parent 82b0ff5 commit 1d90028
Show file tree
Hide file tree
Showing 33 changed files with 252 additions and 81 deletions.
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ classfile = { path = "classfile" }
common = { path = "common" }
instructions = { path = "instructions" }
jni = { path = "jni" }
jni_sys = { path = "jni/sys" }
symbols = { path = "symbols" }

byte-slice-cast = "1.2.2"
clap = "4.5.20"
const_format = "0.2.30"
cesu8 = "1.1.0"
const_format = "0.2.33"
libc = "0.2"
libloading = "0.8.5"
tracing = "0.1.40"
once_cell = "1.17.1"
paste = "1.0.12"
zip = "0.6.3"
ouroboros = "0.18.4"
tracing = "0.1.41"
paste = "1.0.15"
zip = "2.2.1"

# Nested workspace members
tools = { path = "tools" }
Expand Down
4 changes: 2 additions & 2 deletions classfile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
common = { path = "../common" }
paste = { workspace = true }
common.workspace = true
paste.workspace = true
9 changes: 7 additions & 2 deletions generators/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ members = [
]

[workspace.dependencies]
syn = { version = "2", features = ["full"] }
common = { path = "../common" }

combine = "4.6.7"
bitflags = "2.6.0"
proc-macro2 = "1"
quote = "1"
quote = "1"
syn = { version = "2", default-features = false }
walkdir = "2.5.0"
8 changes: 4 additions & 4 deletions generators/native_methods/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bitflags = "2.2.1"
combine = "4.6.6"
common = { path = "../../common" }
walkdir = "2.3.2"
bitflags.workspace = true
combine.workspace = true
common.workspace = true
walkdir.workspace = true
6 changes: 3 additions & 3 deletions generators/vm_symbols/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"
proc-macro = true

[dependencies]
syn = { version = "2", features = ["full"] }
proc-macro2 = "1"
quote = "1"
syn = { workspace = true, features = ["full"] }
proc-macro2.workspace = true
quote.workspace = true
2 changes: 1 addition & 1 deletion instructions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
common = { path = "../common"}
common.workspace = true
8 changes: 4 additions & 4 deletions jimage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
byte-slice-cast = { workspace = true }
common = { path = "../common" }
ouroboros = "0.15.5"
zip = { workspace = true }
byte-slice-cast.workspace = true
common.workspace = true
ouroboros.workspace = true
zip.workspace = true
4 changes: 2 additions & 2 deletions jmod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
common = { path = "../common" }
zip = { workspace = true }
common.workspace = true
zip.workspace = true
4 changes: 2 additions & 2 deletions jni/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jni_sys = { path = "sys" }
jni_sys.workspace = true
libloading.workspace = true
cesu8 = "1.1.0"
cesu8.workspace = true
4 changes: 2 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ instructions = { path = "../instructions" }
jimage = { path = "../jimage" }
jni = { path = "../jni" }

bytemuck = "1.13.0"
bytemuck = "1.20.0"
byte-slice-cast = { workspace = true }
tracing = { workspace = true }
libc.workspace = true
num_cpus = "1.15.0"
num_cpus = "1.16.0"
paste = { workspace = true }
platform = { path = "../platform" }
seq-macro = "0.3.5"
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/calls/jcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! java_call {
tracing::debug!(target: "java_call", "Invoking manual Java call for method `{:?}`", $method);
let max_locals = $method.code.max_locals;
let __ret = $thread.invoke_method_scoped($method, $crate::stack::local_stack::LocalStack::new_with_args(vec![$(Operand::from($arg)),+], max_locals as usize));
tracing::debug!(target: "java_call", "Manual Java call finished");
tracing::debug!(target: "java_call", "Manual Java call finished for method `{:?}`", $method);
__ret
}};
// No arguments path, still needs to allocate a LocalStack for stores
Expand Down
1 change: 1 addition & 0 deletions runtime/src/globals/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ define_classes!(
java_lang_Cloneable,
java_io_FileDescriptor,
java_io_FileInputStream,
java_io_FileOutputStream,
// Primitive arrays
bool_array,
byte_array,
Expand Down
39 changes: 23 additions & 16 deletions runtime/src/initialization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::class::Class;
use crate::class_instance::ClassInstance;
use crate::classpath::classloader::ClassLoader;
use crate::java_call;
Expand Down Expand Up @@ -58,7 +57,12 @@ fn initialize_thread(thread: &mut JavaThread) {
}
}

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

// Init some important classes
initialize_global_classes(thread);

create_thread_object(thread);

// https://github.com/openjdk/jdk/blob/04591595374e84cfbfe38d92bff4409105b28009/src/hotspot/share/runtime/threads.cpp#L408
Expand Down Expand Up @@ -119,6 +123,15 @@ fn load_global_classes() {
)
}

fn initialize_global_classes(thread: &mut 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);

crate::globals::classes::java_lang_Thread().initialize(thread);
crate::globals::classes::java_lang_ThreadGroup().initialize(thread);
}

fn create_thread_object(thread: &mut 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 @@ -159,11 +172,9 @@ fn create_thread_object(thread: &mut JavaThread) {
/// * Thread group of the main thread
fn init_phase_1(thread: &mut 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),
)
.unwrap();
let init_phase_1 = system_class
.resolve_method_step_two(sym!(initPhase1_name), sym!(void_method_signature))
.unwrap();

java_call!(thread, init_phase_1);
}
Expand All @@ -180,11 +191,9 @@ fn init_phase_2(thread: &mut JavaThread) {
let print_stacktrace_on_exception = true;

// TODO: Need some way to check failure
let init_phase_2 = system_class.resolve_method_step_two(
sym!(initPhase2_name),
sym!(bool_bool_int_signature),
)
.unwrap();
let init_phase_2 = system_class
.resolve_method_step_two(sym!(initPhase2_name), sym!(bool_bool_int_signature))
.unwrap();

java_call!(
thread,
Expand All @@ -208,11 +217,9 @@ fn init_phase_2(thread: &mut JavaThread) {
fn init_phase_3(thread: &mut JavaThread) {
let system_class = ClassLoader::Bootstrap.load(sym!(java_lang_System)).unwrap();

let init_phase_3 = system_class.resolve_method_step_two(
sym!(initPhase3_name),
sym!(void_method_signature),
)
.unwrap();
let init_phase_3 = system_class
.resolve_method_step_two(sym!(initPhase3_name), sym!(void_method_signature))
.unwrap();

java_call!(thread, init_phase_3);
}
2 changes: 2 additions & 0 deletions runtime/src/native/java/io/FileDescriptor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(non_upper_case_globals)]

use crate::classpath::classloader::ClassLoader;
use crate::reference::Reference;

Expand Down
2 changes: 2 additions & 0 deletions runtime/src/native/java/io/FileInputStream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(non_upper_case_globals)]

use crate::classpath::classloader::ClassLoader;
use crate::reference::Reference;

Expand Down
66 changes: 66 additions & 0 deletions runtime/src/native/java/io/FileOutputStream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::classpath::classloader::ClassLoader;
use crate::native::Reference;

use std::cell::UnsafeCell;
use std::ptr::NonNull;
use std::sync::atomic::{AtomicBool, Ordering};

use ::jni::env::JniEnv;
use ::jni::sys::{jboolean, jint};
use symbols::sym;

include_generated!("native/java/io/def/FileOutputStream.definitions.rs");

/// `java.io.FileInputStream#fd` field offset
static mut fd: UnsafeCell<usize> = UnsafeCell::new(0);

// throws FileNotFoundException
pub fn open0(_: NonNull<JniEnv>, _this: Reference, _name: Reference /* java.lang.String */) {
unimplemented!("java.io.FileOutputStream#open0");
}

// throws IOException
pub fn write(_: NonNull<JniEnv>, _this: Reference, _b: jint, _append: jboolean) {
unimplemented!("java.io.FileOutputStream#write");
}

// throws IOException
pub fn writeBytes(
_: NonNull<JniEnv>,
_this: Reference,
_b: Reference, // byte[]
_off: jint,
_len: jint,
_append: jboolean,
) {
unimplemented!("java.io.FileOutputStream#write");
}

pub fn initIDs(_: NonNull<JniEnv>) {
static ONCE: AtomicBool = AtomicBool::new(false);
if ONCE
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_err()
{
// TODO
panic!("java.io.FileOutputStream#initIDs: attempt to initialize more than once.");
}

let class = ClassLoader::lookup_class(sym!(java_io_FileOutputStream)).unwrap();
unsafe {
crate::globals::classes::set_java_io_FileOutputStream(class);
}

let mut field_set = false;
for (index, field) in class.fields().enumerate() {
if field.name == sym!(fd) {
unsafe {
*fd.get_mut() = index;
}
field_set = true;
break;
}
}

assert!(field_set, "Field must be present");
}
25 changes: 25 additions & 0 deletions runtime/src/native/java/io/def/FileOutputStream.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.lang.String;

public class FileOutputStream {
// Opens a file, with the specified name, for overwriting or appending.
// @param name name of file to be opened
// @param append whether the file is to be opened in append mode
private native void open0(String name) throws FileNotFoundException;

// Writes the specified byte to this file output stream.
//
// @param b the byte to be written.
// @param append true if the write operation first advances the position to the end of file
private native void write(int b, boolean append) throws IOException;

// Writes a sub array as a sequence of bytes.
// @param b the data to be written
// @param off the start offset in the data
// @param len the number of bytes that are written
// @param append true to first advance the position to the end of file
// @throws IOException If an I/O error has occurred.
private native void writeBytes(byte[] b, int off, int len, boolean append)
throws IOException;

private static native void initIDs();
}
12 changes: 10 additions & 2 deletions runtime/src/native/java/lang/System.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::classpath::classloader::ClassLoader;
use crate::reference::Reference;

use std::ptr::NonNull;
Expand All @@ -6,12 +7,19 @@ use std::time::{SystemTime, UNIX_EPOCH};
use ::jni::env::JniEnv;
use ::jni::sys::{jint, jlong};
use common::traits::PtrType;
use instructions::Operand;
use symbols::sym;

include_generated!("native/java/lang/def/System.registerNatives.rs");
include_generated!("native/java/lang/def/System.definitions.rs");

pub fn setIn0(_: NonNull<JniEnv>, _in_: Reference /* java.io.PrintStream */) {
unimplemented!("System#setIn0")
pub fn setIn0(_: NonNull<JniEnv>, in_: Reference /* java.io.InputStream */) {
let class = ClassLoader::lookup_class(sym!(java_lang_System)).unwrap();
let field = class
.fields()
.find(|field| field.name == sym!(r#in) && field.descriptor.is_class(b"java/io/InputStream"))
.expect("java/lang/System#in field should exist");
field.set_static_value(Operand::Reference(in_));
}
pub fn setOut0(_env: NonNull<JniEnv>, _out: Reference /* java.io.PrintStream */) {
unimplemented!("System#setOut0")
Expand Down
17 changes: 17 additions & 0 deletions runtime/src/native/jdk/internal/misc/ScopedMemoryAccess.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::reference::Reference;

use std::ptr::NonNull;

use ::jni::env::JniEnv;

include_generated!("native/jdk/internal/misc/def/ScopedMemoryAccess.definitions.rs");
include_generated!("native/jdk/internal/misc/def/ScopedMemoryAccess.registerNatives.rs");

pub fn closeScope0(
_env: NonNull<JniEnv>,
_this: Reference, // jdk.internal.misc.ScopedMemoryAccess
_session: Reference, // jdk.internal.foreign.MemorySessionImpl
_error: Reference, // jdk.internal.misc.ScopedMemoryAccess.ScopedAccessError
) -> Reference {
unimplemented!("jdk.internal.misc.ScopedMemoryAccess#closeScope0")
}
1 change: 0 additions & 1 deletion runtime/src/native/jdk/internal/misc/Unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::class::Class;
use crate::class_instance::Instance;
use crate::native::JniEnv;
use crate::objects::class::ClassInitializationState;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.misc.ScopedMemoryAccess.ScopedAccessError;

public class ScopedMemoryAccess {
private static native void registerNatives();

native void closeScope0(MemorySessionImpl session, ScopedAccessError error);
}
Loading

0 comments on commit 1d90028

Please sign in to comment.