< prev index next >

src/hotspot/share/interpreter/bytecodeUtils.cpp

Print this page
rev 56326 : 8218628: Add detailed message to NullPointerException describing what is null.
Summary: This is the implementation of JEP 358: Helpful NullPointerExceptions.
Reviewed-by: coleenp, clanger, rschmelter
rev 56327 : [mq]: find_local_stores.patch

@@ -80,21 +80,37 @@
 };
 
 // A stack consisting of SimulatedOperandStackEntries.
 // This represents the analysis information for the operand stack
 // for a given bytecode at a given bci.
+// It also holds an additional field that serves to collect
+// information whether local slots were written.
 class SimulatedOperandStack: CHeapObj<mtInternal> {
 
  private:
 
   friend class ExceptionMessageBuilder;
   friend class StackSlotAnalysisData;
 
   // The stack.
   GrowableArray<StackSlotAnalysisData> _stack;
 
-  SimulatedOperandStack() { };
+  // Optimized bytecode can reuse local variable slots for several
+  // local variables.
+  // If there is no variable name information, we print 'parameter<i>'
+  // if a parameter maps to a local slot. Once a local slot has been
+  // written, we don't know any more whether it was written as the
+  // corresponding parameter, or whether another local has been
+  // mapped to the slot. So we don't want to print 'parameter<i>' any
+  // more, but 'local<i>'. Similary for 'this'.
+  // Therefore, during the analysis, we mark a bit for local slots that
+  // get written and propagate this information.
+  // We only run the analysis for 64 slots. If a method has more
+  // parameters, we print 'local<i>' in all cases.
+  uint64_t _written_local_slots;
+
+  SimulatedOperandStack(): _written_local_slots(0) { };
   SimulatedOperandStack(const SimulatedOperandStack &copy);
 
   // Pushes the given slot data.
   void push_raw(StackSlotAnalysisData slotData);
 

@@ -117,10 +133,16 @@
   // Returns the size of the stack.
   int get_size() const;
 
   // Returns the slot data at the given index. Slot 0 is top of stack.
   StackSlotAnalysisData get_slot_data(int slot);
+
+  // Mark that local slot i was written.
+  void set_local_slot_written(int i);
+
+  // Check whether local slot i was written by this or a previous bytecode.
+  bool local_slot_was_written(int i);
 };
 
 // Helper class to build internal exception messages for exceptions
 // that are thrown because prerequisites to execute a bytecode
 // are not met.

@@ -278,11 +300,11 @@
 static char const* get_field_name(Method* method, int cp_index) {
   Symbol* name = method->constants()->name_ref_at(cp_index);
   return name->as_C_string();
 }
 
