Skip to content

Commit

Permalink
Merge pull request #1 from fernandezseb/feature/privileged_execution
Browse files Browse the repository at this point in the history
Feature/privileged execution
  • Loading branch information
fernandezseb authored Jan 23, 2024
2 parents eb0a54e + 1c1ee10 commit 2ad9c88
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 140 deletions.
5 changes: 2 additions & 3 deletions src/ClassLoader/ClassLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ ClassInfo* ClassLoader::readClass(ByteArray& byteArray)

uint16_t methodsCount = byteArray.readUnsignedShort();
classInfo->methods = readMethods(byteArray, classInfo->constantPool, methodsCount);
classInfo->methodCount = methodsCount;

AttributeCollection* attributeInfo = AttributeParser::readAttributes(byteArray, classInfo->constantPool, m_memory);
classInfo->attributes = attributeInfo;
Expand Down Expand Up @@ -341,7 +340,7 @@ void ClassLoader::parseDescriptor(const char* descriptor, MethodInfo* method)
method->argsCount = desc.argsCount;
}

MethodInfo** ClassLoader::readMethods(ByteArray& byteArray, ConstantPool* constantPool, uint16_t methodCount)
std::span<MethodInfo*> ClassLoader::readMethods(ByteArray& byteArray, ConstantPool* constantPool, uint16_t methodCount)
{
MethodInfo** methods = (MethodInfo**)m_memory->alloc(sizeof(MethodInfo*) * methodCount);

Expand Down Expand Up @@ -369,5 +368,5 @@ MethodInfo** ClassLoader::readMethods(ByteArray& byteArray, ConstantPool* consta
methods[currentMethod] = info;
}

return methods;
return {methods, methodCount};
}
2 changes: 1 addition & 1 deletion src/ClassLoader/ClassLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ClassLoader {
void parseDescriptor(const char* descriptor, MethodInfo* method);
uint16_t* readInterfaces(ByteArray& byteArray, uint16_t interfacesCount);
FieldInfo** readFields(ByteArray& byteArray, ConstantPool* constantPool, uint16_t fieldsCount);
MethodInfo** readMethods(ByteArray& byteArray, ConstantPool* constantPool, uint16_t methodCount);
std::span<MethodInfo*> readMethods(ByteArray& byteArray, ConstantPool* constantPool, uint16_t methodCount);
ClassInfo* readClass(ByteArray& byteArray);
Memory* m_memory;
};
5 changes: 2 additions & 3 deletions src/Data/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ class ClassInfo {
FieldInfo** fields;
uint16_t fieldsCount;

MethodInfo** methods;
uint16_t methodCount;
std::span<MethodInfo*> methods;

AttributeCollection* attributes;
char* sourceFile;
Expand All @@ -89,7 +88,7 @@ class ClassInfo {

[[nodiscard]] MethodInfo* findMethodWithNameAndDescriptor(const char* name, const char* descriptor) const
{
for (uint16_t currentMethod = 0; currentMethod < methodCount; ++currentMethod) {
for (uint16_t currentMethod = 0; currentMethod < methods.size(); ++currentMethod) {
if (strcmp(methods[currentMethod]->name, name) == 0
&& strcmp(constantPool->getString(methods[currentMethod]->descriptorIndex), descriptor) == 0)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Library/Builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "java/lang/Float.h"
#include "java/lang/Object.h"
#include "java/lang/System.h"
#include "java/security/AccessController.h"
#include "sun/misc/Unsafe.h"
#include "sun/misc/VM.h"
#include "sun/reflect/Reflection.h"
Expand All @@ -42,6 +43,8 @@ void registerBuiltinRegisterNatives()
// sun/misc
registerNative("sun/misc/Unsafe/registerNatives", "()V", lib_sun_misc_Unsafe_registerNatives);
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);
// Vigur/lang package
// registerNative("Vigur/lang/System/registerNatives", "()V", lib_Vigur_lang_System_registerNatives);
// Reflection API
Expand Down
2 changes: 2 additions & 0 deletions src/Library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ add_library(Library
sun/misc/VM.cpp
sun/reflect/Reflection.h
sun/reflect/Reflection.cpp
java/security/AccessController.h
java/security/AccessController.cpp
)
target_include_directories(Library PRIVATE ${PROJECT_SOURCE_DIR}/src/)
31 changes: 31 additions & 0 deletions src/Library/java/security/AccessController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023-2024 Sebastiaan Fernandez.
*
* This file is part of VigurVM.
*
* VigurVM is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
* VigurVM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Foobar.
* If not, see <https://www.gnu.org/licenses/>.
*/

#include "AccessController.h"

void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS)
{
StackFrame* currentFrame = thread->m_currentFrame;
Variable objectVar = currentFrame->localVariables[0];
Object* method = heap->getObject(currentFrame->localVariables[0].data);
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);
}
5 changes: 5 additions & 0 deletions src/Library/java/security/AccessController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "Library/NativeDefs.h"

JCALL void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS);
93 changes: 15 additions & 78 deletions src/VM/Instructions/ControlInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#include "VM/VM.h"
#include "Data/Variable.h"

static void returnCat1Var(VMThread* thread)
{
const Variable returnVal = thread->m_currentFrame->popOperand();
thread->popFrame();
thread->returnVar(returnVal);
}

void gotoInstruction(INSTRUCTION_ARGS)
{
u1 byte1 = args[0];
Expand All @@ -30,97 +37,27 @@ void gotoInstruction(INSTRUCTION_ARGS)

void freturnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable returnVal = thread->m_currentFrame->popOperand();

thread->m_stackstack.top().frames.pop_back();
if (thread->m_stackstack.top().frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stackstack.top().frames[thread->m_stackstack.top().frames.size()-1];
thread->m_currentFrame->operands.push_back(returnVal);
} else
{
thread->m_currentFrame = 0;
}
returnCat1Var(thread);
}

void ireturnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable returnVal = thread->m_currentFrame->popOperand();

thread->m_stackstack.top().frames.pop_back();
if (thread->m_stackstack.top().frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stackstack.top().frames[thread->m_stackstack.top().frames.size()-1];
thread->m_currentFrame->operands.push_back(returnVal);
} else
{
thread->m_currentFrame = 0;
}
returnCat1Var(thread);
}

