963 case Bytecodes::_athrow:
964 stack->pop(-Bytecodes::depth(code));
965 flow_ended = true;
966 break;
967
968 case Bytecodes::_getstatic:
969 case Bytecodes::_getfield: {
970 // Find out the type of the field accessed.
971 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
972 ConstantPool* cp = _method->constants();
973 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
974 int type_index = cp->signature_ref_index_at(name_and_type_index);
975 Symbol* signature = cp->symbol_at(type_index);
976 // Simulate the bytecode: pop the address, push the 'value' loaded
977 // from the field.
978 stack->pop(1 - Bytecodes::depth(code));
979 stack->push(bci, Signature::basic_type(signature));
980 break;
981 }
982
983 case Bytecodes::_putstatic:
984 case Bytecodes::_putfield: {
985 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
986 ConstantPool* cp = _method->constants();
987 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
988 int type_index = cp->signature_ref_index_at(name_and_type_index);
989 Symbol* signature = cp->symbol_at(type_index);
990 BasicType bt = Signature::basic_type(signature);
991 stack->pop(type2size[bt] - Bytecodes::depth(code) - 1);
992 break;
993 }
994
995 case Bytecodes::_invokevirtual:
996 case Bytecodes::_invokespecial:
997 case Bytecodes::_invokestatic:
998 case Bytecodes::_invokeinterface:
999 case Bytecodes::_invokedynamic: {
1000 ConstantPool* cp = _method->constants();
1001 int cp_index;
1002
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 BasicType bt = Signature::basic_type(signature);
1141 return type2size[bt];
1142 }
1143 case Bytecodes::_invokevirtual:
1144 case Bytecodes::_invokespecial:
1145 case Bytecodes::_invokeinterface: {
1146 int cp_index = Bytes::get_native_u2(code_base+ pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1147 ConstantPool* cp = _method->constants();
1148 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1149 int name_index = cp->name_ref_index_at(name_and_type_index);
1150 Symbol* name = cp->symbol_at(name_index);
1151
1152 // Assume the call of a constructor can never cause a NullPointerException
1153 // (which is true in Java). This is mainly used to avoid generating wrong
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,
1404 os->print("Cannot store to long array"); break;
1405 case Bytecodes::_dastore:
1406 os->print("Cannot store to double array"); break;
1407
1408 case Bytecodes::_arraylength:
1409 os->print("Cannot read the array length"); break;
1410 case Bytecodes::_athrow:
1411 os->print("Cannot throw exception"); break;
1412 case Bytecodes::_monitorenter:
1413 os->print("Cannot enter synchronized block"); break;
1414 case Bytecodes::_monitorexit:
1415 os->print("Cannot exit synchronized block"); break;
1416 case Bytecodes::_getfield: {
1417 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1418 ConstantPool* cp = _method->constants();
1419 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
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) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1426 os->print("Cannot assign field \"%s\"", get_field_name(_method, cp_index));
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) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1432 os->print("Cannot invoke \"");
1433 print_method_name(os, _method, cp_index);
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_NativeConstructorAccessorImpl_klass()) {
1451 return false;
1452 }
1453
1454 // Analyse the bytecodes.
1455 ResourceMark rm;
|
963 case Bytecodes::_athrow:
964 stack->pop(-Bytecodes::depth(code));
965 flow_ended = true;
966 break;
967
968 case Bytecodes::_getstatic:
969 case Bytecodes::_getfield: {
970 // Find out the type of the field accessed.
971 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
972 ConstantPool* cp = _method->constants();
973 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
974 int type_index = cp->signature_ref_index_at(name_and_type_index);
975 Symbol* signature = cp->symbol_at(type_index);
976 // Simulate the bytecode: pop the address, push the 'value' loaded
977 // from the field.
978 stack->pop(1 - Bytecodes::depth(code));
979 stack->push(bci, Signature::basic_type(signature));
980 break;
981 }
982
983 case Bytecodes::_withfield:
984 case Bytecodes::_putstatic:
985 case Bytecodes::_putfield: {
986 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
987 ConstantPool* cp = _method->constants();
988 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
989 int type_index = cp->signature_ref_index_at(name_and_type_index);
990 Symbol* signature = cp->symbol_at(type_index);
991 BasicType bt = Signature::basic_type(signature);
992 stack->pop(type2size[bt] - Bytecodes::depth(code) - 1);
993 break;
994 }
995
996 case Bytecodes::_invokevirtual:
997 case Bytecodes::_invokespecial:
998 case Bytecodes::_invokestatic:
999 case Bytecodes::_invokeinterface:
1000 case Bytecodes::_invokedynamic: {
1001 ConstantPool* cp = _method->constants();
1002 int cp_index;
1003
1095 }
1096
1097 #define INVALID_BYTECODE_ENCOUNTERED -1
1098 #define NPE_EXPLICIT_CONSTRUCTED -2
1099 int ExceptionMessageBuilder::get_NPE_null_slot(int bci) {
1100 // Get the bytecode.
1101 address code_base = _method->constMethod()->code_base();
1102 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1103 int pos = bci + 1; // Position of argument of the bytecode.
1104 if (code == Bytecodes::_wide) {
1105 code = Bytecodes::java_code_at(_method, code_base + bci + 1);
1106 pos += 1;
1107 }
1108
1109 switch (code) {
1110 case Bytecodes::_getfield:
1111 case Bytecodes::_arraylength:
1112 case Bytecodes::_athrow:
1113 case Bytecodes::_monitorenter:
1114 case Bytecodes::_monitorexit:
1115 case Bytecodes::_checkcast:
1116 return 0;
1117 case Bytecodes::_iaload:
1118 case Bytecodes::_faload:
1119 case Bytecodes::_aaload:
1120 case Bytecodes::_baload:
1121 case Bytecodes::_caload:
1122 case Bytecodes::_saload:
1123 case Bytecodes::_laload:
1124 case Bytecodes::_daload:
1125 return 1;
1126 case Bytecodes::_iastore:
1127 case Bytecodes::_fastore:
1128 case Bytecodes::_aastore:
1129 case Bytecodes::_bastore:
1130 case Bytecodes::_castore:
1131 case Bytecodes::_sastore:
1132 return 2;
1133 case Bytecodes::_lastore:
1134 case Bytecodes::_dastore:
1135 return 3;
1136 case Bytecodes::_withfield:
1137 case Bytecodes::_putfield: {
1138 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1139 ConstantPool* cp = _method->constants();
1140 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1141 int type_index = cp->signature_ref_index_at(name_and_type_index);
1142 Symbol* signature = cp->symbol_at(type_index);
1143 BasicType bt = Signature::basic_type(signature);
1144 return type2size[bt];
1145 }
1146 case Bytecodes::_invokevirtual:
1147 case Bytecodes::_invokespecial:
1148 case Bytecodes::_invokeinterface: {
1149 int cp_index = Bytes::get_native_u2(code_base+ pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1150 ConstantPool* cp = _method->constants();
1151 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1152 int name_index = cp->name_ref_index_at(name_and_type_index);
1153 Symbol* name = cp->symbol_at(name_index);
1154
1155 // Assume the call of a constructor can never cause a NullPointerException
1156 // (which is true in Java). This is mainly used to avoid generating wrong
1157 // messages for NullPointerExceptions created explicitly by new in Java code.
1158 if (name != vmSymbols::object_initializer_name()) {
1159 int type_index = cp->signature_ref_index_at(name_and_type_index);
1160 Symbol* signature = cp->symbol_at(type_index);
1161 // The 'this' parameter was null. Return the slot of it.
1162 return ArgumentSizeComputer(signature).size();
1163 } else {
1164 return NPE_EXPLICIT_CONSTRUCTED;
1165 }
1166 }
1167
1168 default:
1169 break;
1170 }
1171
1172 return INVALID_BYTECODE_ENCOUNTERED;
1173 }
1174
1175 bool ExceptionMessageBuilder::print_NPE_cause(outputStream* os, int bci, int slot) {
1176 if (print_NPE_cause0(os, bci, slot, _max_cause_detail, false, " because \"")) {
1177 address code_base = _method->constMethod()->code_base();
1178 Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1179 if (code == Bytecodes::_aastore) {
1180 os->print("\" is null or is a null-free array and there's an attempt to store null in it");
1181 } else {
1182 os->print("\" is null");
1183 }
1184 return true;
1185 }
1186 return false;
1187 }
1188
1189 // Recursively print what was null.
1190 //
1191 // Go to the bytecode that pushed slot 'slot' on the operand stack
1192 // at bytecode 'bci'. Compute a message for that bytecode. If
1193 // necessary (array, field), recur further.
1194 // At most do max_detail recursions.
1195 // Prefix is used to print a proper beginning of the whole
1196 // sentence.
1197 // inner_expr is used to omit some text, like 'static' in
1198 // inner expressions like array subscripts.
1199 //
1200 // Returns true if something was printed.
1201 //
1202 bool ExceptionMessageBuilder::print_NPE_cause0(outputStream* os, int bci, int slot,
1203 int max_detail,
1413 os->print("Cannot store to long array"); break;
1414 case Bytecodes::_dastore:
1415 os->print("Cannot store to double array"); break;
1416
1417 case Bytecodes::_arraylength:
1418 os->print("Cannot read the array length"); break;
1419 case Bytecodes::_athrow:
1420 os->print("Cannot throw exception"); break;
1421 case Bytecodes::_monitorenter:
1422 os->print("Cannot enter synchronized block"); break;
1423 case Bytecodes::_monitorexit:
1424 os->print("Cannot exit synchronized block"); break;
1425 case Bytecodes::_getfield: {
1426 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1427 ConstantPool* cp = _method->constants();
1428 int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1429 int name_index = cp->name_ref_index_at(name_and_type_index);
1430 Symbol* name = cp->symbol_at(name_index);
1431 os->print("Cannot read field \"%s\"", name->as_C_string());
1432 } break;
1433 case Bytecodes::_withfield:
1434 case Bytecodes::_putfield: {
1435 int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1436 os->print("Cannot assign field \"%s\"", get_field_name(_method, cp_index));
1437 } break;
1438 case Bytecodes::_invokevirtual:
1439 case Bytecodes::_invokespecial:
1440 case Bytecodes::_invokeinterface: {
1441 int cp_index = Bytes::get_native_u2(code_base+ pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1442 os->print("Cannot invoke \"");
1443 print_method_name(os, _method, cp_index);
1444 os->print("\"");
1445 } break;
1446 case Bytecodes::_checkcast: {
1447 int cp_index = Bytes::get_Java_u2(code_base + pos);
1448 ConstantPool* cp = _method->constants();
1449 os->print("Cannot cast to null-free type \"%s\"", cp->klass_at_noresolve(cp_index)->as_C_string());
1450 } break;
1451
1452 default:
1453 assert(0, "We should have checked this bytecode in get_NPE_null_slot().");
1454 break;
1455 }
1456 }
1457
1458 // Main API
1459 bool BytecodeUtils::get_NPE_message_at(outputStream* ss, Method* method, int bci) {
1460
1461 NoSafepointVerifier _nsv; // Cannot use this object over a safepoint.
1462
1463 // If this NPE was created via reflection, we have no real NPE.
1464 if (method->method_holder() ==
1465 vmClasses::reflect_NativeConstructorAccessorImpl_klass()) {
1466 return false;
1467 }
1468
1469 // Analyse the bytecodes.
1470 ResourceMark rm;
|