src/share/vm/prims/jvmtiClassFileReconstituter.cpp

Tue, 26 Mar 2013 15:00:34 +0100

author
rbackman
date
Tue, 26 Mar 2013 15:00:34 +0100
changeset 4818
1916ca1dec2f
parent 4271
8aaef2cee3b2
child 5045
c456f4510385
permissions
-rw-r--r--

8009382: Add JVM_Get{Field|Method}TypeAnnotations
Reviewed-by: dcubed, rbackman
Contributed-by: Joel Borggren-Franck <joel.franck@oracle.com>

duke@435 1 /*
dsamersoff@3626 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/symbolTable.hpp"
stefank@2314 27 #include "interpreter/bytecodeStream.hpp"
never@3137 28 #include "oops/fieldStreams.hpp"
stefank@2314 29 #include "prims/jvmtiClassFileReconstituter.hpp"
stefank@2314 30 #include "runtime/signature.hpp"
stefank@2314 31 #ifdef TARGET_ARCH_x86
stefank@2314 32 # include "bytes_x86.hpp"
stefank@2314 33 #endif
stefank@2314 34 #ifdef TARGET_ARCH_sparc
stefank@2314 35 # include "bytes_sparc.hpp"
stefank@2314 36 #endif
stefank@2314 37 #ifdef TARGET_ARCH_zero
stefank@2314 38 # include "bytes_zero.hpp"
stefank@2314 39 #endif
bobv@2508 40 #ifdef TARGET_ARCH_arm
bobv@2508 41 # include "bytes_arm.hpp"
bobv@2508 42 #endif
bobv@2508 43 #ifdef TARGET_ARCH_ppc
bobv@2508 44 # include "bytes_ppc.hpp"
bobv@2508 45 #endif
minqi@4083 46 // FIXME: add Deprecated attribute
duke@435 47 // FIXME: fix Synthetic attribute
coleenp@4037 48 // FIXME: per Serguei, add error return handling for ConstantPool::copy_cpool_bytes()
duke@435 49
duke@435 50
duke@435 51 // Write the field information portion of ClassFile structure
duke@435 52 // JVMSpec| u2 fields_count;
duke@435 53 // JVMSpec| field_info fields[fields_count];
duke@435 54 void JvmtiClassFileReconstituter::write_field_infos() {
duke@435 55 HandleMark hm(thread());
coleenp@4037 56 Array<AnnotationArray*>* fields_anno = ikh()->fields_annotations();
duke@435 57
never@3137 58 // Compute the real number of Java fields
never@3137 59 int java_fields = ikh()->java_fields_count();
never@3137 60
never@3143 61 write_u2(java_fields);
never@3137 62 for (JavaFieldStream fs(ikh()); !fs.done(); fs.next()) {
never@3137 63 AccessFlags access_flags = fs.access_flags();
never@3137 64 int name_index = fs.name_index();
never@3137 65 int signature_index = fs.signature_index();
never@3137 66 int initial_value_index = fs.initval_index();
duke@435 67 guarantee(name_index != 0 && signature_index != 0, "bad constant pool index for field");
never@3137 68 // int offset = ikh()->field_offset( index );
never@3137 69 int generic_signature_index = fs.generic_signature_index();
coleenp@4037 70 AnnotationArray* anno = fields_anno == NULL ? NULL : fields_anno->at(fs.index());
duke@435 71
duke@435 72 // JVMSpec| field_info {
duke@435 73 // JVMSpec| u2 access_flags;
duke@435 74 // JVMSpec| u2 name_index;
duke@435 75 // JVMSpec| u2 descriptor_index;
duke@435 76 // JVMSpec| u2 attributes_count;
duke@435 77 // JVMSpec| attribute_info attributes[attributes_count];
duke@435 78 // JVMSpec| }
duke@435 79
never@3137 80 write_u2(access_flags.as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
duke@435 81 write_u2(name_index);
duke@435 82 write_u2(signature_index);
duke@435 83 int attr_count = 0;
duke@435 84 if (initial_value_index != 0) {
duke@435 85 ++attr_count;
duke@435 86 }
duke@435 87 if (access_flags.is_synthetic()) {
duke@435 88 // ++attr_count;
duke@435 89 }
duke@435 90 if (generic_signature_index != 0) {
duke@435 91 ++attr_count;
duke@435 92 }
coleenp@4037 93 if (anno != NULL) {
duke@435 94 ++attr_count; // has RuntimeVisibleAnnotations attribute
duke@435 95 }
duke@435 96
duke@435 97 write_u2(attr_count);
duke@435 98
duke@435 99 if (initial_value_index != 0) {
duke@435 100 write_attribute_name_index("ConstantValue");
duke@435 101 write_u4(2); //length always 2
duke@435 102 write_u2(initial_value_index);
duke@435 103 }
duke@435 104 if (access_flags.is_synthetic()) {
duke@435 105 // write_synthetic_attribute();
duke@435 106 }
duke@435 107 if (generic_signature_index != 0) {
duke@435 108 write_signature_attribute(generic_signature_index);
duke@435 109 }
coleenp@4037 110 if (anno != NULL) {
duke@435 111 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
duke@435 112 }
duke@435 113 }
duke@435 114 }
duke@435 115
duke@435 116 // Write Code attribute
duke@435 117 // JVMSpec| Code_attribute {
duke@435 118 // JVMSpec| u2 attribute_name_index;
duke@435 119 // JVMSpec| u4 attribute_length;
duke@435 120 // JVMSpec| u2 max_stack;
duke@435 121 // JVMSpec| u2 max_locals;
duke@435 122 // JVMSpec| u4 code_length;
duke@435 123 // JVMSpec| u1 code[code_length];
duke@435 124 // JVMSpec| u2 exception_table_length;
duke@435 125 // JVMSpec| { u2 start_pc;
duke@435 126 // JVMSpec| u2 end_pc;
duke@435 127 // JVMSpec| u2 handler_pc;
duke@435 128 // JVMSpec| u2 catch_type;
duke@435 129 // JVMSpec| } exception_table[exception_table_length];
duke@435 130 // JVMSpec| u2 attributes_count;
duke@435 131 // JVMSpec| attribute_info attributes[attributes_count];
duke@435 132 // JVMSpec| }
duke@435 133 void JvmtiClassFileReconstituter::write_code_attribute(methodHandle method) {
coleenp@4037 134 ConstMethod* const_method = method->constMethod();
duke@435 135 u2 line_num_cnt = 0;
duke@435 136 int stackmap_len = 0;
coleenp@3345 137 int local_variable_table_length = 0;
minqi@4083 138 int local_variable_type_table_length = 0;
duke@435 139
coleenp@3345 140 // compute number and length of attributes
duke@435 141 int attr_count = 0;
duke@435 142 int attr_size = 0;
duke@435 143 if (const_method->has_linenumber_table()) {
duke@435 144 line_num_cnt = line_number_table_entries(method);
duke@435 145 if (line_num_cnt != 0) {
duke@435 146 ++attr_count;
duke@435 147 // Compute the complete size of the line number table attribute:
duke@435 148 // LineNumberTable_attribute {
duke@435 149 // u2 attribute_name_index;
duke@435 150 // u4 attribute_length;
duke@435 151 // u2 line_number_table_length;
duke@435 152 // { u2 start_pc;
duke@435 153 // u2 line_number;
duke@435 154 // } line_number_table[line_number_table_length];
duke@435 155 // }
duke@435 156 attr_size += 2 + 4 + 2 + line_num_cnt * (2 + 2);
duke@435 157 }
duke@435 158 }
duke@435 159 if (method->has_stackmap_table()) {
duke@435 160 stackmap_len = method->stackmap_data()->length();
duke@435 161 if (stackmap_len != 0) {
duke@435 162 ++attr_count;
duke@435 163 // Compute the size of the stack map table attribute (VM stores raw):
duke@435 164 // StackMapTable_attribute {
duke@435 165 // u2 attribute_name_index;
duke@435 166 // u4 attribute_length;
duke@435 167 // u2 number_of_entries;
duke@435 168 // stack_map_frame_entries[number_of_entries];
duke@435 169 // }
duke@435 170 attr_size += 2 + 4 + stackmap_len;
duke@435 171 }
duke@435 172 }
coleenp@3345 173 if (method->has_localvariable_table()) {
coleenp@3345 174 local_variable_table_length = method->localvariable_table_length();
coleenp@3345 175 if (local_variable_table_length != 0) {
minqi@4083 176 ++attr_count;
coleenp@3345 177 // Compute the size of the local variable table attribute (VM stores raw):
coleenp@3345 178 // LocalVariableTable_attribute {
coleenp@3345 179 // u2 attribute_name_index;
coleenp@3345 180 // u4 attribute_length;
coleenp@3345 181 // u2 local_variable_table_length;
coleenp@3345 182 // {
coleenp@3345 183 // u2 start_pc;
coleenp@3345 184 // u2 length;
coleenp@3345 185 // u2 name_index;
coleenp@3345 186 // u2 descriptor_index;
coleenp@3345 187 // u2 index;
coleenp@3345 188 // }
coleenp@3345 189 attr_size += 2 + 4 + 2 + local_variable_table_length * (2 + 2 + 2 + 2 + 2);
minqi@4083 190
minqi@4083 191 // Local variables with generic signatures must have LVTT entries
minqi@4083 192 LocalVariableTableElement *elem = method->localvariable_table_start();
minqi@4083 193 for (int idx = 0; idx < local_variable_table_length; idx++) {
minqi@4083 194 if (elem[idx].signature_cp_index != 0) {
minqi@4083 195 local_variable_type_table_length++;
minqi@4083 196 }
minqi@4083 197 }
minqi@4083 198
minqi@4083 199 if (local_variable_type_table_length != 0) {
minqi@4083 200 ++attr_count;
minqi@4083 201 // Compute the size of the local variable type table attribute (VM stores raw):
minqi@4083 202 // LocalVariableTypeTable_attribute {
minqi@4083 203 // u2 attribute_name_index;
minqi@4083 204 // u4 attribute_length;
minqi@4083 205 // u2 local_variable_type_table_length;
minqi@4083 206 // {
minqi@4083 207 // u2 start_pc;
minqi@4083 208 // u2 length;
minqi@4083 209 // u2 name_index;
minqi@4083 210 // u2 signature_index;
minqi@4083 211 // u2 index;
minqi@4083 212 // }
minqi@4083 213 attr_size += 2 + 4 + 2 + local_variable_type_table_length * (2 + 2 + 2 + 2 + 2);
minqi@4083 214 }
coleenp@3345 215 }
coleenp@3345 216 }
duke@435 217
jiangli@3917 218 ExceptionTable exception_table(method());
jiangli@3917 219 int exception_table_length = exception_table.length();
duke@435 220 int code_size = const_method->code_size();
duke@435 221 int size =
duke@435 222 2+2+4 + // max_stack, max_locals, code_length
duke@435 223 code_size + // code
duke@435 224 2 + // exception_table_length
jiangli@3917 225 (2+2+2+2) * exception_table_length + // exception_table
duke@435 226 2 + // attributes_count
duke@435 227 attr_size; // attributes
duke@435 228
duke@435 229 write_attribute_name_index("Code");
duke@435 230 write_u4(size);
sspitsyn@4270 231 write_u2(method->verifier_max_stack());
duke@435 232 write_u2(method->max_locals());
duke@435 233 write_u4(code_size);
duke@435 234 copy_bytecodes(method, (unsigned char*)writeable_address(code_size));
jiangli@3917 235 write_u2(exception_table_length);
jiangli@3917 236 for (int index = 0; index < exception_table_length; index++) {
jiangli@3917 237 write_u2(exception_table.start_pc(index));
jiangli@3917 238 write_u2(exception_table.end_pc(index));
jiangli@3917 239 write_u2(exception_table.handler_pc(index));
jiangli@3917 240 write_u2(exception_table.catch_type_index(index));
duke@435 241 }
duke@435 242 write_u2(attr_count);
duke@435 243 if (line_num_cnt != 0) {
duke@435 244 write_line_number_table_attribute(method, line_num_cnt);
duke@435 245 }
duke@435 246 if (stackmap_len != 0) {
duke@435 247 write_stackmap_table_attribute(method, stackmap_len);
duke@435 248 }
coleenp@3345 249 if (local_variable_table_length != 0) {
coleenp@3345 250 write_local_variable_table_attribute(method, local_variable_table_length);
coleenp@3345 251 }
minqi@4083 252 if (local_variable_type_table_length != 0) {
minqi@4083 253 write_local_variable_type_table_attribute(method, local_variable_type_table_length);
minqi@4083 254 }
duke@435 255 }
duke@435 256
duke@435 257 // Write Exceptions attribute
duke@435 258 // JVMSpec| Exceptions_attribute {
duke@435 259 // JVMSpec| u2 attribute_name_index;
duke@435 260 // JVMSpec| u4 attribute_length;
duke@435 261 // JVMSpec| u2 number_of_exceptions;
duke@435 262 // JVMSpec| u2 exception_index_table[number_of_exceptions];
duke@435 263 // JVMSpec| }
coleenp@4037 264 void JvmtiClassFileReconstituter::write_exceptions_attribute(ConstMethod* const_method) {
duke@435 265 CheckedExceptionElement* checked_exceptions = const_method->checked_exceptions_start();
duke@435 266 int checked_exceptions_length = const_method->checked_exceptions_length();
duke@435 267 int size =
duke@435 268 2 + // number_of_exceptions
duke@435 269 2 * checked_exceptions_length; // exception_index_table
duke@435 270
duke@435 271 write_attribute_name_index("Exceptions");
duke@435 272 write_u4(size);
duke@435 273 write_u2(checked_exceptions_length);
duke@435 274 for (int index = 0; index < checked_exceptions_length; index++) {
duke@435 275 write_u2(checked_exceptions[index].class_cp_index);
duke@435 276 }
duke@435 277 }
duke@435 278
duke@435 279 // Write SourceFile attribute
duke@435 280 // JVMSpec| SourceFile_attribute {
duke@435 281 // JVMSpec| u2 attribute_name_index;
duke@435 282 // JVMSpec| u4 attribute_length;
duke@435 283 // JVMSpec| u2 sourcefile_index;
duke@435 284 // JVMSpec| }
duke@435 285 void JvmtiClassFileReconstituter::write_source_file_attribute() {
duke@435 286 assert(ikh()->source_file_name() != NULL, "caller must check");
duke@435 287
duke@435 288 write_attribute_name_index("SourceFile");
duke@435 289 write_u4(2); // always length 2
duke@435 290 write_u2(symbol_to_cpool_index(ikh()->source_file_name()));
duke@435 291 }
duke@435 292
duke@435 293 // Write SourceDebugExtension attribute
duke@435 294 // JSR45| SourceDebugExtension_attribute {
duke@435 295 // JSR45| u2 attribute_name_index;
duke@435 296 // JSR45| u4 attribute_length;
fparain@3906 297 // JSR45| u1 debug_extension[attribute_length];
duke@435 298 // JSR45| }
duke@435 299 void JvmtiClassFileReconstituter::write_source_debug_extension_attribute() {
duke@435 300 assert(ikh()->source_debug_extension() != NULL, "caller must check");
duke@435 301
duke@435 302 write_attribute_name_index("SourceDebugExtension");
fparain@3906 303 int len = (int)strlen(ikh()->source_debug_extension());
fparain@3906 304 write_u4(len);
fparain@3906 305 u1* ext = (u1*)ikh()->source_debug_extension();
fparain@3906 306 for (int i=0; i<len; i++) {
fparain@3906 307 write_u1(ext[i]);
fparain@3906 308 }
duke@435 309 }
duke@435 310
duke@435 311 // Write (generic) Signature attribute
duke@435 312 // JVMSpec| Signature_attribute {
duke@435 313 // JVMSpec| u2 attribute_name_index;
duke@435 314 // JVMSpec| u4 attribute_length;
duke@435 315 // JVMSpec| u2 signature_index;
duke@435 316 // JVMSpec| }
duke@435 317 void JvmtiClassFileReconstituter::write_signature_attribute(u2 generic_signature_index) {
duke@435 318 write_attribute_name_index("Signature");
duke@435 319 write_u4(2); // always length 2
duke@435 320 write_u2(generic_signature_index);
duke@435 321 }
duke@435 322
duke@435 323 // Compute the number of entries in the InnerClasses attribute
duke@435 324 u2 JvmtiClassFileReconstituter::inner_classes_attribute_length() {
jiangli@3670 325 InnerClassesIterator iter(ikh());
jiangli@3670 326 return iter.length();
duke@435 327 }
duke@435 328
duke@435 329 // Write an annotation attribute. The VM stores them in raw form, so all we need
duke@435 330 // to do is add the attrubute name and fill in the length.
duke@435 331 // JSR202| *Annotations_attribute {
duke@435 332 // JSR202| u2 attribute_name_index;
duke@435 333 // JSR202| u4 attribute_length;
duke@435 334 // JSR202| ...
duke@435 335 // JSR202| }
duke@435 336 void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
coleenp@4037 337 AnnotationArray* annos) {
duke@435 338 u4 length = annos->length();
duke@435 339 write_attribute_name_index(attr_name);
duke@435 340 write_u4(length);
coleenp@4037 341 memcpy(writeable_address(length), annos->adr_at(0), length);
duke@435 342 }
duke@435 343
duke@435 344
duke@435 345 // Write InnerClasses attribute
duke@435 346 // JVMSpec| InnerClasses_attribute {
duke@435 347 // JVMSpec| u2 attribute_name_index;
duke@435 348 // JVMSpec| u4 attribute_length;
duke@435 349 // JVMSpec| u2 number_of_classes;
duke@435 350 // JVMSpec| { u2 inner_class_info_index;
duke@435 351 // JVMSpec| u2 outer_class_info_index;
duke@435 352 // JVMSpec| u2 inner_name_index;
duke@435 353 // JVMSpec| u2 inner_class_access_flags;
duke@435 354 // JVMSpec| } classes[number_of_classes];
duke@435 355 // JVMSpec| }
duke@435 356 void JvmtiClassFileReconstituter::write_inner_classes_attribute(int length) {
jiangli@3670 357 InnerClassesIterator iter(ikh());
jiangli@3670 358 guarantee(iter.length() != 0 && iter.length() == length,
duke@435 359 "caller must check");
coleenp@4037 360 u2 entry_count = length / InstanceKlass::inner_class_next_offset;
duke@435 361 u4 size = 2 + entry_count * (2+2+2+2);
duke@435 362
duke@435 363 write_attribute_name_index("InnerClasses");
duke@435 364 write_u4(size);
duke@435 365 write_u2(entry_count);
jiangli@3670 366 for (; !iter.done(); iter.next()) {
jiangli@3670 367 write_u2(iter.inner_class_info_index());
jiangli@3670 368 write_u2(iter.outer_class_info_index());
jiangli@3670 369 write_u2(iter.inner_name_index());
jiangli@3670 370 write_u2(iter.inner_access_flags());
duke@435 371 }
duke@435 372 }
duke@435 373
duke@435 374 // Write Synthetic attribute
duke@435 375 // JVMSpec| Synthetic_attribute {
duke@435 376 // JVMSpec| u2 attribute_name_index;
duke@435 377 // JVMSpec| u4 attribute_length;
duke@435 378 // JVMSpec| }
duke@435 379 void JvmtiClassFileReconstituter::write_synthetic_attribute() {
duke@435 380 write_attribute_name_index("Synthetic");
duke@435 381 write_u4(0); //length always zero
duke@435 382 }
duke@435 383
duke@435 384 // Compute size of LineNumberTable
duke@435 385 u2 JvmtiClassFileReconstituter::line_number_table_entries(methodHandle method) {
duke@435 386 // The line number table is compressed so we don't know how big it is until decompressed.
duke@435 387 // Decompression is really fast so we just do it twice.
duke@435 388 u2 num_entries = 0;
duke@435 389 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
duke@435 390 while (stream.read_pair()) {
duke@435 391 num_entries++;
duke@435 392 }
duke@435 393 return num_entries;
duke@435 394 }
duke@435 395
duke@435 396 // Write LineNumberTable attribute
duke@435 397 // JVMSpec| LineNumberTable_attribute {
duke@435 398 // JVMSpec| u2 attribute_name_index;
duke@435 399 // JVMSpec| u4 attribute_length;
duke@435 400 // JVMSpec| u2 line_number_table_length;
duke@435 401 // JVMSpec| { u2 start_pc;
duke@435 402 // JVMSpec| u2 line_number;
duke@435 403 // JVMSpec| } line_number_table[line_number_table_length];
duke@435 404 // JVMSpec| }
duke@435 405 void JvmtiClassFileReconstituter::write_line_number_table_attribute(methodHandle method,
duke@435 406 u2 num_entries) {
duke@435 407
duke@435 408 write_attribute_name_index("LineNumberTable");
duke@435 409 write_u4(2 + num_entries * (2 + 2));
duke@435 410 write_u2(num_entries);
duke@435 411
duke@435 412 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
duke@435 413 while (stream.read_pair()) {
duke@435 414 write_u2(stream.bci());
duke@435 415 write_u2(stream.line());
duke@435 416 }
duke@435 417 }
duke@435 418
minqi@4083 419 // Write LocalVariableTable attribute
coleenp@3345 420 // JVMSpec| LocalVariableTable_attribute {
coleenp@3345 421 // JVMSpec| u2 attribute_name_index;
coleenp@3345 422 // JVMSpec| u4 attribute_length;
coleenp@3345 423 // JVMSpec| u2 local_variable_table_length;
coleenp@3345 424 // JVMSpec| { u2 start_pc;
coleenp@3345 425 // JVMSpec| u2 length;
coleenp@3345 426 // JVMSpec| u2 name_index;
coleenp@3345 427 // JVMSpec| u2 descriptor_index;
coleenp@3345 428 // JVMSpec| u2 index;
coleenp@3345 429 // JVMSpec| } local_variable_table[local_variable_table_length];
coleenp@3345 430 // JVMSpec| }
coleenp@3345 431 void JvmtiClassFileReconstituter::write_local_variable_table_attribute(methodHandle method, u2 num_entries) {
coleenp@3345 432 write_attribute_name_index("LocalVariableTable");
coleenp@3345 433 write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
coleenp@3345 434 write_u2(num_entries);
coleenp@3345 435
coleenp@3345 436 assert(method->localvariable_table_length() == num_entries, "just checking");
coleenp@3345 437
coleenp@3345 438 LocalVariableTableElement *elem = method->localvariable_table_start();
coleenp@3345 439 for (int j=0; j<method->localvariable_table_length(); j++) {
coleenp@3345 440 write_u2(elem->start_bci);
coleenp@3345 441 write_u2(elem->length);
coleenp@3345 442 write_u2(elem->name_cp_index);
coleenp@3345 443 write_u2(elem->descriptor_cp_index);
coleenp@3345 444 write_u2(elem->slot);
coleenp@3345 445 elem++;
coleenp@3345 446 }
coleenp@3345 447 }
coleenp@3345 448
minqi@4083 449 // Write LocalVariableTypeTable attribute
minqi@4083 450 // JVMSpec| LocalVariableTypeTable_attribute {
minqi@4083 451 // JVMSpec| u2 attribute_name_index;
minqi@4083 452 // JVMSpec| u4 attribute_length;
minqi@4083 453 // JVMSpec| u2 local_variable_type_table_length;
minqi@4083 454 // JVMSpec| { u2 start_pc;
minqi@4083 455 // JVMSpec| u2 length;
minqi@4083 456 // JVMSpec| u2 name_index;
minqi@4083 457 // JVMSpec| u2 signature_index;
minqi@4083 458 // JVMSpec| u2 index;
minqi@4083 459 // JVMSpec| } local_variable_type_table[local_variable_type_table_length];
minqi@4083 460 // JVMSpec| }
minqi@4083 461 void JvmtiClassFileReconstituter::write_local_variable_type_table_attribute(methodHandle method, u2 num_entries) {
minqi@4083 462 write_attribute_name_index("LocalVariableTypeTable");
minqi@4083 463 write_u4(2 + num_entries * (2 + 2 + 2 + 2 + 2));
minqi@4083 464 write_u2(num_entries);
minqi@4083 465
minqi@4083 466 LocalVariableTableElement *elem = method->localvariable_table_start();
minqi@4083 467 for (int j=0; j<method->localvariable_table_length(); j++) {
minqi@4083 468 if (elem->signature_cp_index > 0) {
minqi@4083 469 // Local variable has a generic signature - write LVTT attribute entry
minqi@4083 470 write_u2(elem->start_bci);
minqi@4083 471 write_u2(elem->length);
minqi@4083 472 write_u2(elem->name_cp_index);
minqi@4083 473 write_u2(elem->signature_cp_index);
minqi@4083 474 write_u2(elem->slot);
minqi@4083 475 num_entries--;
minqi@4083 476 }
minqi@4083 477 elem++;
minqi@4083 478 }
minqi@4083 479 assert(num_entries == 0, "just checking");
minqi@4083 480 }
minqi@4083 481
duke@435 482 // Write stack map table attribute
duke@435 483 // JSR-202| StackMapTable_attribute {
duke@435 484 // JSR-202| u2 attribute_name_index;
duke@435 485 // JSR-202| u4 attribute_length;
duke@435 486 // JSR-202| u2 number_of_entries;
duke@435 487 // JSR-202| stack_map_frame_entries[number_of_entries];
duke@435 488 // JSR-202| }
duke@435 489 void JvmtiClassFileReconstituter::write_stackmap_table_attribute(methodHandle method,
duke@435 490 int stackmap_len) {
duke@435 491
duke@435 492 write_attribute_name_index("StackMapTable");
duke@435 493 write_u4(stackmap_len);
duke@435 494 memcpy(
duke@435 495 writeable_address(stackmap_len),
coleenp@4037 496 (void*)(method->stackmap_data()->adr_at(0)),
duke@435 497 stackmap_len);
duke@435 498 }
duke@435 499
duke@435 500 // Write one method_info structure
duke@435 501 // JVMSpec| method_info {
duke@435 502 // JVMSpec| u2 access_flags;
duke@435 503 // JVMSpec| u2 name_index;
duke@435 504 // JVMSpec| u2 descriptor_index;
duke@435 505 // JVMSpec| u2 attributes_count;
duke@435 506 // JVMSpec| attribute_info attributes[attributes_count];
duke@435 507 // JVMSpec| }
duke@435 508 void JvmtiClassFileReconstituter::write_method_info(methodHandle method) {
duke@435 509 AccessFlags access_flags = method->access_flags();
coleenp@4037 510 ConstMethod* const_method = method->constMethod();
duke@435 511 u2 generic_signature_index = const_method->generic_signature_index();
coleenp@4037 512 AnnotationArray* anno = method->annotations();
coleenp@4037 513 AnnotationArray* param_anno = method->parameter_annotations();
coleenp@4037 514 AnnotationArray* default_anno = method->annotation_default();
duke@435 515
duke@435 516 write_u2(access_flags.get_flags() & JVM_RECOGNIZED_METHOD_MODIFIERS);
duke@435 517 write_u2(const_method->name_index());
duke@435 518 write_u2(const_method->signature_index());
duke@435 519
duke@435 520 // write attributes in the same order javac does, so we can test with byte for
duke@435 521 // byte comparison
duke@435 522 int attr_count = 0;
duke@435 523 if (const_method->code_size() != 0) {
duke@435 524 ++attr_count; // has Code attribute
duke@435 525 }
duke@435 526 if (const_method->has_checked_exceptions()) {
duke@435 527 ++attr_count; // has Exceptions attribute
duke@435 528 }
coleenp@4037 529 if (default_anno != NULL) {
duke@435 530 ++attr_count; // has AnnotationDefault attribute
duke@435 531 }
duke@435 532 // Deprecated attribute would go here
duke@435 533 if (access_flags.is_synthetic()) { // FIXME
duke@435 534 // ++attr_count;
duke@435 535 }
duke@435 536 if (generic_signature_index != 0) {
duke@435 537 ++attr_count;
duke@435 538 }
coleenp@4037 539 if (anno != NULL) {
duke@435 540 ++attr_count; // has RuntimeVisibleAnnotations attribute
duke@435 541 }
coleenp@4037 542 if (param_anno != NULL) {
duke@435 543 ++attr_count; // has RuntimeVisibleParameterAnnotations attribute
duke@435 544 }
duke@435 545
duke@435 546 write_u2(attr_count);
duke@435 547 if (const_method->code_size() > 0) {
duke@435 548 write_code_attribute(method);
duke@435 549 }
duke@435 550 if (const_method->has_checked_exceptions()) {
duke@435 551 write_exceptions_attribute(const_method);
duke@435 552 }
coleenp@4037 553 if (default_anno != NULL) {
duke@435 554 write_annotations_attribute("AnnotationDefault", default_anno);
duke@435 555 }
duke@435 556 // Deprecated attribute would go here
duke@435 557 if (access_flags.is_synthetic()) {
duke@435 558 // write_synthetic_attribute();
duke@435 559 }
duke@435 560 if (generic_signature_index != 0) {
duke@435 561 write_signature_attribute(generic_signature_index);
duke@435 562 }
coleenp@4037 563 if (anno != NULL) {
duke@435 564 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
duke@435 565 }
coleenp@4037 566 if (param_anno != NULL) {
duke@435 567 write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
duke@435 568 }
duke@435 569 }
duke@435 570
duke@435 571 // Write the class attributes portion of ClassFile structure
duke@435 572 // JVMSpec| u2 attributes_count;
duke@435 573 // JVMSpec| attribute_info attributes[attributes_count];
duke@435 574 void JvmtiClassFileReconstituter::write_class_attributes() {
duke@435 575 u2 inner_classes_length = inner_classes_attribute_length();
coleenp@2497 576 Symbol* generic_signature = ikh()->generic_signature();
coleenp@4037 577 AnnotationArray* anno = ikh()->class_annotations();
duke@435 578
duke@435 579 int attr_count = 0;
coleenp@2497 580 if (generic_signature != NULL) {
duke@435 581 ++attr_count;
duke@435 582 }
duke@435 583 if (ikh()->source_file_name() != NULL) {
duke@435 584 ++attr_count;
duke@435 585 }
duke@435 586 if (ikh()->source_debug_extension() != NULL) {
duke@435 587 ++attr_count;
duke@435 588 }
duke@435 589 if (inner_classes_length > 0) {
duke@435 590 ++attr_count;
duke@435 591 }
coleenp@4037 592 if (anno != NULL) {
duke@435 593 ++attr_count; // has RuntimeVisibleAnnotations attribute
duke@435 594 }
duke@435 595
duke@435 596 write_u2(attr_count);
duke@435 597
coleenp@2497 598 if (generic_signature != NULL) {
coleenp@2497 599 write_signature_attribute(symbol_to_cpool_index(generic_signature));
duke@435 600 }
duke@435 601 if (ikh()->source_file_name() != NULL) {
duke@435 602 write_source_file_attribute();
duke@435 603 }
duke@435 604 if (ikh()->source_debug_extension() != NULL) {
duke@435 605 write_source_debug_extension_attribute();
duke@435 606 }
duke@435 607 if (inner_classes_length > 0) {
duke@435 608 write_inner_classes_attribute(inner_classes_length);
duke@435 609 }
coleenp@4037 610 if (anno != NULL) {
duke@435 611 write_annotations_attribute("RuntimeVisibleAnnotations", anno);
duke@435 612 }
duke@435 613 }
duke@435 614
duke@435 615 // Write the method information portion of ClassFile structure
duke@435 616 // JVMSpec| u2 methods_count;
duke@435 617 // JVMSpec| method_info methods[methods_count];
duke@435 618 void JvmtiClassFileReconstituter::write_method_infos() {
duke@435 619 HandleMark hm(thread());
coleenp@4037 620 Array<Method*>* methods = ikh()->methods();
duke@435 621 int num_methods = methods->length();
duke@435 622
duke@435 623 write_u2(num_methods);
duke@435 624 if (JvmtiExport::can_maintain_original_method_order()) {
duke@435 625 int index;
duke@435 626 int original_index;
coleenp@4037 627 intArray method_order(num_methods, 0);
duke@435 628
duke@435 629 // invert the method order mapping
duke@435 630 for (index = 0; index < num_methods; index++) {
coleenp@4037 631 original_index = ikh()->method_ordering()->at(index);
duke@435 632 assert(original_index >= 0 && original_index < num_methods,
duke@435 633 "invalid original method index");
coleenp@4037 634 method_order.at_put(original_index, index);
duke@435 635 }
duke@435 636
duke@435 637 // write in original order
duke@435 638 for (original_index = 0; original_index < num_methods; original_index++) {
coleenp@4037 639 index = method_order.at(original_index);
coleenp@4037 640 methodHandle method(thread(), methods->at(index));
duke@435 641 write_method_info(method);
duke@435 642 }
duke@435 643 } else {
duke@435 644 // method order not preserved just dump the method infos
duke@435 645 for (int index = 0; index < num_methods; index++) {
coleenp@4037 646 methodHandle method(thread(), methods->at(index));
duke@435 647 write_method_info(method);
duke@435 648 }
duke@435 649 }
duke@435 650 }
duke@435 651
duke@435 652 void JvmtiClassFileReconstituter::write_class_file_format() {
duke@435 653 ReallocMark();
duke@435 654
duke@435 655 // JVMSpec| ClassFile {
duke@435 656 // JVMSpec| u4 magic;
duke@435 657 write_u4(0xCAFEBABE);
duke@435 658
duke@435 659 // JVMSpec| u2 minor_version;
duke@435 660 // JVMSpec| u2 major_version;
duke@435 661 write_u2(ikh()->minor_version());
duke@435 662 u2 major = ikh()->major_version();
duke@435 663 write_u2(major);
duke@435 664
duke@435 665 // JVMSpec| u2 constant_pool_count;
duke@435 666 // JVMSpec| cp_info constant_pool[constant_pool_count-1];
duke@435 667 write_u2(cpool()->length());
duke@435 668 copy_cpool_bytes(writeable_address(cpool_size()));
duke@435 669
duke@435 670 // JVMSpec| u2 access_flags;
duke@435 671 write_u2(ikh()->access_flags().get_flags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
duke@435 672
duke@435 673 // JVMSpec| u2 this_class;
duke@435 674 // JVMSpec| u2 super_class;
duke@435 675 write_u2(class_symbol_to_cpool_index(ikh()->name()));
coleenp@4037 676 Klass* super_class = ikh()->super();
duke@435 677 write_u2(super_class == NULL? 0 : // zero for java.lang.Object
coleenp@4037 678 class_symbol_to_cpool_index(super_class->name()));
duke@435 679
duke@435 680 // JVMSpec| u2 interfaces_count;
duke@435 681 // JVMSpec| u2 interfaces[interfaces_count];
coleenp@4037 682 Array<Klass*>* interfaces = ikh()->local_interfaces();
duke@435 683 int num_interfaces = interfaces->length();
duke@435 684 write_u2(num_interfaces);
duke@435 685 for (int index = 0; index < num_interfaces; index++) {
duke@435 686 HandleMark hm(thread());
coleenp@4037 687 instanceKlassHandle iikh(thread(), interfaces->at(index));
duke@435 688 write_u2(class_symbol_to_cpool_index(iikh->name()));
duke@435 689 }
duke@435 690
duke@435 691 // JVMSpec| u2 fields_count;
duke@435 692 // JVMSpec| field_info fields[fields_count];
duke@435 693 write_field_infos();
duke@435 694
duke@435 695 // JVMSpec| u2 methods_count;
duke@435 696 // JVMSpec| method_info methods[methods_count];
duke@435 697 write_method_infos();
duke@435 698
duke@435 699 // JVMSpec| u2 attributes_count;
duke@435 700 // JVMSpec| attribute_info attributes[attributes_count];
duke@435 701 // JVMSpec| } /* end ClassFile 8?
duke@435 702 write_class_attributes();
duke@435 703 }
duke@435 704
duke@435 705 address JvmtiClassFileReconstituter::writeable_address(size_t size) {
duke@435 706 size_t used_size = _buffer_ptr - _buffer;
duke@435 707 if (size + used_size >= _buffer_size) {
duke@435 708 // compute the new buffer size: must be at least twice as big as before
duke@435 709 // plus whatever new is being used; then convert to nice clean block boundary
duke@435 710 size_t new_buffer_size = (size + _buffer_size*2 + 1) / initial_buffer_size
duke@435 711 * initial_buffer_size;
duke@435 712
duke@435 713 // VM goes belly-up if the memory isn't available, so cannot do OOM processing
duke@435 714 _buffer = REALLOC_RESOURCE_ARRAY(u1, _buffer, _buffer_size, new_buffer_size);
duke@435 715 _buffer_size = new_buffer_size;
duke@435 716 _buffer_ptr = _buffer + used_size;
duke@435 717 }
duke@435 718 u1* ret_ptr = _buffer_ptr;
duke@435 719 _buffer_ptr += size;
duke@435 720 return ret_ptr;
duke@435 721 }
duke@435 722
duke@435 723 void JvmtiClassFileReconstituter::write_attribute_name_index(const char* name) {
coleenp@2497 724 TempNewSymbol sym = SymbolTable::probe(name, (int)strlen(name));
duke@435 725 assert(sym != NULL, "attribute name symbol not found");
duke@435 726 u2 attr_name_index = symbol_to_cpool_index(sym);
duke@435 727 assert(attr_name_index != 0, "attribute name symbol not in constant pool");
duke@435 728 write_u2(attr_name_index);
duke@435 729 }
duke@435 730
duke@435 731 void JvmtiClassFileReconstituter::write_u1(u1 x) {
duke@435 732 *writeable_address(1) = x;
duke@435 733 }
duke@435 734
duke@435 735 void JvmtiClassFileReconstituter::write_u2(u2 x) {
duke@435 736 Bytes::put_Java_u2(writeable_address(2), x);
duke@435 737 }
duke@435 738
duke@435 739 void JvmtiClassFileReconstituter::write_u4(u4 x) {
duke@435 740 Bytes::put_Java_u4(writeable_address(4), x);
duke@435 741 }
duke@435 742
duke@435 743 void JvmtiClassFileReconstituter::write_u8(u8 x) {
duke@435 744 Bytes::put_Java_u8(writeable_address(8), x);
duke@435 745 }
duke@435 746
duke@435 747 void JvmtiClassFileReconstituter::copy_bytecodes(methodHandle mh,
duke@435 748 unsigned char* bytecodes) {
duke@435 749 // use a BytecodeStream to iterate over the bytecodes. JVM/fast bytecodes
duke@435 750 // and the breakpoint bytecode are converted to their original bytecodes.
duke@435 751
duke@435 752 BytecodeStream bs(mh);
duke@435 753
duke@435 754 unsigned char* p = bytecodes;
duke@435 755 Bytecodes::Code code;
coleenp@4251 756 bool is_rewritten = mh->method_holder()->is_rewritten();
duke@435 757
duke@435 758 while ((code = bs.next()) >= 0) {
duke@435 759 assert(Bytecodes::is_java_code(code), "sanity check");
duke@435 760 assert(code != Bytecodes::_breakpoint, "sanity check");
duke@435 761
duke@435 762 // length of bytecode (mnemonic + operands)
duke@435 763 address bcp = bs.bcp();
jrose@1920 764 int len = bs.instruction_size();
duke@435 765 assert(len > 0, "length must be > 0");
duke@435 766
duke@435 767 // copy the bytecodes
duke@435 768 *p = (unsigned char) (bs.is_wide()? Bytecodes::_wide : code);
duke@435 769 if (len > 1) {
duke@435 770 memcpy(p+1, bcp+1, len-1);
duke@435 771 }
duke@435 772
duke@435 773 // During linking the get/put and invoke instructions are rewritten
duke@435 774 // with an index into the constant pool cache. The original constant
duke@435 775 // pool index must be returned to caller. Rewrite the index.
coleenp@4037 776 if (is_rewritten && len > 1) {
coleenp@4037 777 bool is_wide = false;
duke@435 778 switch (code) {
duke@435 779 case Bytecodes::_getstatic : // fall through
duke@435 780 case Bytecodes::_putstatic : // fall through
duke@435 781 case Bytecodes::_getfield : // fall through
duke@435 782 case Bytecodes::_putfield : // fall through
duke@435 783 case Bytecodes::_invokevirtual : // fall through
duke@435 784 case Bytecodes::_invokespecial : // fall through
duke@435 785 case Bytecodes::_invokestatic : // fall through
jrose@1161 786 case Bytecodes::_invokedynamic : // fall through
coleenp@4037 787 case Bytecodes::_invokeinterface : {
dsamersoff@3626 788 assert(len == 3 ||
dsamersoff@3626 789 (code == Bytecodes::_invokeinterface && len == 5) ||
dsamersoff@3626 790 (code == Bytecodes::_invokedynamic && len == 5),
duke@435 791 "sanity check");
dsamersoff@3626 792
jrose@1161 793 int cpci = Bytes::get_native_u2(bcp+1);
jrose@1161 794 bool is_invokedynamic = (EnableInvokeDynamic && code == Bytecodes::_invokedynamic);
coleenp@4037 795 ConstantPoolCacheEntry* entry;
coleenp@4037 796 if (is_invokedynamic) {
jrose@1161 797 cpci = Bytes::get_native_u4(bcp+1);
coleenp@4037 798 entry = mh->constants()->invokedynamic_cp_cache_entry_at(cpci);
coleenp@4037 799 } else {
duke@435 800 // cache cannot be pre-fetched since some classes won't have it yet
coleenp@4037 801 entry = mh->constants()->cache()->entry_at(cpci);
coleenp@4037 802 }
duke@435 803 int i = entry->constant_pool_index();
duke@435 804 assert(i < mh->constants()->length(), "sanity check");
duke@435 805 Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering
jrose@1161 806 if (is_invokedynamic) *(p+3) = *(p+4) = 0;
duke@435 807 break;
duke@435 808 }
coleenp@4037 809 case Bytecodes::_ldc_w:
coleenp@4037 810 is_wide = true; // fall through
coleenp@4037 811 case Bytecodes::_ldc: {
coleenp@4037 812 if (bs.raw_code() == Bytecodes::_fast_aldc || bs.raw_code() == Bytecodes::_fast_aldc_w) {
coleenp@4037 813 int cpci = is_wide ? Bytes::get_native_u2(bcp+1) : (u1)(*(bcp+1));
coleenp@4037 814 int i = mh->constants()->object_to_cp_index(cpci);
coleenp@4037 815 assert(i < mh->constants()->length(), "sanity check");
coleenp@4037 816 if (is_wide) {
coleenp@4037 817 Bytes::put_Java_u2((address)(p+1), (u2)i); // java byte ordering
coleenp@4037 818 } else {
coleenp@4037 819 *(p+1) = (u1)i;
coleenp@4037 820 }
coleenp@4037 821 }
coleenp@4037 822 break;
coleenp@4037 823 }
coleenp@4037 824 }
duke@435 825 }
duke@435 826
duke@435 827 p += len;
duke@435 828 }
duke@435 829 }

mercurial