JVM
jvm.h
Go to the documentation of this file.
1 #ifndef JVM_H
2 #define JVM_H
3 
5 typedef struct Reference Reference;
6 
7 #include <stdint.h>
8 #include "javaclass.h"
9 #include "opcodes.h"
10 #include "framestack.h"
11 
12 enum JVMStatus {
23 };
24 
25 typedef struct ClassInstance
26 {
28  int32_t* data;
30 
31 typedef struct String
32 {
33  uint8_t* utf8_bytes;
34  uint32_t len;
35 } String;
36 
37 typedef struct Array
38 {
39  uint32_t length;
40  uint8_t* data;
42 } Array;
43 
44 typedef struct ObjectArray
45 {
46  uint32_t length;
47  uint8_t* utf8_className;
48  int32_t utf8_len;
50 } ObjectArray;
51 
52 typedef enum ReferenceType {
58 
59 struct Reference
60 {
62 
63  union {
68  };
69 };
70 
71 typedef struct ReferenceTable
72 {
76 
79 typedef struct LoadedClasses
80 {
83 
92  uint8_t requiresInit;
93 
95  int32_t* staticFieldsData;
96 
100 
105 {
108  uint8_t status;
109 
116 
120 
123 
127 
134  char classPath[256];
135 };
136 
137 void initJVM(JavaVirtualMachine* jvm);
138 void deinitJVM(JavaVirtualMachine* jvm);
139 void executeJVM(JavaVirtualMachine* jvm, LoadedClasses* mainClass);
140 void setClassPath(JavaVirtualMachine* jvm, const char* path);
141 uint8_t resolveClass(JavaVirtualMachine* jvm, const uint8_t* className_utf8_bytes, int32_t utf8_len, LoadedClasses** outClass);
142 uint8_t resolveMethod(JavaVirtualMachine* jvm, JavaClass* jc, cp_info* cp_method, LoadedClasses** outClass);
143 uint8_t resolveField(JavaVirtualMachine* jvm, JavaClass* jc, cp_info* cp_field, LoadedClasses** outClass);
144 uint8_t runMethod(JavaVirtualMachine* jvm, JavaClass* jc, method_info* method, uint8_t numberOfParameters);
145 uint8_t getMethodDescriptorParameterCount(const uint8_t* descriptor_utf8, int32_t utf8_len);
146 
148 LoadedClasses* isClassLoaded(JavaVirtualMachine* jvm, const uint8_t* utf8_bytes, int32_t utf8_len);
150 uint8_t isClassSuperOf(JavaVirtualMachine* jvm, JavaClass* super, JavaClass* jc);
151 uint8_t initClass(JavaVirtualMachine* jvm, LoadedClasses* lc);
152 
153 Reference* newString(JavaVirtualMachine* jvm, const uint8_t* str, int32_t strlen);
155 Reference* newArray(JavaVirtualMachine* jvm, uint32_t length, Opcode_newarray_type type);
156 Reference* newObjectArray(JavaVirtualMachine* jvm, uint32_t length, const uint8_t* utf8_className, int32_t utf8_len);
157 Reference* newObjectMultiArray(JavaVirtualMachine* jvm, int32_t* dimensions, uint8_t dimensionsSize,
158  const uint8_t* utf8_className, int32_t utf8_len);
159 
160 void deleteReference(Reference* obj);
161 
169  #define DEBUG_REPORT_INSTRUCTION_ERROR \
170  printf("\nAbortion request by instruction at %s:%u.\n", __FILE__, __LINE__); \
171  printf("Check at the source file what the cause could be.\n"); \
172  printf("It could be an exception that was supposed to be thrown or an unsupported feature.\n"); \
173  printf("Execution will proceed, but others instruction will surely request abortion.\n\n");
174 
175 #endif // JVM_H
176 
Definition: jvm.h:55
Definition: jvm.h:59
uint8_t initClass(JavaVirtualMachine *jvm, LoadedClasses *lc)
Initializes a class.
Definition: jvm.c:784
ClassInstance ci
Definition: jvm.h:64
ReferenceType
Definition: jvm.h:52
Reference * newObjectMultiArray(JavaVirtualMachine *jvm, int32_t *dimensions, uint8_t dimensionsSize, const uint8_t *utf8_className, int32_t utf8_len)
Definition: jvm.c:1102
void executeJVM(JavaVirtualMachine *jvm, LoadedClasses *mainClass)
Executes the main method of a given class.
Definition: jvm.c:94
ReferenceTable * objects
Linked list of all objects that have been created during the execution of the JVM.
Definition: jvm.h:119
Definition: jvm.h:56
Opcode_newarray_type
Definition: opcodes.h:80
Definition: jvm.h:14
int32_t * staticFieldsData
Array containing the data for the static fields of the class.
Definition: jvm.h:95
struct Array Array
uint32_t length
Definition: jvm.h:46
uint8_t runMethod(JavaVirtualMachine *jvm, JavaClass *jc, method_info *method, uint8_t numberOfParameters)
Executes the bytecode of a given method.
Definition: jvm.c:469
String str
Definition: jvm.h:67
int32_t utf8_len
Definition: jvm.h:48
Reference * newObjectArray(JavaVirtualMachine *jvm, uint32_t length, const uint8_t *utf8_className, int32_t utf8_len)
Definition: jvm.c:1023
uint32_t len
Definition: jvm.h:34
Linked list data struct that holds information about a class that has already been resolved...
Definition: jvm.h:79
Opcode_newarray_type type
Definition: jvm.h:41
Definition: jvm.h:13
Definition: jvm.h:19
Definition: jvm.h:71
Reference * obj
Definition: jvm.h:73
Definition: constantpool.h:9
Definition: jvm.h:37
void deinitJVM(JavaVirtualMachine *jvm)
Deallocates all memory used by the JavaVirtualMachine structure.
Definition: jvm.c:42
uint8_t * utf8_bytes
Definition: jvm.h:33
uint8_t resolveMethod(JavaVirtualMachine *jvm, JavaClass *jc, cp_info *cp_method, LoadedClasses **outClass)
Resolves a method.
Definition: jvm.c:326
Reference * newArray(JavaVirtualMachine *jvm, uint32_t length, Opcode_newarray_type type)
Definition: jvm.c:945
uint8_t status
Status of the execution of the JVM, indicating whether there are errors or not.
Definition: jvm.h:108
LoadedClasses * isClassLoaded(JavaVirtualMachine *jvm, const uint8_t *utf8_bytes, int32_t utf8_len)
Checks if a class has already been loaded by its name.
Definition: jvm.c:691
uint8_t * data
Definition: jvm.h:40
LoadedClasses * classes
Linked list containing all classes that have been resolved by the JVM.
Definition: jvm.h:126
struct String String
ReferenceType type
Definition: jvm.h:61
uint8_t resolveField(JavaVirtualMachine *jvm, JavaClass *jc, cp_info *cp_field, LoadedClasses **outClass)
Resolves a field.
Definition: jvm.c:406
int32_t * data
Definition: jvm.h:28
Definition: jvm.h:53
Reference ** elements
Definition: jvm.h:49
void setClassPath(JavaVirtualMachine *jvm, const char *path)
Will set the path to look for classes when opening them.
Definition: jvm.c:133
void initJVM(JavaVirtualMachine *jvm)
Initializes a JavaVirtualMachine structure.
Definition: jvm.c:17
Definition: jvm.h:31
struct ReferenceTable * next
Definition: jvm.h:74
Reference * newString(JavaVirtualMachine *jvm, const uint8_t *str, int32_t strlen)
Definition: jvm.c:853
struct ObjectArray ObjectArray
Definition: jvm.h:25
uint8_t getMethodDescriptorParameterCount(const uint8_t *descriptor_utf8, int32_t utf8_len)
Gets how many operands a method descriptor requires.
Definition: jvm.c:596
uint32_t length
Definition: jvm.h:39
uint8_t isClassSuperOf(JavaVirtualMachine *jvm, JavaClass *super, JavaClass *jc)
Check if one class is a super class of another.
Definition: jvm.c:741
JVMStatus
Definition: jvm.h:12
A java virtual machine, storing all loaded classes, created objects and frames for methods being exec...
Definition: jvm.h:104
uint8_t simulatingSystemAndStringClasses
Boolean telling if the System/String classes are to be simulated instead of being loaded from their a...
Definition: jvm.h:115
JavaClass * getSuperClass(JavaVirtualMachine *jvm, JavaClass *jc)
Gets the super class of a given class.
Definition: jvm.c:717
uint8_t resolveClass(JavaVirtualMachine *jvm, const uint8_t *className_utf8_bytes, int32_t utf8_len, LoadedClasses **outClass)
Loads a .class file without initializing it.
Definition: jvm.c:174
Reference * newClassInstance(JavaVirtualMachine *jvm, LoadedClasses *jc)
Definition: jvm.c:898
Definition: jvm.h:20
ObjectArray oar
Definition: jvm.h:66
Definition: javaclass.h:96
JavaClass * jc
Pointer to the JavaClass struct of the resolved class.
Definition: jvm.h:82
uint8_t requiresInit
Boolean telling if the class has already been initialized or if it still needs to be...
Definition: jvm.h:92
Array arr
Definition: jvm.h:65
struct LoadedClasses LoadedClasses
Linked list data struct that holds information about a class that has already been resolved...
FrameStack * frames
Stack of all frames created by method calls.
Definition: jvm.h:122
Definition: jvm.h:54
struct ClassInstance ClassInstance
struct ReferenceTable ReferenceTable
void deleteReference(Reference *obj)
Definition: jvm.c:1175
struct LoadedClasses * next
Pointer to the next node of the linked list.
Definition: jvm.h:98
Stack of frames to store all frames created by method calling during execution of the JVM...
Definition: framestack.h:44
JavaClass * c
Definition: jvm.h:27
uint8_t * utf8_className
Definition: jvm.h:47
Definition: methods.h:10
LoadedClasses * addClassToLoadedClasses(JavaVirtualMachine *jvm, JavaClass *jc)
Adds a class to the list of all loaded classes by the JVM.
Definition: jvm.c:666
Definition: jvm.h:44