1 /*
   2  * Copyright (c) 2019, 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/systemDictionary.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>'. Similary 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 &copy);
 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 occured.
 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 the 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 = NULL);
 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) {
 274   ResourceMark rm;
 275   ConstantPool* cp  = method->constants();
 276   Symbol* klass     = cp->klass_ref_at_noresolve(cp_index);
 277   Symbol* name      = cp->name_ref_at(cp_index);
 278   Symbol* signature = cp->signature_ref_at(cp_index);
 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) {
 290   ResourceMark rm;
 291   ConstantPool* cp = method->constants();
 292   Symbol* klass    = cp->klass_ref_at_noresolve(cp_index);
 293   Symbol *name     = cp->name_ref_at(cp_index);
 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) {
 301   Symbol* name = method->constants()->name_ref_at(cp_index);
 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(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 &copy) {
 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 < 0");
 440   assert(slot < get_size(), "Slot >= 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 | (1 << i);
 451 }
 452 
 453 bool SimulatedOperandStack::local_slot_was_written(int i) {
 454   if (i > 63) return true;
 455   return (_written_local_slots & (1 << 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   assert(bci >= 0, "BCI too low");
 462   assert(bci < get_size(), "BCI too large");
 463 
 464   ConstMethod* const_method = method->constMethod();
 465   const int len = const_method->code_size();
 466 
 467   _stacks = new GrowableArray<SimulatedOperandStack*> (len + 1);
 468 
 469   for (int i = 0; i <= len; ++i) {
 470     _stacks->push(NULL);
 471   }
 472 
 473   // Initialize stack a bci 0.
 474   _stacks->at_put(0, new SimulatedOperandStack());
 475 
 476   // And initialize the start of all exception handlers.
 477   if (const_method->has_exception_handler()) {
 478     ExceptionTableElement *et = const_method->exception_table_start();
 479     for (int i = 0; i < const_method->exception_table_length(); ++i) {
 480       u2 index = et[i].handler_pc;
 481 
 482       if (_stacks->at(index) == NULL) {
 483         _stacks->at_put(index, new SimulatedOperandStack());
 484         _stacks->at(index)->push(index, T_OBJECT);
 485       }
 486     }
 487   }
 488 
 489   // Do this until each bytecode has a stack or we haven't
 490   // added a new stack in one iteration.
 491   while (!_all_processed && _added_one) {
 492     _all_processed = true;
 493     _added_one = false;
 494 
 495     for (int i = 0; i < len; ) {
 496       // Analyse bytecode i. Step by size of the analyzed bytecode to next bytecode.
 497       i += do_instruction(i);
 498 
 499       // If we want the data only for a certain bci, we can possibly end early.
 500       if ((bci == i) && (_stacks->at(i) != NULL)) {
 501         _all_processed = true;
 502         break;
 503       }
 504 
 505       if (_nr_of_entries > _max_entries) {
 506         return;
 507       }
 508     }
 509   }
 510 }
 511 
 512 ExceptionMessageBuilder::~ExceptionMessageBuilder() {
 513   if (_stacks != NULL) {
 514     for (int i = 0; i < _stacks->length(); ++i) {
 515       delete _stacks->at(i);
 516     }
 517   }
 518 }
 519 
 520 void ExceptionMessageBuilder::merge(int bci, SimulatedOperandStack* stack) {
 521   assert(stack != _stacks->at(bci), "Cannot merge itself");
 522 
 523   if (_stacks->at(bci) != NULL) {
 524     stack->merge(*_stacks->at(bci));
 525   } else {
 526     // Got a new stack, so count the entries.
 527     _nr_of_entries += stack->get_size();
 528   }
 529 
 530   // Replace the stack at this bci with a copy of our new merged stack.
 531   delete _stacks->at(bci);
 532   _stacks->at_put(bci, new SimulatedOperandStack(*stack));
 533 }
 534 
 535 int ExceptionMessageBuilder::do_instruction(int bci) {
 536   ConstMethod* const_method = _method->constMethod();
 537   address code_base = _method->constMethod()->code_base();
 538 
 539   // We use the java code, since we don't want to cope with all the fast variants.
 540   int len = Bytecodes::java_length_at(_method, code_base + bci);
 541 
 542   // If we have no stack for this bci, we cannot process the bytecode now.
 543   if (_stacks->at(bci) == NULL) {
 544     _all_processed = false;
 545     return len;
 546   }
 547 
 548   // Make a local copy of the stack for this bci to work on.
 549   SimulatedOperandStack* stack = new SimulatedOperandStack(*_stacks->at(bci));
 550 
 551   // dest_bci is != -1 if we branch.
 552   int dest_bci = -1;
 553 
 554   // This is for table and lookup switch.
 555   static const int initial_length = 2;
 556   GrowableArray<int> dests(initial_length);
 557 
 558   bool flow_ended = false;
 559 
 560   // Get the bytecode.
 561   bool is_wide = false;
 562   Bytecodes::Code raw_code = Bytecodes::code_at(_method, code_base + bci);
 563   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
 564   int pos = bci + 1;
 565 
 566   if (code == Bytecodes::_wide) {
 567     is_wide = true;
 568     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
 569     pos += 1;
 570   }
 571 
 572   // Now simulate the action of each bytecode.
 573   switch (code) {
 574     case Bytecodes::_nop:
 575     case Bytecodes::_aconst_null:
 576     case Bytecodes::_iconst_m1:
 577     case Bytecodes::_iconst_0:
 578     case Bytecodes::_iconst_1:
 579     case Bytecodes::_iconst_2:
 580     case Bytecodes::_iconst_3:
 581     case Bytecodes::_iconst_4:
 582     case Bytecodes::_iconst_5:
 583     case Bytecodes::_lconst_0:
 584     case Bytecodes::_lconst_1:
 585     case Bytecodes::_fconst_0:
 586     case Bytecodes::_fconst_1:
 587     case Bytecodes::_fconst_2:
 588     case Bytecodes::_dconst_0:
 589     case Bytecodes::_dconst_1:
 590     case Bytecodes::_bipush:
 591     case Bytecodes::_sipush:
 592     case Bytecodes::_iload:
 593     case Bytecodes::_lload:
 594     case Bytecodes::_fload:
 595     case Bytecodes::_dload:
 596     case Bytecodes::_aload:
 597     case Bytecodes::_iload_0:
 598     case Bytecodes::_iload_1:
 599     case Bytecodes::_iload_2:
 600     case Bytecodes::_iload_3:
 601     case Bytecodes::_lload_0:
 602     case Bytecodes::_lload_1:
 603     case Bytecodes::_lload_2:
 604     case Bytecodes::_lload_3:
 605     case Bytecodes::_fload_0:
 606     case Bytecodes::_fload_1:
 607     case Bytecodes::_fload_2:
 608     case Bytecodes::_fload_3:
 609     case Bytecodes::_dload_0:
 610     case Bytecodes::_dload_1:
 611     case Bytecodes::_dload_2:
 612     case Bytecodes::_dload_3:
 613     case Bytecodes::_aload_0:
 614     case Bytecodes::_aload_1:
 615     case Bytecodes::_aload_2:
 616     case Bytecodes::_aload_3:
 617     case Bytecodes::_iinc:
 618     case Bytecodes::_new:
 619       stack->push(bci, Bytecodes::result_type(code));
 620       break;
 621 
 622     case Bytecodes::_ldc:
 623     case Bytecodes::_ldc_w:
 624     case Bytecodes::_ldc2_w: {
 625       int cp_index;
 626       ConstantPool* cp = _method->constants();
 627 
 628       if (code == Bytecodes::_ldc) {
 629         cp_index = *(uint8_t*) (code_base + pos);
 630 
 631         if (raw_code == Bytecodes::_fast_aldc) {
 632           cp_index = cp->object_to_cp_index(cp_index);
 633         }
 634       } else {
 635         if (raw_code == Bytecodes::_fast_aldc_w) {
 636           cp_index = Bytes::get_native_u2(code_base + pos);
 637           cp_index = cp->object_to_cp_index(cp_index);
 638         }
 639         else {
 640           cp_index = Bytes::get_Java_u2(code_base + pos);
 641         }
 642       }
 643 
 644       constantTag tag = cp->tag_at(cp_index);
 645       if (tag.is_klass()  || tag.is_unresolved_klass() ||
 646           tag.is_method() || tag.is_interface_method() ||
 647           tag.is_field()  || tag.is_string()) {
 648         stack->push(bci, T_OBJECT);
 649       } else if (tag.is_int()) {
 650         stack->push(bci, T_INT);
 651       } else if (tag.is_long()) {
 652         stack->push(bci, T_LONG);
 653       } else if (tag.is_float()) {
 654         stack->push(bci, T_FLOAT);
 655       } else if (tag.is_double()) {
 656         stack->push(bci, T_DOUBLE);
 657       } else {
 658         assert(false, "Unexpected tag");
 659       }
 660       break;
 661     }
 662 
 663     case Bytecodes::_iaload:
 664     case Bytecodes::_faload:
 665     case Bytecodes::_aaload:
 666     case Bytecodes::_baload:
 667     case Bytecodes::_caload:
 668     case Bytecodes::_saload:
 669     case Bytecodes::_laload:
 670     case Bytecodes::_daload:
 671       stack->pop(2);
 672       stack->push(bci, Bytecodes::result_type(code));
 673       break;
 674 
 675     case Bytecodes::_istore:
 676     case Bytecodes::_lstore:
 677     case Bytecodes::_fstore:
 678     case Bytecodes::_dstore:
 679     case Bytecodes::_astore:
 680       int index;
 681       if (is_wide) {
 682         index = Bytes::get_Java_u2(code_base + bci + 2);
 683       } else {
 684         index = *(uint8_t*) (code_base + bci + 1);
 685       }
 686       stack->set_local_slot_written(index);
 687       stack->pop(-Bytecodes::depth(code));
 688       break;
 689     case Bytecodes::_istore_0:
 690     case Bytecodes::_lstore_0:
 691     case Bytecodes::_fstore_0:
 692     case Bytecodes::_dstore_0:
 693     case Bytecodes::_astore_0:
 694       stack->set_local_slot_written(0);
 695       stack->pop(-Bytecodes::depth(code));
 696       break;
 697     case Bytecodes::_istore_1:
 698     case Bytecodes::_fstore_1:
 699     case Bytecodes::_lstore_1:
 700     case Bytecodes::_dstore_1:
 701     case Bytecodes::_astore_1:
 702       stack->set_local_slot_written(1);
 703       stack->pop(-Bytecodes::depth(code));
 704       break;
 705     case Bytecodes::_istore_2:
 706     case Bytecodes::_lstore_2:
 707     case Bytecodes::_fstore_2:
 708     case Bytecodes::_dstore_2:
 709     case Bytecodes::_astore_2:
 710       stack->set_local_slot_written(2);
 711       stack->pop(-Bytecodes::depth(code));
 712       break;
 713     case Bytecodes::_istore_3:
 714     case Bytecodes::_lstore_3:
 715     case Bytecodes::_fstore_3:
 716     case Bytecodes::_dstore_3:
 717     case Bytecodes::_astore_3:
 718       stack->set_local_slot_written(3);
 719       stack->pop(-Bytecodes::depth(code));
 720       break;
 721     case Bytecodes::_iastore:
 722     case Bytecodes::_lastore:
 723     case Bytecodes::_fastore:
 724     case Bytecodes::_dastore:
 725     case Bytecodes::_aastore:
 726     case Bytecodes::_bastore:
 727     case Bytecodes::_castore:
 728     case Bytecodes::_sastore:
 729     case Bytecodes::_pop:
 730     case Bytecodes::_pop2:
 731     case Bytecodes::_monitorenter:
 732     case Bytecodes::_monitorexit:
 733     case Bytecodes::_breakpoint:
 734       stack->pop(-Bytecodes::depth(code));
 735       break;
 736 
 737     case Bytecodes::_dup:
 738       stack->push_raw(stack->get_slot_data(0));
 739       break;
 740 
 741     case Bytecodes::_dup_x1: {
 742       StackSlotAnalysisData top1 = stack->get_slot_data(0);
 743       StackSlotAnalysisData top2 = stack->get_slot_data(1);
 744       stack->pop(2);
 745       stack->push_raw(top1);
 746       stack->push_raw(top2);
 747       stack->push_raw(top1);
 748       break;
 749     }
 750 
 751     case Bytecodes::_dup_x2: {
 752       StackSlotAnalysisData top1 = stack->get_slot_data(0);
 753       StackSlotAnalysisData top2 = stack->get_slot_data(1);
 754       StackSlotAnalysisData top3 = stack->get_slot_data(2);
 755       stack->pop(3);
 756       stack->push_raw(top1);
 757       stack->push_raw(top3);
 758       stack->push_raw(top2);
 759       stack->push_raw(top1);
 760       break;
 761     }
 762 
 763     case Bytecodes::_dup2:
 764       stack->push_raw(stack->get_slot_data(1));
 765       // The former '0' entry is now at '1'.
 766       stack->push_raw(stack->get_slot_data(1));
 767       break;
 768 
 769     case Bytecodes::_dup2_x1: {
 770       StackSlotAnalysisData top1 = stack->get_slot_data(0);
 771       StackSlotAnalysisData top2 = stack->get_slot_data(1);
 772       StackSlotAnalysisData top3 = stack->get_slot_data(2);
 773       stack->pop(3);
 774       stack->push_raw(top2);
 775       stack->push_raw(top1);
 776       stack->push_raw(top3);
 777       stack->push_raw(top2);
 778       stack->push_raw(top1);
 779       break;
 780     }
 781 
 782     case Bytecodes::_dup2_x2: {
 783       StackSlotAnalysisData top1 = stack->get_slot_data(0);
 784       StackSlotAnalysisData top2 = stack->get_slot_data(1);
 785       StackSlotAnalysisData top3 = stack->get_slot_data(2);
 786       StackSlotAnalysisData top4 = stack->get_slot_data(3);
 787       stack->pop(4);
 788       stack->push_raw(top2);
 789       stack->push_raw(top1);
 790       stack->push_raw(top4);
 791       stack->push_raw(top3);
 792       stack->push_raw(top2);
 793       stack->push_raw(top1);
 794       break;
 795     }
 796 
 797     case Bytecodes::_swap: {
 798       StackSlotAnalysisData top1 = stack->get_slot_data(0);
 799       StackSlotAnalysisData top2 = stack->get_slot_data(1);
 800       stack->pop(2);
 801       stack->push(top1);
 802       stack->push(top2);
 803       break;
 804     }
 805 
 806     case Bytecodes::_iadd:
 807     case Bytecodes::_ladd:
 808     case Bytecodes::_fadd:
 809     case Bytecodes::_dadd:
 810     case Bytecodes::_isub:
 811     case Bytecodes::_lsub:
 812     case Bytecodes::_fsub:
 813     case Bytecodes::_dsub:
 814     case Bytecodes::_imul:
 815     case Bytecodes::_lmul:
 816     case Bytecodes::_fmul:
 817     case Bytecodes::_dmul:
 818     case Bytecodes::_idiv:
 819     case Bytecodes::_ldiv:
 820     case Bytecodes::_fdiv:
 821     case Bytecodes::_ddiv:
 822     case Bytecodes::_irem:
 823     case Bytecodes::_lrem:
 824     case Bytecodes::_frem:
 825     case Bytecodes::_drem:
 826     case Bytecodes::_iand:
 827     case Bytecodes::_land:
 828     case Bytecodes::_ior:
 829     case Bytecodes::_lor:
 830     case Bytecodes::_ixor:
 831     case Bytecodes::_lxor:
 832       stack->pop(2 * type2size[Bytecodes::result_type(code)]);
 833       stack->push(bci, Bytecodes::result_type(code));
 834       break;
 835 
 836     case Bytecodes::_ineg:
 837     case Bytecodes::_lneg:
 838     case Bytecodes::_fneg:
 839     case Bytecodes::_dneg:
 840       stack->pop(type2size[Bytecodes::result_type(code)]);
 841       stack->push(bci, Bytecodes::result_type(code));
 842       break;
 843 
 844     case Bytecodes::_ishl:
 845     case Bytecodes::_lshl:
 846     case Bytecodes::_ishr:
 847     case Bytecodes::_lshr:
 848     case Bytecodes::_iushr:
 849     case Bytecodes::_lushr:
 850       stack->pop(1 + type2size[Bytecodes::result_type(code)]);
 851       stack->push(bci, Bytecodes::result_type(code));
 852       break;
 853 
 854     case Bytecodes::_i2l:
 855     case Bytecodes::_i2f:
 856     case Bytecodes::_i2d:
 857     case Bytecodes::_f2i:
 858     case Bytecodes::_f2l:
 859     case Bytecodes::_f2d:
 860     case Bytecodes::_i2b:
 861     case Bytecodes::_i2c:
 862     case Bytecodes::_i2s:
 863       stack->pop(1);
 864       stack->push(bci, Bytecodes::result_type(code));
 865       break;
 866 
 867     case Bytecodes::_l2i:
 868     case Bytecodes::_l2f:
 869     case Bytecodes::_l2d:
 870     case Bytecodes::_d2i:
 871     case Bytecodes::_d2l:
 872     case Bytecodes::_d2f:
 873       stack->pop(2);
 874       stack->push(bci, Bytecodes::result_type(code));
 875       break;
 876 
 877     case Bytecodes::_lcmp:
 878     case Bytecodes::_fcmpl:
 879     case Bytecodes::_fcmpg:
 880     case Bytecodes::_dcmpl:
 881     case Bytecodes::_dcmpg:
 882       stack->pop(1 - Bytecodes::depth(code));
 883       stack->push(bci, T_INT);
 884       break;
 885 
 886     case Bytecodes::_ifeq:
 887     case Bytecodes::_ifne:
 888     case Bytecodes::_iflt:
 889     case Bytecodes::_ifge:
 890     case Bytecodes::_ifgt:
 891     case Bytecodes::_ifle:
 892     case Bytecodes::_if_icmpeq:
 893     case Bytecodes::_if_icmpne:
 894     case Bytecodes::_if_icmplt:
 895     case Bytecodes::_if_icmpge:
 896     case Bytecodes::_if_icmpgt:
 897     case Bytecodes::_if_icmple:
 898     case Bytecodes::_if_acmpeq:
 899     case Bytecodes::_if_acmpne:
 900     case Bytecodes::_ifnull:
 901     case Bytecodes::_ifnonnull:
 902       stack->pop(-Bytecodes::depth(code));
 903       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
 904       break;
 905 
 906     case Bytecodes::_jsr:
 907       // NOTE: Bytecodes has wrong depth for jsr.
 908       stack->push(bci, T_ADDRESS);
 909       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
 910       flow_ended = true;
 911       break;
 912 
 913     case Bytecodes::_jsr_w: {
 914       // NOTE: Bytecodes has wrong depth for jsr.
 915       stack->push(bci, T_ADDRESS);
 916       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
 917       flow_ended = true;
 918       break;
 919     }
 920 
 921     case Bytecodes::_ret:
 922       // We don't track local variables, so we cannot know were we
 923       // return. This makes the stacks imprecise, but we have to
 924       // live with that.
 925       flow_ended = true;
 926       break;
 927 
 928     case Bytecodes::_tableswitch: {
 929       stack->pop(1);
 930       pos = (pos + 3) & ~3;
 931       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
 932       int low = (int32_t) Bytes::get_Java_u4(code_base + pos + 4);
 933       int high = (int32_t) Bytes::get_Java_u4(code_base + pos + 8);
 934 
 935       for (int64_t i = low; i <= high; ++i) {
 936         dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 4 * (i - low)));
 937       }
 938 
 939       break;
 940     }
 941 
 942     case Bytecodes::_lookupswitch: {
 943       stack->pop(1);
 944       pos = (pos + 3) & ~3;
 945       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
 946       int nr_of_dests = (int32_t) Bytes::get_Java_u4(code_base + pos + 4);
 947 
 948       for (int i = 0; i < nr_of_dests; ++i) {
 949         dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 8 * i));
 950       }
 951 
 952       break;
 953     }
 954 
 955     case Bytecodes::_ireturn:
 956     case Bytecodes::_lreturn:
 957     case Bytecodes::_freturn:
 958     case Bytecodes::_dreturn:
 959     case Bytecodes::_areturn:
 960     case Bytecodes::_return:
 961     case Bytecodes::_athrow:
 962       stack->pop(-Bytecodes::depth(code));
 963       flow_ended = true;
 964       break;
 965 
 966     case Bytecodes::_getstatic:
 967     case Bytecodes::_getfield: {
 968       // Find out the type of the field accessed.
 969       int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
 970       ConstantPool* cp = _method->constants();
 971       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
 972       int type_index = cp->signature_ref_index_at(name_and_type_index);
 973       Symbol* signature = cp->symbol_at(type_index);
 974       // Simulate the bytecode: pop the address, push the 'value' loaded
 975       // from the field.
 976       stack->pop(1 - Bytecodes::depth(code));
 977       stack->push(bci, char2type((char) signature->char_at(0)));
 978       break;
 979     }
 980 
 981     case Bytecodes::_putstatic:
 982     case Bytecodes::_putfield: {
 983       int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
 984       ConstantPool* cp = _method->constants();
 985       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
 986       int type_index = cp->signature_ref_index_at(name_and_type_index);
 987       Symbol* signature = cp->symbol_at(type_index);
 988       ResultTypeFinder result_type(signature);
 989       stack->pop(type2size[char2type((char) signature->char_at(0))] - Bytecodes::depth(code) - 1);
 990       break;
 991     }
 992 
 993     case Bytecodes::_invokevirtual:
 994     case Bytecodes::_invokespecial:
 995     case Bytecodes::_invokestatic:
 996     case Bytecodes::_invokeinterface:
 997     case Bytecodes::_invokedynamic: {
 998       ConstantPool* cp = _method->constants();
 999       int cp_index;
1000 
1001       if (code == Bytecodes::_invokedynamic) {
1002         cp_index = ((int) Bytes::get_native_u4(code_base + pos));
1003       } else {
1004         cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1005       }
1006 
1007       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1008       int type_index = cp->signature_ref_index_at(name_and_type_index);
1009       Symbol* signature = cp->symbol_at(type_index);
1010 
1011       if ((code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic)) {
1012         // Pop receiver.
1013         stack->pop(1);
1014       }
1015 
1016       stack->pop(ArgumentSizeComputer(signature).size());
1017       ResultTypeFinder result_type(signature);
1018       stack->push(bci, result_type.type());
1019       break;
1020     }
1021 
1022     case Bytecodes::_newarray:
1023     case Bytecodes::_anewarray:
1024     case Bytecodes::_instanceof:
1025       stack->pop(1);
1026       stack->push(bci, Bytecodes::result_type(code));
1027       break;
1028 
1029     case Bytecodes::_arraylength:
1030       // The return type of arraylength is wrong in the bytecodes table (T_VOID).
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) == NULL) {
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) == NULL) {
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 (int64_t i = 0; i < dests.length(); ++i) {
1084     if (_stacks->at(dests.at(i)) == NULL) {
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       return 0;
1114     case Bytecodes::_iaload:
1115     case Bytecodes::_faload:
1116     case Bytecodes::_aaload:
1117     case Bytecodes::_baload:
1118     case Bytecodes::_caload:
1119     case Bytecodes::_saload:
1120     case Bytecodes::_laload:
1121     case Bytecodes::_daload:
1122       return 1;
1123     case Bytecodes::_iastore:
1124     case Bytecodes::_fastore:
1125     case Bytecodes::_aastore:
1126     case Bytecodes::_bastore:
1127     case Bytecodes::_castore:
1128     case Bytecodes::_sastore:
1129       return 2;
1130     case Bytecodes::_lastore:
1131     case Bytecodes::_dastore:
1132       return 3;
1133     case Bytecodes::_putfield: {
1134         int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1135         ConstantPool* cp = _method->constants();
1136         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1137         int type_index = cp->signature_ref_index_at(name_and_type_index);
1138         Symbol* signature = cp->symbol_at(type_index);
1139         return type2size[char2type((char) signature->char_at(0))];
1140       }
1141     case Bytecodes::_invokevirtual:
1142     case Bytecodes::_invokespecial:
1143     case Bytecodes::_invokeinterface: {
1144         int cp_index = Bytes::get_native_u2(code_base+ pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1145         ConstantPool* cp = _method->constants();
1146         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1147         int name_index = cp->name_ref_index_at(name_and_type_index);
1148         Symbol* name = cp->symbol_at(name_index);
1149 
1150         // Assume the the call of a constructor can never cause a NullPointerException
1151         // (which is true in Java). This is mainly used to avoid generating wrong
1152         // messages for NullPointerExceptions created explicitly by new in Java code.
1153         if (name != vmSymbols::object_initializer_name()) {
1154           int     type_index = cp->signature_ref_index_at(name_and_type_index);
1155           Symbol* signature  = cp->symbol_at(type_index);
1156           // The 'this' parameter was null. Return the slot of it.
1157           return ArgumentSizeComputer(signature).size();
1158         } else {
1159           return NPE_EXPLICIT_CONSTRUCTED;
1160         }
1161       }
1162 
1163     default:
1164       break;
1165   }
1166 
1167   return INVALID_BYTECODE_ENCOUNTERED;
1168 }
1169 
1170 bool ExceptionMessageBuilder::print_NPE_cause(outputStream* os, int bci, int slot) {
1171   if (print_NPE_cause0(os, bci, slot, _max_cause_detail, false, " because '")) {
1172     os->print("' is null.");
1173     return true;
1174   }
1175   return false;
1176 }
1177 
1178 // Recursively print what was null.
1179 //
1180 // Go to the bytecode that pushed slot 'slot' on the operand stack
1181 // at bytecode 'bci'. Compute a message for that bytecode. If
1182 // necessary (array, field), recur further.
1183 // At most do max_detail recursions.
1184 // Prefix is used to print a proper beginning of the whole
1185 // sentence.
1186 // inner_expr is used to omit some text, like 'static' in
1187 // inner expressions like array subscripts.
1188 //
1189 // Returns true if something was printed.
1190 //
1191 bool ExceptionMessageBuilder::print_NPE_cause0(outputStream* os, int bci, int slot,
1192                                                int max_detail,
1193                                                bool inner_expr, const char *prefix) {
1194   assert(bci >= 0, "BCI too low");
1195   assert(bci < get_size(), "BCI too large");
1196 
1197   if (max_detail <= 0) {
1198     return false;
1199   }
1200 
1201   if (_stacks->at(bci) == NULL) {
1202     return false;
1203   }
1204 
1205   SimulatedOperandStack* stack = _stacks->at(bci);
1206   assert(slot >= 0, "Slot nr. too low");
1207   assert(slot < stack->get_size(), "Slot nr. too large");
1208 
1209   StackSlotAnalysisData slotData = stack->get_slot_data(slot);
1210 
1211   if (!slotData.has_bci()) {
1212     return false;
1213   }
1214 
1215   // Get the bytecode.
1216   unsigned int source_bci = slotData.get_bci();
1217   address code_base = _method->constMethod()->code_base();
1218   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + source_bci);
1219   bool is_wide = false;
1220   int pos = source_bci + 1;
1221 
1222   if (code == Bytecodes::_wide) {
1223     is_wide = true;
1224     code = Bytecodes::java_code_at(_method, code_base + source_bci + 1);
1225     pos += 1;
1226   }
1227 
1228   if (max_detail == _max_cause_detail &&
1229       prefix != NULL &&
1230       code != Bytecodes::_invokevirtual &&
1231       code != Bytecodes::_invokespecial &&
1232       code != Bytecodes::_invokestatic &&
1233       code != Bytecodes::_invokeinterface) {
1234     os->print("%s", prefix);
1235   }
1236 
1237   switch (code) {
1238     case Bytecodes::_iload_0:
1239     case Bytecodes::_aload_0:
1240       print_local_var(os, source_bci, _method, 0, !stack->local_slot_was_written(0));
1241       return true;
1242 
1243     case Bytecodes::_iload_1:
1244     case Bytecodes::_aload_1:
1245       print_local_var(os, source_bci, _method, 1, !stack->local_slot_was_written(1));
1246       return true;
1247 
1248     case Bytecodes::_iload_2:
1249     case Bytecodes::_aload_2:
1250       print_local_var(os, source_bci, _method, 2, !stack->local_slot_was_written(2));
1251       return true;
1252 
1253     case Bytecodes::_iload_3:
1254     case Bytecodes::_aload_3:
1255       print_local_var(os, source_bci, _method, 3, !stack->local_slot_was_written(3));
1256       return true;
1257 
1258     case Bytecodes::_iload:
1259     case Bytecodes::_aload: {
1260       int index;
1261       if (is_wide) {
1262         index = Bytes::get_Java_u2(code_base + source_bci + 2);
1263       } else {
1264         index = *(uint8_t*) (code_base + source_bci + 1);
1265       }
1266       print_local_var(os, source_bci, _method, index, !stack->local_slot_was_written(index));
1267       return true;
1268     }
1269 
1270     case Bytecodes::_aconst_null:
1271       os->print("null");
1272       return true;
1273     case Bytecodes::_iconst_m1:
1274       os->print("-1");
1275       return true;
1276     case Bytecodes::_iconst_0:
1277       os->print("0");
1278       return true;
1279     case Bytecodes::_iconst_1:
1280       os->print("1");
1281       return true;
1282     case Bytecodes::_iconst_2:
1283       os->print("2");
1284       return true;
1285     case Bytecodes::_iconst_3:
1286       os->print("3");
1287       return true;
1288     case Bytecodes::_iconst_4:
1289       os->print("4");
1290       return true;
1291     case Bytecodes::_iconst_5:
1292       os->print("5");
1293       return true;
1294     case Bytecodes::_bipush: {
1295       jbyte con = *(jbyte*) (code_base + source_bci + 1);
1296       os->print("%d", con);
1297       return true;
1298     }
1299     case Bytecodes::_sipush: {
1300       u2 con = Bytes::get_Java_u2(code_base + source_bci + 1);
1301       os->print("%d", con);
1302       return true;
1303     }
1304    case Bytecodes::_iaload:
1305    case Bytecodes::_aaload: {
1306       // Print the 'name' of the array. Go back to the bytecode that
1307       // pushed the array reference on the operand stack.
1308      if (!print_NPE_cause0(os, source_bci, 1, max_detail - 1, inner_expr)) {
1309         //  Returned false. Max recursion depth was reached. Print dummy.
1310         os->print("<array>");
1311       }
1312       os->print("[");
1313       // Print the index expression. Go back to the bytecode that
1314       // pushed the index on the operand stack.
1315       // inner_expr == true so we don't print unwanted strings
1316       // as "The return value of'". And don't decrement max_detail so we always
1317       // get a value here and only cancel out on the dereference.
1318       if (!print_NPE_cause0(os, source_bci, 0, max_detail, true)) {
1319         // Returned false. We don't print complex array index expressions. Print placeholder.
1320         os->print("...");
1321       }
1322       os->print("]");
1323       return true;
1324     }
1325 
1326     case Bytecodes::_getstatic: {
1327       int cp_index = Bytes::get_native_u2(code_base + pos) + ConstantPool::CPCACHE_INDEX_TAG;
1328       print_field_and_class(os, _method, cp_index);
1329       return true;
1330     }
1331 
1332     case Bytecodes::_getfield: {
1333       // Print the sender. Go back to the bytecode that
1334       // pushed the sender on the operand stack.
1335       if (print_NPE_cause0(os, source_bci, 0, max_detail - 1, inner_expr)) {
1336         os->print(".");
1337       }
1338       int cp_index = Bytes::get_native_u2(code_base + pos) + ConstantPool::CPCACHE_INDEX_TAG;
1339       os->print("%s", get_field_name(_method, cp_index));
1340       return true;
1341     }
1342 
1343     case Bytecodes::_invokevirtual:
1344     case Bytecodes::_invokespecial:
1345     case Bytecodes::_invokestatic:
1346     case Bytecodes::_invokeinterface: {
1347       int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1348       if (max_detail == _max_cause_detail && !inner_expr) {
1349         os->print(" because the return value of '");
1350       }
1351       print_method_name(os, _method, cp_index);
1352       return true;
1353     }
1354 
1355     default: break;
1356   }
1357   return false;
1358 }
1359 
1360 void ExceptionMessageBuilder::print_NPE_failed_action(outputStream *os, int bci) {
1361 
1362   // Get the bytecode.
1363   address code_base = _method->constMethod()->code_base();
1364   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1365   int pos = bci + 1;
1366   if (code == Bytecodes::_wide) {
1367     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
1368     pos += 1;
1369   }
1370 
1371   switch (code) {
1372     case Bytecodes::_iaload:
1373       os->print("Cannot load from int array"); break;
1374     case Bytecodes::_faload:
1375       os->print("Cannot load from float array"); break;
1376     case Bytecodes::_aaload:
1377       os->print("Cannot load from object array"); break;
1378     case Bytecodes::_baload:
1379       os->print("Cannot load from byte/boolean array"); break;
1380     case Bytecodes::_caload:
1381       os->print("Cannot load from char array"); break;
1382     case Bytecodes::_saload:
1383       os->print("Cannot load from short array"); break;
1384     case Bytecodes::_laload:
1385       os->print("Cannot load from long array"); break;
1386     case Bytecodes::_daload:
1387       os->print("Cannot load from double array"); break;
1388 
1389     case Bytecodes::_iastore:
1390       os->print("Cannot store to int array"); break;
1391     case Bytecodes::_fastore:
1392       os->print("Cannot store to float array"); break;
1393     case Bytecodes::_aastore:
1394       os->print("Cannot store to object array"); break;
1395     case Bytecodes::_bastore:
1396       os->print("Cannot store to byte/boolean array"); break;
1397     case Bytecodes::_castore:
1398       os->print("Cannot store to char array"); break;
1399     case Bytecodes::_sastore:
1400       os->print("Cannot store to short array"); break;
1401     case Bytecodes::_lastore:
1402       os->print("Cannot store to long array"); break;
1403     case Bytecodes::_dastore:
1404       os->print("Cannot store to double array"); break;
1405 
1406     case Bytecodes::_arraylength:
1407       os->print("Cannot read the array length"); break;
1408     case Bytecodes::_athrow:
1409       os->print("Cannot throw exception"); break;
1410     case Bytecodes::_monitorenter:
1411       os->print("Cannot enter synchronized block"); break;
1412     case Bytecodes::_monitorexit:
1413       os->print("Cannot exit synchronized block"); break;
1414     case Bytecodes::_getfield: {
1415         int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1416         ConstantPool* cp = _method->constants();
1417         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1418         int name_index = cp->name_ref_index_at(name_and_type_index);
1419         Symbol* name = cp->symbol_at(name_index);
1420         os->print("Cannot read field '%s'", name->as_C_string());
1421       } break;
1422     case Bytecodes::_putfield: {
1423         int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1424         os->print("Cannot assign field '%s'", get_field_name(_method, cp_index));
1425       } break;
1426     case Bytecodes::_invokevirtual:
1427     case Bytecodes::_invokespecial:
1428     case Bytecodes::_invokeinterface: {
1429         int cp_index = Bytes::get_native_u2(code_base+ pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1430         os->print("Cannot invoke '");
1431         print_method_name(os, _method, cp_index);
1432         os->print("'");
1433       } break;
1434 
1435     default:
1436       assert(0, "We should have checked this bytecode in get_NPE_null_slot().");
1437       break;
1438   }
1439 }
1440 
1441 // Main API
1442 bool BytecodeUtils::get_NPE_message_at(outputStream* ss, Method* method, int bci) {
1443 
1444   NoSafepointVerifier _nsv;   // Cannot use this object over a safepoint.
1445 
1446   // If this NPE was created via reflection, we have no real NPE.
1447   if (method->method_holder() ==
1448       SystemDictionary::reflect_NativeConstructorAccessorImpl_klass()) {
1449     return false;
1450   }
1451 
1452   // Analyse the bytecodes.
1453   ResourceMark rm;
1454   ExceptionMessageBuilder emb(method, bci);
1455 
1456   // The slot of the operand stack that contains the null reference.
1457   // Also checks for NPE explicitly constructed and returns NPE_EXPLICIT_CONSTRUCTED.
1458   int slot = emb.get_NPE_null_slot(bci);
1459 
1460   // Build the message.
1461   if (slot == NPE_EXPLICIT_CONSTRUCTED) {
1462     // We don't want to print a message.
1463     return false;
1464   } else if (slot == INVALID_BYTECODE_ENCOUNTERED) {
1465     // We encountered a bytecode that does not dereference a reference.
1466     DEBUG_ONLY(ss->print("There cannot be a NullPointerException at bci %d of method %s",
1467                          bci, method->external_name()));
1468     NOT_DEBUG(return false);
1469   } else {
1470     // Print string describing which action (bytecode) could not be
1471     // performed because of the null reference.
1472     emb.print_NPE_failed_action(ss, bci);
1473     // Print a description of what is null.
1474     if (!emb.print_NPE_cause(ss, bci, slot)) {
1475       // Nothing was printed. End the sentence without the 'because'
1476       // subordinate sentence.
1477       ss->print(".");
1478     }
1479   }
1480   return true;
1481 }