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