- codeattrnum = suck_u2(cb);
-
- for (; codeattrnum > 0; codeattrnum--) {
- utf *caname;
-
- if (!check_classbuffer_size(cb, 2))
- return false;
-
- caname = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
-
- if (caname == utf_linenumbertable) {
- u2 lncid;
-
- if (!check_classbuffer_size(cb, 4 + 2))
- return false;
-
- suck_u4(cb);
- m->linenumbercount = suck_u2(cb);
-
- if (!check_classbuffer_size(cb,
- (2 + 2) * m->linenumbercount))
- return false;
-
- m->linenumbers = MNEW(lineinfo, m->linenumbercount);
-
- for (lncid = 0; lncid < m->linenumbercount; lncid++) {
- m->linenumbers[lncid].start_pc = suck_u2(cb);
- m->linenumbers[lncid].line_number = suck_u2(cb);
- }
- codeattrnum--;
-
- if (!skipattributes(cb, codeattrnum))
- return false;
-
- break;
- } else {
- if (!skipattributebody(cb))
- return false;
- }
- }
- }
- }
-
- if (!m->jcode && !(m->flags & (ACC_ABSTRACT | ACC_NATIVE))) {
- *exceptionptr = new_classformaterror(c, "Missing Code attribute");
-
- return false;
- }
-
- /* everything was ok */
-
- return true;
-}
-
-
-/********************* Function: method_free ***********************************
-
- frees all memory that was allocated for this method
-
-*******************************************************************************/
-
-static void method_free(methodinfo *m)
-{
- if (m->jcode)
- MFREE(m->jcode, u1, m->jcodelength);
-
- if (m->exceptiontable)
- MFREE(m->exceptiontable, exceptiontable, m->exceptiontablelength);
-
- if (m->mcode)
- CFREE(m->mcode, m->mcodelength);
-
- if (m->stubroutine) {
- if (m->flags & ACC_NATIVE) {
- removenativestub(m->stubroutine);
-
- } else {
- removecompilerstub(m->stubroutine);
- }
- }
-}
-
-
-/************** Function: method_display (debugging only) **************/
-
-void method_display(methodinfo *m)
-{
- printf(" ");
- printflags(m->flags);
- printf(" ");
- utf_display(m->name);
- printf(" ");
- utf_display(m->descriptor);
- printf("\n");
-}
-
-/************** Function: method_display_flags_last (debugging only) **************/
-
-void method_display_flags_last(methodinfo *m)
-{
- printf(" ");
- utf_display(m->name);
- printf(" ");
- utf_display(m->descriptor);
- printf(" ");
- printflags(m->flags);
- printf("\n");
-}
-
-
-/******************** Function: method_canoverwrite ****************************
-
- Check if m and old are identical with respect to type and name. This means
- that old can be overwritten with m.
-
-*******************************************************************************/
-
-static bool method_canoverwrite(methodinfo *m, methodinfo *old)
-{
- if (m->name != old->name) return false;
- if (m->descriptor != old->descriptor) return false;
- if (m->flags & ACC_STATIC) return false;
- return true;
-}
-
-
-/******************************************************************************/
-/************************ Functions for class *********************************/
-/******************************************************************************/
-
-
-/******************** function:: class_getconstant *****************************
-
- retrieves the value at position 'pos' of the constantpool of a class
- if the type of the value is other than 'ctype' the system is stopped
-
-*******************************************************************************/
-
-voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
-{
- /* invalid position in constantpool */
- /* (pos == 0 is caught by type comparison) */
- if (pos >= c->cpcount)
- panic("Attempt to access constant outside range");
-
- /* check type of constantpool entry */
-
- if (c->cptags[pos] != ctype) {
- class_showconstantpool(c);
- error("Type mismatch on constant: %d requested, %d here (class_getconstant)",
- (int) ctype, (int) c->cptags[pos]);
- }
-
- return c->cpinfos[pos];
-}
-
-
-/********************* Function: class_constanttype ****************************
-
- Determines the type of a class entry in the ConstantPool
-
-*******************************************************************************/
-
-u4 class_constanttype(classinfo *c, u4 pos)
-{
- if (pos >= c->cpcount)
- panic("Attempt to access constant outside range");
-
- return c->cptags[pos];
-}
-
-
-/******************** function: class_loadcpool ********************************
-
- loads the constantpool of a class,
- the entries are transformed into a simpler format
- by resolving references
- (a detailed overview of the compact structures can be found in global.h)
-
-*******************************************************************************/
-
-static bool class_loadcpool(classbuffer *cb, classinfo *c)
-{
-
- /* The following structures are used to save information which cannot be
- processed during the first pass. After the complete constantpool has
- been traversed the references can be resolved.
- (only in specific order) */
-
- /* CONSTANT_Class_info entries */
- typedef struct forward_class {
- struct forward_class *next;
- u2 thisindex;
- u2 name_index;
- } forward_class;
-
- /* CONSTANT_String */
- typedef struct forward_string {
- struct forward_string *next;
- u2 thisindex;
- u2 string_index;
- } forward_string;
-
- /* CONSTANT_NameAndType */
- typedef struct forward_nameandtype {
- struct forward_nameandtype *next;
- u2 thisindex;
- u2 name_index;
- u2 sig_index;
- } forward_nameandtype;
-
- /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
- typedef struct forward_fieldmethint {
- struct forward_fieldmethint *next;
- u2 thisindex;
- u1 tag;
- u2 class_index;
- u2 nameandtype_index;
- } forward_fieldmethint;
-
-
- u4 idx;
- long int dumpsize = dump_size ();
-
- forward_class *forward_classes = NULL;
- forward_string *forward_strings = NULL;
- forward_nameandtype *forward_nameandtypes = NULL;
- forward_fieldmethint *forward_fieldmethints = NULL;
-
- u4 cpcount;
- u1 *cptags;
- voidptr *cpinfos;
-
- /* number of entries in the constant_pool table plus one */
- if (!check_classbuffer_size(cb, 2))
- return false;
-
- cpcount = c->cpcount = suck_u2(cb);
-
- /* allocate memory */
- cptags = c->cptags = MNEW(u1, cpcount);
- cpinfos = c->cpinfos = MNEW(voidptr, cpcount);
-
- if (!cpcount)
- panic("Invalid constant_pool_count (0)");
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_const_pool_len += (sizeof(voidptr) + 1) * cpcount;
-#endif
-
- /* initialize constantpool */
- for (idx = 0; idx < cpcount; idx++) {
- cptags[idx] = CONSTANT_UNUSED;
- cpinfos[idx] = NULL;
- }
-
-
- /******* first pass *******/
- /* entries which cannot be resolved now are written into
- temporary structures and traversed again later */
-
- idx = 1;
- while (idx < cpcount) {
- u4 t;
-
- /* get constant type */
- if (!check_classbuffer_size(cb, 1))
- return false;
-
- t = suck_u1(cb);
-
- switch (t) {
- case CONSTANT_Class: {
- forward_class *nfc = DNEW(forward_class);
-
- nfc->next = forward_classes;
- forward_classes = nfc;
-
- nfc->thisindex = idx;
- /* reference to CONSTANT_NameAndType */
- if (!check_classbuffer_size(cb, 2))
- return false;
-
- nfc->name_index = suck_u2(cb);
-
- idx++;
- break;
- }
-
- case CONSTANT_Fieldref:
- case CONSTANT_Methodref:
- case CONSTANT_InterfaceMethodref: {
- forward_fieldmethint *nff = DNEW(forward_fieldmethint);
-
- nff->next = forward_fieldmethints;
- forward_fieldmethints = nff;
-
- nff->thisindex = idx;
- /* constant type */
- nff->tag = t;
-
- if (!check_classbuffer_size(cb, 2 + 2))
- return false;
-
- /* class or interface type that contains the declaration of the
- field or method */
- nff->class_index = suck_u2(cb);
-
- /* name and descriptor of the field or method */
- nff->nameandtype_index = suck_u2(cb);
-
- idx++;
- break;
- }
-
- case CONSTANT_String: {
- forward_string *nfs = DNEW(forward_string);
-
- nfs->next = forward_strings;
- forward_strings = nfs;
-
- nfs->thisindex = idx;
-
- /* reference to CONSTANT_Utf8_info with string characters */
- if (!check_classbuffer_size(cb, 2))
- return false;
-
- nfs->string_index = suck_u2(cb);
-
- idx++;
- break;
- }
-
- case CONSTANT_NameAndType: {
- forward_nameandtype *nfn = DNEW(forward_nameandtype);
-
- nfn->next = forward_nameandtypes;
- forward_nameandtypes = nfn;
-
- nfn->thisindex = idx;
-
- if (!check_classbuffer_size(cb, 2 + 2))
- return false;
-
- /* reference to CONSTANT_Utf8_info containing simple name */
- nfn->name_index = suck_u2(cb);
-
- /* reference to CONSTANT_Utf8_info containing field or method
- descriptor */
- nfn->sig_index = suck_u2(cb);
-
- idx++;
- break;
- }
-
- case CONSTANT_Integer: {
- constant_integer *ci = NEW(constant_integer);
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_const_pool_len += sizeof(constant_integer);
-#endif
-
- if (!check_classbuffer_size(cb, 4))
- return false;
-
- ci->value = suck_s4(cb);
- cptags[idx] = CONSTANT_Integer;
- cpinfos[idx] = ci;
-
- idx++;
- break;
- }
-
- case CONSTANT_Float: {
- constant_float *cf = NEW(constant_float);
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_const_pool_len += sizeof(constant_float);
-#endif
-
- if (!check_classbuffer_size(cb, 4))
- return false;
-
- cf->value = suck_float(cb);
- cptags[idx] = CONSTANT_Float;
- cpinfos[idx] = cf;
-
- idx++;
- break;
- }
-
- case CONSTANT_Long: {
- constant_long *cl = NEW(constant_long);
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_const_pool_len += sizeof(constant_long);
-#endif
-
- if (!check_classbuffer_size(cb, 8))
- return false;
-
- cl->value = suck_s8(cb);
- cptags[idx] = CONSTANT_Long;
- cpinfos[idx] = cl;
- idx += 2;
- if (idx > cpcount)
- panic("Long constant exceeds constant pool");
- break;
- }
-
- case CONSTANT_Double: {
- constant_double *cd = NEW(constant_double);
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_const_pool_len += sizeof(constant_double);
-#endif
-
- if (!check_classbuffer_size(cb, 8))
- return false;
-
- cd->value = suck_double(cb);
- cptags[idx] = CONSTANT_Double;
- cpinfos[idx] = cd;
- idx += 2;
- if (idx > cpcount)
- panic("Double constant exceeds constant pool");
- break;
- }
-
- case CONSTANT_Utf8: {
- u4 length;
-
- /* number of bytes in the bytes array (not string-length) */
- if (!check_classbuffer_size(cb, 2))
- return false;
-
- length = suck_u2(cb);
- cptags[idx] = CONSTANT_Utf8;
-
- /* validate the string */
- if (!check_classbuffer_size(cb, length))
- return false;
-
- if (opt_verify &&
- !is_valid_utf(cb->pos + 1, cb->pos + 1 + length)) {
- dolog("Invalid UTF-8 string (constant pool index %d)",idx);
- panic("Invalid UTF-8 string");
- }
- /* insert utf-string into the utf-symboltable */
- cpinfos[idx] = utf_new_intern(cb->pos + 1, length);
-
- /* skip bytes of the string (buffer size check above) */
- skip_nbytes(cb, length);
- idx++;
- break;
- }
-
- default:
- error("Unkown constant type: %d",(int) t);
- } /* end switch */
- } /* end while */
-
-
- /* resolve entries in temporary structures */
-
- while (forward_classes) {
- utf *name =
- class_getconstant(c, forward_classes->name_index, CONSTANT_Utf8);
-
- if (opt_verify && !is_valid_name_utf(name))
- panic("Class reference with invalid name");
-
- cptags[forward_classes->thisindex] = CONSTANT_Class;
- /* retrieve class from class-table */
- if (opt_eager) {
- classinfo *tc;
- tc = class_new_intern(name);
-
- if (!class_load(tc))
- return false;
-
- /* link the class later, so we cannot link the currently loaded
- class */
- list_addfirst(&unlinkedclasses, tc);
-
- cpinfos[forward_classes->thisindex] = tc;
-
- } else {
- cpinfos[forward_classes->thisindex] = class_new(name);
- }
-
- forward_classes = forward_classes->next;
- }
-
- while (forward_strings) {
- utf *text =
- class_getconstant(c, forward_strings->string_index, CONSTANT_Utf8);
-
- /* resolve utf-string */
- cptags[forward_strings->thisindex] = CONSTANT_String;
- cpinfos[forward_strings->thisindex] = text;
-
- forward_strings = forward_strings->next;
- }
-
- while (forward_nameandtypes) {
- constant_nameandtype *cn = NEW(constant_nameandtype);
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_const_pool_len += sizeof(constant_nameandtype);
-#endif
-
- /* resolve simple name and descriptor */
- cn->name = class_getconstant(c,
- forward_nameandtypes->name_index,
- CONSTANT_Utf8);
-
- cn->descriptor = class_getconstant(c,
- forward_nameandtypes->sig_index,
- CONSTANT_Utf8);
-
- if (opt_verify) {
- /* check name */
- if (!is_valid_name_utf(cn->name))
- panic("NameAndType with invalid name");
- /* disallow referencing <clinit> among others */
- if (cn->name->text[0] == '<' && cn->name != utf_init)
- panic("NameAndType with invalid special name");
- }
-
- cptags[forward_nameandtypes->thisindex] = CONSTANT_NameAndType;
- cpinfos[forward_nameandtypes->thisindex] = cn;
-
- forward_nameandtypes = forward_nameandtypes->next;
- }
-
- while (forward_fieldmethints) {
- constant_nameandtype *nat;
- constant_FMIref *fmi = NEW(constant_FMIref);
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_const_pool_len += sizeof(constant_FMIref);
-#endif
- /* resolve simple name and descriptor */
- nat = class_getconstant(c,
- forward_fieldmethints->nameandtype_index,
- CONSTANT_NameAndType);
-
- fmi->class = class_getconstant(c,
- forward_fieldmethints->class_index,
- CONSTANT_Class);
- fmi->name = nat->name;
- fmi->descriptor = nat->descriptor;
-
- cptags[forward_fieldmethints->thisindex] = forward_fieldmethints->tag;
- cpinfos[forward_fieldmethints->thisindex] = fmi;
-
- switch (forward_fieldmethints->tag) {
- case CONSTANT_Fieldref: /* check validity of descriptor */
- checkfielddescriptor(fmi->descriptor->text,
- utf_end(fmi->descriptor));
- break;
- case CONSTANT_InterfaceMethodref:
- case CONSTANT_Methodref: /* check validity of descriptor */
- checkmethoddescriptor(c, fmi->descriptor);
- break;
- }
-
- forward_fieldmethints = forward_fieldmethints->next;
- }
-
- dump_release(dumpsize);
-
- /* everything was ok */
-
- return true;
-}
-
-
-/********************** Function: class_load ***********************************
-
- Loads everything interesting about a class from the class file. The
- 'classinfo' structure must have been allocated previously.
-
- The super class and the interfaces implemented by this class need not be
- loaded. The link is set later by the function 'class_link'.
-
- The loaded class is removed from the list 'unloadedclasses' and added to
- the list 'unlinkedclasses'.
-
-*******************************************************************************/
-
-classinfo *class_load_intern(classbuffer *cb);
-
-classinfo *class_load(classinfo *c)
-{
- classbuffer *cb;
- classinfo *r;
- s8 starttime;
- s8 stoptime;
-
- /* enter a monitor on the class */
-
- builtin_monitorenter((java_objectheader *) c);
-
- /* maybe the class is already loaded */
- if (c->loaded) {
- builtin_monitorexit((java_objectheader *) c);
-
- return c;
- }
-
- /* measure time */
- if (getloadingtime)
- starttime = getcputime();
-
- /* load classdata, throw exception on error */
-
- if ((cb = suck_start(c)) == NULL) {
- /* this means, the classpath was not set properly */
- if (c->name == utf_java_lang_Object)
- throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
- "java/lang/Object");
-
- *exceptionptr =
- new_exception_utfmessage(string_java_lang_NoClassDefFoundError,
- c->name);
-
- builtin_monitorexit((java_objectheader *) c);
-
- return NULL;
- }
-
- /* call the internal function */
- r = class_load_intern(cb);
-
- /* if return value is NULL, we had a problem and the class is not loaded */
- if (!r) {
- c->loaded = false;
-
- /* now free the allocated memory, otherwise we could ran into a DOS */
- class_remove(c);
- }
-
- /* free memory */
- suck_stop(cb);
-
- /* measure time */
- if (getloadingtime) {
- stoptime = getcputime();
- loadingtime += (stoptime - starttime);
- }
-
- /* leave the monitor */
-
- builtin_monitorexit((java_objectheader *) c);
-
- return r;
-}
-
-
-classinfo *class_load_intern(classbuffer *cb)
-{
- classinfo *c;
- u4 i;
- u4 mi, ma;
-/* s4 classdata_left; */
- char msg[MAXLOGTEXT]; /* maybe we get an exception */
-
- /* get the classbuffer's class */
- c = cb->class;
-
- /* maybe the class is already loaded */
- if (c->loaded)
- return c;
-
-#if defined(STATISTICS)
- if (opt_stat)
- count_class_loads++;
-#endif
-
- /* output for debugging purposes */
- if (loadverbose)
- log_message_class("Loading class: ", c);
-
- /* class is somewhat loaded */
- c->loaded = true;
-
- if (!check_classbuffer_size(cb, 4 + 2 + 2))
- return NULL;
-
- /* check signature */
- if (suck_u4(cb) != MAGIC) {
- *exceptionptr = new_classformaterror(c, "Bad magic number");
-
- return NULL;
- }
-
- /* check version */
- mi = suck_u2(cb);
- ma = suck_u2(cb);
-
- if (!(ma < MAJOR_VERSION || (ma == MAJOR_VERSION && mi <= MINOR_VERSION))) {
- *exceptionptr =
- new_classformaterror(c,
- "Unsupported major.minor version %d.%d",
- ma, mi);
-
- return NULL;
- }
-
- if (!class_loadcpool(cb, c))
- return NULL;
-
- /*JOWENN*/
- c->erroneous_state = 0;
- c->initializing_thread = 0;
- /*JOWENN*/
- c->classUsed = NOTUSED; /* not used initially CO-RT */
- c->impldBy = NULL;
-
- /* ACC flags */
- if (!check_classbuffer_size(cb, 2))
- return NULL;
-
- c->flags = suck_u2(cb);
- /*if (!(c->flags & ACC_PUBLIC)) { log_text("CLASS NOT PUBLIC"); } JOWENN*/
-
- /* check ACC flags consistency */
- if (c->flags & ACC_INTERFACE) {
- if (!(c->flags & ACC_ABSTRACT)) {
- /* We work around this because interfaces in JDK 1.1 are
- * not declared abstract. */
-
- c->flags |= ACC_ABSTRACT;
- /* panic("Interface class not declared abstract"); */
- }
-
- if (c->flags & ACC_FINAL) {
- *exceptionptr =
- new_classformaterror(c,
- "Illegal class modifiers: 0x%x", c->flags);
-
- return NULL;
- }
-
- if (c->flags & ACC_SUPER) {
- c->flags &= ~ACC_SUPER; /* kjc seems to set this on interfaces */
- }
- }
-
- if ((c->flags & (ACC_ABSTRACT | ACC_FINAL)) == (ACC_ABSTRACT | ACC_FINAL)) {
- *exceptionptr =
- new_classformaterror(c, "Illegal class modifiers: 0x%x", c->flags);
-
- return NULL;
- }
-
- if (!check_classbuffer_size(cb, 2 + 2))
- return NULL;
-
- /* this class */
- i = suck_u2(cb);
- if (class_getconstant(c, i, CONSTANT_Class) != c) {
- utf_sprint(msg, c->name);
- sprintf(msg + strlen(msg), " (wrong name: ");
- utf_sprint(msg + strlen(msg),
- ((classinfo *) class_getconstant(c, i, CONSTANT_Class))->name);
- sprintf(msg + strlen(msg), ")");
-
- *exceptionptr =
- new_exception_message(string_java_lang_NoClassDefFoundError, msg);
-
- return NULL;
- }
-
- /* retrieve superclass */
- if ((i = suck_u2(cb))) {
- c->super = class_getconstant(c, i, CONSTANT_Class);
-
- /* java.lang.Object may not have a super class. */
- if (c->name == utf_java_lang_Object) {
- *exceptionptr =
- new_exception_message(string_java_lang_ClassFormatError,
- "java.lang.Object with superclass");
-
- return NULL;
- }
-
- /* Interfaces must have java.lang.Object as super class. */
- if ((c->flags & ACC_INTERFACE) &&
- c->super->name != utf_java_lang_Object) {
- *exceptionptr =
- new_exception_message(string_java_lang_ClassFormatError,
- "Interfaces must have java.lang.Object as superclass");
-
- return NULL;
- }
-
- } else {
- c->super = NULL;
-
- /* This is only allowed for java.lang.Object. */
- if (c->name != utf_java_lang_Object) {
- *exceptionptr = new_classformaterror(c, "Bad superclass index");
-
- return NULL;
- }
-
- }
-
- /* retrieve interfaces */
- if (!check_classbuffer_size(cb, 2))
- return NULL;
-
- c->interfacescount = suck_u2(cb);
-
- if (!check_classbuffer_size(cb, 2 * c->interfacescount))
- return NULL;
-
- c->interfaces = MNEW(classinfo*, c->interfacescount);
- for (i = 0; i < c->interfacescount; i++) {
- c->interfaces[i] = class_getconstant(c, suck_u2(cb), CONSTANT_Class);
- }
-
- /* load fields */
- if (!check_classbuffer_size(cb, 2))
- return NULL;
-
- c->fieldscount = suck_u2(cb);
- c->fields = GCNEW(fieldinfo, c->fieldscount);
-/* c->fields = MNEW(fieldinfo, c->fieldscount); */
- for (i = 0; i < c->fieldscount; i++) {
- if (!field_load(cb, c, &(c->fields[i])))
- return NULL;
- }
-
- /* load methods */
- if (!check_classbuffer_size(cb, 2))
- return NULL;
-
- c->methodscount = suck_u2(cb);
- c->methods = GCNEW(methodinfo, c->methodscount);
-/* c->methods = MNEW(methodinfo, c->methodscount); */
- for (i = 0; i < c->methodscount; i++) {
- if (!method_load(cb, c, &(c->methods[i])))
- return NULL;
- }
-
- /* Check if all fields and methods can be uniquely
- * identified by (name,descriptor). */
- if (opt_verify) {
- /* We use a hash table here to avoid making the
- * average case quadratic in # of methods, fields.
- */
- static int shift = 0;
- u2 *hashtab;
- u2 *next; /* for chaining colliding hash entries */
- size_t len;
- size_t hashlen;
- u2 index;
- u2 old;
-
- /* Allocate hashtable */
- len = c->methodscount;
- if (len < c->fieldscount) len = c->fieldscount;
- hashlen = 5 * len;
- hashtab = MNEW(u2,(hashlen + len));
- next = hashtab + hashlen;
-
- /* Determine bitshift (to get good hash values) */
- if (!shift) {
- len = sizeof(utf);
- while (len) {
- len >>= 1;
- shift++;