Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestStringEqualsBadLength.java
41153 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 802744526* @summary String.equals() may be called with a length whose upper bits are not cleared27*28* @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation29* compiler.intrinsics.string.TestStringEqualsBadLength30*/3132package compiler.intrinsics.string;3334import java.util.Arrays;3536public class TestStringEqualsBadLength {3738int v1;39int v2;4041boolean m(String s1) {42int l = v2 - v1; // 0 - (-1) = 1. On 64 bit: 0xffffffff0000000143char[] arr = new char[l];44arr[0] = 'a';45String s2 = new String(arr);46// The string length is not reloaded but the value computed is47// reused so pointer computation must not use48// 0xffffffff0000000149return s2.equals(s1);50}5152// Same thing with String.compareTo()53int m2(String s1) {54int l = v2 - v1;55char[] arr = new char[l+1];56arr[0] = 'a';57arr[1] = 'b';58String s2 = new String(arr);59return s2.compareTo(s1);60}6162// Same thing with equals() for arrays63boolean m3(char[] arr1) {64int l = v2 - v1; // 0 - (-1) = 1. On 64 bit: 0xffffffff0000000165char[] arr2 = new char[l];66arr2[0] = 'a';67return Arrays.equals(arr2, arr1);68}6970static public void main(String[] args) {71TestStringEqualsBadLength tse = new TestStringEqualsBadLength();72tse.v1 = -1;73tse.v2 = 0;74char[] arr = new char[1];75arr[0] = 'a';76for (int i = 0; i < 20000; i++) {77tse.m("a");78tse.m2("ab");79tse.m3(arr);80}8182System.out.println("TEST PASSED");83}84}858687