configure.in: Changed mingw) to *mingw*).
[gcc.git] / libjava / prims.cc
1 // prims.cc - Code for core of runtime environment.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 #include <config.h>
12 #include <platform.h>
13
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <signal.h>
19
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23
24 #include <gcj/cni.h>
25 #include <jvm.h>
26 #include <java-signal.h>
27 #include <java-threads.h>
28
29 #ifdef ENABLE_JVMPI
30 #include <jvmpi.h>
31 #include <java/lang/ThreadGroup.h>
32 #endif
33
34 #ifndef DISABLE_GETENV_PROPERTIES
35 #include <ctype.h>
36 #include <java-props.h>
37 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
38 #else
39 #define PROCESS_GCJ_PROPERTIES
40 #endif // DISABLE_GETENV_PROPERTIES
41
42 #include <java/lang/Class.h>
43 #include <java/lang/ClassLoader.h>
44 #include <java/lang/Runtime.h>
45 #include <java/lang/String.h>
46 #include <java/lang/Thread.h>
47 #include <java/lang/ThreadGroup.h>
48 #include <java/lang/ArrayIndexOutOfBoundsException.h>
49 #include <java/lang/ArithmeticException.h>
50 #include <java/lang/ClassFormatError.h>
51 #include <java/lang/InternalError.h>
52 #include <java/lang/NegativeArraySizeException.h>
53 #include <java/lang/NullPointerException.h>
54 #include <java/lang/OutOfMemoryError.h>
55 #include <java/lang/System.h>
56 #include <java/lang/reflect/Modifier.h>
57 #include <java/io/PrintStream.h>
58 #include <java/lang/UnsatisfiedLinkError.h>
59 #include <java/lang/VirtualMachineError.h>
60 #include <gnu/gcj/runtime/VMClassLoader.h>
61 #include <gnu/gcj/runtime/FinalizerThread.h>
62 #include <gnu/gcj/runtime/FirstThread.h>
63
64 #ifdef USE_LTDL
65 #include <ltdl.h>
66 #endif
67
68 // We allocate a single OutOfMemoryError exception which we keep
69 // around for use if we run out of memory.
70 static java::lang::OutOfMemoryError *no_memory;
71
72 // Largest representable size_t.
73 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
74
75 static const char *no_properties[] = { NULL };
76
77 // Properties set at compile time.
78 const char **_Jv_Compiler_Properties = no_properties;
79
80 // The JAR file to add to the beginning of java.class.path.
81 const char *_Jv_Jar_Class_Path;
82
83 #ifndef DISABLE_GETENV_PROPERTIES
84 // Property key/value pairs.
85 property_pair *_Jv_Environment_Properties;
86 #endif
87
88 // The name of this executable.
89 static char *_Jv_execName;
90
91 // Stash the argv pointer to benefit native libraries that need it.
92 const char **_Jv_argv;
93 int _Jv_argc;
94
95 #ifdef ENABLE_JVMPI
96 // Pointer to JVMPI notification functions.
97 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
98 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
99 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
100 #endif
101 \f
102
103 extern "C" void _Jv_ThrowSignal (jthrowable) __attribute ((noreturn));
104
105 // Just like _Jv_Throw, but fill in the stack trace first. Although
106 // this is declared extern in order that its name not be mangled, it
107 // is not intended to be used outside this file.
108 void
109 _Jv_ThrowSignal (jthrowable throwable)
110 {
111 throwable->fillInStackTrace ();
112 throw throwable;
113 }
114
115 #ifdef HANDLE_SEGV
116 static java::lang::NullPointerException *nullp;
117
118 SIGNAL_HANDLER (catch_segv)
119 {
120 MAKE_THROW_FRAME (nullp);
121 _Jv_ThrowSignal (nullp);
122 }
123 #endif
124
125 static java::lang::ArithmeticException *arithexception;
126
127 #ifdef HANDLE_FPE
128 SIGNAL_HANDLER (catch_fpe)
129 {
130 #ifdef HANDLE_DIVIDE_OVERFLOW
131 HANDLE_DIVIDE_OVERFLOW;
132 #else
133 MAKE_THROW_FRAME (arithexception);
134 #endif
135 _Jv_ThrowSignal (arithexception);
136 }
137 #endif
138
139 \f
140
141 jboolean
142 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
143 {
144 int len;
145 _Jv_ushort *aptr, *bptr;
146 if (a == b)
147 return true;
148 if (a->hash != b->hash)
149 return false;
150 len = a->length;
151 if (b->length != len)
152 return false;
153 aptr = (_Jv_ushort *)a->data;
154 bptr = (_Jv_ushort *)b->data;
155 len = (len + 1) >> 1;
156 while (--len >= 0)
157 if (*aptr++ != *bptr++)
158 return false;
159 return true;
160 }
161
162 /* True iff A is equal to STR.
163 HASH is STR->hashCode().
164 */
165
166 jboolean
167 _Jv_equal (Utf8Const* a, jstring str, jint hash)
168 {
169 if (a->hash != (_Jv_ushort) hash)
170 return false;
171 jint len = str->length();
172 jint i = 0;
173 jchar *sptr = _Jv_GetStringChars (str);
174 unsigned char* ptr = (unsigned char*) a->data;
175 unsigned char* limit = ptr + a->length;
176 for (;; i++, sptr++)
177 {
178 int ch = UTF8_GET (ptr, limit);
179 if (i == len)
180 return ch < 0;
181 if (ch != *sptr)
182 return false;
183 }
184 return true;
185 }
186
187 /* Like _Jv_equal, but stop after N characters. */
188 jboolean
189 _Jv_equaln (Utf8Const *a, jstring str, jint n)
190 {
191 jint len = str->length();
192 jint i = 0;
193 jchar *sptr = _Jv_GetStringChars (str);
194 unsigned char* ptr = (unsigned char*) a->data;
195 unsigned char* limit = ptr + a->length;
196 for (; n-- > 0; i++, sptr++)
197 {
198 int ch = UTF8_GET (ptr, limit);
199 if (i == len)
200 return ch < 0;
201 if (ch != *sptr)
202 return false;
203 }
204 return true;
205 }
206
207 /* Count the number of Unicode chars encoded in a given Ut8 string. */
208 int
209 _Jv_strLengthUtf8(char* str, int len)
210 {
211 unsigned char* ptr;
212 unsigned char* limit;
213 int str_length;
214
215 ptr = (unsigned char*) str;
216 limit = ptr + len;
217 str_length = 0;
218 for (; ptr < limit; str_length++)
219 {
220 if (UTF8_GET (ptr, limit) < 0)
221 return (-1);
222 }
223 return (str_length);
224 }
225
226 /* Calculate a hash value for a string encoded in Utf8 format.
227 * This returns the same hash value as specified or java.lang.String.hashCode.
228 */
229 static jint
230 hashUtf8String (char* str, int len)
231 {
232 unsigned char* ptr = (unsigned char*) str;
233 unsigned char* limit = ptr + len;
234 jint hash = 0;
235
236 for (; ptr < limit;)
237 {
238 int ch = UTF8_GET (ptr, limit);
239 /* Updated specification from
240 http://www.javasoft.com/docs/books/jls/clarify.html. */
241 hash = (31 * hash) + ch;
242 }
243 return hash;
244 }
245
246 _Jv_Utf8Const *
247 _Jv_makeUtf8Const (char* s, int len)
248 {
249 if (len < 0)
250 len = strlen (s);
251 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
252 memcpy (m->data, s, len);
253 m->data[len] = 0;
254 m->length = len;
255 m->hash = hashUtf8String (s, len) & 0xFFFF;
256 return (m);
257 }
258
259 _Jv_Utf8Const *
260 _Jv_makeUtf8Const (jstring string)
261 {
262 jint hash = string->hashCode ();
263 jint len = _Jv_GetStringUTFLength (string);
264
265 Utf8Const* m = (Utf8Const*)
266 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
267
268 m->hash = hash;
269 m->length = len;
270
271 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
272 m->data[len] = 0;
273
274 return m;
275 }
276
277 \f
278
279 #ifdef DEBUG
280 void
281 _Jv_Abort (const char *function, const char *file, int line,
282 const char *message)
283 #else
284 void
285 _Jv_Abort (const char *, const char *, int, const char *message)
286 #endif
287 {
288 #ifdef DEBUG
289 fprintf (stderr,
290 "libgcj failure: %s\n in function %s, file %s, line %d\n",
291 message, function, file, line);
292 #else
293 fprintf (stderr, "libgcj failure: %s\n", message);
294 #endif
295 abort ();
296 }
297
298 static void
299 fail_on_finalization (jobject)
300 {
301 JvFail ("object was finalized");
302 }
303
304 void
305 _Jv_GCWatch (jobject obj)
306 {
307 _Jv_RegisterFinalizer (obj, fail_on_finalization);
308 }
309
310 void
311 _Jv_ThrowBadArrayIndex(jint bad_index)
312 {
313 throw new java::lang::ArrayIndexOutOfBoundsException
314 (java::lang::String::valueOf (bad_index));
315 }
316
317 void
318 _Jv_ThrowNullPointerException ()
319 {
320 throw new java::lang::NullPointerException;
321 }
322
323 // Explicitly throw a no memory exception.
324 // The collector calls this when it encounters an out-of-memory condition.
325 void _Jv_ThrowNoMemory()
326 {
327 throw no_memory;
328 }
329
330 #ifdef ENABLE_JVMPI
331 static void
332 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
333 {
334 // Service JVMPI allocation request.
335 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
336 {
337 JVMPI_Event event;
338
339 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
340 event.env_id = NULL;
341 event.u.obj_alloc.arena_id = 0;
342 event.u.obj_alloc.class_id = (jobjectID) klass;
343 event.u.obj_alloc.is_array = 0;
344 event.u.obj_alloc.size = size;
345 event.u.obj_alloc.obj_id = (jobjectID) obj;
346
347 // FIXME: This doesn't look right for the Boehm GC. A GC may
348 // already be in progress. _Jv_DisableGC () doesn't wait for it.
349 // More importantly, I don't see the need for disabling GC, since we
350 // blatantly have a pointer to obj on our stack, ensuring that the
351 // object can't be collected. Even for a nonconservative collector,
352 // it appears to me that this must be true, since we are about to
353 // return obj. Isn't this whole approach way too intrusive for
354 // a useful profiling interface? - HB
355 _Jv_DisableGC ();
356 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
357 _Jv_EnableGC ();
358 }
359 }
360 #else /* !ENABLE_JVMPI */
361 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
362 #endif
363
364 // Allocate a new object of class KLASS. SIZE is the size of the object
365 // to allocate. You might think this is redundant, but it isn't; some
366 // classes, such as String, aren't of fixed size.
367 // First a version that assumes that we have no finalizer, and that
368 // the class is already initialized.
369 // If we know that JVMPI is disabled, this can be replaced by a direct call
370 // to the allocator for the appropriate GC.
371 jobject
372 _Jv_AllocObjectNoInitNoFinalizer (jclass klass, jint size)
373 {
374 jobject obj = (jobject) _Jv_AllocObj (size, klass);
375 jvmpi_notify_alloc (klass, size, obj);
376 return obj;
377 }
378
379 // And now a version that initializes if necessary.
380 jobject
381 _Jv_AllocObjectNoFinalizer (jclass klass, jint size)
382 {
383 _Jv_InitClass (klass);
384 jobject obj = (jobject) _Jv_AllocObj (size, klass);
385 jvmpi_notify_alloc (klass, size, obj);
386 return obj;
387 }
388
389 // And now the general version that registers a finalizer if necessary.
390 jobject
391 _Jv_AllocObject (jclass klass, jint size)
392 {
393 jobject obj = _Jv_AllocObjectNoFinalizer (klass, size);
394
395 // We assume that the compiler only generates calls to this routine
396 // if there really is an interesting finalizer.
397 // Unfortunately, we still have to the dynamic test, since there may
398 // be cni calls to this routine.
399 // Nore that on IA64 get_finalizer() returns the starting address of the
400 // function, not a function pointer. Thus this still works.
401 if (klass->vtable->get_finalizer ()
402 != java::lang::Object::class$.vtable->get_finalizer ())
403 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
404 return obj;
405 }
406
407 // A version of the above that assumes the object contains no pointers,
408 // and requires no finalization. This can't happen if we need pointers
409 // to locks.
410 #ifdef JV_HASH_SYNCHRONIZATION
411 jobject
412 _Jv_AllocPtrFreeObject (jclass klass, jint size)
413 {
414 _Jv_InitClass (klass);
415
416 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
417
418 #ifdef ENABLE_JVMPI
419 // Service JVMPI request.
420
421 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
422 {
423 JVMPI_Event event;
424
425 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
426 event.env_id = NULL;
427 event.u.obj_alloc.arena_id = 0;
428 event.u.obj_alloc.class_id = (jobjectID) klass;
429 event.u.obj_alloc.is_array = 0;
430 event.u.obj_alloc.size = size;
431 event.u.obj_alloc.obj_id = (jobjectID) obj;
432
433 _Jv_DisableGC ();
434 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
435 _Jv_EnableGC ();
436 }
437 #endif
438
439 return obj;
440 }
441 #endif /* JV_HASH_SYNCHRONIZATION */
442
443
444 // Allocate a new array of Java objects. Each object is of type
445 // `elementClass'. `init' is used to initialize each slot in the
446 // array.
447 jobjectArray
448 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
449 {
450 if (__builtin_expect (count < 0, false))
451 throw new java::lang::NegativeArraySizeException;
452
453 JvAssert (! elementClass->isPrimitive ());
454
455 // Ensure that elements pointer is properly aligned.
456 jobjectArray obj = NULL;
457 size_t size = (size_t) elements (obj);
458 size += count * sizeof (jobject);
459
460 // FIXME: second argument should be "current loader"
461 jclass klass = _Jv_GetArrayClass (elementClass, 0);
462
463 obj = (jobjectArray) _Jv_AllocArray (size, klass);
464 // Cast away const.
465 jsize *lp = const_cast<jsize *> (&obj->length);
466 *lp = count;
467 // We know the allocator returns zeroed memory. So don't bother
468 // zeroing it again.
469 if (init)
470 {
471 jobject *ptr = elements(obj);
472 while (--count >= 0)
473 *ptr++ = init;
474 }
475 return obj;
476 }
477
478 // Allocate a new array of primitives. ELTYPE is the type of the
479 // element, COUNT is the size of the array.
480 jobject
481 _Jv_NewPrimArray (jclass eltype, jint count)
482 {
483 int elsize = eltype->size();
484 if (__builtin_expect (count < 0, false))
485 throw new java::lang::NegativeArraySizeException;
486
487 JvAssert (eltype->isPrimitive ());
488 jobject dummy = NULL;
489 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
490
491 // Check for overflow.
492 if (__builtin_expect ((size_t) count >
493 (SIZE_T_MAX - size) / elsize, false))
494 throw no_memory;
495
496 jclass klass = _Jv_GetArrayClass (eltype, 0);
497
498 # ifdef JV_HASH_SYNCHRONIZATION
499 // Since the vtable is always statically allocated,
500 // these are completely pointerfree! Make sure the GC doesn't touch them.
501 __JArray *arr =
502 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
503 memset((char *)arr + size, 0, elsize * count);
504 # else
505 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
506 // Note that we assume we are given zeroed memory by the allocator.
507 # endif
508 // Cast away const.
509 jsize *lp = const_cast<jsize *> (&arr->length);
510 *lp = count;
511
512 return arr;
513 }
514
515 jobject
516 _Jv_NewArray (jint type, jint size)
517 {
518 switch (type)
519 {
520 case 4: return JvNewBooleanArray (size);
521 case 5: return JvNewCharArray (size);
522 case 6: return JvNewFloatArray (size);
523 case 7: return JvNewDoubleArray (size);
524 case 8: return JvNewByteArray (size);
525 case 9: return JvNewShortArray (size);
526 case 10: return JvNewIntArray (size);
527 case 11: return JvNewLongArray (size);
528 }
529 throw new java::lang::InternalError
530 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
531 }
532
533 // Allocate a possibly multi-dimensional array but don't check that
534 // any array length is <0.
535 static jobject
536 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
537 {
538 JvAssert (type->isArray());
539 jclass element_type = type->getComponentType();
540 jobject result;
541 if (element_type->isPrimitive())
542 result = _Jv_NewPrimArray (element_type, sizes[0]);
543 else
544 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
545
546 if (dimensions > 1)
547 {
548 JvAssert (! element_type->isPrimitive());
549 JvAssert (element_type->isArray());
550 jobject *contents = elements ((jobjectArray) result);
551 for (int i = 0; i < sizes[0]; ++i)
552 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
553 sizes + 1);
554 }
555
556 return result;
557 }
558
559 jobject
560 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
561 {
562 for (int i = 0; i < dimensions; ++i)
563 if (sizes[i] < 0)
564 throw new java::lang::NegativeArraySizeException;
565
566 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
567 }
568
569 jobject
570 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
571 {
572 va_list args;
573 jint sizes[dimensions];
574 va_start (args, dimensions);
575 for (int i = 0; i < dimensions; ++i)
576 {
577 jint size = va_arg (args, jint);
578 if (size < 0)
579 throw new java::lang::NegativeArraySizeException;
580 sizes[i] = size;
581 }
582 va_end (args);
583
584 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
585 }
586
587 \f
588
589 #define DECLARE_PRIM_TYPE(NAME) \
590 _Jv_ArrayVTable _Jv_##NAME##VTable; \
591 java::lang::Class _Jv_##NAME##Class;
592
593 DECLARE_PRIM_TYPE(byte);
594 DECLARE_PRIM_TYPE(short);
595 DECLARE_PRIM_TYPE(int);
596 DECLARE_PRIM_TYPE(long);
597 DECLARE_PRIM_TYPE(boolean);
598 DECLARE_PRIM_TYPE(char);
599 DECLARE_PRIM_TYPE(float);
600 DECLARE_PRIM_TYPE(double);
601 DECLARE_PRIM_TYPE(void);
602
603 void
604 _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len,
605 _Jv_ArrayVTable *array_vtable)
606 {
607 using namespace java::lang::reflect;
608
609 _Jv_InitNewClassFields (cl);
610
611 // We must set the vtable for the class; the Java constructor
612 // doesn't do this.
613 (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
614
615 // Initialize the fields we care about. We do this in the same
616 // order they are declared in Class.h.
617 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
618 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
619 cl->method_count = sig;
620 cl->size_in_bytes = len;
621 cl->vtable = JV_PRIMITIVE_VTABLE;
622 cl->state = JV_STATE_DONE;
623 cl->depth = -1;
624 if (sig != 'V')
625 _Jv_NewArrayClass (cl, NULL, (_Jv_VTable *) array_vtable);
626 }
627
628 jclass
629 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
630 {
631 switch (*sig)
632 {
633 case 'B':
634 return JvPrimClass (byte);
635 case 'S':
636 return JvPrimClass (short);
637 case 'I':
638 return JvPrimClass (int);
639 case 'J':
640 return JvPrimClass (long);
641 case 'Z':
642 return JvPrimClass (boolean);
643 case 'C':
644 return JvPrimClass (char);
645 case 'F':
646 return JvPrimClass (float);
647 case 'D':
648 return JvPrimClass (double);
649 case 'V':
650 return JvPrimClass (void);
651 case 'L':
652 {
653 int i;
654 for (i = 1; sig[i] && sig[i] != ';'; ++i)
655 ;
656 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
657 return _Jv_FindClass (name, loader);
658
659 }
660 case '[':
661 {
662 jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
663 if (! klass)
664 return NULL;
665 return _Jv_GetArrayClass (klass, loader);
666 }
667 }
668
669 return NULL; // Placate compiler.
670 }
671
672 \f
673
674 JArray<jstring> *
675 JvConvertArgv (int argc, const char **argv)
676 {
677 if (argc < 0)
678 argc = 0;
679 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
680 jobject *ptr = elements(ar);
681 jbyteArray bytes = NULL;
682 for (int i = 0; i < argc; i++)
683 {
684 const char *arg = argv[i];
685 int len = strlen (arg);
686 if (bytes == NULL || bytes->length < len)
687 bytes = JvNewByteArray (len);
688 jbyte *bytePtr = elements (bytes);
689 // We assume jbyte == char.
690 memcpy (bytePtr, arg, len);
691
692 // Now convert using the default encoding.
693 *ptr++ = new java::lang::String (bytes, 0, len);
694 }
695 return (JArray<jstring>*) ar;
696 }
697
698 // FIXME: These variables are static so that they will be
699 // automatically scanned by the Boehm collector. This is needed
700 // because with qthreads the collector won't scan the initial stack --
701 // it will only scan the qthreads stacks.
702
703 // Command line arguments.
704 static JArray<jstring> *arg_vec;
705
706 // The primary thread.
707 static java::lang::Thread *main_thread;
708
709 char *
710 _Jv_ThisExecutable (void)
711 {
712 return _Jv_execName;
713 }
714
715 void
716 _Jv_ThisExecutable (const char *name)
717 {
718 if (name)
719 {
720 _Jv_execName = (char *) _Jv_Malloc (strlen (name) + 1);
721 strcpy (_Jv_execName, name);
722 }
723 }
724
725 #ifndef DISABLE_GETENV_PROPERTIES
726
727 static char *
728 next_property_key (char *s, size_t *length)
729 {
730 size_t l = 0;
731
732 JvAssert (s);
733
734 // Skip over whitespace
735 while (isspace (*s))
736 s++;
737
738 // If we've reached the end, return NULL. Also return NULL if for
739 // some reason we've come across a malformed property string.
740 if (*s == 0
741 || *s == ':'
742 || *s == '=')
743 return NULL;
744
745 // Determine the length of the property key.
746 while (s[l] != 0
747 && ! isspace (s[l])
748 && s[l] != ':'
749 && s[l] != '=')
750 {
751 if (s[l] == '\\'
752 && s[l+1] != 0)
753 l++;
754 l++;
755 }
756
757 *length = l;
758
759 return s;
760 }
761
762 static char *
763 next_property_value (char *s, size_t *length)
764 {
765 size_t l = 0;
766
767 JvAssert (s);
768
769 while (isspace (*s))
770 s++;
771
772 if (*s == ':'
773 || *s == '=')
774 s++;
775
776 while (isspace (*s))
777 s++;
778
779 // If we've reached the end, return NULL.
780 if (*s == 0)
781 return NULL;
782
783 // Determine the length of the property value.
784 while (s[l] != 0
785 && ! isspace (s[l])
786 && s[l] != ':'
787 && s[l] != '=')
788 {
789 if (s[l] == '\\'
790 && s[l+1] != 0)
791 l += 2;
792 else
793 l++;
794 }
795
796 *length = l;
797
798 return s;
799 }
800
801 static void
802 process_gcj_properties ()
803 {
804 char *props = getenv("GCJ_PROPERTIES");
805 char *p = props;
806 size_t length;
807 size_t property_count = 0;
808
809 if (NULL == props)
810 return;
811
812 // Whip through props quickly in order to count the number of
813 // property values.
814 while (p && (p = next_property_key (p, &length)))
815 {
816 // Skip to the end of the key
817 p += length;
818
819 p = next_property_value (p, &length);
820 if (p)
821 p += length;
822
823 property_count++;
824 }
825
826 // Allocate an array of property value/key pairs.
827 _Jv_Environment_Properties =
828 (property_pair *) malloc (sizeof(property_pair)
829 * (property_count + 1));
830
831 // Go through the properties again, initializing _Jv_Properties
832 // along the way.
833 p = props;
834 property_count = 0;
835 while (p && (p = next_property_key (p, &length)))
836 {
837 _Jv_Environment_Properties[property_count].key = p;
838 _Jv_Environment_Properties[property_count].key_length = length;
839
840 // Skip to the end of the key
841 p += length;
842
843 p = next_property_value (p, &length);
844
845 _Jv_Environment_Properties[property_count].value = p;
846 _Jv_Environment_Properties[property_count].value_length = length;
847
848 if (p)
849 p += length;
850
851 property_count++;
852 }
853 memset ((void *) &_Jv_Environment_Properties[property_count],
854 0, sizeof (property_pair));
855 {
856 size_t i = 0;
857
858 // Null terminate the strings.
859 while (_Jv_Environment_Properties[i].key)
860 {
861 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
862 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
863 }
864 }
865 }
866 #endif // DISABLE_GETENV_PROPERTIES
867
868 namespace gcj
869 {
870 _Jv_Utf8Const *void_signature;
871 _Jv_Utf8Const *clinit_name;
872 _Jv_Utf8Const *init_name;
873 _Jv_Utf8Const *finit_name;
874
875 bool runtimeInitialized = false;
876 }
877
878 jint
879 _Jv_CreateJavaVM (void* /*vm_args*/)
880 {
881 using namespace gcj;
882
883 if (runtimeInitialized)
884 return -1;
885
886 runtimeInitialized = true;
887
888 PROCESS_GCJ_PROPERTIES;
889
890 _Jv_InitThreads ();
891 _Jv_InitGC ();
892 _Jv_InitializeSyncMutex ();
893
894 /* Initialize Utf8 constants declared in jvm.h. */
895 void_signature = _Jv_makeUtf8Const ("()V", 3);
896 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
897 init_name = _Jv_makeUtf8Const ("<init>", 6);
898 finit_name = _Jv_makeUtf8Const ("finit$", 6);
899
900 /* Initialize built-in classes to represent primitive TYPEs. */
901 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1, &_Jv_byteVTable);
902 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2, &_Jv_shortVTable);
903 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4, &_Jv_intVTable);
904 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8, &_Jv_longVTable);
905 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1, &_Jv_booleanVTable);
906 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2, &_Jv_charVTable);
907 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4, &_Jv_floatVTable);
908 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8, &_Jv_doubleVTable);
909 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0, &_Jv_voidVTable);
910
911 // Turn stack trace generation off while creating exception objects.
912 _Jv_InitClass (&java::lang::Throwable::class$);
913 java::lang::Throwable::trace_enabled = 0;
914
915 INIT_SEGV;
916 #ifdef HANDLE_FPE
917 INIT_FPE;
918 #else
919 arithexception = new java::lang::ArithmeticException
920 (JvNewStringLatin1 ("/ by zero"));
921 #endif
922
923 no_memory = new java::lang::OutOfMemoryError;
924
925 java::lang::Throwable::trace_enabled = 1;
926
927 #ifdef USE_LTDL
928 LTDL_SET_PRELOADED_SYMBOLS ();
929 #endif
930
931 #ifdef WIN32
932 // Initialise winsock for networking
933 WSADATA data;
934 if (WSAStartup (MAKEWORD (1, 1), &data))
935 MessageBox (NULL, "Error initialising winsock library.", "Error", MB_OK | MB_ICONEXCLAMATION);
936 // Install exception handler
937 SetUnhandledExceptionFilter (win32_exception_handler);
938 #elif defined(HAVE_SIGACTION)
939 // We only want this on POSIX systems.
940 struct sigaction act;
941 act.sa_handler = SIG_IGN;
942 sigemptyset (&act.sa_mask);
943 act.sa_flags = 0;
944 sigaction (SIGPIPE, &act, NULL);
945 #else
946 signal (SIGPIPE, SIG_IGN);
947 #endif
948
949 _Jv_JNI_Init ();
950
951 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
952
953 // Start the GC finalizer thread. A VirtualMachineError can be
954 // thrown by the runtime if, say, threads aren't available. In this
955 // case finalizers simply won't run.
956 try
957 {
958 using namespace gnu::gcj::runtime;
959 FinalizerThread *ft = new FinalizerThread ();
960 ft->start ();
961 }
962 catch (java::lang::VirtualMachineError *ignore)
963 {
964 }
965
966 return 0;
967 }
968
969 void
970 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
971 bool is_jar)
972 {
973 _Jv_argv = argv;
974 _Jv_argc = argc;
975
976 java::lang::Runtime *runtime = NULL;
977
978
979 #ifdef DISABLE_MAIN_ARGS
980 _Jv_ThisExecutable ("[Embedded App]");
981 #else
982 #ifdef HAVE_PROC_SELF_EXE
983 char exec_name[20];
984 sprintf (exec_name, "/proc/%d/exe", getpid ());
985 _Jv_ThisExecutable (exec_name);
986 #else
987 _Jv_ThisExecutable (argv[0]);
988 #endif /* HAVE_PROC_SELF_EXE */
989 #endif /* DISABLE_MAIN_ARGS */
990
991 try
992 {
993 // Set this very early so that it is seen when java.lang.System
994 // is initialized.
995 if (is_jar)
996 _Jv_Jar_Class_Path = strdup (name);
997 _Jv_CreateJavaVM (NULL);
998
999 // Get the Runtime here. We want to initialize it before searching
1000 // for `main'; that way it will be set up if `main' is a JNI method.
1001 runtime = java::lang::Runtime::getRuntime ();
1002
1003 #ifdef DISABLE_MAIN_ARGS
1004 arg_vec = JvConvertArgv (0, 0);
1005 #else
1006 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1007 #endif
1008
1009 using namespace gnu::gcj::runtime;
1010 if (klass)
1011 main_thread = new FirstThread (klass, arg_vec);
1012 else
1013 main_thread = new FirstThread (JvNewStringLatin1 (name),
1014 arg_vec, is_jar);
1015 }
1016 catch (java::lang::Throwable *t)
1017 {
1018 java::lang::System::err->println (JvNewStringLatin1
1019 ("Exception during runtime initialization"));
1020 t->printStackTrace();
1021 runtime->exit (1);
1022 }
1023
1024 _Jv_AttachCurrentThread (main_thread);
1025 _Jv_ThreadRun (main_thread);
1026 _Jv_ThreadWait ();
1027
1028 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1029 runtime->exit (status);
1030 }
1031
1032 void
1033 JvRunMain (jclass klass, int argc, const char **argv)
1034 {
1035 _Jv_RunMain (klass, NULL, argc, argv, false);
1036 }
1037
1038 \f
1039
1040 // Parse a string and return a heap size.
1041 static size_t
1042 parse_heap_size (const char *spec)
1043 {
1044 char *end;
1045 unsigned long val = strtoul (spec, &end, 10);
1046 if (*end == 'k' || *end == 'K')
1047 val *= 1024;
1048 else if (*end == 'm' || *end == 'M')
1049 val *= 1048576;
1050 return (size_t) val;
1051 }
1052
1053 // Set the initial heap size. This might be ignored by the GC layer.
1054 // This must be called before _Jv_RunMain.
1055 void
1056 _Jv_SetInitialHeapSize (const char *arg)
1057 {
1058 size_t size = parse_heap_size (arg);
1059 _Jv_GCSetInitialHeapSize (size);
1060 }
1061
1062 // Set the maximum heap size. This might be ignored by the GC layer.
1063 // This must be called before _Jv_RunMain.
1064 void
1065 _Jv_SetMaximumHeapSize (const char *arg)
1066 {
1067 size_t size = parse_heap_size (arg);
1068 _Jv_GCSetMaximumHeapSize (size);
1069 }
1070
1071 \f
1072
1073 void *
1074 _Jv_Malloc (jsize size)
1075 {
1076 if (__builtin_expect (size == 0, false))
1077 size = 1;
1078 void *ptr = malloc ((size_t) size);
1079 if (__builtin_expect (ptr == NULL, false))
1080 throw no_memory;
1081 return ptr;
1082 }
1083
1084 void *
1085 _Jv_Realloc (void *ptr, jsize size)
1086 {
1087 if (__builtin_expect (size == 0, false))
1088 size = 1;
1089 ptr = realloc (ptr, (size_t) size);
1090 if (__builtin_expect (ptr == NULL, false))
1091 throw no_memory;
1092 return ptr;
1093 }
1094
1095 void *
1096 _Jv_MallocUnchecked (jsize size)
1097 {
1098 if (__builtin_expect (size == 0, false))
1099 size = 1;
1100 return malloc ((size_t) size);
1101 }
1102
1103 void
1104 _Jv_Free (void* ptr)
1105 {
1106 return free (ptr);
1107 }
1108
1109 \f
1110
1111 // In theory, these routines can be #ifdef'd away on machines which
1112 // support divide overflow signals. However, we never know if some
1113 // code might have been compiled with "-fuse-divide-subroutine", so we
1114 // always include them in libgcj.
1115
1116 jint
1117 _Jv_divI (jint dividend, jint divisor)
1118 {
1119 if (__builtin_expect (divisor == 0, false))
1120 _Jv_ThrowSignal (arithexception);
1121
1122 if (dividend == (jint) 0x80000000L && divisor == -1)
1123 return dividend;
1124
1125 return dividend / divisor;
1126 }
1127
1128 jint
1129 _Jv_remI (jint dividend, jint divisor)
1130 {
1131 if (__builtin_expect (divisor == 0, false))
1132 _Jv_ThrowSignal (arithexception);
1133
1134 if (dividend == (jint) 0x80000000L && divisor == -1)
1135 return 0;
1136
1137 return dividend % divisor;
1138 }
1139
1140 jlong
1141 _Jv_divJ (jlong dividend, jlong divisor)
1142 {
1143 if (__builtin_expect (divisor == 0, false))
1144 _Jv_ThrowSignal (arithexception);
1145
1146 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1147 return dividend;
1148
1149 return dividend / divisor;
1150 }
1151
1152 jlong
1153 _Jv_remJ (jlong dividend, jlong divisor)
1154 {
1155 if (__builtin_expect (divisor == 0, false))
1156 _Jv_ThrowSignal (arithexception);
1157
1158 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1159 return 0;
1160
1161 return dividend % divisor;
1162 }