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