-static void print_local_var(outputStream *os, unsigned int bci, Method* method, int slot) {
+static void print_local_var(outputStream *os, unsigned int bci, Method* method, int slot, bool is_parameter) {
   if (method->has_localvariable_table()) {
     for (int i = 0; i < method->localvariable_table_length(); i++) {
       LocalVariableTableElement* elem = method->localvariable_table_start() + i;
       unsigned int start = elem->start_bci;
       unsigned int end = start + elem->length;

@@ -296,11 +318,11 @@
       }
     }
   }
 
   // Handle at least some cases we know.
-  if (!method->is_static() && (slot == 0)) {
+  if (!method->is_static() && (slot == 0) && is_parameter) {
     os->print("this");
   } else {
     int curr = method->is_static() ? 0 : 1;
     SignatureStream ss(method->signature());
     int param_index = 1;

@@ -317,11 +339,11 @@
       }
       param_index += 1;
       curr += size;
     }
 
-    if (found) {
+    if (found && is_parameter) {
       os->print("<parameter%d>", param_index);
     } else {
       // This is the best we can do.
       os->print("<local%d>", slot);
     }

@@ -366,10 +388,11 @@
 
 SimulatedOperandStack::SimulatedOperandStack(const SimulatedOperandStack &copy) {
   for (int i = 0; i < copy.get_size(); i++) {
     push_raw(copy._stack.at(i));
   }
+  _written_local_slots = copy._written_local_slots;
 }
 
 void SimulatedOperandStack::push_raw(StackSlotAnalysisData slotData) {
   if (slotData.get_type() == T_VOID) {
     return;

@@ -403,10 +426,11 @@
   assert(get_size() == other.get_size(), "Stacks not of same size");
 
   for (int i = get_size() - 1; i >= 0; --i) {
     _stack.at_put(i, _stack.at(i).merge(other._stack.at(i)));
   }
+  _written_local_slots = _written_local_slots | other._written_local_slots;
 }
 
 int SimulatedOperandStack::get_size() const {
   return _stack.length();
 }

@@ -416,10 +440,23 @@
   assert(slot < get_size(), "Slot >= size");
 
   return _stack.at(get_size() - slot - 1);
 }
 
+void SimulatedOperandStack::set_local_slot_written(int i) {
+  // Local slots > 63 are very unlikely. Consider these
+  // as written all the time. Saves space and complexity
+  // for dynamic data size.
+  if (i > 63) return;
+  _written_local_slots = _written_local_slots | (1 << i);
+}
+
+bool SimulatedOperandStack::local_slot_was_written(int i) {
+  if (i > 63) return true;
+  return (_written_local_slots & (1 << i)) != 0;
+}
+
 ExceptionMessageBuilder::ExceptionMessageBuilder(Method* method, int bci) :
                     _method(method), _nr_of_entries(0),
                     _added_one(true), _all_processed(false) {
   assert(bci >= 0, "BCI too low");
   assert(bci < get_size(), "BCI too large");

@@ -519,15 +556,17 @@
   GrowableArray<int> dests(initial_length);
 
   bool flow_ended = false;
 
   // Get the bytecode.
+  bool is_wide = false;
   Bytecodes::Code raw_code = Bytecodes::code_at(_method, code_base + bci);
   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
   int pos = bci + 1;
 
   if (code == Bytecodes::_wide) {
+    is_wide = true;
     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
     pos += 1;
   }
 
   // Now simulate the action of each bytecode.

@@ -636,30 +675,51 @@
     case Bytecodes::_istore:
     case Bytecodes::_lstore:
     case Bytecodes::_fstore:
     case Bytecodes::_dstore:
     case Bytecodes::_astore:
+      int index;
+      if (is_wide) {
+        index = Bytes::get_Java_u2(code_base + bci + 2);
+      } else {
+        index = *(uint8_t*) (code_base + bci + 1);
+      }
+      stack->set_local_slot_written(index);
+      stack->pop(-Bytecodes::depth(code));
+      break;
     case Bytecodes::_istore_0:
-    case Bytecodes::_istore_1:
-    case Bytecodes::_istore_2:
-    case Bytecodes::_istore_3:
     case Bytecodes::_lstore_0:
-    case Bytecodes::_lstore_1:
-    case Bytecodes::_lstore_2:
-    case Bytecodes::_lstore_3:
     case Bytecodes::_fstore_0:
-    case Bytecodes::_fstore_1:
-    case Bytecodes::_fstore_2:
-    case Bytecodes::_fstore_3:
     case Bytecodes::_dstore_0:
-    case Bytecodes::_dstore_1:
-    case Bytecodes::_dstore_2:
-    case Bytecodes::_dstore_3:
     case Bytecodes::_astore_0:
+      stack->set_local_slot_written(0);
+      stack->pop(-Bytecodes::depth(code));
+      break;
+    case Bytecodes::_istore_1:
+    case Bytecodes::_fstore_1:
+    case Bytecodes::_lstore_1:
+    case Bytecodes::_dstore_1:
     case Bytecodes::_astore_1:
+      stack->set_local_slot_written(1);
+      stack->pop(-Bytecodes::depth(code));
+      break;
+    case Bytecodes::_istore_2:
+    case Bytecodes::_lstore_2:
+    case Bytecodes::_fstore_2:
+    case Bytecodes::_dstore_2:
     case Bytecodes::_astore_2:
+      stack->set_local_slot_written(2);
+      stack->pop(-Bytecodes::depth(code));
+      break;
+    case Bytecodes::_istore_3:
+    case Bytecodes::_lstore_3:
+    case Bytecodes::_fstore_3:
+    case Bytecodes::_dstore_3:
     case Bytecodes::_astore_3:
+      stack->set_local_slot_written(3);
+      stack->pop(-Bytecodes::depth(code));
+      break;
     case Bytecodes::_iastore:
     case Bytecodes::_lastore:
     case Bytecodes::_fastore:
     case Bytecodes::_dastore:
     case Bytecodes::_aastore:

@@ -1175,37 +1235,37 @@
   }
 
   switch (code) {
     case Bytecodes::_iload_0:
     case Bytecodes::_aload_0:
-      print_local_var(os, source_bci, _method, 0);
+      print_local_var(os, source_bci, _method, 0, !stack->local_slot_was_written(0));
       return true;
 
     case Bytecodes::_iload_1:
     case Bytecodes::_aload_1:
-      print_local_var(os, source_bci, _method, 1);
+      print_local_var(os, source_bci, _method, 1, !stack->local_slot_was_written(1));
       return true;
 
     case Bytecodes::_iload_2:
     case Bytecodes::_aload_2:
-      print_local_var(os, source_bci, _method, 2);
+      print_local_var(os, source_bci, _method, 2, !stack->local_slot_was_written(2));
       return true;
 
     case Bytecodes::_iload_3:
     case Bytecodes::_aload_3:
-      print_local_var(os, source_bci, _method, 3);
+      print_local_var(os, source_bci, _method, 3, !stack->local_slot_was_written(3));
       return true;
 
     case Bytecodes::_iload:
     case Bytecodes::_aload: {
       int index;
       if (is_wide) {
         index = Bytes::get_Java_u2(code_base + source_bci + 2);
       } else {
         index = *(uint8_t*) (code_base + source_bci + 1);
       }
-      print_local_var(os, source_bci, _method, index);
+      print_local_var(os, source_bci, _method, index, !stack->local_slot_was_written(index));
       return true;
     }
 
     case Bytecodes::_aconst_null:
       os->print("null");
< prev index next >