JVM
|
#include "jvm.h"
#include "utf8.h"
#include "natives.h"
#include "instructions.h"
#include "debugging.h"
#include <string.h>
Functions | |
void | initJVM (JavaVirtualMachine *jvm) |
Initializes a JavaVirtualMachine structure. More... | |
void | deinitJVM (JavaVirtualMachine *jvm) |
Deallocates all memory used by the JavaVirtualMachine structure. More... | |
void | executeJVM (JavaVirtualMachine *jvm, LoadedClasses *mainClass) |
Executes the main method of a given class. More... | |
void | setClassPath (JavaVirtualMachine *jvm, const char *path) |
Will set the path to look for classes when opening them. More... | |
uint8_t | resolveClass (JavaVirtualMachine *jvm, const uint8_t *className_utf8_bytes, int32_t utf8_len, LoadedClasses **outClass) |
Loads a .class file without initializing it. More... | |
uint8_t | resolveMethod (JavaVirtualMachine *jvm, JavaClass *jc, cp_info *cp_method, LoadedClasses **outClass) |
Resolves a method. More... | |
uint8_t | resolveField (JavaVirtualMachine *jvm, JavaClass *jc, cp_info *cp_field, LoadedClasses **outClass) |
Resolves a field. More... | |
uint8_t | runMethod (JavaVirtualMachine *jvm, JavaClass *jc, method_info *method, uint8_t numberOfParameters) |
Executes the bytecode of a given method. More... | |
uint8_t | getMethodDescriptorParameterCount (const uint8_t *descriptor_utf8, int32_t utf8_len) |
Gets how many operands a method descriptor requires. More... | |
LoadedClasses * | addClassToLoadedClasses (JavaVirtualMachine *jvm, JavaClass *jc) |
Adds a class to the list of all loaded classes by the JVM. More... | |
LoadedClasses * | isClassLoaded (JavaVirtualMachine *jvm, const uint8_t *utf8_bytes, int32_t utf8_len) |
Checks if a class has already been loaded by its name. More... | |
JavaClass * | getSuperClass (JavaVirtualMachine *jvm, JavaClass *jc) |
Gets the super class of a given class. More... | |
uint8_t | isClassSuperOf (JavaVirtualMachine *jvm, JavaClass *super, JavaClass *jc) |
Check if one class is a super class of another. More... | |
uint8_t | initClass (JavaVirtualMachine *jvm, LoadedClasses *lc) |
Initializes a class. More... | |
Reference * | newString (JavaVirtualMachine *jvm, const uint8_t *str, int32_t strlen) |
Reference * | newClassInstance (JavaVirtualMachine *jvm, LoadedClasses *lc) |
Reference * | newArray (JavaVirtualMachine *jvm, uint32_t length, Opcode_newarray_type type) |
Reference * | newObjectArray (JavaVirtualMachine *jvm, uint32_t length, const uint8_t *utf8_className, int32_t utf8_len) |
Reference * | newObjectMultiArray (JavaVirtualMachine *jvm, int32_t *dimensions, uint8_t dimensionsSize, const uint8_t *utf8_className, int32_t utf8_len) |
void | deleteReference (Reference *obj) |
LoadedClasses* addClassToLoadedClasses | ( | JavaVirtualMachine * | jvm, |
JavaClass * | jc | ||
) |
Adds a class to the list of all loaded classes by the JVM.
JavaVirtualMachine* | jvm - the JVM that has loaded the given class |
JavaClass* | jc - a class that has already been loaded and will be added to the list. |
void deinitJVM | ( | JavaVirtualMachine * | jvm | ) |
Deallocates all memory used by the JavaVirtualMachine structure.
JavaVirtualMachine* | jvm - pointer to the structure to be deallocated. |
All loaded classes and objects created during the execution of the JVM will be freed.
void deleteReference | ( | Reference * | obj | ) |
void executeJVM | ( | JavaVirtualMachine * | jvm, |
LoadedClasses * | mainClass | ||
) |
Executes the main method of a given class.
JavaVirtualMachine* | jvm - pointer to an already initialized JVM structure. |
LoadedClasses* | mainClass - pointer to a class that has been loaded and contains the public void static main method. |
If mainClass
is a null pointer, the class at the top of the stack of loaded classes will be used as entry point. If no classes are loaded, then the status of the JVM will be changed to JVM_STATUS_MAIN_METHOD_NOT_FOUND
. If mainClass
is not a null pointer, the class must have been previously resolved with a call to resolveClass()
.
uint8_t getMethodDescriptorParameterCount | ( | const uint8_t * | descriptor_utf8, |
int32_t | utf8_len | ||
) |
Gets how many operands a method descriptor requires.
const | uint8_t* descriptor_utf8 - UTF-8 string containing the method descriptor |
int32_t | utf8_len - length of the UTF-8 string |
This function counts how many operands (which are 32-bit integers) are needed from a function with the given descriptor. For example, the following method:
will have the following descriptor: (JB)I. When called, this method would require 3 operands to be passed as parameter, 2 to form the 64-bit long variable, and another one to form the 32-bit byte variable. A call to this function with that example descriptor will return 3, meaning that three operands should be popped from a caller frame and pushed to the callee frame.
JavaClass* getSuperClass | ( | JavaVirtualMachine * | jvm, |
JavaClass * | jc | ||
) |
Gets the super class of a given class.
JavaVirtualMachine* | jvm - the JVM that contains the classes |
JavaClass* | jc - the class that will have its super class seached. |
uint8_t initClass | ( | JavaVirtualMachine * | jvm, |
LoadedClasses * | lc | ||
) |
Initializes a class.
JavaVirtualMachine* | jvm - the JVM that is being executed |
LoadedClasses* | lc - node of the list of loaded classes that holds the class to be initialized. |
Class initialization is done by allocating memory for the class static data. The number of bytes necessary to hold the static data is determined when the class file is opened and parsed (openClassFile()). The static data is also initialized if the static fields have a ConstantValue attribute. After allocating the static data, the method <clinit> of the class will be called, if it exists. Even if this function is called several times, class initialization is performed only once.
void initJVM | ( | JavaVirtualMachine * | jvm | ) |
Initializes a JavaVirtualMachine structure.
JavaVirtualMachine* | jvm - pointer to the structure to be initialized. |
This function must be called before calling other JavaVirtualMachine functions.
LoadedClasses* isClassLoaded | ( | JavaVirtualMachine * | jvm, |
const uint8_t * | utf8_bytes, | ||
int32_t | utf8_len | ||
) |
Checks if a class has already been loaded by its name.
JavaVirtualMachine* | jvm - the JVM that contains a loaded clases list. |
const | uint8_t* utf8_bytes - UTF-8 string containing the class name to be searched. |
int32_t | utf8_len - length of the UTF-8 string. |
uint8_t isClassSuperOf | ( | JavaVirtualMachine * | jvm, |
JavaClass * | super, | ||
JavaClass * | jc | ||
) |
Check if one class is a super class of another.
JavaVirtualMachine* | jvm - the JVM that holds both classes |
JavaClass* | super - a class that will be checked to see if it is extended by jc. |
JavaClass* | jc - the class that will be checked to see if it extends super. |
super
and jc
must be classes that have already been loaded. super
and jc
point to the same class, the function returns false. Reference* newArray | ( | JavaVirtualMachine * | jvm, |
uint32_t | length, | ||
Opcode_newarray_type | type | ||
) |
Reference* newClassInstance | ( | JavaVirtualMachine * | jvm, |
LoadedClasses * | lc | ||
) |
Reference* newObjectArray | ( | JavaVirtualMachine * | jvm, |
uint32_t | length, | ||
const uint8_t * | utf8_className, | ||
int32_t | utf8_len | ||
) |
Reference* newObjectMultiArray | ( | JavaVirtualMachine * | jvm, |
int32_t * | dimensions, | ||
uint8_t | dimensionsSize, | ||
const uint8_t * | utf8_className, | ||
int32_t | utf8_len | ||
) |
Reference* newString | ( | JavaVirtualMachine * | jvm, |
const uint8_t * | str, | ||
int32_t | strlen | ||
) |
uint8_t resolveClass | ( | JavaVirtualMachine * | jvm, |
const uint8_t * | className_utf8_bytes, | ||
int32_t | utf8_len, | ||
LoadedClasses ** | outClass | ||
) |
Loads a .class file without initializing it.
JavaVirtualMachine* | jvm - pointer to the JVM structure that is resolving the class | |
const | uint8_t* className_utf8_bytes - UTF-8 bytes containing the class name. | |
int32_t | utf8_len - length of the UTF-8 class name | |
[out] | LoadedClasses** | outClass - the class that has been resolved, as an element of the loaded classes table of the JVM. |
This function will open the class file and read its content. All interfaces and super classes are also resolved. During class resolution, the amount of bytes required to hold an instance of that class is determined. The class will not be initialized, which means that the static data won't be allocated and the method <clinit> won't be called. To initialize a class, the function initClass() needs to be called. Class resolution may be triggered by resolution of fields, methods or some instructions that refer to classes (getstatic, new etc). All resolved classes will be added to the list of all loaded classes of the JVM. If the parameter outClass is not null, it will receive the node containig the already loaded class that was resolved by a call to this function.
uint8_t resolveField | ( | JavaVirtualMachine * | jvm, |
JavaClass * | jc, | ||
cp_info * | cp_field, | ||
LoadedClasses ** | outClass | ||
) |
Resolves a field.
JavaVirtualMachine* | jvm - pointer to the JVM structure that is running | |
JavaClass* | jc - pointer to the class that holds the field being resolved in its constant pool | |
cp_info* | cp_method - pointer to the element in the constant pool that contains the field to be resolved. Must be a CONSTANT_Fieldref CP entry. | |
[out] | LoadedClasses** | outClass - the class that contains the field that has been resolved, as an element of the loaded classes table of the JVM. |
Field resolution means to resolve the class that declared the field and type of the field, if it is a class. If the type of the field is a class, then it will also be resolved, with a call to resolveClass(). The resolved classes will be added to the list of all loaded classes of the JVM. If the parameter outClass is not null, it will receive the node containig the already loaded class that declared that field.
uint8_t resolveMethod | ( | JavaVirtualMachine * | jvm, |
JavaClass * | jc, | ||
cp_info * | cp_method, | ||
LoadedClasses ** | outClass | ||
) |
Resolves a method.
JavaVirtualMachine* | jvm - pointer to the JVM structure that is running | |
JavaClass* | jc - pointer to the class that holds the method being resolved in its constant pool | |
cp_info* | cp_method - pointer to the element in the constant pool that contains the method to be resolved. Must be a CONSTANT_Methodref CP entry. | |
[out] | LoadedClasses** | outClass - the class that contains the method that has been resolved, as an element of the loaded classes table of the JVM. |
Method resolution means to resolve possible classes in its parameters or return type, and the class in which the method is defined. All identified classes will also be resolved, with a call to resolveClass(). All resolved classes will be added to the list of all loaded classes of the JVM. If the parameter outClass is not null, it will receive the node containig the already loaded class that declared that method.
uint8_t runMethod | ( | JavaVirtualMachine * | jvm, |
JavaClass * | jc, | ||
method_info * | method, | ||
uint8_t | numberOfParameters | ||
) |
Executes the bytecode of a given method.
JavaVirtualMachine* | jvm - pointer to the JVM structure that is running. |
JavaClass* | jc - pointer to the class that contains the method that will be executed. |
method_info* | method - pointer to method that will be executed. |
uint8_t | numberOfParameters - number of operands that need to be popped from the top frame and pushed to the frame that will be created to execute the given method (parameter passing). |
This function will create a new frame and push it in the stack of frame of the JVM (FrameStack). To see what a frame is and why it is necessary, check documentation of Frame. Instruction will be fetched one by one and executed until there is no more code to be executed from this method. Then the frame will be popped.
void setClassPath | ( | JavaVirtualMachine * | jvm, |
const char * | path | ||
) |
Will set the path to look for classes when opening them.
JavaVirtualMachine* | jvm - The JVM that will load classes. |
const | char* path - The string containing the path. |