6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems

Mon, 24 Mar 2014 08:43:10 -0700

author
poonam
date
Mon, 24 Mar 2014 08:43:10 -0700
changeset 6425
9ab9f254cfe2
parent 6421
7d175751ef7f
child 6426
460f312abe11

6653795: C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
Summary: Native pointers less than 64 bits wide should be extended as an unsigned number.
Reviewed-by: kvn, kevinw

src/share/vm/opto/graphKit.cpp file | annotate | diff | comparison | revisions
src/share/vm/opto/graphKit.hpp file | annotate | diff | comparison | revisions
src/share/vm/opto/library_call.cpp file | annotate | diff | comparison | revisions
src/share/vm/opto/type.hpp file | annotate | diff | comparison | revisions
test/compiler/intrinsics/unsafe/UnsafeGetAddressTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/vm/opto/graphKit.cpp	Mon Mar 24 15:34:10 2014 -0700
     1.2 +++ b/src/share/vm/opto/graphKit.cpp	Mon Mar 24 08:43:10 2014 -0700
     1.3 @@ -1124,6 +1124,17 @@
     1.4    }
     1.5    return _gvn.transform( new (C) ConvI2LNode(offset));
     1.6  }
     1.7 +
     1.8 +Node* GraphKit::ConvI2UL(Node* offset) {
     1.9 +  juint offset_con = (juint) find_int_con(offset, Type::OffsetBot);
    1.10 +  if (offset_con != (juint) Type::OffsetBot) {
    1.11 +    return longcon((julong) offset_con);
    1.12 +  }
    1.13 +  Node* conv = _gvn.transform( new (C) ConvI2LNode(offset));
    1.14 +  Node* mask = _gvn.transform( ConLNode::make(C, (julong) max_juint) );
    1.15 +  return _gvn.transform( new (C) AndLNode(conv, mask) );
    1.16 +}
    1.17 +
    1.18  Node* GraphKit::ConvL2I(Node* offset) {
    1.19    // short-circuit a common case
    1.20    jlong offset_con = find_long_con(offset, (jlong)Type::OffsetBot);
     2.1 --- a/src/share/vm/opto/graphKit.hpp	Mon Mar 24 15:34:10 2014 -0700
     2.2 +++ b/src/share/vm/opto/graphKit.hpp	Mon Mar 24 08:43:10 2014 -0700
     2.3 @@ -338,6 +338,7 @@
     2.4    // Convert between int and long, and size_t.
     2.5    // (See macros ConvI2X, etc., in type.hpp for ConvI2X, etc.)
     2.6    Node* ConvI2L(Node* offset);
     2.7 +  Node* ConvI2UL(Node* offset);
     2.8    Node* ConvL2I(Node* offset);
     2.9    // Find out the klass of an object.
    2.10    Node* load_object_klass(Node* object);
     3.1 --- a/src/share/vm/opto/library_call.cpp	Mon Mar 24 15:34:10 2014 -0700
     3.2 +++ b/src/share/vm/opto/library_call.cpp	Mon Mar 24 08:43:10 2014 -0700
     3.3 @@ -2595,7 +2595,7 @@
     3.4      case T_ADDRESS:
     3.5        // Cast to an int type.
     3.6        p = _gvn.transform(new (C) CastP2XNode(NULL, p));
     3.7 -      p = ConvX2L(p);
     3.8 +      p = ConvX2UL(p);
     3.9        break;
    3.10      default:
    3.11        fatal(err_msg_res("unexpected type %d: %s", type, type2name(type)));
     4.1 --- a/src/share/vm/opto/type.hpp	Mon Mar 24 15:34:10 2014 -0700
     4.2 +++ b/src/share/vm/opto/type.hpp	Mon Mar 24 08:43:10 2014 -0700
     4.3 @@ -1713,6 +1713,7 @@
     4.4  #define ConvL2X(x)   (x)
     4.5  #define ConvX2I(x)   ConvL2I(x)
     4.6  #define ConvX2L(x)   (x)
     4.7 +#define ConvX2UL(x)  (x)
     4.8  
     4.9  #else
    4.10  
    4.11 @@ -1757,6 +1758,7 @@
    4.12  #define ConvL2X(x)   ConvL2I(x)
    4.13  #define ConvX2I(x)   (x)
    4.14  #define ConvX2L(x)   ConvI2L(x)
    4.15 +#define ConvX2UL(x)  ConvI2UL(x)
    4.16  
    4.17  #endif
    4.18  
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/compiler/intrinsics/unsafe/UnsafeGetAddressTest.java	Mon Mar 24 08:43:10 2014 -0700
     5.3 @@ -0,0 +1,68 @@
     5.4 +/*
     5.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.7 + *
     5.8 + * This code is free software; you can redistribute it and/or modify it
     5.9 + * under the terms of the GNU General Public License version 2 only, as
    5.10 + * published by the Free Software Foundation.
    5.11 + *
    5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.15 + * version 2 for more details (a copy is included in the LICENSE file that
    5.16 + * accompanied this code).
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License version
    5.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    5.21 + *
    5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    5.23 + * or visit www.oracle.com if you need additional information or have any
    5.24 + * questions.
    5.25 + */
    5.26 +
    5.27 +/*
    5.28 + * @test
    5.29 + * @bug 6653795
    5.30 + * @summary C2 intrinsic for Unsafe.getAddress performs pointer sign extension on 32-bit systems
    5.31 + * @run main UnsafeGetAddressTest
    5.32 + *
    5.33 + */
    5.34 +
    5.35 +import sun.misc.Unsafe;
    5.36 +import java.lang.reflect.*;
    5.37 +
    5.38 +public class UnsafeGetAddressTest {
    5.39 +    private static Unsafe unsafe;
    5.40 +
    5.41 +    public static void main(String[] args) throws Exception {
    5.42 +        Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("sun.misc.Unsafe");
    5.43 +        Field f = c.getDeclaredField("theUnsafe");
    5.44 +        f.setAccessible(true);
    5.45 +        unsafe = (Unsafe)f.get(c);
    5.46 +
    5.47 +        long address = unsafe.allocateMemory(unsafe.addressSize());
    5.48 +        unsafe.putAddress(address, 0x0000000080000000L);
    5.49 +        // from sun.misc.Unsafe.getAddress' documentation:
    5.50 +        // "If the native pointer is less than 64 bits wide, it is
    5.51 +        // extended as an unsigned number to a Java long."
    5.52 +        result = unsafe.getAddress(address);
    5.53 +        System.out.printf("1: was 0x%x, expected 0x%x\n", result,
    5.54 +                0x0000000080000000L);
    5.55 +        for (int i = 0; i < 1000000; i++) {
    5.56 +            result = unsafe.getAddress(address);
    5.57 +        }
    5.58 +
    5.59 +        // The code has got compiled, check the result now
    5.60 +        System.out.printf("2: was 0x%x, expected 0x%x\n", result,
    5.61 +                0x0000000080000000L);
    5.62 +        if (result != 0x0000000080000000L) {
    5.63 +            System.out.println("Test Failed");
    5.64 +            System.exit(97);
    5.65 +        } else {
    5.66 +            System.out.println("Test Passed");
    5.67 +        }
    5.68 +    }
    5.69 +    static volatile long result;
    5.70 +}
    5.71 +

mercurial