1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8027445
  27  * @summary String.equals() may be called with a length whose upper bits are not cleared
  28  *
  29  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation
  30  *      compiler.intrinsics.string.TestStringEqualsBadLength
  31  */
  32 
  33 package compiler.intrinsics.string;
  34 
  35 import java.util.Arrays;
  36 
  37 public class TestStringEqualsBadLength {
  38 
  39     int v1;
  40     int v2;
  41 
  42     boolean m(String s1) {
  43         int l = v2 - v1; // 0 - (-1) = 1. On 64 bit: 0xffffffff00000001
  44         char[] arr = new char[l];
  45         arr[0] = 'a';
  46         String s2 = new String(arr);
  47         // The string length is not reloaded but the value computed is
  48         // reused so pointer computation must not use
  49         // 0xffffffff00000001
  50         return s2.equals(s1);
  51     }
  52 
  53     // Same thing with String.compareTo()
  54     int m2(String s1) {
  55         int l = v2 - v1;
  56         char[] arr = new char[l+1];
  57         arr[0] = 'a';
  58         arr[1] = 'b';
  59         String s2 = new String(arr);
  60         return s2.compareTo(s1);
  61     }
  62 
  63     // Same thing with equals() for arrays
  64     boolean m3(char[] arr1) {
  65         int l = v2 - v1; // 0 - (-1) = 1. On 64 bit: 0xffffffff00000001
  66         char[] arr2 = new char[l];
  67         arr2[0] = 'a';
  68         return Arrays.equals(arr2, arr1);
  69     }
  70 
  71     static public void main(String[] args) {
  72         TestStringEqualsBadLength tse = new TestStringEqualsBadLength();
  73         tse.v1 = -1;
  74         tse.v2 = 0;
  75         char[] arr = new char[1];
  76         arr[0] = 'a';
  77         for (int i = 0; i < 20000; i++) {
  78             tse.m("a");
  79             tse.m2("ab");
  80             tse.m3(arr);
  81         }
  82 
  83         System.out.println("TEST PASSED");
  84     }
  85 }