comparison src/share/vm/classfile/classFileParser.cpp @ 2044:06f017f7daa7

Merge.
author Thomas Wuerthinger <wuerthinger@ssw.jku.at>
date Fri, 07 Jan 2011 18:18:08 +0100
parents dad31fc330cd
children ef3c5db0b3ae
comparison
equal deleted inserted replaced
1942:00bc9eaf0e24 2044:06f017f7daa7
20 * or visit www.oracle.com if you need additional information or have any 20 * or visit www.oracle.com if you need additional information or have any
21 * questions. 21 * questions.
22 * 22 *
23 */ 23 */
24 24
25 #include "incls/_precompiled.incl" 25 #include "precompiled.hpp"
26 #include "incls/_classFileParser.cpp.incl" 26 #include "classfile/classFileParser.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/javaClasses.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/verificationType.hpp"
32 #include "classfile/verifier.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "memory/allocation.hpp"
35 #include "memory/gcLocker.hpp"
36 #include "memory/oopFactory.hpp"
37 #include "memory/universe.inline.hpp"
38 #include "oops/constantPoolOop.hpp"
39 #include "oops/instanceKlass.hpp"
40 #include "oops/klass.inline.hpp"
41 #include "oops/klassOop.hpp"
42 #include "oops/klassVtable.hpp"
43 #include "oops/methodOop.hpp"
44 #include "oops/symbolOop.hpp"
45 #include "prims/jvmtiExport.hpp"
46 #include "runtime/javaCalls.hpp"
47 #include "runtime/perfData.hpp"
48 #include "runtime/reflection.hpp"
49 #include "runtime/signature.hpp"
50 #include "runtime/timer.hpp"
51 #include "services/classLoadingService.hpp"
52 #include "services/threadService.hpp"
27 53
28 // We generally try to create the oops directly when parsing, rather than 54 // We generally try to create the oops directly when parsing, rather than
29 // allocating temporary data structures and copying the bytes twice. A 55 // allocating temporary data structures and copying the bytes twice. A
30 // temporary area is only needed when parsing utf8 entries in the constant 56 // temporary area is only needed when parsing utf8 entries in the constant
31 // pool and when parsing line number tables. 57 // pool and when parsing line number tables.
71 int lengths[SymbolTable::symbol_alloc_batch_size]; 97 int lengths[SymbolTable::symbol_alloc_batch_size];
72 int indices[SymbolTable::symbol_alloc_batch_size]; 98 int indices[SymbolTable::symbol_alloc_batch_size];
73 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size]; 99 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
74 int names_count = 0; 100 int names_count = 0;
75 101
76 // Side buffer for operands of variable-sized (InvokeDynamic) entries.
77 GrowableArray<int>* operands = NULL;
78 #ifdef ASSERT
79 GrowableArray<int>* indy_instructions = new GrowableArray<int>(THREAD, 10);
80 #endif
81
82 // parsing Index 0 is unused 102 // parsing Index 0 is unused
83 for (int index = 1; index < length; index++) { 103 for (int index = 1; index < length; index++) {
84 // Each of the following case guarantees one more byte in the stream 104 // Each of the following case guarantees one more byte in the stream
85 // for the following tag or the access_flags following constant pool, 105 // for the following tag or the access_flags following constant pool,
86 // so we don't need bounds-check for reading tag. 106 // so we don't need bounds-check for reading tag.
156 (!EnableInvokeDynamic ? 176 (!EnableInvokeDynamic ?
157 "This JVM does not support constant tag %u in class file %s" : 177 "This JVM does not support constant tag %u in class file %s" :
158 "Class file version does not support constant tag %u in class file %s"), 178 "Class file version does not support constant tag %u in class file %s"),
159 tag, CHECK); 179 tag, CHECK);
160 } 180 }
161 if (!AllowTransitionalJSR292 && tag == JVM_CONSTANT_InvokeDynamicTrans) { 181 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags
162 classfile_parse_error( 182 u2 bootstrap_specifier_index = cfs->get_u2_fast();
183 u2 name_and_type_index = cfs->get_u2_fast();
184 if (tag == JVM_CONSTANT_InvokeDynamicTrans) {
185 if (!AllowTransitionalJSR292)
186 classfile_parse_error(
163 "This JVM does not support transitional InvokeDynamic tag %u in class file %s", 187 "This JVM does not support transitional InvokeDynamic tag %u in class file %s",
164 tag, CHECK); 188 tag, CHECK);
189 cp->invoke_dynamic_trans_at_put(index, bootstrap_specifier_index, name_and_type_index);
190 break;
165 } 191 }
166 bool trans_no_argc = AllowTransitionalJSR292 && (tag == JVM_CONSTANT_InvokeDynamicTrans); 192 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index)
167 cfs->guarantee_more(7, CHECK); // bsm_index, nt, argc, ..., tag/access_flags 193 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later
168 u2 bootstrap_method_index = cfs->get_u2_fast(); 194 cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index);
169 u2 name_and_type_index = cfs->get_u2_fast();
170 int argument_count = trans_no_argc ? 0 : cfs->get_u2_fast();
171 cfs->guarantee_more(2*argument_count + 1, CHECK); // argv[argc]..., tag/access_flags
172 int argv_offset = constantPoolOopDesc::_indy_argv_offset;
173 int op_count = argv_offset + argument_count; // bsm, nt, argc, argv[]...
174 int op_base = start_operand_group(operands, op_count, CHECK);
175 assert(argv_offset == 3, "else adjust next 3 assignments");
176 operands->at_put(op_base + constantPoolOopDesc::_indy_bsm_offset, bootstrap_method_index);
177 operands->at_put(op_base + constantPoolOopDesc::_indy_nt_offset, name_and_type_index);
178 operands->at_put(op_base + constantPoolOopDesc::_indy_argc_offset, argument_count);
179 for (int arg_i = 0; arg_i < argument_count; arg_i++) {
180 int arg = cfs->get_u2_fast();
181 operands->at_put(op_base + constantPoolOopDesc::_indy_argv_offset + arg_i, arg);
182 }
183 cp->invoke_dynamic_at_put(index, op_base, op_count);
184 #ifdef ASSERT
185 // Record the steps just taken for later checking.
186 indy_instructions->append(index);
187 indy_instructions->append(bootstrap_method_index);
188 indy_instructions->append(name_and_type_index);
189 indy_instructions->append(argument_count);
190 #endif //ASSERT
191 } 195 }
192 break; 196 break;
193 case JVM_CONSTANT_Integer : 197 case JVM_CONSTANT_Integer :
194 { 198 {
195 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 199 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags
288 // Allocate the remaining symbols 292 // Allocate the remaining symbols
289 if (names_count > 0) { 293 if (names_count > 0) {
290 oopFactory::new_symbols(cp, names_count, names, lengths, indices, hashValues, CHECK); 294 oopFactory::new_symbols(cp, names_count, names, lengths, indices, hashValues, CHECK);
291 } 295 }
292 296
293 if (operands != NULL && operands->length() > 0) {
294 store_operand_array(operands, cp, CHECK);
295 }
296 #ifdef ASSERT
297 // Re-assert the indy structures, now that assertion checking can work.
298 for (int indy_i = 0; indy_i < indy_instructions->length(); ) {
299 int index = indy_instructions->at(indy_i++);
300 int bootstrap_method_index = indy_instructions->at(indy_i++);
301 int name_and_type_index = indy_instructions->at(indy_i++);
302 int argument_count = indy_instructions->at(indy_i++);
303 assert(cp->check_invoke_dynamic_at(index,
304 bootstrap_method_index, name_and_type_index,
305 argument_count),
306 "indy structure is OK");
307 }
308 #endif //ASSERT
309
310 // Copy _current pointer of local copy back to stream(). 297 // Copy _current pointer of local copy back to stream().
311 #ifdef ASSERT 298 #ifdef ASSERT
312 assert(cfs0->current() == old_current, "non-exclusive use of stream()"); 299 assert(cfs0->current() == old_current, "non-exclusive use of stream()");
313 #endif 300 #endif
314 cfs0->set_current(cfs1.current()); 301 cfs0->set_current(cfs1.current());
315 } 302 }
316
317 int ClassFileParser::start_operand_group(GrowableArray<int>* &operands, int op_count, TRAPS) {
318 if (operands == NULL) {
319 operands = new GrowableArray<int>(THREAD, 100);
320 int fillp_offset = constantPoolOopDesc::_multi_operand_buffer_fill_pointer_offset;
321 while (operands->length() <= fillp_offset)
322 operands->append(0); // force op_base > 0, for an error check
323 DEBUG_ONLY(operands->at_put(fillp_offset, (int)badHeapWordVal));
324 }
325 int cnt_pos = operands->append(op_count);
326 int arg_pos = operands->length();
327 operands->at_grow(arg_pos + op_count - 1); // grow to include the operands
328 assert(operands->length() == arg_pos + op_count, "");
329 int op_base = cnt_pos - constantPoolOopDesc::_multi_operand_count_offset;
330 return op_base;
331 }
332
333 void ClassFileParser::store_operand_array(GrowableArray<int>* operands, constantPoolHandle cp, TRAPS) {
334 // Collect the buffer of operands from variable-sized entries into a permanent array.
335 int arraylen = operands->length();
336 int fillp_offset = constantPoolOopDesc::_multi_operand_buffer_fill_pointer_offset;
337 assert(operands->at(fillp_offset) == (int)badHeapWordVal, "value unused so far");
338 operands->at_put(fillp_offset, arraylen);
339 cp->multi_operand_buffer_grow(arraylen, CHECK);
340 typeArrayOop operands_oop = cp->operands();
341 assert(operands_oop->length() == arraylen, "");
342 for (int i = 0; i < arraylen; i++) {
343 operands_oop->int_at_put(i, operands->at(i));
344 }
345 cp->set_operands(operands_oop);
346 // The fill_pointer is used only by constantPoolOop::copy_entry_to and friends,
347 // when constant pools need to be merged. Make sure it is sane now.
348 assert(cp->multi_operand_buffer_fill_pointer() == arraylen, "");
349 }
350
351 303
352 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); } 304 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); }
353 305
354 constantPoolHandle ClassFileParser::parse_constant_pool(TRAPS) { 306 constantPoolHandle ClassFileParser::parse_constant_pool(TRAPS) {
355 ClassFileStream* cfs = stream(); 307 ClassFileStream* cfs = stream();
373 325
374 int index = 1; // declared outside of loops for portability 326 int index = 1; // declared outside of loops for portability
375 327
376 // first verification pass - validate cross references and fixup class and string constants 328 // first verification pass - validate cross references and fixup class and string constants
377 for (index = 1; index < length; index++) { // Index 0 is unused 329 for (index = 1; index < length; index++) { // Index 0 is unused
378 switch (cp->tag_at(index).value()) { 330 jbyte tag = cp->tag_at(index).value();
331 switch (tag) {
379 case JVM_CONSTANT_Class : 332 case JVM_CONSTANT_Class :
380 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 333 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
381 break; 334 break;
382 case JVM_CONSTANT_Fieldref : 335 case JVM_CONSTANT_Fieldref :
383 // fall through 336 // fall through
515 "Invalid constant pool index %u in class file %s", 468 "Invalid constant pool index %u in class file %s",
516 ref_index, CHECK_(nullHandle)); 469 ref_index, CHECK_(nullHandle));
517 } 470 }
518 break; 471 break;
519 case JVM_CONSTANT_InvokeDynamicTrans : 472 case JVM_CONSTANT_InvokeDynamicTrans :
520 ShouldNotReachHere(); // this tag does not appear in the heap
521 case JVM_CONSTANT_InvokeDynamic : 473 case JVM_CONSTANT_InvokeDynamic :
522 { 474 {
523 int bootstrap_method_ref_index = cp->invoke_dynamic_bootstrap_method_ref_index_at(index);
524 int name_and_type_ref_index = cp->invoke_dynamic_name_and_type_ref_index_at(index); 475 int name_and_type_ref_index = cp->invoke_dynamic_name_and_type_ref_index_at(index);
525 check_property((bootstrap_method_ref_index == 0 && AllowTransitionalJSR292)
526 ||
527 (valid_cp_range(bootstrap_method_ref_index, length) &&
528 (cp->tag_at(bootstrap_method_ref_index).is_method_handle())),
529 "Invalid constant pool index %u in class file %s",
530 bootstrap_method_ref_index,
531 CHECK_(nullHandle));
532 check_property(valid_cp_range(name_and_type_ref_index, length) && 476 check_property(valid_cp_range(name_and_type_ref_index, length) &&
533 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 477 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
534 "Invalid constant pool index %u in class file %s", 478 "Invalid constant pool index %u in class file %s",
535 name_and_type_ref_index, 479 name_and_type_ref_index,
536 CHECK_(nullHandle)); 480 CHECK_(nullHandle));
537 int argc = cp->invoke_dynamic_argument_count_at(index); 481 if (tag == JVM_CONSTANT_InvokeDynamicTrans) {
538 for (int arg_i = 0; arg_i < argc; arg_i++) { 482 int bootstrap_method_ref_index = cp->invoke_dynamic_bootstrap_method_ref_index_at(index);
539 int arg = cp->invoke_dynamic_argument_index_at(index, arg_i); 483 check_property(valid_cp_range(bootstrap_method_ref_index, length) &&
540 check_property(valid_cp_range(arg, length) && 484 cp->tag_at(bootstrap_method_ref_index).is_method_handle(),
541 cp->tag_at(arg).is_loadable_constant() ||
542 // temporary early forms of string and class:
543 cp->tag_at(arg).is_klass_index() ||
544 cp->tag_at(arg).is_string_index(),
545 "Invalid constant pool index %u in class file %s", 485 "Invalid constant pool index %u in class file %s",
546 arg, 486 bootstrap_method_ref_index,
547 CHECK_(nullHandle)); 487 CHECK_(nullHandle));
548 } 488 }
489 // bootstrap specifier index must be checked later, when BootstrapMethods attr is available
549 break; 490 break;
550 } 491 }
551 default: 492 default:
552 fatal(err_msg("bad constant pool tag value %u", 493 fatal(err_msg("bad constant pool tag value %u",
553 cp->tag_at(index).value())); 494 cp->tag_at(index).value()));
2401 "Invalid constant pool index %u in Signature attribute in class file %s", 2342 "Invalid constant pool index %u in Signature attribute in class file %s",
2402 signature_index, CHECK); 2343 signature_index, CHECK);
2403 k->set_generic_signature(cp->symbol_at(signature_index)); 2344 k->set_generic_signature(cp->symbol_at(signature_index));
2404 } 2345 }
2405 2346
2347 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, instanceKlassHandle k,
2348 u4 attribute_byte_length, TRAPS) {
2349 ClassFileStream* cfs = stream();
2350 u1* current_start = cfs->current();
2351
2352 cfs->guarantee_more(2, CHECK); // length
2353 int attribute_array_length = cfs->get_u2_fast();
2354
2355 guarantee_property(_max_bootstrap_specifier_index < attribute_array_length,
2356 "Short length on BootstrapMethods in class file %s",
2357 CHECK);
2358
2359 // The attribute contains a counted array of counted tuples of shorts,
2360 // represending bootstrap specifiers:
2361 // length*{bootstrap_method_index, argument_count*{argument_index}}
2362 int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2);
2363 // operand_count = number of shorts in attr, except for leading length
2364
2365 // The attribute is copied into a short[] array.
2366 // The array begins with a series of short[2] pairs, one for each tuple.
2367 int index_size = (attribute_array_length * 2);
2368
2369 typeArrayOop operands_oop = oopFactory::new_permanent_intArray(index_size + operand_count, CHECK);
2370 typeArrayHandle operands(THREAD, operands_oop);
2371 operands_oop = NULL; // tidy
2372
2373 int operand_fill_index = index_size;
2374 int cp_size = cp->length();
2375
2376 for (int n = 0; n < attribute_array_length; n++) {
2377 // Store a 32-bit offset into the header of the operand array.
2378 assert(constantPoolOopDesc::operand_offset_at(operands(), n) == 0, "");
2379 constantPoolOopDesc::operand_offset_at_put(operands(), n, operand_fill_index);
2380
2381 // Read a bootstrap specifier.
2382 cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc
2383 u2 bootstrap_method_index = cfs->get_u2_fast();
2384 u2 argument_count = cfs->get_u2_fast();
2385 check_property(
2386 valid_cp_range(bootstrap_method_index, cp_size) &&
2387 cp->tag_at(bootstrap_method_index).is_method_handle(),
2388 "bootstrap_method_index %u has bad constant type in class file %s",
2389 CHECK);
2390 operands->short_at_put(operand_fill_index++, bootstrap_method_index);
2391 operands->short_at_put(operand_fill_index++, argument_count);
2392
2393 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc]
2394 for (int j = 0; j < argument_count; j++) {
2395 u2 arg_index = cfs->get_u2_fast();
2396 check_property(
2397 valid_cp_range(arg_index, cp_size) &&
2398 cp->tag_at(arg_index).is_loadable_constant(),
2399 "argument_index %u has bad constant type in class file %s",
2400 CHECK);
2401 operands->short_at_put(operand_fill_index++, arg_index);
2402 }
2403 }
2404
2405 assert(operand_fill_index == operands()->length(), "exact fill");
2406 assert(constantPoolOopDesc::operand_array_length(operands()) == attribute_array_length, "correct decode");
2407
2408 u1* current_end = cfs->current();
2409 guarantee_property(current_end == current_start + attribute_byte_length,
2410 "Bad length on BootstrapMethods in class file %s",
2411 CHECK);
2412
2413 cp->set_operands(operands());
2414 }
2415
2416
2406 void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { 2417 void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2407 ClassFileStream* cfs = stream(); 2418 ClassFileStream* cfs = stream();
2408 // Set inner classes attribute to default sentinel 2419 // Set inner classes attribute to default sentinel
2409 k->set_inner_classes(Universe::the_empty_short_array()); 2420 k->set_inner_classes(Universe::the_empty_short_array());
2410 cfs->guarantee_more(2, CHECK); // attributes_count 2421 cfs->guarantee_more(2, CHECK); // attributes_count
2411 u2 attributes_count = cfs->get_u2_fast(); 2422 u2 attributes_count = cfs->get_u2_fast();
2412 bool parsed_sourcefile_attribute = false; 2423 bool parsed_sourcefile_attribute = false;
2413 bool parsed_innerclasses_attribute = false; 2424 bool parsed_innerclasses_attribute = false;
2414 bool parsed_enclosingmethod_attribute = false; 2425 bool parsed_enclosingmethod_attribute = false;
2426 bool parsed_bootstrap_methods_attribute = false;
2415 u1* runtime_visible_annotations = NULL; 2427 u1* runtime_visible_annotations = NULL;
2416 int runtime_visible_annotations_length = 0; 2428 int runtime_visible_annotations_length = 0;
2417 u1* runtime_invisible_annotations = NULL; 2429 u1* runtime_invisible_annotations = NULL;
2418 int runtime_invisible_annotations_length = 0; 2430 int runtime_invisible_annotations_length = 0;
2419 // Iterate over attributes 2431 // Iterate over attributes
2508 (!cp->is_within_bounds(method_index) || 2520 (!cp->is_within_bounds(method_index) ||
2509 !cp->tag_at(method_index).is_name_and_type())) { 2521 !cp->tag_at(method_index).is_name_and_type())) {
2510 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK); 2522 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2511 } 2523 }
2512 k->set_enclosing_method_indices(class_index, method_index); 2524 k->set_enclosing_method_indices(class_index, method_index);
2525 } else if (tag == vmSymbols::tag_bootstrap_methods() &&
2526 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2527 if (parsed_bootstrap_methods_attribute)
2528 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK);
2529 parsed_bootstrap_methods_attribute = true;
2530 parse_classfile_bootstrap_methods_attribute(cp, k, attribute_length, CHECK);
2513 } else { 2531 } else {
2514 // Unknown attribute 2532 // Unknown attribute
2515 cfs->skip_u1(attribute_length, CHECK); 2533 cfs->skip_u1(attribute_length, CHECK);
2516 } 2534 }
2517 } else { 2535 } else {
2523 runtime_visible_annotations_length, 2541 runtime_visible_annotations_length,
2524 runtime_invisible_annotations, 2542 runtime_invisible_annotations,
2525 runtime_invisible_annotations_length, 2543 runtime_invisible_annotations_length,
2526 CHECK); 2544 CHECK);
2527 k->set_class_annotations(annotations()); 2545 k->set_class_annotations(annotations());
2546
2547 if (_max_bootstrap_specifier_index >= 0) {
2548 guarantee_property(parsed_bootstrap_methods_attribute,
2549 "Missing BootstrapMethods attribute in class file %s", CHECK);
2550 }
2528 } 2551 }
2529 2552
2530 2553
2531 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations, 2554 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations,
2532 int runtime_visible_annotations_length, 2555 int runtime_visible_annotations_length,
2840 jt->get_thread_stat()->perf_recursion_counts_addr(), 2863 jt->get_thread_stat()->perf_recursion_counts_addr(),
2841 jt->get_thread_stat()->perf_timers_addr(), 2864 jt->get_thread_stat()->perf_timers_addr(),
2842 PerfClassTraceTime::PARSE_CLASS); 2865 PerfClassTraceTime::PARSE_CLASS);
2843 2866
2844 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false; 2867 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
2868 _max_bootstrap_specifier_index = -1;
2845 2869
2846 if (JvmtiExport::should_post_class_file_load_hook()) { 2870 if (JvmtiExport::should_post_class_file_load_hook()) {
2847 unsigned char* ptr = cfs->buffer(); 2871 unsigned char* ptr = cfs->buffer();
2848 unsigned char* end_ptr = cfs->buffer() + cfs->length(); 2872 unsigned char* end_ptr = cfs->buffer() + cfs->length();
2849 2873