JVM
jvm.c File Reference
#include "jvm.h"
#include "utf8.h"
#include "natives.h"
#include "instructions.h"
#include "debugging.h"
#include <string.h>
Include dependency graph for jvm.c:

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...
 
LoadedClassesaddClassToLoadedClasses (JavaVirtualMachine *jvm, JavaClass *jc)
 Adds a class to the list of all loaded classes by the JVM. More...
 
LoadedClassesisClassLoaded (JavaVirtualMachine *jvm, const uint8_t *utf8_bytes, int32_t utf8_len)
 Checks if a class has already been loaded by its name. More...
 
JavaClassgetSuperClass (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...
 
ReferencenewString (JavaVirtualMachine *jvm, const uint8_t *str, int32_t strlen)
 
ReferencenewClassInstance (JavaVirtualMachine *jvm, LoadedClasses *lc)
 
ReferencenewArray (JavaVirtualMachine *jvm, uint32_t length, Opcode_newarray_type type)
 
ReferencenewObjectArray (JavaVirtualMachine *jvm, uint32_t length, const uint8_t *utf8_className, int32_t utf8_len)
 
ReferencenewObjectMultiArray (JavaVirtualMachine *jvm, int32_t *dimensions, uint8_t dimensionsSize, const uint8_t *utf8_className, int32_t utf8_len)
 
void deleteReference (Reference *obj)
 

Function Documentation

§ addClassToLoadedClasses()

LoadedClasses* addClassToLoadedClasses ( JavaVirtualMachine jvm,
JavaClass jc 
)

Adds a class to the list of all loaded classes by the JVM.

Parameters
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.
Returns
The node created to store the class.
Here is the caller graph for this function:

§ deinitJVM()

void deinitJVM ( JavaVirtualMachine jvm)

Deallocates all memory used by the JavaVirtualMachine structure.

Parameters
JavaVirtualMachine*jvm - pointer to the structure to be deallocated.

All loaded classes and objects created during the execution of the JVM will be freed.

See also
initJVM()
Here is the call graph for this function:
Here is the caller graph for this function:

§ deleteReference()

void deleteReference ( Reference obj)
Here is the caller graph for this function:

§ executeJVM()

void executeJVM ( JavaVirtualMachine jvm,
LoadedClasses mainClass 
)

Executes the main method of a given class.

Parameters
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().

See also
resolveClass(), JavaClass
Here is the call graph for this function:
Here is the caller graph for this function:

§ getMethodDescriptorParameterCount()

uint8_t getMethodDescriptorParameterCount ( const uint8_t *  descriptor_utf8,
int32_t  utf8_len 
)

Gets how many operands a method descriptor requires.

Parameters
constuint8_t* descriptor_utf8 - UTF-8 string containing the method descriptor
int32_tutf8_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:

public int mymethod(long a, byte b)

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.

Returns
Number of operands that need to be handled when calling a method with the given descriptor.
Here is the caller graph for this function:

§ getSuperClass()

JavaClass* getSuperClass ( JavaVirtualMachine jvm,
JavaClass jc 
)

Gets the super class of a given class.

Parameters
JavaVirtualMachine*jvm - the JVM that contains the classes
JavaClass*jc - the class that will have its super class seached.
Returns
If the class is found, returns it. Otherwise, returns NULL.
Note
This function performs a linear search, by calling isClassLoaded().
Here is the call graph for this function:
Here is the caller graph for this function:

§ initClass()

uint8_t initClass ( JavaVirtualMachine jvm,
LoadedClasses lc 
)

Initializes a class.

Parameters
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.

Returns
Will return 1 if the initialization was completed successfully. Otherwise, 0. Initialization could fail if there wasn't enough memory to allocate space for the static data or if there were any problems running its <clinit> method.
Here is the call graph for this function:
Here is the caller graph for this function:

§ initJVM()

void initJVM ( JavaVirtualMachine jvm)

Initializes a JavaVirtualMachine structure.

Parameters
JavaVirtualMachine*jvm - pointer to the structure to be initialized.

This function must be called before calling other JavaVirtualMachine functions.

See also
executeJVM(), deinitJVM()
Here is the caller graph for this function:

§ isClassLoaded()

LoadedClasses* isClassLoaded ( JavaVirtualMachine jvm,
const uint8_t *  utf8_bytes,
int32_t  utf8_len 
)

Checks if a class has already been loaded by its name.

Parameters
JavaVirtualMachine*jvm - the JVM that contains a loaded clases list.
constuint8_t* utf8_bytes - UTF-8 string containing the class name to be searched.
int32_tutf8_len - length of the UTF-8 string.
Returns
If the class is found, returns the node containing that loaded class. Otherwise, returns NULL.
Note
This function performs a linear search.
Here is the call graph for this function:
Here is the caller graph for this function:

§ isClassSuperOf()

uint8_t isClassSuperOf ( JavaVirtualMachine jvm,
JavaClass super,
JavaClass jc 
)

Check if one class is a super class of another.

Parameters
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.
Precondition
Both super and jc must be classes that have already been loaded.
Note
If super and jc point to the same class, the function returns false.
Returns
Will return 1 if super is a super class of jc. Otherwise, 0.
Here is the call graph for this function:
Here is the caller graph for this function:

§ newArray()

Reference* newArray ( JavaVirtualMachine jvm,
uint32_t  length,
Opcode_newarray_type  type 
)
Here is the caller graph for this function:

§ newClassInstance()

Reference* newClassInstance ( JavaVirtualMachine jvm,
LoadedClasses lc 
)
Here is the call graph for this function:
Here is the caller graph for this function:

§ newObjectArray()

Reference* newObjectArray ( JavaVirtualMachine jvm,
uint32_t  length,
const uint8_t *  utf8_className,
int32_t  utf8_len 
)
Here is the call graph for this function:
Here is the caller graph for this function:

§ newObjectMultiArray()

Reference* newObjectMultiArray ( JavaVirtualMachine jvm,
int32_t *  dimensions,
uint8_t  dimensionsSize,
const uint8_t *  utf8_className,
int32_t  utf8_len 
)
Here is the call graph for this function:
Here is the caller graph for this function:

§ newString()

Reference* newString ( JavaVirtualMachine jvm,
const uint8_t *  str,
int32_t  strlen 
)
Here is the caller graph for this function:

§ resolveClass()

uint8_t resolveClass ( JavaVirtualMachine jvm,
const uint8_t *  className_utf8_bytes,
int32_t  utf8_len,
LoadedClasses **  outClass 
)

Loads a .class file without initializing it.

Parameters
JavaVirtualMachine*jvm - pointer to the JVM structure that is resolving the class
constuint8_t* className_utf8_bytes - UTF-8 bytes containing the class name.
int32_tutf8_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.

Returns
Will return 1 if the resolution was completed successfully, otherwise 0. Resolution may fail if there was a problem opening and parsing any of the class files, may it be the class being resolved or a super class or interface.
See also
initClass(), resolveField(), resolveMethod()
Here is the call graph for this function:
Here is the caller graph for this function:

§ resolveField()

uint8_t resolveField ( JavaVirtualMachine jvm,
JavaClass jc,
cp_info cp_field,
LoadedClasses **  outClass 
)

Resolves a field.

Parameters
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.

Returns
Will return 1 if the resolution was completed successfully, otherwise 0. Resolution may fail if there was a problem with possible calls to resolveClass().
See also
resolveClass()
Here is the call graph for this function:
Here is the caller graph for this function:

§ resolveMethod()

uint8_t resolveMethod ( JavaVirtualMachine jvm,
JavaClass jc,
cp_info cp_method,
LoadedClasses **  outClass 
)

Resolves a method.

Parameters
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.

Returns
Will return 1 if the resolution was completed successfully, otherwise 0. Resolution may fail if there was a problem with possible calls to resolveClass().
See also
resolveField(), resolveClass()
Here is the call graph for this function:
Here is the caller graph for this function:

§ runMethod()

uint8_t runMethod ( JavaVirtualMachine jvm,
JavaClass jc,
method_info method,
uint8_t  numberOfParameters 
)

Executes the bytecode of a given method.

Parameters
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_tnumberOfParameters - 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.

Returns
Will return 1 if the execution was completed successfully, otherwise 0. Execution will fail if there was a problem running an instruction or some other problems, like insufficient memory, unsupported feature/instruction, unimplemented native method, etc.
See also
resolveClass()
Here is the call graph for this function:
Here is the caller graph for this function:

§ setClassPath()

void setClassPath ( JavaVirtualMachine jvm,
const char *  path 
)

Will set the path to look for classes when opening them.

Parameters
JavaVirtualMachine*jvm - The JVM that will load classes.
constchar* path - The string containing the path.
Note
The JVM will always look for the class in the current working directory first, and then (if the file couldn't be found) look for it in the class path.
Here is the caller graph for this function: