From 464868f116885c0c8756be5b3dee0994e7226ef2 Mon Sep 17 00:00:00 2001 From: Sebastiaan Fernandez Date: Fri, 2 Feb 2024 14:30:46 +0100 Subject: [PATCH] Refactor thread creation and add more implementations --- src/Library/Builtin.cpp | 1 + src/Library/java/lang/Thread.cpp | 5 +---- .../java/security/AccessController.cpp | 12 ++++++---- src/Library/java/security/AccessController.h | 3 ++- .../Instructions/ComparisonInstructions.cpp | 16 ++++++++++++++ src/VM/Instructions/ComparisonInstructions.h | 1 + src/VM/VM.cpp | 22 +++++++++++++++++++ src/VM/VM.h | 5 ++++- src/VM/VMThread.h | 1 + 9 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/Library/Builtin.cpp b/src/Library/Builtin.cpp index a5a65e0..699253c 100644 --- a/src/Library/Builtin.cpp +++ b/src/Library/Builtin.cpp @@ -47,6 +47,7 @@ void registerBuiltinRegisterNatives() registerNative("sun/misc/VM/initialize", "()V", lib_sun_misc_VM_initialize); // Security API registerNative("java/security/AccessController/doPrivileged", "(Ljava/security/PrivilegedAction;)Ljava/lang/Object;", lib_java_security_AccessController_doPriviliged); + registerNative("java/security/AccessController/getStackAccessControlContext", "()Ljava/security/AccessControlContext;", lib_java_security_AccessController_getStackAccessControlContext); // Vigur/lang package // registerNative("Vigur/lang/System/registerNatives", "()V", lib_Vigur_lang_System_registerNatives); // Reflection API diff --git a/src/Library/java/lang/Thread.cpp b/src/Library/java/lang/Thread.cpp index e1ca572..64536c2 100644 --- a/src/Library/java/lang/Thread.cpp +++ b/src/Library/java/lang/Thread.cpp @@ -22,10 +22,7 @@ JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS) JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS) { - // TODO: Maybe check if an object was already created? - ClassInfo* threadClass = VM->getClass("java/lang/Thread", thread); - const u4 objectReference = heap->createObject(threadClass, VM); StackFrame* returnFrame = thread->getTopFrameNonNative(); - returnFrame->pushObject(objectReference); + returnFrame->pushObject(thread->threadObject); } diff --git a/src/Library/java/security/AccessController.cpp b/src/Library/java/security/AccessController.cpp index 360fe0f..00d0d28 100644 --- a/src/Library/java/security/AccessController.cpp +++ b/src/Library/java/security/AccessController.cpp @@ -17,15 +17,19 @@ void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS) { - StackFrame* currentFrame = thread->m_currentFrame; - Variable objectVar = currentFrame->localVariables[0]; + const StackFrame* currentFrame = thread->m_currentFrame; + const Variable objectVar = currentFrame->localVariables[0]; const Object* method = heap->getObject(currentFrame->localVariables[0].data); - MethodInfo* methodInfo = method->classInfo->findMethodWithNameAndDescriptor("run", "()Ljava/lang/Object;"); + const MethodInfo* methodInfo = method->classInfo->findMethodWithNameAndDescriptor("run", "()Ljava/lang/Object;"); ClassInfo* classInfo = method->classInfo; thread->pushStackFrameWithoutParams(classInfo, methodInfo); - thread->m_currentFrame->localVariables[0] = objectVar; VM->executeLoop(thread); } + +void lib_java_security_AccessController_getStackAccessControlContext(NATIVE_ARGS) +{ + thread->returnVar(Variable{VariableType_REFERENCE, 0}); +} diff --git a/src/Library/java/security/AccessController.h b/src/Library/java/security/AccessController.h index 11b40da..17c3f58 100644 --- a/src/Library/java/security/AccessController.h +++ b/src/Library/java/security/AccessController.h @@ -2,4 +2,5 @@ #include "Library/NativeDefs.h" -JCALL void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS); \ No newline at end of file +JCALL void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS); +JCALL void lib_java_security_AccessController_getStackAccessControlContext(NATIVE_ARGS); \ No newline at end of file diff --git a/src/VM/Instructions/ComparisonInstructions.cpp b/src/VM/Instructions/ComparisonInstructions.cpp index 9ac5277..3b0914c 100644 --- a/src/VM/Instructions/ComparisonInstructions.cpp +++ b/src/VM/Instructions/ComparisonInstructions.cpp @@ -210,6 +210,22 @@ void if_icmplt(INSTRUCTION_ARGS) } } +void if_icmpge(INSTRUCTION_ARGS) +{ + const u1 byte1 = args[0]; + const u1 byte2 = args[1]; + + const i2 branchByte = (byte1 << 8) | byte2; + + Variable var2 = thread->m_currentFrame->popOperand(); + Variable var1 = thread->m_currentFrame->popOperand(); + + if (std::bit_cast(var1.data) >= std::bit_cast(var2.data)) + { + thread->m_pc = thread->m_pc-3+branchByte; + } +} + void if_icmpgt(INSTRUCTION_ARGS) { const u1 byte1 = args[0]; diff --git a/src/VM/Instructions/ComparisonInstructions.h b/src/VM/Instructions/ComparisonInstructions.h index a3d4347..e7410eb 100644 --- a/src/VM/Instructions/ComparisonInstructions.h +++ b/src/VM/Instructions/ComparisonInstructions.h @@ -16,6 +16,7 @@ void ifgt(INSTRUCTION_ARGS); void ifle(INSTRUCTION_ARGS); void if_icmpne(INSTRUCTION_ARGS); void if_icmplt(INSTRUCTION_ARGS); +void if_icmpge(INSTRUCTION_ARGS); void if_icmpgt(INSTRUCTION_ARGS); void if_icmple(INSTRUCTION_ARGS); void if_acmpne(INSTRUCTION_ARGS); diff --git a/src/VM/VM.cpp b/src/VM/VM.cpp index fee21e9..63f5859 100644 --- a/src/VM/VM.cpp +++ b/src/VM/VM.cpp @@ -41,6 +41,28 @@ void VM::start() getClass("java/lang/String", &m_mainThread); getClass("java/lang/System", &m_mainThread); getClass("java/lang/Thread", &m_mainThread); + getClass("java/lang/ThreadGroup", &m_mainThread); + + const u4 threadGroupReference = createThreadGroupObject(&m_mainThread); + m_mainThread.threadObject = createThreadObject(&m_mainThread, threadGroupReference); +} + +u4 VM::createThreadGroupObject(VMThread* thread) +{ + ClassInfo* threadGroupClass = getClass("java/lang/ThreadGroup", thread); + return m_heap.createObject(threadGroupClass, this); +} + +u4 VM::createThreadObject(VMThread* thread, const u4 threadGroupReference) +{ + ClassInfo* threadClass = getClass("java/lang/Thread", thread); + const u4 objectReference = m_heap.createObject(threadClass, this); + const Object* threadObject = m_heap.getObject(objectReference); + + FieldData* field = threadObject->getField("group", "Ljava/lang/ThreadGroup;", &m_heap); + field->data->data = threadGroupReference; + + return objectReference; } std::vector VM::createVariableForDescriptor(const char* descriptor) diff --git a/src/VM/VM.h b/src/VM/VM.h index d5db241..d9367e3 100644 --- a/src/VM/VM.h +++ b/src/VM/VM.h @@ -48,7 +48,7 @@ class VM { void executeLoop(VMThread* thread); static void checkType(Variable var, VariableType type, VMThread *thread); private: - inline static constexpr std::array m_instructions{{ + inline static constexpr std::array m_instructions{{ // Constants {i_nop, 0, "nop", 0, nop}, {i_aconst_null, 0, "aconst_null", 0, aconst_null}, @@ -136,6 +136,7 @@ class VM { {i_ifle, 2, "ifle", 0, ifle}, {i_if_icmpne, 2, "if_icmpne", 0, if_icmpne}, {i_if_icmplt, 2, "if_icmplt", 0, if_icmplt}, + {i_if_icmpge, 2, "if_icmpge", 0, if_icmpge}, {i_if_icmpgt, 2, "if_icmpgt", 0, if_icmpgt}, {i_if_icmple, 2, "if_icmple", 0, if_icmple}, {i_if_acmpne, 2, "if_acmpne", 0, if_acmpne}, @@ -173,6 +174,8 @@ class VM { Configuration m_configuration; void initStaticFields(ClassInfo* class_info, VMThread* thread); void runStaticInitializer(ClassInfo* classInfo, VMThread* thread); + u4 createThreadObject(VMThread* thread, u4 threadGroupReference); + u4 createThreadGroupObject(VMThread* thread); }; diff --git a/src/VM/VMThread.h b/src/VM/VMThread.h index 9b6e571..31981a8 100644 --- a/src/VM/VMThread.h +++ b/src/VM/VMThread.h @@ -17,6 +17,7 @@ class VMThread // current class ClassInfo* m_currentClass{nullptr}; std::string_view m_name; + u4 threadObject{0}; explicit VMThread(const std::string_view name, const size_t frameSize) noexcept : m_stack(frameSize), m_name(name)