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  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation TestStringEqualsBadLength
  29  *
  30  */
  31 
  32 import java.util.Arrays;
  33 
  34 public class TestStringEqualsBadLength {
  35 
  36     int v1;
  37     int v2;
  38 
  39     boolean m(String s1) {
  40         int l = v2 - v1; // 0 - (-1) = 1. On 64 bit: 0xffffffff00000001
  41         char[] arr = new char[l];
  42         arr[0] = 'a';
  43         String s2 = new String(arr);
  44         // The string length is not reloaded but the value computed is
  45         // reused so pointer computation must not use
  46         // 0xffffffff00000001
  47         return s2.equals(s1);
  48     }
  49 
  50     // Same thing with String.compareTo()
  51     int m2(String s1) {
  52         int l = v2 - v1;
  53         char[] arr = new char[l+1];
  54         arr[0] = 'a';
  55         arr[1] = 'b';
  56         String s2 = new String(arr);
  57         return s2.compareTo(s1);
  58     }
  59 
  60     // Same thing with equals() for arrays
  61     boolean m3(char[] arr1) {
  62         int l = v2 - v1; // 0 - (-1) = 1. On 64 bit: 0xffffffff00000001
  63         char[] arr2 = new char[l];
  64         arr2[0] = 'a';
  65         return Arrays.equals(arr2, arr1);
  66     }
  67 
  68     static public void main(String[] args) {
  69         TestStringEqualsBadLength tse = new TestStringEqualsBadLength();
  70         tse.v1 = -1;
  71         tse.v2 = 0;
  72         char[] arr = new char[1];
  73         arr[0] = 'a';
  74         for (int i = 0; i < 20000; i++) {
  75             tse.m("a");
  76             tse.m2("ab");
  77             tse.m3(arr);
  78         }
  79 
  80         System.out.println("TEST PASSED");
  81     }
  82 }