hotspot/test/compiler/unsafe/UnsafeRaw.java
author iveresov
Tue, 23 Sep 2014 17:24:34 -0700
changeset 26820 be0f5226f8cd
child 27453 9aeb9b97bef6
permissions -rw-r--r--
8059002: 8058744 needs a test case Summary: Added a test case the UnsafeRawOp intrinsics Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26820
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     1
/*
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    29
 * @run main/othervm -Xbatch UnsafeRaw
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    30
 */
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    31
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    32
import com.oracle.java.testlibrary.Utils;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    33
import java.util.Random;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    34
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    35
public class UnsafeRaw {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    36
  public static class Tests {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    37
    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
    38
      return unsafe.getInt(base + (index << 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    39
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    40
    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
    41
      return unsafe.getInt(base + (index << 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    42
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    43
    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
    44
      return unsafe.getInt(base + (index >> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    45
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    46
    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
    47
      return unsafe.getInt(base + (index >>> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    48
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    49
    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
    50
      return unsafe.getInt(base + (index >> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    51
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    52
    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
    53
      return unsafe.getInt(base + (index >>> 2));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    54
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    55
    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
    56
      int idx4 = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    57
      return unsafe.getInt(base + idx4);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    58
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    59
    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
    60
      long idx5 = 0x1234567890abcdefL;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    61
      return unsafe.getInt(base + idx5);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    62
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    63
    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
    64
      return unsafe.getInt(base + (index * 4));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    65
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    66
    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
    67
      return unsafe.getInt(base + (index * 4));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    68
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    69
    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
    70
      return unsafe.getInt(base + (index * 16));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    71
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    72
    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
    73
      return unsafe.getInt(base + (index * 16));
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    74
    }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    75
  }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    76
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    77
  public static void main(String[] args) throws Exception {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    78
    sun.misc.Unsafe unsafe = Utils.getUnsafe();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    79
    final int array_size = 128;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    80
    final int element_size = 4;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    81
    final int magic = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    82
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    83
    Random rnd = new Random();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    84
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    85
    long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    86
    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
    87
    unsafe.putInt(addr, magic);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    88
    for (int j = 0; j < 100000; j++) {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    89
       if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    90
       if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    91
       if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    92
       if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    93
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    94
         long idx1 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    95
         long addr1 = addr - (idx1 << 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    96
         if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    97
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    98
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
    99
         long idx2 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   100
         long addr2 = addr - (idx2 >> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   101
         if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   102
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   103
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   104
         long idx3 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   105
         long addr3 = addr - (idx3 >>> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   106
         if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   107
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   108
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   109
         long idx4 = 0x12345678;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   110
         long addr4 = addr - idx4;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   111
         if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   112
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   113
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   114
         long idx5 = 0x1234567890abcdefL;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   115
         long addr5 = addr - idx5;
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   116
         if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   117
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   118
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   119
         int idx6 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   120
         long addr6 = addr - (idx6 >> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   121
         if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   122
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   123
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   124
         int idx7 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   125
         long addr7 = addr - (idx7 >>> 2);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   126
         if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   127
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   128
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   129
         int idx8 = rnd.nextInt();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   130
         long addr8 = addr - (idx8 * 16);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   131
         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
   132
       }
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   133
       {
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   134
         long idx9 = rnd.nextLong();
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   135
         long addr9 = addr - (idx9 * 16);
be0f5226f8cd 8059002: 8058744 needs a test case
iveresov
parents:
diff changeset
   136
         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
   137
       }
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
}