Skip to content

Commit

Permalink
Refactor thread creation and add more implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Feb 2, 2024
1 parent 013aa4f commit 464868f
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Library/Builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/Library/java/lang/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

12 changes: 8 additions & 4 deletions src/Library/java/security/AccessController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
3 changes: 2 additions & 1 deletion src/Library/java/security/AccessController.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "Library/NativeDefs.h"

JCALL void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS);
JCALL void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS);
JCALL void lib_java_security_AccessController_getStackAccessControlContext(NATIVE_ARGS);
16 changes: 16 additions & 0 deletions src/VM/Instructions/ComparisonInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<i4>(var1.data) >= std::bit_cast<i4>(var2.data))
{
thread->m_pc = thread->m_pc-3+branchByte;
}
}

void if_icmpgt(INSTRUCTION_ARGS)
{
const u1 byte1 = args[0];
Expand Down
1 change: 1 addition & 0 deletions src/VM/Instructions/ComparisonInstructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
22 changes: 22 additions & 0 deletions src/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Variable> VM::createVariableForDescriptor(const char* descriptor)
Expand Down
5 changes: 4 additions & 1 deletion src/VM/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Instruction,107> m_instructions{{
inline static constexpr std::array<Instruction,108> m_instructions{{
// Constants
{i_nop, 0, "nop", 0, nop},
{i_aconst_null, 0, "aconst_null", 0, aconst_null},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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);
};


1 change: 1 addition & 0 deletions src/VM/VMThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 464868f

Please sign in to comment.