test/hotspot/jtreg/compiler/unsafe/UnsafeRaw.java
author iignatyev
Mon, 18 Feb 2019 09:31:07 -0800
changeset 53791 bec6c8739833
parent 47216 71c04702a3d5
permissions -rw-r--r--
8219157: vm/mlvm/mixed/stress/java/findDeadlock should be problem-listed only on mac Reviewed-by: thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     1
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40059
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     4
 *
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     8
 *
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    13
 * accompanied this code).
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    14
 *
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    18
 *
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    21
 * questions.
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    22
 */
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    23
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    24
/*
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    25
 * @test
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    26
 * @bug 8058744
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    27
 * @summary Invalid pattern-matching of address computations in raw unsafe
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40059
diff changeset
    28
 * @library /test/lib
36851
03e2f4d0a421 8153737: Unsupported Module
chegar
parents: 30604
diff changeset
    29
 * @modules java.base/jdk.internal.misc
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27453
diff changeset
    30
 *          java.management
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    31
 * @run main/othervm -Xbatch compiler.unsafe.UnsafeRaw
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    32
 */
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    33
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    34
package compiler.unsafe;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    35
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    36
import jdk.internal.misc.Unsafe;
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 29678
diff changeset
    37
import jdk.test.lib.Utils;
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    38
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    39
import java.util.Random;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    40
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    41
public class UnsafeRaw {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    42
  public static class Tests {
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    43
    public static int int_index(Unsafe unsafe, long base, int index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    44
      return unsafe.getInt(base + (index << 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    45
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    46
    public static int long_index(Unsafe unsafe, long base, long index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    47
      return unsafe.getInt(base + (index << 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    48
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    49
    public static int int_index_back_ashift(Unsafe unsafe, long base, int index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    50
      return unsafe.getInt(base + (index >> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    51
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    52
    public static int int_index_back_lshift(Unsafe unsafe, long base, int index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    53
      return unsafe.getInt(base + (index >>> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    54
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    55
    public static int long_index_back_ashift(Unsafe unsafe, long base, long index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    56
      return unsafe.getInt(base + (index >> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    57
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    58
    public static int long_index_back_lshift(Unsafe unsafe, long base, long index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    59
      return unsafe.getInt(base + (index >>> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    60
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    61
    public static int int_const_12345678_index(Unsafe unsafe, long base) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    62
      int idx4 = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    63
      return unsafe.getInt(base + idx4);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    64
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    65
    public static int long_const_1234567890abcdef_index(Unsafe unsafe, long base) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    66
      long idx5 = 0x1234567890abcdefL;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    67
      return unsafe.getInt(base + idx5);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    68
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    69
    public static int int_index_mul(Unsafe unsafe, long base, int index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    70
      return unsafe.getInt(base + (index * 4));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    71
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    72
    public static int long_index_mul(Unsafe unsafe, long base, long index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    73
      return unsafe.getInt(base + (index * 4));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    74
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    75
    public static int int_index_mul_scale_16(Unsafe unsafe, long base, int index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    76
      return unsafe.getInt(base + (index * 16));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    77
    }
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36851
diff changeset
    78
    public static int long_index_mul_scale_16(Unsafe unsafe, long base, long index) throws Exception {
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    79
      return unsafe.getInt(base + (index * 16));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    80
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    81
  }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    82
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    83
  public static void main(String[] args) throws Exception {
41092
c388d897472d 8165889: Remove jdk.test.lib.unsafe.UnsafeHelper
gtriantafill
parents: 40855
diff changeset
    84
    Unsafe unsafe = Unsafe.getUnsafe();
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    85
    final int array_size = 128;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    86
    final int element_size = 4;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    87
    final int magic = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    88
27453
9aeb9b97bef6 8044186: Introduce a reproducible random generator
iignatyev
parents: 26820
diff changeset
    89
    Random rnd = Utils.getRandomInstance();
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    90
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    91
    long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    92
    long addr = array + array_size * element_size / 2; // something in the middle to work with
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    93
    unsafe.putInt(addr, magic);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    94
    for (int j = 0; j < 100000; j++) {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    95
       if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    96
       if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    97
       if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    98
       if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    99
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   100
         long idx1 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   101
         long addr1 = addr - (idx1 << 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   102
         if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   103
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   104
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   105
         long idx2 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   106
         long addr2 = addr - (idx2 >> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   107
         if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   108
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   109
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   110
         long idx3 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   111
         long addr3 = addr - (idx3 >>> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   112
         if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   113
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   114
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   115
         long idx4 = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   116
         long addr4 = addr - idx4;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   117
         if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   118
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   119
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   120
         long idx5 = 0x1234567890abcdefL;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   121
         long addr5 = addr - idx5;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   122
         if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   123
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   124
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   125
         int idx6 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   126
         long addr6 = addr - (idx6 >> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   127
         if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   128
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   129
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   130
         int idx7 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   131
         long addr7 = addr - (idx7 >>> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   132
         if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   133
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   134
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   135
         int idx8 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   136
         long addr8 = addr - (idx8 * 16);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   137
         if (Tests.int_index_mul_scale_16(unsafe, addr8, idx8) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   138
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   139
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   140
         long idx9 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   141
         long addr9 = addr - (idx9 * 16);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   142
         if (Tests.long_index_mul_scale_16(unsafe, addr9, idx9) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   143
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   144
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   145
  }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   146
}