CACAO
initialize.cpp
Go to the documentation of this file.
1 /* src/vm/initialize.cpp - static class initializer functions
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 
26 #include "config.h"
27 
28 #include <string.h>
29 
30 #include "vm/types.hpp"
31 
32 #include "threads/thread.hpp"
33 #include "threads/atomic.hpp" // for write_memory_barrier
34 #include "threads/lock.hpp"
35 
36 #include "toolbox/logging.hpp"
37 
38 #include "vm/jit/builtin.hpp"
39 #include "vm/class.hpp"
40 #include "vm/exceptions.hpp"
41 #include "vm/global.hpp"
42 #include "vm/globals.hpp"
43 #include "vm/initialize.hpp"
44 #include "vm/loader.hpp"
45 #include "vm/options.hpp"
46 #include "vm/vm.hpp"
47 #include "vm/statistics.hpp"
48 
49 
50 STAT_REGISTER_VAR(int,count_class_inits,0,"class inits","Number of class inits")
51 
52 /* private functions **********************************************************/
53 
54 static bool initialize_class_intern(classinfo *c);
55 
56 
57 /* initialize_init *************************************************************
58 
59  Initialize important system classes.
60 
61 *******************************************************************************/
62 
63 void initialize_init(void)
64 {
65  TRACESUBSYSTEMINITIALIZATION("initialize_init");
66 
67 #if defined(ENABLE_JAVASE)
68 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
69 
70  /* Nothing. */
71 
72 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
73 
75  vm_abort("initialize_init: Initialization failed: java.lang.String");
76 
78  vm_abort("initialize_init: Initialization failed: java.lang.System");
79 
81  vm_abort("initialize_init: Initialization failed: java.lang.ThreadGroup");
82 
84  vm_abort("initialize_init: Initialization failed: java.lang.Thread");
85 
87  vm_abort("initialize_init: Initialization failed: java.lang.Class");
88 
89 # else
90 # error unknown classpath configuration
91 # endif
92 
93 #elif defined(ENABLE_JAVAME_CLDC1_1)
94 
95  /* Nothing. */
96 
97 #else
98 # error unknown Java configuration
99 #endif
100 }
101 
102 /* initialize_class ************************************************************
103 
104  In Java, every class can have a static initialization
105  function. This function has to be called BEFORE calling other
106  methods or accessing static variables.
107 
108 *******************************************************************************/
109 
111 {
112  bool r;
113 
114  if (!makeinitializations)
115  return true;
116 
118 
119  /* maybe the class is already initalized or the current thread, which can
120  pass the monitor, is currently initalizing this class */
121 
124 
125  return true;
126  }
127 
128  /* if <clinit> throw an Error before, the class was marked with an
129  error and we have to throw a NoClassDefFoundError */
130 
131  if (c->state & CLASS_ERROR) {
133 
135 
136  /* ...but return true, this is ok (mauve test) */
137 
138  return true;
139  }
140 
141  /* this initalizing run begins NOW */
142 
145 
146  /* call the internal function */
147 
149 
150  /* if return value is not NULL everything was ok and the class is
151  initialized */
152 
153  if (r) {
154  // Let's make sure that everything is flushed out to memory before
155  // marking the class as initialized.
157 
158  c->state |= CLASS_INITIALIZED;
159  }
160 
161  /* this initalizing run is done */
162 
163  c->state &= ~CLASS_INITIALIZING;
164 
166 
167  return r;
168 }
169 
170 
171 /* initialize_class_intern *****************************************************
172 
173  This function MUST NOT be called directly, because of thread
174  <clinit> race conditions.
175 
176 *******************************************************************************/
177 
179 {
180  methodinfo *m;
181  java_handle_t *cause;
182  classinfo *clazz;
183 
184  /* maybe the class is not already linked */
185 
186  if (!(c->state & CLASS_LINKED))
187  if (!link_class(c))
188  return false;
189 
190  STATISTICS(count_class_inits++);
191 
192  /* Initialize super class. */
193 
194  if (c->super != NULL) {
195  if (!(c->super->state & CLASS_INITIALIZED)) {
196 #if !defined(NDEBUG)
197  if (initverbose)
198  log_message_class_message_class("Initialize super class ",
199  c->super,
200  " from ",
201  c);
202 #endif
203 
204  if (!initialize_class(c->super))
205  return false;
206  }
207  }
208 
209  /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
210 
211  m = class_findmethod(c, utf8::clinit, utf8::void__void);
212 
213  if (m == NULL) {
214 #if !defined(NDEBUG)
215  if (initverbose)
216  log_message_class("Class has no static class initializer: ", c);
217 #endif
218 
219  return true;
220  }
221 
222  /* Sun's and IBM's JVM don't care about the static flag */
223 /* if (!(m->flags & ACC_STATIC)) { */
224 /* log_text("Class initializer is not static!"); */
225 
226 #if !defined(NDEBUG)
227  if (initverbose)
228  log_message_class("Starting static class initializer for class: ", c);
229 #endif
230 
231  /* now call the initializer */
232 
233  (void) vm_call_method(m, NULL);
234 
235  /* we have an exception or error */
236 
237  cause = exceptions_get_exception();
238 
239  if (cause != NULL) {
240  /* class is NOT initialized and is marked with error */
241 
242  c->state |= CLASS_ERROR;
243 
244  /* Load java/lang/Exception for the instanceof check. */
245 
246  clazz = load_class_bootstrap(utf8::java_lang_Exception);
247 
248  if (clazz == NULL)
249  return false;
250 
251  /* Is this an exception? Yes, than wrap it. */
252 
253  if (builtin_instanceof(cause, clazz)) {
254  /* clear exception, because we are calling jit code again */
255 
257 
258  /* wrap the exception */
259 
261  }
262 
263  return false;
264  }
265 
266 #if !defined(NDEBUG)
267  if (initverbose)
268  log_message_class("Finished static class initializer for class: ", c);
269 #endif
270 
271  return true;
272 }
273 
274 
275 /*
276  * These are local overrides for various environment variables in Emacs.
277  * Please do not remove this and leave it at the end of the file, where
278  * Emacs will automagically detect them.
279  * ---------------------------------------------------------------------
280  * Local variables:
281  * mode: c++
282  * indent-tabs-mode: t
283  * c-basic-offset: 4
284  * tab-width: 4
285  * End:
286  * vim:noexpandtab:sw=4:ts=4:
287  */
void write_memory_barrier(void)
Definition: atomic.hpp:97
bool builtin_instanceof(java_handle_t *o, classinfo *c)
Definition: builtin.cpp:403
classinfo * class_java_lang_ThreadGroup
Definition: globals.cpp:42
#define STATISTICS(x)
Wrapper for statistics only code.
Definition: statistics.hpp:975
classinfo * super
Definition: class.hpp:102
void log_message_class_message_class(const char *msg1, classinfo *c1, const char *msg2, classinfo *c2)
Definition: logging.cpp:251
void initialize_init(void)
Definition: initialize.cpp:63
#define LOCK_MONITOR_EXIT(o)
Definition: lock.hpp:94
void exceptions_throw_exceptionininitializererror(java_handle_t *cause)
Definition: exceptions.cpp:752
s4 state
Definition: class.hpp:115
typedef void(JNICALL *jvmtiEventSingleStep)(jvmtiEnv *jvmti_env
bool initverbose
Definition: options.cpp:71
classinfo * load_class_bootstrap(Utf8String name)
Definition: loader.cpp:1276
java_handle_t * vm_call_method(methodinfo *m, java_handle_t *o,...)
methodinfo * class_findmethod(classinfo *c, Utf8String name, Utf8String desc)
Definition: class.cpp:1124
#define TRACESUBSYSTEMINITIALIZATION(text)
Definition: options.hpp:258
void exceptions_throw_noclassdeffounderror(Utf8String name)
Definition: exceptions.cpp:700
void vm_abort(const char *text,...)
Definition: vm.cpp:2586
threadobject * initializing_thread
Definition: class.hpp:120
This file contains the statistics framework.
Utf8String name
Definition: class.hpp:91
classinfo * class_java_lang_Class
Definition: globals.cpp:35
classinfo * class_java_lang_System
Definition: globals.cpp:40
bool initialize_class(classinfo *c)
Definition: initialize.cpp:110
classinfo * class_java_lang_String
Definition: globals.cpp:39
static threadobject * thread_get_current()
Return the threadobject for the current thread.
Definition: thread-none.hpp:56
bool makeinitializations
Definition: options.cpp:95
classinfo * link_class(classinfo *c)
Definition: linker.cpp:378
#define LOCK_MONITOR_ENTER(o)
Definition: lock.hpp:93
void exceptions_clear_exception(void)
Definition: exceptions.cpp:127
static bool class_is_or_almost_initialized(classinfo *c)
Definition: class.hpp:433
java_handle_t * exceptions_get_exception(void)
Definition: exceptions.cpp:76
classinfo * class_java_lang_Thread
Definition: globals.cpp:41
static bool initialize_class_intern(classinfo *c)
Definition: initialize.cpp:178
#define STAT_REGISTER_VAR(type, var, init, name, description)
Register an external statistics variable.
Definition: statistics.hpp:966
void log_message_class(const char *msg, classinfo *c)
Definition: logging.cpp:237