Thu, 22 May 2014 09:12:29 +0200
8027230: Overflow in java.lang.instrument.Instrumentation.getObjectSize() method
Reviewed-by: dholmes, sspitsyn
1.1 --- a/src/share/vm/prims/jvmtiEnv.cpp Wed May 21 11:25:25 2014 +0200 1.2 +++ b/src/share/vm/prims/jvmtiEnv.cpp Thu May 22 09:12:29 2014 +0200 1.3 @@ -307,9 +307,9 @@ 1.4 !java_lang_Class::is_primitive(mirror)) { 1.5 Klass* k = java_lang_Class::as_Klass(mirror); 1.6 assert(k != NULL, "class for non-primitive mirror must exist"); 1.7 - *size_ptr = k->size() * wordSize; 1.8 + *size_ptr = (jlong)k->size() * wordSize; 1.9 } else { 1.10 - *size_ptr = mirror->size() * wordSize; 1.11 + *size_ptr = (jlong)mirror->size() * wordSize; 1.12 } 1.13 return JVMTI_ERROR_NONE; 1.14 } /* end GetObjectSize */
2.1 --- a/test/TEST.groups Wed May 21 11:25:25 2014 +0200 2.2 +++ b/test/TEST.groups Thu May 22 09:12:29 2014 +0200 2.3 @@ -134,6 +134,8 @@ 2.4 gc/arguments/TestDynMaxHeapFreeRatio.java \ 2.5 runtime/InternalApi/ThreadCpuTimesDeadlock.java \ 2.6 serviceability/threads/TestFalseDeadLock.java \ 2.7 + serviceability/jvmti/GetObjectSizeOverflow.java \ 2.8 + serviceability/jvmti/TestRedefineWithUnresolvedClass.java \ 2.9 compiler/tiered/NonTieredLevelsTest.java \ 2.10 compiler/tiered/TieredLevelsTest.java \ 2.11 compiler/intrinsics/bmi/verifycode
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/serviceability/jvmti/GetObjectSizeOverflow.java Thu May 22 09:12:29 2014 +0200 3.3 @@ -0,0 +1,64 @@ 3.4 +/* 3.5 + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 +import java.io.PrintWriter; 3.27 +import com.oracle.java.testlibrary.*; 3.28 + 3.29 +/* 3.30 + * Test to verify GetObjectSize does not overflow on a 600M element int[] 3.31 + * 3.32 + * @test 3.33 + * @bug 8027230 3.34 + * @library /testlibrary 3.35 + * @build GetObjectSizeOverflowAgent 3.36 + * @run main ClassFileInstaller GetObjectSizeOverflowAgent 3.37 + * @run main GetObjectSizeOverflow 3.38 + */ 3.39 +public class GetObjectSizeOverflow { 3.40 + public static void main(String[] args) throws Exception { 3.41 + 3.42 + if (!Platform.is64bit()) { 3.43 + System.out.println("Test needs a 4GB heap and can only be run as a 64bit process, skipping."); 3.44 + return; 3.45 + } 3.46 + 3.47 + PrintWriter pw = new PrintWriter("MANIFEST.MF"); 3.48 + pw.println("Premain-Class: GetObjectSizeOverflowAgent"); 3.49 + pw.close(); 3.50 + 3.51 + ProcessBuilder pb = new ProcessBuilder(); 3.52 + pb.command(new String[] { JDKToolFinder.getJDKTool("jar"), "cmf", "MANIFEST.MF", "agent.jar", "GetObjectSizeOverflowAgent.class"}); 3.53 + pb.start().waitFor(); 3.54 + 3.55 + ProcessBuilder pt = ProcessTools.createJavaProcessBuilder(true, "-Xmx4000m", "-javaagent:agent.jar", "GetObjectSizeOverflowAgent"); 3.56 + OutputAnalyzer output = new OutputAnalyzer(pt.start()); 3.57 + 3.58 + if (output.getStdout().contains("Could not reserve enough space") || output.getStderr().contains("java.lang.OutOfMemoryError")) { 3.59 + System.out.println("stdout: " + output.getStdout()); 3.60 + System.out.println("stderr: " + output.getStderr()); 3.61 + System.out.println("Test could not reserve or allocate enough space, skipping"); 3.62 + return; 3.63 + } 3.64 + 3.65 + output.stdoutShouldContain("GetObjectSizeOverflow passed"); 3.66 + } 3.67 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/serviceability/jvmti/GetObjectSizeOverflowAgent.java Thu May 22 09:12:29 2014 +0200 4.3 @@ -0,0 +1,43 @@ 4.4 +/* 4.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 +import java.lang.instrument.*; 4.27 + 4.28 +public class GetObjectSizeOverflowAgent { 4.29 + 4.30 + static Instrumentation instrumentation; 4.31 + 4.32 + public static void premain(String agentArgs, Instrumentation instrumentation) { 4.33 + GetObjectSizeOverflowAgent.instrumentation = instrumentation; 4.34 + } 4.35 + 4.36 + public static void main(String[] args) throws Exception { 4.37 + int[] a = new int[600_000_000]; 4.38 + long size = instrumentation.getObjectSize(a); 4.39 + 4.40 + if (size < 2_400_000_000L) { 4.41 + throw new RuntimeException("Invalid size of array, expected >= 2400000000, got " + size); 4.42 + } 4.43 + 4.44 + System.out.println("GetObjectSizeOverflow passed"); 4.45 + } 4.46 +}