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 "interpreter/interpreter.hpp"
27 #include "memory/gcLocker.hpp"
28 #include "memory/heapInspection.hpp"
29 #include "memory/metadataFactory.hpp"
30 #include "oops/constMethod.hpp"
31 #include "oops/method.hpp"
32 
33 // Static initialization
34 const u2 ConstMethod::MAX_IDNUM   = 0xFFFE;
35 const u2 ConstMethod::UNSET_IDNUM = 0xFFFF;
36 
allocate(ClassLoaderData * loader_data,int byte_code_size,InlineTableSizes * sizes,MethodType method_type,TRAPS)37 ConstMethod* ConstMethod::allocate(ClassLoaderData* loader_data,
38                                    int byte_code_size,
39                                    InlineTableSizes* sizes,
40                                    MethodType method_type,
41                                    TRAPS) {
42   int size = ConstMethod::size(byte_code_size, sizes);
43   return new (loader_data, size, true, MetaspaceObj::ConstMethodType, THREAD) ConstMethod(
44       byte_code_size, sizes, method_type, size);
45 }
46 
ConstMethod(int byte_code_size,InlineTableSizes * sizes,MethodType method_type,int size)47 ConstMethod::ConstMethod(int byte_code_size,
48                          InlineTableSizes* sizes,
49                          MethodType method_type,
50                          int size) {
51 
52   No_Safepoint_Verifier no_safepoint;
53   init_fingerprint();
54   set_constants(NULL);
55   set_stackmap_data(NULL);
56   set_code_size(byte_code_size);
57   set_constMethod_size(size);
58   set_inlined_tables_length(sizes); // sets _flags
59   set_method_type(method_type);
60   assert(this->size() == size, "wrong size for object");
61   set_name_index(0);
62   set_signature_index(0);
63   set_constants(NULL);
64   set_max_stack(0);
65   set_max_locals(0);
66   set_method_idnum(0);
67   set_size_of_parameters(0);
68   set_result_type(T_VOID);
69 }
70 
71 // Accessor that copies to metadata.
copy_stackmap_data(ClassLoaderData * loader_data,u1 * sd,int length,TRAPS)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*
deallocate_contents(ClassLoaderData * loader_data)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 
size(int code_size,InlineTableSizes * sizes)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   if (sizes->method_parameters_length() > 0) {
121     extra_bytes += sizeof(u2);
122     extra_bytes += sizes->method_parameters_length() * sizeof(MethodParametersElement);
123   }
124 
125   // Align sizes up to a word.
126   extra_bytes = align_size_up(extra_bytes, BytesPerWord);
127 
128   // One pointer per annotation array
129   if (sizes->method_annotations_length() > 0) {
130     extra_bytes += sizeof(AnnotationArray*);
131   }
132   if (sizes->parameter_annotations_length() > 0) {
133     extra_bytes += sizeof(AnnotationArray*);
134   }
135   if (sizes->type_annotations_length() > 0) {
136     extra_bytes += sizeof(AnnotationArray*);
137   }
138   if (sizes->default_annotations_length() > 0) {
139     extra_bytes += sizeof(AnnotationArray*);
140   }
141 
142   int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord;
143   assert(extra_words == extra_bytes/BytesPerWord, "should already be aligned");
144   return align_object_size(header_size() + extra_words);
145 }
146 
method() const147 Method* ConstMethod::method() const {
148     return _constants->pool_holder()->method_with_idnum(_method_idnum);
149   }
150 
151 // linenumber table - note that length is unknown until decompression,
152 // see class CompressedLineNumberReadStream.
153 
compressed_linenumber_table() const154 u_char* ConstMethod::compressed_linenumber_table() const {
155   // Located immediately following the bytecodes.
156   assert(has_linenumber_table(), "called only if table is present");
157   return code_end();
158 }
159 
160 // Last short in ConstMethod* before annotations
last_u2_element() const161 u2* ConstMethod::last_u2_element() const {
162   int offset = 0;
163   if (has_method_annotations()) offset++;
164   if (has_parameter_annotations()) offset++;
165   if (has_type_annotations()) offset++;
166   if (has_default_annotations()) offset++;
167   return (u2*)((AnnotationArray**)constMethod_end() - offset) - 1;
168 }
169 
generic_signature_index_addr() const170 u2* ConstMethod::generic_signature_index_addr() const {
171   // Located at the end of the constMethod.
172   assert(has_generic_signature(), "called only if generic signature exists");
173   return last_u2_element();
174 }
175 
method_parameters_length_addr() const176 u2* ConstMethod::method_parameters_length_addr() const {
177   assert(has_method_parameters(), "called only if table is present");
178   return has_generic_signature() ? (last_u2_element() - 1) :
179                                     last_u2_element();
180 }
181 
checked_exceptions_length_addr() const182 u2* ConstMethod::checked_exceptions_length_addr() const {
183   // Located immediately before the generic signature index.
184   assert(has_checked_exceptions(), "called only if table is present");
185   if(has_method_parameters()) {
186     // If method parameters present, locate immediately before them.
187     return (u2*)method_parameters_start() - 1;
188   } else {
189     // Else, the exception table is at the end of the constMethod.
190     return has_generic_signature() ? (last_u2_element() - 1) :
191                                      last_u2_element();
192   }
193 }
194 
exception_table_length_addr() const195 u2* ConstMethod::exception_table_length_addr() const {
196   assert(has_exception_handler(), "called only if table is present");
197   if (has_checked_exceptions()) {
198     // If checked_exception present, locate immediately before them.
199     return (u2*) checked_exceptions_start() - 1;
200   } else {
201     if(has_method_parameters()) {
202       // If method parameters present, locate immediately before them.
203       return (u2*)method_parameters_start() - 1;
204     } else {
205       // Else, the exception table is at the end of the constMethod.
206       return has_generic_signature() ? (last_u2_element() - 1) :
207                                         last_u2_element();
208     }
209   }
210 }
211 
localvariable_table_length_addr() const212 u2* ConstMethod::localvariable_table_length_addr() const {
213   assert(has_localvariable_table(), "called only if table is present");
214   if (has_exception_handler()) {
215     // If exception_table present, locate immediately before them.
216     return (u2*) exception_table_start() - 1;
217   } else {
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 
234 // Update the flags to indicate the presence of these optional fields.
set_inlined_tables_length(InlineTableSizes * sizes)235 void ConstMethod::set_inlined_tables_length(InlineTableSizes* sizes) {
236   _flags = 0;
237   if (sizes->compressed_linenumber_size() > 0)
238     _flags |= _has_linenumber_table;
239   if (sizes->generic_signature_index() != 0)
240     _flags |= _has_generic_signature;
241   if (sizes->method_parameters_length() > 0)
242     _flags |= _has_method_parameters;
243   if (sizes->checked_exceptions_length() > 0)
244     _flags |= _has_checked_exceptions;
245   if (sizes->exception_table_length() > 0)
246     _flags |= _has_exception_table;
247   if (sizes->localvariable_table_length() > 0)
248     _flags |= _has_localvariable_table;
249 
250   // annotations, they are all pointer sized embedded objects so don't have
251   // a length embedded also.
252   if (sizes->method_annotations_length() > 0)
253     _flags |= _has_method_annotations;
254   if (sizes->parameter_annotations_length() > 0)
255     _flags |= _has_parameter_annotations;
256   if (sizes->type_annotations_length() > 0)
257     _flags |= _has_type_annotations;
258   if (sizes->default_annotations_length() > 0)
259     _flags |= _has_default_annotations;
260 
261   // This code is extremely brittle and should possibly be revised.
262   // The *_length_addr functions walk backwards through the
263   // constMethod data, using each of the length indexes ahead of them,
264   // as well as the flags variable.  Therefore, the indexes must be
265   // initialized in reverse order, or else they will compute the wrong
266   // offsets.  Moving the initialization of _flags into a separate
267   // block solves *half* of the problem, but the following part will
268   // still break if the order is not exactly right.
269   //
270   // Also, the servicability agent needs to be informed anytime
271   // anything is added here.  It might be advisable to have some sort
272   // of indication of this inline.
273   if (sizes->generic_signature_index() != 0)
274     *(generic_signature_index_addr()) = sizes->generic_signature_index();
275   // New data should probably go here.
276   if (sizes->method_parameters_length() > 0)
277     *(method_parameters_length_addr()) = sizes->method_parameters_length();
278   if (sizes->checked_exceptions_length() > 0)
279     *(checked_exceptions_length_addr()) = sizes->checked_exceptions_length();
280   if (sizes->exception_table_length() > 0)
281     *(exception_table_length_addr()) = sizes->exception_table_length();
282   if (sizes->localvariable_table_length() > 0)
283     *(localvariable_table_length_addr()) = sizes->localvariable_table_length();
284 }
285 
method_parameters_length() const286 int ConstMethod::method_parameters_length() const {
287   return has_method_parameters() ? *(method_parameters_length_addr()) : 0;
288 }
289 
method_parameters_start() const290 MethodParametersElement* ConstMethod::method_parameters_start() const {
291   u2* addr = method_parameters_length_addr();
292   u2 length = *addr;
293   assert(length > 0, "should only be called if table is present");
294   addr -= length * sizeof(MethodParametersElement) / sizeof(u2);
295   return (MethodParametersElement*) addr;
296 }
297 
298 
checked_exceptions_length() const299 int ConstMethod::checked_exceptions_length() const {
300   return has_checked_exceptions() ? *(checked_exceptions_length_addr()) : 0;
301 }
302 
303 
checked_exceptions_start() const304 CheckedExceptionElement* ConstMethod::checked_exceptions_start() const {
305   u2* addr = checked_exceptions_length_addr();
306   u2 length = *addr;
307   assert(length > 0, "should only be called if table is present");
308   addr -= length * sizeof(CheckedExceptionElement) / sizeof(u2);
309   return (CheckedExceptionElement*) addr;
310 }
311 
312 
localvariable_table_length() const313 int ConstMethod::localvariable_table_length() const {
314   return has_localvariable_table() ? *(localvariable_table_length_addr()) : 0;
315 }
316 
317 
localvariable_table_start() const318 LocalVariableTableElement* ConstMethod::localvariable_table_start() const {
319   u2* addr = localvariable_table_length_addr();
320   u2 length = *addr;
321   assert(length > 0, "should only be called if table is present");
322   addr -= length * sizeof(LocalVariableTableElement) / sizeof(u2);
323   return (LocalVariableTableElement*) addr;
324 }
325 
exception_table_length() const326 int ConstMethod::exception_table_length() const {
327   return has_exception_handler() ? *(exception_table_length_addr()) : 0;
328 }
329 
exception_table_start() const330 ExceptionTableElement* ConstMethod::exception_table_start() const {
331   u2* addr = exception_table_length_addr();
332   u2 length = *addr;
333   assert(length > 0, "should only be called if table is present");
334   addr -= length * sizeof(ExceptionTableElement) / sizeof(u2);
335   return (ExceptionTableElement*)addr;
336 }
337 
method_annotations_addr() const338 AnnotationArray** ConstMethod::method_annotations_addr() const {
339   assert(has_method_annotations(), "should only be called if method annotations are present");
340   return (AnnotationArray**)constMethod_end() - 1;
341 }
342 
parameter_annotations_addr() const343 AnnotationArray** ConstMethod::parameter_annotations_addr() const {
344   assert(has_parameter_annotations(), "should only be called if method parameter annotations are present");
345   int offset = 1;
346   if (has_method_annotations()) offset++;
347   return (AnnotationArray**)constMethod_end() - offset;
348 }
349 
type_annotations_addr() const350 AnnotationArray** ConstMethod::type_annotations_addr() const {
351   assert(has_type_annotations(), "should only be called if method type annotations are present");
352   int offset = 1;
353   if (has_method_annotations()) offset++;
354   if (has_parameter_annotations()) offset++;
355   return (AnnotationArray**)constMethod_end() - offset;
356 }
357 
default_annotations_addr() const358 AnnotationArray** ConstMethod::default_annotations_addr() const {
359   assert(has_default_annotations(), "should only be called if method default annotations are present");
360   int offset = 1;
361   if (has_method_annotations()) offset++;
362   if (has_parameter_annotations()) offset++;
363   if (has_type_annotations()) offset++;
364   return (AnnotationArray**)constMethod_end() - offset;
365 }
366 
367 // copy annotations from 'cm' to 'this'
copy_annotations_from(ConstMethod * cm)368 void ConstMethod::copy_annotations_from(ConstMethod* cm) {
369   if (cm->has_method_annotations()) {
370     assert(has_method_annotations(), "should be allocated already");
371     set_method_annotations(cm->method_annotations());
372   }
373   if (cm->has_parameter_annotations()) {
374     assert(has_parameter_annotations(), "should be allocated already");
375     set_parameter_annotations(cm->parameter_annotations());
376   }
377   if (cm->has_type_annotations()) {
378     assert(has_type_annotations(), "should be allocated already");
379     set_type_annotations(cm->type_annotations());
380   }
381   if (cm->has_default_annotations()) {
382     assert(has_default_annotations(), "should be allocated already");
383     set_default_annotations(cm->default_annotations());
384   }
385 }
386 
387 // Printing
388 
print_on(outputStream * st) const389 void ConstMethod::print_on(outputStream* st) const {
390   ResourceMark rm;
391   assert(is_constMethod(), "must be constMethod");
392   st->print_cr("%s", internal_name());
393   Method* m = method();
394   st->print(" - method:       " INTPTR_FORMAT " ", p2i((address)m));
395   if (m != NULL) {
396     m->print_value_on(st);
397   }
398   st->cr();
399   if (has_stackmap_table()) {
400     st->print(" - stackmap data:       ");
401     stackmap_data()->print_value_on(st);
402     st->cr();
403   }
404 }
405 
406 // Short version of printing ConstMethod* - just print the name of the
407 // method it belongs to.
print_value_on(outputStream * st) const408 void ConstMethod::print_value_on(outputStream* st) const {
409   assert(is_constMethod(), "must be constMethod");
410   st->print(" const part of method " );
411   Method* m = method();
412   if (m != NULL) {
413     m->print_value_on(st);
414   } else {
415     st->print("NULL");
416   }
417 }
418 
419 #if INCLUDE_SERVICES
420 // Size Statistics
collect_statistics(KlassSizeStats * sz) const421 void ConstMethod::collect_statistics(KlassSizeStats *sz) const {
422   int n1, n2, n3;
423   sz->_const_method_bytes += (n1 = sz->count(this));
424   sz->_bytecode_bytes     += (n2 = code_size());
425   sz->_stackmap_bytes     += (n3 = sz->count_array(stackmap_data()));
426 
427   // Count method annotations
428   int a1 = 0, a2 = 0, a3 = 0, a4 = 0;
429   if (has_method_annotations()) {
430     sz->_methods_annotations_bytes += (a1 = sz->count_array(method_annotations()));
431   }
432   if (has_parameter_annotations()) {
433     sz->_methods_parameter_annotations_bytes += (a2 = sz->count_array(parameter_annotations()));
434   }
435   if (has_type_annotations()) {
436     sz->_methods_type_annotations_bytes += (a3 = sz->count_array(type_annotations()));
437   }
438   if (has_default_annotations()) {
439     sz->_methods_default_annotations_bytes += (a4 = sz->count_array(default_annotations()));
440   }
441 
442   int size_annotations = a1 + a2 + a3 + a4;
443 
444   sz->_method_all_bytes += n1 + n3 + size_annotations; // note: n2 is part of n3
445   sz->_ro_bytes += n1 + n3 + size_annotations;
446 }
447 #endif // INCLUDE_SERVICES
448 
449 // Verification
450 
verify_on(outputStream * st)451 void ConstMethod::verify_on(outputStream* st) {
452   guarantee(is_constMethod(), "object must be constMethod");
453 
454   // Verification can occur during oop construction before the method or
455   // other fields have been initialized.
456   guarantee(method() != NULL && method()->is_method(), "should be method");
457 
458   address m_end = (address)((intptr_t) this + size());
459   address compressed_table_start = code_end();
460   guarantee(compressed_table_start <= m_end, "invalid method layout");
461   address compressed_table_end = compressed_table_start;
462   // Verify line number table
463   if (has_linenumber_table()) {
464     CompressedLineNumberReadStream stream(compressed_linenumber_table());
465     while (stream.read_pair()) {
466       guarantee(stream.bci() >= 0 && stream.bci() <= code_size(), "invalid bci in line number table");
467     }
468     compressed_table_end += stream.position();
469   }
470   guarantee(compressed_table_end <= m_end, "invalid method layout");
471   // Verify checked exceptions, exception table and local variable tables
472   if (has_method_parameters()) {
473     u2* addr = method_parameters_length_addr();
474     guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
475   }
476   if (has_checked_exceptions()) {
477     u2* addr = checked_exceptions_length_addr();
478     guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
479   }
480   if (has_exception_handler()) {
481     u2* addr = exception_table_length_addr();
482      guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
483   }
484   if (has_localvariable_table()) {
485     u2* addr = localvariable_table_length_addr();
486     guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
487   }
488   // Check compressed_table_end relative to uncompressed_table_start
489   u2* uncompressed_table_start;
490   if (has_localvariable_table()) {
491     uncompressed_table_start = (u2*) localvariable_table_start();
492   } else if (has_exception_handler()) {
493     uncompressed_table_start = (u2*) exception_table_start();
494   } else if (has_checked_exceptions()) {
495       uncompressed_table_start = (u2*) checked_exceptions_start();
496   } else if (has_method_parameters()) {
497       uncompressed_table_start = (u2*) method_parameters_start();
498   } else {
499       uncompressed_table_start = (u2*) m_end;
500   }
501   int gap = (intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end;
502   int max_gap = align_object_size(1)*BytesPerWord;
503   guarantee(gap >= 0 && gap < max_gap, "invalid method layout");
504 }
505