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