Thu, 07 Feb 2013 16:05:48 -0500
Merge
1 /*
2 * Copyright (c) 2003, 2013, 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 */
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"
33 // Static initialization
34 const u2 ConstMethod::MAX_IDNUM = 0xFFFE;
35 const u2 ConstMethod::UNSET_IDNUM = 0xFFFF;
37 ConstMethod* ConstMethod::allocate(ClassLoaderData* loader_data,
38 int byte_code_size,
39 int compressed_line_number_size,
40 int localvariable_table_length,
41 int exception_table_length,
42 int checked_exceptions_length,
43 int method_parameters_length,
44 u2 generic_signature_index,
45 MethodType method_type,
46 TRAPS) {
47 int size = ConstMethod::size(byte_code_size,
48 compressed_line_number_size,
49 localvariable_table_length,
50 exception_table_length,
51 checked_exceptions_length,
52 method_parameters_length,
53 generic_signature_index);
54 return new (loader_data, size, true, THREAD) ConstMethod(
55 byte_code_size, compressed_line_number_size, localvariable_table_length,
56 exception_table_length, checked_exceptions_length,
57 method_parameters_length, generic_signature_index,
58 method_type, size);
59 }
61 ConstMethod::ConstMethod(int byte_code_size,
62 int compressed_line_number_size,
63 int localvariable_table_length,
64 int exception_table_length,
65 int checked_exceptions_length,
66 int method_parameters_length,
67 u2 generic_signature_index,
68 MethodType method_type,
69 int size) {
71 No_Safepoint_Verifier no_safepoint;
72 set_interpreter_kind(Interpreter::invalid);
73 init_fingerprint();
74 set_constants(NULL);
75 set_stackmap_data(NULL);
76 set_code_size(byte_code_size);
77 set_constMethod_size(size);
78 set_inlined_tables_length(generic_signature_index,
79 checked_exceptions_length,
80 compressed_line_number_size,
81 localvariable_table_length,
82 exception_table_length,
83 method_parameters_length);
84 set_method_type(method_type);
85 assert(this->size() == size, "wrong size for object");
86 }
89 // Deallocate metadata fields associated with ConstMethod*
90 void ConstMethod::deallocate_contents(ClassLoaderData* loader_data) {
91 set_interpreter_kind(Interpreter::invalid);
92 if (stackmap_data() != NULL) {
93 MetadataFactory::free_array<u1>(loader_data, stackmap_data());
94 }
95 set_stackmap_data(NULL);
96 }
98 // How big must this constMethodObject be?
100 int ConstMethod::size(int code_size,
101 int compressed_line_number_size,
102 int local_variable_table_length,
103 int exception_table_length,
104 int checked_exceptions_length,
105 int method_parameters_length,
106 u2 generic_signature_index) {
107 int extra_bytes = code_size;
108 if (compressed_line_number_size > 0) {
109 extra_bytes += compressed_line_number_size;
110 }
111 if (checked_exceptions_length > 0) {
112 extra_bytes += sizeof(u2);
113 extra_bytes += checked_exceptions_length * sizeof(CheckedExceptionElement);
114 }
115 if (local_variable_table_length > 0) {
116 extra_bytes += sizeof(u2);
117 extra_bytes +=
118 local_variable_table_length * sizeof(LocalVariableTableElement);
119 }
120 if (exception_table_length > 0) {
121 extra_bytes += sizeof(u2);
122 extra_bytes += exception_table_length * sizeof(ExceptionTableElement);
123 }
124 if (generic_signature_index != 0) {
125 extra_bytes += sizeof(u2);
126 }
127 if (method_parameters_length > 0) {
128 extra_bytes += sizeof(u2);
129 extra_bytes += method_parameters_length * sizeof(MethodParametersElement);
130 }
131 int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord;
132 return align_object_size(header_size() + extra_words);
133 }
135 Method* ConstMethod::method() const {
136 return _constants->pool_holder()->method_with_idnum(_method_idnum);
137 }
139 // linenumber table - note that length is unknown until decompression,
140 // see class CompressedLineNumberReadStream.
142 u_char* ConstMethod::compressed_linenumber_table() const {
143 // Located immediately following the bytecodes.
144 assert(has_linenumber_table(), "called only if table is present");
145 return code_end();
146 }
148 u2* ConstMethod::generic_signature_index_addr() const {
149 // Located at the end of the constMethod.
150 assert(has_generic_signature(), "called only if generic signature exists");
151 return last_u2_element();
152 }
154 u2* ConstMethod::checked_exceptions_length_addr() const {
155 // Located immediately before the generic signature index.
156 assert(has_checked_exceptions(), "called only if table is present");
157 if(has_method_parameters()) {
158 // If method parameters present, locate immediately before them.
159 return (u2*)method_parameters_start() - 1;
160 } else {
161 // Else, the exception table is at the end of the constMethod.
162 return has_generic_signature() ? (last_u2_element() - 1) :
163 last_u2_element();
164 }
165 }
167 u2* ConstMethod::method_parameters_length_addr() const {
168 assert(has_method_parameters(), "called only if table is present");
169 return has_generic_signature() ? (last_u2_element() - 1) :
170 last_u2_element();
171 }
173 u2* ConstMethod::exception_table_length_addr() const {
174 assert(has_exception_handler(), "called only if table is present");
175 if (has_checked_exceptions()) {
176 // If checked_exception present, locate immediately before them.
177 return (u2*) checked_exceptions_start() - 1;
178 } else {
179 if(has_method_parameters()) {
180 // If method parameters present, locate immediately before them.
181 return (u2*)method_parameters_start() - 1;
182 } else {
183 // Else, the exception table is at the end of the constMethod.
184 return has_generic_signature() ? (last_u2_element() - 1) :
185 last_u2_element();
186 }
187 }
188 }
190 u2* ConstMethod::localvariable_table_length_addr() const {
191 assert(has_localvariable_table(), "called only if table is present");
192 if (has_exception_handler()) {
193 // If exception_table present, locate immediately before them.
194 return (u2*) exception_table_start() - 1;
195 } else {
196 if (has_checked_exceptions()) {
197 // If checked_exception present, locate immediately before them.
198 return (u2*) checked_exceptions_start() - 1;
199 } else {
200 if(has_method_parameters()) {
201 // If method parameters present, locate immediately before them.
202 return (u2*)method_parameters_start() - 1;
203 } else {
204 // Else, the exception table is at the end of the constMethod.
205 return has_generic_signature() ? (last_u2_element() - 1) :
206 last_u2_element();
207 }
208 }
209 }
210 }
212 // Update the flags to indicate the presence of these optional fields.
213 void ConstMethod::set_inlined_tables_length(u2 generic_signature_index,
214 int checked_exceptions_len,
215 int compressed_line_number_size,
216 int localvariable_table_len,
217 int exception_table_len,
218 int method_parameters_len) {
219 assert(_flags == 0, "Error");
220 if (compressed_line_number_size > 0)
221 _flags |= _has_linenumber_table;
222 if (generic_signature_index != 0)
223 _flags |= _has_generic_signature;
224 if (method_parameters_len > 0)
225 _flags |= _has_method_parameters;
226 if (checked_exceptions_len > 0)
227 _flags |= _has_checked_exceptions;
228 if (exception_table_len > 0)
229 _flags |= _has_exception_table;
230 if (localvariable_table_len > 0)
231 _flags |= _has_localvariable_table;
233 // This code is extremely brittle and should possibly be revised.
234 // The *_length_addr functions walk backwards through the
235 // constMethod data, using each of the length indexes ahead of them,
236 // as well as the flags variable. Therefore, the indexes must be
237 // initialized in reverse order, or else they will compute the wrong
238 // offsets. Moving the initialization of _flags into a separate
239 // block solves *half* of the problem, but the following part will
240 // still break if the order is not exactly right.
241 //
242 // Also, the servicability agent needs to be informed anytime
243 // anything is added here. It might be advisable to have some sort
244 // of indication of this inline.
245 if (generic_signature_index != 0)
246 *(generic_signature_index_addr()) = generic_signature_index;
247 // New data should probably go here.
248 if (method_parameters_len > 0)
249 *(method_parameters_length_addr()) = method_parameters_len;
250 if (checked_exceptions_len > 0)
251 *(checked_exceptions_length_addr()) = checked_exceptions_len;
252 if (exception_table_len > 0)
253 *(exception_table_length_addr()) = exception_table_len;
254 if (localvariable_table_len > 0)
255 *(localvariable_table_length_addr()) = localvariable_table_len;
256 }
258 int ConstMethod::method_parameters_length() const {
259 return has_method_parameters() ? *(method_parameters_length_addr()) : 0;
260 }
262 MethodParametersElement* ConstMethod::method_parameters_start() const {
263 u2* addr = method_parameters_length_addr();
264 u2 length = *addr;
265 assert(length > 0, "should only be called if table is present");
266 addr -= length * sizeof(MethodParametersElement) / sizeof(u2);
267 return (MethodParametersElement*) addr;
268 }
271 int ConstMethod::checked_exceptions_length() const {
272 return has_checked_exceptions() ? *(checked_exceptions_length_addr()) : 0;
273 }
276 CheckedExceptionElement* ConstMethod::checked_exceptions_start() const {
277 u2* addr = checked_exceptions_length_addr();
278 u2 length = *addr;
279 assert(length > 0, "should only be called if table is present");
280 addr -= length * sizeof(CheckedExceptionElement) / sizeof(u2);
281 return (CheckedExceptionElement*) addr;
282 }
285 int ConstMethod::localvariable_table_length() const {
286 return has_localvariable_table() ? *(localvariable_table_length_addr()) : 0;
287 }
290 LocalVariableTableElement* ConstMethod::localvariable_table_start() const {
291 u2* addr = localvariable_table_length_addr();
292 u2 length = *addr;
293 assert(length > 0, "should only be called if table is present");
294 addr -= length * sizeof(LocalVariableTableElement) / sizeof(u2);
295 return (LocalVariableTableElement*) addr;
296 }
298 int ConstMethod::exception_table_length() const {
299 return has_exception_handler() ? *(exception_table_length_addr()) : 0;
300 }
302 ExceptionTableElement* ConstMethod::exception_table_start() const {
303 u2* addr = exception_table_length_addr();
304 u2 length = *addr;
305 assert(length > 0, "should only be called if table is present");
306 addr -= length * sizeof(ExceptionTableElement) / sizeof(u2);
307 return (ExceptionTableElement*)addr;
308 }
311 // Printing
313 void ConstMethod::print_on(outputStream* st) const {
314 ResourceMark rm;
315 assert(is_constMethod(), "must be constMethod");
316 st->print_cr(internal_name());
317 st->print(" - method: " INTPTR_FORMAT " ", (address)method());
318 method()->print_value_on(st); st->cr();
319 if (has_stackmap_table()) {
320 st->print(" - stackmap data: ");
321 stackmap_data()->print_value_on(st);
322 st->cr();
323 }
324 }
326 // Short version of printing ConstMethod* - just print the name of the
327 // method it belongs to.
328 void ConstMethod::print_value_on(outputStream* st) const {
329 assert(is_constMethod(), "must be constMethod");
330 st->print(" const part of method " );
331 method()->print_value_on(st);
332 }
334 #if INCLUDE_SERVICES
335 // Size Statistics
336 void ConstMethod::collect_statistics(KlassSizeStats *sz) const {
337 int n1, n2, n3;
338 sz->_const_method_bytes += (n1 = sz->count(this));
339 sz->_bytecode_bytes += (n2 = code_size());
340 sz->_stackmap_bytes += (n3 = sz->count_array(stackmap_data()));
342 sz->_method_all_bytes += n1 + n3; // note: n2 is part of n3
343 sz->_ro_bytes += n1 + n3;
344 }
345 #endif // INCLUDE_SERVICES
347 // Verification
349 void ConstMethod::verify_on(outputStream* st) {
350 guarantee(is_constMethod(), "object must be constMethod");
351 guarantee(is_metadata(), err_msg("Should be metadata " PTR_FORMAT, this));
353 // Verification can occur during oop construction before the method or
354 // other fields have been initialized.
355 guarantee(is_metadata(), err_msg("Should be metadata " PTR_FORMAT, this));
356 guarantee(method()->is_method(), "should be method");
358 address m_end = (address)((oop*) this + size());
359 address compressed_table_start = code_end();
360 guarantee(compressed_table_start <= m_end, "invalid method layout");
361 address compressed_table_end = compressed_table_start;
362 // Verify line number table
363 if (has_linenumber_table()) {
364 CompressedLineNumberReadStream stream(compressed_linenumber_table());
365 while (stream.read_pair()) {
366 guarantee(stream.bci() >= 0 && stream.bci() <= code_size(), "invalid bci in line number table");
367 }
368 compressed_table_end += stream.position();
369 }
370 guarantee(compressed_table_end <= m_end, "invalid method layout");
371 // Verify checked exceptions, exception table and local variable tables
372 if (has_method_parameters()) {
373 u2* addr = method_parameters_length_addr();
374 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
375 }
376 if (has_checked_exceptions()) {
377 u2* addr = checked_exceptions_length_addr();
378 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
379 }
380 if (has_exception_handler()) {
381 u2* addr = exception_table_length_addr();
382 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
383 }
384 if (has_localvariable_table()) {
385 u2* addr = localvariable_table_length_addr();
386 guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
387 }
388 // Check compressed_table_end relative to uncompressed_table_start
389 u2* uncompressed_table_start;
390 if (has_localvariable_table()) {
391 uncompressed_table_start = (u2*) localvariable_table_start();
392 } else if (has_exception_handler()) {
393 uncompressed_table_start = (u2*) exception_table_start();
394 } else if (has_checked_exceptions()) {
395 uncompressed_table_start = (u2*) checked_exceptions_start();
396 } else if (has_method_parameters()) {
397 uncompressed_table_start = (u2*) method_parameters_start();
398 } else {
399 uncompressed_table_start = (u2*) m_end;
400 }
401 int gap = (intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end;
402 int max_gap = align_object_size(1)*BytesPerWord;
403 guarantee(gap >= 0 && gap < max_gap, "invalid method layout");
404 }