First save
[cacao.git] / doc / handbook / loader.tex
1 \chapter{Class Loader}
2
3
4 \section{Introduction}
5
6 A \textit{Java Virtual Machine} (JVM) dynamically loads, links and
7 initializes classes and interfaces when they are needed. Loading a
8 class or interface means locating the binary representation---the
9 class files---and creating a class of interface structure from that
10 binary representation. Linking takes a loaded class or interface and
11 transfers it into the runtime state of the \textit{Java Virtual
12 Machine} so that it can be executed. Initialization of a class or
13 interface means executing the static class of interface initializer
14 \texttt{<clinit>}.
15
16 The following sections describe the process of loading, linking and
17 initalizing a class or interface in the CACAO \textit{Java Virtual
18 Machine} in greater detail. Further the used data structures and
19 techniques used in CACAO and the interaction with the GNU classpath
20 are described.
21
22
23 \section{System class loader}
24
25 The class loader of a \textit{Java Virtual Machine} (JVM) is
26 responsible for loading all type of classes and interfaces into the
27 runtime system of the JVM. Every JVM has a \textit{system class
28 loader} which is implemented in \texttt{java.lang.ClassLoader} and
29 this class interacts via native function calls with the JVM itself.
30
31 The \textit{GNU classpath} implements the system class loader in
32 \texttt{gnu.java.lang.SystemClassLoader} which extends
33 \texttt{java.lang.ClassLoader} and interacts with the JVM. The
34 \textit{bootstrap class loader} is implemented in
35 \texttt{java.lang.ClassLoader} plus the JVM depended class
36 \texttt{java.lang.VMClassLoader}. \texttt{java.lang.VMClassLoader} is
37 the main class how the bootstrap class loader of the GNU classpath
38 interacts with the JVM. The main functions of this class is
39
40 \begin{verbatim}
41         static final native Class loadClass(String name, boolean resolve)
42           throws ClassNotFoundException;
43 \end{verbatim}
44
45 This is a native function implemented in the CACAO JVM, which is
46 located in \texttt{nat/VMClassLoader.c} and calls the internal loader
47 functions of CACAO. If the \texttt{name} argument is \texttt{NULL}, a
48 new \texttt{java.lang.NullPointerException} is created and the
49 function returns \texttt{NULL}.
50
51 If the \texttt{name} is non-NULL a new UTF8 string of the class' name
52 is created in the internal \textit{symbol table} via
53
54 \begin{verbatim}
55         utf *javastring_toutf(java_lang_String *string, bool isclassname);
56 \end{verbatim}
57
58 This function converts a \texttt{java.lang.String} string into the
59 internal used UTF8 string representation. \texttt{isclassname} tells
60 the function to convert any \texttt{.} (dots) found in the class name
61 into \texttt{/} (slashes), so the class loader can find the specified
62 class.
63
64 Then a new \texttt{classinfo} structure is created via the
65
66 \begin{verbatim}
67         classinfo *class_new(utf *classname);
68 \end{verbatim}
69
70 function call. This function creates a unique representation of this
71 class, identified by its name, in the JVM's internal \textit{class
72 hashtable}. The newly created \texttt{classinfo} structure is
73 initialized with correct values, like \texttt{loaded = false;},
74 \texttt{linked = false;} and \texttt{initialized = false;}. This
75 guarantees a definite state of a new class.
76
77 The next step is to actually load the class requested. Thus the main
78 loader function
79
80 \begin{verbatim}
81         classinfo *class_load(classinfo *c);
82 \end{verbatim}
83
84 is called, which is a wrapper function to the real loader function
85
86 \begin{verbatim}
87         classinfo *class_load_intern(classbuffer *cb);
88 \end{verbatim}
89
90 This wrapper function is required to ensure some requirements:
91
92 \begin{itemize}
93  \item enter a monitor on the \texttt{classinfo} structure, so that
94  only one thread can load the same class at the same time
95
96  \item initialize the \texttt{classbuffer} structure with the actual
97  class file data
98
99  \item remove the \texttt{classinfo} structure from the internal table
100  if we got an exception during loading
101
102  \item free any allocated memory and leave the monitor
103 \end{itemize}
104
105 The \texttt{class\_load\_intern} functions preforms the actual loading
106 of the binary representation of the class or interface. During loading
107 some verifier checks are performed which can throw a
108 \texttt{java.lang.ClassFormatError} or
109 \texttt{java.lang.NoClassDefFoundError}. Some of these
110 \texttt{java.lang.ClassFormatError} checks are
111
112 \begin{itemize}
113  \item \textit{Truncated class file} --- unexpected end of class file
114  data
115
116  \item \textit{Bad magic number} --- class file does not contain the magic bytes
117  (0xCAFEBABE)
118
119  \item \textit{Unsupported major.minor version} --- the bytecode
120  version of the given class file is not supported by the JVM
121 \end{itemize}
122
123 After some loaded bytes, the class' constant pool is loaded via
124
125 \begin{verbatim}
126         static bool class_loadcpool(classbuffer *cb, classinfo *c);
127 \end{verbatim}
128
129 from the \texttt{constant\_pool} table in the binary representation of
130 the class of interface. The constant pool needs to be parsed in two
131 passes. In the first pass the information loaded is saved in temporary
132 structures, which are further processed in the second pass, when the
133 complete constant pool has been traversed. Only when the whole
134 constant pool entries have been loaded, any constant pool entry can be
135 completely resolved, but this resolving can only be done in a specific
136 order:
137
138 \begin{enumerate}
139  \item \texttt{CONSTANT\_Class}
140
141  \item \texttt{CONSTANT\_String}
142
143  \item \texttt{CONSTANT\_NameAndType}
144
145  \item \texttt{CONSTANT\_Fieldref}, \texttt{CONSTANT\_Methodref} and
146  \texttt{CONSTANT\_InterfaceMethodref} --- these are combined into one
147  structure
148 \end{enumerate}
149
150 The remaining constant pool types \texttt{CONSTANT\_Integer},
151 \texttt{CONSTANT\_Float}, \texttt{CONSTANT\_Long},
152 \texttt{CONSTANT\_Double} and \texttt{CONSTANT\_Utf8} can be resolved
153 in the first pass and need no further processing.
154
155 These are the temporary structures used to \textit{forward} the data
156 from the first pass into the second:
157
158 \begin{verbatim}
159         /* CONSTANT_Class entries */
160         typedef struct forward_class {
161             struct forward_class *next;
162             u2 thisindex;
163             u2 name_index;
164         } forward_class;
165
166         /* CONSTANT_String */
167         typedef struct forward_string {
168             struct forward_string *next;
169             u2 thisindex;
170             u2 string_index;
171         } forward_string;
172
173         /* CONSTANT_NameAndType */
174         typedef struct forward_nameandtype {
175             struct forward_nameandtype *next;
176             u2 thisindex;
177             u2 name_index;
178             u2 sig_index;
179         } forward_nameandtype;
180
181         /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
182         typedef struct forward_fieldmethint {
183             struct forward_fieldmethint *next;
184             u2 thisindex;
185             u1 tag;
186             u2 class_index;
187             u2 nameandtype_index;
188         } forward_fieldmethint;
189 \end{verbatim}
190
191 The \texttt{classinfo} structure has two pointers to arrays which
192 contain the class' constant pool infos, namely: \texttt{cptags} and
193 \texttt{cpinfos}. \texttt{cptags} contains the type of the constant
194 pool entry. \texttt{cpinfos} contains a pointer to the constant pool
195 entry itself. In the second pass the references are resolved and the
196 runtime structures are created. In further detail this includes for
197
198 \begin{itemize}
199  \item \texttt{CONSTANT\_Class}: get the UTF8 name string of the
200  class, store type \texttt{CONSTANT\_Class} in \texttt{cptags}, create
201  a class in the class hashtable with the UTF8 name and store the
202  pointer to the new class in \texttt{cpinfos}
203
204  \item \texttt{CONSTANT\_String}: get the UTF8 string of the
205  referenced string, store type \texttt{CONSTANT\_String} in
206  \texttt{cptags} and store the UTF8 string pointer into
207  \texttt{cpinfos}
208
209  \item \texttt{CONSTANT\_NameAndType}: create a
210  \texttt{constant\_nameandtype} structure, get the UTF8 name and
211  description string of the field or method and store them into the
212  \texttt{constant\_nameandtype} structure, store type
213  \texttt{CONSTANT\_NameAndType} into \texttt{cptags} and store a
214  pointer to the \texttt{constant\_nameandtype} structure into
215  \texttt{cpinfos}
216
217  \item \texttt{CONSTANT\_Fieldref}, \texttt{CONSTANT\_Methodref} and
218  \texttt{CONSTANT\_InterfaceMethodref}: create a
219  \texttt{constant\_FMIref} structure, get the referenced
220  \texttt{constant\_nameandtype} structure which contains the name and
221  descriptor resolved in a previous step and store the name and
222  descriptor into the \texttt{constant\_FMIref} structure, get the
223  pointer of the referenced class, which was created in a previous
224  step, and store the pointer of the class into the
225  \texttt{constant\_FMIref} structure, store the type of the current
226  constant pool entry in \texttt{cptags} and store a pointer to
227  \texttt{constant\_FMIref} in \texttt{cpinfos}
228 \end{itemize}
229
230 After we have loaded the complete constant pool and after loading the
231 class flags, we can resolve the class and super class of the currently
232 loaded class or interface.
233
234
235 \section{Data structures}
236
237 \section{Dynamic class loader}
238
239 \section{Eager - lazy class loading}
240
241 \section{Linking}
242
243 \section{Initialization}