1 /* 2 * Copyright (c) 2003, 2024, 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 "interpreter/interpreter.hpp" 27 #include "memory/metadataFactory.hpp" 28 #include "memory/metaspaceClosure.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "oops/constMethod.hpp" 31 #include "oops/method.hpp" 32 #include "runtime/safepointVerifiers.hpp" 33 #include "runtime/signature.hpp" 34 #include "utilities/align.hpp" 35 #include "utilities/checkedCast.hpp" 36 37 // Static initialization 38 const u2 ConstMethod::MAX_IDNUM = 0xFFFE; 39 const u2 ConstMethod::UNSET_IDNUM = 0xFFFF; 40 41 ConstMethod* ConstMethod::allocate(ClassLoaderData* loader_data, 42 int byte_code_size, 43 InlineTableSizes* sizes, 44 MethodType method_type, 45 TRAPS) { 46 int size = ConstMethod::size(byte_code_size, sizes); 47 return new (loader_data, size, MetaspaceObj::ConstMethodType, THREAD) ConstMethod( 48 byte_code_size, sizes, method_type, size); 49 } 50 51 ConstMethod::ConstMethod(int byte_code_size, 52 InlineTableSizes* sizes, 53 MethodType method_type, 54 int size) { 55 56 NoSafepointVerifier no_safepoint; 57 init_fingerprint(); 58 set_constants(nullptr); 59 set_stackmap_data(nullptr); 60 set_code_size(byte_code_size); 61 set_constMethod_size(size); 62 set_inlined_tables_length(sizes); // sets _flags 63 set_method_type(method_type); 64 assert(this->size() == size, "wrong size for object"); 65 set_name_index(0); 66 set_signature_index(0); 67 set_constants(nullptr); 68 set_max_stack(0); 69 set_max_locals(0); 70 set_method_idnum(0); 71 set_size_of_parameters(0); 72 set_result_type((BasicType)0); 73 } 74 75 // Derive size of parameters, return type, and fingerprint, 76 // all in one pass, which is run at load time. 77 // We need the first two, and might as well grab the third. 78 void ConstMethod::compute_from_signature(Symbol* sig, bool is_static) { 79 // At this point, since we are scanning the signature, 80 // we might as well compute the whole fingerprint. 81 Fingerprinter fp(sig, is_static); 82 set_size_of_parameters(fp.size_of_parameters()); 83 set_num_stack_arg_slots(fp.num_stack_arg_slots()); 84 set_result_type(fp.return_type()); 85 set_fingerprint(fp.fingerprint()); 86 } 87 88 89 // Accessor that copies to metadata. 90 void ConstMethod::copy_stackmap_data(ClassLoaderData* loader_data, 91 u1* sd, int length, TRAPS) { 92 _stackmap_data = MetadataFactory::new_array<u1>(loader_data, length, CHECK); 93 memcpy((void*)_stackmap_data->adr_at(0), (void*)sd, length); 94 } 95 96 // Deallocate metadata fields associated with ConstMethod* 97 void ConstMethod::deallocate_contents(ClassLoaderData* loader_data) { 98 if (stackmap_data() != nullptr) { 99 MetadataFactory::free_array<u1>(loader_data, stackmap_data()); 100 } 101 set_stackmap_data(nullptr); 102 103 // deallocate annotation arrays 104 if (has_method_annotations()) 105 MetadataFactory::free_array<u1>(loader_data, method_annotations()); 106 if (has_parameter_annotations()) 107 MetadataFactory::free_array<u1>(loader_data, parameter_annotations()); 108 if (has_type_annotations()) 109 MetadataFactory::free_array<u1>(loader_data, type_annotations()); 110 if (has_default_annotations()) 111 MetadataFactory::free_array<u1>(loader_data, default_annotations()); 112 } 113 114 // How big must this constMethodObject be? 115 116 int ConstMethod::size(int code_size, 117 InlineTableSizes* sizes) { 118 int extra_bytes = code_size; 119 if (sizes->compressed_linenumber_size() > 0) { 120 extra_bytes += sizes->compressed_linenumber_size(); 121 } 122 if (sizes->checked_exceptions_length() > 0) { 123 extra_bytes += sizeof(u2); 124 extra_bytes += sizes->checked_exceptions_length() * (int)sizeof(CheckedExceptionElement); 125 } 126 if (sizes->localvariable_table_length() > 0) { 127 extra_bytes += sizeof(u2); 128 extra_bytes += 129 sizes->localvariable_table_length() * (int)sizeof(LocalVariableTableElement); 130 } 131 if (sizes->exception_table_length() > 0) { 132 extra_bytes += sizeof(u2); 133 extra_bytes += sizes->exception_table_length() * (int)sizeof(ExceptionTableElement); 134 } 135 if (sizes->generic_signature_index() != 0) { 136 extra_bytes += sizeof(u2); 137 } 138 // This has to be a less-than-or-equal check, because we might be 139 // storing information from a zero-length MethodParameters 140 // attribute. We have to store these, because in some cases, they 141 // cause the reflection API to throw a MalformedParametersException. 142 if (sizes->method_parameters_length() >= 0) { 143 extra_bytes += sizeof(u2); 144 extra_bytes += sizes->method_parameters_length() * (int)sizeof(MethodParametersElement); 145 } 146 147 // Align sizes up to a word. 148 extra_bytes = align_up(extra_bytes, BytesPerWord); 149 150 // One pointer per annotation array 151 if (sizes->method_annotations_length() > 0) { 152 extra_bytes += (int)sizeof(AnnotationArray*); 153 } 154 if (sizes->parameter_annotations_length() > 0) { 155 extra_bytes += (int)sizeof(AnnotationArray*); 156 } 157 if (sizes->type_annotations_length() > 0) { 158 extra_bytes += (int)sizeof(AnnotationArray*); 159 } 160 if (sizes->default_annotations_length() > 0) { 161 extra_bytes += (int)sizeof(AnnotationArray*); 162 } 163 164 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord; 165 assert(extra_words == extra_bytes/BytesPerWord, "should already be aligned"); 166 return align_metadata_size(header_size() + extra_words); 167 } 168 169 Method* ConstMethod::method() const { 170 return _constants->pool_holder()->method_with_idnum(_method_idnum); 171 } 172 173 // linenumber table - note that length is unknown until decompression, 174 // see class CompressedLineNumberReadStream. 175 176 u_char* ConstMethod::compressed_linenumber_table() const { 177 // Located immediately following the bytecodes. 178 assert(has_linenumber_table(), "called only if table is present"); 179 return code_end(); 180 } 181 182 // Last short in ConstMethod* before annotations 183 u2* ConstMethod::last_u2_element() const { 184 int offset = 0; 185 if (has_method_annotations()) offset++; 186 if (has_parameter_annotations()) offset++; 187 if (has_type_annotations()) offset++; 188 if (has_default_annotations()) offset++; 189 return (u2*)((AnnotationArray**)constMethod_end() - offset) - 1; 190 } 191 192 u2* ConstMethod::generic_signature_index_addr() const { 193 // Located at the end of the constMethod. 194 assert(has_generic_signature(), "called only if generic signature exists"); 195 return last_u2_element(); 196 } 197 198 u2* ConstMethod::method_parameters_length_addr() const { 199 assert(has_method_parameters(), "called only if table is present"); 200 return has_generic_signature() ? (last_u2_element() - 1) : 201 last_u2_element(); 202 } 203 204 u2* ConstMethod::checked_exceptions_length_addr() const { 205 // Located immediately before the generic signature index. 206 assert(has_checked_exceptions(), "called only if table is present"); 207 if(has_method_parameters()) { 208 // If method parameters present, locate immediately before them. 209 return (u2*)method_parameters_start() - 1; 210 } else { 211 // Else, the exception table is at the end of the constMethod. 212 return has_generic_signature() ? (last_u2_element() - 1) : 213 last_u2_element(); 214 } 215 } 216 217 u2* ConstMethod::exception_table_length_addr() const { 218 assert(has_exception_table(), "called only if table is present"); 219 if (has_checked_exceptions()) { 220 // If checked_exception present, locate immediately before them. 221 return (u2*) checked_exceptions_start() - 1; 222 } else { 223 if(has_method_parameters()) { 224 // If method parameters present, locate immediately before them. 225 return (u2*)method_parameters_start() - 1; 226 } else { 227 // Else, the exception table is at the end of the constMethod. 228 return has_generic_signature() ? (last_u2_element() - 1) : 229 last_u2_element(); 230 } 231 } 232 } 233 234 u2* ConstMethod::localvariable_table_length_addr() const { 235 assert(has_localvariable_table(), "called only if table is present"); 236 if (has_exception_table()) { 237 // If exception_table present, locate immediately before them. 238 return (u2*) exception_table_start() - 1; 239 } else { 240 if (has_checked_exceptions()) { 241 // If checked_exception present, locate immediately before them. 242 return (u2*) checked_exceptions_start() - 1; 243 } else { 244 if(has_method_parameters()) { 245 // If method parameters present, locate immediately before them. 246 return (u2*)method_parameters_start() - 1; 247 } else { 248 // Else, the exception table is at the end of the constMethod. 249 return has_generic_signature() ? (last_u2_element() - 1) : 250 last_u2_element(); 251 } 252 } 253 } 254 } 255 256 // Update the flags to indicate the presence of these optional fields. 257 void ConstMethod::set_inlined_tables_length(InlineTableSizes* sizes) { 258 if (sizes->compressed_linenumber_size() > 0) 259 set_has_linenumber_table(); 260 if (sizes->generic_signature_index() != 0) 261 set_has_generic_signature(); 262 if (sizes->method_parameters_length() >= 0) 263 set_has_method_parameters(); 264 if (sizes->checked_exceptions_length() > 0) 265 set_has_checked_exceptions(); 266 if (sizes->exception_table_length() > 0) 267 set_has_exception_table(); 268 if (sizes->localvariable_table_length() > 0) 269 set_has_localvariable_table(); 270 271 // annotations, they are all pointer sized embedded objects so don't have 272 // a length embedded also. 273 if (sizes->method_annotations_length() > 0) 274 set_has_method_annotations(); 275 if (sizes->parameter_annotations_length() > 0) 276 set_has_parameter_annotations(); 277 if (sizes->type_annotations_length() > 0) 278 set_has_type_annotations(); 279 if (sizes->default_annotations_length() > 0) 280 set_has_default_annotations(); 281 282 // This code is extremely brittle and should possibly be revised. 283 // The *_length_addr functions walk backwards through the 284 // constMethod data, using each of the length indexes ahead of them, 285 // as well as the flags variable. Therefore, the indexes must be 286 // initialized in reverse order, or else they will compute the wrong 287 // offsets. Moving the initialization of _flags into a separate 288 // block solves *half* of the problem, but the following part will 289 // still break if the order is not exactly right. 290 // 291 // Also, the servicability agent needs to be informed anytime 292 // anything is added here. It might be advisable to have some sort 293 // of indication of this inline. 294 if (sizes->generic_signature_index() != 0) 295 *(generic_signature_index_addr()) = checked_cast<u2>(sizes->generic_signature_index()); 296 // New data should probably go here. 297 if (sizes->method_parameters_length() >= 0) 298 *(method_parameters_length_addr()) = checked_cast<u2>(sizes->method_parameters_length()); 299 if (sizes->checked_exceptions_length() > 0) 300 *(checked_exceptions_length_addr()) = checked_cast<u2>(sizes->checked_exceptions_length()); 301 if (sizes->exception_table_length() > 0) 302 *(exception_table_length_addr()) = checked_cast<u2>(sizes->exception_table_length()); 303 if (sizes->localvariable_table_length() > 0) 304 *(localvariable_table_length_addr()) = checked_cast<u2>(sizes->localvariable_table_length()); 305 } 306 307 int ConstMethod::method_parameters_length() const { 308 return has_method_parameters() ? *(method_parameters_length_addr()) : -1; 309 } 310 311 MethodParametersElement* ConstMethod::method_parameters_start() const { 312 u2* addr = method_parameters_length_addr(); 313 u2 length = *addr; 314 addr -= length * sizeof(MethodParametersElement) / sizeof(u2); 315 return (MethodParametersElement*) addr; 316 } 317 318 319 u2 ConstMethod::checked_exceptions_length() const { 320 return has_checked_exceptions() ? *(checked_exceptions_length_addr()) : 0; 321 } 322 323 324 CheckedExceptionElement* ConstMethod::checked_exceptions_start() const { 325 u2* addr = checked_exceptions_length_addr(); 326 u2 length = *addr; 327 assert(length > 0, "should only be called if table is present"); 328 addr -= length * sizeof(CheckedExceptionElement) / sizeof(u2); 329 return (CheckedExceptionElement*) addr; 330 } 331 332 333 u2 ConstMethod::localvariable_table_length() const { 334 return has_localvariable_table() ? *(localvariable_table_length_addr()) : 0; 335 } 336 337 338 LocalVariableTableElement* ConstMethod::localvariable_table_start() const { 339 u2* addr = localvariable_table_length_addr(); 340 u2 length = *addr; 341 assert(length > 0, "should only be called if table is present"); 342 addr -= length * sizeof(LocalVariableTableElement) / sizeof(u2); 343 return (LocalVariableTableElement*) addr; 344 } 345 346 u2 ConstMethod::exception_table_length() const { 347 return has_exception_table() ? *(exception_table_length_addr()) : 0; 348 } 349 350 ExceptionTableElement* ConstMethod::exception_table_start() const { 351 u2* addr = exception_table_length_addr(); 352 u2 length = *addr; 353 assert(length > 0, "should only be called if table is present"); 354 addr -= length * sizeof(ExceptionTableElement) / sizeof(u2); 355 return (ExceptionTableElement*)addr; 356 } 357 358 AnnotationArray** ConstMethod::method_annotations_addr() const { 359 assert(has_method_annotations(), "should only be called if method annotations are present"); 360 return (AnnotationArray**)constMethod_end() - 1; 361 } 362 363 AnnotationArray** ConstMethod::parameter_annotations_addr() const { 364 assert(has_parameter_annotations(), "should only be called if method parameter annotations are present"); 365 int offset = 1; 366 if (has_method_annotations()) offset++; 367 return (AnnotationArray**)constMethod_end() - offset; 368 } 369 370 AnnotationArray** ConstMethod::type_annotations_addr() const { 371 assert(has_type_annotations(), "should only be called if method type annotations are present"); 372 int offset = 1; 373 if (has_method_annotations()) offset++; 374 if (has_parameter_annotations()) offset++; 375 return (AnnotationArray**)constMethod_end() - offset; 376 } 377 378 AnnotationArray** ConstMethod::default_annotations_addr() const { 379 assert(has_default_annotations(), "should only be called if method default annotations are present"); 380 int offset = 1; 381 if (has_method_annotations()) offset++; 382 if (has_parameter_annotations()) offset++; 383 if (has_type_annotations()) offset++; 384 return (AnnotationArray**)constMethod_end() - offset; 385 } 386 387 static Array<u1>* copy_annotations(ClassLoaderData* loader_data, AnnotationArray* from, TRAPS) { 388 int length = from->length(); 389 Array<u1>* a = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL); 390 memcpy((void*)a->adr_at(0), (void*)from->adr_at(0), length); 391 return a; 392 } 393 394 // copy annotations from 'cm' to 'this' 395 // Must make copy because these are deallocated with their constMethod, if redefined. 396 void ConstMethod::copy_annotations_from(ClassLoaderData* loader_data, ConstMethod* cm, TRAPS) { 397 Array<u1>* a; 398 if (cm->has_method_annotations()) { 399 assert(has_method_annotations(), "should be allocated already"); 400 a = copy_annotations(loader_data, cm->method_annotations(), CHECK); 401 set_method_annotations(a); 402 } 403 if (cm->has_parameter_annotations()) { 404 assert(has_parameter_annotations(), "should be allocated already"); 405 a = copy_annotations(loader_data, cm->parameter_annotations(), CHECK); 406 set_parameter_annotations(a); 407 } 408 if (cm->has_type_annotations()) { 409 assert(has_type_annotations(), "should be allocated already"); 410 a = copy_annotations(loader_data, cm->type_annotations(), CHECK); 411 set_type_annotations(a); 412 } 413 if (cm->has_default_annotations()) { 414 assert(has_default_annotations(), "should be allocated already"); 415 a = copy_annotations(loader_data, cm->default_annotations(), CHECK); 416 set_default_annotations(a); 417 } 418 } 419 420 void ConstMethod::metaspace_pointers_do(MetaspaceClosure* it) { 421 log_trace(cds)("Iter(ConstMethod): %p", this); 422 423 if (!method()->method_holder()->is_rewritten()) { 424 it->push(&_constants, MetaspaceClosure::_writable); 425 } else { 426 it->push(&_constants); 427 } 428 it->push(&_stackmap_data); 429 if (has_method_annotations()) { 430 it->push(method_annotations_addr()); 431 } 432 if (has_parameter_annotations()) { 433 it->push(parameter_annotations_addr()); 434 } 435 if (has_type_annotations()) { 436 it->push(type_annotations_addr()); 437 } 438 if (has_default_annotations()) { 439 it->push(default_annotations_addr()); 440 } 441 } 442 443 // Printing 444 445 void ConstMethod::print_on(outputStream* st) const { 446 ResourceMark rm; 447 st->print_cr("%s", internal_name()); 448 Method* m = method(); 449 st->print(" - method: " PTR_FORMAT " ", p2i(m)); 450 if (m != nullptr) { 451 m->print_value_on(st); 452 } 453 st->cr(); 454 st->print(" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr(); 455 if (has_stackmap_table()) { 456 st->print(" - stackmap data: "); 457 stackmap_data()->print_value_on(st); 458 st->cr(); 459 } 460 } 461 462 // Short version of printing ConstMethod* - just print the name of the 463 // method it belongs to. 464 void ConstMethod::print_value_on(outputStream* st) const { 465 st->print(" const part of method " ); 466 Method* m = method(); 467 if (m != nullptr) { 468 m->print_value_on(st); 469 } else { 470 st->print("null"); 471 } 472 } 473 474 // Verification 475 476 void ConstMethod::verify_on(outputStream* st) { 477 // Verification can occur during oop construction before the method or 478 // other fields have been initialized. 479 guarantee(method() != nullptr && method()->is_method(), "should be method"); 480 481 address m_end = (address)((intptr_t) this + size()); 482 address compressed_table_start = code_end(); 483 guarantee(compressed_table_start <= m_end, "invalid method layout"); 484 address compressed_table_end = compressed_table_start; 485 // Verify line number table 486 if (has_linenumber_table()) { 487 CompressedLineNumberReadStream stream(compressed_linenumber_table()); 488 while (stream.read_pair()) { 489 guarantee(stream.bci() >= 0 && stream.bci() <= code_size(), "invalid bci in line number table"); 490 } 491 compressed_table_end += stream.position(); 492 } 493 guarantee(compressed_table_end <= m_end, "invalid method layout"); 494 // Verify checked exceptions, exception table and local variable tables 495 if (has_method_parameters()) { 496 u2* addr = method_parameters_length_addr(); 497 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 498 } 499 if (has_checked_exceptions()) { 500 u2* addr = checked_exceptions_length_addr(); 501 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 502 } 503 if (has_exception_table()) { 504 u2* addr = exception_table_length_addr(); 505 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 506 } 507 if (has_localvariable_table()) { 508 u2* addr = localvariable_table_length_addr(); 509 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout"); 510 } 511 // Check compressed_table_end relative to uncompressed_table_start 512 u2* uncompressed_table_start; 513 if (has_localvariable_table()) { 514 uncompressed_table_start = (u2*) localvariable_table_start(); 515 } else if (has_exception_table()) { 516 uncompressed_table_start = (u2*) exception_table_start(); 517 } else if (has_checked_exceptions()) { 518 uncompressed_table_start = (u2*) checked_exceptions_start(); 519 } else if (has_method_parameters()) { 520 uncompressed_table_start = (u2*) method_parameters_start(); 521 } else { 522 uncompressed_table_start = (u2*) m_end; 523 } 524 int gap = int((intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end); 525 int max_gap = align_metadata_size(1)*BytesPerWord; 526 guarantee(gap >= 0 && gap < max_gap, "invalid method layout"); 527 }