1 /* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc/shared/gcLocker.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "memory/heapInspection.hpp" 29 #include "memory/metadataFactory.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "oops/constMethod.hpp" 32 #include "oops/method.hpp" 33 34 // Static initialization 35 const u2 ConstMethod::MAX_IDNUM = 0xFFFE; 36 const u2 ConstMethod::UNSET_IDNUM = 0xFFFF; 37 38 ConstMethod* ConstMethod::allocate(ClassLoaderData* loader_data, 39 int byte_code_size, 40 InlineTableSizes* sizes, 41 MethodType method_type, 42 TRAPS) { 43 int size = ConstMethod::size(byte_code_size, sizes); 44 return new (loader_data, size, true, MetaspaceObj::ConstMethodType, THREAD) ConstMethod( 45 byte_code_size, sizes, method_type, size); 46 } 47 48 ConstMethod::ConstMethod(int byte_code_size, 49 InlineTableSizes* sizes, 50 MethodType method_type, 51 int size) { 52 53 NoSafepointVerifier no_safepoint; 54 init_fingerprint(); 55 set_constants(NULL); 56 set_stackmap_data(NULL); 57 set_code_size(byte_code_size); 58 set_constMethod_size(size); 59 set_inlined_tables_length(sizes); // sets _flags 60 set_method_type(method_type); 61 assert(this->size() == size, "wrong size for object"); 62 set_name_index(0); 63 set_signature_index(0); 64 set_constants(NULL); 65 set_max_stack(0); 66 set_max_locals(0); 67 set_method_idnum(0); 68 set_size_of_parameters(0); 69 } 70 71 // Accessor that copies to metadata. 72 void ConstMethod::copy_stackmap_data(ClassLoaderData* loader_data, 73 u1* sd, int length, TRAPS) { 74 _stackmap_data = MetadataFactory::new_array<u1>(loader_data, length, CHECK); 75 memcpy((void*)_stackmap_data->adr_at(0), (void*)sd, length); 76 } 77 78 // Deallocate metadata fields associated with ConstMethod* 79 void ConstMethod::deallocate_contents(ClassLoaderData* loader_data) { 80 if (stackmap_data() != NULL) { 81 MetadataFactory::free_array<u1>(loader_data, stackmap_data()); 82 } 83 set_stackmap_data(NULL); 84 85 // deallocate annotation arrays 86 if (has_method_annotations()) 87 MetadataFactory::free_array<u1>(loader_data, method_annotations()); 88 if (has_parameter_annotations()) 89 MetadataFactory::free_array<u1>(loader_data, parameter_annotations()); 90 if (has_type_annotations()) 91 MetadataFactory::free_array<u1>(loader_data, type_annotations()); 92 if (has_default_annotations()) 93 MetadataFactory::free_array<u1>(loader_data, default_annotations()); 94 } 95 96 // How big must this constMethodObject be? 97 98 int ConstMethod::size(int code_size, 99 InlineTableSizes* sizes) { 100 int extra_bytes = code_size; 101 if (sizes->compressed_linenumber_size() > 0) { 102 extra_bytes += sizes->compressed_linenumber_size(); 103 } 104 if (sizes->checked_exceptions_length() > 0) { 105 extra_bytes += sizeof(u2); 106 extra_bytes += sizes->checked_exceptions_length() * sizeof(CheckedExceptionElement); 107 } 108 if (sizes->localvariable_table_length() > 0) { 109 extra_bytes += sizeof(u2); 110 extra_bytes += 111 sizes->localvariable_table_length() * sizeof(LocalVariableTableElement); 112 } 113 if (sizes->exception_table_length() > 0) { 114 extra_bytes += sizeof(u2); 115 extra_bytes += sizes->exception_table_length() * sizeof(ExceptionTableElement); 116 } 117 if (sizes->generic_signature_index() != 0) { 118 extra_bytes += sizeof(u2); 119 } 120 // This has to be a less-than-or-equal check, because we might be 121 // storing information from a zero-length MethodParameters 122 // attribute. We have to store these, because in some cases, they 123 // cause the reflection API to throw a MalformedParametersException. 124 if (sizes->method_parameters_length() >= 0) { 125 extra_bytes += sizeof(u2); 126 extra_bytes += sizes->method_parameters_length() * sizeof(MethodParametersElement); 127 } 128 129 // Align sizes up to a word. 130 extra_bytes = align_size_up(extra_bytes, BytesPerWord); 131 132 // One pointer per annotation array 133 if (sizes->method_annotations_length() > 0) { 134 extra_bytes += sizeof(AnnotationArray*); 135 } 136 if (sizes->parameter_annotations_length() > 0) { 137 extra_bytes += sizeof(AnnotationArray*); 138 } 139 if (sizes->type_annotations_length() > 0) { 140 extra_bytes += sizeof(AnnotationArray*); 141 } 142 if (sizes->default_annotations_length() > 0) { 143 extra_bytes += sizeof(AnnotationArray*); 144 } 145 146 int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord; 147 assert(extra_words == extra_bytes/BytesPerWord, "should already be aligned"); 148 return align_metadata_size(header_size() + extra_words); 149 } 150 151 Method* ConstMethod::method() const { 152 return _constants->pool_holder()->method_with_idnum(_method_idnum); 153 } 154 155 // linenumber table - note that length is unknown until decompression, 156 // see class CompressedLineNumberReadStream. 157 158 u_char* ConstMethod::compressed_linenumber_table() const { 159 // Located immediately following the bytecodes. 160 assert(has_linenumber_table(), "called only if table is present"); 161 return code_end(); 162 } 163 164 // Last short in ConstMethod* before annotations 165 u2* ConstMethod::last_u2_element() const { 166 int offset = 0; 167 if (has_method_annotations()) offset++; 168 if (has_parameter_annotations()) offset++; 169 if (has_type_annotations()) offset++; 170 if (has_default_annotations()) offset++; 171 return (u2*)((AnnotationArray**)constMethod_end() - offset) - 1; 172 } 173 174 u2* ConstMethod::generic_signature_index_addr() const { 175 // Located at the end of the constMethod. 176 assert(has_generic_signature(), "called only if generic signature exists"); 177 return last_u2_element(); 178 } 179 180 u2* ConstMethod::method_parameters_length_addr() const { 181 assert(has_method_parameters(), "called only if table is present"); 182 return has_generic_signature() ? (last_u2_element() - 1) : 183 last_u2_element(); 184 } 185 186 u2* ConstMethod::checked_exceptions_length_addr() const { 187 // Located immediately before the generic signature index. 188 assert(has_checked_exceptions(), "called only if table is present"); 189 if(has_method_parameters()) { 190 // If method parameters present, locate immediately before them. 191 return (u2*)method_parameters_start() - 1; 192 } else { 193 // Else, the exception table is at the end of the constMethod. 194 return has_generic_signature() ? (last_u2_element() - 1) : 195 last_u2_element(); 196 } 197 } 198 199 u2* ConstMethod::exception_table_length_addr() const { 200 assert(has_exception_handler(), "called only if table is present"); 201 if (has_checked_exceptions()) { 202 // If checked_exception present, locate immediately before them. 203 return (u2*) checked_exceptions_start() - 1; 204 } else { 205 if(has_method_parameters()) { 206 // If method parameters present, locate immediately before them. 207 return (u2*)method_parameters_start() - 1; 208 } else { 209 // Else, the exception table is at the end of the constMethod. 210 return has_generic_signature() ? (last_u2_element() - 1) : 211 last_u2_element(); 212 } 213 } 214 } 215 216 u2* ConstMethod::localvariable_table_length_addr() const { 217 assert(has_localvariable_table(), "called only if table is present"); 218 if (has_exception_handler()) { 219 // If exception_table present, locate immediately before them. 220 return (u2*) exception_table_start() - 1; 221 } else { 222 if (has_checked_exceptions()) { 223 // If checked_exception present, locate immediately before them. 224 return (u2*) checked_exceptions_start() - 1; 225 } else { 226 if(has_method_parameters()) { 227 // If method parameters present, locate immediately before them. 228 return (u2*)method_parameters_start() - 1; 229 } else { 230 // Else, the exception table is at the end of the constMethod. 231 return has_generic_signature() ? (last_u2_element() - 1) : 232 last_u2_element(); 233 } 234 } 235 } 236 } 237 238 // Update the flags to indicate the presence of these optional fields. 239 void ConstMethod::set_inlined_tables_length(InlineTableSizes* sizes) { 240 _flags = 0; 241 if (sizes->compressed_linenumber_size() > 0) 242 _flags |= _has_linenumber_table; 243 if (sizes->generic_signature_index() != 0) 244 _flags |= _has_generic_signature; 245 if (sizes->method_parameters_length() >= 0) 246 _flags |= _has_method_parameters; 247 if (sizes->checked_exceptions_length() > 0) 248 _flags |= _has_checked_exceptions; 249 if (sizes->exception_table_length() > 0) 250 _flags |= _has_exception_table; 251 if (sizes->localvariable_table_length() > 0) 252 _flags |= _has_localvariable_table; 253 254 // annotations, they are all pointer sized embedded objects so don't have 255 // a length embedded also. 256 if (sizes->method_annotations_length() > 0) 257 _flags |= _has_method_annotations; 258 if (sizes->parameter_annotations_length() > 0) 259 _flags |= _has_parameter_annotations; 260 if (sizes->type_annotations_length() > 0) 261 _flags |= _has_type_annotations; 262 if (sizes->default_annotations_length() > 0) 263 _flags |= _has_default_annotations; 264 265 // This code is extremely brittle and should possibly be revised. 266 // The *_length_addr functions walk backwards through the 267 // constMethod data, using each of the length indexes ahead of them, 268 // as well as the flags variable. Therefore, the indexes must be 269 // initialized in reverse order, or else they will compute the wrong 270 // offsets. Moving the initialization of _flags into a separate 271 // block solves *half* of the problem, but the following part will 272 // still break if the order is not exactly right. 273 // 274 // Also, the servicability agent needs to be informed anytime 275 // anything is added here. It might be advisable to have some sort 276 // of indication of this inline. 277 if (sizes->generic_signature_index() != 0) 278 *(generic_signature_index_addr()) = sizes->generic_signature_index(); 279 // New data should probably go here. 280 if (sizes->method_parameters_length() >= 0) 281 *(method_parameters_length_addr()) = sizes->method_parameters_length(); 282 if (sizes->checked_exceptions_length() > 0) 283 *(checked_exceptions_length_addr()) = sizes->checked_exceptions_length(); 284 if (sizes->exception_table_length() > 0) 285 *(exception_table_length_addr()) = sizes->exception_table_length(); 286 if (sizes->localvariable_table_length() > 0) 287 *(localvariable_table_length_addr()) = sizes->localvariable_table_length(); 288 } 289 290 int ConstMethod::method_parameters_length() const { 291 return has_method_parameters() ? *(method_parameters_length_addr()) : -1; 292 } 293 294 MethodParametersElement* ConstMethod::method_parameters_start() const { 295 u2* addr = method_parameters_length_addr(); 296 u2 length = *addr; 297 addr -= length * sizeof(MethodParametersElement) / sizeof(u2); 298 return (MethodParametersElement*) addr; 299 } 300 301 302 int ConstMethod::checked_exceptions_length() const { 303 return has_checked_exceptions() ? *(checked_exceptions_length_addr()) : 0; 304 } 305 306 307 CheckedExceptionElement* ConstMethod::checked_exceptions_start() const { 308 u2* addr = checked_exceptions_length_addr(); 309 u2 length = *addr; 310 assert(length > 0, "should only be called if table is present"); 311 addr -= length * sizeof(CheckedExceptionElement) / sizeof(u2); 312 return (CheckedExceptionElement*) addr; 313 } 314 315 316 int ConstMethod::localvariable_table_length() const { 317 return has_localvariable_table() ? *(localvariable_table_length_addr()) : 0; 318 } 319 320 321 LocalVariableTableElement* ConstMethod::localvariable_table_start() const { 322 u2* addr = localvariable_table_length_addr(); 323 u2 length = *addr; 324 assert(length > 0, "should only be called if table is present"); 325 addr -= length * sizeof(LocalVariableTableElement) / sizeof(u2); 326 return (LocalVariableTableElement*) addr; 327 } 328 329 int ConstMethod::exception_table_length() const { 330 return has_exception_handler() ? *(exception_table_length_addr()) : 0; 331 } 332 333 ExceptionTableElement* ConstMethod::exception_table_start() const { 334 u2* addr = exception_table_length_addr(); 335 u2 length = *addr; 336 assert(length > 0, "should only be called if table is present"); 337 addr -= length * sizeof(ExceptionTableElement) / sizeof(u2); 338 return (ExceptionTableElement*)addr; 339 } 340 341 AnnotationArray** ConstMethod::method_annotations_addr() const { 342 assert(has_method_annotations(), "should only be called if method annotations are present"); 343 return (AnnotationArray**)constMethod_end() - 1; 344 } 345 346 AnnotationArray** ConstMethod::parameter_annotations_addr() const { 347 assert(has_parameter_annotations(), "should only be called if method parameter annotations are present"); 348 int offset = 1; 349 if (has_method_annotations()) offset++; 350 return (AnnotationArray**)constMethod_end() - offset; 351 } 352 353 AnnotationArray** ConstMethod::type_annotations_addr() const { 354 assert(has_type_annotations(), "should only be called if method type annotations are present"); 355 int offset = 1; 356 if (has_method_annotations()) offset++; 357 if (has_parameter_annotations()) offset++; 358 return (AnnotationArray**)constMethod_end() - offset; 359 } 360 361 AnnotationArray** ConstMethod::default_annotations_addr() const { 362 assert(has_default_annotations(), "should only be called if method default annotations are present"); 363 int offset = 1; 364 if (has_method_annotations()) offset++; 365 if (has_parameter_annotations()) offset++; 366 if (has_type_annotations()) offset++; 367 return (AnnotationArray**)constMethod_end() - offset; 368 } 369 370 // copy annotations from 'cm' to 'this' 371 void ConstMethod::copy_annotations_from(ConstMethod* cm) { 372 if (cm->has_method_annotations()) { 373 assert(has_method_annotations(), "should be allocated already"); 374 set_method_annotations(cm->method_annotations()); 375 } 376 if (cm->has_parameter_annotations()) { 377 assert(has_parameter_annotations(), "should be allocated already"); 378 set_parameter_annotations(cm->parameter_annotations()); 379 } 380 if (cm->has_type_annotations()) { 381 assert(has_type_annotations(), "should be allocated already"); 382 set_type_annotations(cm->type_annotations()); 383 } 384 if (cm->has_default_annotations()) { 385 assert(has_default_annotations(), "should be allocated already"); 386 set_default_annotations(cm->default_annotations()); 387 } 388 } 389 390 // Printing 391 392 void ConstMethod::print_on(outputStream* st) const { 393 ResourceMark rm; 394 assert(is_constMethod(), "must be constMethod"); 395 st->print_cr("%s", internal_name()); 396 st->print(" - method: " INTPTR_FORMAT " ", p2i((address)method())); 397 method()->print_value_on(st); st->cr(); 398 if (has_stackmap_table()) { 399 st->print(" - stackmap data: "); 400 stackmap_data()->print_value_on(st); 401 st->cr(); 402 } 403 } 404 405 // Short version of printing ConstMethod* - just print the name of the 406 // method it belongs to. 407 void ConstMethod::print_value_on(outputStream* st) const { 408 assert(is_constMethod(), "must be constMethod"); 409 st->print(" const part of method " ); 410 method()->print_value_on(st); 411 } 412 413 #if INCLUDE_SERVICES 414 // Size Statistics 415 void ConstMethod::collect_statistics(KlassSizeStats *sz) const { 416 int n1, n2, n3; 417 sz->_const_method_bytes += (n1 = sz->count(this)); 418 sz->_bytecode_bytes += (n2 = code_size()); 419 sz->_stackmap_bytes += (n3 = sz->count_array(stackmap_data())); 420 421 // Count method annotations 422 int a1 = 0, a2 = 0, a3 = 0, a4 = 0; 423 if (has_method_annotations()) { 424 sz->_methods_annotations_bytes += (a1 = sz->count_array(method_annotations())); 425 } 426 if (has_parameter_annotations()) { 427 sz->_methods_parameter_annotations_bytes += (a2 = sz->count_array(parameter_annotations())); 428 } 429 if (has_type_annotations()) { 430 sz->_methods_type_annotations_bytes += (a3 = sz->count_array(type_annotations())); 431 } 432 if (has_default_annotations()) { 433 sz->_methods_default_annotations_bytes += (a4 = sz->count_array(default_annotations())); 434 } 435 436 int size_annotations = a1 + a2 + a3 + a4; 437 438 sz->_method_all_bytes += n1 + n3 + size_annotations; // note: n2 is part of n3 439 sz->_ro_bytes += n1 + n3 + size_annotations; 440 } 441 #endif // INCLUDE_SERVICES 442 443 // Verification 444 445 void ConstMethod::verify_on(outputStream* st) { 446 guarantee(is_constMethod(), "object must be constMethod"); 447 448 // Verification can occur during oop construction before the method or 449 // other fields have been initialized. 450 guarantee(method()->is_method(), "should be method"); 451 452 address m_end = (address)((intptr_t) this + size()); 453 address compressed_table_start = code_end(); 454 guarantee(compressed_table_start <= m_end, "invalid method layout"); 455 address compressed_table_end = compressed_table_start; 456 // Verify line number table 457 if (has_linenumber_table()) { 458 CompressedLineNumberReadStream stream(compressed_linenumber_table()); 459 while (stream.read_pair()) { 460 guarantee(stream.bci() >= 0 && stream.bci() <= code_size(), "invalid bci in line number table"); 461 } 462 compressed_table_end += stream.position(); 463 } 464 guarantee(compressed_table_end <= m_end, "invalid method layout"); 465 // Verify checked exceptions, exception table and local variable tables 466 if (has_method_parameters()) { 467 u2* addr = method_parameters_length_addr(); 468 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 469 } 470 if (has_checked_exceptions()) { 471 u2* addr = checked_exceptions_length_addr(); 472 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 473 } 474 if (has_exception_handler()) { 475 u2* addr = exception_table_length_addr(); 476 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 477 } 478 if (has_localvariable_table()) { 479 u2* addr = localvariable_table_length_addr(); 480 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 481 } 482 // Check compressed_table_end relative to uncompressed_table_start 483 u2* uncompressed_table_start; 484 if (has_localvariable_table()) { 485 uncompressed_table_start = (u2*) localvariable_table_start(); 486 } else if (has_exception_handler()) { 487 uncompressed_table_start = (u2*) exception_table_start(); 488 } else if (has_checked_exceptions()) { 489 uncompressed_table_start = (u2*) checked_exceptions_start(); 490 } else if (has_method_parameters()) { 491 uncompressed_table_start = (u2*) method_parameters_start(); 492 } else { 493 uncompressed_table_start = (u2*) m_end; 494 } 495 int gap = (intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end; 496 int max_gap = align_metadata_size(1)*BytesPerWord; 497 guarantee(gap >= 0 && gap < max_gap, "invalid method layout"); 498 }