void dreturnInstruction(INSTRUCTION_ARGS) {
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable lowByte = thread->m_currentFrame->popOperand();
Variable highByte = thread->m_currentFrame->popOperand();

thread->m_stackstack.top().frames.pop_back();
if (thread->m_stackstack.top().frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stackstack.top().frames[thread->m_stackstack.top().frames.size()-1];
thread->m_currentFrame->operands.push_back(highByte);
thread->m_currentFrame->operands.push_back(lowByte);
} else
{
thread->m_currentFrame = 0;
}
const Variable lowByte = thread->m_currentFrame->popOperand();
const Variable highByte = thread->m_currentFrame->popOperand();
thread->popFrame();
thread->returnVar(highByte, lowByte);
}

void areturnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable returnVal = thread->m_currentFrame->popOperand();

thread->m_stackstack.top().frames.pop_back();
if (thread->m_stackstack.top().frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stackstack.top().frames[thread->m_stackstack.top().frames.size()-1];
thread->m_currentFrame->operands.push_back(returnVal);
} else
{
thread->m_currentFrame = 0;
}
returnCat1Var(thread);
}

void returnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;
thread->m_stackstack.top().frames.pop_back();
if (thread->m_stackstack.top().frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stackstack.top().frames[thread->m_stackstack.top().frames.size()-1];
} else
{
thread->m_currentFrame = 0;
}
thread->popFrame();
}
1 change: 1 addition & 0 deletions src/VM/Instructions/ReferenceInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "VM/Native.h"

#include <bit>
#include <deque>
#include <string>

static u2 readShort(VMThread* thread)
Expand Down
16 changes: 3 additions & 13 deletions src/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ static u2 readShort(VMThread* thread)

void VM::executeLoop(VMThread* thread)
{
while(!thread->m_stackstack.top().frames.empty())
const std::size_t stackSize = thread->m_stack.frames.size();
const std::size_t depth = stackSize == 0 ? 0 : stackSize-1;
while(thread->m_stack.frames.size() > depth)
{
uint8_t opcode = readByte(thread);
printf("Running instruction with opcode: 0x%0x ", opcode);
Expand Down Expand Up @@ -237,22 +239,10 @@ void VM::runStaticInitializer(ClassInfo* classInfo, VMThread* thread)
return;
}

const u4 oldPc = thread->m_pc;
ClassInfo* oldCurrentClass = thread->m_currentClass;
const MethodInfo* oldCurrentMethod = thread->m_currentMethod;
StackFrame* oldFrame = thread->m_currentFrame;
thread->m_stackstack.emplace(200);

thread->pushStackFrameWithoutParams(classInfo, entryPoint);

printf("Executing static initializers...\n");
executeLoop(thread);

thread->m_pc = oldPc;
thread->m_currentClass = oldCurrentClass;
thread->m_currentMethod = oldCurrentMethod;
thread->m_stackstack.pop();
thread->m_currentFrame = oldFrame;
}

ClassInfo* VM::getClass(const char* className, VMThread* thread)
Expand Down
2 changes: 1 addition & 1 deletion src/VM/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class VM {
void executeNativeMethod(const ClassInfo* targetClass, const MethodInfo* methodInfo, JavaHeap* heap, VMThread* thread);
void runMain();
void shutdown();
void executeLoop(VMThread* thread);
static void checkType(Variable var, VariableType type, VMThread *thread);
private:
inline static constexpr std::array<Instruction,104> m_instructions{{
Expand Down Expand Up @@ -168,7 +169,6 @@ class VM {
VMThread m_mainThread{"main", 200};
Configuration m_configuration;
void initStaticFields(ClassInfo* class_info, VMThread* thread);
void executeLoop(VMThread* thread);
void runStaticInitializer(ClassInfo* classInfo, VMThread* thread);
};

Expand Down
Loading

0 comments on commit 2ad9c88

Please sign in to comment.