| 1 | // defineclass.cc - defining a class from .class format.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 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 | /*
|
|---|
| 12 | Author: Kresten Krab Thorup <[email protected]>
|
|---|
| 13 |
|
|---|
| 14 | Written using the online versions of Java Language Specification (1st
|
|---|
| 15 | ed.) and The Java Virtual Machine Specification (2nd ed.).
|
|---|
| 16 |
|
|---|
| 17 | Future work may include reading (and handling) attributes which are
|
|---|
| 18 | currently being ignored ("InnerClasses", "LineNumber", etc...).
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include <config.h>
|
|---|
| 22 |
|
|---|
| 23 | #include <java-interp.h>
|
|---|
| 24 |
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <java-cpool.h>
|
|---|
| 27 | #include <gcj/cni.h>
|
|---|
| 28 |
|
|---|
| 29 | #include <java/lang/Class.h>
|
|---|
| 30 | #include <java/lang/Float.h>
|
|---|
| 31 | #include <java/lang/Double.h>
|
|---|
| 32 | #include <java/lang/Character.h>
|
|---|
| 33 | #include <java/lang/LinkageError.h>
|
|---|
| 34 | #include <java/lang/InternalError.h>
|
|---|
| 35 | #include <java/lang/ClassFormatError.h>
|
|---|
| 36 | #include <java/lang/NoClassDefFoundError.h>
|
|---|
| 37 | #include <java/lang/ClassCircularityError.h>
|
|---|
| 38 | #include <java/lang/ClassNotFoundException.h>
|
|---|
| 39 | #include <java/lang/IncompatibleClassChangeError.h>
|
|---|
| 40 | #include <java/lang/reflect/Modifier.h>
|
|---|
| 41 |
|
|---|
| 42 | using namespace gcj;
|
|---|
| 43 |
|
|---|
| 44 | #ifdef INTERPRETER
|
|---|
| 45 |
|
|---|
| 46 | // these go in some separate functions, to avoid having _Jv_InitClass
|
|---|
| 47 | // inserted all over the place.
|
|---|
| 48 | static void throw_internal_error (char *msg)
|
|---|
| 49 | __attribute__ ((__noreturn__));
|
|---|
| 50 | static void throw_no_class_def_found_error (jstring msg)
|
|---|
| 51 | __attribute__ ((__noreturn__));
|
|---|
| 52 | static void throw_no_class_def_found_error (char *msg)
|
|---|
| 53 | __attribute__ ((__noreturn__));
|
|---|
| 54 | static void throw_class_format_error (jstring msg)
|
|---|
| 55 | __attribute__ ((__noreturn__));
|
|---|
| 56 | static void throw_incompatible_class_change_error (jstring msg)
|
|---|
| 57 | __attribute__ ((__noreturn__));
|
|---|
| 58 | static void throw_class_circularity_error (jstring msg)
|
|---|
| 59 | __attribute__ ((__noreturn__));
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * We define class reading using a class. It is practical, since then
|
|---|
| 63 | * the entire class-reader can be a friend of class Class (it needs to
|
|---|
| 64 | * write all it's different structures); but also because this makes it
|
|---|
| 65 | * easy to make class definition reentrant, and thus two threads can be
|
|---|
| 66 | * defining classes at the same time. This class (_Jv_ClassReader) is
|
|---|
| 67 | * never exposed outside this file, so we don't have to worry about
|
|---|
| 68 | * public or private members here.
|
|---|
| 69 | */
|
|---|
| 70 |
|
|---|
| 71 | struct _Jv_ClassReader {
|
|---|
| 72 |
|
|---|
| 73 | // do verification? Currently, there is no option to disable this.
|
|---|
| 74 | // This flag just controls the verificaiton done by the class loader;
|
|---|
| 75 | // i.e., checking the integrity of the constant pool; and it is
|
|---|
| 76 | // allways on. You always want this as far as I can see, but it also
|
|---|
| 77 | // controls weither identifiers and type descriptors/signatures are
|
|---|
| 78 | // verified as legal. This could be somewhat more expensive since it
|
|---|
| 79 | // will call Characher.isJavaIdentifier{Start,Part} for each character
|
|---|
| 80 | // in any identifier (field name or method name) it comes by. Thus,
|
|---|
| 81 | // it might be useful to turn off this verification for classes that
|
|---|
| 82 | // come from a trusted source. However, for GCJ, trusted classes are
|
|---|
| 83 | // most likely to be linked in.
|
|---|
| 84 |
|
|---|
| 85 | bool verify;
|
|---|
| 86 |
|
|---|
| 87 | // input data.
|
|---|
| 88 | unsigned char *bytes;
|
|---|
| 89 | int len;
|
|---|
| 90 |
|
|---|
| 91 | // current input position
|
|---|
| 92 | int pos;
|
|---|
| 93 |
|
|---|
| 94 | // the constant pool data
|
|---|
| 95 | int pool_count;
|
|---|
| 96 | unsigned char *tags;
|
|---|
| 97 | unsigned int *offsets;
|
|---|
| 98 |
|
|---|
| 99 | // the class to define (see java-interp.h)
|
|---|
| 100 | _Jv_InterpClass *def;
|
|---|
| 101 |
|
|---|
| 102 | /* check that the given number of input bytes are available */
|
|---|
| 103 | inline void check (int num)
|
|---|
| 104 | {
|
|---|
| 105 | if (pos + num > len)
|
|---|
| 106 | throw_class_format_error ("Premature end of data");
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /* skip a given number of bytes in input */
|
|---|
| 110 | inline void skip (int num)
|
|---|
| 111 | {
|
|---|
| 112 | check (num);
|
|---|
| 113 | pos += num;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /* read an unsignend 1-byte unit */
|
|---|
| 117 | inline static jint get1u (unsigned char* bytes)
|
|---|
| 118 | {
|
|---|
| 119 | return bytes[0];
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /* read an unsigned 1-byte unit */
|
|---|
| 123 | inline jint read1u ()
|
|---|
| 124 | {
|
|---|
| 125 | skip (1);
|
|---|
| 126 | return get1u (bytes+pos-1);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /* read an unsigned 2-byte unit */
|
|---|
| 130 | inline static jint get2u (unsigned char *bytes)
|
|---|
| 131 | {
|
|---|
| 132 | return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* read an unsigned 2-byte unit */
|
|---|
| 136 | inline jint read2u ()
|
|---|
| 137 | {
|
|---|
| 138 | skip (2);
|
|---|
| 139 | return get2u (bytes+pos-2);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /* read a 4-byte unit */
|
|---|
| 143 | static jint get4 (unsigned char *bytes)
|
|---|
| 144 | {
|
|---|
| 145 | return (((jint)bytes[0]) << 24)
|
|---|
| 146 | | (((jint)bytes[1]) << 16)
|
|---|
| 147 | | (((jint)bytes[2]) << 8)
|
|---|
| 148 | | (((jint)bytes[3]) << 0);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /* read a 4-byte unit, (we don't do that quite so often) */
|
|---|
| 152 | inline jint read4 ()
|
|---|
| 153 | {
|
|---|
| 154 | skip (4);
|
|---|
| 155 | return get4 (bytes+pos-4);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /* read a 8-byte unit */
|
|---|
| 159 | static jlong get8 (unsigned char* bytes)
|
|---|
| 160 | {
|
|---|
| 161 | return (((jlong)bytes[0]) << 56)
|
|---|
| 162 | | (((jlong)bytes[1]) << 48)
|
|---|
| 163 | | (((jlong)bytes[2]) << 40)
|
|---|
| 164 | | (((jlong)bytes[3]) << 32)
|
|---|
| 165 | | (((jlong)bytes[4]) << 24)
|
|---|
| 166 | | (((jlong)bytes[5]) << 16)
|
|---|
| 167 | | (((jlong)bytes[6]) << 8)
|
|---|
| 168 | | (((jlong)bytes[7]) << 0);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /* read a 8-byte unit */
|
|---|
| 172 | inline jlong read8 ()
|
|---|
| 173 | {
|
|---|
| 174 | skip (8);
|
|---|
| 175 | return get8 (bytes+pos-8);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | inline void check_tag (int index, char expected_tag)
|
|---|
| 179 | {
|
|---|
| 180 | if (index < 0
|
|---|
| 181 | || index > pool_count
|
|---|
| 182 | || tags[index] != expected_tag)
|
|---|
| 183 | throw_class_format_error ("erroneous constant pool tag");
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | inline void verify_identifier (_Jv_Utf8Const* name)
|
|---|
| 187 | {
|
|---|
| 188 | if (! _Jv_VerifyIdentifier (name))
|
|---|
| 189 | throw_class_format_error ("erroneous identifier");
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
|
|---|
| 193 | {
|
|---|
| 194 | if (! _Jv_VerifyClassName (ptr, length))
|
|---|
| 195 | throw_class_format_error ("erroneous class name");
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | inline void verify_classname (_Jv_Utf8Const *name)
|
|---|
| 199 | {
|
|---|
| 200 | if (! _Jv_VerifyClassName (name))
|
|---|
| 201 | throw_class_format_error ("erroneous class name");
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | inline void verify_field_signature (_Jv_Utf8Const *sig)
|
|---|
| 205 | {
|
|---|
| 206 | if (! _Jv_VerifyFieldSignature (sig))
|
|---|
| 207 | throw_class_format_error ("erroneous type descriptor");
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | inline void verify_method_signature (_Jv_Utf8Const *sig)
|
|---|
| 211 | {
|
|---|
| 212 | if (! _Jv_VerifyMethodSignature (sig))
|
|---|
| 213 | throw_class_format_error ("erroneous type descriptor");
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length)
|
|---|
| 217 | {
|
|---|
| 218 | if (klass == 0 || length < 0 || offset+length > data->length)
|
|---|
| 219 | throw_internal_error ("arguments to _Jv_DefineClass");
|
|---|
| 220 |
|
|---|
| 221 | verify = true;
|
|---|
| 222 | bytes = (unsigned char*) (elements (data)+offset);
|
|---|
| 223 | len = length;
|
|---|
| 224 | pos = 0;
|
|---|
| 225 | def = (_Jv_InterpClass*) klass;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /** and here goes the parser members defined out-of-line */
|
|---|
| 229 | void parse ();
|
|---|
| 230 | void read_constpool ();
|
|---|
| 231 | void prepare_pool_entry (int index, unsigned char tag);
|
|---|
| 232 | void read_fields ();
|
|---|
| 233 | void read_methods ();
|
|---|
| 234 | void read_one_class_attribute ();
|
|---|
| 235 | void read_one_method_attribute (int method);
|
|---|
| 236 | void read_one_code_attribute (int method);
|
|---|
| 237 | void read_one_field_attribute (int field);
|
|---|
| 238 | void throw_class_format_error (char *msg);
|
|---|
| 239 |
|
|---|
| 240 | /** check an utf8 entry, without creating a Utf8Const object */
|
|---|
| 241 | bool is_attribute_name (int index, char *name);
|
|---|
| 242 |
|
|---|
| 243 | /** here goes the class-loader members defined out-of-line */
|
|---|
| 244 | void handleConstantPool ();
|
|---|
| 245 | void handleClassBegin (int, int, int);
|
|---|
| 246 | void handleInterfacesBegin (int);
|
|---|
| 247 | void handleInterface (int, int);
|
|---|
| 248 | void handleFieldsBegin (int);
|
|---|
| 249 | void handleField (int, int, int, int);
|
|---|
| 250 | void handleFieldsEnd ();
|
|---|
| 251 | void handleConstantValueAttribute (int,int);
|
|---|
| 252 | void handleMethodsBegin (int);
|
|---|
| 253 | void handleMethod (int, int, int, int);
|
|---|
| 254 | void handleMethodsEnd ();
|
|---|
| 255 | void handleCodeAttribute (int, int, int, int, int, int);
|
|---|
| 256 | void handleExceptionTableEntry (int, int, int, int, int, int);
|
|---|
| 257 |
|
|---|
| 258 | void checkExtends (jclass sub, jclass super);
|
|---|
| 259 | void checkImplements (jclass sub, jclass super);
|
|---|
| 260 |
|
|---|
| 261 | /*
|
|---|
| 262 | * FIXME: we should keep a hash table of utf8-strings, since many will
|
|---|
| 263 | * be the same. It's a little tricky, however, because the hash table
|
|---|
| 264 | * needs to interact gracefully with the garbage collector. Much
|
|---|
| 265 | * memory is to be saved by this, however! perhaps the improvement
|
|---|
| 266 | * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
|
|---|
| 267 | * computes the hash value anyway.
|
|---|
| 268 | */
|
|---|
| 269 | };
|
|---|
| 270 |
|
|---|
| 271 | void
|
|---|
| 272 | _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length)
|
|---|
| 273 | {
|
|---|
| 274 | _Jv_ClassReader reader (klass, data, offset, length);
|
|---|
| 275 | reader.parse();
|
|---|
| 276 |
|
|---|
| 277 | /* that's it! */
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | |
|---|
| 281 |
|
|---|
| 282 | /** This section defines the parsing/scanning of the class data */
|
|---|
| 283 |
|
|---|
| 284 | void
|
|---|
| 285 | _Jv_ClassReader::parse ()
|
|---|
| 286 | {
|
|---|
| 287 | int magic = read4 ();
|
|---|
| 288 |
|
|---|
| 289 | /* FIXME: Decide which range of version numbers to allow */
|
|---|
| 290 |
|
|---|
| 291 | /* int minor_version = */ read2u ();
|
|---|
| 292 | /* int major_verson = */ read2u ();
|
|---|
| 293 |
|
|---|
| 294 | if (magic != (int) 0xCAFEBABE)
|
|---|
| 295 | throw_class_format_error ("bad magic number");
|
|---|
| 296 |
|
|---|
| 297 | pool_count = read2u ();
|
|---|
| 298 |
|
|---|
| 299 | read_constpool ();
|
|---|
| 300 |
|
|---|
| 301 | int access_flags = read2u ();
|
|---|
| 302 | int this_class = read2u ();
|
|---|
| 303 | int super_class = read2u ();
|
|---|
| 304 |
|
|---|
| 305 | check_tag (this_class, JV_CONSTANT_Class);
|
|---|
| 306 | if (super_class != 0)
|
|---|
| 307 | check_tag (super_class, JV_CONSTANT_Class);
|
|---|
| 308 |
|
|---|
| 309 | handleClassBegin (access_flags, this_class, super_class);
|
|---|
| 310 |
|
|---|
| 311 | int interfaces_count = read2u ();
|
|---|
| 312 |
|
|---|
| 313 | handleInterfacesBegin (interfaces_count);
|
|---|
| 314 |
|
|---|
| 315 | for (int i = 0; i < interfaces_count; i++)
|
|---|
| 316 | {
|
|---|
| 317 | int iface = read2u ();
|
|---|
| 318 | check_tag (iface, JV_CONSTANT_Class);
|
|---|
| 319 | handleInterface (i, iface);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | read_fields ();
|
|---|
| 323 | read_methods ();
|
|---|
| 324 |
|
|---|
| 325 | int attributes_count = read2u ();
|
|---|
| 326 |
|
|---|
| 327 | for (int i = 0; i < attributes_count; i++)
|
|---|
| 328 | {
|
|---|
| 329 | read_one_class_attribute ();
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | if (pos != len)
|
|---|
| 333 | throw_class_format_error ("unused data before end of file");
|
|---|
| 334 |
|
|---|
| 335 | // tell everyone we're done.
|
|---|
| 336 | def->state = JV_STATE_LOADED;
|
|---|
| 337 | def->notifyAll ();
|
|---|
| 338 |
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | void _Jv_ClassReader::read_constpool ()
|
|---|
| 342 | {
|
|---|
| 343 | tags = (unsigned char*) _Jv_AllocBytes (pool_count);
|
|---|
| 344 | offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int)
|
|---|
| 345 | * pool_count) ;
|
|---|
| 346 |
|
|---|
| 347 | /** first, we scan the constant pool, collecting tags and offsets */
|
|---|
| 348 | tags[0] = JV_CONSTANT_Undefined;
|
|---|
| 349 | offsets[0] = pos;
|
|---|
| 350 | for (int c = 1; c < pool_count; c++)
|
|---|
| 351 | {
|
|---|
| 352 | tags[c] = read1u ();
|
|---|
| 353 | offsets[c] = pos;
|
|---|
| 354 |
|
|---|
| 355 | switch (tags[c])
|
|---|
| 356 | {
|
|---|
| 357 | case JV_CONSTANT_String:
|
|---|
| 358 | case JV_CONSTANT_Class:
|
|---|
| 359 | skip (2);
|
|---|
| 360 | break;
|
|---|
| 361 |
|
|---|
| 362 | case JV_CONSTANT_Fieldref:
|
|---|
| 363 | case JV_CONSTANT_Methodref:
|
|---|
| 364 | case JV_CONSTANT_InterfaceMethodref:
|
|---|
| 365 | case JV_CONSTANT_NameAndType:
|
|---|
| 366 | case JV_CONSTANT_Integer:
|
|---|
| 367 | case JV_CONSTANT_Float:
|
|---|
| 368 | skip (4);
|
|---|
| 369 | break;
|
|---|
| 370 |
|
|---|
| 371 | case JV_CONSTANT_Double:
|
|---|
| 372 | case JV_CONSTANT_Long:
|
|---|
| 373 | skip (8);
|
|---|
| 374 | tags[++c] = JV_CONSTANT_Undefined;
|
|---|
| 375 | break;
|
|---|
| 376 |
|
|---|
| 377 | case JV_CONSTANT_Utf8:
|
|---|
| 378 | {
|
|---|
| 379 | int len = read2u ();
|
|---|
| 380 | skip (len);
|
|---|
| 381 | }
|
|---|
| 382 | break;
|
|---|
| 383 |
|
|---|
| 384 | case JV_CONSTANT_Unicode:
|
|---|
| 385 | throw_class_format_error ("unicode not supported");
|
|---|
| 386 | break;
|
|---|
| 387 |
|
|---|
| 388 | default:
|
|---|
| 389 | throw_class_format_error ("erroneous constant pool tag");
|
|---|
| 390 | }
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | handleConstantPool ();
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 | void _Jv_ClassReader::read_fields ()
|
|---|
| 398 | {
|
|---|
| 399 | int fields_count = read2u ();
|
|---|
| 400 | handleFieldsBegin (fields_count);
|
|---|
| 401 |
|
|---|
| 402 | for (int i = 0; i < fields_count; i++)
|
|---|
| 403 | {
|
|---|
| 404 | int access_flags = read2u ();
|
|---|
| 405 | int name_index = read2u ();
|
|---|
| 406 | int descriptor_index = read2u ();
|
|---|
| 407 | int attributes_count = read2u ();
|
|---|
| 408 |
|
|---|
| 409 | check_tag (name_index, JV_CONSTANT_Utf8);
|
|---|
| 410 | prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
|
|---|
| 411 |
|
|---|
| 412 | check_tag (descriptor_index, JV_CONSTANT_Utf8);
|
|---|
| 413 | prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
|
|---|
| 414 |
|
|---|
| 415 | handleField (i, access_flags, name_index, descriptor_index);
|
|---|
| 416 |
|
|---|
| 417 | for (int j = 0; j < attributes_count; j++)
|
|---|
| 418 | {
|
|---|
| 419 | read_one_field_attribute (i);
|
|---|
| 420 | }
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | handleFieldsEnd ();
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | bool
|
|---|
| 427 | _Jv_ClassReader::is_attribute_name (int index, char *name)
|
|---|
| 428 | {
|
|---|
| 429 | check_tag (index, JV_CONSTANT_Utf8);
|
|---|
| 430 | int len = get2u (bytes+offsets[index]);
|
|---|
| 431 | if (len != (int) strlen (name))
|
|---|
| 432 | return false;
|
|---|
| 433 | else
|
|---|
| 434 | return !memcmp (bytes+offsets[index]+2, name, len);
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | void _Jv_ClassReader::read_one_field_attribute (int field_index)
|
|---|
| 438 | {
|
|---|
| 439 | int name = read2u ();
|
|---|
| 440 | int length = read4 ();
|
|---|
| 441 |
|
|---|
| 442 | if (is_attribute_name (name, "ConstantValue"))
|
|---|
| 443 | {
|
|---|
| 444 | int cv = read2u ();
|
|---|
| 445 |
|
|---|
| 446 | if (cv < pool_count
|
|---|
| 447 | && cv > 0
|
|---|
| 448 | && (tags[cv] == JV_CONSTANT_Integer
|
|---|
| 449 | || tags[cv] == JV_CONSTANT_Float
|
|---|
| 450 | || tags[cv] == JV_CONSTANT_Long
|
|---|
| 451 | || tags[cv] == JV_CONSTANT_Double
|
|---|
| 452 | || tags[cv] == JV_CONSTANT_String))
|
|---|
| 453 | {
|
|---|
| 454 | handleConstantValueAttribute (field_index, cv);
|
|---|
| 455 | }
|
|---|
| 456 | else
|
|---|
| 457 | {
|
|---|
| 458 | throw_class_format_error ("erroneous ConstantValue attribute");
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | if (length != 2)
|
|---|
| 462 | throw_class_format_error ("erroneous ConstantValue attribute");
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | else
|
|---|
| 466 | {
|
|---|
| 467 | skip (length);
|
|---|
| 468 | }
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | void _Jv_ClassReader::read_methods ()
|
|---|
| 472 | {
|
|---|
| 473 | int methods_count = read2u ();
|
|---|
| 474 |
|
|---|
| 475 | handleMethodsBegin (methods_count);
|
|---|
| 476 |
|
|---|
| 477 | for (int i = 0; i < methods_count; i++)
|
|---|
| 478 | {
|
|---|
| 479 | int access_flags = read2u ();
|
|---|
| 480 | int name_index = read2u ();
|
|---|
| 481 | int descriptor_index = read2u ();
|
|---|
| 482 | int attributes_count = read2u ();
|
|---|
| 483 |
|
|---|
| 484 | check_tag (name_index, JV_CONSTANT_Utf8);
|
|---|
| 485 | prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
|
|---|
| 486 |
|
|---|
| 487 | check_tag (name_index, JV_CONSTANT_Utf8);
|
|---|
| 488 | prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
|
|---|
| 489 |
|
|---|
| 490 | handleMethod (i, access_flags, name_index,
|
|---|
| 491 | descriptor_index);
|
|---|
| 492 |
|
|---|
| 493 | for (int j = 0; j < attributes_count; j++)
|
|---|
| 494 | {
|
|---|
| 495 | read_one_method_attribute (i);
|
|---|
| 496 | }
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | handleMethodsEnd ();
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | void _Jv_ClassReader::read_one_method_attribute (int method_index)
|
|---|
| 503 | {
|
|---|
| 504 | int name = read2u ();
|
|---|
| 505 | int length = read4 ();
|
|---|
| 506 |
|
|---|
| 507 | if (is_attribute_name (name, "Exceptions"))
|
|---|
| 508 | {
|
|---|
| 509 | _Jv_Method *method = reinterpret_cast<_Jv_Method *>
|
|---|
| 510 | (&def->methods[method_index]);
|
|---|
| 511 | if (method->throws != NULL)
|
|---|
| 512 | throw_class_format_error ("only one Exceptions attribute allowed per method");
|
|---|
| 513 |
|
|---|
| 514 | int num_exceptions = read2u ();
|
|---|
| 515 | // We use malloc here because the GC won't scan the method
|
|---|
| 516 | // objects. FIXME this means a memory leak if we GC a class.
|
|---|
| 517 | // (Currently we never do.)
|
|---|
| 518 | _Jv_Utf8Const **exceptions =
|
|---|
| 519 | (_Jv_Utf8Const **) _Jv_Malloc ((num_exceptions + 1) * sizeof (_Jv_Utf8Const *));
|
|---|
| 520 |
|
|---|
| 521 | int out = 0;
|
|---|
| 522 | _Jv_word *pool_data = def->constants.data;
|
|---|
| 523 | for (int i = 0; i < num_exceptions; ++i)
|
|---|
| 524 | {
|
|---|
| 525 | try
|
|---|
| 526 | {
|
|---|
| 527 | int ndx = read2u ();
|
|---|
| 528 | // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
|
|---|
| 529 | if (ndx != 0)
|
|---|
| 530 | {
|
|---|
| 531 | check_tag (ndx, JV_CONSTANT_Class);
|
|---|
| 532 | exceptions[out++] = pool_data[ndx].utf8;
|
|---|
| 533 | }
|
|---|
| 534 | }
|
|---|
| 535 | catch (java::lang::Throwable *exc)
|
|---|
| 536 | {
|
|---|
| 537 | _Jv_Free (exceptions);
|
|---|
| 538 | throw exc;
|
|---|
| 539 | }
|
|---|
| 540 | }
|
|---|
| 541 | exceptions[out] = NULL;
|
|---|
| 542 | method->throws = exceptions;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | else if (is_attribute_name (name, "Code"))
|
|---|
| 546 | {
|
|---|
| 547 | int start_off = pos;
|
|---|
| 548 | int max_stack = read2u ();
|
|---|
| 549 | int max_locals = read2u ();
|
|---|
| 550 | int code_length = read4 ();
|
|---|
| 551 |
|
|---|
| 552 | int code_start = pos;
|
|---|
| 553 | skip (code_length);
|
|---|
| 554 | int exception_table_length = read2u ();
|
|---|
| 555 |
|
|---|
| 556 | handleCodeAttribute (method_index,
|
|---|
| 557 | max_stack, max_locals,
|
|---|
| 558 | code_start, code_length,
|
|---|
| 559 | exception_table_length);
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 | for (int i = 0; i < exception_table_length; i++)
|
|---|
| 563 | {
|
|---|
| 564 | int start_pc = read2u ();
|
|---|
| 565 | int end_pc = read2u ();
|
|---|
| 566 | int handler_pc = read2u ();
|
|---|
| 567 | int catch_type = read2u ();
|
|---|
| 568 |
|
|---|
| 569 | if (start_pc > end_pc
|
|---|
| 570 | || start_pc < 0
|
|---|
| 571 | // END_PC can be equal to CODE_LENGTH.
|
|---|
| 572 | // See JVM Spec 4.7.4.
|
|---|
| 573 | || end_pc > code_length
|
|---|
| 574 | || handler_pc >= code_length)
|
|---|
| 575 | throw_class_format_error ("erroneous exception handler info");
|
|---|
| 576 |
|
|---|
| 577 | if (! (tags[catch_type] == JV_CONSTANT_Class
|
|---|
| 578 | || tags[catch_type] == 0))
|
|---|
| 579 | {
|
|---|
| 580 | throw_class_format_error ("erroneous exception handler info");
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | handleExceptionTableEntry (method_index,
|
|---|
| 584 | i,
|
|---|
| 585 | start_pc,
|
|---|
| 586 | end_pc,
|
|---|
| 587 | handler_pc,
|
|---|
| 588 | catch_type);
|
|---|
| 589 |
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | int attributes_count = read2u ();
|
|---|
| 593 |
|
|---|
| 594 | for (int i = 0; i < attributes_count; i++)
|
|---|
| 595 | {
|
|---|
| 596 | read_one_code_attribute (method_index);
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | if ((pos - start_off) != length)
|
|---|
| 600 | throw_class_format_error ("code attribute too short");
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | else
|
|---|
| 604 | {
|
|---|
| 605 | /* ignore unknown attributes */
|
|---|
| 606 | skip (length);
|
|---|
| 607 | }
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | void _Jv_ClassReader::read_one_code_attribute (int /*method*/)
|
|---|
| 611 | {
|
|---|
| 612 | /* ignore for now, ... later we may want to pick up
|
|---|
| 613 | line number information, for debugging purposes;
|
|---|
| 614 | in fact, the whole debugger issue is open! */
|
|---|
| 615 |
|
|---|
| 616 | /* int name = */ read2u ();
|
|---|
| 617 | int length = read4 ();
|
|---|
| 618 | skip (length);
|
|---|
| 619 |
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | void _Jv_ClassReader::read_one_class_attribute ()
|
|---|
| 623 | {
|
|---|
| 624 | /* we also ignore the class attributes, ...
|
|---|
| 625 | some day we'll add inner-classes support. */
|
|---|
| 626 |
|
|---|
| 627 | /* int name = */ read2u ();
|
|---|
| 628 | int length = read4 ();
|
|---|
| 629 | skip (length);
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 | |
|---|
| 635 |
|
|---|
| 636 | /* this section defines the semantic actions of the parser */
|
|---|
| 637 |
|
|---|
| 638 | void _Jv_ClassReader::handleConstantPool ()
|
|---|
| 639 | {
|
|---|
| 640 | /** now, we actually define the class' constant pool */
|
|---|
| 641 |
|
|---|
| 642 | // the pool is scanned explicitly by the collector
|
|---|
| 643 | jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
|
|---|
| 644 | _Jv_word *pool_data
|
|---|
| 645 | = (_Jv_word*) _Jv_AllocBytes (pool_count * sizeof (_Jv_word));
|
|---|
| 646 |
|
|---|
| 647 | def->constants.tags = pool_tags;
|
|---|
| 648 | def->constants.data = pool_data;
|
|---|
| 649 | def->constants.size = pool_count;
|
|---|
| 650 |
|
|---|
| 651 | // Here we make a pass to collect the strings! We do this, because
|
|---|
| 652 | // internally in the GCJ runtime, classes are encoded with .'s not /'s.
|
|---|
| 653 | // Therefore, we first collect the strings, and then translate the rest
|
|---|
| 654 | // of the utf8-entries (thus not representing strings) from /-notation
|
|---|
| 655 | // to .-notation.
|
|---|
| 656 | for (int i = 1; i < pool_count; i++)
|
|---|
| 657 | {
|
|---|
| 658 | if (tags[i] == JV_CONSTANT_String)
|
|---|
| 659 | {
|
|---|
| 660 | unsigned char* str_data = bytes + offsets [i];
|
|---|
| 661 | int utf_index = get2u (str_data);
|
|---|
| 662 | check_tag (utf_index, JV_CONSTANT_Utf8);
|
|---|
| 663 | unsigned char *utf_data = bytes + offsets[utf_index];
|
|---|
| 664 | int len = get2u (utf_data);
|
|---|
| 665 | pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
|
|---|
| 666 | pool_tags[i] = JV_CONSTANT_String;
|
|---|
| 667 | }
|
|---|
| 668 | else
|
|---|
| 669 | {
|
|---|
| 670 | pool_tags[i] = JV_CONSTANT_Undefined;
|
|---|
| 671 | }
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | // and now, we scan everything else but strings & utf8-entries. This
|
|---|
| 675 | // leaves out those utf8-entries which are not used; which will be left
|
|---|
| 676 | // with a tag of JV_CONSTANT_Undefined in the class definition.
|
|---|
| 677 | for (int index = 1; index < pool_count; index++)
|
|---|
| 678 | {
|
|---|
| 679 | switch (tags[index])
|
|---|
| 680 | {
|
|---|
| 681 | case JV_CONSTANT_Undefined:
|
|---|
| 682 | case JV_CONSTANT_String:
|
|---|
| 683 | case JV_CONSTANT_Utf8:
|
|---|
| 684 | continue;
|
|---|
| 685 |
|
|---|
| 686 | default:
|
|---|
| 687 | prepare_pool_entry (index, tags[index]);
|
|---|
| 688 | }
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | /* this is a recursive procedure, which will prepare pool entries as needed.
|
|---|
| 694 | Which is how we avoid initializing those entries which go unused. */
|
|---|
| 695 | void
|
|---|
| 696 | _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
|
|---|
| 697 | {
|
|---|
| 698 | /* these two, pool_data and pool_tags, point into the class
|
|---|
| 699 | structure we are currently defining */
|
|---|
| 700 |
|
|---|
| 701 | unsigned char *pool_tags = (unsigned char*) def->constants.tags;
|
|---|
| 702 | _Jv_word *pool_data = def->constants.data;
|
|---|
| 703 |
|
|---|
| 704 | /* this entry was already prepared */
|
|---|
| 705 | if (pool_tags[index] == this_tag)
|
|---|
| 706 | return;
|
|---|
| 707 |
|
|---|
| 708 | /* this_data points to the constant-pool information for the current
|
|---|
| 709 | constant-pool entry */
|
|---|
| 710 |
|
|---|
| 711 | unsigned char *this_data = bytes + offsets[index];
|
|---|
| 712 |
|
|---|
| 713 | switch (this_tag)
|
|---|
| 714 | {
|
|---|
| 715 | case JV_CONSTANT_Utf8:
|
|---|
| 716 | {
|
|---|
| 717 | // If we came here, it is because some other tag needs this
|
|---|
| 718 | // utf8-entry for type information! Thus, we translate /'s to .'s in
|
|---|
| 719 | // order to accomondate gcj's internal representation.
|
|---|
| 720 |
|
|---|
| 721 | int len = get2u (this_data);
|
|---|
| 722 | char *buffer = (char*) __builtin_alloca (len);
|
|---|
| 723 | char *s = ((char*) this_data)+2;
|
|---|
| 724 |
|
|---|
| 725 | /* FIXME: avoid using a buffer here */
|
|---|
| 726 | for (int i = 0; i < len; i++)
|
|---|
| 727 | {
|
|---|
| 728 | if (s[i] == '/')
|
|---|
| 729 | buffer[i] = '.';
|
|---|
| 730 | else
|
|---|
| 731 | buffer[i] = (char) s[i];
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
|
|---|
| 735 | pool_tags[index] = JV_CONSTANT_Utf8;
|
|---|
| 736 | }
|
|---|
| 737 | break;
|
|---|
| 738 |
|
|---|
| 739 | case JV_CONSTANT_Class:
|
|---|
| 740 | {
|
|---|
| 741 | int utf_index = get2u (this_data);
|
|---|
| 742 | check_tag (utf_index, JV_CONSTANT_Utf8);
|
|---|
| 743 | prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
|
|---|
| 744 |
|
|---|
| 745 | if (verify)
|
|---|
| 746 | verify_classname (pool_data[utf_index].utf8);
|
|---|
| 747 |
|
|---|
| 748 | pool_data[index].utf8 = pool_data[utf_index].utf8;
|
|---|
| 749 | pool_tags[index] = JV_CONSTANT_Class;
|
|---|
| 750 | }
|
|---|
| 751 | break;
|
|---|
| 752 |
|
|---|
| 753 | case JV_CONSTANT_String:
|
|---|
| 754 | // already handled before...
|
|---|
| 755 | break;
|
|---|
| 756 |
|
|---|
| 757 | case JV_CONSTANT_Fieldref:
|
|---|
| 758 | case JV_CONSTANT_Methodref:
|
|---|
| 759 | case JV_CONSTANT_InterfaceMethodref:
|
|---|
| 760 | {
|
|---|
| 761 | int class_index = get2u (this_data);
|
|---|
| 762 | int nat_index = get2u (this_data+2);
|
|---|
| 763 |
|
|---|
| 764 | check_tag (class_index, JV_CONSTANT_Class);
|
|---|
| 765 | prepare_pool_entry (class_index, JV_CONSTANT_Class);
|
|---|
| 766 |
|
|---|
| 767 | check_tag (nat_index, JV_CONSTANT_NameAndType);
|
|---|
| 768 | prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
|
|---|
| 769 |
|
|---|
| 770 | // here, verify the signature and identifier name
|
|---|
| 771 | if (verify)
|
|---|
| 772 | {
|
|---|
| 773 | _Jv_ushort name_index, type_index;
|
|---|
| 774 | _Jv_loadIndexes (&pool_data[nat_index],
|
|---|
| 775 | name_index, type_index);
|
|---|
| 776 |
|
|---|
| 777 | if (this_tag == JV_CONSTANT_Fieldref)
|
|---|
| 778 | _Jv_VerifyFieldSignature (pool_data[type_index].utf8);
|
|---|
| 779 | else
|
|---|
| 780 | _Jv_VerifyMethodSignature (pool_data[type_index].utf8);
|
|---|
| 781 |
|
|---|
| 782 | _Jv_Utf8Const* name = pool_data[name_index].utf8;
|
|---|
| 783 |
|
|---|
| 784 | if (this_tag != JV_CONSTANT_Fieldref
|
|---|
| 785 | && ( _Jv_equalUtf8Consts (name, clinit_name)
|
|---|
| 786 | || _Jv_equalUtf8Consts (name, init_name)))
|
|---|
| 787 | /* ignore */;
|
|---|
| 788 | else
|
|---|
| 789 | verify_identifier (pool_data[name_index].utf8);
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
|
|---|
| 793 | pool_tags[index] = this_tag;
|
|---|
|
|---|