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