Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Jan 12, 2025
1 parent 2b766f1 commit 59784a4
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 152 deletions.
15 changes: 2 additions & 13 deletions generators/vm_symbols/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub fn define_symbols(input: TokenStream) -> TokenStream {
index += 1;
}

let r = quote! {
quote! {
const PREINTERED_SYMBOLS_COUNT: u32 = #index;

#[allow(non_upper_case_globals)]
Expand All @@ -228,16 +228,5 @@ pub fn define_symbols(input: TokenStream) -> TokenStream {
]);
}
}
};

let mut f = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open("/home/alex/foo.rs")
.unwrap();
use std::io::Write as _;
f.write_all(r.to_string().as_bytes()).unwrap();

r.into()
}.into()
}
10 changes: 5 additions & 5 deletions jimage/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl JImage {
}

// TODO: https://github.com/openjdk/jdk/blob/62a033ecd7058f4a4354ebdcd667b3d7991e1f3d/src/java.base/share/native/libjimage/jimage.cpp#L102
pub fn find_resource(&self, module_name: &str, name: &str, size: &mut u8) -> Option<u4> {
pub fn find_resource(&self, module_name: &str, name: &str) -> Option<(u4, u8)> {
// TBD: assert!(module_name.len() > 0, "module name must be non-empty");
if name.is_empty() {
// `name` must be non-empty
Expand All @@ -57,7 +57,7 @@ impl JImage {
}

let fullpath = format!("/{}/{}", module_name, name);
self.find_location_index(&fullpath, size)
self.find_location_index(&fullpath)
}

// https://github.com/openjdk/jdk/blob/f56285c3613bb127e22f544bd4b461a0584e9d2a/src/java.base/share/native/libjimage/imageFile.cpp#L523
Expand Down Expand Up @@ -173,7 +173,7 @@ impl JImage {
// https://github.com/openjdk/jdk/blob/f56285c3613bb127e22f544bd4b461a0584e9d2a/src/java.base/share/native/libjimage/imageFile.cpp#L464
/// Find the location index and size associated with the path.
/// Returns the location index and size if the location is found, `None` otherwise.
fn find_location_index(&self, path: &str, size: &mut u8) -> Option<u4> {
fn find_location_index(&self, path: &str) -> Option<(u4, u8)> {
// Locate the entry in the index perfect hash table.
let index = ImageStrings::find(self.endian, path, self.index.redirects_table());

Expand All @@ -188,8 +188,8 @@ impl JImage {

// Make sure result is not a false positive.
if self.verify_location(&location, path) {
*size = location.get_uncompressed_size();
return Some(offset);
let size = location.get_uncompressed_size();
return Some((offset, size));
}
}

Expand Down
4 changes: 2 additions & 2 deletions jimage/src/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const CLASSES: &[&str] = &[
];

fn image_file() -> Option<File> {
const JAVA_HOME: &str = env!("JAVA_HOME");
File::open(Path::new(JAVA_HOME).join("lib").join("modules")).ok()
let java_home = std::env::var("JAVA_HOME").expect("JAVA_HOME not set");
File::open(Path::new(&java_home).join("lib").join("modules")).ok()
}

#[test]
Expand Down
21 changes: 3 additions & 18 deletions runtime/src/classpath/classloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use common::traits::PtrType;
use symbols::{sym, Symbol};

const SUPPORTED_MAJOR_LOWER_BOUND: u1 = 45;
const SUPPORTED_MAJOR_UPPER_BOUND: u1 = 67;
const SUPPORTED_MAJOR_UPPER_BOUND: u1 = 69;
const SUPPORTED_MAJOR_VERSION_RANGE: RangeInclusive<u1> =
SUPPORTED_MAJOR_LOWER_BOUND..=SUPPORTED_MAJOR_UPPER_BOUND;

Expand Down Expand Up @@ -371,27 +371,12 @@ fn init_mirror(class: &'static Class) {
}

// Set the module
let module;
let java_base = crate::modules::java_base();
if !java_base.has_obj() {
if !crate::modules::java_base().has_obj() {
// Assume we are early in VM initialization, where `java.base` isn't a real module yet.
// In this case, we can assume that the intended module is `java.base`.
module = java_base;
} else {
todo!()
}

if !module.has_obj() {
assert_eq!(
module.name(),
Some(sym!(java_base)),
"only java.base can have no associated object"
);

// `java.base` isn't a real module yet. Will need to fix this up later.
class.mirror().get().set_module(Reference::null());
return;
}

class.mirror().get().set_module(module.obj());
todo!("Setting modules other than java.base");
}
6 changes: 2 additions & 4 deletions runtime/src/classpath/jimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ pub fn initialized() -> bool {

pub fn lookup_vm_resource(path: &str) -> Option<Vec<u1>> {
if let Some(file) = unsafe { &*JIMAGE_FILE.get() } {
let mut size = 0;

if let Some(location_offset) = file.find_resource("java.base", path, &mut size) {
if let Some((location_offset, size)) = file.find_resource("java.base", path) {
let mut uncompressed_data = vec![0; size as usize];
file.get_resource(location_offset, &mut uncompressed_data);

Expand All @@ -32,7 +30,7 @@ pub fn lookup_vm_options() -> Option<Vec<u1>> {
"Attempt to lookup vm options twice!"
);

let java_home = env!("JAVA_HOME");
let java_home = std::env::var("JAVA_HOME").expect("JAVA_HOME not set");

let modules_path_len = java_home.len()
+ 1 // Separator
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/globals/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ pub mod java_lang_StackTraceElement {
field_module! {
@CLASS java_lang_StackTraceElement;

/// `java.lang.StackTraceElement#declaringClassObject` field offset
///
/// Expected field type: `Reference` to `java.lang.Class`
@FIELD declaringClassObject: ty @ FieldType::Object(_) if ty.is_class(b"java/lang/Class"),
/// `java.lang.StackTraceElement#classLoaderName` field offset
///
/// Expected field type: `Reference` to `java.lang.String`
Expand Down
1 change: 1 addition & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![feature(sync_unsafe_cell)]
#![feature(core_intrinsics)]
#![feature(try_with_capacity)]
#![feature(array_chunks)]

pub mod calls;
pub mod classpath;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn create_java_base() {
// Don't use the constructors, since they do validation.
let java_base_module = Module {
name: Some(sym!(java_base)),
open: true,
open: false,
obj: Reference::null(),
version: None,
location: None,
Expand Down
5 changes: 4 additions & 1 deletion runtime/src/native/java/io/FileOutputStream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::objects::instance::Instance;
use crate::thread::JavaThread;

use std::io::Write;
use std::mem::ManuallyDrop;
use std::os::fd::{FromRawFd, RawFd};
use std::ptr::NonNull;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -76,7 +77,9 @@ pub fn writeBytes(
panic!("IOException, stream closed"); // TODO
}

let mut file = unsafe { std::fs::File::from_raw_fd(current_fd as RawFd) };
// Wrap in `ManuallyDrop` so the file descriptor doesn't get closed
let mut file =
ManuallyDrop::new(unsafe { std::fs::File::from_raw_fd(current_fd as RawFd) });

let Ok(n) = file.write(&window[offset..]) else {
let _thread = unsafe { JavaThread::for_env(env.as_ptr()) };
Expand Down
62 changes: 62 additions & 0 deletions runtime/src/native/java/lang/Module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::objects::class::Class;
use crate::objects::reference::Reference;

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

use ::jni::env::JniEnv;
use ::jni::sys::jint;
use common::int_types::{s4, s8};
use common::traits::PtrType;
use instructions::Operand;

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

pub fn defineModule0(
_: NonNull<JniEnv>,
_class: &'static Class,
_module: Reference, // java.lang.Module
_is_open: bool,
_version: Reference, // java.lang.String
_location: Reference, // java.lang.String
_pns: Reference, // java.lang.Object[]
) {
unimplemented!("java.lang.Module#defineModule0");
}

pub fn addReads0(
_: NonNull<JniEnv>,
_class: &'static Class,
_from: Reference, // java.lang.Module
_to: Reference, // java.lang.Module
) {
unimplemented!("java.lang.Module#addReads0");
}

pub fn addExports0(
_: NonNull<JniEnv>,
_class: &'static Class,
_from: Reference, // java.lang.Module
_pn: Reference, // java.lang.String
_to: Reference, // java.lang.Module
) {
unimplemented!("java.lang.Module#addExports0");
}

pub fn addExportsToAll0(
_: NonNull<JniEnv>,
_class: &'static Class,
_from: Reference, // java.lang.Module
_pn: Reference, // java.lang.String
) {
unimplemented!("java.lang.Module#addExportsToAll0");
}

pub fn addExportsToAllUnnamed0(
_: NonNull<JniEnv>,
_class: &'static Class,
_from: Reference, // java.lang.Module
_pn: Reference, // java.lang.String
) {
unimplemented!("java.lang.Module#addExportsToAllUnnamed0");
}
Loading

0 comments on commit 59784a4

Please sign in to comment.