--- old/test/hotspot/jtreg/runtime/exceptionMsgs/NullPointerException/NullPointerExceptionTest.java 2019-09-23 11:52:22.853261000 +0200 +++ new/test/hotspot/jtreg/runtime/exceptionMsgs/NullPointerException/NullPointerExceptionTest.java 2019-09-23 11:52:22.040263000 +0200 @@ -62,9 +62,14 @@ import static java.lang.invoke.MethodHandles.lookup; import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC; import static jdk.internal.org.objectweb.asm.Opcodes.ACC_SUPER; +import static jdk.internal.org.objectweb.asm.Opcodes.ACONST_NULL; import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD; +import static jdk.internal.org.objectweb.asm.Opcodes.ASTORE; import static jdk.internal.org.objectweb.asm.Opcodes.GETFIELD; +import static jdk.internal.org.objectweb.asm.Opcodes.GETSTATIC; +import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_1; import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESPECIAL; +import static jdk.internal.org.objectweb.asm.Opcodes.INVOKEVIRTUAL; import static jdk.internal.org.objectweb.asm.Opcodes.IRETURN; import static jdk.internal.org.objectweb.asm.Opcodes.RETURN; @@ -1423,6 +1428,16 @@ // public int throwNPE(F f) { // return f.i; // } + // public void throwNPE_reuseStackSlot1(String s1) { + // System.out.println(s1.substring(1)); + // String s1_2 = null; // Reuses slot 1. + // System.out.println(s1_2.substring(1)); + // } + // public void throwNPE_reuseStackSlot4(String s1, String s2, String s3, String s4) { + // System.out.println(s4.substring(1)); + // String s4_2 = null; // Reuses slot 4. + // System.out.println(s4_2.substring(1)); + // } // } static byte[] generateTestClass() { ClassWriter cw = new ClassWriter(0); @@ -1456,11 +1471,86 @@ mv.visitMaxs(1, 2); mv.visitEnd(); } + + { + mv = cw.visitMethod(ACC_PUBLIC, "throwNPE_reuseStackSlot1", "(Ljava/lang/String;)V", null, null); + mv.visitCode(); + Label label0 = new Label(); + mv.visitLabel(label0); + mv.visitLineNumber(7, label0); + mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); + mv.visitVarInsn(ALOAD, 1); + mv.visitInsn(ICONST_1); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); + Label label1 = new Label(); + mv.visitLabel(label1); + mv.visitLineNumber(8, label1); + mv.visitInsn(ACONST_NULL); + mv.visitVarInsn(ASTORE, 1); + Label label2 = new Label(); + mv.visitLabel(label2); + mv.visitLineNumber(9, label2); + mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); + mv.visitVarInsn(ALOAD, 1); + mv.visitInsn(ICONST_1); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); + Label label3 = new Label(); + mv.visitLabel(label3); + mv.visitLineNumber(10, label3); + mv.visitInsn(RETURN); + mv.visitMaxs(3, 3); + mv.visitEnd(); + } + + { + mv = cw.visitMethod(ACC_PUBLIC, "throwNPE_reuseStackSlot4", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", null, null); + mv.visitCode(); + Label label0 = new Label(); + mv.visitLabel(label0); + mv.visitLineNumber(12, label0); + mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); + mv.visitVarInsn(ALOAD, 4); + mv.visitInsn(ICONST_1); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); + Label label1 = new Label(); + mv.visitLabel(label1); + mv.visitLineNumber(13, label1); + mv.visitInsn(ACONST_NULL); + mv.visitVarInsn(ASTORE, 4); + Label label2 = new Label(); + mv.visitLabel(label2); + mv.visitLineNumber(14, label2); + mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); + mv.visitVarInsn(ALOAD, 4); + mv.visitInsn(ICONST_1); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;", false); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); + Label label3 = new Label(); + mv.visitLabel(label3); + mv.visitLineNumber(15, label3); + mv.visitInsn(RETURN); + mv.visitMaxs(3, 6); + mv.visitEnd(); + } + cw.visitEnd(); return cw.toByteArray(); } + // Assign to a parameter. + // Without debug information, this will print "parameter1" if a NPE + // is raised in the first line because null was passed to the method. + // It will print "local1" if a NPE is raised in line three. + public void assign_to_parameter(String s1) { + System.out.println(s1.substring(1)); + s1 = null; + System.out.println(s1.substring(2)); + } + // Tests that a class generated on the fly is handled properly. public void testGeneratedCode() throws Exception { byte[] classBytes = generateTestClass(); @@ -1474,12 +1564,79 @@ ex.getMessage(), "Cannot read field 'i' because 'f' is null."); } + + // Optimized bytecode can reuse local variable slots for several + // local variables. + // If there is no variable name information, we print 'parameteri' + // 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 'parameteri' any + // more, but 'locali'. Similary for 'this'. + + // Expect message saying "parameter0". + try { + e.throwNPE_reuseStackSlot1(null); + } catch (NullPointerException ex) { + checkMessage(ex, "s1.substring(1)", + ex.getMessage(), + "Cannot invoke 'String.substring(int)' because '' is null."); + } + // Expect message saying "local0". + try { + e.throwNPE_reuseStackSlot1("aa"); + } catch (NullPointerException ex) { + checkMessage(ex, "s1_2.substring(1)", + ex.getMessage(), + "Cannot invoke 'String.substring(int)' because '' is null."); + } + // Expect message saying "parameter4". + try { + e.throwNPE_reuseStackSlot4("aa", "bb", "cc", null); + } catch (NullPointerException ex) { + checkMessage(ex, "s4.substring(1)", + ex.getMessage(), + "Cannot invoke 'String.substring(int)' because '' is null."); + } + // Expect message saying "local4". + try { + e.throwNPE_reuseStackSlot4("aa", "bb", "cc", "dd"); + } catch (NullPointerException ex) { + checkMessage(ex, "s4_2.substring(1)", + ex.getMessage(), + "Cannot invoke 'String.substring(int)' because '' is null."); + } + + // Unfortunately, with the fix for optimized code as described above + // we don't write 'parameteri' any more after the parameter variable + // has been assigned. + + if (!hasDebugInfo) { + // Expect message saying "parameter1". + try { + assign_to_parameter(null); + } catch (NullPointerException ex) { + checkMessage(ex, "s1.substring(1)", + ex.getMessage(), + "Cannot invoke 'String.substring(int)' because '' is null."); + } + // The message says "local1" although "parameter1" would be correct. + try { + assign_to_parameter("aaa"); + } catch (NullPointerException ex) { + checkMessage(ex, "s1.substring(2)", + ex.getMessage(), + "Cannot invoke 'String.substring(int)' because '' is null."); + } + } } } // Helper interface for test cases needed for generateTestClass(). interface E0 { - public int throwNPE(F f); + public int throwNPE(F f); + public void throwNPE_reuseStackSlot1(String s1); + public void throwNPE_reuseStackSlot4(String s1, String s2, String s3, String s4); } // Helper class for test cases needed for generateTestClass().