comparison src/share/vm/classfile/classFileParser.cpp @ 8719:c8b31b461e1a

8003419: NPG: Clean up metadata created during class loading if failure Summary: Store metadata on ClassFileParser instance to be cleaned up by destructor. This enabled some refactoring of the enormous parseClassFile function. Reviewed-by: jmasa, acorn
author coleenp
date Wed, 13 Mar 2013 17:34:29 -0400
parents 96480359523a
children 36376b540a98 16885e702c88
comparison
equal deleted inserted replaced
8718:0ede345ec7c9 8719:c8b31b461e1a
88 #define JAVA_7_VERSION 51 88 #define JAVA_7_VERSION 51
89 89
90 // Extension method support. 90 // Extension method support.
91 #define JAVA_8_VERSION 52 91 #define JAVA_8_VERSION 52
92 92
93 93 void ClassFileParser::parse_constant_pool_entries(int length, TRAPS) {
94 void ClassFileParser::parse_constant_pool_entries(ClassLoaderData* loader_data, constantPoolHandle cp, int length, TRAPS) {
95 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize 94 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
96 // this function (_current can be allocated in a register, with scalar 95 // this function (_current can be allocated in a register, with scalar
97 // replacement of aggregates). The _current pointer is copied back to 96 // replacement of aggregates). The _current pointer is copied back to
98 // stream() when this function returns. DON'T call another method within 97 // stream() when this function returns. DON'T call another method within
99 // this method that uses stream(). 98 // this method that uses stream().
102 ClassFileStream* cfs = &cfs1; 101 ClassFileStream* cfs = &cfs1;
103 #ifdef ASSERT 102 #ifdef ASSERT
104 assert(cfs->allocated_on_stack(),"should be local"); 103 assert(cfs->allocated_on_stack(),"should be local");
105 u1* old_current = cfs0->current(); 104 u1* old_current = cfs0->current();
106 #endif 105 #endif
107 Handle class_loader(THREAD, loader_data->class_loader()); 106 Handle class_loader(THREAD, _loader_data->class_loader());
108 107
109 // Used for batching symbol allocations. 108 // Used for batching symbol allocations.
110 const char* names[SymbolTable::symbol_alloc_batch_size]; 109 const char* names[SymbolTable::symbol_alloc_batch_size];
111 int lengths[SymbolTable::symbol_alloc_batch_size]; 110 int lengths[SymbolTable::symbol_alloc_batch_size];
112 int indices[SymbolTable::symbol_alloc_batch_size]; 111 int indices[SymbolTable::symbol_alloc_batch_size];
122 switch (tag) { 121 switch (tag) {
123 case JVM_CONSTANT_Class : 122 case JVM_CONSTANT_Class :
124 { 123 {
125 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags 124 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
126 u2 name_index = cfs->get_u2_fast(); 125 u2 name_index = cfs->get_u2_fast();
127 cp->klass_index_at_put(index, name_index); 126 _cp->klass_index_at_put(index, name_index);
128 } 127 }
129 break; 128 break;
130 case JVM_CONSTANT_Fieldref : 129 case JVM_CONSTANT_Fieldref :
131 { 130 {
132 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 131 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
133 u2 class_index = cfs->get_u2_fast(); 132 u2 class_index = cfs->get_u2_fast();
134 u2 name_and_type_index = cfs->get_u2_fast(); 133 u2 name_and_type_index = cfs->get_u2_fast();
135 cp->field_at_put(index, class_index, name_and_type_index); 134 _cp->field_at_put(index, class_index, name_and_type_index);
136 } 135 }
137 break; 136 break;
138 case JVM_CONSTANT_Methodref : 137 case JVM_CONSTANT_Methodref :
139 { 138 {
140 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 139 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
141 u2 class_index = cfs->get_u2_fast(); 140 u2 class_index = cfs->get_u2_fast();
142 u2 name_and_type_index = cfs->get_u2_fast(); 141 u2 name_and_type_index = cfs->get_u2_fast();
143 cp->method_at_put(index, class_index, name_and_type_index); 142 _cp->method_at_put(index, class_index, name_and_type_index);
144 } 143 }
145 break; 144 break;
146 case JVM_CONSTANT_InterfaceMethodref : 145 case JVM_CONSTANT_InterfaceMethodref :
147 { 146 {
148 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 147 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
149 u2 class_index = cfs->get_u2_fast(); 148 u2 class_index = cfs->get_u2_fast();
150 u2 name_and_type_index = cfs->get_u2_fast(); 149 u2 name_and_type_index = cfs->get_u2_fast();
151 cp->interface_method_at_put(index, class_index, name_and_type_index); 150 _cp->interface_method_at_put(index, class_index, name_and_type_index);
152 } 151 }
153 break; 152 break;
154 case JVM_CONSTANT_String : 153 case JVM_CONSTANT_String :
155 { 154 {
156 cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags 155 cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags
157 u2 string_index = cfs->get_u2_fast(); 156 u2 string_index = cfs->get_u2_fast();
158 cp->string_index_at_put(index, string_index); 157 _cp->string_index_at_put(index, string_index);
159 } 158 }
160 break; 159 break;
161 case JVM_CONSTANT_MethodHandle : 160 case JVM_CONSTANT_MethodHandle :
162 case JVM_CONSTANT_MethodType : 161 case JVM_CONSTANT_MethodType :
163 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 162 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
172 } 171 }
173 if (tag == JVM_CONSTANT_MethodHandle) { 172 if (tag == JVM_CONSTANT_MethodHandle) {
174 cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags 173 cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags
175 u1 ref_kind = cfs->get_u1_fast(); 174 u1 ref_kind = cfs->get_u1_fast();
176 u2 method_index = cfs->get_u2_fast(); 175 u2 method_index = cfs->get_u2_fast();
177 cp->method_handle_index_at_put(index, ref_kind, method_index); 176 _cp->method_handle_index_at_put(index, ref_kind, method_index);
178 } else if (tag == JVM_CONSTANT_MethodType) { 177 } else if (tag == JVM_CONSTANT_MethodType) {
179 cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags 178 cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags
180 u2 signature_index = cfs->get_u2_fast(); 179 u2 signature_index = cfs->get_u2_fast();
181 cp->method_type_index_at_put(index, signature_index); 180 _cp->method_type_index_at_put(index, signature_index);
182 } else { 181 } else {
183 ShouldNotReachHere(); 182 ShouldNotReachHere();
184 } 183 }
185 break; 184 break;
186 case JVM_CONSTANT_InvokeDynamic : 185 case JVM_CONSTANT_InvokeDynamic :
198 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags 197 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags
199 u2 bootstrap_specifier_index = cfs->get_u2_fast(); 198 u2 bootstrap_specifier_index = cfs->get_u2_fast();
200 u2 name_and_type_index = cfs->get_u2_fast(); 199 u2 name_and_type_index = cfs->get_u2_fast();
201 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) 200 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index)
202 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later 201 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later
203 cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index); 202 _cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index);
204 } 203 }
205 break; 204 break;
206 case JVM_CONSTANT_Integer : 205 case JVM_CONSTANT_Integer :
207 { 206 {
208 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 207 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags
209 u4 bytes = cfs->get_u4_fast(); 208 u4 bytes = cfs->get_u4_fast();
210 cp->int_at_put(index, (jint) bytes); 209 _cp->int_at_put(index, (jint) bytes);
211 } 210 }
212 break; 211 break;
213 case JVM_CONSTANT_Float : 212 case JVM_CONSTANT_Float :
214 { 213 {
215 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 214 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags
216 u4 bytes = cfs->get_u4_fast(); 215 u4 bytes = cfs->get_u4_fast();
217 cp->float_at_put(index, *(jfloat*)&bytes); 216 _cp->float_at_put(index, *(jfloat*)&bytes);
218 } 217 }
219 break; 218 break;
220 case JVM_CONSTANT_Long : 219 case JVM_CONSTANT_Long :
221 // A mangled type might cause you to overrun allocated memory 220 // A mangled type might cause you to overrun allocated memory
222 guarantee_property(index+1 < length, 221 guarantee_property(index+1 < length,
223 "Invalid constant pool entry %u in class file %s", 222 "Invalid constant pool entry %u in class file %s",
224 index, CHECK); 223 index, CHECK);
225 { 224 {
226 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 225 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags
227 u8 bytes = cfs->get_u8_fast(); 226 u8 bytes = cfs->get_u8_fast();
228 cp->long_at_put(index, bytes); 227 _cp->long_at_put(index, bytes);
229 } 228 }
230 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 229 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
231 break; 230 break;
232 case JVM_CONSTANT_Double : 231 case JVM_CONSTANT_Double :
233 // A mangled type might cause you to overrun allocated memory 232 // A mangled type might cause you to overrun allocated memory
235 "Invalid constant pool entry %u in class file %s", 234 "Invalid constant pool entry %u in class file %s",
236 index, CHECK); 235 index, CHECK);
237 { 236 {
238 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 237 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags
239 u8 bytes = cfs->get_u8_fast(); 238 u8 bytes = cfs->get_u8_fast();
240 cp->double_at_put(index, *(jdouble*)&bytes); 239 _cp->double_at_put(index, *(jdouble*)&bytes);
241 } 240 }
242 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 241 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
243 break; 242 break;
244 case JVM_CONSTANT_NameAndType : 243 case JVM_CONSTANT_NameAndType :
245 { 244 {
246 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags 245 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags
247 u2 name_index = cfs->get_u2_fast(); 246 u2 name_index = cfs->get_u2_fast();
248 u2 signature_index = cfs->get_u2_fast(); 247 u2 signature_index = cfs->get_u2_fast();
249 cp->name_and_type_at_put(index, name_index, signature_index); 248 _cp->name_and_type_at_put(index, name_index, signature_index);
250 } 249 }
251 break; 250 break;
252 case JVM_CONSTANT_Utf8 : 251 case JVM_CONSTANT_Utf8 :
253 { 252 {
254 cfs->guarantee_more(2, CHECK); // utf8_length 253 cfs->guarantee_more(2, CHECK); // utf8_length
281 names[names_count] = (char*)utf8_buffer; 280 names[names_count] = (char*)utf8_buffer;
282 lengths[names_count] = utf8_length; 281 lengths[names_count] = utf8_length;
283 indices[names_count] = index; 282 indices[names_count] = index;
284 hashValues[names_count++] = hash; 283 hashValues[names_count++] = hash;
285 if (names_count == SymbolTable::symbol_alloc_batch_size) { 284 if (names_count == SymbolTable::symbol_alloc_batch_size) {
286 SymbolTable::new_symbols(loader_data, cp, names_count, names, lengths, indices, hashValues, CHECK); 285 SymbolTable::new_symbols(_loader_data, _cp, names_count, names, lengths, indices, hashValues, CHECK);
287 names_count = 0; 286 names_count = 0;
288 } 287 }
289 } else { 288 } else {
290 cp->symbol_at_put(index, result); 289 _cp->symbol_at_put(index, result);
291 } 290 }
292 } 291 }
293 break; 292 break;
294 default: 293 default:
295 classfile_parse_error( 294 classfile_parse_error(
298 } 297 }
299 } 298 }
300 299
301 // Allocate the remaining symbols 300 // Allocate the remaining symbols
302 if (names_count > 0) { 301 if (names_count > 0) {
303 SymbolTable::new_symbols(loader_data, cp, names_count, names, lengths, indices, hashValues, CHECK); 302 SymbolTable::new_symbols(_loader_data, _cp, names_count, names, lengths, indices, hashValues, CHECK);
304 } 303 }
305 304
306 // Copy _current pointer of local copy back to stream(). 305 // Copy _current pointer of local copy back to stream().
307 #ifdef ASSERT 306 #ifdef ASSERT
308 assert(cfs0->current() == old_current, "non-exclusive use of stream()"); 307 assert(cfs0->current() == old_current, "non-exclusive use of stream()");
309 #endif 308 #endif
310 cfs0->set_current(cfs1.current()); 309 cfs0->set_current(cfs1.current());
311 } 310 }
312 311
313 // This class unreferences constant pool symbols if an error has occurred
314 // while parsing the class before it is assigned into the class.
315 // If it gets an error after that it is unloaded and the constant pool will
316 // be cleaned up then.
317 class ConstantPoolCleaner : public StackObj {
318 constantPoolHandle _cphandle;
319 bool _in_error;
320 public:
321 ConstantPoolCleaner(constantPoolHandle cp) : _cphandle(cp), _in_error(true) {}
322 ~ConstantPoolCleaner() {
323 if (_in_error && _cphandle.not_null()) {
324 _cphandle->unreference_symbols();
325 }
326 }
327 void set_in_error(bool clean) { _in_error = clean; }
328 };
329
330 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); } 312 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); }
331 313
332 inline Symbol* check_symbol_at(constantPoolHandle cp, int index) { 314 inline Symbol* check_symbol_at(constantPoolHandle cp, int index) {
333 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) 315 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8())
334 return cp->symbol_at(index); 316 return cp->symbol_at(index);
335 else 317 else
336 return NULL; 318 return NULL;
337 } 319 }
338 320
339 constantPoolHandle ClassFileParser::parse_constant_pool(ClassLoaderData* loader_data, TRAPS) { 321 constantPoolHandle ClassFileParser::parse_constant_pool(TRAPS) {
340 ClassFileStream* cfs = stream(); 322 ClassFileStream* cfs = stream();
341 constantPoolHandle nullHandle; 323 constantPoolHandle nullHandle;
342 324
343 cfs->guarantee_more(3, CHECK_(nullHandle)); // length, first cp tag 325 cfs->guarantee_more(3, CHECK_(nullHandle)); // length, first cp tag
344 u2 length = cfs->get_u2_fast(); 326 u2 length = cfs->get_u2_fast();
345 guarantee_property( 327 guarantee_property(
346 length >= 1, "Illegal constant pool size %u in class file %s", 328 length >= 1, "Illegal constant pool size %u in class file %s",
347 length, CHECK_(nullHandle)); 329 length, CHECK_(nullHandle));
348 ConstantPool* constant_pool = 330 ConstantPool* constant_pool = ConstantPool::allocate(_loader_data, length,
349 ConstantPool::allocate(loader_data, 331 CHECK_(nullHandle));
350 length, 332 _cp = constant_pool; // save in case of errors
351 CHECK_(nullHandle));
352 constantPoolHandle cp (THREAD, constant_pool); 333 constantPoolHandle cp (THREAD, constant_pool);
353 334
354 ConstantPoolCleaner cp_in_error(cp); // set constant pool to be cleaned up.
355
356 // parsing constant pool entries 335 // parsing constant pool entries
357 parse_constant_pool_entries(loader_data, cp, length, CHECK_(nullHandle)); 336 parse_constant_pool_entries(length, CHECK_(nullHandle));
358 337
359 int index = 1; // declared outside of loops for portability 338 int index = 1; // declared outside of loops for portability
360 339
361 // first verification pass - validate cross references and fixup class and string constants 340 // first verification pass - validate cross references and fixup class and string constants
362 for (index = 1; index < length; index++) { // Index 0 is unused 341 for (index = 1; index < length; index++) { // Index 0 is unused
371 // fall through 350 // fall through
372 case JVM_CONSTANT_InterfaceMethodref : { 351 case JVM_CONSTANT_InterfaceMethodref : {
373 if (!_need_verify) break; 352 if (!_need_verify) break;
374 int klass_ref_index = cp->klass_ref_index_at(index); 353 int klass_ref_index = cp->klass_ref_index_at(index);
375 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index); 354 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
376 check_property(valid_cp_range(klass_ref_index, length) && 355 check_property(valid_klass_reference_at(klass_ref_index),
377 is_klass_reference(cp, klass_ref_index),
378 "Invalid constant pool index %u in class file %s", 356 "Invalid constant pool index %u in class file %s",
379 klass_ref_index, 357 klass_ref_index,
380 CHECK_(nullHandle)); 358 CHECK_(nullHandle));
381 check_property(valid_cp_range(name_and_type_ref_index, length) && 359 check_property(valid_cp_range(name_and_type_ref_index, length) &&
382 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 360 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
402 break; 380 break;
403 case JVM_CONSTANT_NameAndType : { 381 case JVM_CONSTANT_NameAndType : {
404 if (!_need_verify) break; 382 if (!_need_verify) break;
405 int name_ref_index = cp->name_ref_index_at(index); 383 int name_ref_index = cp->name_ref_index_at(index);
406 int signature_ref_index = cp->signature_ref_index_at(index); 384 int signature_ref_index = cp->signature_ref_index_at(index);
407 check_property( 385 check_property(valid_symbol_at(name_ref_index),
408 valid_cp_range(name_ref_index, length) && 386 "Invalid constant pool index %u in class file %s",
409 cp->tag_at(name_ref_index).is_utf8(), 387 name_ref_index, CHECK_(nullHandle));
410 "Invalid constant pool index %u in class file %s", 388 check_property(valid_symbol_at(signature_ref_index),
411 name_ref_index, CHECK_(nullHandle)); 389 "Invalid constant pool index %u in class file %s",
412 check_property( 390 signature_ref_index, CHECK_(nullHandle));
413 valid_cp_range(signature_ref_index, length) &&
414 cp->tag_at(signature_ref_index).is_utf8(),
415 "Invalid constant pool index %u in class file %s",
416 signature_ref_index, CHECK_(nullHandle));
417 break; 391 break;
418 } 392 }
419 case JVM_CONSTANT_Utf8 : 393 case JVM_CONSTANT_Utf8 :
420 break; 394 break;
421 case JVM_CONSTANT_UnresolvedClass : // fall-through 395 case JVM_CONSTANT_UnresolvedClass : // fall-through
423 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 397 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
424 break; 398 break;
425 case JVM_CONSTANT_ClassIndex : 399 case JVM_CONSTANT_ClassIndex :
426 { 400 {
427 int class_index = cp->klass_index_at(index); 401 int class_index = cp->klass_index_at(index);
428 check_property( 402 check_property(valid_symbol_at(class_index),
429 valid_cp_range(class_index, length) && 403 "Invalid constant pool index %u in class file %s",
430 cp->tag_at(class_index).is_utf8(), 404 class_index, CHECK_(nullHandle));
431 "Invalid constant pool index %u in class file %s",
432 class_index, CHECK_(nullHandle));
433 cp->unresolved_klass_at_put(index, cp->symbol_at(class_index)); 405 cp->unresolved_klass_at_put(index, cp->symbol_at(class_index));
434 } 406 }
435 break; 407 break;
436 case JVM_CONSTANT_StringIndex : 408 case JVM_CONSTANT_StringIndex :
437 { 409 {
438 int string_index = cp->string_index_at(index); 410 int string_index = cp->string_index_at(index);
439 check_property( 411 check_property(valid_symbol_at(string_index),
440 valid_cp_range(string_index, length) && 412 "Invalid constant pool index %u in class file %s",
441 cp->tag_at(string_index).is_utf8(), 413 string_index, CHECK_(nullHandle));
442 "Invalid constant pool index %u in class file %s",
443 string_index, CHECK_(nullHandle));
444 Symbol* sym = cp->symbol_at(string_index); 414 Symbol* sym = cp->symbol_at(string_index);
445 cp->unresolved_string_at_put(index, sym); 415 cp->unresolved_string_at_put(index, sym);
446 } 416 }
447 break; 417 break;
448 case JVM_CONSTANT_MethodHandle : 418 case JVM_CONSTANT_MethodHandle :
489 } 459 }
490 break; 460 break;
491 case JVM_CONSTANT_MethodType : 461 case JVM_CONSTANT_MethodType :
492 { 462 {
493 int ref_index = cp->method_type_index_at(index); 463 int ref_index = cp->method_type_index_at(index);
494 check_property( 464 check_property(valid_symbol_at(ref_index) && EnableInvokeDynamic,
495 valid_cp_range(ref_index, length) && 465 "Invalid constant pool index %u in class file %s",
496 cp->tag_at(ref_index).is_utf8() && 466 ref_index, CHECK_(nullHandle));
497 EnableInvokeDynamic,
498 "Invalid constant pool index %u in class file %s",
499 ref_index, CHECK_(nullHandle));
500 } 467 }
501 break; 468 break;
502 case JVM_CONSTANT_InvokeDynamic : 469 case JVM_CONSTANT_InvokeDynamic :
503 { 470 {
504 int name_and_type_ref_index = cp->invoke_dynamic_name_and_type_ref_index_at(index); 471 int name_and_type_ref_index = cp->invoke_dynamic_name_and_type_ref_index_at(index);
539 } 506 }
540 } 507 }
541 } 508 }
542 509
543 if (!_need_verify) { 510 if (!_need_verify) {
544 cp_in_error.set_in_error(false);
545 return cp; 511 return cp;
546 } 512 }
547 513
548 // second verification pass - checks the strings are of the right format. 514 // second verification pass - checks the strings are of the right format.
549 // but not yet to the other entries 515 // but not yet to the other entries
662 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted"); 628 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
663 } 629 }
664 } // end of switch 630 } // end of switch
665 } // end of for 631 } // end of for
666 632
667 cp_in_error.set_in_error(false);
668 return cp; 633 return cp;
669 } 634 }
670 635
671 636
672 void ClassFileParser::patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS) { 637 void ClassFileParser::patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS) {
784 749
785 return true; 750 return true;
786 } 751 }
787 752
788 753
789 Array<Klass*>* ClassFileParser::parse_interfaces(constantPoolHandle cp, 754 Array<Klass*>* ClassFileParser::parse_interfaces(int length,
790 int length,
791 ClassLoaderData* loader_data,
792 Handle protection_domain, 755 Handle protection_domain,
793 Symbol* class_name, 756 Symbol* class_name,
794 bool* has_default_methods, 757 bool* has_default_methods,
795 TRAPS) { 758 TRAPS) {
796 ClassFileStream* cfs = stream(); 759 if (length == 0) {
797 assert(length > 0, "only called for length>0"); 760 _local_interfaces = Universe::the_empty_klass_array();
798 // FIXME: Leak at later OOM. 761 } else {
799 Array<Klass*>* interfaces = MetadataFactory::new_array<Klass*>(loader_data, length, NULL, CHECK_NULL); 762 ClassFileStream* cfs = stream();
800 763 assert(length > 0, "only called for length>0");
801 int index; 764 _local_interfaces = MetadataFactory::new_array<Klass*>(_loader_data, length, NULL, CHECK_NULL);
802 for (index = 0; index < length; index++) { 765
803 u2 interface_index = cfs->get_u2(CHECK_NULL); 766 int index;
804 KlassHandle interf;
805 check_property(
806 valid_cp_range(interface_index, cp->length()) &&
807 is_klass_reference(cp, interface_index),
808 "Interface name has bad constant pool index %u in class file %s",
809 interface_index, CHECK_NULL);
810 if (cp->tag_at(interface_index).is_klass()) {
811 interf = KlassHandle(THREAD, cp->resolved_klass_at(interface_index));
812 } else {
813 Symbol* unresolved_klass = cp->klass_name_at(interface_index);
814
815 // Don't need to check legal name because it's checked when parsing constant pool.
816 // But need to make sure it's not an array type.
817 guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY,
818 "Bad interface name in class file %s", CHECK_NULL);
819 Handle class_loader(THREAD, loader_data->class_loader());
820
821 // Call resolve_super so classcircularity is checked
822 Klass* k = SystemDictionary::resolve_super_or_fail(class_name,
823 unresolved_klass, class_loader, protection_domain,
824 false, CHECK_NULL);
825 interf = KlassHandle(THREAD, k);
826 }
827
828 if (!interf()->is_interface()) {
829 THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(), "Implementing class", NULL);
830 }
831 if (InstanceKlass::cast(interf())->has_default_methods()) {
832 *has_default_methods = true;
833 }
834 interfaces->at_put(index, interf());
835 }
836
837 if (!_need_verify || length <= 1) {
838 return interfaces;
839 }
840
841 // Check if there's any duplicates in interfaces
842 ResourceMark rm(THREAD);
843 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(
844 THREAD, NameSigHash*, HASH_ROW_SIZE);
845 initialize_hashtable(interface_names);
846 bool dup = false;
847 {
848 debug_only(No_Safepoint_Verifier nsv;)
849 for (index = 0; index < length; index++) { 767 for (index = 0; index < length; index++) {
850 Klass* k = interfaces->at(index); 768 u2 interface_index = cfs->get_u2(CHECK_NULL);
851 Symbol* name = InstanceKlass::cast(k)->name(); 769 KlassHandle interf;
852 // If no duplicates, add (name, NULL) in hashtable interface_names. 770 check_property(
853 if (!put_after_lookup(name, NULL, interface_names)) { 771 valid_klass_reference_at(interface_index),
854 dup = true; 772 "Interface name has bad constant pool index %u in class file %s",
855 break; 773 interface_index, CHECK_NULL);
856 } 774 if (_cp->tag_at(interface_index).is_klass()) {
857 } 775 interf = KlassHandle(THREAD, _cp->resolved_klass_at(interface_index));
858 } 776 } else {
859 if (dup) { 777 Symbol* unresolved_klass = _cp->klass_name_at(interface_index);
860 classfile_parse_error("Duplicate interface name in class file %s", CHECK_NULL); 778
861 } 779 // Don't need to check legal name because it's checked when parsing constant pool.
862 780 // But need to make sure it's not an array type.
863 return interfaces; 781 guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY,
864 } 782 "Bad interface name in class file %s", CHECK_NULL);
865 783 Handle class_loader(THREAD, _loader_data->class_loader());
866 784
867 void ClassFileParser::verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS) { 785 // Call resolve_super so classcircularity is checked
786 Klass* k = SystemDictionary::resolve_super_or_fail(class_name,
787 unresolved_klass, class_loader, protection_domain,
788 false, CHECK_NULL);
789 interf = KlassHandle(THREAD, k);
790 }
791
792 if (!interf()->is_interface()) {
793 THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(), "Implementing class", NULL);
794 }
795 if (InstanceKlass::cast(interf())->has_default_methods()) {
796 *has_default_methods = true;
797 }
798 _local_interfaces->at_put(index, interf());
799 }
800
801 if (!_need_verify || length <= 1) {
802 return _local_interfaces;
803 }
804
805 // Check if there's any duplicates in interfaces
806 ResourceMark rm(THREAD);
807 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(
808 THREAD, NameSigHash*, HASH_ROW_SIZE);
809 initialize_hashtable(interface_names);
810 bool dup = false;
811 {
812 debug_only(No_Safepoint_Verifier nsv;)
813 for (index = 0; index < length; index++) {
814 Klass* k = _local_interfaces->at(index);
815 Symbol* name = InstanceKlass::cast(k)->name();
816 // If no duplicates, add (name, NULL) in hashtable interface_names.
817 if (!put_after_lookup(name, NULL, interface_names)) {
818 dup = true;
819 break;
820 }
821 }
822 }
823 if (dup) {
824 classfile_parse_error("Duplicate interface name in class file %s", CHECK_NULL);
825 }
826 }
827 return _local_interfaces;
828 }
829
830
831 void ClassFileParser::verify_constantvalue(int constantvalue_index, int signature_index, TRAPS) {
868 // Make sure the constant pool entry is of a type appropriate to this field 832 // Make sure the constant pool entry is of a type appropriate to this field
869 guarantee_property( 833 guarantee_property(
870 (constantvalue_index > 0 && 834 (constantvalue_index > 0 &&
871 constantvalue_index < cp->length()), 835 constantvalue_index < _cp->length()),
872 "Bad initial value index %u in ConstantValue attribute in class file %s", 836 "Bad initial value index %u in ConstantValue attribute in class file %s",
873 constantvalue_index, CHECK); 837 constantvalue_index, CHECK);
874 constantTag value_type = cp->tag_at(constantvalue_index); 838 constantTag value_type = _cp->tag_at(constantvalue_index);
875 switch ( cp->basic_type_for_signature_at(signature_index) ) { 839 switch ( _cp->basic_type_for_signature_at(signature_index) ) {
876 case T_LONG: 840 case T_LONG:
877 guarantee_property(value_type.is_long(), "Inconsistent constant value type in class file %s", CHECK); 841 guarantee_property(value_type.is_long(), "Inconsistent constant value type in class file %s", CHECK);
878 break; 842 break;
879 case T_FLOAT: 843 case T_FLOAT:
880 guarantee_property(value_type.is_float(), "Inconsistent constant value type in class file %s", CHECK); 844 guarantee_property(value_type.is_float(), "Inconsistent constant value type in class file %s", CHECK);
884 break; 848 break;
885 case T_BYTE: case T_CHAR: case T_SHORT: case T_BOOLEAN: case T_INT: 849 case T_BYTE: case T_CHAR: case T_SHORT: case T_BOOLEAN: case T_INT:
886 guarantee_property(value_type.is_int(), "Inconsistent constant value type in class file %s", CHECK); 850 guarantee_property(value_type.is_int(), "Inconsistent constant value type in class file %s", CHECK);
887 break; 851 break;
888 case T_OBJECT: 852 case T_OBJECT:
889 guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;") 853 guarantee_property((_cp->symbol_at(signature_index)->equals("Ljava/lang/String;")
890 && value_type.is_string()), 854 && value_type.is_string()),
891 "Bad string initial value in class file %s", CHECK); 855 "Bad string initial value in class file %s", CHECK);
892 break; 856 break;
893 default: 857 default:
894 classfile_parse_error( 858 classfile_parse_error(
897 } 861 }
898 } 862 }
899 863
900 864
901 // Parse attributes for a field. 865 // Parse attributes for a field.
902 void ClassFileParser::parse_field_attributes(ClassLoaderData* loader_data, 866 void ClassFileParser::parse_field_attributes(u2 attributes_count,
903 constantPoolHandle cp,
904 u2 attributes_count,
905 bool is_static, u2 signature_index, 867 bool is_static, u2 signature_index,
906 u2* constantvalue_index_addr, 868 u2* constantvalue_index_addr,
907 bool* is_synthetic_addr, 869 bool* is_synthetic_addr,
908 u2* generic_signature_index_addr, 870 u2* generic_signature_index_addr,
909 AnnotationArray** field_annotations,
910 AnnotationArray** field_type_annotations,
911 ClassFileParser::FieldAnnotationCollector* parsed_annotations, 871 ClassFileParser::FieldAnnotationCollector* parsed_annotations,
912 TRAPS) { 872 TRAPS) {
913 ClassFileStream* cfs = stream(); 873 ClassFileStream* cfs = stream();
914 assert(attributes_count > 0, "length should be greater than 0"); 874 assert(attributes_count > 0, "length should be greater than 0");
915 u2 constantvalue_index = 0; 875 u2 constantvalue_index = 0;
925 int runtime_invisible_type_annotations_length = 0; 885 int runtime_invisible_type_annotations_length = 0;
926 while (attributes_count--) { 886 while (attributes_count--) {
927 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 887 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
928 u2 attribute_name_index = cfs->get_u2_fast(); 888 u2 attribute_name_index = cfs->get_u2_fast();
929 u4 attribute_length = cfs->get_u4_fast(); 889 u4 attribute_length = cfs->get_u4_fast();
930 check_property(valid_cp_range(attribute_name_index, cp->length()) && 890 check_property(valid_symbol_at(attribute_name_index),
931 cp->tag_at(attribute_name_index).is_utf8(),
932 "Invalid field attribute index %u in class file %s", 891 "Invalid field attribute index %u in class file %s",
933 attribute_name_index, 892 attribute_name_index,
934 CHECK); 893 CHECK);
935 Symbol* attribute_name = cp->symbol_at(attribute_name_index); 894 Symbol* attribute_name = _cp->symbol_at(attribute_name_index);
936 if (is_static && attribute_name == vmSymbols::tag_constant_value()) { 895 if (is_static && attribute_name == vmSymbols::tag_constant_value()) {
937 // ignore if non-static 896 // ignore if non-static
938 if (constantvalue_index != 0) { 897 if (constantvalue_index != 0) {
939 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK); 898 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK);
940 } 899 }
942 attribute_length == 2, 901 attribute_length == 2,
943 "Invalid ConstantValue field attribute length %u in class file %s", 902 "Invalid ConstantValue field attribute length %u in class file %s",
944 attribute_length, CHECK); 903 attribute_length, CHECK);
945 constantvalue_index = cfs->get_u2(CHECK); 904 constantvalue_index = cfs->get_u2(CHECK);
946 if (_need_verify) { 905 if (_need_verify) {
947 verify_constantvalue(constantvalue_index, signature_index, cp, CHECK); 906 verify_constantvalue(constantvalue_index, signature_index, CHECK);
948 } 907 }
949 } else if (attribute_name == vmSymbols::tag_synthetic()) { 908 } else if (attribute_name == vmSymbols::tag_synthetic()) {
950 if (attribute_length != 0) { 909 if (attribute_length != 0) {
951 classfile_parse_error( 910 classfile_parse_error(
952 "Invalid Synthetic field attribute length %u in class file %s", 911 "Invalid Synthetic field attribute length %u in class file %s",
969 generic_signature_index = cfs->get_u2(CHECK); 928 generic_signature_index = cfs->get_u2(CHECK);
970 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 929 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
971 runtime_visible_annotations_length = attribute_length; 930 runtime_visible_annotations_length = attribute_length;
972 runtime_visible_annotations = cfs->get_u1_buffer(); 931 runtime_visible_annotations = cfs->get_u1_buffer();
973 assert(runtime_visible_annotations != NULL, "null visible annotations"); 932 assert(runtime_visible_annotations != NULL, "null visible annotations");
974 parse_annotations(loader_data, 933 parse_annotations(runtime_visible_annotations,
975 runtime_visible_annotations,
976 runtime_visible_annotations_length, 934 runtime_visible_annotations_length,
977 cp,
978 parsed_annotations, 935 parsed_annotations,
979 CHECK); 936 CHECK);
980 cfs->skip_u1(runtime_visible_annotations_length, CHECK); 937 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
981 } else if (PreserveAllAnnotations && attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 938 } else if (PreserveAllAnnotations && attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
982 runtime_invisible_annotations_length = attribute_length; 939 runtime_invisible_annotations_length = attribute_length;
1002 } 959 }
1003 960
1004 *constantvalue_index_addr = constantvalue_index; 961 *constantvalue_index_addr = constantvalue_index;
1005 *is_synthetic_addr = is_synthetic; 962 *is_synthetic_addr = is_synthetic;
1006 *generic_signature_index_addr = generic_signature_index; 963 *generic_signature_index_addr = generic_signature_index;
1007 *field_annotations = assemble_annotations(loader_data, 964 AnnotationArray* a = assemble_annotations(runtime_visible_annotations,
1008 runtime_visible_annotations,
1009 runtime_visible_annotations_length, 965 runtime_visible_annotations_length,
1010 runtime_invisible_annotations, 966 runtime_invisible_annotations,
1011 runtime_invisible_annotations_length, 967 runtime_invisible_annotations_length,
1012 CHECK); 968 CHECK);
1013 *field_type_annotations = assemble_annotations(loader_data, 969 parsed_annotations->set_field_annotations(a);
1014 runtime_visible_type_annotations, 970 a = assemble_annotations(runtime_visible_type_annotations,
1015 runtime_visible_type_annotations_length, 971 runtime_visible_type_annotations_length,
1016 runtime_invisible_type_annotations, 972 runtime_invisible_type_annotations,
1017 runtime_invisible_type_annotations_length, 973 runtime_invisible_type_annotations_length,
1018 CHECK); 974 CHECK);
975 parsed_annotations->set_field_type_annotations(a);
1019 return; 976 return;
1020 } 977 }
1021 978
1022 979
1023 // Field allocation types. Used for computing field offsets. 980 // Field allocation types. Used for computing field offsets.
1104 count[atype]++; 1061 count[atype]++;
1105 return atype; 1062 return atype;
1106 } 1063 }
1107 }; 1064 };
1108 1065
1109 Array<u2>* ClassFileParser::parse_fields(ClassLoaderData* loader_data, 1066 Array<u2>* ClassFileParser::parse_fields(Symbol* class_name,
1110 Symbol* class_name,
1111 constantPoolHandle cp,
1112 bool is_interface, 1067 bool is_interface,
1113 FieldAllocationCount *fac, 1068 FieldAllocationCount *fac,
1114 Array<AnnotationArray*>** fields_annotations,
1115 Array<AnnotationArray*>** fields_type_annotations,
1116 u2* java_fields_count_ptr, TRAPS) { 1069 u2* java_fields_count_ptr, TRAPS) {
1117 ClassFileStream* cfs = stream(); 1070 ClassFileStream* cfs = stream();
1118 cfs->guarantee_more(2, CHECK_NULL); // length 1071 cfs->guarantee_more(2, CHECK_NULL); // length
1119 u2 length = cfs->get_u2_fast(); 1072 u2 length = cfs->get_u2_fast();
1120 *java_fields_count_ptr = length; 1073 *java_fields_count_ptr = length;
1145 // array and any unused slots will be discarded. 1098 // array and any unused slots will be discarded.
1146 ResourceMark rm(THREAD); 1099 ResourceMark rm(THREAD);
1147 u2* fa = NEW_RESOURCE_ARRAY_IN_THREAD( 1100 u2* fa = NEW_RESOURCE_ARRAY_IN_THREAD(
1148 THREAD, u2, total_fields * (FieldInfo::field_slots + 1)); 1101 THREAD, u2, total_fields * (FieldInfo::field_slots + 1));
1149 1102
1150 AnnotationArray* field_annotations = NULL;
1151 AnnotationArray* field_type_annotations = NULL;
1152 // The generic signature slots start after all other fields' data. 1103 // The generic signature slots start after all other fields' data.
1153 int generic_signature_slot = total_fields * FieldInfo::field_slots; 1104 int generic_signature_slot = total_fields * FieldInfo::field_slots;
1154 int num_generic_signature = 0; 1105 int num_generic_signature = 0;
1155 for (int n = 0; n < length; n++) { 1106 for (int n = 0; n < length; n++) {
1156 cfs->guarantee_more(8, CHECK_NULL); // access_flags, name_index, descriptor_index, attributes_count 1107 cfs->guarantee_more(8, CHECK_NULL); // access_flags, name_index, descriptor_index, attributes_count
1159 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS; 1110 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1160 verify_legal_field_modifiers(flags, is_interface, CHECK_NULL); 1111 verify_legal_field_modifiers(flags, is_interface, CHECK_NULL);
1161 access_flags.set_flags(flags); 1112 access_flags.set_flags(flags);
1162 1113
1163 u2 name_index = cfs->get_u2_fast(); 1114 u2 name_index = cfs->get_u2_fast();
1164 int cp_size = cp->length(); 1115 int cp_size = _cp->length();
1165 check_property( 1116 check_property(valid_symbol_at(name_index),
1166 valid_cp_range(name_index, cp_size) && cp->tag_at(name_index).is_utf8(),
1167 "Invalid constant pool index %u for field name in class file %s", 1117 "Invalid constant pool index %u for field name in class file %s",
1168 name_index, CHECK_NULL); 1118 name_index,
1169 Symbol* name = cp->symbol_at(name_index); 1119 CHECK_NULL);
1120 Symbol* name = _cp->symbol_at(name_index);
1170 verify_legal_field_name(name, CHECK_NULL); 1121 verify_legal_field_name(name, CHECK_NULL);
1171 1122
1172 u2 signature_index = cfs->get_u2_fast(); 1123 u2 signature_index = cfs->get_u2_fast();
1173 check_property( 1124 check_property(valid_symbol_at(signature_index),
1174 valid_cp_range(signature_index, cp_size) &&
1175 cp->tag_at(signature_index).is_utf8(),
1176 "Invalid constant pool index %u for field signature in class file %s", 1125 "Invalid constant pool index %u for field signature in class file %s",
1177 signature_index, CHECK_NULL); 1126 signature_index, CHECK_NULL);
1178 Symbol* sig = cp->symbol_at(signature_index); 1127 Symbol* sig = _cp->symbol_at(signature_index);
1179 verify_legal_field_signature(name, sig, CHECK_NULL); 1128 verify_legal_field_signature(name, sig, CHECK_NULL);
1180 1129
1181 u2 constantvalue_index = 0; 1130 u2 constantvalue_index = 0;
1182 bool is_synthetic = false; 1131 bool is_synthetic = false;
1183 u2 generic_signature_index = 0; 1132 u2 generic_signature_index = 0;
1184 bool is_static = access_flags.is_static(); 1133 bool is_static = access_flags.is_static();
1185 FieldAnnotationCollector parsed_annotations; 1134 FieldAnnotationCollector parsed_annotations(_loader_data);
1186 1135
1187 u2 attributes_count = cfs->get_u2_fast(); 1136 u2 attributes_count = cfs->get_u2_fast();
1188 if (attributes_count > 0) { 1137 if (attributes_count > 0) {
1189 parse_field_attributes(loader_data, 1138 parse_field_attributes(attributes_count, is_static, signature_index,
1190 cp, attributes_count, is_static, signature_index,
1191 &constantvalue_index, &is_synthetic, 1139 &constantvalue_index, &is_synthetic,
1192 &generic_signature_index, &field_annotations, 1140 &generic_signature_index, &parsed_annotations,
1193 &field_type_annotations, &parsed_annotations,
1194 CHECK_NULL); 1141 CHECK_NULL);
1195 if (field_annotations != NULL) { 1142 if (parsed_annotations.field_annotations() != NULL) {
1196 if (*fields_annotations == NULL) { 1143 if (_fields_annotations == NULL) {
1197 *fields_annotations = MetadataFactory::new_array<AnnotationArray*>( 1144 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>(
1198 loader_data, length, NULL, 1145 _loader_data, length, NULL,
1199 CHECK_NULL); 1146 CHECK_NULL);
1200 } 1147 }
1201 (*fields_annotations)->at_put(n, field_annotations); 1148 _fields_annotations->at_put(n, parsed_annotations.field_annotations());
1202 } 1149 parsed_annotations.set_field_annotations(NULL);
1203 if (field_type_annotations != NULL) { 1150 }
1204 if (*fields_type_annotations == NULL) { 1151 if (parsed_annotations.field_type_annotations() != NULL) {
1205 *fields_type_annotations = MetadataFactory::new_array<AnnotationArray*>( 1152 if (_fields_type_annotations == NULL) {
1206 loader_data, length, NULL, 1153 _fields_type_annotations = MetadataFactory::new_array<AnnotationArray*>(
1154 _loader_data, length, NULL,
1207 CHECK_NULL); 1155 CHECK_NULL);
1208 } 1156 }
1209 (*fields_type_annotations)->at_put(n, field_type_annotations); 1157 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations());
1210 } 1158 parsed_annotations.set_field_type_annotations(NULL);
1159 }
1160
1211 if (is_synthetic) { 1161 if (is_synthetic) {
1212 access_flags.set_is_synthetic(); 1162 access_flags.set_is_synthetic();
1213 } 1163 }
1214 if (generic_signature_index != 0) { 1164 if (generic_signature_index != 0) {
1215 access_flags.set_field_has_generic_signature(); 1165 access_flags.set_field_has_generic_signature();
1222 FieldInfo* field = FieldInfo::from_field_array(fa, n); 1172 FieldInfo* field = FieldInfo::from_field_array(fa, n);
1223 field->initialize(access_flags.as_short(), 1173 field->initialize(access_flags.as_short(),
1224 name_index, 1174 name_index,
1225 signature_index, 1175 signature_index,
1226 constantvalue_index); 1176 constantvalue_index);
1227 BasicType type = cp->basic_type_for_signature_at(signature_index); 1177 BasicType type = _cp->basic_type_for_signature_at(signature_index);
1228 1178
1229 // Remember how many oops we encountered and compute allocation type 1179 // Remember how many oops we encountered and compute allocation type
1230 FieldAllocationType atype = fac->update(is_static, type); 1180 FieldAllocationType atype = fac->update(is_static, type);
1231 field->set_allocation_type(atype); 1181 field->set_allocation_type(atype);
1232 1182
1243 Symbol* name = injected[n].name(); 1193 Symbol* name = injected[n].name();
1244 Symbol* signature = injected[n].signature(); 1194 Symbol* signature = injected[n].signature();
1245 bool duplicate = false; 1195 bool duplicate = false;
1246 for (int i = 0; i < length; i++) { 1196 for (int i = 0; i < length; i++) {
1247 FieldInfo* f = FieldInfo::from_field_array(fa, i); 1197 FieldInfo* f = FieldInfo::from_field_array(fa, i);
1248 if (name == cp->symbol_at(f->name_index()) && 1198 if (name == _cp->symbol_at(f->name_index()) &&
1249 signature == cp->symbol_at(f->signature_index())) { 1199 signature == _cp->symbol_at(f->signature_index())) {
1250 // Symbol is desclared in Java so skip this one 1200 // Symbol is desclared in Java so skip this one
1251 duplicate = true; 1201 duplicate = true;
1252 break; 1202 break;
1253 } 1203 }
1254 } 1204 }
1278 // Sometimes injected fields already exist in the Java source so 1228 // Sometimes injected fields already exist in the Java source so
1279 // the fields array could be too long. In that case the 1229 // the fields array could be too long. In that case the
1280 // fields array is trimed. Also unused slots that were reserved 1230 // fields array is trimed. Also unused slots that were reserved
1281 // for generic signature indexes are discarded. 1231 // for generic signature indexes are discarded.
1282 Array<u2>* fields = MetadataFactory::new_array<u2>( 1232 Array<u2>* fields = MetadataFactory::new_array<u2>(
1283 loader_data, index * FieldInfo::field_slots + num_generic_signature, 1233 _loader_data, index * FieldInfo::field_slots + num_generic_signature,
1284 CHECK_NULL); 1234 CHECK_NULL);
1235 _fields = fields; // save in case of error
1285 { 1236 {
1286 int i = 0; 1237 int i = 0;
1287 for (; i < index * FieldInfo::field_slots; i++) { 1238 for (; i < index * FieldInfo::field_slots; i++) {
1288 fields->at_put(i, fa[i]); 1239 fields->at_put(i, fa[i]);
1289 } 1240 }
1301 THREAD, NameSigHash*, HASH_ROW_SIZE); 1252 THREAD, NameSigHash*, HASH_ROW_SIZE);
1302 initialize_hashtable(names_and_sigs); 1253 initialize_hashtable(names_and_sigs);
1303 bool dup = false; 1254 bool dup = false;
1304 { 1255 {
1305 debug_only(No_Safepoint_Verifier nsv;) 1256 debug_only(No_Safepoint_Verifier nsv;)
1306 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { 1257 for (AllFieldStream fs(fields, _cp); !fs.done(); fs.next()) {
1307 Symbol* name = fs.name(); 1258 Symbol* name = fs.name();
1308 Symbol* sig = fs.signature(); 1259 Symbol* sig = fs.signature();
1309 // If no duplicates, add name/signature in hashtable names_and_sigs. 1260 // If no duplicates, add name/signature in hashtable names_and_sigs.
1310 if (!put_after_lookup(name, sig, names_and_sigs)) { 1261 if (!put_after_lookup(name, sig, names_and_sigs)) {
1311 dup = true; 1262 dup = true;
1328 *dest++ = Bytes::get_Java_u2((u1*) (src++)); 1279 *dest++ = Bytes::get_Java_u2((u1*) (src++));
1329 } 1280 }
1330 } 1281 }
1331 1282
1332 1283
1333 u2* ClassFileParser::parse_exception_table(ClassLoaderData* loader_data, 1284 u2* ClassFileParser::parse_exception_table(u4 code_length,
1334 u4 code_length,
1335 u4 exception_table_length, 1285 u4 exception_table_length,
1336 constantPoolHandle cp,
1337 TRAPS) { 1286 TRAPS) {
1338 ClassFileStream* cfs = stream(); 1287 ClassFileStream* cfs = stream();
1339 1288
1340 u2* exception_table_start = cfs->get_u2_buffer(); 1289 u2* exception_table_start = cfs->get_u2_buffer();
1341 assert(exception_table_start != NULL, "null exception table"); 1290 assert(exception_table_start != NULL, "null exception table");
1352 CHECK_NULL); 1301 CHECK_NULL);
1353 guarantee_property(handler_pc < code_length, 1302 guarantee_property(handler_pc < code_length,
1354 "Illegal exception table handler in class file %s", 1303 "Illegal exception table handler in class file %s",
1355 CHECK_NULL); 1304 CHECK_NULL);
1356 if (catch_type_index != 0) { 1305 if (catch_type_index != 0) {
1357 guarantee_property(valid_cp_range(catch_type_index, cp->length()) && 1306 guarantee_property(valid_klass_reference_at(catch_type_index),
1358 is_klass_reference(cp, catch_type_index),
1359 "Catch type in exception table has bad constant type in class file %s", CHECK_NULL); 1307 "Catch type in exception table has bad constant type in class file %s", CHECK_NULL);
1360 } 1308 }
1361 } 1309 }
1362 } else { 1310 } else {
1363 cfs->skip_u2_fast(exception_table_length * 4); 1311 cfs->skip_u2_fast(exception_table_length * 4);
1504 // Function is used to parse both attributes: 1452 // Function is used to parse both attributes:
1505 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT) 1453 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT)
1506 u2* ClassFileParser::parse_localvariable_table(u4 code_length, 1454 u2* ClassFileParser::parse_localvariable_table(u4 code_length,
1507 u2 max_locals, 1455 u2 max_locals,
1508 u4 code_attribute_length, 1456 u4 code_attribute_length,
1509 constantPoolHandle cp,
1510 u2* localvariable_table_length, 1457 u2* localvariable_table_length,
1511 bool isLVTT, 1458 bool isLVTT,
1512 TRAPS) { 1459 TRAPS) {
1513 ClassFileStream* cfs = stream(); 1460 ClassFileStream* cfs = stream();
1514 const char * tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable"; 1461 const char * tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable";
1542 if (end_pc > code_length) { 1489 if (end_pc > code_length) {
1543 classfile_parse_error( 1490 classfile_parse_error(
1544 "Invalid length %u in %s in class file %s", 1491 "Invalid length %u in %s in class file %s",
1545 length, tbl_name, CHECK_NULL); 1492 length, tbl_name, CHECK_NULL);
1546 } 1493 }
1547 int cp_size = cp->length(); 1494 int cp_size = _cp->length();
1548 guarantee_property( 1495 guarantee_property(valid_symbol_at(name_index),
1549 valid_cp_range(name_index, cp_size) &&
1550 cp->tag_at(name_index).is_utf8(),
1551 "Name index %u in %s has bad constant type in class file %s", 1496 "Name index %u in %s has bad constant type in class file %s",
1552 name_index, tbl_name, CHECK_NULL); 1497 name_index, tbl_name, CHECK_NULL);
1553 guarantee_property( 1498 guarantee_property(valid_symbol_at(descriptor_index),
1554 valid_cp_range(descriptor_index, cp_size) &&
1555 cp->tag_at(descriptor_index).is_utf8(),
1556 "Signature index %u in %s has bad constant type in class file %s", 1499 "Signature index %u in %s has bad constant type in class file %s",
1557 descriptor_index, tbl_name, CHECK_NULL); 1500 descriptor_index, tbl_name, CHECK_NULL);
1558 1501
1559 Symbol* name = cp->symbol_at(name_index); 1502 Symbol* name = _cp->symbol_at(name_index);
1560 Symbol* sig = cp->symbol_at(descriptor_index); 1503 Symbol* sig = _cp->symbol_at(descriptor_index);
1561 verify_legal_field_name(name, CHECK_NULL); 1504 verify_legal_field_name(name, CHECK_NULL);
1562 u2 extra_slot = 0; 1505 u2 extra_slot = 0;
1563 if (!isLVTT) { 1506 if (!isLVTT) {
1564 verify_legal_field_signature(name, sig, CHECK_NULL); 1507 verify_legal_field_signature(name, sig, CHECK_NULL);
1565 1508
1577 return localvariable_table_start; 1520 return localvariable_table_start;
1578 } 1521 }
1579 1522
1580 1523
1581 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index, 1524 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
1582 u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS) { 1525 u1* u1_array, u2* u2_array, TRAPS) {
1583 ClassFileStream* cfs = stream(); 1526 ClassFileStream* cfs = stream();
1584 u2 index = 0; // index in the array with long/double occupying two slots 1527 u2 index = 0; // index in the array with long/double occupying two slots
1585 u4 i1 = *u1_index; 1528 u4 i1 = *u1_index;
1586 u4 i2 = *u2_index + 1; 1529 u4 i2 = *u2_index + 1;
1587 for(int i = 0; i < array_length; i++) { 1530 for(int i = 0; i < array_length; i++) {
1589 index++; 1532 index++;
1590 if (tag == ITEM_Long || tag == ITEM_Double) { 1533 if (tag == ITEM_Long || tag == ITEM_Double) {
1591 index++; 1534 index++;
1592 } else if (tag == ITEM_Object) { 1535 } else if (tag == ITEM_Object) {
1593 u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK); 1536 u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK);
1594 guarantee_property(valid_cp_range(class_index, cp->length()) && 1537 guarantee_property(valid_klass_reference_at(class_index),
1595 is_klass_reference(cp, class_index),
1596 "Bad class index %u in StackMap in class file %s", 1538 "Bad class index %u in StackMap in class file %s",
1597 class_index, CHECK); 1539 class_index, CHECK);
1598 } else if (tag == ITEM_Uninitialized) { 1540 } else if (tag == ITEM_Uninitialized) {
1599 u2 offset = u2_array[i2++] = cfs->get_u2(CHECK); 1541 u2 offset = u2_array[i2++] = cfs->get_u2(CHECK);
1600 guarantee_property( 1542 guarantee_property(
1611 u2_array[*u2_index] = index; 1553 u2_array[*u2_index] = index;
1612 *u1_index = i1; 1554 *u1_index = i1;
1613 *u2_index = i2; 1555 *u2_index = i2;
1614 } 1556 }
1615 1557
1616 Array<u1>* ClassFileParser::parse_stackmap_table( 1558 u1* ClassFileParser::parse_stackmap_table(
1617 ClassLoaderData* loader_data,
1618 u4 code_attribute_length, TRAPS) { 1559 u4 code_attribute_length, TRAPS) {
1619 if (code_attribute_length == 0) 1560 if (code_attribute_length == 0)
1620 return NULL; 1561 return NULL;
1621 1562
1622 ClassFileStream* cfs = stream(); 1563 ClassFileStream* cfs = stream();
1627 stream()->skip_u1(code_attribute_length, CHECK_NULL); 1568 stream()->skip_u1(code_attribute_length, CHECK_NULL);
1628 1569
1629 if (!_need_verify && !DumpSharedSpaces) { 1570 if (!_need_verify && !DumpSharedSpaces) {
1630 return NULL; 1571 return NULL;
1631 } 1572 }
1632 1573 return stackmap_table_start;
1633 Array<u1>* stackmap_data =
1634 MetadataFactory::new_array<u1>(loader_data, code_attribute_length, 0, CHECK_NULL);
1635
1636 memcpy((void*)stackmap_data->adr_at(0),
1637 (void*)stackmap_table_start, code_attribute_length);
1638 return stackmap_data;
1639 } 1574 }
1640 1575
1641 u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length, 1576 u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length,
1642 u4 method_attribute_length, 1577 u4 method_attribute_length,
1643 constantPoolHandle cp, TRAPS) { 1578 TRAPS) {
1644 ClassFileStream* cfs = stream(); 1579 ClassFileStream* cfs = stream();
1645 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length 1580 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length
1646 *checked_exceptions_length = cfs->get_u2_fast(); 1581 *checked_exceptions_length = cfs->get_u2_fast();
1647 unsigned int size = (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2); 1582 unsigned int size = (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2);
1648 u2* checked_exceptions_start = cfs->get_u2_buffer(); 1583 u2* checked_exceptions_start = cfs->get_u2_buffer();
1655 u2 len = *checked_exceptions_length; 1590 u2 len = *checked_exceptions_length;
1656 cfs->guarantee_more(2 * len, CHECK_NULL); 1591 cfs->guarantee_more(2 * len, CHECK_NULL);
1657 for (int i = 0; i < len; i++) { 1592 for (int i = 0; i < len; i++) {
1658 checked_exception = cfs->get_u2_fast(); 1593 checked_exception = cfs->get_u2_fast();
1659 check_property( 1594 check_property(
1660 valid_cp_range(checked_exception, cp->length()) && 1595 valid_klass_reference_at(checked_exception),
1661 is_klass_reference(cp, checked_exception),
1662 "Exception name has bad type at constant pool %u in class file %s", 1596 "Exception name has bad type at constant pool %u in class file %s",
1663 checked_exception, CHECK_NULL); 1597 checked_exception, CHECK_NULL);
1664 } 1598 }
1665 } 1599 }
1666 // check exceptions attribute length 1600 // check exceptions attribute length
1733 } 1667 }
1734 return index; 1668 return index;
1735 } 1669 }
1736 1670
1737 // Sift through annotations, looking for those significant to the VM: 1671 // Sift through annotations, looking for those significant to the VM:
1738 void ClassFileParser::parse_annotations(ClassLoaderData* loader_data, 1672 void ClassFileParser::parse_annotations(u1* buffer, int limit,
1739 u1* buffer, int limit,
1740 constantPoolHandle cp,
1741 ClassFileParser::AnnotationCollector* coll, 1673 ClassFileParser::AnnotationCollector* coll,
1742 TRAPS) { 1674 TRAPS) {
1743 // annotations := do(nann:u2) {annotation} 1675 // annotations := do(nann:u2) {annotation}
1744 int index = 0; 1676 int index = 0;
1745 if ((index += 2) >= limit) return; // read nann 1677 if ((index += 2) >= limit) return; // read nann
1765 int index0 = index; 1697 int index0 = index;
1766 index = skip_annotation(buffer, limit, index); 1698 index = skip_annotation(buffer, limit, index);
1767 u1* abase = buffer + index0; 1699 u1* abase = buffer + index0;
1768 int atype = Bytes::get_Java_u2(abase + atype_off); 1700 int atype = Bytes::get_Java_u2(abase + atype_off);
1769 int count = Bytes::get_Java_u2(abase + count_off); 1701 int count = Bytes::get_Java_u2(abase + count_off);
1770 Symbol* aname = check_symbol_at(cp, atype); 1702 Symbol* aname = check_symbol_at(_cp, atype);
1771 if (aname == NULL) break; // invalid annotation name 1703 if (aname == NULL) break; // invalid annotation name
1772 Symbol* member = NULL; 1704 Symbol* member = NULL;
1773 if (count >= 1) { 1705 if (count >= 1) {
1774 int member_index = Bytes::get_Java_u2(abase + member_off); 1706 int member_index = Bytes::get_Java_u2(abase + member_off);
1775 member = check_symbol_at(cp, member_index); 1707 member = check_symbol_at(_cp, member_index);
1776 if (member == NULL) break; // invalid member name 1708 if (member == NULL) break; // invalid member name
1777 } 1709 }
1778 1710
1779 // Here is where parsing particular annotations will take place. 1711 // Here is where parsing particular annotations will take place.
1780 AnnotationCollector::ID id = coll->annotation_index(loader_data, aname); 1712 AnnotationCollector::ID id = coll->annotation_index(_loader_data, aname);
1781 if (id == AnnotationCollector::_unknown) continue; 1713 if (id == AnnotationCollector::_unknown) continue;
1782 coll->set_annotation(id); 1714 coll->set_annotation(id);
1783 1715
1784 if (id == AnnotationCollector::_sun_misc_Contended) { 1716 if (id == AnnotationCollector::_sun_misc_Contended) {
1785 if (count == 1 1717 if (count == 1
1834 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) { 1766 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
1835 if (is_contended()) 1767 if (is_contended())
1836 f->set_contended_group(contended_group()); 1768 f->set_contended_group(contended_group());
1837 } 1769 }
1838 1770
1771 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() {
1772 // If there's an error deallocate metadata for field annotations
1773 MetadataFactory::free_array<u1>(_loader_data, _field_annotations);
1774 MetadataFactory::free_array<u1>(_loader_data, _field_type_annotations);
1775 }
1776
1839 void ClassFileParser::MethodAnnotationCollector::apply_to(methodHandle m) { 1777 void ClassFileParser::MethodAnnotationCollector::apply_to(methodHandle m) {
1840 if (has_annotation(_method_ForceInline)) 1778 if (has_annotation(_method_ForceInline))
1841 m->set_force_inline(true); 1779 m->set_force_inline(true);
1842 if (has_annotation(_method_DontInline)) 1780 if (has_annotation(_method_DontInline))
1843 m->set_dont_inline(true); 1781 m->set_dont_inline(true);
1892 // If no duplicates, add LVT elem in hashtable lvt_Hash. 1830 // If no duplicates, add LVT elem in hashtable lvt_Hash.
1893 if (LVT_put_after_lookup(lvt, lvt_Hash) == false 1831 if (LVT_put_after_lookup(lvt, lvt_Hash) == false
1894 && _need_verify 1832 && _need_verify
1895 && _major_version >= JAVA_1_5_VERSION) { 1833 && _major_version >= JAVA_1_5_VERSION) {
1896 clear_hashtable(lvt_Hash); 1834 clear_hashtable(lvt_Hash);
1897 ConstantPool* cp = cm->constants();
1898 classfile_parse_error("Duplicated LocalVariableTable attribute " 1835 classfile_parse_error("Duplicated LocalVariableTable attribute "
1899 "entry for '%s' in class file %s", 1836 "entry for '%s' in class file %s",
1900 cp->symbol_at(lvt->name_cp_index)->as_utf8(), 1837 _cp->symbol_at(lvt->name_cp_index)->as_utf8(),
1901 CHECK); 1838 CHECK);
1902 } 1839 }
1903 } 1840 }
1904 } 1841 }
1905 1842
1914 int index = hash(&lvtt_elem); 1851 int index = hash(&lvtt_elem);
1915 LVT_Hash* entry = LVT_lookup(&lvtt_elem, index, lvt_Hash); 1852 LVT_Hash* entry = LVT_lookup(&lvtt_elem, index, lvt_Hash);
1916 if (entry == NULL) { 1853 if (entry == NULL) {
1917 if (_need_verify) { 1854 if (_need_verify) {
1918 clear_hashtable(lvt_Hash); 1855 clear_hashtable(lvt_Hash);
1919 ConstantPool* cp = cm->constants();
1920 classfile_parse_error("LVTT entry for '%s' in class file %s " 1856 classfile_parse_error("LVTT entry for '%s' in class file %s "
1921 "does not match any LVT entry", 1857 "does not match any LVT entry",
1922 cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 1858 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1923 CHECK); 1859 CHECK);
1924 } 1860 }
1925 } else if (entry->_elem->signature_cp_index != 0 && _need_verify) { 1861 } else if (entry->_elem->signature_cp_index != 0 && _need_verify) {
1926 clear_hashtable(lvt_Hash); 1862 clear_hashtable(lvt_Hash);
1927 ConstantPool* cp = cm->constants();
1928 classfile_parse_error("Duplicated LocalVariableTypeTable attribute " 1863 classfile_parse_error("Duplicated LocalVariableTypeTable attribute "
1929 "entry for '%s' in class file %s", 1864 "entry for '%s' in class file %s",
1930 cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 1865 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1931 CHECK); 1866 CHECK);
1932 } else { 1867 } else {
1933 // to add generic signatures into LocalVariableTable 1868 // to add generic signatures into LocalVariableTable
1934 entry->_elem->signature_cp_index = lvtt_elem.descriptor_cp_index; 1869 entry->_elem->signature_cp_index = lvtt_elem.descriptor_cp_index;
1935 } 1870 }
1937 } 1872 }
1938 clear_hashtable(lvt_Hash); 1873 clear_hashtable(lvt_Hash);
1939 } 1874 }
1940 1875
1941 1876
1942 void ClassFileParser::copy_method_annotations(ClassLoaderData* loader_data, 1877 void ClassFileParser::copy_method_annotations(ConstMethod* cm,
1943 ConstMethod* cm,
1944 u1* runtime_visible_annotations, 1878 u1* runtime_visible_annotations,
1945 int runtime_visible_annotations_length, 1879 int runtime_visible_annotations_length,
1946 u1* runtime_invisible_annotations, 1880 u1* runtime_invisible_annotations,
1947 int runtime_invisible_annotations_length, 1881 int runtime_invisible_annotations_length,
1948 u1* runtime_visible_parameter_annotations, 1882 u1* runtime_visible_parameter_annotations,
1959 1893
1960 AnnotationArray* a; 1894 AnnotationArray* a;
1961 1895
1962 if (runtime_visible_annotations_length + 1896 if (runtime_visible_annotations_length +
1963 runtime_invisible_annotations_length > 0) { 1897 runtime_invisible_annotations_length > 0) {
1964 a = assemble_annotations(loader_data, 1898 a = assemble_annotations(runtime_visible_annotations,
1965 runtime_visible_annotations,
1966 runtime_visible_annotations_length, 1899 runtime_visible_annotations_length,
1967 runtime_invisible_annotations, 1900 runtime_invisible_annotations,
1968 runtime_invisible_annotations_length, 1901 runtime_invisible_annotations_length,
1969 CHECK); 1902 CHECK);
1970 cm->set_method_annotations(a); 1903 cm->set_method_annotations(a);
1971 } 1904 }
1972 1905
1973 if (runtime_visible_parameter_annotations_length + 1906 if (runtime_visible_parameter_annotations_length +
1974 runtime_invisible_parameter_annotations_length > 0) { 1907 runtime_invisible_parameter_annotations_length > 0) {
1975 a = assemble_annotations(loader_data, 1908 a = assemble_annotations(runtime_visible_parameter_annotations,
1976 runtime_visible_parameter_annotations,
1977 runtime_visible_parameter_annotations_length, 1909 runtime_visible_parameter_annotations_length,
1978 runtime_invisible_parameter_annotations, 1910 runtime_invisible_parameter_annotations,
1979 runtime_invisible_parameter_annotations_length, 1911 runtime_invisible_parameter_annotations_length,
1980 CHECK); 1912 CHECK);
1981 cm->set_parameter_annotations(a); 1913 cm->set_parameter_annotations(a);
1982 } 1914 }
1983 1915
1984 if (annotation_default_length > 0) { 1916 if (annotation_default_length > 0) {
1985 a = assemble_annotations(loader_data, 1917 a = assemble_annotations(annotation_default,
1986 annotation_default,
1987 annotation_default_length, 1918 annotation_default_length,
1988 NULL, 1919 NULL,
1989 0, 1920 0,
1990 CHECK); 1921 CHECK);
1991 cm->set_default_annotations(a); 1922 cm->set_default_annotations(a);
1992 } 1923 }
1993 1924
1994 if (runtime_visible_type_annotations_length + 1925 if (runtime_visible_type_annotations_length +
1995 runtime_invisible_type_annotations_length > 0) { 1926 runtime_invisible_type_annotations_length > 0) {
1996 a = assemble_annotations(loader_data, 1927 a = assemble_annotations(runtime_visible_type_annotations,
1997 runtime_visible_type_annotations,
1998 runtime_visible_type_annotations_length, 1928 runtime_visible_type_annotations_length,
1999 runtime_invisible_type_annotations, 1929 runtime_invisible_type_annotations,
2000 runtime_invisible_type_annotations_length, 1930 runtime_invisible_type_annotations_length,
2001 CHECK); 1931 CHECK);
2002 cm->set_type_annotations(a); 1932 cm->set_type_annotations(a);
2011 // 1941 //
2012 // The promoted_flags parameter is used to pass relevant access_flags 1942 // The promoted_flags parameter is used to pass relevant access_flags
2013 // from the method back up to the containing klass. These flag values 1943 // from the method back up to the containing klass. These flag values
2014 // are added to klass's access_flags. 1944 // are added to klass's access_flags.
2015 1945
2016 methodHandle ClassFileParser::parse_method(ClassLoaderData* loader_data, 1946 methodHandle ClassFileParser::parse_method(bool is_interface,
2017 constantPoolHandle cp,
2018 bool is_interface,
2019 AccessFlags *promoted_flags, 1947 AccessFlags *promoted_flags,
2020 TRAPS) { 1948 TRAPS) {
2021 ClassFileStream* cfs = stream(); 1949 ClassFileStream* cfs = stream();
2022 methodHandle nullHandle; 1950 methodHandle nullHandle;
2023 ResourceMark rm(THREAD); 1951 ResourceMark rm(THREAD);
2024 // Parse fixed parts 1952 // Parse fixed parts
2025 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count 1953 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count
2026 1954
2027 int flags = cfs->get_u2_fast(); 1955 int flags = cfs->get_u2_fast();
2028 u2 name_index = cfs->get_u2_fast(); 1956 u2 name_index = cfs->get_u2_fast();
2029 int cp_size = cp->length(); 1957 int cp_size = _cp->length();
2030 check_property( 1958 check_property(
2031 valid_cp_range(name_index, cp_size) && 1959 valid_symbol_at(name_index),
2032 cp->tag_at(name_index).is_utf8(),
2033 "Illegal constant pool index %u for method name in class file %s", 1960 "Illegal constant pool index %u for method name in class file %s",
2034 name_index, CHECK_(nullHandle)); 1961 name_index, CHECK_(nullHandle));
2035 Symbol* name = cp->symbol_at(name_index); 1962 Symbol* name = _cp->symbol_at(name_index);
2036 verify_legal_method_name(name, CHECK_(nullHandle)); 1963 verify_legal_method_name(name, CHECK_(nullHandle));
2037 1964
2038 u2 signature_index = cfs->get_u2_fast(); 1965 u2 signature_index = cfs->get_u2_fast();
2039 guarantee_property( 1966 guarantee_property(
2040 valid_cp_range(signature_index, cp_size) && 1967 valid_symbol_at(signature_index),
2041 cp->tag_at(signature_index).is_utf8(),
2042 "Illegal constant pool index %u for method signature in class file %s", 1968 "Illegal constant pool index %u for method signature in class file %s",
2043 signature_index, CHECK_(nullHandle)); 1969 signature_index, CHECK_(nullHandle));
2044 Symbol* signature = cp->symbol_at(signature_index); 1970 Symbol* signature = _cp->symbol_at(signature_index);
2045 1971
2046 AccessFlags access_flags; 1972 AccessFlags access_flags;
2047 if (name == vmSymbols::class_initializer_name()) { 1973 if (name == vmSymbols::class_initializer_name()) {
2048 // We ignore the other access flags for a valid class initializer. 1974 // We ignore the other access flags for a valid class initializer.
2049 // (JVM Spec 2nd ed., chapter 4.6) 1975 // (JVM Spec 2nd ed., chapter 4.6)
2095 bool method_parameters_four_byte_flags; 2021 bool method_parameters_four_byte_flags;
2096 bool parsed_code_attribute = false; 2022 bool parsed_code_attribute = false;
2097 bool parsed_checked_exceptions_attribute = false; 2023 bool parsed_checked_exceptions_attribute = false;
2098 bool parsed_stackmap_attribute = false; 2024 bool parsed_stackmap_attribute = false;
2099 // stackmap attribute - JDK1.5 2025 // stackmap attribute - JDK1.5
2100 Array<u1>* stackmap_data = NULL; 2026 u1* stackmap_data = NULL;
2027 int stackmap_data_length = 0;
2101 u2 generic_signature_index = 0; 2028 u2 generic_signature_index = 0;
2102 MethodAnnotationCollector parsed_annotations; 2029 MethodAnnotationCollector parsed_annotations;
2103 u1* runtime_visible_annotations = NULL; 2030 u1* runtime_visible_annotations = NULL;
2104 int runtime_visible_annotations_length = 0; 2031 int runtime_visible_annotations_length = 0;
2105 u1* runtime_invisible_annotations = NULL; 2032 u1* runtime_invisible_annotations = NULL;
2120 while (method_attributes_count--) { 2047 while (method_attributes_count--) {
2121 cfs->guarantee_more(6, CHECK_(nullHandle)); // method_attribute_name_index, method_attribute_length 2048 cfs->guarantee_more(6, CHECK_(nullHandle)); // method_attribute_name_index, method_attribute_length
2122 u2 method_attribute_name_index = cfs->get_u2_fast(); 2049 u2 method_attribute_name_index = cfs->get_u2_fast();
2123 u4 method_attribute_length = cfs->get_u4_fast(); 2050 u4 method_attribute_length = cfs->get_u4_fast();
2124 check_property( 2051 check_property(
2125 valid_cp_range(method_attribute_name_index, cp_size) && 2052 valid_symbol_at(method_attribute_name_index),
2126 cp->tag_at(method_attribute_name_index).is_utf8(),
2127 "Invalid method attribute name index %u in class file %s", 2053 "Invalid method attribute name index %u in class file %s",
2128 method_attribute_name_index, CHECK_(nullHandle)); 2054 method_attribute_name_index, CHECK_(nullHandle));
2129 2055
2130 Symbol* method_attribute_name = cp->symbol_at(method_attribute_name_index); 2056 Symbol* method_attribute_name = _cp->symbol_at(method_attribute_name_index);
2131 if (method_attribute_name == vmSymbols::tag_code()) { 2057 if (method_attribute_name == vmSymbols::tag_code()) {
2132 // Parse Code attribute 2058 // Parse Code attribute
2133 if (_need_verify) { 2059 if (_need_verify) {
2134 guarantee_property( 2060 guarantee_property(
2135 !access_flags.is_native() && !access_flags.is_abstract(), 2061 !access_flags.is_native() && !access_flags.is_abstract(),
2169 // Exception handler table 2095 // Exception handler table
2170 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length 2096 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length
2171 exception_table_length = cfs->get_u2_fast(); 2097 exception_table_length = cfs->get_u2_fast();
2172 if (exception_table_length > 0) { 2098 if (exception_table_length > 0) {
2173 exception_table_start = 2099 exception_table_start =
2174 parse_exception_table(loader_data, code_length, exception_table_length, cp, CHECK_(nullHandle)); 2100 parse_exception_table(code_length, exception_table_length, CHECK_(nullHandle));
2175 } 2101 }
2176 2102
2177 // Parse additional attributes in code attribute 2103 // Parse additional attributes in code attribute
2178 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count 2104 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count
2179 u2 code_attributes_count = cfs->get_u2_fast(); 2105 u2 code_attributes_count = cfs->get_u2_fast();
2202 u2 code_attribute_name_index = cfs->get_u2_fast(); 2128 u2 code_attribute_name_index = cfs->get_u2_fast();
2203 u4 code_attribute_length = cfs->get_u4_fast(); 2129 u4 code_attribute_length = cfs->get_u4_fast();
2204 calculated_attribute_length += code_attribute_length + 2130 calculated_attribute_length += code_attribute_length +
2205 sizeof(code_attribute_name_index) + 2131 sizeof(code_attribute_name_index) +
2206 sizeof(code_attribute_length); 2132 sizeof(code_attribute_length);
2207 check_property(valid_cp_range(code_attribute_name_index, cp_size) && 2133 check_property(valid_symbol_at(code_attribute_name_index),
2208 cp->tag_at(code_attribute_name_index).is_utf8(),
2209 "Invalid code attribute name index %u in class file %s", 2134 "Invalid code attribute name index %u in class file %s",
2210 code_attribute_name_index, 2135 code_attribute_name_index,
2211 CHECK_(nullHandle)); 2136 CHECK_(nullHandle));
2212 if (LoadLineNumberTables && 2137 if (LoadLineNumberTables &&
2213 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) { 2138 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) {
2214 // Parse and compress line number table 2139 // Parse and compress line number table
2215 parse_linenumber_table(code_attribute_length, code_length, 2140 parse_linenumber_table(code_attribute_length, code_length,
2216 &linenumber_table, CHECK_(nullHandle)); 2141 &linenumber_table, CHECK_(nullHandle));
2217 2142
2218 } else if (LoadLocalVariableTables && 2143 } else if (LoadLocalVariableTables &&
2219 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) { 2144 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) {
2220 // Parse local variable table 2145 // Parse local variable table
2221 if (!lvt_allocated) { 2146 if (!lvt_allocated) {
2222 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2147 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2223 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2148 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2224 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2149 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2236 } 2161 }
2237 localvariable_table_start[lvt_cnt] = 2162 localvariable_table_start[lvt_cnt] =
2238 parse_localvariable_table(code_length, 2163 parse_localvariable_table(code_length,
2239 max_locals, 2164 max_locals,
2240 code_attribute_length, 2165 code_attribute_length,
2241 cp,
2242 &localvariable_table_length[lvt_cnt], 2166 &localvariable_table_length[lvt_cnt],
2243 false, // is not LVTT 2167 false, // is not LVTT
2244 CHECK_(nullHandle)); 2168 CHECK_(nullHandle));
2245 total_lvt_length += localvariable_table_length[lvt_cnt]; 2169 total_lvt_length += localvariable_table_length[lvt_cnt];
2246 lvt_cnt++; 2170 lvt_cnt++;
2247 } else if (LoadLocalVariableTypeTables && 2171 } else if (LoadLocalVariableTypeTables &&
2248 _major_version >= JAVA_1_5_VERSION && 2172 _major_version >= JAVA_1_5_VERSION &&
2249 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) { 2173 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) {
2250 if (!lvt_allocated) { 2174 if (!lvt_allocated) {
2251 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2175 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2252 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2176 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
2253 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2177 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
2254 THREAD, u2*, INITIAL_MAX_LVT_NUMBER); 2178 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
2266 } 2190 }
2267 localvariable_type_table_start[lvtt_cnt] = 2191 localvariable_type_table_start[lvtt_cnt] =
2268 parse_localvariable_table(code_length, 2192 parse_localvariable_table(code_length,
2269 max_locals, 2193 max_locals,
2270 code_attribute_length, 2194 code_attribute_length,
2271 cp,
2272 &localvariable_type_table_length[lvtt_cnt], 2195 &localvariable_type_table_length[lvtt_cnt],
2273 true, // is LVTT 2196 true, // is LVTT
2274 CHECK_(nullHandle)); 2197 CHECK_(nullHandle));
2275 lvtt_cnt++; 2198 lvtt_cnt++;
2276 } else if (UseSplitVerifier && 2199 } else if (UseSplitVerifier &&
2277 _major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION && 2200 _major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION &&
2278 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) { 2201 _cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) {
2279 // Stack map is only needed by the new verifier in JDK1.5. 2202 // Stack map is only needed by the new verifier in JDK1.5.
2280 if (parsed_stackmap_attribute) { 2203 if (parsed_stackmap_attribute) {
2281 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_(nullHandle)); 2204 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_(nullHandle));
2282 } 2205 }
2283 stackmap_data = parse_stackmap_table(loader_data, code_attribute_length, CHECK_(nullHandle)); 2206 stackmap_data = parse_stackmap_table(code_attribute_length, CHECK_(nullHandle));
2207 stackmap_data_length = code_attribute_length;
2284 parsed_stackmap_attribute = true; 2208 parsed_stackmap_attribute = true;
2285 } else { 2209 } else {
2286 // Skip unknown attributes 2210 // Skip unknown attributes
2287 cfs->skip_u1(code_attribute_length, CHECK_(nullHandle)); 2211 cfs->skip_u1(code_attribute_length, CHECK_(nullHandle));
2288 } 2212 }
2299 } 2223 }
2300 parsed_checked_exceptions_attribute = true; 2224 parsed_checked_exceptions_attribute = true;
2301 checked_exceptions_start = 2225 checked_exceptions_start =
2302 parse_checked_exceptions(&checked_exceptions_length, 2226 parse_checked_exceptions(&checked_exceptions_length,
2303 method_attribute_length, 2227 method_attribute_length,
2304 cp, CHECK_(nullHandle)); 2228 CHECK_(nullHandle));
2305 } else if (method_attribute_name == vmSymbols::tag_method_parameters()) { 2229 } else if (method_attribute_name == vmSymbols::tag_method_parameters()) {
2306 // reject multiple method parameters 2230 // reject multiple method parameters
2307 if (method_parameters_seen) { 2231 if (method_parameters_seen) {
2308 classfile_parse_error("Multiple MethodParameters attributes in class file %s", CHECK_(nullHandle)); 2232 classfile_parse_error("Multiple MethodParameters attributes in class file %s", CHECK_(nullHandle));
2309 } 2233 }
2357 generic_signature_index = cfs->get_u2_fast(); 2281 generic_signature_index = cfs->get_u2_fast();
2358 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 2282 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
2359 runtime_visible_annotations_length = method_attribute_length; 2283 runtime_visible_annotations_length = method_attribute_length;
2360 runtime_visible_annotations = cfs->get_u1_buffer(); 2284 runtime_visible_annotations = cfs->get_u1_buffer();
2361 assert(runtime_visible_annotations != NULL, "null visible annotations"); 2285 assert(runtime_visible_annotations != NULL, "null visible annotations");
2362 parse_annotations(loader_data, 2286 parse_annotations(runtime_visible_annotations,
2363 runtime_visible_annotations, 2287 runtime_visible_annotations_length, &parsed_annotations,
2364 runtime_visible_annotations_length, cp, &parsed_annotations,
2365 CHECK_(nullHandle)); 2288 CHECK_(nullHandle));
2366 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle)); 2289 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle));
2367 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 2290 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
2368 runtime_invisible_annotations_length = method_attribute_length; 2291 runtime_invisible_annotations_length = method_attribute_length;
2369 runtime_invisible_annotations = cfs->get_u1_buffer(); 2292 runtime_invisible_annotations = cfs->get_u1_buffer();
2432 runtime_invisible_type_annotations_length, 2355 runtime_invisible_type_annotations_length,
2433 annotation_default_length, 2356 annotation_default_length,
2434 0); 2357 0);
2435 2358
2436 Method* m = Method::allocate( 2359 Method* m = Method::allocate(
2437 loader_data, code_length, access_flags, &sizes, 2360 _loader_data, code_length, access_flags, &sizes,
2438 ConstMethod::NORMAL, CHECK_(nullHandle)); 2361 ConstMethod::NORMAL, CHECK_(nullHandle));
2439 2362
2440 ClassLoadingService::add_class_method_size(m->size()*HeapWordSize); 2363 ClassLoadingService::add_class_method_size(m->size()*HeapWordSize);
2441 2364
2442 // Fill in information from fixed part (access_flags already set) 2365 // Fill in information from fixed part (access_flags already set)
2443 m->set_constants(cp()); 2366 m->set_constants(_cp);
2444 m->set_name_index(name_index); 2367 m->set_name_index(name_index);
2445 m->set_signature_index(signature_index); 2368 m->set_signature_index(signature_index);
2446 #ifdef CC_INTERP 2369 #ifdef CC_INTERP
2447 // hmm is there a gc issue here?? 2370 // hmm is there a gc issue here??
2448 ResultTypeFinder rtf(cp->symbol_at(signature_index)); 2371 ResultTypeFinder rtf(_cp->symbol_at(signature_index));
2449 m->set_result_index(rtf.type()); 2372 m->set_result_index(rtf.type());
2450 #endif 2373 #endif
2451 2374
2452 if (args_size >= 0) { 2375 if (args_size >= 0) {
2453 m->set_size_of_parameters(args_size); 2376 m->set_size_of_parameters(args_size);
2462 #endif 2385 #endif
2463 2386
2464 // Fill in code attribute information 2387 // Fill in code attribute information
2465 m->set_max_stack(max_stack); 2388 m->set_max_stack(max_stack);
2466 m->set_max_locals(max_locals); 2389 m->set_max_locals(max_locals);
2467 m->constMethod()->set_stackmap_data(stackmap_data); 2390 if (stackmap_data != NULL) {
2391 m->constMethod()->copy_stackmap_data(_loader_data, stackmap_data,
2392 stackmap_data_length, CHECK_NULL);
2393 }
2468 2394
2469 // Copy byte codes 2395 // Copy byte codes
2470 m->set_code(code_start); 2396 m->set_code(code_start);
2471 2397
2472 // Copy line number table 2398 // Copy line number table
2518 2444
2519 if (parsed_annotations.has_any_annotations()) 2445 if (parsed_annotations.has_any_annotations())
2520 parsed_annotations.apply_to(m); 2446 parsed_annotations.apply_to(m);
2521 2447
2522 // Copy annotations 2448 // Copy annotations
2523 copy_method_annotations(loader_data, m->constMethod(), 2449 copy_method_annotations(m->constMethod(),
2524 runtime_visible_annotations, 2450 runtime_visible_annotations,
2525 runtime_visible_annotations_length, 2451 runtime_visible_annotations_length,
2526 runtime_invisible_annotations, 2452 runtime_invisible_annotations,
2527 runtime_invisible_annotations_length, 2453 runtime_invisible_annotations_length,
2528 runtime_visible_parameter_annotations, 2454 runtime_visible_parameter_annotations,
2558 2484
2559 // The promoted_flags parameter is used to pass relevant access_flags 2485 // The promoted_flags parameter is used to pass relevant access_flags
2560 // from the methods back up to the containing klass. These flag values 2486 // from the methods back up to the containing klass. These flag values
2561 // are added to klass's access_flags. 2487 // are added to klass's access_flags.
2562 2488
2563 Array<Method*>* ClassFileParser::parse_methods(ClassLoaderData* loader_data, 2489 Array<Method*>* ClassFileParser::parse_methods(bool is_interface,
2564 constantPoolHandle cp,
2565 bool is_interface,
2566 AccessFlags* promoted_flags, 2490 AccessFlags* promoted_flags,
2567 bool* has_final_method, 2491 bool* has_final_method,
2568 bool* has_default_methods, 2492 bool* has_default_methods,
2569 TRAPS) { 2493 TRAPS) {
2570 ClassFileStream* cfs = stream(); 2494 ClassFileStream* cfs = stream();
2571 cfs->guarantee_more(2, CHECK_NULL); // length 2495 cfs->guarantee_more(2, CHECK_NULL); // length
2572 u2 length = cfs->get_u2_fast(); 2496 u2 length = cfs->get_u2_fast();
2573 if (length == 0) { 2497 if (length == 0) {
2574 return Universe::the_empty_method_array(); 2498 _methods = Universe::the_empty_method_array();
2575 } else { 2499 } else {
2576 // FIXME: Handle leaks at later failures. 2500 _methods = MetadataFactory::new_array<Method*>(_loader_data, length, NULL, CHECK_NULL);
2577 Array<Method*>* methods = MetadataFactory::new_array<Method*>(loader_data, length, NULL, CHECK_NULL);
2578 2501
2579 HandleMark hm(THREAD); 2502 HandleMark hm(THREAD);
2580 for (int index = 0; index < length; index++) { 2503 for (int index = 0; index < length; index++) {
2581 methodHandle method = parse_method(loader_data, 2504 methodHandle method = parse_method(is_interface,
2582 cp, is_interface,
2583 promoted_flags, 2505 promoted_flags,
2584 CHECK_NULL); 2506 CHECK_NULL);
2585 2507
2586 if (method->is_final()) { 2508 if (method->is_final()) {
2587 *has_final_method = true; 2509 *has_final_method = true;
2588 } 2510 }
2589 if (is_interface && !method->is_abstract() && !method->is_static()) { 2511 if (is_interface && !method->is_abstract() && !method->is_static()) {
2590 // default method 2512 // default method
2591 *has_default_methods = true; 2513 *has_default_methods = true;
2592 } 2514 }
2593 methods->at_put(index, method()); 2515 _methods->at_put(index, method());
2594 } 2516 }
2595 2517
2596 if (_need_verify && length > 1) { 2518 if (_need_verify && length > 1) {
2597 // Check duplicated methods 2519 // Check duplicated methods
2598 ResourceMark rm(THREAD); 2520 ResourceMark rm(THREAD);
2601 initialize_hashtable(names_and_sigs); 2523 initialize_hashtable(names_and_sigs);
2602 bool dup = false; 2524 bool dup = false;
2603 { 2525 {
2604 debug_only(No_Safepoint_Verifier nsv;) 2526 debug_only(No_Safepoint_Verifier nsv;)
2605 for (int i = 0; i < length; i++) { 2527 for (int i = 0; i < length; i++) {
2606 Method* m = methods->at(i); 2528 Method* m = _methods->at(i);
2607 // If no duplicates, add name/signature in hashtable names_and_sigs. 2529 // If no duplicates, add name/signature in hashtable names_and_sigs.
2608 if (!put_after_lookup(m->name(), m->signature(), names_and_sigs)) { 2530 if (!put_after_lookup(m->name(), m->signature(), names_and_sigs)) {
2609 dup = true; 2531 dup = true;
2610 break; 2532 break;
2611 } 2533 }
2614 if (dup) { 2536 if (dup) {
2615 classfile_parse_error("Duplicate method name&signature in class file %s", 2537 classfile_parse_error("Duplicate method name&signature in class file %s",
2616 CHECK_NULL); 2538 CHECK_NULL);
2617 } 2539 }
2618 } 2540 }
2619 return methods; 2541 }
2620 } 2542 return _methods;
2621 } 2543 }
2622 2544
2623 2545
2624 Array<int>* ClassFileParser::sort_methods(ClassLoaderData* loader_data, 2546 intArray* ClassFileParser::sort_methods(Array<Method*>* methods) {
2625 Array<Method*>* methods,
2626 TRAPS) {
2627 int length = methods->length(); 2547 int length = methods->length();
2628 // If JVMTI original method ordering or sharing is enabled we have to 2548 // If JVMTI original method ordering or sharing is enabled we have to
2629 // remember the original class file ordering. 2549 // remember the original class file ordering.
2630 // We temporarily use the vtable_index field in the Method* to store the 2550 // We temporarily use the vtable_index field in the Method* to store the
2631 // class file index, so we can read in after calling qsort. 2551 // class file index, so we can read in after calling qsort.
2639 } 2559 }
2640 // Sort method array by ascending method name (for faster lookups & vtable construction) 2560 // Sort method array by ascending method name (for faster lookups & vtable construction)
2641 // Note that the ordering is not alphabetical, see Symbol::fast_compare 2561 // Note that the ordering is not alphabetical, see Symbol::fast_compare
2642 Method::sort_methods(methods); 2562 Method::sort_methods(methods);
2643 2563
2564 intArray* method_ordering = NULL;
2644 // If JVMTI original method ordering or sharing is enabled construct int 2565 // If JVMTI original method ordering or sharing is enabled construct int
2645 // array remembering the original ordering 2566 // array remembering the original ordering
2646 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { 2567 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) {
2647 Array<int>* method_ordering = MetadataFactory::new_array<int>(loader_data, length, CHECK_NULL); 2568 method_ordering = new intArray(length);
2648 for (int index = 0; index < length; index++) { 2569 for (int index = 0; index < length; index++) {
2649 Method* m = methods->at(index); 2570 Method* m = methods->at(index);
2650 int old_index = m->vtable_index(); 2571 int old_index = m->vtable_index();
2651 assert(old_index >= 0 && old_index < length, "invalid method index"); 2572 assert(old_index >= 0 && old_index < length, "invalid method index");
2652 method_ordering->at_put(index, old_index); 2573 method_ordering->at_put(index, old_index);
2653 m->set_vtable_index(Method::invalid_vtable_index); 2574 m->set_vtable_index(Method::invalid_vtable_index);
2654 } 2575 }
2655 return method_ordering; 2576 }
2656 } else { 2577 return method_ordering;
2657 return Universe::the_empty_int_array(); 2578 }
2658 } 2579
2659 } 2580
2660 2581 void ClassFileParser::parse_classfile_sourcefile_attribute(TRAPS) {
2661
2662 void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, TRAPS) {
2663 ClassFileStream* cfs = stream(); 2582 ClassFileStream* cfs = stream();
2664 cfs->guarantee_more(2, CHECK); // sourcefile_index 2583 cfs->guarantee_more(2, CHECK); // sourcefile_index
2665 u2 sourcefile_index = cfs->get_u2_fast(); 2584 u2 sourcefile_index = cfs->get_u2_fast();
2666 check_property( 2585 check_property(
2667 valid_cp_range(sourcefile_index, cp->length()) && 2586 valid_symbol_at(sourcefile_index),
2668 cp->tag_at(sourcefile_index).is_utf8(),
2669 "Invalid SourceFile attribute at constant pool index %u in class file %s", 2587 "Invalid SourceFile attribute at constant pool index %u in class file %s",
2670 sourcefile_index, CHECK); 2588 sourcefile_index, CHECK);
2671 set_class_sourcefile(cp->symbol_at(sourcefile_index)); 2589 set_class_sourcefile(_cp->symbol_at(sourcefile_index));
2672 } 2590 }
2673 2591
2674 2592
2675 2593
2676 void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, 2594 void ClassFileParser::parse_classfile_source_debug_extension_attribute(int length, TRAPS) {
2677 int length, TRAPS) {
2678 ClassFileStream* cfs = stream(); 2595 ClassFileStream* cfs = stream();
2679 u1* sde_buffer = cfs->get_u1_buffer(); 2596 u1* sde_buffer = cfs->get_u1_buffer();
2680 assert(sde_buffer != NULL, "null sde buffer"); 2597 assert(sde_buffer != NULL, "null sde buffer");
2681 2598
2682 // Don't bother storing it if there is no way to retrieve it 2599 // Don't bother storing it if there is no way to retrieve it
2696 2613
2697 // Inner classes can be static, private or protected (classic VM does this) 2614 // Inner classes can be static, private or protected (classic VM does this)
2698 #define RECOGNIZED_INNER_CLASS_MODIFIERS (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED | JVM_ACC_STATIC) 2615 #define RECOGNIZED_INNER_CLASS_MODIFIERS (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED | JVM_ACC_STATIC)
2699 2616
2700 // Return number of classes in the inner classes attribute table 2617 // Return number of classes in the inner classes attribute table
2701 u2 ClassFileParser::parse_classfile_inner_classes_attribute(ClassLoaderData* loader_data, 2618 u2 ClassFileParser::parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start,
2702 u1* inner_classes_attribute_start,
2703 bool parsed_enclosingmethod_attribute, 2619 bool parsed_enclosingmethod_attribute,
2704 u2 enclosing_method_class_index, 2620 u2 enclosing_method_class_index,
2705 u2 enclosing_method_method_index, 2621 u2 enclosing_method_method_index,
2706 constantPoolHandle cp,
2707 TRAPS) { 2622 TRAPS) {
2708 ClassFileStream* cfs = stream(); 2623 ClassFileStream* cfs = stream();
2709 u1* current_mark = cfs->current(); 2624 u1* current_mark = cfs->current();
2710 u2 length = 0; 2625 u2 length = 0;
2711 if (inner_classes_attribute_start != NULL) { 2626 if (inner_classes_attribute_start != NULL) {
2722 // inner_class_access_flags, 2637 // inner_class_access_flags,
2723 // ... 2638 // ...
2724 // enclosing_method_class_index, 2639 // enclosing_method_class_index,
2725 // enclosing_method_method_index] 2640 // enclosing_method_method_index]
2726 int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0); 2641 int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0);
2727 // FIXME: Will leak on exceptions. 2642 Array<u2>* inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
2728 Array<u2>* inner_classes = MetadataFactory::new_array<u2>(loader_data, size, CHECK_0); 2643 _inner_classes = inner_classes;
2644
2729 int index = 0; 2645 int index = 0;
2730 int cp_size = cp->length(); 2646 int cp_size = _cp->length();
2731 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2 2647 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2
2732 for (int n = 0; n < length; n++) { 2648 for (int n = 0; n < length; n++) {
2733 // Inner class index 2649 // Inner class index
2734 u2 inner_class_info_index = cfs->get_u2_fast(); 2650 u2 inner_class_info_index = cfs->get_u2_fast();
2735 check_property( 2651 check_property(
2736 inner_class_info_index == 0 || 2652 inner_class_info_index == 0 ||
2737 (valid_cp_range(inner_class_info_index, cp_size) && 2653 valid_klass_reference_at(inner_class_info_index),
2738 is_klass_reference(cp, inner_class_info_index)),
2739 "inner_class_info_index %u has bad constant type in class file %s", 2654 "inner_class_info_index %u has bad constant type in class file %s",
2740 inner_class_info_index, CHECK_0); 2655 inner_class_info_index, CHECK_0);
2741 // Outer class index 2656 // Outer class index
2742 u2 outer_class_info_index = cfs->get_u2_fast(); 2657 u2 outer_class_info_index = cfs->get_u2_fast();
2743 check_property( 2658 check_property(
2744 outer_class_info_index == 0 || 2659 outer_class_info_index == 0 ||
2745 (valid_cp_range(outer_class_info_index, cp_size) && 2660 valid_klass_reference_at(outer_class_info_index),
2746 is_klass_reference(cp, outer_class_info_index)),
2747 "outer_class_info_index %u has bad constant type in class file %s", 2661 "outer_class_info_index %u has bad constant type in class file %s",
2748 outer_class_info_index, CHECK_0); 2662 outer_class_info_index, CHECK_0);
2749 // Inner class name 2663 // Inner class name
2750 u2 inner_name_index = cfs->get_u2_fast(); 2664 u2 inner_name_index = cfs->get_u2_fast();
2751 check_property( 2665 check_property(
2752 inner_name_index == 0 || (valid_cp_range(inner_name_index, cp_size) && 2666 inner_name_index == 0 || valid_symbol_at(inner_name_index),
2753 cp->tag_at(inner_name_index).is_utf8()),
2754 "inner_name_index %u has bad constant type in class file %s", 2667 "inner_name_index %u has bad constant type in class file %s",
2755 inner_name_index, CHECK_0); 2668 inner_name_index, CHECK_0);
2756 if (_need_verify) { 2669 if (_need_verify) {
2757 guarantee_property(inner_class_info_index != outer_class_info_index, 2670 guarantee_property(inner_class_info_index != outer_class_info_index,
2758 "Class is both outer and inner class in class file %s", CHECK_0); 2671 "Class is both outer and inner class in class file %s", CHECK_0);
2792 inner_classes->at_put(index++, enclosing_method_class_index); 2705 inner_classes->at_put(index++, enclosing_method_class_index);
2793 inner_classes->at_put(index++, enclosing_method_method_index); 2706 inner_classes->at_put(index++, enclosing_method_method_index);
2794 } 2707 }
2795 assert(index == size, "wrong size"); 2708 assert(index == size, "wrong size");
2796 2709
2797 // Update InstanceKlass with inner class info.
2798 set_class_inner_classes(inner_classes);
2799
2800 // Restore buffer's current position. 2710 // Restore buffer's current position.
2801 cfs->set_current(current_mark); 2711 cfs->set_current(current_mark);
2802 2712
2803 return length; 2713 return length;
2804 } 2714 }
2805 2715
2806 void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, TRAPS) { 2716 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) {
2807 set_class_synthetic_flag(true); 2717 set_class_synthetic_flag(true);
2808 } 2718 }
2809 2719
2810 void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, TRAPS) { 2720 void ClassFileParser::parse_classfile_signature_attribute(TRAPS) {
2811 ClassFileStream* cfs = stream(); 2721 ClassFileStream* cfs = stream();
2812 u2 signature_index = cfs->get_u2(CHECK); 2722 u2 signature_index = cfs->get_u2(CHECK);
2813 check_property( 2723 check_property(
2814 valid_cp_range(signature_index, cp->length()) && 2724 valid_symbol_at(signature_index),
2815 cp->tag_at(signature_index).is_utf8(),
2816 "Invalid constant pool index %u in Signature attribute in class file %s", 2725 "Invalid constant pool index %u in Signature attribute in class file %s",
2817 signature_index, CHECK); 2726 signature_index, CHECK);
2818 set_class_generic_signature(cp->symbol_at(signature_index)); 2727 set_class_generic_signature(_cp->symbol_at(signature_index));
2819 } 2728 }
2820 2729
2821 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(ClassLoaderData* loader_data, 2730 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(u4 attribute_byte_length, TRAPS) {
2822 constantPoolHandle cp,
2823 u4 attribute_byte_length, TRAPS) {
2824 ClassFileStream* cfs = stream(); 2731 ClassFileStream* cfs = stream();
2825 u1* current_start = cfs->current(); 2732 u1* current_start = cfs->current();
2826 2733
2827 cfs->guarantee_more(2, CHECK); // length 2734 cfs->guarantee_more(2, CHECK); // length
2828 int attribute_array_length = cfs->get_u2_fast(); 2735 int attribute_array_length = cfs->get_u2_fast();
2839 2746
2840 // The attribute is copied into a short[] array. 2747 // The attribute is copied into a short[] array.
2841 // The array begins with a series of short[2] pairs, one for each tuple. 2748 // The array begins with a series of short[2] pairs, one for each tuple.
2842 int index_size = (attribute_array_length * 2); 2749 int index_size = (attribute_array_length * 2);
2843 2750
2844 Array<u2>* operands = MetadataFactory::new_array<u2>(loader_data, index_size + operand_count, CHECK); 2751 Array<u2>* operands = MetadataFactory::new_array<u2>(_loader_data, index_size + operand_count, CHECK);
2752
2753 // Eagerly assign operands so they will be deallocated with the constant
2754 // pool if there is an error.
2755 _cp->set_operands(operands);
2845 2756
2846 int operand_fill_index = index_size; 2757 int operand_fill_index = index_size;
2847 int cp_size = cp->length(); 2758 int cp_size = _cp->length();
2848 2759
2849 for (int n = 0; n < attribute_array_length; n++) { 2760 for (int n = 0; n < attribute_array_length; n++) {
2850 // Store a 32-bit offset into the header of the operand array. 2761 // Store a 32-bit offset into the header of the operand array.
2851 ConstantPool::operand_offset_at_put(operands, n, operand_fill_index); 2762 ConstantPool::operand_offset_at_put(operands, n, operand_fill_index);
2852 2763
2854 cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc 2765 cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc
2855 u2 bootstrap_method_index = cfs->get_u2_fast(); 2766 u2 bootstrap_method_index = cfs->get_u2_fast();
2856 u2 argument_count = cfs->get_u2_fast(); 2767 u2 argument_count = cfs->get_u2_fast();
2857 check_property( 2768 check_property(
2858 valid_cp_range(bootstrap_method_index, cp_size) && 2769 valid_cp_range(bootstrap_method_index, cp_size) &&
2859 cp->tag_at(bootstrap_method_index).is_method_handle(), 2770 _cp->tag_at(bootstrap_method_index).is_method_handle(),
2860 "bootstrap_method_index %u has bad constant type in class file %s", 2771 "bootstrap_method_index %u has bad constant type in class file %s",
2861 bootstrap_method_index, 2772 bootstrap_method_index,
2862 CHECK); 2773 CHECK);
2863 operands->at_put(operand_fill_index++, bootstrap_method_index); 2774 operands->at_put(operand_fill_index++, bootstrap_method_index);
2864 operands->at_put(operand_fill_index++, argument_count); 2775 operands->at_put(operand_fill_index++, argument_count);
2866 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc] 2777 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc]
2867 for (int j = 0; j < argument_count; j++) { 2778 for (int j = 0; j < argument_count; j++) {
2868 u2 argument_index = cfs->get_u2_fast(); 2779 u2 argument_index = cfs->get_u2_fast();
2869 check_property( 2780 check_property(
2870 valid_cp_range(argument_index, cp_size) && 2781 valid_cp_range(argument_index, cp_size) &&
2871 cp->tag_at(argument_index).is_loadable_constant(), 2782 _cp->tag_at(argument_index).is_loadable_constant(),
2872 "argument_index %u has bad constant type in class file %s", 2783 "argument_index %u has bad constant type in class file %s",
2873 argument_index, 2784 argument_index,
2874 CHECK); 2785 CHECK);
2875 operands->at_put(operand_fill_index++, argument_index); 2786 operands->at_put(operand_fill_index++, argument_index);
2876 } 2787 }
2881 2792
2882 u1* current_end = cfs->current(); 2793 u1* current_end = cfs->current();
2883 guarantee_property(current_end == current_start + attribute_byte_length, 2794 guarantee_property(current_end == current_start + attribute_byte_length,
2884 "Bad length on BootstrapMethods in class file %s", 2795 "Bad length on BootstrapMethods in class file %s",
2885 CHECK); 2796 CHECK);
2886 2797 }
2887 cp->set_operands(operands); 2798
2888 } 2799 void ClassFileParser::parse_classfile_attributes(ClassFileParser::ClassAnnotationCollector* parsed_annotations,
2889
2890 void ClassFileParser::parse_classfile_attributes(ClassLoaderData* loader_data,
2891 constantPoolHandle cp,
2892 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
2893 TRAPS) { 2800 TRAPS) {
2894 ClassFileStream* cfs = stream(); 2801 ClassFileStream* cfs = stream();
2895 // Set inner classes attribute to default sentinel 2802 // Set inner classes attribute to default sentinel
2896 set_class_inner_classes(Universe::the_empty_short_array()); 2803 _inner_classes = Universe::the_empty_short_array();
2897 cfs->guarantee_more(2, CHECK); // attributes_count 2804 cfs->guarantee_more(2, CHECK); // attributes_count
2898 u2 attributes_count = cfs->get_u2_fast(); 2805 u2 attributes_count = cfs->get_u2_fast();
2899 bool parsed_sourcefile_attribute = false; 2806 bool parsed_sourcefile_attribute = false;
2900 bool parsed_innerclasses_attribute = false; 2807 bool parsed_innerclasses_attribute = false;
2901 bool parsed_enclosingmethod_attribute = false; 2808 bool parsed_enclosingmethod_attribute = false;
2916 while (attributes_count--) { 2823 while (attributes_count--) {
2917 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 2824 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
2918 u2 attribute_name_index = cfs->get_u2_fast(); 2825 u2 attribute_name_index = cfs->get_u2_fast();
2919 u4 attribute_length = cfs->get_u4_fast(); 2826 u4 attribute_length = cfs->get_u4_fast();
2920 check_property( 2827 check_property(
2921 valid_cp_range(attribute_name_index, cp->length()) && 2828 valid_symbol_at(attribute_name_index),
2922 cp->tag_at(attribute_name_index).is_utf8(),
2923 "Attribute name has bad constant pool index %u in class file %s", 2829 "Attribute name has bad constant pool index %u in class file %s",
2924 attribute_name_index, CHECK); 2830 attribute_name_index, CHECK);
2925 Symbol* tag = cp->symbol_at(attribute_name_index); 2831 Symbol* tag = _cp->symbol_at(attribute_name_index);
2926 if (tag == vmSymbols::tag_source_file()) { 2832 if (tag == vmSymbols::tag_source_file()) {
2927 // Check for SourceFile tag 2833 // Check for SourceFile tag
2928 if (_need_verify) { 2834 if (_need_verify) {
2929 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK); 2835 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
2930 } 2836 }
2931 if (parsed_sourcefile_attribute) { 2837 if (parsed_sourcefile_attribute) {
2932 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK); 2838 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
2933 } else { 2839 } else {
2934 parsed_sourcefile_attribute = true; 2840 parsed_sourcefile_attribute = true;
2935 } 2841 }
2936 parse_classfile_sourcefile_attribute(cp, CHECK); 2842 parse_classfile_sourcefile_attribute(CHECK);
2937 } else if (tag == vmSymbols::tag_source_debug_extension()) { 2843 } else if (tag == vmSymbols::tag_source_debug_extension()) {
2938 // Check for SourceDebugExtension tag 2844 // Check for SourceDebugExtension tag
2939 parse_classfile_source_debug_extension_attribute(cp, (int)attribute_length, CHECK); 2845 parse_classfile_source_debug_extension_attribute((int)attribute_length, CHECK);
2940 } else if (tag == vmSymbols::tag_inner_classes()) { 2846 } else if (tag == vmSymbols::tag_inner_classes()) {
2941 // Check for InnerClasses tag 2847 // Check for InnerClasses tag
2942 if (parsed_innerclasses_attribute) { 2848 if (parsed_innerclasses_attribute) {
2943 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK); 2849 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK);
2944 } else { 2850 } else {
2953 if (attribute_length != 0) { 2859 if (attribute_length != 0) {
2954 classfile_parse_error( 2860 classfile_parse_error(
2955 "Invalid Synthetic classfile attribute length %u in class file %s", 2861 "Invalid Synthetic classfile attribute length %u in class file %s",
2956 attribute_length, CHECK); 2862 attribute_length, CHECK);
2957 } 2863 }
2958 parse_classfile_synthetic_attribute(cp, CHECK); 2864 parse_classfile_synthetic_attribute(CHECK);
2959 } else if (tag == vmSymbols::tag_deprecated()) { 2865 } else if (tag == vmSymbols::tag_deprecated()) {
2960 // Check for Deprecatd tag - 4276120 2866 // Check for Deprecatd tag - 4276120
2961 if (attribute_length != 0) { 2867 if (attribute_length != 0) {
2962 classfile_parse_error( 2868 classfile_parse_error(
2963 "Invalid Deprecated classfile attribute length %u in class file %s", 2869 "Invalid Deprecated classfile attribute length %u in class file %s",
2968 if (attribute_length != 2) { 2874 if (attribute_length != 2) {
2969 classfile_parse_error( 2875 classfile_parse_error(
2970 "Wrong Signature attribute length %u in class file %s", 2876 "Wrong Signature attribute length %u in class file %s",
2971 attribute_length, CHECK); 2877 attribute_length, CHECK);
2972 } 2878 }
2973 parse_classfile_signature_attribute(cp, CHECK); 2879 parse_classfile_signature_attribute(CHECK);
2974 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { 2880 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
2975 runtime_visible_annotations_length = attribute_length; 2881 runtime_visible_annotations_length = attribute_length;
2976 runtime_visible_annotations = cfs->get_u1_buffer(); 2882 runtime_visible_annotations = cfs->get_u1_buffer();
2977 assert(runtime_visible_annotations != NULL, "null visible annotations"); 2883 assert(runtime_visible_annotations != NULL, "null visible annotations");
2978 parse_annotations(loader_data, 2884 parse_annotations(runtime_visible_annotations,
2979 runtime_visible_annotations,
2980 runtime_visible_annotations_length, 2885 runtime_visible_annotations_length,
2981 cp,
2982 parsed_annotations, 2886 parsed_annotations,
2983 CHECK); 2887 CHECK);
2984 cfs->skip_u1(runtime_visible_annotations_length, CHECK); 2888 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2985 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) { 2889 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) {
2986 runtime_invisible_annotations_length = attribute_length; 2890 runtime_invisible_annotations_length = attribute_length;
2998 enclosing_method_method_index = cfs->get_u2_fast(); 2902 enclosing_method_method_index = cfs->get_u2_fast();
2999 if (enclosing_method_class_index == 0) { 2903 if (enclosing_method_class_index == 0) {
3000 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK); 2904 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
3001 } 2905 }
3002 // Validate the constant pool indices and types 2906 // Validate the constant pool indices and types
3003 if (!cp->is_within_bounds(enclosing_method_class_index) || 2907 check_property(valid_klass_reference_at(enclosing_method_class_index),
3004 !is_klass_reference(cp, enclosing_method_class_index)) { 2908 "Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
3005 classfile_parse_error("Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
3006 }
3007 if (enclosing_method_method_index != 0 && 2909 if (enclosing_method_method_index != 0 &&
3008 (!cp->is_within_bounds(enclosing_method_method_index) || 2910 (!_cp->is_within_bounds(enclosing_method_method_index) ||
3009 !cp->tag_at(enclosing_method_method_index).is_name_and_type())) { 2911 !_cp->tag_at(enclosing_method_method_index).is_name_and_type())) {
3010 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK); 2912 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
3011 } 2913 }
3012 } else if (tag == vmSymbols::tag_bootstrap_methods() && 2914 } else if (tag == vmSymbols::tag_bootstrap_methods() &&
3013 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 2915 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
3014 if (parsed_bootstrap_methods_attribute) 2916 if (parsed_bootstrap_methods_attribute)
3015 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK); 2917 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK);
3016 parsed_bootstrap_methods_attribute = true; 2918 parsed_bootstrap_methods_attribute = true;
3017 parse_classfile_bootstrap_methods_attribute(loader_data, cp, attribute_length, CHECK); 2919 parse_classfile_bootstrap_methods_attribute(attribute_length, CHECK);
3018 } else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) { 2920 } else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) {
3019 runtime_visible_type_annotations_length = attribute_length; 2921 runtime_visible_type_annotations_length = attribute_length;
3020 runtime_visible_type_annotations = cfs->get_u1_buffer(); 2922 runtime_visible_type_annotations = cfs->get_u1_buffer();
3021 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 2923 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
3022 // No need for the VM to parse Type annotations 2924 // No need for the VM to parse Type annotations
3033 } else { 2935 } else {
3034 // Unknown attribute 2936 // Unknown attribute
3035 cfs->skip_u1(attribute_length, CHECK); 2937 cfs->skip_u1(attribute_length, CHECK);
3036 } 2938 }
3037 } 2939 }
3038 AnnotationArray* annotations = assemble_annotations(loader_data, 2940 _annotations = assemble_annotations(runtime_visible_annotations,
3039 runtime_visible_annotations, 2941 runtime_visible_annotations_length,
3040 runtime_visible_annotations_length, 2942 runtime_invisible_annotations,
3041 runtime_invisible_annotations, 2943 runtime_invisible_annotations_length,
3042 runtime_invisible_annotations_length, 2944 CHECK);
3043 CHECK); 2945 _type_annotations = assemble_annotations(runtime_visible_type_annotations,
3044 set_class_annotations(annotations); 2946 runtime_visible_type_annotations_length,
3045 AnnotationArray* type_annotations = assemble_annotations(loader_data, 2947 runtime_invisible_type_annotations,
3046 runtime_visible_type_annotations, 2948 runtime_invisible_type_annotations_length,
3047 runtime_visible_type_annotations_length, 2949 CHECK);
3048 runtime_invisible_type_annotations,
3049 runtime_invisible_type_annotations_length,
3050 CHECK);
3051 set_class_type_annotations(type_annotations);
3052 2950
3053 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { 2951 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {
3054 u2 num_of_classes = parse_classfile_inner_classes_attribute( 2952 u2 num_of_classes = parse_classfile_inner_classes_attribute(
3055 loader_data,
3056 inner_classes_attribute_start, 2953 inner_classes_attribute_start,
3057 parsed_innerclasses_attribute, 2954 parsed_innerclasses_attribute,
3058 enclosing_method_class_index, 2955 enclosing_method_class_index,
3059 enclosing_method_method_index, 2956 enclosing_method_method_index,
3060 cp, CHECK); 2957 CHECK);
3061 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) { 2958 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) {
3062 guarantee_property( 2959 guarantee_property(
3063 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, 2960 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
3064 "Wrong InnerClasses attribute length in class file %s", CHECK); 2961 "Wrong InnerClasses attribute length in class file %s", CHECK);
3065 } 2962 }
3083 k->set_generic_signature(_generic_signature); 2980 k->set_generic_signature(_generic_signature);
3084 } 2981 }
3085 if (_sde_buffer != NULL) { 2982 if (_sde_buffer != NULL) {
3086 k->set_source_debug_extension(_sde_buffer, _sde_length); 2983 k->set_source_debug_extension(_sde_buffer, _sde_length);
3087 } 2984 }
3088 k->set_inner_classes(_inner_classes); 2985 }
3089 } 2986
3090 2987 // Transfer ownership of metadata allocated to the InstanceKlass.
3091 AnnotationArray* ClassFileParser::assemble_annotations(ClassLoaderData* loader_data, 2988 void ClassFileParser::apply_parsed_class_metadata(
3092 u1* runtime_visible_annotations, 2989 instanceKlassHandle this_klass,
2990 int java_fields_count, TRAPS) {
2991 // Assign annotations if needed
2992 if (_annotations != NULL || _type_annotations != NULL ||
2993 _fields_annotations != NULL || _fields_type_annotations != NULL) {
2994 Annotations* annotations = Annotations::allocate(_loader_data, CHECK);
2995 annotations->set_class_annotations(_annotations);
2996 annotations->set_class_type_annotations(_type_annotations);
2997 annotations->set_fields_annotations(_fields_annotations);
2998 annotations->set_fields_type_annotations(_fields_type_annotations);
2999 this_klass->set_annotations(annotations);
3000 }
3001
3002 _cp->set_pool_holder(this_klass());
3003 this_klass->set_constants(_cp);
3004 this_klass->set_fields(_fields, java_fields_count);
3005 this_klass->set_methods(_methods);
3006 this_klass->set_inner_classes(_inner_classes);
3007 this_klass->set_local_interfaces(_local_interfaces);
3008 this_klass->set_transitive_interfaces(_transitive_interfaces);
3009
3010 // Clear out these fields so they don't get deallocated by the destructor
3011 clear_class_metadata();
3012 }
3013
3014 AnnotationArray* ClassFileParser::assemble_annotations(u1* runtime_visible_annotations,
3093 int runtime_visible_annotations_length, 3015 int runtime_visible_annotations_length,
3094 u1* runtime_invisible_annotations, 3016 u1* runtime_invisible_annotations,
3095 int runtime_invisible_annotations_length, TRAPS) { 3017 int runtime_invisible_annotations_length, TRAPS) {
3096 AnnotationArray* annotations = NULL; 3018 AnnotationArray* annotations = NULL;
3097 if (runtime_visible_annotations != NULL || 3019 if (runtime_visible_annotations != NULL ||
3098 runtime_invisible_annotations != NULL) { 3020 runtime_invisible_annotations != NULL) {
3099 annotations = MetadataFactory::new_array<u1>(loader_data, 3021 annotations = MetadataFactory::new_array<u1>(_loader_data,
3100 runtime_visible_annotations_length + 3022 runtime_visible_annotations_length +
3101 runtime_invisible_annotations_length, 3023 runtime_invisible_annotations_length,
3102 CHECK_(annotations)); 3024 CHECK_(annotations));
3103 if (runtime_visible_annotations != NULL) { 3025 if (runtime_visible_annotations != NULL) {
3104 for (int i = 0; i < runtime_visible_annotations_length; i++) { 3026 for (int i = 0; i < runtime_visible_annotations_length; i++) {
3140 method_spec->print_on(tty); 3062 method_spec->print_on(tty);
3141 } 3063 }
3142 } 3064 }
3143 } 3065 }
3144 #endif // ndef PRODUCT 3066 #endif // ndef PRODUCT
3067
3068
3069 instanceKlassHandle ClassFileParser::parse_super_class(int super_class_index,
3070 TRAPS) {
3071 instanceKlassHandle super_klass;
3072 if (super_class_index == 0) {
3073 check_property(_class_name == vmSymbols::java_lang_Object(),
3074 "Invalid superclass index %u in class file %s",
3075 super_class_index,
3076 CHECK_NULL);
3077 } else {
3078 check_property(valid_klass_reference_at(super_class_index),
3079 "Invalid superclass index %u in class file %s",
3080 super_class_index,
3081 CHECK_NULL);
3082 // The class name should be legal because it is checked when parsing constant pool.
3083 // However, make sure it is not an array type.
3084 bool is_array = false;
3085 if (_cp->tag_at(super_class_index).is_klass()) {
3086 super_klass = instanceKlassHandle(THREAD, _cp->resolved_klass_at(super_class_index));
3087 if (_need_verify)
3088 is_array = super_klass->oop_is_array();
3089 } else if (_need_verify) {
3090 is_array = (_cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
3091 }
3092 if (_need_verify) {
3093 guarantee_property(!is_array,
3094 "Bad superclass name in class file %s", CHECK_NULL);
3095 }
3096 }
3097 return super_klass;
3098 }
3099
3100
3101 // Values needed for oopmap and InstanceKlass creation
3102 class FieldLayoutInfo : public StackObj {
3103 public:
3104 int* nonstatic_oop_offsets;
3105 unsigned int* nonstatic_oop_counts;
3106 unsigned int nonstatic_oop_map_count;
3107 unsigned int total_oop_map_count;
3108 int instance_size;
3109 int nonstatic_field_size;
3110 int static_field_size;
3111 bool has_nonstatic_fields;
3112 };
3113
3114 // Layout fields and fill in FieldLayoutInfo. Could use more refactoring!
3115 void ClassFileParser::layout_fields(Handle class_loader,
3116 FieldAllocationCount* fac,
3117 ClassAnnotationCollector* parsed_annotations,
3118 FieldLayoutInfo* info,
3119 TRAPS) {
3120
3121 // get the padding width from the option
3122 // TODO: Ask VM about specific CPU we are running on
3123 int pad_size = ContendedPaddingWidth;
3124
3125 // Field size and offset computation
3126 int nonstatic_field_size = _super_klass() == NULL ? 0 : _super_klass()->nonstatic_field_size();
3127 #ifndef PRODUCT
3128 int orig_nonstatic_field_size = 0;
3129 #endif
3130 int next_static_oop_offset;
3131 int next_static_double_offset;
3132 int next_static_word_offset;
3133 int next_static_short_offset;
3134 int next_static_byte_offset;
3135 int next_nonstatic_oop_offset;
3136 int next_nonstatic_double_offset;
3137 int next_nonstatic_word_offset;
3138 int next_nonstatic_short_offset;
3139 int next_nonstatic_byte_offset;
3140 int next_nonstatic_type_offset;
3141 int first_nonstatic_oop_offset;
3142 int first_nonstatic_field_offset;
3143 int next_nonstatic_field_offset;
3144 int next_nonstatic_padded_offset;
3145
3146 // Count the contended fields by type.
3147 int nonstatic_contended_count = 0;
3148 FieldAllocationCount fac_contended;
3149 for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) {
3150 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3151 if (fs.is_contended()) {
3152 fac_contended.count[atype]++;
3153 if (!fs.access_flags().is_static()) {
3154 nonstatic_contended_count++;
3155 }
3156 }
3157 }
3158 int contended_count = nonstatic_contended_count;
3159
3160
3161 // Calculate the starting byte offsets
3162 next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields();
3163 next_static_double_offset = next_static_oop_offset +
3164 ((fac->count[STATIC_OOP]) * heapOopSize);
3165 if ( fac->count[STATIC_DOUBLE] &&
3166 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
3167 Universe::field_type_should_be_aligned(T_LONG)) ) {
3168 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
3169 }
3170
3171 next_static_word_offset = next_static_double_offset +
3172 ((fac->count[STATIC_DOUBLE]) * BytesPerLong);
3173 next_static_short_offset = next_static_word_offset +
3174 ((fac->count[STATIC_WORD]) * BytesPerInt);
3175 next_static_byte_offset = next_static_short_offset +
3176 ((fac->count[STATIC_SHORT]) * BytesPerShort);
3177
3178 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
3179 nonstatic_field_size * heapOopSize;
3180
3181 // class is contended, pad before all the fields
3182 if (parsed_annotations->is_contended()) {
3183 first_nonstatic_field_offset += pad_size;
3184 }
3185
3186 next_nonstatic_field_offset = first_nonstatic_field_offset;
3187
3188 unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE];
3189 unsigned int nonstatic_word_count = fac->count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD];
3190 unsigned int nonstatic_short_count = fac->count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT];
3191 unsigned int nonstatic_byte_count = fac->count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE];
3192 unsigned int nonstatic_oop_count = fac->count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP];
3193
3194 bool super_has_nonstatic_fields =
3195 (_super_klass() != NULL && _super_klass->has_nonstatic_fields());
3196 bool has_nonstatic_fields = super_has_nonstatic_fields ||
3197 ((nonstatic_double_count + nonstatic_word_count +
3198 nonstatic_short_count + nonstatic_byte_count +
3199 nonstatic_oop_count) != 0);
3200
3201
3202 // Prepare list of oops for oop map generation.
3203 int* nonstatic_oop_offsets;
3204 unsigned int* nonstatic_oop_counts;
3205 unsigned int nonstatic_oop_map_count = 0;
3206
3207 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3208 THREAD, int, nonstatic_oop_count + 1);
3209 nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
3210 THREAD, unsigned int, nonstatic_oop_count + 1);
3211
3212 first_nonstatic_oop_offset = 0; // will be set for first oop field
3213
3214 #ifndef PRODUCT
3215 if( PrintCompactFieldsSavings ) {
3216 next_nonstatic_double_offset = next_nonstatic_field_offset +
3217 (nonstatic_oop_count * heapOopSize);
3218 if ( nonstatic_double_count > 0 ) {
3219 next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
3220 }
3221 next_nonstatic_word_offset = next_nonstatic_double_offset +
3222 (nonstatic_double_count * BytesPerLong);
3223 next_nonstatic_short_offset = next_nonstatic_word_offset +
3224 (nonstatic_word_count * BytesPerInt);
3225 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3226 (nonstatic_short_count * BytesPerShort);
3227 next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset +
3228 nonstatic_byte_count ), heapOopSize );
3229 orig_nonstatic_field_size = nonstatic_field_size +
3230 ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
3231 }
3232 #endif
3233 bool compact_fields = CompactFields;
3234 int allocation_style = FieldsAllocationStyle;
3235 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3236 assert(false, "0 <= FieldsAllocationStyle <= 2");
3237 allocation_style = 1; // Optimistic
3238 }
3239
3240 // The next classes have predefined hard-coded fields offsets
3241 // (see in JavaClasses::compute_hard_coded_offsets()).
3242 // Use default fields allocation order for them.
3243 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
3244 (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
3245 _class_name == vmSymbols::java_lang_Class() ||
3246 _class_name == vmSymbols::java_lang_ClassLoader() ||
3247 _class_name == vmSymbols::java_lang_ref_Reference() ||
3248 _class_name == vmSymbols::java_lang_ref_SoftReference() ||
3249 _class_name == vmSymbols::java_lang_StackTraceElement() ||
3250 _class_name == vmSymbols::java_lang_String() ||
3251 _class_name == vmSymbols::java_lang_Throwable() ||
3252 _class_name == vmSymbols::java_lang_Boolean() ||
3253 _class_name == vmSymbols::java_lang_Character() ||
3254 _class_name == vmSymbols::java_lang_Float() ||
3255 _class_name == vmSymbols::java_lang_Double() ||
3256 _class_name == vmSymbols::java_lang_Byte() ||
3257 _class_name == vmSymbols::java_lang_Short() ||
3258 _class_name == vmSymbols::java_lang_Integer() ||
3259 _class_name == vmSymbols::java_lang_Long())) {
3260 allocation_style = 0; // Allocate oops first
3261 compact_fields = false; // Don't compact fields
3262 }
3263
3264 if( allocation_style == 0 ) {
3265 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
3266 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3267 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3268 (nonstatic_oop_count * heapOopSize);
3269 } else if( allocation_style == 1 ) {
3270 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
3271 next_nonstatic_double_offset = next_nonstatic_field_offset;
3272 } else if( allocation_style == 2 ) {
3273 // Fields allocation: oops fields in super and sub classes are together.
3274 if( nonstatic_field_size > 0 && _super_klass() != NULL &&
3275 _super_klass->nonstatic_oop_map_size() > 0 ) {
3276 unsigned int map_count = _super_klass->nonstatic_oop_map_count();
3277 OopMapBlock* first_map = _super_klass->start_of_nonstatic_oop_maps();
3278 OopMapBlock* last_map = first_map + map_count - 1;
3279 int next_offset = last_map->offset() + (last_map->count() * heapOopSize);
3280 if (next_offset == next_nonstatic_field_offset) {
3281 allocation_style = 0; // allocate oops first
3282 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3283 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3284 (nonstatic_oop_count * heapOopSize);
3285 }
3286 }
3287 if( allocation_style == 2 ) {
3288 allocation_style = 1; // allocate oops last
3289 next_nonstatic_double_offset = next_nonstatic_field_offset;
3290 }
3291 } else {
3292 ShouldNotReachHere();
3293 }
3294
3295 int nonstatic_oop_space_count = 0;
3296 int nonstatic_word_space_count = 0;
3297 int nonstatic_short_space_count = 0;
3298 int nonstatic_byte_space_count = 0;
3299 int nonstatic_oop_space_offset;
3300 int nonstatic_word_space_offset;
3301 int nonstatic_short_space_offset;
3302 int nonstatic_byte_space_offset;
3303
3304 if( nonstatic_double_count > 0 ) {
3305 int offset = next_nonstatic_double_offset;
3306 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong);
3307 if( compact_fields && offset != next_nonstatic_double_offset ) {
3308 // Allocate available fields into the gap before double field.
3309 int length = next_nonstatic_double_offset - offset;
3310 assert(length == BytesPerInt, "");
3311 nonstatic_word_space_offset = offset;
3312 if( nonstatic_word_count > 0 ) {
3313 nonstatic_word_count -= 1;
3314 nonstatic_word_space_count = 1; // Only one will fit
3315 length -= BytesPerInt;
3316 offset += BytesPerInt;
3317 }
3318 nonstatic_short_space_offset = offset;
3319 while( length >= BytesPerShort && nonstatic_short_count > 0 ) {
3320 nonstatic_short_count -= 1;
3321 nonstatic_short_space_count += 1;
3322 length -= BytesPerShort;
3323 offset += BytesPerShort;
3324 }
3325 nonstatic_byte_space_offset = offset;
3326 while( length > 0 && nonstatic_byte_count > 0 ) {
3327 nonstatic_byte_count -= 1;
3328 nonstatic_byte_space_count += 1;
3329 length -= 1;
3330 }
3331 // Allocate oop field in the gap if there are no other fields for that.
3332 nonstatic_oop_space_offset = offset;
3333 if( length >= heapOopSize && nonstatic_oop_count > 0 &&
3334 allocation_style != 0 ) { // when oop fields not first
3335 nonstatic_oop_count -= 1;
3336 nonstatic_oop_space_count = 1; // Only one will fit
3337 length -= heapOopSize;
3338 offset += heapOopSize;
3339 }
3340 }
3341 }
3342
3343 next_nonstatic_word_offset = next_nonstatic_double_offset +
3344 (nonstatic_double_count * BytesPerLong);
3345 next_nonstatic_short_offset = next_nonstatic_word_offset +
3346 (nonstatic_word_count * BytesPerInt);
3347 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3348 (nonstatic_short_count * BytesPerShort);
3349 next_nonstatic_padded_offset = next_nonstatic_byte_offset +
3350 nonstatic_byte_count;
3351
3352 // let oops jump before padding with this allocation style
3353 if( allocation_style == 1 ) {
3354 next_nonstatic_oop_offset = next_nonstatic_padded_offset;
3355 if( nonstatic_oop_count > 0 ) {
3356 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
3357 }
3358 next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
3359 }
3360
3361 // Iterate over fields again and compute correct offsets.
3362 // The field allocation type was temporarily stored in the offset slot.
3363 // oop fields are located before non-oop fields (static and non-static).
3364 for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) {
3365
3366 // skip already laid out fields
3367 if (fs.is_offset_set()) continue;
3368
3369 // contended instance fields are handled below
3370 if (fs.is_contended() && !fs.access_flags().is_static()) continue;
3371
3372 int real_offset;
3373 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3374
3375 // pack the rest of the fields
3376 switch (atype) {
3377 case STATIC_OOP:
3378 real_offset = next_static_oop_offset;
3379 next_static_oop_offset += heapOopSize;
3380 break;
3381 case STATIC_BYTE:
3382 real_offset = next_static_byte_offset;
3383 next_static_byte_offset += 1;
3384 break;
3385 case STATIC_SHORT:
3386 real_offset = next_static_short_offset;
3387 next_static_short_offset += BytesPerShort;
3388 break;
3389 case STATIC_WORD:
3390 real_offset = next_static_word_offset;
3391 next_static_word_offset += BytesPerInt;
3392 break;
3393 case STATIC_DOUBLE:
3394 real_offset = next_static_double_offset;
3395 next_static_double_offset += BytesPerLong;
3396 break;
3397 case NONSTATIC_OOP:
3398 if( nonstatic_oop_space_count > 0 ) {
3399 real_offset = nonstatic_oop_space_offset;
3400 nonstatic_oop_space_offset += heapOopSize;
3401 nonstatic_oop_space_count -= 1;
3402 } else {
3403 real_offset = next_nonstatic_oop_offset;
3404 next_nonstatic_oop_offset += heapOopSize;
3405 }
3406 // Update oop maps
3407 if( nonstatic_oop_map_count > 0 &&
3408 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3409 real_offset -
3410 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
3411 heapOopSize ) {
3412 // Extend current oop map
3413 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
3414 } else {
3415 // Create new oop map
3416 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
3417 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
3418 nonstatic_oop_map_count += 1;
3419 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3420 first_nonstatic_oop_offset = real_offset;
3421 }
3422 }
3423 break;
3424 case NONSTATIC_BYTE:
3425 if( nonstatic_byte_space_count > 0 ) {
3426 real_offset = nonstatic_byte_space_offset;
3427 nonstatic_byte_space_offset += 1;
3428 nonstatic_byte_space_count -= 1;
3429 } else {
3430 real_offset = next_nonstatic_byte_offset;
3431 next_nonstatic_byte_offset += 1;
3432 }
3433 break;
3434 case NONSTATIC_SHORT:
3435 if( nonstatic_short_space_count > 0 ) {
3436 real_offset = nonstatic_short_space_offset;
3437 nonstatic_short_space_offset += BytesPerShort;
3438 nonstatic_short_space_count -= 1;
3439 } else {
3440 real_offset = next_nonstatic_short_offset;
3441 next_nonstatic_short_offset += BytesPerShort;
3442 }
3443 break;
3444 case NONSTATIC_WORD:
3445 if( nonstatic_word_space_count > 0 ) {
3446 real_offset = nonstatic_word_space_offset;
3447 nonstatic_word_space_offset += BytesPerInt;
3448 nonstatic_word_space_count -= 1;
3449 } else {
3450 real_offset = next_nonstatic_word_offset;
3451 next_nonstatic_word_offset += BytesPerInt;
3452 }
3453 break;
3454 case NONSTATIC_DOUBLE:
3455 real_offset = next_nonstatic_double_offset;
3456 next_nonstatic_double_offset += BytesPerLong;
3457 break;
3458 default:
3459 ShouldNotReachHere();
3460 }
3461 fs.set_offset(real_offset);
3462 }
3463
3464
3465 // Handle the contended cases.
3466 //
3467 // Each contended field should not intersect the cache line with another contended field.
3468 // In the absence of alignment information, we end up with pessimistically separating
3469 // the fields with full-width padding.
3470 //
3471 // Additionally, this should not break alignment for the fields, so we round the alignment up
3472 // for each field.
3473 if (contended_count > 0) {
3474
3475 // if there is at least one contended field, we need to have pre-padding for them
3476 if (nonstatic_contended_count > 0) {
3477 next_nonstatic_padded_offset += pad_size;
3478 }
3479
3480 // collect all contended groups
3481 BitMap bm(_cp->size());
3482 for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) {
3483 // skip already laid out fields
3484 if (fs.is_offset_set()) continue;
3485
3486 if (fs.is_contended()) {
3487 bm.set_bit(fs.contended_group());
3488 }
3489 }
3490
3491 int current_group = -1;
3492 while ((current_group = (int)bm.get_next_one_offset(current_group + 1)) != (int)bm.size()) {
3493
3494 for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) {
3495
3496 // skip already laid out fields
3497 if (fs.is_offset_set()) continue;
3498
3499 // skip non-contended fields and fields from different group
3500 if (!fs.is_contended() || (fs.contended_group() != current_group)) continue;
3501
3502 // handle statics below
3503 if (fs.access_flags().is_static()) continue;
3504
3505 int real_offset;
3506 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3507
3508 switch (atype) {
3509 case NONSTATIC_BYTE:
3510 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, 1);
3511 real_offset = next_nonstatic_padded_offset;
3512 next_nonstatic_padded_offset += 1;
3513 break;
3514
3515 case NONSTATIC_SHORT:
3516 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerShort);
3517 real_offset = next_nonstatic_padded_offset;
3518 next_nonstatic_padded_offset += BytesPerShort;
3519 break;
3520
3521 case NONSTATIC_WORD:
3522 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerInt);
3523 real_offset = next_nonstatic_padded_offset;
3524 next_nonstatic_padded_offset += BytesPerInt;
3525 break;
3526
3527 case NONSTATIC_DOUBLE:
3528 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerLong);
3529 real_offset = next_nonstatic_padded_offset;
3530 next_nonstatic_padded_offset += BytesPerLong;
3531 break;
3532
3533 case NONSTATIC_OOP:
3534 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, heapOopSize);
3535 real_offset = next_nonstatic_padded_offset;
3536 next_nonstatic_padded_offset += heapOopSize;
3537
3538 // Create new oop map
3539 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
3540 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
3541 nonstatic_oop_map_count += 1;
3542 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3543 first_nonstatic_oop_offset = real_offset;
3544 }
3545 break;
3546
3547 default:
3548 ShouldNotReachHere();
3549 }
3550
3551 if (fs.contended_group() == 0) {
3552 // Contended group defines the equivalence class over the fields:
3553 // the fields within the same contended group are not inter-padded.
3554 // The only exception is default group, which does not incur the
3555 // equivalence, and so requires intra-padding.
3556 next_nonstatic_padded_offset += pad_size;
3557 }
3558
3559 fs.set_offset(real_offset);
3560 } // for
3561
3562 // Start laying out the next group.
3563 // Note that this will effectively pad the last group in the back;
3564 // this is expected to alleviate memory contention effects for
3565 // subclass fields and/or adjacent object.
3566 // If this was the default group, the padding is already in place.
3567 if (current_group != 0) {
3568 next_nonstatic_padded_offset += pad_size;
3569 }
3570 }
3571
3572 // handle static fields
3573 }
3574
3575 // Size of instances
3576 int notaligned_offset = next_nonstatic_padded_offset;
3577
3578 // Entire class is contended, pad in the back.
3579 // This helps to alleviate memory contention effects for subclass fields
3580 // and/or adjacent object.
3581 if (parsed_annotations->is_contended()) {
3582 notaligned_offset += pad_size;
3583 }
3584
3585 int next_static_type_offset = align_size_up(next_static_byte_offset, wordSize);
3586 int static_field_size = (next_static_type_offset -
3587 InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
3588
3589 next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
3590 nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
3591 - first_nonstatic_field_offset)/heapOopSize);
3592
3593 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3594 int instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3595
3596 assert(instance_size == align_object_size(align_size_up(
3597 (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize + ((parsed_annotations->is_contended()) ? pad_size : 0)),
3598 wordSize) / wordSize), "consistent layout helper value");
3599
3600 // Number of non-static oop map blocks allocated at end of klass.
3601 const unsigned int total_oop_map_count =
3602 compute_oop_map_count(_super_klass, nonstatic_oop_map_count,
3603 first_nonstatic_oop_offset);
3604
3605 #ifndef PRODUCT
3606 if( PrintCompactFieldsSavings ) {
3607 ResourceMark rm;
3608 if( nonstatic_field_size < orig_nonstatic_field_size ) {
3609 tty->print("[Saved %d of %d bytes in %s]\n",
3610 (orig_nonstatic_field_size - nonstatic_field_size)*heapOopSize,
3611 orig_nonstatic_field_size*heapOopSize,
3612 _class_name);
3613 } else if( nonstatic_field_size > orig_nonstatic_field_size ) {
3614 tty->print("[Wasted %d over %d bytes in %s]\n",
3615 (nonstatic_field_size - orig_nonstatic_field_size)*heapOopSize,
3616 orig_nonstatic_field_size*heapOopSize,
3617 _class_name);
3618 }
3619 }
3620
3621 if (PrintFieldLayout) {
3622 print_field_layout(_class_name,
3623 _fields,
3624 _cp,
3625 instance_size,
3626 first_nonstatic_field_offset,
3627 next_nonstatic_field_offset,
3628 next_static_type_offset);
3629 }
3630
3631 #endif
3632 // Pass back information needed for InstanceKlass creation
3633 info->nonstatic_oop_offsets = nonstatic_oop_offsets;
3634 info->nonstatic_oop_counts = nonstatic_oop_counts;
3635 info->nonstatic_oop_map_count = nonstatic_oop_map_count;
3636 info->total_oop_map_count = total_oop_map_count;
3637 info->instance_size = instance_size;
3638 info->static_field_size = static_field_size;
3639 info->nonstatic_field_size = nonstatic_field_size;
3640 info->has_nonstatic_fields = has_nonstatic_fields;
3641 }
3145 3642
3146 3643
3147 instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name, 3644 instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name,
3148 ClassLoaderData* loader_data, 3645 ClassLoaderData* loader_data,
3149 Handle protection_domain, 3646 Handle protection_domain,
3174 NULL, 3671 NULL,
3175 jt->get_thread_stat()->perf_recursion_counts_addr(), 3672 jt->get_thread_stat()->perf_recursion_counts_addr(),
3176 jt->get_thread_stat()->perf_timers_addr(), 3673 jt->get_thread_stat()->perf_timers_addr(),
3177 PerfClassTraceTime::PARSE_CLASS); 3674 PerfClassTraceTime::PARSE_CLASS);
3178 3675
3179 init_parsed_class_attributes(); 3676 init_parsed_class_attributes(loader_data);
3180 3677
3181 if (JvmtiExport::should_post_class_file_load_hook()) { 3678 if (JvmtiExport::should_post_class_file_load_hook()) {
3182 // Get the cached class file bytes (if any) from the class that 3679 // Get the cached class file bytes (if any) from the class that
3183 // is being redefined or retransformed. We use jvmti_thread_state() 3680 // is being redefined or retransformed. We use jvmti_thread_state()
3184 // instead of JvmtiThreadState::state_for(jt) so we don't allocate 3681 // instead of JvmtiThreadState::state_for(jt) so we don't allocate
3269 // Check if verification needs to be relaxed for this class file 3766 // Check if verification needs to be relaxed for this class file
3270 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376) 3767 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
3271 _relax_verify = Verifier::relax_verify_for(class_loader()); 3768 _relax_verify = Verifier::relax_verify_for(class_loader());
3272 3769
3273 // Constant pool 3770 // Constant pool
3274 constantPoolHandle cp = parse_constant_pool(loader_data, CHECK_(nullHandle)); 3771 constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle));
3275 ConstantPoolCleaner error_handler(cp); // set constant pool to be cleaned up.
3276 3772
3277 int cp_size = cp->length(); 3773 int cp_size = cp->length();
3278 3774
3279 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len 3775 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
3280 3776
3288 } 3784 }
3289 verify_legal_class_modifiers(flags, CHECK_(nullHandle)); 3785 verify_legal_class_modifiers(flags, CHECK_(nullHandle));
3290 access_flags.set_flags(flags); 3786 access_flags.set_flags(flags);
3291 3787
3292 // This class and superclass 3788 // This class and superclass
3293 instanceKlassHandle super_klass;
3294 u2 this_class_index = cfs->get_u2_fast(); 3789 u2 this_class_index = cfs->get_u2_fast();
3295 check_property( 3790 check_property(
3296 valid_cp_range(this_class_index, cp_size) && 3791 valid_cp_range(this_class_index, cp_size) &&
3297 cp->tag_at(this_class_index).is_unresolved_klass(), 3792 cp->tag_at(this_class_index).is_unresolved_klass(),
3298 "Invalid this class index %u in constant pool in class file %s", 3793 "Invalid this class index %u in constant pool in class file %s",
3343 if (cfs->source() != NULL) tty->print(" from %s", cfs->source()); 3838 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
3344 tty->print_cr("]"); 3839 tty->print_cr("]");
3345 } 3840 }
3346 3841
3347 u2 super_class_index = cfs->get_u2_fast(); 3842 u2 super_class_index = cfs->get_u2_fast();
3348 if (super_class_index == 0) { 3843 instanceKlassHandle super_klass = parse_super_class(super_class_index,
3349 check_property(class_name == vmSymbols::java_lang_Object(), 3844 CHECK_NULL);
3350 "Invalid superclass index %u in class file %s",
3351 super_class_index,
3352 CHECK_(nullHandle));
3353 } else {
3354 check_property(valid_cp_range(super_class_index, cp_size) &&
3355 is_klass_reference(cp, super_class_index),
3356 "Invalid superclass index %u in class file %s",
3357 super_class_index,
3358 CHECK_(nullHandle));
3359 // The class name should be legal because it is checked when parsing constant pool.
3360 // However, make sure it is not an array type.
3361 bool is_array = false;
3362 if (cp->tag_at(super_class_index).is_klass()) {
3363 super_klass = instanceKlassHandle(THREAD, cp->resolved_klass_at(super_class_index));
3364 if (_need_verify)
3365 is_array = super_klass->oop_is_array();
3366 } else if (_need_verify) {
3367 is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
3368 }
3369 if (_need_verify) {
3370 guarantee_property(!is_array,
3371 "Bad superclass name in class file %s", CHECK_(nullHandle));
3372 }
3373 }
3374 3845
3375 // Interfaces 3846 // Interfaces
3376 u2 itfs_len = cfs->get_u2_fast(); 3847 u2 itfs_len = cfs->get_u2_fast();
3377 Array<Klass*>* local_interfaces; 3848 Array<Klass*>* local_interfaces =
3378 if (itfs_len == 0) { 3849 parse_interfaces(itfs_len, protection_domain, _class_name,
3379 local_interfaces = Universe::the_empty_klass_array(); 3850 &has_default_methods, CHECK_(nullHandle));
3380 } else {
3381 local_interfaces = parse_interfaces(
3382 cp, itfs_len, loader_data, protection_domain, _class_name,
3383 &has_default_methods, CHECK_(nullHandle));
3384 }
3385 3851
3386 u2 java_fields_count = 0; 3852 u2 java_fields_count = 0;
3387 // Fields (offsets are filled in later) 3853 // Fields (offsets are filled in later)
3388 FieldAllocationCount fac; 3854 FieldAllocationCount fac;
3389 Array<AnnotationArray*>* fields_annotations = NULL; 3855 Array<u2>* fields = parse_fields(class_name,
3390 Array<AnnotationArray*>* fields_type_annotations = NULL; 3856 access_flags.is_interface(),
3391 Array<u2>* fields = parse_fields(loader_data, class_name, cp, access_flags.is_interface(), &fac, &fields_annotations, 3857 &fac, &java_fields_count,
3392 &fields_type_annotations, 3858 CHECK_(nullHandle));
3393 &java_fields_count,
3394 CHECK_(nullHandle));
3395 // Methods 3859 // Methods
3396 bool has_final_method = false; 3860 bool has_final_method = false;
3397 AccessFlags promoted_flags; 3861 AccessFlags promoted_flags;
3398 promoted_flags.set_flags(0); 3862 promoted_flags.set_flags(0);
3399 Array<Method*>* methods = parse_methods(loader_data, 3863 Array<Method*>* methods = parse_methods(access_flags.is_interface(),
3400 cp, access_flags.is_interface(),
3401 &promoted_flags, 3864 &promoted_flags,
3402 &has_final_method, 3865 &has_final_method,
3403 &has_default_methods, 3866 &has_default_methods,
3404 CHECK_(nullHandle)); 3867 CHECK_(nullHandle));
3405 3868
3406 // Additional attributes 3869 // Additional attributes
3407 ClassAnnotationCollector parsed_annotations; 3870 ClassAnnotationCollector parsed_annotations;
3408 parse_classfile_attributes(loader_data, cp, &parsed_annotations, CHECK_(nullHandle)); 3871 parse_classfile_attributes(&parsed_annotations, CHECK_(nullHandle));
3409 3872
3410 // Make sure this is the end of class file stream 3873 // Make sure this is the end of class file stream
3411 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle)); 3874 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3412 3875
3413 // We check super class after class file is parsed and format is checked 3876 // We check super class after class file is parsed and format is checked
3450 if (super_klass->is_final()) { 3913 if (super_klass->is_final()) {
3451 THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle); 3914 THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle);
3452 } 3915 }
3453 } 3916 }
3454 3917
3918 // save super klass for error handling.
3919 _super_klass = super_klass;
3920
3455 // Compute the transitive list of all unique interfaces implemented by this class 3921 // Compute the transitive list of all unique interfaces implemented by this class
3456 Array<Klass*>* transitive_interfaces = compute_transitive_interfaces(loader_data, super_klass, local_interfaces, CHECK_(nullHandle)); 3922 _transitive_interfaces =
3923 compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle));
3457 3924
3458 // sort methods 3925 // sort methods
3459 Array<int>* method_ordering = sort_methods(loader_data, 3926 intArray* method_ordering = sort_methods(methods);
3460 methods,
3461 CHECK_(nullHandle));
3462 3927
3463 // promote flags from parse_methods() to the klass' flags 3928 // promote flags from parse_methods() to the klass' flags
3464 access_flags.add_promoted_flags(promoted_flags.as_int()); 3929 access_flags.add_promoted_flags(promoted_flags.as_int());
3465 3930
3466 // Size of Java vtable (in words) 3931 // Size of Java vtable (in words)
3474 &vtable_size, &num_miranda_methods, &all_mirandas, super_klass(), methods, 3939 &vtable_size, &num_miranda_methods, &all_mirandas, super_klass(), methods,
3475 access_flags, class_loader, class_name, local_interfaces, 3940 access_flags, class_loader, class_name, local_interfaces,
3476 CHECK_(nullHandle)); 3941 CHECK_(nullHandle));
3477 3942
3478 // Size of Java itable (in words) 3943 // Size of Java itable (in words)
3479 itable_size = access_flags.is_interface() ? 0 : klassItable::compute_itable_size(transitive_interfaces); 3944 itable_size = access_flags.is_interface() ? 0 : klassItable::compute_itable_size(_transitive_interfaces);
3480 3945
3481 // get the padding width from the option 3946 FieldLayoutInfo info;
3482 // TODO: Ask VM about specific CPU we are running on 3947 layout_fields(class_loader, &fac, &parsed_annotations, &info, CHECK_NULL);
3483 int pad_size = ContendedPaddingWidth; 3948
3484 3949 int total_oop_map_size2 =
3485 // Field size and offset computation 3950 InstanceKlass::nonstatic_oop_map_size(info.total_oop_map_count);
3486 int nonstatic_field_size = super_klass() == NULL ? 0 : super_klass->nonstatic_field_size(); 3951
3487 #ifndef PRODUCT
3488 int orig_nonstatic_field_size = 0;
3489 #endif
3490 int next_static_oop_offset;
3491 int next_static_double_offset;
3492 int next_static_word_offset;
3493 int next_static_short_offset;
3494 int next_static_byte_offset;
3495 int next_nonstatic_oop_offset;
3496 int next_nonstatic_double_offset;
3497 int next_nonstatic_word_offset;
3498 int next_nonstatic_short_offset;
3499 int next_nonstatic_byte_offset;
3500 int next_nonstatic_type_offset;
3501 int first_nonstatic_oop_offset;
3502 int first_nonstatic_field_offset;
3503 int next_nonstatic_field_offset;
3504 int next_nonstatic_padded_offset;
3505
3506 // Count the contended fields by type.
3507 int nonstatic_contended_count = 0;
3508 FieldAllocationCount fac_contended;
3509 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3510 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3511 if (fs.is_contended()) {
3512 fac_contended.count[atype]++;
3513 if (!fs.access_flags().is_static()) {
3514 nonstatic_contended_count++;
3515 }
3516 }
3517 }
3518 int contended_count = nonstatic_contended_count;
3519
3520
3521 // Calculate the starting byte offsets
3522 next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields();
3523
3524 next_static_double_offset = next_static_oop_offset +
3525 ((fac.count[STATIC_OOP]) * heapOopSize);
3526 if ( fac.count[STATIC_DOUBLE] &&
3527 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
3528 Universe::field_type_should_be_aligned(T_LONG)) ) {
3529 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
3530 }
3531
3532 next_static_word_offset = next_static_double_offset +
3533 ((fac.count[STATIC_DOUBLE]) * BytesPerLong);
3534 next_static_short_offset = next_static_word_offset +
3535 ((fac.count[STATIC_WORD]) * BytesPerInt);
3536 next_static_byte_offset = next_static_short_offset +
3537 ((fac.count[STATIC_SHORT]) * BytesPerShort);
3538
3539 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
3540 nonstatic_field_size * heapOopSize;
3541
3542 // class is contended, pad before all the fields
3543 if (parsed_annotations.is_contended()) {
3544 first_nonstatic_field_offset += pad_size;
3545 }
3546
3547 next_nonstatic_field_offset = first_nonstatic_field_offset;
3548
3549 unsigned int nonstatic_double_count = fac.count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE];
3550 unsigned int nonstatic_word_count = fac.count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD];
3551 unsigned int nonstatic_short_count = fac.count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT];
3552 unsigned int nonstatic_byte_count = fac.count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE];
3553 unsigned int nonstatic_oop_count = fac.count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP];
3554
3555 bool super_has_nonstatic_fields =
3556 (super_klass() != NULL && super_klass->has_nonstatic_fields());
3557 bool has_nonstatic_fields = super_has_nonstatic_fields ||
3558 ((nonstatic_double_count + nonstatic_word_count +
3559 nonstatic_short_count + nonstatic_byte_count +
3560 nonstatic_oop_count) != 0);
3561
3562
3563 // Prepare list of oops for oop map generation.
3564 int* nonstatic_oop_offsets;
3565 unsigned int* nonstatic_oop_counts;
3566 unsigned int nonstatic_oop_map_count = 0;
3567
3568 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3569 THREAD, int, nonstatic_oop_count + 1);
3570 nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
3571 THREAD, unsigned int, nonstatic_oop_count + 1);
3572
3573 first_nonstatic_oop_offset = 0; // will be set for first oop field
3574
3575 #ifndef PRODUCT
3576 if( PrintCompactFieldsSavings ) {
3577 next_nonstatic_double_offset = next_nonstatic_field_offset +
3578 (nonstatic_oop_count * heapOopSize);
3579 if ( nonstatic_double_count > 0 ) {
3580 next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
3581 }
3582 next_nonstatic_word_offset = next_nonstatic_double_offset +
3583 (nonstatic_double_count * BytesPerLong);
3584 next_nonstatic_short_offset = next_nonstatic_word_offset +
3585 (nonstatic_word_count * BytesPerInt);
3586 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3587 (nonstatic_short_count * BytesPerShort);
3588 next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset +
3589 nonstatic_byte_count ), heapOopSize );
3590 orig_nonstatic_field_size = nonstatic_field_size +
3591 ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
3592 }
3593 #endif
3594 bool compact_fields = CompactFields;
3595 int allocation_style = FieldsAllocationStyle;
3596 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3597 assert(false, "0 <= FieldsAllocationStyle <= 2");
3598 allocation_style = 1; // Optimistic
3599 }
3600
3601 // The next classes have predefined hard-coded fields offsets
3602 // (see in JavaClasses::compute_hard_coded_offsets()).
3603 // Use default fields allocation order for them.
3604 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
3605 (class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
3606 class_name == vmSymbols::java_lang_Class() ||
3607 class_name == vmSymbols::java_lang_ClassLoader() ||
3608 class_name == vmSymbols::java_lang_ref_Reference() ||
3609 class_name == vmSymbols::java_lang_ref_SoftReference() ||
3610 class_name == vmSymbols::java_lang_StackTraceElement() ||
3611 class_name == vmSymbols::java_lang_String() ||
3612 class_name == vmSymbols::java_lang_Throwable() ||
3613 class_name == vmSymbols::java_lang_Boolean() ||
3614 class_name == vmSymbols::java_lang_Character() ||
3615 class_name == vmSymbols::java_lang_Float() ||
3616 class_name == vmSymbols::java_lang_Double() ||
3617 class_name == vmSymbols::java_lang_Byte() ||
3618 class_name == vmSymbols::java_lang_Short() ||
3619 class_name == vmSymbols::java_lang_Integer() ||
3620 class_name == vmSymbols::java_lang_Long())) {
3621 allocation_style = 0; // Allocate oops first
3622 compact_fields = false; // Don't compact fields
3623 }
3624
3625 if( allocation_style == 0 ) {
3626 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
3627 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3628 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3629 (nonstatic_oop_count * heapOopSize);
3630 } else if( allocation_style == 1 ) {
3631 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
3632 next_nonstatic_double_offset = next_nonstatic_field_offset;
3633 } else if( allocation_style == 2 ) {
3634 // Fields allocation: oops fields in super and sub classes are together.
3635 if( nonstatic_field_size > 0 && super_klass() != NULL &&
3636 super_klass->nonstatic_oop_map_size() > 0 ) {
3637 int map_count = super_klass->nonstatic_oop_map_count();
3638 OopMapBlock* first_map = super_klass->start_of_nonstatic_oop_maps();
3639 OopMapBlock* last_map = first_map + map_count - 1;
3640 int next_offset = last_map->offset() + (last_map->count() * heapOopSize);
3641 if (next_offset == next_nonstatic_field_offset) {
3642 allocation_style = 0; // allocate oops first
3643 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3644 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3645 (nonstatic_oop_count * heapOopSize);
3646 }
3647 }
3648 if( allocation_style == 2 ) {
3649 allocation_style = 1; // allocate oops last
3650 next_nonstatic_double_offset = next_nonstatic_field_offset;
3651 }
3652 } else {
3653 ShouldNotReachHere();
3654 }
3655
3656 int nonstatic_oop_space_count = 0;
3657 int nonstatic_word_space_count = 0;
3658 int nonstatic_short_space_count = 0;
3659 int nonstatic_byte_space_count = 0;
3660 int nonstatic_oop_space_offset;
3661 int nonstatic_word_space_offset;
3662 int nonstatic_short_space_offset;
3663 int nonstatic_byte_space_offset;
3664
3665 if( nonstatic_double_count > 0 ) {
3666 int offset = next_nonstatic_double_offset;
3667 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong);
3668 if( compact_fields && offset != next_nonstatic_double_offset ) {
3669 // Allocate available fields into the gap before double field.
3670 int length = next_nonstatic_double_offset - offset;
3671 assert(length == BytesPerInt, "");
3672 nonstatic_word_space_offset = offset;
3673 if( nonstatic_word_count > 0 ) {
3674 nonstatic_word_count -= 1;
3675 nonstatic_word_space_count = 1; // Only one will fit
3676 length -= BytesPerInt;
3677 offset += BytesPerInt;
3678 }
3679 nonstatic_short_space_offset = offset;
3680 while( length >= BytesPerShort && nonstatic_short_count > 0 ) {
3681 nonstatic_short_count -= 1;
3682 nonstatic_short_space_count += 1;
3683 length -= BytesPerShort;
3684 offset += BytesPerShort;
3685 }
3686 nonstatic_byte_space_offset = offset;
3687 while( length > 0 && nonstatic_byte_count > 0 ) {
3688 nonstatic_byte_count -= 1;
3689 nonstatic_byte_space_count += 1;
3690 length -= 1;
3691 }
3692 // Allocate oop field in the gap if there are no other fields for that.
3693 nonstatic_oop_space_offset = offset;
3694 if( length >= heapOopSize && nonstatic_oop_count > 0 &&
3695 allocation_style != 0 ) { // when oop fields not first
3696 nonstatic_oop_count -= 1;
3697 nonstatic_oop_space_count = 1; // Only one will fit
3698 length -= heapOopSize;
3699 offset += heapOopSize;
3700 }
3701 }
3702 }
3703
3704 next_nonstatic_word_offset = next_nonstatic_double_offset +
3705 (nonstatic_double_count * BytesPerLong);
3706 next_nonstatic_short_offset = next_nonstatic_word_offset +
3707 (nonstatic_word_count * BytesPerInt);
3708 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3709 (nonstatic_short_count * BytesPerShort);
3710 next_nonstatic_padded_offset = next_nonstatic_byte_offset +
3711 nonstatic_byte_count;
3712
3713 // let oops jump before padding with this allocation style
3714 if( allocation_style == 1 ) {
3715 next_nonstatic_oop_offset = next_nonstatic_padded_offset;
3716 if( nonstatic_oop_count > 0 ) {
3717 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
3718 }
3719 next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
3720 }
3721
3722 // Iterate over fields again and compute correct offsets.
3723 // The field allocation type was temporarily stored in the offset slot.
3724 // oop fields are located before non-oop fields (static and non-static).
3725 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3726
3727 // skip already laid out fields
3728 if (fs.is_offset_set()) continue;
3729
3730 // contended instance fields are handled below
3731 if (fs.is_contended() && !fs.access_flags().is_static()) continue;
3732
3733 int real_offset;
3734 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3735
3736 // pack the rest of the fields
3737 switch (atype) {
3738 case STATIC_OOP:
3739 real_offset = next_static_oop_offset;
3740 next_static_oop_offset += heapOopSize;
3741 break;
3742 case STATIC_BYTE:
3743 real_offset = next_static_byte_offset;
3744 next_static_byte_offset += 1;
3745 break;
3746 case STATIC_SHORT:
3747 real_offset = next_static_short_offset;
3748 next_static_short_offset += BytesPerShort;
3749 break;
3750 case STATIC_WORD:
3751 real_offset = next_static_word_offset;
3752 next_static_word_offset += BytesPerInt;
3753 break;
3754 case STATIC_DOUBLE:
3755 real_offset = next_static_double_offset;
3756 next_static_double_offset += BytesPerLong;
3757 break;
3758 case NONSTATIC_OOP:
3759 if( nonstatic_oop_space_count > 0 ) {
3760 real_offset = nonstatic_oop_space_offset;
3761 nonstatic_oop_space_offset += heapOopSize;
3762 nonstatic_oop_space_count -= 1;
3763 } else {
3764 real_offset = next_nonstatic_oop_offset;
3765 next_nonstatic_oop_offset += heapOopSize;
3766 }
3767 // Update oop maps
3768 if( nonstatic_oop_map_count > 0 &&
3769 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3770 real_offset -
3771 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
3772 heapOopSize ) {
3773 // Extend current oop map
3774 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
3775 } else {
3776 // Create new oop map
3777 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
3778 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
3779 nonstatic_oop_map_count += 1;
3780 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3781 first_nonstatic_oop_offset = real_offset;
3782 }
3783 }
3784 break;
3785 case NONSTATIC_BYTE:
3786 if( nonstatic_byte_space_count > 0 ) {
3787 real_offset = nonstatic_byte_space_offset;
3788 nonstatic_byte_space_offset += 1;
3789 nonstatic_byte_space_count -= 1;
3790 } else {
3791 real_offset = next_nonstatic_byte_offset;
3792 next_nonstatic_byte_offset += 1;
3793 }
3794 break;
3795 case NONSTATIC_SHORT:
3796 if( nonstatic_short_space_count > 0 ) {
3797 real_offset = nonstatic_short_space_offset;
3798 nonstatic_short_space_offset += BytesPerShort;
3799 nonstatic_short_space_count -= 1;
3800 } else {
3801 real_offset = next_nonstatic_short_offset;
3802 next_nonstatic_short_offset += BytesPerShort;
3803 }
3804 break;
3805 case NONSTATIC_WORD:
3806 if( nonstatic_word_space_count > 0 ) {
3807 real_offset = nonstatic_word_space_offset;
3808 nonstatic_word_space_offset += BytesPerInt;
3809 nonstatic_word_space_count -= 1;
3810 } else {
3811 real_offset = next_nonstatic_word_offset;
3812 next_nonstatic_word_offset += BytesPerInt;
3813 }
3814 break;
3815 case NONSTATIC_DOUBLE:
3816 real_offset = next_nonstatic_double_offset;
3817 next_nonstatic_double_offset += BytesPerLong;
3818 break;
3819 default:
3820 ShouldNotReachHere();
3821 }
3822 fs.set_offset(real_offset);
3823 }
3824
3825
3826 // Handle the contended cases.
3827 //
3828 // Each contended field should not intersect the cache line with another contended field.
3829 // In the absence of alignment information, we end up with pessimistically separating
3830 // the fields with full-width padding.
3831 //
3832 // Additionally, this should not break alignment for the fields, so we round the alignment up
3833 // for each field.
3834 if (contended_count > 0) {
3835
3836 // if there is at least one contended field, we need to have pre-padding for them
3837 if (nonstatic_contended_count > 0) {
3838 next_nonstatic_padded_offset += pad_size;
3839 }
3840
3841 // collect all contended groups
3842 BitMap bm(cp->size());
3843 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3844 // skip already laid out fields
3845 if (fs.is_offset_set()) continue;
3846
3847 if (fs.is_contended()) {
3848 bm.set_bit(fs.contended_group());
3849 }
3850 }
3851
3852 int current_group = -1;
3853 while ((current_group = (int)bm.get_next_one_offset(current_group + 1)) != (int)bm.size()) {
3854
3855 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3856
3857 // skip already laid out fields
3858 if (fs.is_offset_set()) continue;
3859
3860 // skip non-contended fields and fields from different group
3861 if (!fs.is_contended() || (fs.contended_group() != current_group)) continue;
3862
3863 // handle statics below
3864 if (fs.access_flags().is_static()) continue;
3865
3866 int real_offset;
3867 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3868
3869 switch (atype) {
3870 case NONSTATIC_BYTE:
3871 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, 1);
3872 real_offset = next_nonstatic_padded_offset;
3873 next_nonstatic_padded_offset += 1;
3874 break;
3875
3876 case NONSTATIC_SHORT:
3877 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerShort);
3878 real_offset = next_nonstatic_padded_offset;
3879 next_nonstatic_padded_offset += BytesPerShort;
3880 break;
3881
3882 case NONSTATIC_WORD:
3883 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerInt);
3884 real_offset = next_nonstatic_padded_offset;
3885 next_nonstatic_padded_offset += BytesPerInt;
3886 break;
3887
3888 case NONSTATIC_DOUBLE:
3889 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, BytesPerLong);
3890 real_offset = next_nonstatic_padded_offset;
3891 next_nonstatic_padded_offset += BytesPerLong;
3892 break;
3893
3894 case NONSTATIC_OOP:
3895 next_nonstatic_padded_offset = align_size_up(next_nonstatic_padded_offset, heapOopSize);
3896 real_offset = next_nonstatic_padded_offset;
3897 next_nonstatic_padded_offset += heapOopSize;
3898
3899 // Create new oop map
3900 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
3901 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
3902 nonstatic_oop_map_count += 1;
3903 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3904 first_nonstatic_oop_offset = real_offset;
3905 }
3906 break;
3907
3908 default:
3909 ShouldNotReachHere();
3910 }
3911
3912 if (fs.contended_group() == 0) {
3913 // Contended group defines the equivalence class over the fields:
3914 // the fields within the same contended group are not inter-padded.
3915 // The only exception is default group, which does not incur the
3916 // equivalence, and so requires intra-padding.
3917 next_nonstatic_padded_offset += pad_size;
3918 }
3919
3920 fs.set_offset(real_offset);
3921 } // for
3922
3923 // Start laying out the next group.
3924 // Note that this will effectively pad the last group in the back;
3925 // this is expected to alleviate memory contention effects for
3926 // subclass fields and/or adjacent object.
3927 // If this was the default group, the padding is already in place.
3928 if (current_group != 0) {
3929 next_nonstatic_padded_offset += pad_size;
3930 }
3931 }
3932
3933 // handle static fields
3934
3935 } // handle contended
3936
3937 // Size of instances
3938 int instance_size;
3939
3940 int notaligned_offset = next_nonstatic_padded_offset;
3941
3942 // Entire class is contended, pad in the back.
3943 // This helps to alleviate memory contention effects for subclass fields
3944 // and/or adjacent object.
3945 if (parsed_annotations.is_contended()) {
3946 notaligned_offset += pad_size;
3947 }
3948
3949 int next_static_type_offset = align_size_up(next_static_byte_offset, wordSize);
3950 int static_field_size = (next_static_type_offset -
3951 InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
3952
3953 next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
3954 nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
3955 - first_nonstatic_field_offset)/heapOopSize);
3956
3957 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3958 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3959
3960 assert(instance_size == align_object_size(align_size_up(
3961 (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize + ((parsed_annotations.is_contended()) ? pad_size : 0)),
3962 wordSize) / wordSize), "consistent layout helper value");
3963
3964 // Number of non-static oop map blocks allocated at end of klass.
3965 const unsigned int total_oop_map_count =
3966 compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3967 first_nonstatic_oop_offset);
3968 // Compute reference type 3952 // Compute reference type
3969 ReferenceType rt; 3953 ReferenceType rt;
3970 if (super_klass() == NULL) { 3954 if (super_klass() == NULL) {
3971 rt = REF_NONE; 3955 rt = REF_NONE;
3972 } else { 3956 } else {
3973 rt = super_klass->reference_type(); 3957 rt = super_klass->reference_type();
3974 } 3958 }
3975 3959
3976 // We can now create the basic Klass* for this klass 3960 // We can now create the basic Klass* for this klass
3977 int total_oop_map_size2 = 3961 _klass = InstanceKlass::allocate_instance_klass(loader_data,
3978 InstanceKlass::nonstatic_oop_map_size(total_oop_map_count); 3962 vtable_size,
3979 3963 itable_size,
3980 Klass* ik = InstanceKlass::allocate_instance_klass(loader_data, 3964 info.static_field_size,
3981 vtable_size, 3965 total_oop_map_size2,
3982 itable_size, 3966 rt,
3983 static_field_size, 3967 access_flags,
3984 total_oop_map_size2, 3968 name,
3985 rt, 3969 super_klass(),
3986 access_flags, 3970 !host_klass.is_null(),
3987 name, 3971 CHECK_(nullHandle));
3988 super_klass(), 3972 instanceKlassHandle this_klass (THREAD, _klass);
3989 !host_klass.is_null(), 3973
3990 CHECK_(nullHandle)); 3974 assert(this_klass->static_field_size() == info.static_field_size, "sanity");
3991 3975 assert(this_klass->nonstatic_oop_map_count() == info.total_oop_map_count,
3992 // Add all classes to our internal class loader list here,
3993 // including classes in the bootstrap (NULL) class loader.
3994 loader_data->add_class(ik);
3995
3996 instanceKlassHandle this_klass (THREAD, ik);
3997
3998 assert(this_klass->static_field_size() == static_field_size, "sanity");
3999 assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
4000 "sanity"); 3976 "sanity");
4001 3977
4002 // Fill in information already parsed 3978 // Fill in information already parsed
4003 this_klass->set_should_verify_class(verify); 3979 this_klass->set_should_verify_class(verify);
4004 jint lh = Klass::instance_layout_helper(instance_size, false); 3980 jint lh = Klass::instance_layout_helper(info.instance_size, false);
4005 this_klass->set_layout_helper(lh); 3981 this_klass->set_layout_helper(lh);
4006 assert(this_klass->oop_is_instance(), "layout is correct"); 3982 assert(this_klass->oop_is_instance(), "layout is correct");
4007 assert(this_klass->size_helper() == instance_size, "correct size_helper"); 3983 assert(this_klass->size_helper() == info.instance_size, "correct size_helper");
4008 // Not yet: supers are done below to support the new subtype-checking fields 3984 // Not yet: supers are done below to support the new subtype-checking fields
4009 //this_klass->set_super(super_klass()); 3985 //this_klass->set_super(super_klass());
4010 this_klass->set_class_loader_data(loader_data); 3986 this_klass->set_class_loader_data(loader_data);
4011 this_klass->set_nonstatic_field_size(nonstatic_field_size); 3987 this_klass->set_nonstatic_field_size(info.nonstatic_field_size);
4012 this_klass->set_has_nonstatic_fields(has_nonstatic_fields); 3988 this_klass->set_has_nonstatic_fields(info.has_nonstatic_fields);
4013 this_klass->set_static_oop_field_count(fac.count[STATIC_OOP]); 3989 this_klass->set_static_oop_field_count(fac.count[STATIC_OOP]);
4014 cp->set_pool_holder(this_klass()); 3990
4015 error_handler.set_in_error(false); // turn off error handler for cp 3991 apply_parsed_class_metadata(this_klass, java_fields_count, CHECK_NULL);
4016 this_klass->set_constants(cp()); 3992
4017 this_klass->set_local_interfaces(local_interfaces);
4018 this_klass->set_fields(fields, java_fields_count);
4019 this_klass->set_methods(methods);
4020 if (has_final_method) { 3993 if (has_final_method) {
4021 this_klass->set_has_final_method(); 3994 this_klass->set_has_final_method();
4022 } 3995 }
4023 this_klass->set_method_ordering(method_ordering); 3996 this_klass->copy_method_ordering(method_ordering, CHECK_NULL);
4024 // The InstanceKlass::_methods_jmethod_ids cache and the 3997 // The InstanceKlass::_methods_jmethod_ids cache and the
4025 // InstanceKlass::_methods_cached_itable_indices cache are 3998 // InstanceKlass::_methods_cached_itable_indices cache are
4026 // both managed on the assumption that the initial cache 3999 // both managed on the assumption that the initial cache
4027 // size is equal to the number of methods in the class. If 4000 // size is equal to the number of methods in the class. If
4028 // that changes, then InstanceKlass::idnum_can_increment() 4001 // that changes, then InstanceKlass::idnum_can_increment()
4030 this_klass->set_initial_method_idnum(methods->length()); 4003 this_klass->set_initial_method_idnum(methods->length());
4031 this_klass->set_name(cp->klass_name_at(this_class_index)); 4004 this_klass->set_name(cp->klass_name_at(this_class_index));
4032 if (is_anonymous()) // I am well known to myself 4005 if (is_anonymous()) // I am well known to myself
4033 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve 4006 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
4034 4007
4035 // Assign allocations if needed
4036 if (_annotations != NULL || _type_annotations != NULL ||
4037 fields_annotations != NULL || fields_type_annotations != NULL) {
4038 Annotations* annotations = Annotations::allocate(loader_data, CHECK_NULL);
4039 annotations->set_class_annotations(_annotations);
4040 annotations->set_class_type_annotations(_type_annotations);
4041 annotations->set_fields_annotations(fields_annotations);
4042 annotations->set_fields_type_annotations(fields_type_annotations);
4043 this_klass->set_annotations(annotations);
4044 }
4045
4046 this_klass->set_minor_version(minor_version); 4008 this_klass->set_minor_version(minor_version);
4047 this_klass->set_major_version(major_version); 4009 this_klass->set_major_version(major_version);
4048 this_klass->set_has_default_methods(has_default_methods); 4010 this_klass->set_has_default_methods(has_default_methods);
4049 4011
4050 // Set up Method*::intrinsic_id as soon as we know the names of methods. 4012 // Set up Method*::intrinsic_id as soon as we know the names of methods.
4075 // super class exists and this class inherited miranda methods 4037 // super class exists and this class inherited miranda methods
4076 ) { 4038 ) {
4077 this_klass->set_has_miranda_methods(); // then set a flag 4039 this_klass->set_has_miranda_methods(); // then set a flag
4078 } 4040 }
4079 4041
4080 this_klass->set_transitive_interfaces(transitive_interfaces);
4081
4082 // Fill in information needed to compute superclasses. 4042 // Fill in information needed to compute superclasses.
4083 this_klass->initialize_supers(super_klass(), CHECK_(nullHandle)); 4043 this_klass->initialize_supers(super_klass(), CHECK_(nullHandle));
4084 4044
4085 // Initialize itable offset tables 4045 // Initialize itable offset tables
4086 klassItable::setup_itable_offset_table(this_klass); 4046 klassItable::setup_itable_offset_table(this_klass);
4087 4047
4088 // Compute transitive closure of interfaces this class implements 4048 // Compute transitive closure of interfaces this class implements
4089 // Do final class setup 4049 // Do final class setup
4090 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts); 4050 fill_oop_maps(this_klass, info.nonstatic_oop_map_count, info.nonstatic_oop_offsets, info.nonstatic_oop_counts);
4091 4051
4092 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper 4052 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
4093 set_precomputed_flags(this_klass); 4053 set_precomputed_flags(this_klass);
4094 4054
4095 // reinitialize modifiers, using the InnerClasses attribute 4055 // reinitialize modifiers, using the InnerClasses attribute
4184 tty->print("RESOLVE %s %s (interface)\n", from, to); 4144 tty->print("RESOLVE %s %s (interface)\n", from, to);
4185 } 4145 }
4186 } 4146 }
4187 } 4147 }
4188 4148
4189 #ifndef PRODUCT
4190 if( PrintCompactFieldsSavings ) {
4191 ResourceMark rm;
4192 if( nonstatic_field_size < orig_nonstatic_field_size ) {
4193 tty->print("[Saved %d of %d bytes in %s]\n",
4194 (orig_nonstatic_field_size - nonstatic_field_size)*heapOopSize,
4195 orig_nonstatic_field_size*heapOopSize,
4196 this_klass->external_name());
4197 } else if( nonstatic_field_size > orig_nonstatic_field_size ) {
4198 tty->print("[Wasted %d over %d bytes in %s]\n",
4199 (nonstatic_field_size - orig_nonstatic_field_size)*heapOopSize,
4200 orig_nonstatic_field_size*heapOopSize,
4201 this_klass->external_name());
4202 }
4203 }
4204 #endif
4205
4206 #ifndef PRODUCT
4207 if (PrintFieldLayout) {
4208 print_field_layout(name,
4209 fields,
4210 cp,
4211 instance_size,
4212 first_nonstatic_field_offset,
4213 next_nonstatic_field_offset,
4214 next_static_type_offset);
4215 }
4216 #endif
4217
4218 // preserve result across HandleMark 4149 // preserve result across HandleMark
4219 preserve_this_klass = this_klass(); 4150 preserve_this_klass = this_klass();
4220 } 4151 }
4221 4152
4222 // Create new handle outside HandleMark (might be needed for 4153 // Create new handle outside HandleMark (might be needed for
4223 // Extended Class Redefinition) 4154 // Extended Class Redefinition)
4224 instanceKlassHandle this_klass (THREAD, preserve_this_klass); 4155 instanceKlassHandle this_klass (THREAD, preserve_this_klass);
4225 debug_only(this_klass->verify();) 4156 debug_only(this_klass->verify();)
4226 4157
4158 // Clear class if no error has occurred so destructor doesn't deallocate it
4159 _klass = NULL;
4227 return this_klass; 4160 return this_klass;
4161 }
4162
4163 // Destructor to clean up if there's an error
4164 ClassFileParser::~ClassFileParser() {
4165 MetadataFactory::free_metadata(_loader_data, _cp);
4166 MetadataFactory::free_array<u2>(_loader_data, _fields);
4167
4168 // Free methods
4169 InstanceKlass::deallocate_methods(_loader_data, _methods);
4170
4171 // beware of the Universe::empty_blah_array!!
4172 if (_inner_classes != Universe::the_empty_short_array()) {
4173 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
4174 }
4175
4176 // Free interfaces
4177 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass(),
4178 _local_interfaces, _transitive_interfaces);
4179
4180 MetadataFactory::free_array<u1>(_loader_data, _annotations);
4181 MetadataFactory::free_array<u1>(_loader_data, _type_annotations);
4182 Annotations::free_contents(_loader_data, _fields_annotations);
4183 Annotations::free_contents(_loader_data, _fields_type_annotations);
4184
4185 clear_class_metadata();
4186
4187 // deallocate the klass if already created.
4188 MetadataFactory::free_metadata(_loader_data, _klass);
4189 _klass = NULL;
4228 } 4190 }
4229 4191
4230 void ClassFileParser::print_field_layout(Symbol* name, 4192 void ClassFileParser::print_field_layout(Symbol* name,
4231 Array<u2>* fields, 4193 Array<u2>* fields,
4232 constantPoolHandle cp, 4194 constantPoolHandle cp,
4416 } 4378 }
4417 } 4379 }
4418 } 4380 }
4419 } 4381 }
4420 4382
4421 // utility method for appending and array with check for duplicates 4383 // utility methods for appending an array with check for duplicates
4422 4384
4423 void append_interfaces(GrowableArray<Klass*>* result, Array<Klass*>* ifs) { 4385 void append_interfaces(GrowableArray<Klass*>* result, Array<Klass*>* ifs) {
4424 // iterate over new interfaces 4386 // iterate over new interfaces
4425 for (int i = 0; i < ifs->length(); i++) { 4387 for (int i = 0; i < ifs->length(); i++) {
4426 Klass* e = ifs->at(i); 4388 Klass* e = ifs->at(i);
4428 // add new interface 4390 // add new interface
4429 result->append_if_missing(e); 4391 result->append_if_missing(e);
4430 } 4392 }
4431 } 4393 }
4432 4394
4433 4395 Array<Klass*>* ClassFileParser::compute_transitive_interfaces(
4434 Array<Klass*>* ClassFileParser::compute_transitive_interfaces(ClassLoaderData* loader_data, instanceKlassHandle super, Array<Klass*>* local_ifs, TRAPS) { 4396 instanceKlassHandle super,
4397 Array<Klass*>* local_ifs, TRAPS) {
4435 // Compute maximum size for transitive interfaces 4398 // Compute maximum size for transitive interfaces
4436 int max_transitive_size = 0; 4399 int max_transitive_size = 0;
4437 int super_size = 0; 4400 int super_size = 0;
4438 // Add superclass transitive interfaces size 4401 // Add superclass transitive interfaces size
4439 if (super.not_null()) { 4402 if (super.not_null()) {
4476 append_interfaces(result, local_ifs); 4439 append_interfaces(result, local_ifs);
4477 4440
4478 // length will be less than the max_transitive_size if duplicates were removed 4441 // length will be less than the max_transitive_size if duplicates were removed
4479 int length = result->length(); 4442 int length = result->length();
4480 assert(length <= max_transitive_size, "just checking"); 4443 assert(length <= max_transitive_size, "just checking");
4481 Array<Klass*>* new_result = MetadataFactory::new_array<Klass*>(loader_data, length, CHECK_NULL); 4444 Array<Klass*>* new_result = MetadataFactory::new_array<Klass*>(_loader_data, length, CHECK_NULL);
4482 for (int i = 0; i < length; i++) { 4445 for (int i = 0; i < length; i++) {
4483 Klass* e = result->at(i); 4446 Klass* e = result->at(i);
4484 assert(e != NULL, "just checking"); 4447 assert(e != NULL, "just checking");
4485 new_result->at_put(i, e); 4448 new_result->at_put(i, e);
4486 } 4449 }
4487 return new_result; 4450 return new_result;
4488 } 4451 }
4489 } 4452 }
4490
4491 4453
4492 void ClassFileParser::check_super_class_access(instanceKlassHandle this_klass, TRAPS) { 4454 void ClassFileParser::check_super_class_access(instanceKlassHandle this_klass, TRAPS) {
4493 Klass* super = this_klass->super(); 4455 Klass* super = this_klass->super();
4494 if ((super != NULL) && 4456 if ((super != NULL) &&
4495 (!Reflection::verify_class_access(this_klass(), super, false))) { 4457 (!Reflection::verify_class_access(this_klass(), super, false))) {