I need to make smaller commits

This commit is contained in:
vegowotenks 2024-09-10 00:17:17 +02:00
parent 64eef60c4e
commit 0c54a1d7e1
8 changed files with 377 additions and 83 deletions

View file

@ -13,8 +13,8 @@ use crate::constantpool::{ ConstantPoolInfo, ConstantClassInfo, ConstantUtf8Info
use crate::heap_area::{ HeapArea, FieldValue, ObjectReference, CompartmentEntry };
use crate::iterators::{ ClassMethodIterator, ClassFieldIterator };
use crate::native_methods;
use crate::native_methods::ignore_call;
use crate::native_registry::{ NativeRegistry };
use crate::native_methods::{ EntryPoint, ignore_call };
use crate::native_registry::NativeRegistry;
use crate::stackframe;
use crate::stackframe::{ StackFrame, StackValue, OperandStack };
@ -233,10 +233,6 @@ impl JVM {
primitive_class_object
}
fn register_native(&mut self, class_name: &str, method_name: &str, method_descriptor: &MethodDescriptor) {
self.native_registry.register("java/lang/System", "registerNatives", ignore_call);
}
pub fn entrypoint(&mut self, class_name: &String, method_name: &String, arguments: &[&str]) -> Result<(), Error> {
let entry_class = JavaClassFile {
minor_version: 0,
@ -251,12 +247,17 @@ impl JVM {
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: class_name.to_string() }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: method_name.to_string() }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "([Ljava/lang/String;)V".to_string() }),
ConstantPoolInfo::MethodRef(ConstantMethodRefInfo { class_index: 11, name_and_type_index: 13}), // 10
ConstantPoolInfo::Class(ConstantClassInfo { name_index: 12 } ),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "java/lang/String".to_string() }),
ConstantPoolInfo::NameAndType(ConstantNameAndTypeInfo { name_index: 14, descriptor_index: 15 }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "<init>".to_string() }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "([B)V".to_string() }), // 15
ConstantPoolInfo::MethodRef(ConstantMethodRefInfo { class_index: 1, name_and_type_index: 11}), // 10
ConstantPoolInfo::NameAndType(ConstantNameAndTypeInfo { name_index: 12, descriptor_index: 13 }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "populateUnsafeConstants".to_string() }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "()V".to_string() }),
ConstantPoolInfo::Class(ConstantClassInfo { name_index: 15 }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "jdk/internal/misc/UnsafeConstants".to_string() }), // 15
ConstantPoolInfo::MethodRef(ConstantMethodRefInfo { class_index: 17, name_and_type_index: 19}),
ConstantPoolInfo::Class(ConstantClassInfo { name_index: 18 }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "java/lang/System".to_string() }),
ConstantPoolInfo::NameAndType(ConstantNameAndTypeInfo { name_index: 20, descriptor_index: 13 }),
ConstantPoolInfo::Utf8(ConstantUtf8Info { utf8: "initPhase1".to_string() }),
]
),
access_flags: ClassAccessFlagMask { mask: ClassAccessFlag::Super.discriminant() },
@ -267,7 +268,7 @@ impl JVM {
methods: Box::new([
MethodInfo {
access_flags: MethodAccessFlagMask {
mask: MethodAccessFlag::Public.discriminant() | MethodAccessFlag::Static.discriminant()
mask: MethodAccessFlag::Private.discriminant() | MethodAccessFlag::Static.discriminant()
},
name: "call_main".to_string(),
descriptor: MethodDescriptor {
@ -287,8 +288,22 @@ impl JVM {
max_locals: 1,
code: Bytecode {
bytes: Box::new([
0x2A_u8.to_be(), // aload_0
// access something from UnsafeConstants
0x12_u8.to_be(), // ldc
0x0E_u8.to_be(), // index 14 into the constant pool
0x57_u8.to_be(), // pop
// Update UnsafeConstants to actual values
0xb8_u8.to_be(), // invokestatic
0x0A_u16.to_be_bytes()[0], // index 10 into the constant
0x0A_u16.to_be_bytes()[1], // pool
// call initPhase1
0xb8_u8.to_be(), // invokestatic
0x10_u16.to_be_bytes()[0], // index 10 into the constant
0x10_u16.to_be_bytes()[1], // pool
0x2A_u8.to_be(), // aload_0
0xB8_u8.to_be(), // invokestatic
0x04_u16.to_be_bytes()[0], // index 4 into the constant
0x04_u16.to_be_bytes()[1], // pool
@ -301,29 +316,47 @@ impl JVM {
}
])
},
MethodInfo {
access_flags: MethodAccessFlagMask {
mask: MethodAccessFlag::Private.discriminant() | MethodAccessFlag::Static.discriminant() | MethodAccessFlag::Native.discriminant()
},
name: "populateUnsafeConstants".to_string(),
descriptor: MethodDescriptor {
argument_types: Box::new([]),
return_type: AbstractTypeDescription {
array_level: 0,
kind: AbstractTypeKind::Void(),
}
},
code_attribute_index: 0,
attributes: Box::new([])
}
]),
attributes: Box::new([]),
};
self.class_store.add_class(entry_class, true)?; // 0
self.class_store.add_class(JVM::class_native_class_data(), true)?; // 1
self.load_class(&"java/lang/Object".to_string())?; // 2
self.load_class(&"java/lang/Number".to_string())?; // 3
self.load_class(&"java/lang/Byte".to_string())?; // 4
self.load_class(&"java/lang/String".to_string())?; // 5
self.load_class(&"java/lang/Class".to_string())?; // 6
self.native_registry.register("::EntryPoint", "populateUnsafeConstants", EntryPoint::populate_unsafe_constants);
self.class_store.add_class(entry_class, true)?; // 0
self.class_store.add_class(JVM::class_native_class_data(), true)?; // 1
self.load_class(&"java/lang/Object".to_string())?; // 2
self.load_class(&"java/lang/Number".to_string())?; // 3
let byte_class_index = self.load_class(&"java/lang/Byte".to_string())?; // 4
let string_class_index = self.load_class(&"java/lang/String".to_string())?; // 5
let class_class_index = self.load_class(&"java/lang/Class".to_string())?; // 6
let system_class_index = self.load_class(&"java/lang/System".to_string())?; // 7
self.make_class_class("Ljava/lang/Byte;");
self.make_class_class("Ljava/lang/String;");
self.make_array_class(
self.class_store.get_class_objectref_from_index(4),
self.class_store.get_class_objectref_from_index(byte_class_index),
AbstractTypeDescription {
array_level: 0,
kind: AbstractTypeKind::Classname("java/lang/Byte".into()),
}
);
self.make_array_class(
self.class_store.get_class_objectref_from_index(5),
self.class_store.get_class_objectref_from_index(string_class_index),
AbstractTypeDescription {
array_level: 0,
kind: AbstractTypeKind::Classname("java/lang/String".into()),
@ -332,18 +365,24 @@ impl JVM {
self.heap_area.fill_byte_cache(&self.class_store);
let int_class_ref = self.make_primitive_class("int", "I");
let byte_class_ref = self.make_primitive_class("byte", "B");
self.class_store.primitive_classes.int_class = self.make_primitive_class("int", "I");
self.class_store.primitive_classes.byte_class = self.make_primitive_class("byte", "B");
self.class_store.primitive_classes.char_class = self.make_primitive_class("char", "C");
self.class_store.primitive_classes.long_class = self.make_primitive_class("long", "J");
self.class_store.primitive_classes.float_class = self.make_primitive_class("float", "F");
self.class_store.primitive_classes.short_class = self.make_primitive_class("short", "S");
self.class_store.primitive_classes.double_class = self.make_primitive_class("double", "D");
self.class_store.primitive_classes.boolean_class = self.make_primitive_class("boolean", "Z");
self.make_array_class(
int_class_ref,
self.class_store.primitive_classes.int_class,
AbstractTypeDescription {
array_level: 0,
kind: AbstractTypeKind::Int(),
}
);
self.make_array_class(
byte_class_ref,
self.class_store.primitive_classes.byte_class,
AbstractTypeDescription {
array_level: 0,
kind: AbstractTypeKind::Byte(),
@ -366,7 +405,6 @@ impl JVM {
pub fn run(&mut self) -> Result<(), Error> {
while self.stack_frames.len() != 0 {
println!("Enter bytecode loop:");
let jvm_op = self.bytecode_loop()?;
match jvm_op {
@ -517,6 +555,11 @@ impl JVM {
FieldValue::Long(long_entry.value)
},
AbstractTypeKind::Float() => {
let float_entry = class_file.pool_float_entry(constant_value_info.constant_value_index)?;
FieldValue::Float(float_entry.value)
},
AbstractTypeKind::Classname(ref name) => {
if name == "java/lang/String" {
let string_entry = class_file.gather_string(constant_value_info.constant_value_index)?;
@ -585,15 +628,15 @@ impl JVM {
}
fn bytecode_loop(&mut self) -> Result<JVMCallbackOperation, Error> {
//println!("Enter bytecode loop:");
let frame = {
let frame_index = self.stack_frames.len() - 1;
&mut self.stack_frames[frame_index]
};
let frame_index = self.stack_frames.len() - 1;
let frame = &mut self.stack_frames[frame_index];
let class = self.class_store.class_file_from_idx(frame.class_index).unwrap();
let method = & class.methods[frame.method_index as usize];
if method.access_flags & MethodAccessFlag::Native {
println!("{:25}.{:15}: (native)", class.get_classname().unwrap(), method.name);
return self.native_call()
}
@ -604,10 +647,17 @@ impl JVM {
let (instruction, offset) = bytecode.next_instruction(frame.instruction_pointer as usize);
frame.instruction_pointer += offset as u32;
println!("{:25}.{:15}:{:<10}{instruction:?}", class.get_classname().unwrap(), method.name, frame.instruction_pointer);
println!("{}{:25}.{:15}:{:<10}{instruction:?}", " ".repeat(frame_index), class.get_classname().unwrap(), method.name, frame.instruction_pointer);
match instruction {
Instruction::AddInt() => {
let value_0 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
let value_1 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
wrap_stackframe_error(class, method, frame.operand_stack.push(StackValue::Int(value_0 + value_1)))?;
}
Instruction::ArrayLength() => {
let array_reference = wrap_stackframe_error(class, method, frame.operand_stack.pop_reference(0))?;
@ -659,6 +709,24 @@ impl JVM {
}
}
Instruction::BranchNonNegative(branch_offset) => {
let test_value = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
if test_value >= 0 {
frame.instruction_pointer -= offset as u32;
frame.instruction_pointer = if branch_offset < 0 { frame.instruction_pointer - branch_offset.abs() as u32} else { frame.instruction_pointer + branch_offset.abs() as u32};
}
}
Instruction::BranchNonZero(branch_offset) => {
let test_value = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
if test_value != 0 {
frame.instruction_pointer -= offset as u32;
frame.instruction_pointer = if branch_offset < 0 { frame.instruction_pointer - branch_offset.abs() as u32} else { frame.instruction_pointer + branch_offset.abs() as u32};
}
}
Instruction::BranchNull(branch_offset) => {
let test_value = wrap_stackframe_error(class, method, frame.operand_stack.pop_reference(0))?;
@ -840,9 +908,6 @@ impl JVM {
)));
}
if callee_method_info.access_flags & MethodAccessFlag::Native {
}
let supplied_descriptor: MethodDescriptor = supplied_descriptor_string.try_into()?;
// TODO: Throw exception on fail
@ -982,6 +1047,12 @@ impl JVM {
wrap_stackframe_error(class, method, frame.operand_stack.push(StackValue::Reference(string_obj_ref)))?;
},
ConstantPoolInfo::Float(_) => {
// TODO: Handle error instead of unwrap
let float_constant = class.gather_float(index as u16).unwrap();
wrap_stackframe_error(class, method, frame.operand_stack.push(StackValue::Float(float_constant)))?;
},
_ => {
println!("{:?}", class.pool_entry(index as u16).unwrap());
todo!()
@ -1000,11 +1071,7 @@ impl JVM {
let array_level = class_name.len() - component_name.len();
let array_type_desc = AbstractTypeDescription {
array_level: array_level as u8,
kind: if component_name.len() == 1 {
component_name.into()
} else {
AbstractTypeKind::Classname(component_name.to_string())
}
kind: component_name.into(),
};
if let Some(array_ref) = self.class_store.get_array_class_ref(&array_type_desc) {
@ -1166,6 +1233,10 @@ impl JVM {
wrap_stackframe_error(class, method, frame.operand_stack.push(StackValue::Reference(new_object)))?;
},
Instruction::Pop() => {
wrap_stackframe_error(class, method, frame.operand_stack.pop_computational_1(0))?;
}
Instruction::PushConstInt0() => {
wrap_stackframe_error(class, method, frame.operand_stack.push(StackValue::Int(0)))?;
}
@ -1189,6 +1260,26 @@ impl JVM {
wrap_stackframe_error(class, method, frame.operand_stack.push(StackValue::Reference(ObjectReference::NULL)))?;
}
Instruction::PutField(fieldref_index) => {
let (_target_class_name, target_field_name, expected_field_descriptor) = class.gather_fieldref(fieldref_index)?;
let value = match expected_field_descriptor.as_str() {
"J" | "D" => wrap_stackframe_error(class, method, frame.operand_stack.pop_computational_2(0))?,
_ => match wrap_stackframe_error(class, method, frame.operand_stack.pop_computational_1(0))? {
StackValue::Int(i) => FieldValue::Int(i),
StackValue::Reference(r) => FieldValue::Reference(r),
stack_value @ _ => {
println!("{stack_value:?}");
todo!()
}
}
};
let this_object = wrap_stackframe_error(class, method, frame.operand_stack.pop_reference(0))?;
self.heap_area.object_area.set_object_field(this_object, target_field_name, value, frame.class_index, &self.class_store)?;
}
Instruction::PutStatic(fieldref_index) => {
let (target_class_name, target_field_name, expected_field_descriptor) = class.gather_fieldref(fieldref_index)?;
@ -1228,7 +1319,13 @@ impl JVM {
let int_value = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
FieldValue::Boolean((int_value & 1) != 0)
},
}
(0, AbstractTypeKind::Int()) => {
let int_value = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
FieldValue::Int(int_value)
}
(0..=255, AbstractTypeKind::Classname(_field_type_name)) => {
let ref_value = wrap_stackframe_error(class, method, frame.operand_stack.pop_reference(0))?;
@ -1404,7 +1501,8 @@ fn load_local_int(class: &JavaClassFile, method: &MethodInfo, frame: &mut StackF
}
fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut VecDeque<StackValue>, argument_types: &Box<[AbstractTypeDescription]>, stack: &mut OperandStack) -> Result<(), Error> {
for argument_type in argument_types {
for argument_type_index in 0..argument_types.len() {
let argument_type = &argument_types[argument_types.len() - argument_type_index - 1];
if argument_type.array_level != 0 {
// TODO: Type checking
arguments.push_front(
@ -1540,7 +1638,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
Ok(())
}
fn wrap_stackframe_error<T>(class: &JavaClassFile, method: &MethodInfo, frame_result: Result<T, stackframe::Error>) -> Result<T, Error> {
pub fn wrap_stackframe_error<T>(class: &JavaClassFile, method: &MethodInfo, frame_result: Result<T, stackframe::Error>) -> Result<T, Error> {
match frame_result {
Ok(t) => Ok(t),
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),