Mercurial > hg > truffle
annotate test/compiler/intrinsics/unsafe/UnsafeGetAddressTest.java @ 19069:0499dbe2da01
factored out common code
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Mon, 02 Feb 2015 21:40:08 +0100 |
parents | 9ab9f254cfe2 |
children |
rev | line source |
---|---|
17776
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
1 /* |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
4 * |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
8 * |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
13 * accompanied this code). |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
14 * |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
18 * |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
21 * questions. |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
22 */ |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
23 |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
24 /* |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
25 * @test |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
26 * @bug 6653795 |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
27 * @summary C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
28 * @run main UnsafeGetAddressTest |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
29 * |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
30 */ |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
31 |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
32 import sun.misc.Unsafe; |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
33 import java.lang.reflect.*; |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
34 |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
35 public class UnsafeGetAddressTest { |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
36 private static Unsafe unsafe; |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
37 |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
38 public static void main(String[] args) throws Exception { |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
39 Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("sun.misc.Unsafe"); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
40 Field f = c.getDeclaredField("theUnsafe"); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
41 f.setAccessible(true); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
42 unsafe = (Unsafe)f.get(c); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
43 |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
44 long address = unsafe.allocateMemory(unsafe.addressSize()); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
45 unsafe.putAddress(address, 0x0000000080000000L); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
46 // from sun.misc.Unsafe.getAddress' documentation: |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
47 // "If the native pointer is less than 64 bits wide, it is |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
48 // extended as an unsigned number to a Java long." |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
49 result = unsafe.getAddress(address); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
50 System.out.printf("1: was 0x%x, expected 0x%x\n", result, |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
51 0x0000000080000000L); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
52 for (int i = 0; i < 1000000; i++) { |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
53 result = unsafe.getAddress(address); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
54 } |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
55 |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
56 // The code has got compiled, check the result now |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
57 System.out.printf("2: was 0x%x, expected 0x%x\n", result, |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
58 0x0000000080000000L); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
59 if (result != 0x0000000080000000L) { |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
60 System.out.println("Test Failed"); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
61 System.exit(97); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
62 } else { |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
63 System.out.println("Test Passed"); |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
64 } |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
65 } |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
66 static volatile long result; |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
67 } |
9ab9f254cfe2
6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
poonam
parents:
diff
changeset
|
68 |