From da17bb3675114f104c55c2360bd45d331bd6ea22 Mon Sep 17 00:00:00 2001 From: Nils Eliasson Date: Fri, 28 May 2021 20:37:47 +0200 Subject: [PATCH 1/3] Fix overflow in strconcat --- src/hotspot/share/opto/stringopts.cpp | 5 ++ .../stringopts/TestFetchStaticField.java | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 test/hotspot/jtreg/compiler/stringopts/TestFetchStaticField.java diff --git a/src/hotspot/share/opto/stringopts.cpp b/src/hotspot/share/opto/stringopts.cpp index d053b6253ff51..6392cd8ca025c 100644 --- a/src/hotspot/share/opto/stringopts.cpp +++ b/src/hotspot/share/opto/stringopts.cpp @@ -1151,6 +1151,11 @@ Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) { int arg_val = arg->get_int(); int count = 1; if (arg_val < 0) { + // Special case for min_jint - it can't be negated. + if (arg_val == min_jint) { + return __ intcon(11); + } + arg_val = -arg_val; count++; } diff --git a/test/hotspot/jtreg/compiler/stringopts/TestFetchStaticField.java b/test/hotspot/jtreg/compiler/stringopts/TestFetchStaticField.java new file mode 100644 index 0000000000000..e4c2553c33bb4 --- /dev/null +++ b/test/hotspot/jtreg/compiler/stringopts/TestFetchStaticField.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021, 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 8267773 + * @summary test that fetching a static field produces the correct result + * @library /test/lib / + * @run main compiler.stringopts.TestFetchStaticField + */ + +package compiler.stringopts; + +import java.lang.StringIndexOutOfBoundsException; +import jdk.test.lib.Asserts; + +public class TestFetchStaticField { + + public void runTest() { + System.out.println ("Test: StringIndexOutOfBoundsException(Integer.MIN_VALUE)"); + for (int i = 0; i < 100_000; i++ ) { + run1Test(i); + } + System.out.println("Test finished."); + } + + public static void main(String[] argv) + { + System.out.println("Running on:"); + System.out.println("os.arch: " + System.getProperty("os.arch")); + System.out.println("os.name: " + System.getProperty("os.name")); + System.out.println("java.runtime.version: " + System.getProperty("java.runtime.version")); + System.out.println("java.vm.name: " + System.getProperty("java.vm.name")); + System.out.println("java.vm.version: " + System.getProperty("java.vm.version")); + + TestFetchStaticField test = new TestFetchStaticField(); + test.runTest(); + } + + public void run1Test(int i) { + StringIndexOutOfBoundsException obj = new StringIndexOutOfBoundsException(Integer.MIN_VALUE); + if (obj == null) { + Asserts.fail("Failed: obj == null"); + } else { + if (!obj.toString().equals("java.lang.StringIndexOutOfBoundsException: String index out of range: -2147483648")) { + Asserts.fail("Failed on invocation " + i + ": obj.toString() = \"" + obj.toString() + "\", not \"java.lang.StringIndexOutOfBoundsException: String index out of range: -2147483648\""); + } + } + } +} From af28ac42b6871ff89dc8dbc0640690d5e1c824a3 Mon Sep 17 00:00:00 2001 From: Nils Eliasson Date: Mon, 31 May 2021 14:53:29 +0200 Subject: [PATCH 2/3] Add and fix test --- ...aticField.java => TestMinIntToString.java} | 0 .../lang/String/concat/IntegerMinValue.java | 60 +++++++++++++++++++ 2 files changed, 60 insertions(+) rename test/hotspot/jtreg/compiler/stringopts/{TestFetchStaticField.java => TestMinIntToString.java} (100%) create mode 100644 test/jdk/java/lang/String/concat/IntegerMinValue.java diff --git a/test/hotspot/jtreg/compiler/stringopts/TestFetchStaticField.java b/test/hotspot/jtreg/compiler/stringopts/TestMinIntToString.java similarity index 100% rename from test/hotspot/jtreg/compiler/stringopts/TestFetchStaticField.java rename to test/hotspot/jtreg/compiler/stringopts/TestMinIntToString.java diff --git a/test/jdk/java/lang/String/concat/IntegerMinValue.java b/test/jdk/java/lang/String/concat/IntegerMinValue.java new file mode 100644 index 0000000000000..6bc4c418c7d83 --- /dev/null +++ b/test/jdk/java/lang/String/concat/IntegerMinValue.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021, Red Hat, Inc. 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 8267773 + * @summary Test + * + * @compile IntegerMinValue.java + * @run main/othervm -Xverify:all -Xbatch IntegerMinValue + * + * @compile -XDstringConcat=inline IntegerMinValue.java + * @run main/othervm -Xverify:all -Xbatch IntegerMinValue + * + * @compile -XDstringConcat=indy IntegerMinValue.java + * @run main/othervm -Xverify:all -Xbatch IntegerMinValue + * + * @compile -XDstringConcat=indyWithConstants IntegerMinValue.java + * @run main/othervm -Xverify:all -Xbatch IntegerMinValue +*/ + +public class IntegerMinValue { + + public void test() { + int i = Integer.MIN_VALUE; + String s = "" + i; + if (!"-2147483648".equals(s)) { + throw new IllegalStateException("Failed: " + s); + } + System.out.println(s); + } + + public static void main(String[] strArr) { + IntegerMinValue t = new IntegerMinValue(); + for (int i = 0; i < 100_000; i++ ) { + t.test(); + } + } + +} From 43125189c8c1111dd6585857d76fdbafd2d7982c Mon Sep 17 00:00:00 2001 From: Nils Eliasson Date: Mon, 31 May 2021 14:55:54 +0200 Subject: [PATCH 3/3] Remove test --- .../stringopts/TestMinIntToString.java | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 test/hotspot/jtreg/compiler/stringopts/TestMinIntToString.java diff --git a/test/hotspot/jtreg/compiler/stringopts/TestMinIntToString.java b/test/hotspot/jtreg/compiler/stringopts/TestMinIntToString.java deleted file mode 100644 index e4c2553c33bb4..0000000000000 --- a/test/hotspot/jtreg/compiler/stringopts/TestMinIntToString.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2021, 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 8267773 - * @summary test that fetching a static field produces the correct result - * @library /test/lib / - * @run main compiler.stringopts.TestFetchStaticField - */ - -package compiler.stringopts; - -import java.lang.StringIndexOutOfBoundsException; -import jdk.test.lib.Asserts; - -public class TestFetchStaticField { - - public void runTest() { - System.out.println ("Test: StringIndexOutOfBoundsException(Integer.MIN_VALUE)"); - for (int i = 0; i < 100_000; i++ ) { - run1Test(i); - } - System.out.println("Test finished."); - } - - public static void main(String[] argv) - { - System.out.println("Running on:"); - System.out.println("os.arch: " + System.getProperty("os.arch")); - System.out.println("os.name: " + System.getProperty("os.name")); - System.out.println("java.runtime.version: " + System.getProperty("java.runtime.version")); - System.out.println("java.vm.name: " + System.getProperty("java.vm.name")); - System.out.println("java.vm.version: " + System.getProperty("java.vm.version")); - - TestFetchStaticField test = new TestFetchStaticField(); - test.runTest(); - } - - public void run1Test(int i) { - StringIndexOutOfBoundsException obj = new StringIndexOutOfBoundsException(Integer.MIN_VALUE); - if (obj == null) { - Asserts.fail("Failed: obj == null"); - } else { - if (!obj.toString().equals("java.lang.StringIndexOutOfBoundsException: String index out of range: -2147483648")) { - Asserts.fail("Failed on invocation " + i + ": obj.toString() = \"" + obj.toString() + "\", not \"java.lang.StringIndexOutOfBoundsException: String index out of range: -2147483648\""); - } - } - } -}