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