hotspot/test/compiler/unsafe/UnsafeRaw.java
author chegar
Sat, 09 Apr 2016 23:03:39 +0100
changeset 36851 03e2f4d0a421
parent 30604 b8d532cb6420
child 38152 80e5da81fb2c
permissions -rw-r--r--
8153737: Unsupported Module Reviewed-by: alanb, mchung, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     1
/*
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27453
diff changeset
     2
 * Copyright (c) 2014, 2015, 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
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    28
 * @library /testlibrary
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
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    31
 * @run main/othervm -Xbatch UnsafeRaw
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    32
 */
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    33
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 29678
diff changeset
    34
import jdk.test.lib.Utils;
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    35
import java.util.Random;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    36
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    37
public class UnsafeRaw {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    38
  public static class Tests {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    39
    public static int int_index(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    40
      return unsafe.getInt(base + (index << 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    41
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    42
    public static int long_index(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    43
      return unsafe.getInt(base + (index << 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    44
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    45
    public static int int_index_back_ashift(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    46
      return unsafe.getInt(base + (index >> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    47
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    48
    public static int int_index_back_lshift(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    49
      return unsafe.getInt(base + (index >>> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    50
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    51
    public static int long_index_back_ashift(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    52
      return unsafe.getInt(base + (index >> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    53
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    54
    public static int long_index_back_lshift(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    55
      return unsafe.getInt(base + (index >>> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    56
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    57
    public static int int_const_12345678_index(sun.misc.Unsafe unsafe, long base) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    58
      int idx4 = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    59
      return unsafe.getInt(base + idx4);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    60
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    61
    public static int long_const_1234567890abcdef_index(sun.misc.Unsafe unsafe, long base) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    62
      long idx5 = 0x1234567890abcdefL;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    63
      return unsafe.getInt(base + idx5);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    64
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    65
    public static int int_index_mul(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    66
      return unsafe.getInt(base + (index * 4));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    67
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    68
    public static int long_index_mul(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    69
      return unsafe.getInt(base + (index * 4));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    70
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    71
    public static int int_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, int index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    72
      return unsafe.getInt(base + (index * 16));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    73
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    74
    public static int long_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, long index) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    75
      return unsafe.getInt(base + (index * 16));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    76
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    77
  }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    78
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    79
  public static void main(String[] args) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    80
    sun.misc.Unsafe unsafe = Utils.getUnsafe();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    81
    final int array_size = 128;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    82
    final int element_size = 4;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    83
    final int magic = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    84
27453
9aeb9b97bef6 8044186: Introduce a reproducible random generator
iignatyev
parents: 26820
diff changeset
    85
    Random rnd = Utils.getRandomInstance();
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    86
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    87
    long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    88
    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
    89
    unsafe.putInt(addr, magic);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    90
    for (int j = 0; j < 100000; j++) {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    91
       if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    92
       if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    93
       if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    94
       if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    95
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    96
         long idx1 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    97
         long addr1 = addr - (idx1 << 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    98
         if (Tests.long_index(unsafe, addr1, idx1) != 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
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   101
         long idx2 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   102
         long addr2 = addr - (idx2 >> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   103
         if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   104
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   105
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   106
         long idx3 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   107
         long addr3 = addr - (idx3 >>> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   108
         if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   109
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   110
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   111
         long idx4 = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   112
         long addr4 = addr - idx4;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   113
         if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   114
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   115
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   116
         long idx5 = 0x1234567890abcdefL;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   117
         long addr5 = addr - idx5;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   118
         if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   119
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   120
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   121
         int idx6 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   122
         long addr6 = addr - (idx6 >> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   123
         if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   124
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   125
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   126
         int idx7 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   127
         long addr7 = addr - (idx7 >>> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   128
         if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   129
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   130
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   131
         int idx8 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   132
         long addr8 = addr - (idx8 * 16);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   133
         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
   134
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   135
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   136
         long idx9 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   137
         long addr9 = addr - (idx9 * 16);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   138
         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
   139
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   140
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   141
  }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   142
}