1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
26 #include <java-signal.h>
27 #include <java-threads.h>
28 #include <java-interp.h>
32 #include <java/lang/ThreadGroup.h>
35 #ifndef DISABLE_GETENV_PROPERTIES
37 #include <java-props.h>
38 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
40 #define PROCESS_GCJ_PROPERTIES
41 #endif // DISABLE_GETENV_PROPERTIES
43 #include <java/lang/Class.h>
44 #include <java/lang/ClassLoader.h>
45 #include <java/lang/Runtime.h>
46 #include <java/lang/String.h>
47 #include <java/lang/Thread.h>
48 #include <java/lang/ThreadGroup.h>
49 #include <java/lang/ArrayIndexOutOfBoundsException.h>
50 #include <java/lang/ArithmeticException.h>
51 #include <java/lang/ClassFormatError.h>
52 #include <java/lang/ClassNotFoundException.h>
53 #include <java/lang/InternalError.h>
54 #include <java/lang/NegativeArraySizeException.h>
55 #include <java/lang/NoClassDefFoundError.h>
56 #include <java/lang/NullPointerException.h>
57 #include <java/lang/OutOfMemoryError.h>
58 #include <java/lang/System.h>
59 #include <java/lang/VMThrowable.h>
60 #include <java/lang/VMClassLoader.h>
61 #include <java/lang/reflect/Modifier.h>
62 #include <java/io/PrintStream.h>
63 #include <java/lang/UnsatisfiedLinkError.h>
64 #include <java/lang/VirtualMachineError.h>
65 #include <gnu/gcj/runtime/ExtensionClassLoader.h>
66 #include <gnu/gcj/runtime/FinalizerThread.h>
67 #include <execution.h>
68 #include <gnu/java/lang/MainThread.h>
74 // Execution engine for compiled code.
75 _Jv_CompiledEngine _Jv_soleCompiledEngine
;
77 // Execution engine for code compiled with -findirect-classes
78 _Jv_IndirectCompiledEngine _Jv_soleIndirectCompiledEngine
;
80 // We allocate a single OutOfMemoryError exception which we keep
81 // around for use if we run out of memory.
82 static java::lang::OutOfMemoryError
*no_memory
;
84 // Number of bytes in largest array object we create. This could be
85 // increased to the largest size_t value, so long as the appropriate
86 // functions are changed to take a size_t argument instead of jint.
87 #define MAX_OBJECT_SIZE ((1<<31) - 1)
89 // Properties set at compile time.
90 const char **_Jv_Compiler_Properties
= NULL
;
91 int _Jv_Properties_Count
= 0;
93 #ifndef DISABLE_GETENV_PROPERTIES
94 // Property key/value pairs.
95 property_pair
*_Jv_Environment_Properties
;
98 // Stash the argv pointer to benefit native libraries that need it.
99 const char **_Jv_argv
;
106 // _Jv_argc is 0 if not explicitly initialized.
111 _Jv_GetSafeArg (int index
)
113 if (index
>=0 && index
< _Jv_GetNbArgs ())
114 return _Jv_argv
[index
];
120 _Jv_SetArgs (int argc
, const char **argv
)
127 // Pointer to JVMPI notification functions.
128 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (JVMPI_Event
*event
);
129 void (*_Jv_JVMPI_Notify_THREAD_START
) (JVMPI_Event
*event
);
130 void (*_Jv_JVMPI_Notify_THREAD_END
) (JVMPI_Event
*event
);
134 #if defined (HANDLE_SEGV) || defined(HANDLE_FPE)
135 /* Unblock a signal. Unless we do this, the signal may only be sent
138 unblock_signal (int signum
__attribute__ ((__unused__
)))
140 #ifdef _POSIX_VERSION
144 sigaddset (&sigs
, signum
);
145 sigprocmask (SIG_UNBLOCK
, &sigs
, NULL
);
151 SIGNAL_HANDLER (catch_segv
)
153 unblock_signal (SIGSEGV
);
154 MAKE_THROW_FRAME (nullp
);
155 java::lang::NullPointerException
*nullp
156 = new java::lang::NullPointerException
;
162 SIGNAL_HANDLER (catch_fpe
)
164 unblock_signal (SIGFPE
);
165 #ifdef HANDLE_DIVIDE_OVERFLOW
166 HANDLE_DIVIDE_OVERFLOW
;
168 MAKE_THROW_FRAME (arithexception
);
170 java::lang::ArithmeticException
*arithexception
171 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
172 throw arithexception
;
178 _Jv_equalUtf8Consts (const Utf8Const
* a
, const Utf8Const
*b
)
181 const _Jv_ushort
*aptr
, *bptr
;
184 if (a
->hash
!= b
->hash
)
187 if (b
->length
!= len
)
189 aptr
= (const _Jv_ushort
*)a
->data
;
190 bptr
= (const _Jv_ushort
*)b
->data
;
191 len
= (len
+ 1) >> 1;
193 if (*aptr
++ != *bptr
++)
198 /* True iff A is equal to STR.
199 HASH is STR->hashCode().
203 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
205 if (a
->hash
!= (_Jv_ushort
) hash
)
207 jint len
= str
->length();
209 jchar
*sptr
= _Jv_GetStringChars (str
);
210 unsigned char* ptr
= (unsigned char*) a
->data
;
211 unsigned char* limit
= ptr
+ a
->length
;
214 int ch
= UTF8_GET (ptr
, limit
);
223 /* Like _Jv_equal, but stop after N characters. */
225 _Jv_equaln (Utf8Const
*a
, jstring str
, jint n
)
227 jint len
= str
->length();
229 jchar
*sptr
= _Jv_GetStringChars (str
);
230 unsigned char* ptr
= (unsigned char*) a
->data
;
231 unsigned char* limit
= ptr
+ a
->length
;
232 for (; n
-- > 0; i
++, sptr
++)
234 int ch
= UTF8_GET (ptr
, limit
);
243 // Determines whether the given Utf8Const object contains
244 // a type which is primitive or some derived form of it, eg.
245 // an array or multi-dimensional array variant.
247 _Jv_isPrimitiveOrDerived(const Utf8Const
*a
)
249 unsigned char *aptr
= (unsigned char *) a
->data
;
250 unsigned char *alimit
= aptr
+ a
->length
;
251 int ac
= UTF8_GET(aptr
, alimit
);
253 // Skips any leading array marks.
255 ac
= UTF8_GET(aptr
, alimit
);
257 // There should not be another character. This implies that
258 // the type name is only one character long.
259 if (UTF8_GET(aptr
, alimit
) == -1)
278 // Find out whether two _Jv_Utf8Const candidates contain the same
280 // The method is written to handle the different formats of classnames.
281 // Eg. "Ljava/lang/Class;", "Ljava.lang.Class;", "java/lang/Class" and
282 // "java.lang.Class" will be seen as equal.
283 // Warning: This function is not smart enough to declare "Z" and "boolean"
284 // and similar cases as equal (and is not meant to be used this way)!
286 _Jv_equalUtf8Classnames (const Utf8Const
*a
, const Utf8Const
*b
)
288 // If the class name's length differs by two characters
289 // it is possible that we have candidates which are given
290 // in the two different formats ("Lp1/p2/cn;" vs. "p1/p2/cn")
291 switch (a
->length
- b
->length
)
301 unsigned char *aptr
= (unsigned char *) a
->data
;
302 unsigned char *alimit
= aptr
+ a
->length
;
303 unsigned char *bptr
= (unsigned char *) b
->data
;
304 unsigned char *blimit
= bptr
+ b
->length
;
306 if (alimit
[-1] == ';')
309 if (blimit
[-1] == ';')
312 int ac
= UTF8_GET(aptr
, alimit
);
313 int bc
= UTF8_GET(bptr
, blimit
);
315 // Checks whether both strings have the same amount of leading [ characters.
320 ac
= UTF8_GET(aptr
, alimit
);
321 bc
= UTF8_GET(bptr
, blimit
);
328 // Skips leading L character.
330 ac
= UTF8_GET(aptr
, alimit
);
333 bc
= UTF8_GET(bptr
, blimit
);
335 // Compares the remaining characters.
336 while (ac
!= -1 && bc
!= -1)
338 // Replaces package separating dots with slashes.
345 // Now classnames differ if there is at least one non-matching
350 ac
= UTF8_GET(aptr
, alimit
);
351 bc
= UTF8_GET(bptr
, blimit
);
357 /* Count the number of Unicode chars encoded in a given Ut8 string. */
359 _Jv_strLengthUtf8(const char* str
, int len
)
362 unsigned char* limit
;
365 ptr
= (unsigned char*) str
;
368 for (; ptr
< limit
; str_length
++)
370 if (UTF8_GET (ptr
, limit
) < 0)
376 /* Calculate a hash value for a string encoded in Utf8 format.
377 * This returns the same hash value as specified or java.lang.String.hashCode.
380 _Jv_hashUtf8String (const char* str
, int len
)
382 unsigned char* ptr
= (unsigned char*) str
;
383 unsigned char* limit
= ptr
+ len
;
388 int ch
= UTF8_GET (ptr
, limit
);
389 /* Updated specification from
390 http://www.javasoft.com/docs/books/jls/clarify.html. */
391 hash
= (31 * hash
) + ch
;
397 _Jv_Utf8Const::init(const char *s
, int len
)
399 ::memcpy (data
, s
, len
);
402 hash
= _Jv_hashUtf8String (s
, len
) & 0xFFFF;
406 _Jv_makeUtf8Const (const char* s
, int len
)
411 = (Utf8Const
*) _Jv_AllocBytes (_Jv_Utf8Const::space_needed(s
, len
));
417 _Jv_makeUtf8Const (jstring string
)
419 jint hash
= string
->hashCode ();
420 jint len
= _Jv_GetStringUTFLength (string
);
422 Utf8Const
* m
= (Utf8Const
*)
423 _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
428 _Jv_GetStringUTFRegion (string
, 0, string
->length (), m
->data
);
438 _Jv_Abort (const char *function
, const char *file
, int line
,
442 _Jv_Abort (const char *, const char *, int, const char *message
)
447 "libgcj failure: %s\n in function %s, file %s, line %d\n",
448 message
, function
, file
, line
);
450 fprintf (stderr
, "libgcj failure: %s\n", message
);
456 fail_on_finalization (jobject
)
458 JvFail ("object was finalized");
462 _Jv_GCWatch (jobject obj
)
464 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
468 _Jv_ThrowBadArrayIndex(jint bad_index
)
470 throw new java::lang::ArrayIndexOutOfBoundsException
471 (java::lang::String::valueOf (bad_index
));
475 _Jv_ThrowNullPointerException ()
477 throw new java::lang::NullPointerException
;
480 // Resolve an entry in the constant pool and return the target
483 _Jv_ResolvePoolEntry (jclass this_class
, jint index
)
485 _Jv_Constants
*pool
= &this_class
->constants
;
487 if ((pool
->tags
[index
] & JV_CONSTANT_ResolvedFlag
) != 0)
488 return pool
->data
[index
].field
->u
.addr
;
490 JvSynchronize
sync (this_class
);
491 return (_Jv_Linker::resolve_pool_entry (this_class
, index
))
496 // Explicitly throw a no memory exception.
497 // The collector calls this when it encounters an out-of-memory condition.
498 void _Jv_ThrowNoMemory()
504 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) \
505 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false)) \
506 jvmpi_notify_alloc(klass,size,obj);
508 jvmpi_notify_alloc(jclass klass
, jint size
, jobject obj
)
510 // Service JVMPI allocation request.
513 event
.event_type
= JVMPI_EVENT_OBJECT_ALLOC
;
515 event
.u
.obj_alloc
.arena_id
= 0;
516 event
.u
.obj_alloc
.class_id
= (jobjectID
) klass
;
517 event
.u
.obj_alloc
.is_array
= 0;
518 event
.u
.obj_alloc
.size
= size
;
519 event
.u
.obj_alloc
.obj_id
= (jobjectID
) obj
;
521 // FIXME: This doesn't look right for the Boehm GC. A GC may
522 // already be in progress. _Jv_DisableGC () doesn't wait for it.
523 // More importantly, I don't see the need for disabling GC, since we
524 // blatantly have a pointer to obj on our stack, ensuring that the
525 // object can't be collected. Even for a nonconservative collector,
526 // it appears to me that this must be true, since we are about to
527 // return obj. Isn't this whole approach way too intrusive for
528 // a useful profiling interface? - HB
530 (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (&event
);
533 #else /* !ENABLE_JVMPI */
534 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) /* do nothing */
537 // Allocate a new object of class KLASS.
538 // First a version that assumes that we have no finalizer, and that
539 // the class is already initialized.
540 // If we know that JVMPI is disabled, this can be replaced by a direct call
541 // to the allocator for the appropriate GC.
543 _Jv_AllocObjectNoInitNoFinalizer (jclass klass
)
545 jint size
= klass
->size ();
546 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
547 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
551 // And now a version that initializes if necessary.
553 _Jv_AllocObjectNoFinalizer (jclass klass
)
555 if (_Jv_IsPhantomClass(klass
) )
556 throw new java::lang::NoClassDefFoundError(klass
->getName());
558 _Jv_InitClass (klass
);
559 jint size
= klass
->size ();
560 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
561 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
565 // And now the general version that registers a finalizer if necessary.
567 _Jv_AllocObject (jclass klass
)
569 jobject obj
= _Jv_AllocObjectNoFinalizer (klass
);
571 // We assume that the compiler only generates calls to this routine
572 // if there really is an interesting finalizer.
573 // Unfortunately, we still have to the dynamic test, since there may
574 // be cni calls to this routine.
575 // Note that on IA64 get_finalizer() returns the starting address of the
576 // function, not a function pointer. Thus this still works.
577 if (klass
->vtable
->get_finalizer ()
578 != java::lang::Object::class$
.vtable
->get_finalizer ())
579 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
583 // Allocate a String, including variable length storage.
585 _Jv_AllocString(jsize len
)
587 using namespace java::lang
;
589 jsize sz
= sizeof(java::lang::String
) + len
* sizeof(jchar
);
591 // We assert that for strings allocated this way, the data field
592 // will always point to the object itself. Thus there is no reason
593 // for the garbage collector to scan any of it.
594 // Furthermore, we're about to overwrite the string data, so
595 // initialization of the object is not an issue.
597 // String needs no initialization, and there is no finalizer, so
598 // we can go directly to the collector's allocator interface.
599 jstring obj
= (jstring
) _Jv_AllocPtrFreeObj(sz
, &String::class$
);
602 obj
->boffset
= sizeof(java::lang::String
);
604 obj
->cachedHashCode
= 0;
606 JVMPI_NOTIFY_ALLOC (&String::class$
, sz
, obj
);
611 // A version of the above that assumes the object contains no pointers,
612 // and requires no finalization. This can't happen if we need pointers
614 #ifdef JV_HASH_SYNCHRONIZATION
616 _Jv_AllocPtrFreeObject (jclass klass
)
618 _Jv_InitClass (klass
);
619 jint size
= klass
->size ();
621 jobject obj
= (jobject
) _Jv_AllocPtrFreeObj (size
, klass
);
623 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
627 #endif /* JV_HASH_SYNCHRONIZATION */
630 // Allocate a new array of Java objects. Each object is of type
631 // `elementClass'. `init' is used to initialize each slot in the
634 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
636 // Creating an array of an unresolved type is impossible. So we throw
637 // the NoClassDefFoundError.
638 if ( _Jv_IsPhantomClass(elementClass
) )
639 throw new java::lang::NoClassDefFoundError(elementClass
->getName());
641 if (__builtin_expect (count
< 0, false))
642 throw new java::lang::NegativeArraySizeException
;
644 JvAssert (! elementClass
->isPrimitive ());
646 // Ensure that elements pointer is properly aligned.
647 jobjectArray obj
= NULL
;
648 size_t size
= (size_t) elements (obj
);
649 // Check for overflow.
650 if (__builtin_expect ((size_t) count
>
651 (MAX_OBJECT_SIZE
- 1 - size
) / sizeof (jobject
), false))
654 size
+= count
* sizeof (jobject
);
656 jclass klass
= _Jv_GetArrayClass (elementClass
,
657 elementClass
->getClassLoaderInternal());
659 obj
= (jobjectArray
) _Jv_AllocArray (size
, klass
);
661 jsize
*lp
= const_cast<jsize
*> (&obj
->length
);
663 // We know the allocator returns zeroed memory. So don't bother
667 jobject
*ptr
= elements(obj
);
674 // Allocate a new array of primitives. ELTYPE is the type of the
675 // element, COUNT is the size of the array.
677 _Jv_NewPrimArray (jclass eltype
, jint count
)
679 int elsize
= eltype
->size();
680 if (__builtin_expect (count
< 0, false))
681 throw new java::lang::NegativeArraySizeException
;
683 JvAssert (eltype
->isPrimitive ());
684 jobject dummy
= NULL
;
685 size_t size
= (size_t) _Jv_GetArrayElementFromElementType (dummy
, eltype
);
687 // Check for overflow.
688 if (__builtin_expect ((size_t) count
>
689 (MAX_OBJECT_SIZE
- size
) / elsize
, false))
692 jclass klass
= _Jv_GetArrayClass (eltype
, 0);
694 # ifdef JV_HASH_SYNCHRONIZATION
695 // Since the vtable is always statically allocated,
696 // these are completely pointerfree! Make sure the GC doesn't touch them.
698 (__JArray
*) _Jv_AllocPtrFreeObj (size
+ elsize
* count
, klass
);
699 memset((char *)arr
+ size
, 0, elsize
* count
);
701 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (size
+ elsize
* count
, klass
);
702 // Note that we assume we are given zeroed memory by the allocator.
705 jsize
*lp
= const_cast<jsize
*> (&arr
->length
);
712 _Jv_NewArray (jint type
, jint size
)
716 case 4: return JvNewBooleanArray (size
);
717 case 5: return JvNewCharArray (size
);
718 case 6: return JvNewFloatArray (size
);
719 case 7: return JvNewDoubleArray (size
);
720 case 8: return JvNewByteArray (size
);
721 case 9: return JvNewShortArray (size
);
722 case 10: return JvNewIntArray (size
);
723 case 11: return JvNewLongArray (size
);
725 throw new java::lang::InternalError
726 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
729 // Allocate a possibly multi-dimensional array but don't check that
730 // any array length is <0.
732 _Jv_NewMultiArrayUnchecked (jclass type
, jint dimensions
, jint
*sizes
)
734 JvAssert (type
->isArray());
735 jclass element_type
= type
->getComponentType();
737 if (element_type
->isPrimitive())
738 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
740 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
744 JvAssert (! element_type
->isPrimitive());
745 JvAssert (element_type
->isArray());
746 jobject
*contents
= elements ((jobjectArray
) result
);
747 for (int i
= 0; i
< sizes
[0]; ++i
)
748 contents
[i
] = _Jv_NewMultiArrayUnchecked (element_type
, dimensions
- 1,
756 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
758 for (int i
= 0; i
< dimensions
; ++i
)
760 throw new java::lang::NegativeArraySizeException
;
762 return _Jv_NewMultiArrayUnchecked (type
, dimensions
, sizes
);
766 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
768 // Creating an array of an unresolved type is impossible. So we throw
769 // the NoClassDefFoundError.
770 if (_Jv_IsPhantomClass(array_type
))
771 throw new java::lang::NoClassDefFoundError(array_type
->getName());
774 jint sizes
[dimensions
];
775 va_start (args
, dimensions
);
776 for (int i
= 0; i
< dimensions
; ++i
)
778 jint size
= va_arg (args
, jint
);
780 throw new java::lang::NegativeArraySizeException
;
785 return _Jv_NewMultiArrayUnchecked (array_type
, dimensions
, sizes
);
790 // Ensure 8-byte alignment, for hash synchronization.
791 #define DECLARE_PRIM_TYPE(NAME) \
792 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
794 DECLARE_PRIM_TYPE(byte
)
795 DECLARE_PRIM_TYPE(short)
796 DECLARE_PRIM_TYPE(int)
797 DECLARE_PRIM_TYPE(long)
798 DECLARE_PRIM_TYPE(boolean
)
799 DECLARE_PRIM_TYPE(char)
800 DECLARE_PRIM_TYPE(float)
801 DECLARE_PRIM_TYPE(double)
802 DECLARE_PRIM_TYPE(void)
805 _Jv_InitPrimClass (jclass cl
, const char *cname
, char sig
, int len
)
807 using namespace java::lang::reflect
;
809 // We must set the vtable for the class; the Java constructor
811 (*(_Jv_VTable
**) cl
) = java::lang::Class::class$
.vtable
;
813 // Initialize the fields we care about. We do this in the same
814 // order they are declared in Class.h.
815 cl
->name
= _Jv_makeUtf8Const ((char *) cname
, -1);
816 cl
->accflags
= Modifier::PUBLIC
| Modifier::FINAL
| Modifier::ABSTRACT
;
817 cl
->method_count
= sig
;
818 cl
->size_in_bytes
= len
;
819 cl
->vtable
= JV_PRIMITIVE_VTABLE
;
820 cl
->state
= JV_STATE_DONE
;
825 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
,
828 // First count arrays.
836 jclass result
= NULL
;
840 result
= JvPrimClass (byte
);
843 result
= JvPrimClass (short);
846 result
= JvPrimClass (int);
849 result
= JvPrimClass (long);
852 result
= JvPrimClass (boolean
);
855 result
= JvPrimClass (char);
858 result
= JvPrimClass (float);
861 result
= JvPrimClass (double);
864 result
= JvPrimClass (void);
869 while (*sig
&& *sig
!= ';')
871 // Do nothing if signature appears to be malformed.
874 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (save
, sig
- save
);
875 result
= _Jv_FindClass (name
, loader
);
880 // Do nothing -- bad signature.
886 // Not really the "end", but the last valid character that we
895 while (array_count
-- > 0)
896 result
= _Jv_GetArrayClass (result
, loader
);
902 _Jv_FindClassFromSignatureNoException (char *sig
, java::lang::ClassLoader
*loader
,
909 klass
= _Jv_FindClassFromSignature(sig
, loader
, endp
);
911 catch (java::lang::NoClassDefFoundError
*ncdfe
)
915 catch (java::lang::ClassNotFoundException
*cnfe
)
924 JvConvertArgv (int argc
, const char **argv
)
928 jobjectArray ar
= JvNewObjectArray(argc
, &java::lang::String::class$
, NULL
);
929 jobject
*ptr
= elements(ar
);
930 jbyteArray bytes
= NULL
;
931 for (int i
= 0; i
< argc
; i
++)
933 const char *arg
= argv
[i
];
934 int len
= strlen (arg
);
935 if (bytes
== NULL
|| bytes
->length
< len
)
936 bytes
= JvNewByteArray (len
);
937 jbyte
*bytePtr
= elements (bytes
);
938 // We assume jbyte == char.
939 memcpy (bytePtr
, arg
, len
);
941 // Now convert using the default encoding.
942 *ptr
++ = new java::lang::String (bytes
, 0, len
);
944 return (JArray
<jstring
>*) ar
;
947 // FIXME: These variables are static so that they will be
948 // automatically scanned by the Boehm collector. This is needed
949 // because with qthreads the collector won't scan the initial stack --
950 // it will only scan the qthreads stacks.
952 // Command line arguments.
953 static JArray
<jstring
> *arg_vec
;
955 // The primary thread.
956 static java::lang::Thread
*main_thread
;
958 #ifndef DISABLE_GETENV_PROPERTIES
961 next_property_key (char *s
, size_t *length
)
967 // Skip over whitespace
971 // If we've reached the end, return NULL. Also return NULL if for
972 // some reason we've come across a malformed property string.
978 // Determine the length of the property key.
996 next_property_value (char *s
, size_t *length
)
1002 while (isspace (*s
))
1009 while (isspace (*s
))
1012 // Determine the length of the property value.
1031 process_gcj_properties ()
1033 char *props
= getenv("GCJ_PROPERTIES");
1038 // Later on we will write \0s into this string. It is simplest to
1039 // just duplicate it here.
1040 props
= strdup (props
);
1044 size_t property_count
= 0;
1046 // Whip through props quickly in order to count the number of
1048 while (p
&& (p
= next_property_key (p
, &length
)))
1050 // Skip to the end of the key
1053 p
= next_property_value (p
, &length
);
1060 // Allocate an array of property value/key pairs.
1061 _Jv_Environment_Properties
=
1062 (property_pair
*) malloc (sizeof(property_pair
)
1063 * (property_count
+ 1));
1065 // Go through the properties again, initializing _Jv_Properties
1069 while (p
&& (p
= next_property_key (p
, &length
)))
1071 _Jv_Environment_Properties
[property_count
].key
= p
;
1072 _Jv_Environment_Properties
[property_count
].key_length
= length
;
1074 // Skip to the end of the key
1077 p
= next_property_value (p
, &length
);
1079 _Jv_Environment_Properties
[property_count
].value
= p
;
1080 _Jv_Environment_Properties
[property_count
].value_length
= length
;
1087 memset ((void *) &_Jv_Environment_Properties
[property_count
],
1088 0, sizeof (property_pair
));
1090 // Null terminate the strings.
1091 for (property_pair
*prop
= &_Jv_Environment_Properties
[0];
1095 prop
->key
[prop
->key_length
] = 0;
1096 prop
->value
[prop
->value_length
] = 0;
1099 #endif // DISABLE_GETENV_PROPERTIES
1103 _Jv_Utf8Const
*void_signature
;
1104 _Jv_Utf8Const
*clinit_name
;
1105 _Jv_Utf8Const
*init_name
;
1106 _Jv_Utf8Const
*finit_name
;
1108 bool runtimeInitialized
= false;
1110 // When true, print debugging information about class loading.
1111 bool verbose_class_flag
;
1113 // When true, enable the bytecode verifier and BC-ABI type verification.
1114 bool verifyClasses
= true;
1116 // Thread stack size specified by the -Xss runtime argument.
1117 size_t stack_size
= 0;
1120 // We accept all non-standard options accepted by Sun's java command,
1121 // for compatibility with existing application launch scripts.
1123 parse_x_arg (char* option_string
)
1125 if (strlen (option_string
) <= 0)
1128 if (! strcmp (option_string
, "int"))
1130 // FIXME: this should cause the vm to never load shared objects
1132 else if (! strcmp (option_string
, "mixed"))
1134 // FIXME: allow interpreted and native code
1136 else if (! strcmp (option_string
, "batch"))
1138 // FIXME: disable background JIT'ing
1140 else if (! strcmp (option_string
, "debug"))
1142 // FIXME: add JDWP/JVMDI support
1144 else if (! strncmp (option_string
, "bootclasspath:", 14))
1146 // FIXME: add a parse_bootclasspath_arg function
1148 else if (! strncmp (option_string
, "bootclasspath/a:", 16))
1151 else if (! strncmp (option_string
, "bootclasspath/p:", 16))
1154 else if (! strcmp (option_string
, "check:jni"))
1156 // FIXME: enable strict JNI checking
1158 else if (! strcmp (option_string
, "future"))
1160 // FIXME: enable strict class file format checks
1162 else if (! strcmp (option_string
, "noclassgc"))
1164 // FIXME: disable garbage collection for classes
1166 else if (! strcmp (option_string
, "incgc"))
1168 // FIXME: incremental garbage collection
1170 else if (! strncmp (option_string
, "loggc:", 6))
1172 if (option_string
[6] == '\0')
1175 "libgcj: filename argument expected for loggc option\n");
1178 // FIXME: set gc logging filename
1180 else if (! strncmp (option_string
, "ms", 2))
1182 // FIXME: ignore this option until PR 20699 is fixed.
1183 // _Jv_SetInitialHeapSize (option_string + 2);
1185 else if (! strncmp (option_string
, "mx", 2))
1186 _Jv_SetMaximumHeapSize (option_string
+ 2);
1187 else if (! strcmp (option_string
, "prof"))
1189 // FIXME: enable profiling of program running in vm
1191 else if (! strncmp (option_string
, "runhprof:", 9))
1193 // FIXME: enable specific type of vm profiling. add a
1194 // parse_runhprof_arg function
1196 else if (! strcmp (option_string
, "rs"))
1198 // FIXME: reduced system signal usage. disable thread dumps,
1199 // only terminate in response to user-initiated calls,
1200 // e.g. System.exit()
1202 else if (! strncmp (option_string
, "ss", 2))
1204 _Jv_SetStackSize (option_string
+ 2);
1206 else if (! strcmp (option_string
, "X:+UseAltSigs"))
1208 // FIXME: use signals other than SIGUSR1 and SIGUSR2
1210 else if (! strcmp (option_string
, "share:off"))
1212 // FIXME: don't share class data
1214 else if (! strcmp (option_string
, "share:auto"))
1216 // FIXME: share class data where possible
1218 else if (! strcmp (option_string
, "share:on"))
1220 // FIXME: fail if impossible to share class data
1227 parse_verbose_args (char* option_string
,
1228 bool ignore_unrecognized
)
1230 size_t len
= sizeof ("-verbose") - 1;
1232 if (strlen (option_string
) < len
)
1235 if (option_string
[len
] == ':'
1236 && option_string
[len
+ 1] != '\0')
1238 char* verbose_args
= option_string
+ len
+ 1;
1242 if (! strncmp (verbose_args
,
1243 "gc", sizeof ("gc") - 1))
1245 if (verbose_args
[sizeof ("gc") - 1] == '\0'
1246 || verbose_args
[sizeof ("gc") - 1] == ',')
1248 // FIXME: we should add functions to boehm-gc that
1249 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1250 // GC_print_back_height.
1251 verbose_args
+= sizeof ("gc") - 1;
1256 fprintf (stderr
, "libgcj: unknown verbose option: %s\n",
1261 else if (! strncmp (verbose_args
,
1263 sizeof ("class") - 1))
1265 if (verbose_args
[sizeof ("class") - 1] == '\0'
1266 || verbose_args
[sizeof ("class") - 1] == ',')
1268 gcj::verbose_class_flag
= true;
1269 verbose_args
+= sizeof ("class") - 1;
1272 goto verbose_arg_err
;
1274 else if (! strncmp (verbose_args
, "jni",
1275 sizeof ("jni") - 1))
1277 if (verbose_args
[sizeof ("jni") - 1] == '\0'
1278 || verbose_args
[sizeof ("jni") - 1] == ',')
1280 // FIXME: enable JNI messages.
1281 verbose_args
+= sizeof ("jni") - 1;
1284 goto verbose_arg_err
;
1286 else if (ignore_unrecognized
1287 && verbose_args
[0] == 'X')
1289 // ignore unrecognized non-standard verbose option
1290 while (verbose_args
[0] != '\0'
1291 && verbose_args
[0] != ',')
1294 else if (verbose_args
[0] == ',')
1299 goto verbose_arg_err
;
1301 if (verbose_args
[0] == ',')
1304 while (verbose_args
[0] != '\0');
1306 else if (option_string
[len
] == 'g'
1307 && option_string
[len
+ 1] == 'c'
1308 && option_string
[len
+ 2] == '\0')
1310 // FIXME: we should add functions to boehm-gc that
1311 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1312 // GC_print_back_height.
1315 else if (option_string
[len
] == '\0')
1317 gcj::verbose_class_flag
= true;
1322 // unrecognized option beginning with -verbose
1329 parse_init_args (JvVMInitArgs
* vm_args
)
1331 // if _Jv_Compiler_Properties is non-NULL then it needs to be
1332 // re-allocated dynamically.
1333 if (_Jv_Compiler_Properties
)
1335 const char** props
= _Jv_Compiler_Properties
;
1336 _Jv_Compiler_Properties
= NULL
;
1338 for (int i
= 0; props
[i
]; i
++)
1340 _Jv_Compiler_Properties
= (const char**) _Jv_Realloc
1341 (_Jv_Compiler_Properties
,
1342 (_Jv_Properties_Count
+ 1) * sizeof (const char*));
1343 _Jv_Compiler_Properties
[_Jv_Properties_Count
++] = props
[i
];
1347 if (vm_args
== NULL
)
1350 for (int i
= 0; i
< vm_args
->nOptions
; ++i
)
1352 char* option_string
= vm_args
->options
[i
].optionString
;
1353 if (! strcmp (option_string
, "vfprintf")
1354 || ! strcmp (option_string
, "exit")
1355 || ! strcmp (option_string
, "abort"))
1357 // FIXME: we are required to recognize these, but for
1358 // now we don't handle them in any way.
1361 else if (! strncmp (option_string
,
1362 "-verbose", sizeof ("-verbose") - 1))
1364 jint result
= parse_verbose_args (option_string
,
1365 vm_args
->ignoreUnrecognized
);
1369 else if (! strncmp (option_string
, "-D", 2))
1371 _Jv_Compiler_Properties
= (const char**) _Jv_Realloc
1372 (_Jv_Compiler_Properties
,
1373 (_Jv_Properties_Count
+ 1) * sizeof (char*));
1375 _Jv_Compiler_Properties
[_Jv_Properties_Count
++] =
1376 strdup (option_string
+ 2);
1380 else if (vm_args
->ignoreUnrecognized
)
1382 if (option_string
[0] == '_')
1383 parse_x_arg (option_string
+ 1);
1384 else if (! strncmp (option_string
, "-X", 2))
1385 parse_x_arg (option_string
+ 2);
1389 fprintf (stderr
, "libgcj: unknown option: %s\n", option_string
);
1394 goto unknown_option
;
1400 _Jv_CreateJavaVM (JvVMInitArgs
* vm_args
)
1402 using namespace gcj
;
1404 if (runtimeInitialized
)
1407 runtimeInitialized
= true;
1409 jint result
= parse_init_args (vm_args
);
1413 PROCESS_GCJ_PROPERTIES
;
1415 /* Threads must be initialized before the GC, so that it inherits the
1419 _Jv_InitializeSyncMutex ();
1422 _Jv_InitInterpreter ();
1433 /* Initialize Utf8 constants declared in jvm.h. */
1434 void_signature
= _Jv_makeUtf8Const ("()V", 3);
1435 clinit_name
= _Jv_makeUtf8Const ("<clinit>", 8);
1436 init_name
= _Jv_makeUtf8Const ("<init>", 6);
1437 finit_name
= _Jv_makeUtf8Const ("finit$", 6);
1439 /* Initialize built-in classes to represent primitive TYPEs. */
1440 _Jv_InitPrimClass (&_Jv_byteClass
, "byte", 'B', 1);
1441 _Jv_InitPrimClass (&_Jv_shortClass
, "short", 'S', 2);
1442 _Jv_InitPrimClass (&_Jv_intClass
, "int", 'I', 4);
1443 _Jv_InitPrimClass (&_Jv_longClass
, "long", 'J', 8);
1444 _Jv_InitPrimClass (&_Jv_booleanClass
, "boolean", 'Z', 1);
1445 _Jv_InitPrimClass (&_Jv_charClass
, "char", 'C', 2);
1446 _Jv_InitPrimClass (&_Jv_floatClass
, "float", 'F', 4);
1447 _Jv_InitPrimClass (&_Jv_doubleClass
, "double", 'D', 8);
1448 _Jv_InitPrimClass (&_Jv_voidClass
, "void", 'V', 0);
1450 // Turn stack trace generation off while creating exception objects.
1451 _Jv_InitClass (&java::lang::VMThrowable::class$
);
1452 java::lang::VMThrowable::trace_enabled
= 0;
1454 // We have to initialize this fairly early, to avoid circular class
1455 // initialization. In particular we want to start the
1456 // initialization of ClassLoader before we start the initialization
1457 // of VMClassLoader.
1458 _Jv_InitClass (&java::lang::ClassLoader::class$
);
1460 // Set up the system class loader and the bootstrap class loader.
1461 gnu::gcj::runtime::ExtensionClassLoader::initialize();
1462 java::lang::VMClassLoader::initialize(JvNewStringLatin1(TOOLEXECLIBDIR
));
1464 _Jv_RegisterBootstrapPackages();
1466 no_memory
= new java::lang::OutOfMemoryError
;
1468 java::lang::VMThrowable::trace_enabled
= 1;
1471 LTDL_SET_PRELOADED_SYMBOLS ();
1474 _Jv_platform_initialize ();
1478 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady
);
1480 // Start the GC finalizer thread. A VirtualMachineError can be
1481 // thrown by the runtime if, say, threads aren't available.
1484 using namespace gnu::gcj::runtime
;
1485 FinalizerThread
*ft
= new FinalizerThread ();
1488 catch (java::lang::VirtualMachineError
*ignore
)
1496 _Jv_RunMain (JvVMInitArgs
*vm_args
, jclass klass
, const char *name
, int argc
,
1497 const char **argv
, bool is_jar
)
1499 #ifndef DISABLE_MAIN_ARGS
1500 _Jv_SetArgs (argc
, argv
);
1503 java::lang::Runtime
*runtime
= NULL
;
1507 if (_Jv_CreateJavaVM (vm_args
) < 0)
1509 fprintf (stderr
, "libgcj: couldn't create virtual machine\n");
1513 // Get the Runtime here. We want to initialize it before searching
1514 // for `main'; that way it will be set up if `main' is a JNI method.
1515 runtime
= java::lang::Runtime::getRuntime ();
1517 #ifdef DISABLE_MAIN_ARGS
1518 arg_vec
= JvConvertArgv (0, 0);
1520 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
1523 using namespace gnu::java::lang
;
1525 main_thread
= new MainThread (klass
, arg_vec
);
1527 main_thread
= new MainThread (JvNewStringUTF (name
),
1530 catch (java::lang::Throwable
*t
)
1532 java::lang::System::err
->println (JvNewStringLatin1
1533 ("Exception during runtime initialization"));
1534 t
->printStackTrace();
1536 java::lang::Runtime::exitNoChecksAccessor (1);
1537 // In case the runtime creation failed.
1541 _Jv_AttachCurrentThread (main_thread
);
1542 _Jv_ThreadRun (main_thread
);
1544 // If we got here then something went wrong, as MainThread is not
1545 // supposed to terminate.
1550 _Jv_RunMain (jclass klass
, const char *name
, int argc
, const char **argv
,
1553 _Jv_RunMain (NULL
, klass
, name
, argc
, argv
, is_jar
);
1557 JvRunMain (jclass klass
, int argc
, const char **argv
)
1559 _Jv_RunMain (klass
, NULL
, argc
, argv
, false);
1564 // Parse a string and return a heap size.
1566 parse_memory_size (const char *spec
)
1569 unsigned long val
= strtoul (spec
, &end
, 10);
1570 if (*end
== 'k' || *end
== 'K')
1572 else if (*end
== 'm' || *end
== 'M')
1574 return (size_t) val
;
1577 // Set the initial heap size. This might be ignored by the GC layer.
1578 // This must be called before _Jv_RunMain.
1580 _Jv_SetInitialHeapSize (const char *arg
)
1582 size_t size
= parse_memory_size (arg
);
1583 _Jv_GCSetInitialHeapSize (size
);
1586 // Set the maximum heap size. This might be ignored by the GC layer.
1587 // This must be called before _Jv_RunMain.
1589 _Jv_SetMaximumHeapSize (const char *arg
)
1591 size_t size
= parse_memory_size (arg
);
1592 _Jv_GCSetMaximumHeapSize (size
);
1596 _Jv_SetStackSize (const char *arg
)
1598 size_t size
= parse_memory_size (arg
);
1599 gcj::stack_size
= size
;
1603 _Jv_Malloc (jsize size
)
1605 if (__builtin_expect (size
== 0, false))
1607 void *ptr
= malloc ((size_t) size
);
1608 if (__builtin_expect (ptr
== NULL
, false))
1614 _Jv_Realloc (void *ptr
, jsize size
)
1616 if (__builtin_expect (size
== 0, false))
1618 ptr
= realloc (ptr
, (size_t) size
);
1619 if (__builtin_expect (ptr
== NULL
, false))
1625 _Jv_MallocUnchecked (jsize size
)
1627 if (__builtin_expect (size
== 0, false))
1629 return malloc ((size_t) size
);
1633 _Jv_Free (void* ptr
)
1640 // In theory, these routines can be #ifdef'd away on machines which
1641 // support divide overflow signals. However, we never know if some
1642 // code might have been compiled with "-fuse-divide-subroutine", so we
1643 // always include them in libgcj.
1646 _Jv_divI (jint dividend
, jint divisor
)
1648 if (__builtin_expect (divisor
== 0, false))
1650 java::lang::ArithmeticException
*arithexception
1651 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1652 throw arithexception
;
1655 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1658 return dividend
/ divisor
;
1662 _Jv_remI (jint dividend
, jint divisor
)
1664 if (__builtin_expect (divisor
== 0, false))
1666 java::lang::ArithmeticException
*arithexception
1667 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1668 throw arithexception
;
1671 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1674 return dividend
% divisor
;
1678 _Jv_divJ (jlong dividend
, jlong divisor
)
1680 if (__builtin_expect (divisor
== 0, false))
1682 java::lang::ArithmeticException
*arithexception
1683 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1684 throw arithexception
;
1687 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1690 return dividend
/ divisor
;
1694 _Jv_remJ (jlong dividend
, jlong divisor
)
1696 if (__builtin_expect (divisor
== 0, false))
1698 java::lang::ArithmeticException
*arithexception
1699 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1700 throw arithexception
;
1703 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1706 return dividend
% divisor
;
1711 // Return true if SELF_KLASS can access a field or method in
1712 // OTHER_KLASS. The field or method's access flags are specified in
1715 _Jv_CheckAccess (jclass self_klass
, jclass other_klass
, jint flags
)
1717 using namespace java::lang::reflect
;
1718 return ((self_klass
== other_klass
)
1719 || ((flags
& Modifier::PUBLIC
) != 0)
1720 || (((flags
& Modifier::PROTECTED
) != 0)
1721 && _Jv_IsAssignableFromSlow (self_klass
, other_klass
))
1722 || (((flags
& Modifier::PRIVATE
) == 0)
1723 && _Jv_ClassNameSamePackage (self_klass
->name
,
1724 other_klass
->name
)));
1727 // Prepend GCJ_VERSIONED_LIBDIR to a module search path stored in a C
1728 // char array, if the path is not already prefixed by
1729 // GCJ_VERSIONED_LIBDIR. Return a newly JvMalloc'd char buffer. The
1730 // result should be freed using JvFree.
1732 _Jv_PrependVersionedLibdir (char* libpath
)
1736 if (libpath
&& libpath
[0] != '\0')
1738 if (! strncmp (libpath
,
1739 GCJ_VERSIONED_LIBDIR
,
1740 sizeof (GCJ_VERSIONED_LIBDIR
) - 1))
1742 // LD_LIBRARY_PATH is already prefixed with
1743 // GCJ_VERSIONED_LIBDIR.
1744 retval
= (char*) _Jv_Malloc (strlen (libpath
) + 1);
1745 strcpy (retval
, libpath
);
1749 // LD_LIBRARY_PATH is not prefixed with
1750 // GCJ_VERSIONED_LIBDIR.
1751 jsize total
= (sizeof (GCJ_VERSIONED_LIBDIR
) - 1)
1752 + (sizeof (PATH_SEPARATOR
) - 1) + strlen (libpath
) + 1;
1753 retval
= (char*) _Jv_Malloc (total
);
1754 strcpy (retval
, GCJ_VERSIONED_LIBDIR
);
1755 strcat (retval
, PATH_SEPARATOR
);
1756 strcat (retval
, libpath
);
1761 // LD_LIBRARY_PATH was not specified or is empty.
1762 retval
= (char*) _Jv_Malloc (sizeof (GCJ_VERSIONED_LIBDIR
));
1763 strcpy (retval
, GCJ_VERSIONED_LIBDIR
);