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;
1154 // messages for NullPointerExceptions created explicitly by new in Java code.
1155 if (name != vmSymbols::object_initializer_name()) {
1156 int type_index = cp->signature_ref_index_at(name_and_type_index);
1157 Symbol* signature = cp->symbol_at(type_index);
1158 // The 'this' parameter was null. Return the slot of it.
1159 return ArgumentSizeComputer(signature).size();
1160 } else {
1161 return NPE_EXPLICIT_CONSTRUCTED;
1162 }
1163 }
1164
1165 default:
1166 break;
1167 }
1168
1169 return INVALID_BYTECODE_ENCOUNTERED;
1170 }
1171
1172 bool ExceptionMessageBuilder::print_NPE_cause(outputStream* os, int bci, int slot) {
1173 if (print_NPE_cause0(os, bci, slot, _max_cause_detail, false, " because \"")) {
1174 os->print("\" is null");
1175 return true;
1176 }
1177 return false;
1178 }
1179
1180 // Recursively print what was null.
1181 //
1182 // Go to the bytecode that pushed slot 'slot' on the operand stack
1183 // at bytecode 'bci'. Compute a message for that bytecode. If
1184 // necessary (array, field), recur further.
1185 // At most do max_detail recursions.
1186 // Prefix is used to print a proper beginning of the whole
1187 // sentence.
1188 // inner_expr is used to omit some text, like 'static' in
1189 // inner expressions like array subscripts.
1190 //
1191 // Returns true if something was printed.
1192 //
1193 bool ExceptionMessageBuilder::print_NPE_cause0(outputStream* os, int bci, int slot,
1194 int max_detail,
1416 case Bytecodes::_getfield: {
1417 int cp_index = Bytes::get_native_u2(code_base + pos);
1418 ConstantPool* cp = _method->constants();
1419 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code);
1420 int name_index = cp->name_ref_index_at(name_and_type_index);
1421 Symbol* name = cp->symbol_at(name_index);
1422 os->print("Cannot read field \"%s\"", name->as_C_string());
1423 } break;
1424 case Bytecodes::_putfield: {
1425 int cp_index = Bytes::get_native_u2(code_base + pos);
1426 os->print("Cannot assign field \"%s\"", get_field_name(_method, cp_index, code));
1427 } break;
1428 case Bytecodes::_invokevirtual:
1429 case Bytecodes::_invokespecial:
1430 case Bytecodes::_invokeinterface: {
1431 int cp_index = Bytes::get_native_u2(code_base+ pos);
1432 os->print("Cannot invoke \"");
1433 print_method_name(os, _method, cp_index, code);
1434 os->print("\"");
1435 } break;
1436
1437 default:
1438 assert(0, "We should have checked this bytecode in get_NPE_null_slot().");
1439 break;
1440 }
1441 }
1442
1443 // Main API
1444 bool BytecodeUtils::get_NPE_message_at(outputStream* ss, Method* method, int bci) {
1445
1446 NoSafepointVerifier _nsv; // Cannot use this object over a safepoint.
1447
1448 // If this NPE was created via reflection, we have no real NPE.
1449 if (method->method_holder() ==
1450 vmClasses::reflect_DirectConstructorHandleAccessor_NativeAccessor_klass()) {
1451 return false;
1452 }
1453
1454 // Analyse the bytecodes.
1455 ResourceMark rm;
|
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 case Bytecodes::_checkcast:
1115 return 0;
1116 case Bytecodes::_iaload:
1117 case Bytecodes::_faload:
1118 case Bytecodes::_aaload:
1119 case Bytecodes::_baload:
1120 case Bytecodes::_caload:
1121 case Bytecodes::_saload:
1122 case Bytecodes::_laload:
1123 case Bytecodes::_daload:
1124 return 1;
1125 case Bytecodes::_iastore:
1126 case Bytecodes::_fastore:
1127 case Bytecodes::_aastore:
1128 case Bytecodes::_bastore:
1129 case Bytecodes::_castore:
1130 case Bytecodes::_sastore:
1131 return 2;
1132 case Bytecodes::_lastore:
1133 case Bytecodes::_dastore:
1134 return 3;
1155 // messages for NullPointerExceptions created explicitly by new in Java code.
1156 if (name != vmSymbols::object_initializer_name()) {
1157 int type_index = cp->signature_ref_index_at(name_and_type_index);
1158 Symbol* signature = cp->symbol_at(type_index);
1159 // The 'this' parameter was null. Return the slot of it.
1160 return ArgumentSizeComputer(signature).size();
1161 } else {
1162 return NPE_EXPLICIT_CONSTRUCTED;
1163 }
1164 }
1165
1166 default:
1167 break;
1168 }
1169
1170 return INVALID_BYTECODE_ENCOUNTERED;
1171 }
1172
1173 bool ExceptionMessageBuilder::print_NPE_cause(outputStream* os, int bci, int slot) {
1174 if (print_NPE_cause0(os, bci, slot, _max_cause_detail, false, " because \"")) {
1175 address code_base = _method->constMethod()->code_base();
1176 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1177 if (code == Bytecodes::_aastore) {
1178 os->print("\" is null or is a null-free array and there's an attempt to store null in it");
1179 } else {
1180 os->print("\" is null");
1181 }
1182 return true;
1183 }
1184 return false;
1185 }
1186
1187 // Recursively print what was null.
1188 //
1189 // Go to the bytecode that pushed slot 'slot' on the operand stack
1190 // at bytecode 'bci'. Compute a message for that bytecode. If
1191 // necessary (array, field), recur further.
1192 // At most do max_detail recursions.
1193 // Prefix is used to print a proper beginning of the whole
1194 // sentence.
1195 // inner_expr is used to omit some text, like 'static' in
1196 // inner expressions like array subscripts.
1197 //
1198 // Returns true if something was printed.
1199 //
1200 bool ExceptionMessageBuilder::print_NPE_cause0(outputStream* os, int bci, int slot,
1201 int max_detail,
1423 case Bytecodes::_getfield: {
1424 int cp_index = Bytes::get_native_u2(code_base + pos);
1425 ConstantPool* cp = _method->constants();
1426 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code);
1427 int name_index = cp->name_ref_index_at(name_and_type_index);
1428 Symbol* name = cp->symbol_at(name_index);
1429 os->print("Cannot read field \"%s\"", name->as_C_string());
1430 } break;
1431 case Bytecodes::_putfield: {
1432 int cp_index = Bytes::get_native_u2(code_base + pos);
1433 os->print("Cannot assign field \"%s\"", get_field_name(_method, cp_index, code));
1434 } break;
1435 case Bytecodes::_invokevirtual:
1436 case Bytecodes::_invokespecial:
1437 case Bytecodes::_invokeinterface: {
1438 int cp_index = Bytes::get_native_u2(code_base+ pos);
1439 os->print("Cannot invoke \"");
1440 print_method_name(os, _method, cp_index, code);
1441 os->print("\"");
1442 } break;
1443 case Bytecodes::_checkcast: {
1444 int cp_index = Bytes::get_Java_u2(code_base + pos);
1445 ConstantPool* cp = _method->constants();
1446 os->print("Cannot cast to null-free type \"%s\"", cp->klass_at_noresolve(cp_index)->as_C_string());
1447 } break;
1448
1449 default:
1450 assert(0, "We should have checked this bytecode in get_NPE_null_slot().");
1451 break;
1452 }
1453 }
1454
1455 // Main API
1456 bool BytecodeUtils::get_NPE_message_at(outputStream* ss, Method* method, int bci) {
1457
1458 NoSafepointVerifier _nsv; // Cannot use this object over a safepoint.
1459
1460 // If this NPE was created via reflection, we have no real NPE.
1461 if (method->method_holder() ==
1462 vmClasses::reflect_DirectConstructorHandleAccessor_NativeAccessor_klass()) {
1463 return false;
1464 }
1465
1466 // Analyse the bytecodes.
1467 ResourceMark rm;
|