Tue, 22 Mar 2011 13:36:33 -0700
6962931: move interned strings out of the perm gen
Reviewed-by: never, coleenp, ysr, jwilhelm
1 /*
2 * Copyright (c) 2003, 2010, 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 "classfile/stackMapTable.hpp"
27 #include "classfile/verifier.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "runtime/fieldType.hpp"
31 #include "runtime/handles.inline.hpp"
33 StackMapTable::StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
34 u2 max_locals, u2 max_stack,
35 char* code_data, int code_len, TRAPS) {
36 _code_length = code_len;
37 _frame_count = reader->get_frame_count();
38 if (_frame_count > 0) {
39 _frame_array = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
40 StackMapFrame*, _frame_count);
41 StackMapFrame* pre_frame = init_frame;
42 for (int32_t i = 0; i < _frame_count; i++) {
43 StackMapFrame* frame = reader->next(
44 pre_frame, i == 0, max_locals, max_stack,
45 CHECK_VERIFY(pre_frame->verifier()));
46 _frame_array[i] = frame;
47 int offset = frame->offset();
48 if (offset >= code_len || code_data[offset] == 0) {
49 frame->verifier()->verify_error("StackMapTable error: bad offset");
50 return;
51 }
52 pre_frame = frame;
53 }
54 }
55 reader->check_end(CHECK);
56 }
58 // This method is only called by method in StackMapTable.
59 int StackMapTable::get_index_from_offset(int32_t offset) const {
60 int i = 0;
61 for (; i < _frame_count; i++) {
62 if (_frame_array[i]->offset() == offset) {
63 return i;
64 }
65 }
66 return i; // frame with offset doesn't exist in the array
67 }
69 bool StackMapTable::match_stackmap(
70 StackMapFrame* frame, int32_t target,
71 bool match, bool update, TRAPS) const {
72 int index = get_index_from_offset(target);
74 return match_stackmap(
75 frame, target, index, match,
76 update, CHECK_VERIFY_(frame->verifier(), false));
77 }
79 // Match and/or update current_frame to the frame in stackmap table with
80 // specified offset and frame index. Return true if the two frames match.
81 //
82 // The values of match and update are: _match__update_
83 //
84 // checking a branch target/exception handler: true false
85 // linear bytecode verification following an
86 // unconditional branch: false true
87 // linear bytecode verification not following an
88 // unconditional branch: true true
89 bool StackMapTable::match_stackmap(
90 StackMapFrame* frame, int32_t target, int32_t frame_index,
91 bool match, bool update, TRAPS) const {
92 if (frame_index < 0 || frame_index >= _frame_count) {
93 frame->verifier()->verify_error(frame->offset(),
94 "Expecting a stackmap frame at branch target %d", target);
95 return false;
96 }
98 bool result = true;
99 StackMapFrame *stackmap_frame = _frame_array[frame_index];
100 if (match) {
101 // Has direct control flow from last instruction, need to match the two
102 // frames.
103 result = frame->is_assignable_to(
104 stackmap_frame, CHECK_VERIFY_(frame->verifier(), false));
105 }
106 if (update) {
107 // Use the frame in stackmap table as current frame
108 int lsize = stackmap_frame->locals_size();
109 int ssize = stackmap_frame->stack_size();
110 if (frame->locals_size() > lsize || frame->stack_size() > ssize) {
111 // Make sure unused type array items are all _bogus_type.
112 frame->reset();
113 }
114 frame->set_locals_size(lsize);
115 frame->copy_locals(stackmap_frame);
116 frame->set_stack_size(ssize);
117 frame->copy_stack(stackmap_frame);
118 frame->set_flags(stackmap_frame->flags());
119 }
120 return result;
121 }
123 void StackMapTable::check_jump_target(
124 StackMapFrame* frame, int32_t target, TRAPS) const {
125 bool match = match_stackmap(
126 frame, target, true, false, CHECK_VERIFY(frame->verifier()));
127 if (!match || (target < 0 || target >= _code_length)) {
128 frame->verifier()->verify_error(frame->offset(),
129 "Inconsistent stackmap frames at branch target %d", target);
130 return;
131 }
132 // check if uninitialized objects exist on backward branches
133 check_new_object(frame, target, CHECK_VERIFY(frame->verifier()));
134 }
136 void StackMapTable::check_new_object(
137 const StackMapFrame* frame, int32_t target, TRAPS) const {
138 if (frame->offset() > target && frame->has_new_object()) {
139 frame->verifier()->verify_error(frame->offset(),
140 "Uninitialized object exists on backward branch %d", target);
141 return;
142 }
143 }
145 #ifndef PRODUCT
147 void StackMapTable::print() const {
148 tty->print_cr("StackMapTable: frame_count = %d", _frame_count);
149 tty->print_cr("table = { ");
150 for (int32_t i = 0; i < _frame_count; i++) {
151 _frame_array[i]->print();
152 }
153 tty->print_cr(" }");
154 }
156 #endif
158 int32_t StackMapReader::chop(
159 VerificationType* locals, int32_t length, int32_t chops) {
160 if (locals == NULL) return -1;
161 int32_t pos = length - 1;
162 for (int32_t i=0; i<chops; i++) {
163 if (locals[pos].is_category2_2nd()) {
164 pos -= 2;
165 } else {
166 pos --;
167 }
168 if (pos<0 && i<(chops-1)) return -1;
169 }
170 return pos+1;
171 }
173 VerificationType StackMapReader::parse_verification_type(u1* flags, TRAPS) {
174 u1 tag = _stream->get_u1(THREAD);
175 if (tag < (u1)ITEM_UninitializedThis) {
176 return VerificationType::from_tag(tag);
177 }
178 if (tag == ITEM_Object) {
179 u2 class_index = _stream->get_u2(THREAD);
180 int nconstants = _cp->length();
181 if ((class_index <= 0 || class_index >= nconstants) ||
182 (!_cp->tag_at(class_index).is_klass() &&
183 !_cp->tag_at(class_index).is_unresolved_klass())) {
184 _stream->stackmap_format_error("bad class index", THREAD);
185 return VerificationType::bogus_type();
186 }
187 return VerificationType::reference_type(_cp->klass_name_at(class_index));
188 }
189 if (tag == ITEM_UninitializedThis) {
190 if (flags != NULL) {
191 *flags |= FLAG_THIS_UNINIT;
192 }
193 return VerificationType::uninitialized_this_type();
194 }
195 if (tag == ITEM_Uninitialized) {
196 u2 offset = _stream->get_u2(THREAD);
197 if (offset >= _code_length ||
198 _code_data[offset] != ClassVerifier::NEW_OFFSET) {
199 ResourceMark rm(THREAD);
200 _verifier->class_format_error(
201 "StackMapTable format error: bad offset for Uninitialized");
202 return VerificationType::bogus_type();
203 }
204 return VerificationType::uninitialized_type(offset);
205 }
206 _stream->stackmap_format_error("bad verification type", THREAD);
207 return VerificationType::bogus_type();
208 }
210 StackMapFrame* StackMapReader::next(
211 StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) {
212 StackMapFrame* frame;
213 int offset;
214 VerificationType* locals = NULL;
215 u1 frame_type = _stream->get_u1(THREAD);
216 if (frame_type < 64) {
217 // same_frame
218 if (first) {
219 offset = frame_type;
220 // Can't share the locals array since that is updated by the verifier.
221 if (pre_frame->locals_size() > 0) {
222 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
223 THREAD, VerificationType, pre_frame->locals_size());
224 }
225 } else {
226 offset = pre_frame->offset() + frame_type + 1;
227 locals = pre_frame->locals();
228 }
229 frame = new StackMapFrame(
230 offset, pre_frame->flags(), pre_frame->locals_size(), 0,
231 max_locals, max_stack, locals, NULL, _verifier);
232 if (first && locals != NULL) {
233 frame->copy_locals(pre_frame);
234 }
235 return frame;
236 }
237 if (frame_type < 128) {
238 // same_locals_1_stack_item_frame
239 if (first) {
240 offset = frame_type - 64;
241 // Can't share the locals array since that is updated by the verifier.
242 if (pre_frame->locals_size() > 0) {
243 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
244 THREAD, VerificationType, pre_frame->locals_size());
245 }
246 } else {
247 offset = pre_frame->offset() + frame_type - 63;
248 locals = pre_frame->locals();
249 }
250 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
251 THREAD, VerificationType, 2);
252 u2 stack_size = 1;
253 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
254 if (stack[0].is_category2()) {
255 stack[1] = stack[0].to_category2_2nd();
256 stack_size = 2;
257 }
258 check_verification_type_array_size(
259 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
260 frame = new StackMapFrame(
261 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
262 max_locals, max_stack, locals, stack, _verifier);
263 if (first && locals != NULL) {
264 frame->copy_locals(pre_frame);
265 }
266 return frame;
267 }
269 u2 offset_delta = _stream->get_u2(THREAD);
271 if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
272 // reserved frame types
273 _stream->stackmap_format_error(
274 "reserved frame type", CHECK_VERIFY_(_verifier, NULL));
275 }
277 if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
278 // same_locals_1_stack_item_frame_extended
279 if (first) {
280 offset = offset_delta;
281 // Can't share the locals array since that is updated by the verifier.
282 if (pre_frame->locals_size() > 0) {
283 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
284 THREAD, VerificationType, pre_frame->locals_size());
285 }
286 } else {
287 offset = pre_frame->offset() + offset_delta + 1;
288 locals = pre_frame->locals();
289 }
290 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
291 THREAD, VerificationType, 2);
292 u2 stack_size = 1;
293 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
294 if (stack[0].is_category2()) {
295 stack[1] = stack[0].to_category2_2nd();
296 stack_size = 2;
297 }
298 check_verification_type_array_size(
299 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
300 frame = new StackMapFrame(
301 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
302 max_locals, max_stack, locals, stack, _verifier);
303 if (first && locals != NULL) {
304 frame->copy_locals(pre_frame);
305 }
306 return frame;
307 }
309 if (frame_type <= SAME_EXTENDED) {
310 // chop_frame or same_frame_extended
311 locals = pre_frame->locals();
312 int length = pre_frame->locals_size();
313 int chops = SAME_EXTENDED - frame_type;
314 int new_length = length;
315 u1 flags = pre_frame->flags();
316 if (chops != 0) {
317 new_length = chop(locals, length, chops);
318 check_verification_type_array_size(
319 new_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
320 // Recompute flags since uninitializedThis could have been chopped.
321 flags = 0;
322 for (int i=0; i<new_length; i++) {
323 if (locals[i].is_uninitialized_this()) {
324 flags |= FLAG_THIS_UNINIT;
325 break;
326 }
327 }
328 }
329 if (first) {
330 offset = offset_delta;
331 // Can't share the locals array since that is updated by the verifier.
332 if (new_length > 0) {
333 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
334 THREAD, VerificationType, new_length);
335 } else {
336 locals = NULL;
337 }
338 } else {
339 offset = pre_frame->offset() + offset_delta + 1;
340 }
341 frame = new StackMapFrame(
342 offset, flags, new_length, 0, max_locals, max_stack,
343 locals, NULL, _verifier);
344 if (first && locals != NULL) {
345 frame->copy_locals(pre_frame);
346 }
347 return frame;
348 } else if (frame_type < SAME_EXTENDED + 4) {
349 // append_frame
350 int appends = frame_type - SAME_EXTENDED;
351 int real_length = pre_frame->locals_size();
352 int new_length = real_length + appends*2;
353 locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length);
354 VerificationType* pre_locals = pre_frame->locals();
355 int i;
356 for (i=0; i<pre_frame->locals_size(); i++) {
357 locals[i] = pre_locals[i];
358 }
359 u1 flags = pre_frame->flags();
360 for (i=0; i<appends; i++) {
361 locals[real_length] = parse_verification_type(&flags, THREAD);
362 if (locals[real_length].is_category2()) {
363 locals[real_length + 1] = locals[real_length].to_category2_2nd();
364 ++real_length;
365 }
366 ++real_length;
367 }
368 check_verification_type_array_size(
369 real_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
370 if (first) {
371 offset = offset_delta;
372 } else {
373 offset = pre_frame->offset() + offset_delta + 1;
374 }
375 frame = new StackMapFrame(
376 offset, flags, real_length, 0, max_locals,
377 max_stack, locals, NULL, _verifier);
378 return frame;
379 }
380 if (frame_type == FULL) {
381 // full_frame
382 u1 flags = 0;
383 u2 locals_size = _stream->get_u2(THREAD);
384 int real_locals_size = 0;
385 if (locals_size > 0) {
386 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
387 THREAD, VerificationType, locals_size*2);
388 }
389 int i;
390 for (i=0; i<locals_size; i++) {
391 locals[real_locals_size] = parse_verification_type(&flags, THREAD);
392 if (locals[real_locals_size].is_category2()) {
393 locals[real_locals_size + 1] =
394 locals[real_locals_size].to_category2_2nd();
395 ++real_locals_size;
396 }
397 ++real_locals_size;
398 }
399 check_verification_type_array_size(
400 real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL));
401 u2 stack_size = _stream->get_u2(THREAD);
402 int real_stack_size = 0;
403 VerificationType* stack = NULL;
404 if (stack_size > 0) {
405 stack = NEW_RESOURCE_ARRAY_IN_THREAD(
406 THREAD, VerificationType, stack_size*2);
407 }
408 for (i=0; i<stack_size; i++) {
409 stack[real_stack_size] = parse_verification_type(NULL, THREAD);
410 if (stack[real_stack_size].is_category2()) {
411 stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd();
412 ++real_stack_size;
413 }
414 ++real_stack_size;
415 }
416 check_verification_type_array_size(
417 real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
418 if (first) {
419 offset = offset_delta;
420 } else {
421 offset = pre_frame->offset() + offset_delta + 1;
422 }
423 frame = new StackMapFrame(
424 offset, flags, real_locals_size, real_stack_size,
425 max_locals, max_stack, locals, stack, _verifier);
426 return frame;
427 }
429 _stream->stackmap_format_error(
430 "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL));
431 return NULL;
432 }