1 /* 2 * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2019 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "classfile/vmClasses.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "gc/shared/gcLocker.hpp" 29 #include "interpreter/bytecodeUtils.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "runtime/signature.hpp" 32 #include "runtime/safepointVerifiers.hpp" 33 #include "utilities/events.hpp" 34 #include "utilities/ostream.hpp" 35 36 class SimulatedOperandStack; 37 class ExceptionMessageBuilder; 38 39 // The entries of a SimulatedOperandStack. They carry the analysis 40 // information gathered for the slot. 41 class StackSlotAnalysisData { 42 private: 43 44 friend class SimulatedOperandStack; 45 friend class ExceptionMessageBuilder; 46 47 unsigned int _bci:17; // The bci of the bytecode that pushed the current value on the operand stack. 48 // INVALID if ambiguous, e.g. after a control flow merge. 49 // 16 bits for bci (max bytecode size) and one for INVALID. 50 unsigned int _type:15; // The BasicType of the value on the operand stack. 51 52 // Merges this slot data with the given one and returns the result. If 53 // the bcis of the two merged objects are different, the bci of the result 54 // will be undefined. If the types are different, the result type is T_CONFLICT. 55 // (An exception is if one type is an array and the other is object, then 56 // the result type will be T_OBJECT). 57 StackSlotAnalysisData merge(StackSlotAnalysisData other); 58 59 public: 60 61 // Creates a new object with an invalid bci and the given type. 62 StackSlotAnalysisData(BasicType type = T_CONFLICT); 63 64 // Creates a new object with the given bci and type. 65 StackSlotAnalysisData(int bci, BasicType type); 66 67 enum { 68 // An invalid bytecode index, as > 65535. 69 INVALID = 0x1FFFF 70 }; 71 72 // Returns the bci. If the bci is invalid, INVALID is returned. 73 unsigned int get_bci(); 74 75 // Returns true, if the bci is not invalid. 76 bool has_bci() { return get_bci() != INVALID; } 77 78 // Returns the type of the slot data. 79 BasicType get_type(); 80 }; 81 82 // A stack consisting of SimulatedOperandStackEntries. 83 // This represents the analysis information for the operand stack 84 // for a given bytecode at a given bci. 85 // It also holds an additional field that serves to collect 86 // information whether local slots were written. 87 class SimulatedOperandStack: CHeapObj<mtInternal> { 88 89 private: 90 91 friend class ExceptionMessageBuilder; 92 friend class StackSlotAnalysisData; 93 94 // The stack. 95 GrowableArray<StackSlotAnalysisData> _stack; 96 97 // Optimized bytecode can reuse local variable slots for several 98 // local variables. 99 // If there is no variable name information, we print 'parameter<i>' 100 // if a parameter maps to a local slot. Once a local slot has been 101 // written, we don't know any more whether it was written as the 102 // corresponding parameter, or whether another local has been 103 // mapped to the slot. So we don't want to print 'parameter<i>' any 104 // more, but 'local<i>'. Similarly for 'this'. 105 // Therefore, during the analysis, we mark a bit for local slots that 106 // get written and propagate this information. 107 // We only run the analysis for 64 slots. If a method has more 108 // parameters, we print 'local<i>' in all cases. 109 uint64_t _written_local_slots; 110 111 SimulatedOperandStack(): _written_local_slots(0) { }; 112 SimulatedOperandStack(const SimulatedOperandStack ©); 113 114 // Pushes the given slot data. 115 void push_raw(StackSlotAnalysisData slotData); 116 117 // Like push_raw, but if the slotData has type long or double, we push two. 118 void push(StackSlotAnalysisData slotData); 119 120 // Like push(slotData), but using bci/type to create an instance of 121 // StackSlotAnalysisData first. 122 void push(int bci, BasicType type); 123 124 // Pops the given number of entries. 125 void pop(int slots); 126 127 // Merges this with the given stack by merging all entries. The 128 // size of the stacks must be the same. 129 void merge(SimulatedOperandStack const& other); 130 131 public: 132 133 // Returns the size of the stack. 134 int get_size() const; 135 136 // Returns the slot data at the given index. Slot 0 is top of stack. 137 StackSlotAnalysisData get_slot_data(int slot); 138 139 // Mark that local slot i was written. 140 void set_local_slot_written(int i); 141 142 // Check whether local slot i was written by this or a previous bytecode. 143 bool local_slot_was_written(int i); 144 }; 145 146 // Helper class to build internal exception messages for exceptions 147 // that are thrown because prerequisites to execute a bytecode 148 // are not met. 149 // E.g., if a NPE is thrown because an iload can not be executed 150 // by the VM because the reference to load from is null. 151 // 152 // It analyses the bytecode to assemble Java-like message text 153 // to give precise information where in a larger expression the 154 // exception occurred. 155 // 156 // To assemble this message text, it is needed to know how 157 // operand stack slot entries were pushed on the operand stack. 158 // This class contains an analysis over the bytecodes to compute 159 // this information. The information is stored in a 160 // SimulatedOperandStack for each bytecode. 161 class ExceptionMessageBuilder : public StackObj { 162 163 // The stacks for each bytecode. 164 GrowableArray<SimulatedOperandStack*>* _stacks; 165 166 // The method. 167 Method* _method; 168 169 // The number of entries used (the sum of all entries of all stacks). 170 int _nr_of_entries; 171 172 // If true, we have added at least one new stack. 173 bool _added_one; 174 175 // If true, we have processed all bytecodes. 176 bool _all_processed; 177 178 // The maximum number of entries we want to use. This is used to 179 // limit the amount of memory we waste for insane methods (as they 180 // appear in JCK tests). 181 static const int _max_entries = 1000000; 182 183 static const int _max_cause_detail = 5; 184 185 // Merges the stack at the given bci with the given stack. If there 186 // is no stack at the bci, we just put the given stack there. This 187 // method doesn't takes ownership of the stack. 188 void merge(int bci, SimulatedOperandStack* stack); 189 190 // Processes the instruction at the given bci in the method. Returns 191 // the size of the instruction. 192 int do_instruction(int bci); 193 194 bool print_NPE_cause0(outputStream *os, int bci, int slot, int max_detail, 195 bool inner_expr = false, const char *prefix = nullptr); 196 197 public: 198 199 // Creates an ExceptionMessageBuilder object and runs the analysis 200 // building SimulatedOperandStacks for each bytecode in the given 201 // method (the method must be rewritten already). Note that you're 202 // not allowed to use this object when crossing a safepoint! If the 203 // bci is != -1, we only create the stacks as far as needed to get a 204 // stack for the bci. 205 ExceptionMessageBuilder(Method* method, int bci = -1); 206 207 // Releases the resources. 208 ~ExceptionMessageBuilder(); 209 210 // Returns the number of stacks (this is the size of the method). 211 int get_size() { return _stacks->length() - 1; } 212 213 // Assuming that a NullPointerException was thrown at the given bci, 214 // we return the nr of the slot holding the null reference. If this 215 // NPE is created by hand, we return -2 as the slot. If there 216 // cannot be a NullPointerException at the bci, -1 is returned. 217 int get_NPE_null_slot(int bci); 218 219 // Prints a java-like expression for the bytecode that pushed 220 // the value to the given slot being live at the given bci. 221 // It constructs the expression by recursing backwards over the 222 // bytecode using the results of the analysis done in the 223 // constructor of ExceptionMessageBuilder. 224 // os: The stream to print the message to. 225 // bci: The index of the bytecode that caused the NPE. 226 // slot: The slot on the operand stack that contains null. 227 // The slots are numbered from TOS downwards, i.e., 228 // TOS has the slot number 0, that below 1 and so on. 229 // 230 // Returns false if nothing was printed, else true. 231 bool print_NPE_cause(outputStream *os, int bci, int slot); 232 233 // Prints a string describing the failed action. 234 void print_NPE_failed_action(outputStream *os, int bci); 235 }; 236 237 // Replaces the following well-known class names: 238 // java.lang.Object -> Object 239 // java.lang.String -> String 240 static char *trim_well_known_class_names_from_signature(char *signature) { 241 size_t len = strlen(signature); 242 size_t skip_len = strlen("java.lang."); 243 size_t min_pattern_len = strlen("java.lang.String"); 244 if (len < min_pattern_len) return signature; 245 246 for (size_t isrc = 0, idst = 0; isrc <= len; isrc++, idst++) { 247 // We must be careful not to trim names like test.java.lang.String. 248 if ((isrc == 0 && strncmp(signature + isrc, "java.lang.Object", min_pattern_len) == 0) || 249 (isrc == 0 && strncmp(signature + isrc, "java.lang.String", min_pattern_len) == 0) || 250 (isrc > 1 && strncmp(signature + isrc-2, ", java.lang.Object", min_pattern_len+2) == 0) || 251 (isrc > 1 && strncmp(signature + isrc-2, ", java.lang.String", min_pattern_len+2) == 0) ) { 252 isrc += skip_len; 253 } 254 if (idst != isrc) { 255 signature[idst] = signature[isrc]; 256 } 257 } 258 return signature; 259 } 260 261 // Replaces the following well-known class names: 262 // java.lang.Object -> Object 263 // java.lang.String -> String 264 static void print_klass_name(outputStream *os, Symbol *klass) { 265 const char *name = klass->as_klass_external_name(); 266 if (strcmp(name, "java.lang.Object") == 0) name = "Object"; 267 if (strcmp(name, "java.lang.String") == 0) name = "String"; 268 os->print("%s", name); 269 } 270 271 // Prints the name of the method that is described at constant pool 272 // index cp_index in the constant pool of method 'method'. 273 static void print_method_name(outputStream *os, Method* method, int cp_index, Bytecodes::Code bc) { 274 ResourceMark rm; 275 ConstantPool* cp = method->constants(); 276 Symbol* klass = cp->klass_ref_at_noresolve(cp_index, bc); 277 Symbol* name = cp->name_ref_at(cp_index, bc); 278 Symbol* signature = cp->signature_ref_at(cp_index, bc); 279 280 print_klass_name(os, klass); 281 os->print(".%s(", name->as_C_string()); 282 stringStream sig; 283 signature->print_as_signature_external_parameters(&sig); 284 os->print("%s)", trim_well_known_class_names_from_signature(sig.as_string())); 285 } 286 287 // Prints the name of the field that is described at constant pool 288 // index cp_index in the constant pool of method 'method'. 289 static void print_field_and_class(outputStream *os, Method* method, int cp_index, Bytecodes::Code bc) { 290 ResourceMark rm; 291 ConstantPool* cp = method->constants(); 292 Symbol* klass = cp->klass_ref_at_noresolve(cp_index, bc); 293 Symbol *name = cp->name_ref_at(cp_index, bc); 294 print_klass_name(os, klass); 295 os->print(".%s", name->as_C_string()); 296 } 297 298 // Returns the name of the field that is described at constant pool 299 // index cp_index in the constant pool of method 'method'. 300 static char const* get_field_name(Method* method, int cp_index, Bytecodes::Code bc) { 301 Symbol* name = method->constants()->name_ref_at(cp_index, bc); 302 return name->as_C_string(); 303 } 304 305 static void print_local_var(outputStream *os, unsigned int bci, Method* method, int slot, bool is_parameter) { 306 if (method->has_localvariable_table()) { 307 for (int i = 0; i < method->localvariable_table_length(); i++) { 308 LocalVariableTableElement* elem = method->localvariable_table_start() + i; 309 unsigned int start = elem->start_bci; 310 unsigned int end = start + elem->length; 311 312 if ((bci >= start) && (bci < end) && (elem->slot == slot)) { 313 ConstantPool* cp = method->constants(); 314 char *var = cp->symbol_at(elem->name_cp_index)->as_C_string(); 315 os->print("%s", var); 316 317 return; 318 } 319 } 320 } 321 322 // Handle at least some cases we know. 323 if (!method->is_static() && (slot == 0) && is_parameter) { 324 os->print("this"); 325 } else { 326 int curr = method->is_static() ? 0 : 1; 327 SignatureStream ss(method->signature()); 328 int param_index = 1; 329 bool found = false; 330 331 for (SignatureStream ss(method->signature()); !ss.is_done(); ss.next()) { 332 if (ss.at_return_type()) { 333 continue; 334 } 335 int size = type2size[ss.type()]; 336 if ((slot >= curr) && (slot < curr + size)) { 337 found = true; 338 break; 339 } 340 param_index += 1; 341 curr += size; 342 } 343 344 if (found && is_parameter) { 345 os->print("<parameter%d>", param_index); 346 } else { 347 // This is the best we can do. 348 os->print("<local%d>", slot); 349 } 350 } 351 } 352 353 StackSlotAnalysisData::StackSlotAnalysisData(BasicType type) : _bci(INVALID), _type(type) {} 354 355 StackSlotAnalysisData::StackSlotAnalysisData(int bci, BasicType type) : _bci((u2)bci), _type(type) { 356 assert(bci >= 0, "BCI must be >= 0"); 357 assert(bci < 65536, "BCI must be < 65536"); 358 } 359 360 unsigned int StackSlotAnalysisData::get_bci() { 361 return _bci; 362 } 363 364 BasicType StackSlotAnalysisData::get_type() { 365 return (BasicType)_type; 366 } 367 368 StackSlotAnalysisData StackSlotAnalysisData::merge(StackSlotAnalysisData other) { 369 if (get_type() != other.get_type()) { 370 if (((get_type() == T_OBJECT) || (get_type() == T_ARRAY)) && 371 ((other.get_type() == T_OBJECT) || (other.get_type() == T_ARRAY))) { 372 if (get_bci() == other.get_bci()) { 373 return StackSlotAnalysisData(get_bci(), T_OBJECT); 374 } else { 375 return StackSlotAnalysisData(T_OBJECT); 376 } 377 } else { 378 return StackSlotAnalysisData(T_CONFLICT); 379 } 380 } 381 382 if (get_bci() == other.get_bci()) { 383 return *this; 384 } else { 385 return StackSlotAnalysisData(get_type()); 386 } 387 } 388 389 SimulatedOperandStack::SimulatedOperandStack(const SimulatedOperandStack ©) { 390 for (int i = 0; i < copy.get_size(); i++) { 391 push_raw(copy._stack.at(i)); 392 } 393 _written_local_slots = copy._written_local_slots; 394 } 395 396 void SimulatedOperandStack::push_raw(StackSlotAnalysisData slotData) { 397 if (slotData.get_type() == T_VOID) { 398 return; 399 } 400 401 _stack.push(slotData); 402 } 403 404 void SimulatedOperandStack::push(StackSlotAnalysisData slotData) { 405 if (type2size[slotData.get_type()] == 2) { 406 push_raw(slotData); 407 push_raw(slotData); 408 } else { 409 push_raw(slotData); 410 } 411 } 412 413 void SimulatedOperandStack::push(int bci, BasicType type) { 414 push(StackSlotAnalysisData(bci, type)); 415 } 416 417 void SimulatedOperandStack::pop(int slots) { 418 for (int i = 0; i < slots; ++i) { 419 _stack.pop(); 420 } 421 422 assert(get_size() >= 0, "Popped too many slots"); 423 } 424 425 void SimulatedOperandStack::merge(SimulatedOperandStack const& other) { 426 assert(get_size() == other.get_size(), "Stacks not of same size"); 427 428 for (int i = get_size() - 1; i >= 0; --i) { 429 _stack.at_put(i, _stack.at(i).merge(other._stack.at(i))); 430 } 431 _written_local_slots = _written_local_slots | other._written_local_slots; 432 } 433 434 int SimulatedOperandStack::get_size() const { 435 return _stack.length(); 436 } 437 438 StackSlotAnalysisData SimulatedOperandStack::get_slot_data(int slot) { 439 assert(slot >= 0, "Slot=%d < 0", slot); 440 assert(slot < get_size(), "Slot=%d >= size=%d", slot, get_size()); 441 442 return _stack.at(get_size() - slot - 1); 443 } 444 445 void SimulatedOperandStack::set_local_slot_written(int i) { 446 // Local slots > 63 are very unlikely. Consider these 447 // as written all the time. Saves space and complexity 448 // for dynamic data size. 449 if (i > 63) return; 450 _written_local_slots = _written_local_slots | (1ULL << i); 451 } 452 453 bool SimulatedOperandStack::local_slot_was_written(int i) { 454 if (i > 63) return true; 455 return (_written_local_slots & (1ULL << i)) != 0; 456 } 457 458 ExceptionMessageBuilder::ExceptionMessageBuilder(Method* method, int bci) : 459 _method(method), _nr_of_entries(0), 460 _added_one(true), _all_processed(false) { 461 462 ConstMethod* const_method = method->constMethod(); 463 const int len = const_method->code_size(); 464 465 assert(bci >= 0, "BCI too low: %d", bci); 466 assert(bci < len, "BCI too large: %d size: %d", bci, len); 467 468 _stacks = new GrowableArray<SimulatedOperandStack*> (len + 1); 469 470 for (int i = 0; i <= len; ++i) { 471 _stacks->push(nullptr); 472 } 473 474 // Initialize stack a bci 0. 475 _stacks->at_put(0, new SimulatedOperandStack()); 476 477 // And initialize the start of all exception handlers. 478 if (const_method->has_exception_table()) { 479 ExceptionTableElement *et = const_method->exception_table_start(); 480 for (int i = 0; i < const_method->exception_table_length(); ++i) { 481 u2 index = et[i].handler_pc; 482 483 if (_stacks->at(index) == nullptr) { 484 _stacks->at_put(index, new SimulatedOperandStack()); 485 _stacks->at(index)->push(index, T_OBJECT); 486 } 487 } 488 } 489 490 // Do this until each bytecode has a stack or we haven't 491 // added a new stack in one iteration. 492 while (!_all_processed && _added_one) { 493 _all_processed = true; 494 _added_one = false; 495 496 for (int i = 0; i < len; ) { 497 // Analyse bytecode i. Step by size of the analyzed bytecode to next bytecode. 498 i += do_instruction(i); 499 500 // If we want the data only for a certain bci, we can possibly end early. 501 if ((bci == i) && (_stacks->at(i) != nullptr)) { 502 _all_processed = true; 503 break; 504 } 505 506 if (_nr_of_entries > _max_entries) { 507 return; 508 } 509 } 510 } 511 } 512 513 ExceptionMessageBuilder::~ExceptionMessageBuilder() { 514 if (_stacks != nullptr) { 515 for (int i = 0; i < _stacks->length(); ++i) { 516 delete _stacks->at(i); 517 } 518 } 519 } 520 521 void ExceptionMessageBuilder::merge(int bci, SimulatedOperandStack* stack) { 522 assert(stack != _stacks->at(bci), "Cannot merge itself"); 523 524 if (_stacks->at(bci) != nullptr) { 525 stack->merge(*_stacks->at(bci)); 526 } else { 527 // Got a new stack, so count the entries. 528 _nr_of_entries += stack->get_size(); 529 } 530 531 // Replace the stack at this bci with a copy of our new merged stack. 532 delete _stacks->at(bci); 533 _stacks->at_put(bci, new SimulatedOperandStack(*stack)); 534 } 535 536 int ExceptionMessageBuilder::do_instruction(int bci) { 537 ConstMethod* const_method = _method->constMethod(); 538 address code_base = _method->constMethod()->code_base(); 539 540 // We use the java code, since we don't want to cope with all the fast variants. 541 int len = Bytecodes::java_length_at(_method, code_base + bci); 542 543 // If we have no stack for this bci, we cannot process the bytecode now. 544 if (_stacks->at(bci) == nullptr) { 545 _all_processed = false; 546 return len; 547 } 548 549 // Make a local copy of the stack for this bci to work on. 550 SimulatedOperandStack* stack = new SimulatedOperandStack(*_stacks->at(bci)); 551 552 // dest_bci is != -1 if we branch. 553 int dest_bci = -1; 554 555 // This is for table and lookup switch. 556 static const int initial_length = 2; 557 GrowableArray<int> dests(initial_length); 558 559 bool flow_ended = false; 560 561 // Get the bytecode. 562 bool is_wide = false; 563 Bytecodes::Code raw_code = Bytecodes::code_at(_method, code_base + bci); 564 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci); 565 int pos = bci + 1; 566 567 if (code == Bytecodes::_wide) { 568 is_wide = true; 569 code = Bytecodes::java_code_at(_method, code_base + bci + 1); 570 pos += 1; 571 } 572 573 // Now simulate the action of each bytecode. 574 switch (code) { 575 case Bytecodes::_nop: 576 case Bytecodes::_aconst_null: 577 case Bytecodes::_iconst_m1: 578 case Bytecodes::_iconst_0: 579 case Bytecodes::_iconst_1: 580 case Bytecodes::_iconst_2: 581 case Bytecodes::_iconst_3: 582 case Bytecodes::_iconst_4: 583 case Bytecodes::_iconst_5: 584 case Bytecodes::_lconst_0: 585 case Bytecodes::_lconst_1: 586 case Bytecodes::_fconst_0: 587 case Bytecodes::_fconst_1: 588 case Bytecodes::_fconst_2: 589 case Bytecodes::_dconst_0: 590 case Bytecodes::_dconst_1: 591 case Bytecodes::_bipush: 592 case Bytecodes::_sipush: 593 case Bytecodes::_iload: 594 case Bytecodes::_lload: 595 case Bytecodes::_fload: 596 case Bytecodes::_dload: 597 case Bytecodes::_aload: 598 case Bytecodes::_iload_0: 599 case Bytecodes::_iload_1: 600 case Bytecodes::_iload_2: 601 case Bytecodes::_iload_3: 602 case Bytecodes::_lload_0: 603 case Bytecodes::_lload_1: 604 case Bytecodes::_lload_2: 605 case Bytecodes::_lload_3: 606 case Bytecodes::_fload_0: 607 case Bytecodes::_fload_1: 608 case Bytecodes::_fload_2: 609 case Bytecodes::_fload_3: 610 case Bytecodes::_dload_0: 611 case Bytecodes::_dload_1: 612 case Bytecodes::_dload_2: 613 case Bytecodes::_dload_3: 614 case Bytecodes::_aload_0: 615 case Bytecodes::_aload_1: 616 case Bytecodes::_aload_2: 617 case Bytecodes::_aload_3: 618 case Bytecodes::_iinc: 619 case Bytecodes::_new: 620 stack->push(bci, Bytecodes::result_type(code)); 621 break; 622 623 case Bytecodes::_ldc: 624 case Bytecodes::_ldc_w: 625 case Bytecodes::_ldc2_w: { 626 int cp_index; 627 ConstantPool* cp = _method->constants(); 628 629 if (code == Bytecodes::_ldc) { 630 cp_index = *(uint8_t*) (code_base + pos); 631 632 if (raw_code == Bytecodes::_fast_aldc) { 633 cp_index = cp->object_to_cp_index(cp_index); 634 } 635 } else { 636 if (raw_code == Bytecodes::_fast_aldc_w) { 637 cp_index = Bytes::get_native_u2(code_base + pos); 638 cp_index = cp->object_to_cp_index(cp_index); 639 } 640 else { 641 cp_index = Bytes::get_Java_u2(code_base + pos); 642 } 643 } 644 645 constantTag tag = cp->constant_tag_at(cp_index); 646 if (tag.is_klass() || tag.is_unresolved_klass() || 647 tag.is_method() || tag.is_interface_method() || 648 tag.is_field() || tag.is_string()) { 649 stack->push(bci, T_OBJECT); 650 } else if (tag.is_int()) { 651 stack->push(bci, T_INT); 652 } else if (tag.is_long()) { 653 stack->push(bci, T_LONG); 654 } else if (tag.is_float()) { 655 stack->push(bci, T_FLOAT); 656 } else if (tag.is_double()) { 657 stack->push(bci, T_DOUBLE); 658 } else { 659 assert(false, "Unexpected tag"); 660 } 661 break; 662 } 663 664 case Bytecodes::_iaload: 665 case Bytecodes::_faload: 666 case Bytecodes::_aaload: 667 case Bytecodes::_baload: 668 case Bytecodes::_caload: 669 case Bytecodes::_saload: 670 case Bytecodes::_laload: 671 case Bytecodes::_daload: 672 stack->pop(2); 673 stack->push(bci, Bytecodes::result_type(code)); 674 break; 675 676 case Bytecodes::_istore: 677 case Bytecodes::_lstore: 678 case Bytecodes::_fstore: 679 case Bytecodes::_dstore: 680 case Bytecodes::_astore: 681 int index; 682 if (is_wide) { 683 index = Bytes::get_Java_u2(code_base + bci + 2); 684 } else { 685 index = *(uint8_t*) (code_base + bci + 1); 686 } 687 stack->set_local_slot_written(index); 688 stack->pop(-Bytecodes::depth(code)); 689 break; 690 case Bytecodes::_istore_0: 691 case Bytecodes::_lstore_0: 692 case Bytecodes::_fstore_0: 693 case Bytecodes::_dstore_0: 694 case Bytecodes::_astore_0: 695 stack->set_local_slot_written(0); 696 stack->pop(-Bytecodes::depth(code)); 697 break; 698 case Bytecodes::_istore_1: 699 case Bytecodes::_fstore_1: 700 case Bytecodes::_lstore_1: 701 case Bytecodes::_dstore_1: 702 case Bytecodes::_astore_1: 703 stack->set_local_slot_written(1); 704 stack->pop(-Bytecodes::depth(code)); 705 break; 706 case Bytecodes::_istore_2: 707 case Bytecodes::_lstore_2: 708 case Bytecodes::_fstore_2: 709 case Bytecodes::_dstore_2: 710 case Bytecodes::_astore_2: 711 stack->set_local_slot_written(2); 712 stack->pop(-Bytecodes::depth(code)); 713 break; 714 case Bytecodes::_istore_3: 715 case Bytecodes::_lstore_3: 716 case Bytecodes::_fstore_3: 717 case Bytecodes::_dstore_3: 718 case Bytecodes::_astore_3: 719 stack->set_local_slot_written(3); 720 stack->pop(-Bytecodes::depth(code)); 721 break; 722 case Bytecodes::_iastore: 723 case Bytecodes::_lastore: 724 case Bytecodes::_fastore: 725 case Bytecodes::_dastore: 726 case Bytecodes::_aastore: 727 case Bytecodes::_bastore: 728 case Bytecodes::_castore: 729 case Bytecodes::_sastore: 730 case Bytecodes::_pop: 731 case Bytecodes::_pop2: 732 case Bytecodes::_monitorenter: 733 case Bytecodes::_monitorexit: 734 case Bytecodes::_breakpoint: 735 stack->pop(-Bytecodes::depth(code)); 736 break; 737 738 case Bytecodes::_dup: 739 stack->push_raw(stack->get_slot_data(0)); 740 break; 741 742 case Bytecodes::_dup_x1: { 743 StackSlotAnalysisData top1 = stack->get_slot_data(0); 744 StackSlotAnalysisData top2 = stack->get_slot_data(1); 745 stack->pop(2); 746 stack->push_raw(top1); 747 stack->push_raw(top2); 748 stack->push_raw(top1); 749 break; 750 } 751 752 case Bytecodes::_dup_x2: { 753 StackSlotAnalysisData top1 = stack->get_slot_data(0); 754 StackSlotAnalysisData top2 = stack->get_slot_data(1); 755 StackSlotAnalysisData top3 = stack->get_slot_data(2); 756 stack->pop(3); 757 stack->push_raw(top1); 758 stack->push_raw(top3); 759 stack->push_raw(top2); 760 stack->push_raw(top1); 761 break; 762 } 763 764 case Bytecodes::_dup2: 765 stack->push_raw(stack->get_slot_data(1)); 766 // The former '0' entry is now at '1'. 767 stack->push_raw(stack->get_slot_data(1)); 768 break; 769 770 case Bytecodes::_dup2_x1: { 771 StackSlotAnalysisData top1 = stack->get_slot_data(0); 772 StackSlotAnalysisData top2 = stack->get_slot_data(1); 773 StackSlotAnalysisData top3 = stack->get_slot_data(2); 774 stack->pop(3); 775 stack->push_raw(top2); 776 stack->push_raw(top1); 777 stack->push_raw(top3); 778 stack->push_raw(top2); 779 stack->push_raw(top1); 780 break; 781 } 782 783 case Bytecodes::_dup2_x2: { 784 StackSlotAnalysisData top1 = stack->get_slot_data(0); 785 StackSlotAnalysisData top2 = stack->get_slot_data(1); 786 StackSlotAnalysisData top3 = stack->get_slot_data(2); 787 StackSlotAnalysisData top4 = stack->get_slot_data(3); 788 stack->pop(4); 789 stack->push_raw(top2); 790 stack->push_raw(top1); 791 stack->push_raw(top4); 792 stack->push_raw(top3); 793 stack->push_raw(top2); 794 stack->push_raw(top1); 795 break; 796 } 797 798 case Bytecodes::_swap: { 799 StackSlotAnalysisData top1 = stack->get_slot_data(0); 800 StackSlotAnalysisData top2 = stack->get_slot_data(1); 801 stack->pop(2); 802 stack->push(top1); 803 stack->push(top2); 804 break; 805 } 806 807 case Bytecodes::_iadd: 808 case Bytecodes::_ladd: 809 case Bytecodes::_fadd: 810 case Bytecodes::_dadd: 811 case Bytecodes::_isub: 812 case Bytecodes::_lsub: 813 case Bytecodes::_fsub: 814 case Bytecodes::_dsub: 815 case Bytecodes::_imul: 816 case Bytecodes::_lmul: 817 case Bytecodes::_fmul: 818 case Bytecodes::_dmul: 819 case Bytecodes::_idiv: 820 case Bytecodes::_ldiv: 821 case Bytecodes::_fdiv: 822 case Bytecodes::_ddiv: 823 case Bytecodes::_irem: 824 case Bytecodes::_lrem: 825 case Bytecodes::_frem: 826 case Bytecodes::_drem: 827 case Bytecodes::_iand: 828 case Bytecodes::_land: 829 case Bytecodes::_ior: 830 case Bytecodes::_lor: 831 case Bytecodes::_ixor: 832 case Bytecodes::_lxor: 833 stack->pop(2 * type2size[Bytecodes::result_type(code)]); 834 stack->push(bci, Bytecodes::result_type(code)); 835 break; 836 837 case Bytecodes::_ineg: 838 case Bytecodes::_lneg: 839 case Bytecodes::_fneg: 840 case Bytecodes::_dneg: 841 stack->pop(type2size[Bytecodes::result_type(code)]); 842 stack->push(bci, Bytecodes::result_type(code)); 843 break; 844 845 case Bytecodes::_ishl: 846 case Bytecodes::_lshl: 847 case Bytecodes::_ishr: 848 case Bytecodes::_lshr: 849 case Bytecodes::_iushr: 850 case Bytecodes::_lushr: 851 stack->pop(1 + type2size[Bytecodes::result_type(code)]); 852 stack->push(bci, Bytecodes::result_type(code)); 853 break; 854 855 case Bytecodes::_i2l: 856 case Bytecodes::_i2f: 857 case Bytecodes::_i2d: 858 case Bytecodes::_f2i: 859 case Bytecodes::_f2l: 860 case Bytecodes::_f2d: 861 case Bytecodes::_i2b: 862 case Bytecodes::_i2c: 863 case Bytecodes::_i2s: 864 stack->pop(1); 865 stack->push(bci, Bytecodes::result_type(code)); 866 break; 867 868 case Bytecodes::_l2i: 869 case Bytecodes::_l2f: 870 case Bytecodes::_l2d: 871 case Bytecodes::_d2i: 872 case Bytecodes::_d2l: 873 case Bytecodes::_d2f: 874 stack->pop(2); 875 stack->push(bci, Bytecodes::result_type(code)); 876 break; 877 878 case Bytecodes::_lcmp: 879 case Bytecodes::_fcmpl: 880 case Bytecodes::_fcmpg: 881 case Bytecodes::_dcmpl: 882 case Bytecodes::_dcmpg: 883 stack->pop(1 - Bytecodes::depth(code)); 884 stack->push(bci, T_INT); 885 break; 886 887 case Bytecodes::_ifeq: 888 case Bytecodes::_ifne: 889 case Bytecodes::_iflt: 890 case Bytecodes::_ifge: 891 case Bytecodes::_ifgt: 892 case Bytecodes::_ifle: 893 case Bytecodes::_if_icmpeq: 894 case Bytecodes::_if_icmpne: 895 case Bytecodes::_if_icmplt: 896 case Bytecodes::_if_icmpge: 897 case Bytecodes::_if_icmpgt: 898 case Bytecodes::_if_icmple: 899 case Bytecodes::_if_acmpeq: 900 case Bytecodes::_if_acmpne: 901 case Bytecodes::_ifnull: 902 case Bytecodes::_ifnonnull: 903 stack->pop(-Bytecodes::depth(code)); 904 dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos); 905 break; 906 907 case Bytecodes::_jsr: 908 // NOTE: Bytecodes has wrong depth for jsr. 909 stack->push(bci, T_ADDRESS); 910 dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos); 911 flow_ended = true; 912 break; 913 914 case Bytecodes::_jsr_w: { 915 // NOTE: Bytecodes has wrong depth for jsr. 916 stack->push(bci, T_ADDRESS); 917 dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos); 918 flow_ended = true; 919 break; 920 } 921 922 case Bytecodes::_ret: 923 // We don't track local variables, so we cannot know were we 924 // return. This makes the stacks imprecise, but we have to 925 // live with that. 926 flow_ended = true; 927 break; 928 929 case Bytecodes::_tableswitch: { 930 stack->pop(1); 931 pos = (pos + 3) & ~3; 932 dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos); 933 int low = (int32_t) Bytes::get_Java_u4(code_base + pos + 4); 934 int high = (int32_t) Bytes::get_Java_u4(code_base + pos + 8); 935 936 for (int64_t i = low; i <= high; ++i) { 937 dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 4 * (i - low))); 938 } 939 940 break; 941 } 942 943 case Bytecodes::_lookupswitch: { 944 stack->pop(1); 945 pos = (pos + 3) & ~3; 946 dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos); 947 int nr_of_dests = (int32_t) Bytes::get_Java_u4(code_base + pos + 4); 948 949 for (int i = 0; i < nr_of_dests; ++i) { 950 dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 8 * i)); 951 } 952 953 break; 954 } 955 956 case Bytecodes::_ireturn: 957 case Bytecodes::_lreturn: 958 case Bytecodes::_freturn: 959 case Bytecodes::_dreturn: 960 case Bytecodes::_areturn: 961 case Bytecodes::_return: 962 case Bytecodes::_athrow: 963 stack->pop(-Bytecodes::depth(code)); 964 flow_ended = true; 965 break; 966 967 case Bytecodes::_getstatic: 968 case Bytecodes::_getfield: { 969 // Find out the type of the field accessed. 970 int cp_index = Bytes::get_native_u2(code_base + pos); 971 ConstantPool* cp = _method->constants(); 972 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code); 973 int type_index = cp->signature_ref_index_at(name_and_type_index); 974 Symbol* signature = cp->symbol_at(type_index); 975 // Simulate the bytecode: pop the address, push the 'value' loaded 976 // from the field. 977 stack->pop(1 - Bytecodes::depth(code)); 978 stack->push(bci, Signature::basic_type(signature)); 979 break; 980 } 981 982 case Bytecodes::_putstatic: 983 case Bytecodes::_putfield: { 984 int cp_index = Bytes::get_native_u2(code_base + pos); 985 ConstantPool* cp = _method->constants(); 986 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code); 987 int type_index = cp->signature_ref_index_at(name_and_type_index); 988 Symbol* signature = cp->symbol_at(type_index); 989 BasicType bt = Signature::basic_type(signature); 990 stack->pop(type2size[bt] - Bytecodes::depth(code) - 1); 991 break; 992 } 993 994 case Bytecodes::_invokevirtual: 995 case Bytecodes::_invokespecial: 996 case Bytecodes::_invokestatic: 997 case Bytecodes::_invokeinterface: 998 case Bytecodes::_invokedynamic: { 999 ConstantPool* cp = _method->constants(); 1000 int cp_index; 1001 1002 if (code == Bytecodes::_invokedynamic) { 1003 cp_index = ((int) Bytes::get_native_u4(code_base + pos)); 1004 } else { 1005 cp_index = Bytes::get_native_u2(code_base + pos); 1006 } 1007 1008 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code); 1009 int type_index = cp->signature_ref_index_at(name_and_type_index); 1010 Symbol* signature = cp->symbol_at(type_index); 1011 1012 if ((code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic)) { 1013 // Pop receiver. 1014 stack->pop(1); 1015 } 1016 1017 stack->pop(ArgumentSizeComputer(signature).size()); 1018 ResultTypeFinder result_type(signature); 1019 stack->push(bci, result_type.type()); 1020 break; 1021 } 1022 1023 case Bytecodes::_newarray: 1024 case Bytecodes::_anewarray: 1025 case Bytecodes::_instanceof: 1026 stack->pop(1); 1027 stack->push(bci, Bytecodes::result_type(code)); 1028 break; 1029 1030 case Bytecodes::_arraylength: 1031 stack->pop(1); 1032 stack->push(bci, T_INT); 1033 break; 1034 1035 case Bytecodes::_checkcast: 1036 break; 1037 1038 case Bytecodes::_multianewarray: 1039 stack->pop(*(uint8_t*) (code_base + pos + 2)); 1040 stack->push(bci, T_OBJECT); 1041 break; 1042 1043 case Bytecodes::_goto: 1044 stack->pop(-Bytecodes::depth(code)); 1045 dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos); 1046 flow_ended = true; 1047 break; 1048 1049 1050 case Bytecodes::_goto_w: 1051 stack->pop(-Bytecodes::depth(code)); 1052 dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos); 1053 flow_ended = true; 1054 break; 1055 1056 default: 1057 // Allow at least the bcis which have stack info to work. 1058 _all_processed = false; 1059 _added_one = false; 1060 delete stack; 1061 1062 return len; 1063 } 1064 1065 // Put new stack to the next instruction, if we might reach it from 1066 // this bci. 1067 if (!flow_ended) { 1068 if (_stacks->at(bci + len) == nullptr) { 1069 _added_one = true; 1070 } 1071 merge(bci + len, stack); 1072 } 1073 1074 // Put the stack to the branch target too. 1075 if (dest_bci != -1) { 1076 if (_stacks->at(dest_bci) == nullptr) { 1077 _added_one = true; 1078 } 1079 merge(dest_bci, stack); 1080 } 1081 1082 // If we have more than one branch target, process these too. 1083 for (int i = 0; i < dests.length(); ++i) { 1084 if (_stacks->at(dests.at(i)) == nullptr) { 1085 _added_one = true; 1086 } 1087 merge(dests.at(i), stack); 1088 } 1089 1090 delete stack; 1091 1092 return len; 1093 } 1094 1095 #define INVALID_BYTECODE_ENCOUNTERED -1 1096 #define NPE_EXPLICIT_CONSTRUCTED -2 1097 int ExceptionMessageBuilder::get_NPE_null_slot(int bci) { 1098 // Get the bytecode. 1099 address code_base = _method->constMethod()->code_base(); 1100 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci); 1101 int pos = bci + 1; // Position of argument of the bytecode. 1102 if (code == Bytecodes::_wide) { 1103 code = Bytecodes::java_code_at(_method, code_base + bci + 1); 1104 pos += 1; 1105 } 1106 1107 switch (code) { 1108 case Bytecodes::_getfield: 1109 case Bytecodes::_arraylength: 1110 case Bytecodes::_athrow: 1111 case Bytecodes::_monitorenter: 1112 case Bytecodes::_monitorexit: 1113 case Bytecodes::_checkcast: 1114 return 0; 1115 case Bytecodes::_iaload: 1116 case Bytecodes::_faload: 1117 case Bytecodes::_aaload: 1118 case Bytecodes::_baload: 1119 case Bytecodes::_caload: 1120 case Bytecodes::_saload: 1121 case Bytecodes::_laload: 1122 case Bytecodes::_daload: 1123 return 1; 1124 case Bytecodes::_iastore: 1125 case Bytecodes::_fastore: 1126 case Bytecodes::_aastore: 1127 case Bytecodes::_bastore: 1128 case Bytecodes::_castore: 1129 case Bytecodes::_sastore: 1130 return 2; 1131 case Bytecodes::_lastore: 1132 case Bytecodes::_dastore: 1133 return 3; 1134 case Bytecodes::_putfield: { 1135 int cp_index = Bytes::get_native_u2(code_base + pos); 1136 ConstantPool* cp = _method->constants(); 1137 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code); 1138 int type_index = cp->signature_ref_index_at(name_and_type_index); 1139 Symbol* signature = cp->symbol_at(type_index); 1140 BasicType bt = Signature::basic_type(signature); 1141 return type2size[bt]; 1142 } 1143 case Bytecodes::_invokevirtual: 1144 case Bytecodes::_invokespecial: 1145 case Bytecodes::_invokeinterface: { 1146 int cp_index = Bytes::get_native_u2(code_base+ pos); 1147 ConstantPool* cp = _method->constants(); 1148 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code); 1149 int name_index = cp->name_ref_index_at(name_and_type_index); 1150 Symbol* name = cp->symbol_at(name_index); 1151 1152 // Assume the call of a constructor can never cause a NullPointerException 1153 // (which is true in Java). This is mainly used to avoid generating wrong 1154 // messages for NullPointerExceptions created explicitly by new in Java code. 1155 if (name != vmSymbols::object_initializer_name()) { 1156 int type_index = cp->signature_ref_index_at(name_and_type_index); 1157 Symbol* signature = cp->symbol_at(type_index); 1158 // The 'this' parameter was null. Return the slot of it. 1159 return ArgumentSizeComputer(signature).size(); 1160 } else { 1161 return NPE_EXPLICIT_CONSTRUCTED; 1162 } 1163 } 1164 1165 default: 1166 break; 1167 } 1168 1169 return INVALID_BYTECODE_ENCOUNTERED; 1170 } 1171 1172 bool ExceptionMessageBuilder::print_NPE_cause(outputStream* os, int bci, int slot) { 1173 if (print_NPE_cause0(os, bci, slot, _max_cause_detail, false, " because \"")) { 1174 address code_base = _method->constMethod()->code_base(); 1175 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci); 1176 if (code == Bytecodes::_aastore) { 1177 os->print("\" is null or is a null-free array and there's an attempt to store null in it"); 1178 } else { 1179 os->print("\" is null"); 1180 } 1181 return true; 1182 } 1183 return false; 1184 } 1185 1186 // Recursively print what was null. 1187 // 1188 // Go to the bytecode that pushed slot 'slot' on the operand stack 1189 // at bytecode 'bci'. Compute a message for that bytecode. If 1190 // necessary (array, field), recur further. 1191 // At most do max_detail recursions. 1192 // Prefix is used to print a proper beginning of the whole 1193 // sentence. 1194 // inner_expr is used to omit some text, like 'static' in 1195 // inner expressions like array subscripts. 1196 // 1197 // Returns true if something was printed. 1198 // 1199 bool ExceptionMessageBuilder::print_NPE_cause0(outputStream* os, int bci, int slot, 1200 int max_detail, 1201 bool inner_expr, const char *prefix) { 1202 assert(bci >= 0, "BCI too low"); 1203 assert(bci < get_size(), "BCI too large"); 1204 1205 if (max_detail <= 0) { 1206 return false; 1207 } 1208 1209 if (_stacks->at(bci) == nullptr) { 1210 return false; 1211 } 1212 1213 SimulatedOperandStack* stack = _stacks->at(bci); 1214 assert(slot >= 0, "Slot nr. too low"); 1215 assert(slot < stack->get_size(), "Slot nr. too large"); 1216 1217 StackSlotAnalysisData slotData = stack->get_slot_data(slot); 1218 1219 if (!slotData.has_bci()) { 1220 return false; 1221 } 1222 1223 // Get the bytecode. 1224 unsigned int source_bci = slotData.get_bci(); 1225 address code_base = _method->constMethod()->code_base(); 1226 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + source_bci); 1227 bool is_wide = false; 1228 int pos = source_bci + 1; 1229 1230 if (code == Bytecodes::_wide) { 1231 is_wide = true; 1232 code = Bytecodes::java_code_at(_method, code_base + source_bci + 1); 1233 pos += 1; 1234 } 1235 1236 if (max_detail == _max_cause_detail && 1237 prefix != nullptr && 1238 code != Bytecodes::_invokevirtual && 1239 code != Bytecodes::_invokespecial && 1240 code != Bytecodes::_invokestatic && 1241 code != Bytecodes::_invokeinterface) { 1242 os->print("%s", prefix); 1243 } 1244 1245 switch (code) { 1246 case Bytecodes::_iload_0: 1247 case Bytecodes::_aload_0: 1248 print_local_var(os, source_bci, _method, 0, !stack->local_slot_was_written(0)); 1249 return true; 1250 1251 case Bytecodes::_iload_1: 1252 case Bytecodes::_aload_1: 1253 print_local_var(os, source_bci, _method, 1, !stack->local_slot_was_written(1)); 1254 return true; 1255 1256 case Bytecodes::_iload_2: 1257 case Bytecodes::_aload_2: 1258 print_local_var(os, source_bci, _method, 2, !stack->local_slot_was_written(2)); 1259 return true; 1260 1261 case Bytecodes::_iload_3: 1262 case Bytecodes::_aload_3: 1263 print_local_var(os, source_bci, _method, 3, !stack->local_slot_was_written(3)); 1264 return true; 1265 1266 case Bytecodes::_iload: 1267 case Bytecodes::_aload: { 1268 int index; 1269 if (is_wide) { 1270 index = Bytes::get_Java_u2(code_base + source_bci + 2); 1271 } else { 1272 index = *(uint8_t*) (code_base + source_bci + 1); 1273 } 1274 print_local_var(os, source_bci, _method, index, !stack->local_slot_was_written(index)); 1275 return true; 1276 } 1277 1278 case Bytecodes::_aconst_null: 1279 os->print("null"); 1280 return true; 1281 case Bytecodes::_iconst_m1: 1282 os->print("-1"); 1283 return true; 1284 case Bytecodes::_iconst_0: 1285 os->print("0"); 1286 return true; 1287 case Bytecodes::_iconst_1: 1288 os->print("1"); 1289 return true; 1290 case Bytecodes::_iconst_2: 1291 os->print("2"); 1292 return true; 1293 case Bytecodes::_iconst_3: 1294 os->print("3"); 1295 return true; 1296 case Bytecodes::_iconst_4: 1297 os->print("4"); 1298 return true; 1299 case Bytecodes::_iconst_5: 1300 os->print("5"); 1301 return true; 1302 case Bytecodes::_bipush: { 1303 jbyte con = *(jbyte*) (code_base + source_bci + 1); 1304 os->print("%d", con); 1305 return true; 1306 } 1307 case Bytecodes::_sipush: { 1308 u2 con = Bytes::get_Java_u2(code_base + source_bci + 1); 1309 os->print("%d", con); 1310 return true; 1311 } 1312 case Bytecodes::_iaload: 1313 case Bytecodes::_aaload: { 1314 // Print the 'name' of the array. Go back to the bytecode that 1315 // pushed the array reference on the operand stack. 1316 if (!print_NPE_cause0(os, source_bci, 1, max_detail - 1, inner_expr)) { 1317 // Returned false. Max recursion depth was reached. Print dummy. 1318 os->print("<array>"); 1319 } 1320 os->print("["); 1321 // Print the index expression. Go back to the bytecode that 1322 // pushed the index on the operand stack. 1323 // inner_expr == true so we don't print unwanted strings 1324 // as "The return value of'". And don't decrement max_detail so we always 1325 // get a value here and only cancel out on the dereference. 1326 if (!print_NPE_cause0(os, source_bci, 0, max_detail, true)) { 1327 // Returned false. We don't print complex array index expressions. Print placeholder. 1328 os->print("..."); 1329 } 1330 os->print("]"); 1331 return true; 1332 } 1333 1334 case Bytecodes::_getstatic: { 1335 int cp_index = Bytes::get_native_u2(code_base + pos); 1336 print_field_and_class(os, _method, cp_index, code); 1337 return true; 1338 } 1339 1340 case Bytecodes::_getfield: { 1341 // Print the sender. Go back to the bytecode that 1342 // pushed the sender on the operand stack. 1343 if (print_NPE_cause0(os, source_bci, 0, max_detail - 1, inner_expr)) { 1344 os->print("."); 1345 } 1346 int cp_index = Bytes::get_native_u2(code_base + pos); 1347 os->print("%s", get_field_name(_method, cp_index, code)); 1348 return true; 1349 } 1350 1351 case Bytecodes::_invokevirtual: 1352 case Bytecodes::_invokespecial: 1353 case Bytecodes::_invokestatic: 1354 case Bytecodes::_invokeinterface: { 1355 int cp_index = Bytes::get_native_u2(code_base + pos); 1356 if (max_detail == _max_cause_detail && !inner_expr) { 1357 os->print(" because the return value of \""); 1358 } 1359 print_method_name(os, _method, cp_index, code); 1360 return true; 1361 } 1362 1363 default: break; 1364 } 1365 return false; 1366 } 1367 1368 void ExceptionMessageBuilder::print_NPE_failed_action(outputStream *os, int bci) { 1369 1370 // Get the bytecode. 1371 address code_base = _method->constMethod()->code_base(); 1372 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci); 1373 int pos = bci + 1; 1374 if (code == Bytecodes::_wide) { 1375 code = Bytecodes::java_code_at(_method, code_base + bci + 1); 1376 pos += 1; 1377 } 1378 1379 switch (code) { 1380 case Bytecodes::_iaload: 1381 os->print("Cannot load from int array"); break; 1382 case Bytecodes::_faload: 1383 os->print("Cannot load from float array"); break; 1384 case Bytecodes::_aaload: 1385 os->print("Cannot load from object array"); break; 1386 case Bytecodes::_baload: 1387 os->print("Cannot load from byte/boolean array"); break; 1388 case Bytecodes::_caload: 1389 os->print("Cannot load from char array"); break; 1390 case Bytecodes::_saload: 1391 os->print("Cannot load from short array"); break; 1392 case Bytecodes::_laload: 1393 os->print("Cannot load from long array"); break; 1394 case Bytecodes::_daload: 1395 os->print("Cannot load from double array"); break; 1396 1397 case Bytecodes::_iastore: 1398 os->print("Cannot store to int array"); break; 1399 case Bytecodes::_fastore: 1400 os->print("Cannot store to float array"); break; 1401 case Bytecodes::_aastore: 1402 os->print("Cannot store to object array"); break; 1403 case Bytecodes::_bastore: 1404 os->print("Cannot store to byte/boolean array"); break; 1405 case Bytecodes::_castore: 1406 os->print("Cannot store to char array"); break; 1407 case Bytecodes::_sastore: 1408 os->print("Cannot store to short array"); break; 1409 case Bytecodes::_lastore: 1410 os->print("Cannot store to long array"); break; 1411 case Bytecodes::_dastore: 1412 os->print("Cannot store to double array"); break; 1413 1414 case Bytecodes::_arraylength: 1415 os->print("Cannot read the array length"); break; 1416 case Bytecodes::_athrow: 1417 os->print("Cannot throw exception"); break; 1418 case Bytecodes::_monitorenter: 1419 os->print("Cannot enter synchronized block"); break; 1420 case Bytecodes::_monitorexit: 1421 os->print("Cannot exit synchronized block"); break; 1422 case Bytecodes::_getfield: { 1423 int cp_index = Bytes::get_native_u2(code_base + pos); 1424 ConstantPool* cp = _method->constants(); 1425 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code); 1426 int name_index = cp->name_ref_index_at(name_and_type_index); 1427 Symbol* name = cp->symbol_at(name_index); 1428 os->print("Cannot read field \"%s\"", name->as_C_string()); 1429 } break; 1430 case Bytecodes::_putfield: { 1431 int cp_index = Bytes::get_native_u2(code_base + pos); 1432 os->print("Cannot assign field \"%s\"", get_field_name(_method, cp_index, code)); 1433 } break; 1434 case Bytecodes::_invokevirtual: 1435 case Bytecodes::_invokespecial: 1436 case Bytecodes::_invokeinterface: { 1437 int cp_index = Bytes::get_native_u2(code_base+ pos); 1438 os->print("Cannot invoke \""); 1439 print_method_name(os, _method, cp_index, code); 1440 os->print("\""); 1441 } break; 1442 case Bytecodes::_checkcast: { 1443 int cp_index = Bytes::get_Java_u2(code_base + pos); 1444 ConstantPool* cp = _method->constants(); 1445 os->print("Cannot cast to null-free type \"%s\"", cp->klass_at_noresolve(cp_index)->as_C_string()); 1446 } break; 1447 1448 default: 1449 assert(0, "We should have checked this bytecode in get_NPE_null_slot()."); 1450 break; 1451 } 1452 } 1453 1454 // Main API 1455 bool BytecodeUtils::get_NPE_message_at(outputStream* ss, Method* method, int bci) { 1456 1457 NoSafepointVerifier _nsv; // Cannot use this object over a safepoint. 1458 1459 // If this NPE was created via reflection, we have no real NPE. 1460 if (method->method_holder() == 1461 vmClasses::reflect_DirectConstructorHandleAccessor_NativeAccessor_klass()) { 1462 return false; 1463 } 1464 1465 // Analyse the bytecodes. 1466 ResourceMark rm; 1467 ExceptionMessageBuilder emb(method, bci); 1468 1469 // The slot of the operand stack that contains the null reference. 1470 // Also checks for NPE explicitly constructed and returns NPE_EXPLICIT_CONSTRUCTED. 1471 int slot = emb.get_NPE_null_slot(bci); 1472 1473 // Build the message. 1474 if (slot == NPE_EXPLICIT_CONSTRUCTED) { 1475 // We don't want to print a message. 1476 return false; 1477 } else if (slot == INVALID_BYTECODE_ENCOUNTERED) { 1478 // We encountered a bytecode that does not dereference a reference. 1479 DEBUG_ONLY(ss->print("There cannot be a NullPointerException at bci %d of method %s", 1480 bci, method->external_name())); 1481 NOT_DEBUG(return false); 1482 } else { 1483 // Print string describing which action (bytecode) could not be 1484 // performed because of the null reference. 1485 emb.print_NPE_failed_action(ss, bci); 1486 // Print a description of what is null. 1487 if (!emb.print_NPE_cause(ss, bci, slot)) { 1488 // Nothing was printed. End the sentence without the 'because' 1489 // subordinate sentence. 1490 } 1491 } 1492 return true; 1493 }