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