changeset 48814:f01f81fa1242

8194246: JVM crashes when calling getStackTrace if stack contains a method that is a member of a very large class Summary: Use unsigned short to save method_id in stack trace. Reviewed-by: mchung, hseigel
author coleenp
date Mon, 29 Jan 2018 11:55:40 -0500
parents c092a2fbb7c3
children d80d521e9cb1
files src/hotspot/share/classfile/javaClasses.cpp test/hotspot/jtreg/runtime/StackTrace/LargeClassTest.java
diffstat 2 files changed, 151 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/classfile/javaClasses.cpp	Sun Jan 28 19:49:06 2018 -0500
+++ b/src/hotspot/share/classfile/javaClasses.cpp	Mon Jan 29 11:55:40 2018 -0500
@@ -1702,7 +1702,7 @@
       method = mhandle();
     }
 
-    _methods->short_at_put(_index, method->orig_method_idnum());
+    _methods->ushort_at_put(_index, method->orig_method_idnum());
     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
 
     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
@@ -1756,7 +1756,7 @@
 
   BacktraceElement next(Thread* thread) {
     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
-                        _methods->short_at(_index),
+                        _methods->ushort_at(_index),
                         Backtrace::version_at(_bcis->int_at(_index)),
                         Backtrace::bci_at(_bcis->int_at(_index)),
                         _names->symbol_at(_index));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/StackTrace/LargeClassTest.java	Mon Jan 29 11:55:40 2018 -0500
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8194246
+ * @summary JVM crashes on stack trace for large number of methods.
+ * @library /test/lib
+ * @modules java.base/jdk.internal.org.objectweb.asm
+ *          java.base/jdk.internal.misc
+ * @compile LargeClassTest.java
+ * @run main LargeClassTest
+ */
+
+import java.io.File;
+import java.io.FileOutputStream;
+import jdk.internal.org.objectweb.asm.ClassWriter;
+import jdk.internal.org.objectweb.asm.MethodVisitor;
+import jdk.internal.org.objectweb.asm.FieldVisitor;
+import jdk.internal.org.objectweb.asm.Label;
+import jdk.internal.org.objectweb.asm.AnnotationVisitor;
+import jdk.internal.org.objectweb.asm.Opcodes;
+import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.process.OutputAnalyzer;
+
+public class LargeClassTest implements Opcodes {
+    public static void main(String... args) throws Exception {
+        writeClassFile();
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-cp", ".",  "Large");
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        output.shouldHaveExitValue(0);
+    }
+
+    // Writes a Large class with > signed 16 bit int methods
+    public static void writeClassFile() throws Exception {
+
+        ClassWriter cw = new ClassWriter(0);
+        FieldVisitor fv;
+        MethodVisitor mv;
+        AnnotationVisitor av0;
+
+        cw.visit(55, ACC_PUBLIC + ACC_SUPER, "Large", null, "java/lang/Object", null);
+
+        {
+          mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
+          mv.visitCode();
+          mv.visitVarInsn(ALOAD, 0);
+          mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+          mv.visitInsn(RETURN);
+          mv.visitMaxs(1, 1);
+          mv.visitEnd();
+        }
+        {
+          // public static void main(String[] args) {
+          //     Large large = new Large();
+          //     large.f_1(55);
+          // }
+          mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
+          mv.visitCode();
+          mv.visitTypeInsn(NEW, "Large");
+          mv.visitInsn(DUP);
+          mv.visitMethodInsn(INVOKESPECIAL, "Large", "<init>", "()V", false);
+          mv.visitVarInsn(ASTORE, 1);
+          mv.visitVarInsn(ALOAD, 1);
+          mv.visitIntInsn(BIPUSH, 55);
+          mv.visitMethodInsn(INVOKEVIRTUAL, "Large", "f_1", "(I)I", false);
+          mv.visitInsn(POP);
+          mv.visitInsn(RETURN);
+          mv.visitMaxs(2, 2);
+          mv.visitEnd();
+        }
+
+        // Write 33560 methods called f_$i
+        for (int i = 1000; i < 34560; i++)
+        {
+          mv = cw.visitMethod(ACC_PUBLIC, "f_" + i, "()V", null, null);
+          mv.visitCode();
+          mv.visitInsn(RETURN);
+          mv.visitMaxs(0, 1);
+          mv.visitEnd();
+        }
+        {
+          // public int f_1(int prior) {
+          //   int total = prior + new java.util.Random(1).nextInt();
+          //   return total + f_2(total);
+          // }
+          mv = cw.visitMethod(ACC_PUBLIC, "f_1", "(I)I", null, null);
+          mv.visitCode();
+          mv.visitVarInsn(ILOAD, 1);
+          mv.visitTypeInsn(NEW, "java/util/Random");
+          mv.visitInsn(DUP);
+          mv.visitInsn(LCONST_1);
+          mv.visitMethodInsn(INVOKESPECIAL, "java/util/Random", "<init>", "(J)V", false);
+          mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Random", "nextInt", "()I", false);
+          mv.visitInsn(IADD);
+          mv.visitVarInsn(ISTORE, 2);
+          mv.visitVarInsn(ILOAD, 2);
+          mv.visitVarInsn(ALOAD, 0);
+          mv.visitVarInsn(ILOAD, 2);
+          mv.visitMethodInsn(INVOKEVIRTUAL, "Large", "f_2", "(I)I", false);
+          mv.visitInsn(IADD);
+          mv.visitInsn(IRETURN);
+          mv.visitMaxs(5, 3);
+          mv.visitEnd();
+        }
+        {
+          // public int f_2(int total) {
+          //   System.out.println(java.util.Arrays.toString(Thread.currentThread().getStackTrace()));
+          //   return 10;
+          // }
+          mv = cw.visitMethod(ACC_PUBLIC, "f_2", "(I)I", null, null);
+          mv.visitCode();
+          mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
+          mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;", false);
+          mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Thread", "getStackTrace", "()[Ljava/lang/StackTraceElement;", false);
+          mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "toString", "([Ljava/lang/Object;)Ljava/lang/String;", false);
+          mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
+          mv.visitIntInsn(BIPUSH, 10);
+          mv.visitInsn(IRETURN);
+          mv.visitMaxs(2, 2);
+          mv.visitEnd();
+        }
+        cw.visitEnd();
+
+        try (FileOutputStream fos = new FileOutputStream(new File("Large.class"))) {
+          fos.write(cw.toByteArray());
+        }
+    }
+}