src/share/classes/sun/misc/Unsafe.java

Print this page




  64      *
  65      * <p> Here is a suggested idiom for using unsafe operations:
  66      *
  67      * <blockquote><pre>
  68      * class MyTrustedClass {
  69      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  70      *   ...
  71      *   private long myCountAddress = ...;
  72      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  73      * }
  74      * </pre></blockquote>
  75      *
  76      * (It may assist compilers to make the local variable be
  77      * <code>final</code>.)
  78      *
  79      * @exception  SecurityException  if a security manager exists and its
  80      *             <code>checkPropertiesAccess</code> method doesn't allow
  81      *             access to the system properties.
  82      */
  83     public static Unsafe getUnsafe() {
  84         Class cc = sun.reflect.Reflection.getCallerClass(2);
  85         if (cc.getClassLoader() != null)
  86             throw new SecurityException("Unsafe");
  87         return theUnsafe;
  88     }
  89 
  90     /// peek and poke operations
  91     /// (compilers should optimize these to memory ops)
  92 
  93     // These work on object fields in the Java heap.
  94     // They will not work on elements of packed arrays.
  95 
  96     /**
  97      * Fetches a value from a given Java variable.
  98      * More specifically, fetches a field or array element within the given
  99      * object <code>o</code> at the given offset, or (if <code>o</code> is
 100      * null) from the memory address whose numerical value is the given
 101      * offset.
 102      * <p>
 103      * The results are undefined unless one of the following cases is true:
 104      * <ul>


 599     /**
 600      * Returns the base address for accessing some static field
 601      * in the given class.  This method is implemented as follows:
 602      * <blockquote><pre>
 603      * public Object staticFieldBase(Class c) {
 604      *     Field[] fields = c.getDeclaredFields();
 605      *     for (int i = 0; i < fields.length; i++) {
 606      *         if (Modifier.isStatic(fields[i].getModifiers())) {
 607      *             return staticFieldBase(fields[i]);
 608      *         }
 609      *     }
 610      *     return null;
 611      * }
 612      * </pre></blockquote>
 613      * @deprecated As of 1.4.1, use {@link #staticFieldBase(Field)}
 614      * to obtain the base pertaining to a specific {@link Field}.
 615      * This method works only for JVMs which store all statics
 616      * for a given class in one place.
 617      */
 618     @Deprecated
 619     public Object staticFieldBase(Class c) {
 620         Field[] fields = c.getDeclaredFields();
 621         for (int i = 0; i < fields.length; i++) {
 622             if (Modifier.isStatic(fields[i].getModifiers())) {
 623                 return staticFieldBase(fields[i]);
 624             }
 625         }
 626         return null;
 627     }
 628 
 629     /**
 630      * Report the location of a given field in the storage allocation of its
 631      * class.  Do not expect to perform any sort of arithmetic on this offset;
 632      * it is just a cookie which is passed to the unsafe heap memory accessors.
 633      *
 634      * <p>Any given field will always have the same offset and base, and no
 635      * two distinct fields of the same class will ever have the same offset
 636      * and base.
 637      *
 638      * <p>As of 1.4.1, offsets for fields are represented as long values,
 639      * although the Sun JVM does not use the most significant 32 bits.


 665      */
 666     public native long objectFieldOffset(Field f);
 667 
 668     /**
 669      * Report the location of a given static field, in conjunction with {@link
 670      * #staticFieldOffset}.
 671      * <p>Fetch the base "Object", if any, with which static fields of the
 672      * given class can be accessed via methods like {@link #getInt(Object,
 673      * long)}.  This value may be null.  This value may refer to an object
 674      * which is a "cookie", not guaranteed to be a real Object, and it should
 675      * not be used in any way except as argument to the get and put routines in
 676      * this class.
 677      */
 678     public native Object staticFieldBase(Field f);
 679 
 680     /**
 681      * Ensure the given class has been initialized. This is often
 682      * needed in conjunction with obtaining the static field base of a
 683      * class.
 684      */
 685     public native void ensureClassInitialized(Class c);
 686 
 687     /**
 688      * Report the offset of the first element in the storage allocation of a
 689      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 690      * for the same class, you may use that scale factor, together with this
 691      * base offset, to form new offsets to access elements of arrays of the
 692      * given class.
 693      *
 694      * @see #getInt(Object, long)
 695      * @see #putInt(Object, long, int)
 696      */
 697     public native int arrayBaseOffset(Class arrayClass);
 698 
 699     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 700     public static final int ARRAY_BOOLEAN_BASE_OFFSET
 701             = theUnsafe.arrayBaseOffset(boolean[].class);
 702 
 703     /** The value of {@code arrayBaseOffset(byte[].class)} */
 704     public static final int ARRAY_BYTE_BASE_OFFSET
 705             = theUnsafe.arrayBaseOffset(byte[].class);
 706 
 707     /** The value of {@code arrayBaseOffset(short[].class)} */
 708     public static final int ARRAY_SHORT_BASE_OFFSET
 709             = theUnsafe.arrayBaseOffset(short[].class);
 710 
 711     /** The value of {@code arrayBaseOffset(char[].class)} */
 712     public static final int ARRAY_CHAR_BASE_OFFSET
 713             = theUnsafe.arrayBaseOffset(char[].class);
 714 
 715     /** The value of {@code arrayBaseOffset(int[].class)} */
 716     public static final int ARRAY_INT_BASE_OFFSET
 717             = theUnsafe.arrayBaseOffset(int[].class);


 726 
 727     /** The value of {@code arrayBaseOffset(double[].class)} */
 728     public static final int ARRAY_DOUBLE_BASE_OFFSET
 729             = theUnsafe.arrayBaseOffset(double[].class);
 730 
 731     /** The value of {@code arrayBaseOffset(Object[].class)} */
 732     public static final int ARRAY_OBJECT_BASE_OFFSET
 733             = theUnsafe.arrayBaseOffset(Object[].class);
 734 
 735     /**
 736      * Report the scale factor for addressing elements in the storage
 737      * allocation of a given array class.  However, arrays of "narrow" types
 738      * will generally not work properly with accessors like {@link
 739      * #getByte(Object, int)}, so the scale factor for such classes is reported
 740      * as zero.
 741      *
 742      * @see #arrayBaseOffset
 743      * @see #getInt(Object, long)
 744      * @see #putInt(Object, long, int)
 745      */
 746     public native int arrayIndexScale(Class arrayClass);
 747 
 748     /** The value of {@code arrayIndexScale(boolean[].class)} */
 749     public static final int ARRAY_BOOLEAN_INDEX_SCALE
 750             = theUnsafe.arrayIndexScale(boolean[].class);
 751 
 752     /** The value of {@code arrayIndexScale(byte[].class)} */
 753     public static final int ARRAY_BYTE_INDEX_SCALE
 754             = theUnsafe.arrayIndexScale(byte[].class);
 755 
 756     /** The value of {@code arrayIndexScale(short[].class)} */
 757     public static final int ARRAY_SHORT_INDEX_SCALE
 758             = theUnsafe.arrayIndexScale(short[].class);
 759 
 760     /** The value of {@code arrayIndexScale(char[].class)} */
 761     public static final int ARRAY_CHAR_INDEX_SCALE
 762             = theUnsafe.arrayIndexScale(char[].class);
 763 
 764     /** The value of {@code arrayIndexScale(int[].class)} */
 765     public static final int ARRAY_INT_INDEX_SCALE
 766             = theUnsafe.arrayIndexScale(int[].class);


 788      * fully by their information content.
 789      */
 790     public native int addressSize();
 791 
 792     /** The value of {@code addressSize()} */
 793     public static final int ADDRESS_SIZE = theUnsafe.addressSize();
 794 
 795     /**
 796      * Report the size in bytes of a native memory page (whatever that is).
 797      * This value will always be a power of two.
 798      */
 799     public native int pageSize();
 800 
 801 
 802     /// random trusted operations from JNI:
 803 
 804     /**
 805      * Tell the VM to define a class, without security checks.  By default, the
 806      * class loader and protection domain come from the caller's class.
 807      */
 808     public native Class defineClass(String name, byte[] b, int off, int len,
 809                                     ClassLoader loader,
 810                                     ProtectionDomain protectionDomain);
 811 
 812     public native Class defineClass(String name, byte[] b, int off, int len);
 813 
 814     /**
 815      * Define a class but do not make it known to the class loader or system dictionary.
 816      * <p>
 817      * For each CP entry, the corresponding CP patch must either be null or have
 818      * the a format that matches its tag:
 819      * <ul>
 820      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
 821      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
 822      * <li>Class: any java.lang.Class object
 823      * <li>String: any object (not just a java.lang.String)
 824      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
 825      * </ul>
 826      * @params hostClass context for linkage, access control, protection domain, and class loader
 827      * @params data      bytes of a class file
 828      * @params cpPatches where non-null entries exist, they replace corresponding CP entries in data
 829      */
 830     public native Class defineAnonymousClass(Class hostClass, byte[] data, Object[] cpPatches);
 831 
 832 
 833     /** Allocate an instance but do not run any constructor.
 834         Initializes the class if it has not yet been. */
 835     public native Object allocateInstance(Class cls)
 836         throws InstantiationException;
 837 
 838     /** Lock the object.  It must get unlocked via {@link #monitorExit}. */
 839     public native void monitorEnter(Object o);
 840 
 841     /**
 842      * Unlock the object.  It must have been locked via {@link
 843      * #monitorEnter}.
 844      */
 845     public native void monitorExit(Object o);
 846 
 847     /**
 848      * Tries to lock the object.  Returns true or false to indicate
 849      * whether the lock succeeded.  If it did, the object must be
 850      * unlocked via {@link #monitorExit}.
 851      */
 852     public native boolean tryMonitorEnter(Object o);
 853 
 854     /** Throw the exception without telling the verifier. */
 855     public native void throwException(Throwable ee);




  64      *
  65      * <p> Here is a suggested idiom for using unsafe operations:
  66      *
  67      * <blockquote><pre>
  68      * class MyTrustedClass {
  69      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  70      *   ...
  71      *   private long myCountAddress = ...;
  72      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  73      * }
  74      * </pre></blockquote>
  75      *
  76      * (It may assist compilers to make the local variable be
  77      * <code>final</code>.)
  78      *
  79      * @exception  SecurityException  if a security manager exists and its
  80      *             <code>checkPropertiesAccess</code> method doesn't allow
  81      *             access to the system properties.
  82      */
  83     public static Unsafe getUnsafe() {
  84         Class<?> cc = sun.reflect.Reflection.getCallerClass(2);
  85         if (cc.getClassLoader() != null)
  86             throw new SecurityException("Unsafe");
  87         return theUnsafe;
  88     }
  89 
  90     /// peek and poke operations
  91     /// (compilers should optimize these to memory ops)
  92 
  93     // These work on object fields in the Java heap.
  94     // They will not work on elements of packed arrays.
  95 
  96     /**
  97      * Fetches a value from a given Java variable.
  98      * More specifically, fetches a field or array element within the given
  99      * object <code>o</code> at the given offset, or (if <code>o</code> is
 100      * null) from the memory address whose numerical value is the given
 101      * offset.
 102      * <p>
 103      * The results are undefined unless one of the following cases is true:
 104      * <ul>


 599     /**
 600      * Returns the base address for accessing some static field
 601      * in the given class.  This method is implemented as follows:
 602      * <blockquote><pre>
 603      * public Object staticFieldBase(Class c) {
 604      *     Field[] fields = c.getDeclaredFields();
 605      *     for (int i = 0; i < fields.length; i++) {
 606      *         if (Modifier.isStatic(fields[i].getModifiers())) {
 607      *             return staticFieldBase(fields[i]);
 608      *         }
 609      *     }
 610      *     return null;
 611      * }
 612      * </pre></blockquote>
 613      * @deprecated As of 1.4.1, use {@link #staticFieldBase(Field)}
 614      * to obtain the base pertaining to a specific {@link Field}.
 615      * This method works only for JVMs which store all statics
 616      * for a given class in one place.
 617      */
 618     @Deprecated
 619     public Object staticFieldBase(Class<?> c) {
 620         Field[] fields = c.getDeclaredFields();
 621         for (int i = 0; i < fields.length; i++) {
 622             if (Modifier.isStatic(fields[i].getModifiers())) {
 623                 return staticFieldBase(fields[i]);
 624             }
 625         }
 626         return null;
 627     }
 628 
 629     /**
 630      * Report the location of a given field in the storage allocation of its
 631      * class.  Do not expect to perform any sort of arithmetic on this offset;
 632      * it is just a cookie which is passed to the unsafe heap memory accessors.
 633      *
 634      * <p>Any given field will always have the same offset and base, and no
 635      * two distinct fields of the same class will ever have the same offset
 636      * and base.
 637      *
 638      * <p>As of 1.4.1, offsets for fields are represented as long values,
 639      * although the Sun JVM does not use the most significant 32 bits.


 665      */
 666     public native long objectFieldOffset(Field f);
 667 
 668     /**
 669      * Report the location of a given static field, in conjunction with {@link
 670      * #staticFieldOffset}.
 671      * <p>Fetch the base "Object", if any, with which static fields of the
 672      * given class can be accessed via methods like {@link #getInt(Object,
 673      * long)}.  This value may be null.  This value may refer to an object
 674      * which is a "cookie", not guaranteed to be a real Object, and it should
 675      * not be used in any way except as argument to the get and put routines in
 676      * this class.
 677      */
 678     public native Object staticFieldBase(Field f);
 679 
 680     /**
 681      * Ensure the given class has been initialized. This is often
 682      * needed in conjunction with obtaining the static field base of a
 683      * class.
 684      */
 685     public native void ensureClassInitialized(Class<?> c);
 686 
 687     /**
 688      * Report the offset of the first element in the storage allocation of a
 689      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 690      * for the same class, you may use that scale factor, together with this
 691      * base offset, to form new offsets to access elements of arrays of the
 692      * given class.
 693      *
 694      * @see #getInt(Object, long)
 695      * @see #putInt(Object, long, int)
 696      */
 697     public native int arrayBaseOffset(Class<?> arrayClass);
 698 
 699     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 700     public static final int ARRAY_BOOLEAN_BASE_OFFSET
 701             = theUnsafe.arrayBaseOffset(boolean[].class);
 702 
 703     /** The value of {@code arrayBaseOffset(byte[].class)} */
 704     public static final int ARRAY_BYTE_BASE_OFFSET
 705             = theUnsafe.arrayBaseOffset(byte[].class);
 706 
 707     /** The value of {@code arrayBaseOffset(short[].class)} */
 708     public static final int ARRAY_SHORT_BASE_OFFSET
 709             = theUnsafe.arrayBaseOffset(short[].class);
 710 
 711     /** The value of {@code arrayBaseOffset(char[].class)} */
 712     public static final int ARRAY_CHAR_BASE_OFFSET
 713             = theUnsafe.arrayBaseOffset(char[].class);
 714 
 715     /** The value of {@code arrayBaseOffset(int[].class)} */
 716     public static final int ARRAY_INT_BASE_OFFSET
 717             = theUnsafe.arrayBaseOffset(int[].class);


 726 
 727     /** The value of {@code arrayBaseOffset(double[].class)} */
 728     public static final int ARRAY_DOUBLE_BASE_OFFSET
 729             = theUnsafe.arrayBaseOffset(double[].class);
 730 
 731     /** The value of {@code arrayBaseOffset(Object[].class)} */
 732     public static final int ARRAY_OBJECT_BASE_OFFSET
 733             = theUnsafe.arrayBaseOffset(Object[].class);
 734 
 735     /**
 736      * Report the scale factor for addressing elements in the storage
 737      * allocation of a given array class.  However, arrays of "narrow" types
 738      * will generally not work properly with accessors like {@link
 739      * #getByte(Object, int)}, so the scale factor for such classes is reported
 740      * as zero.
 741      *
 742      * @see #arrayBaseOffset
 743      * @see #getInt(Object, long)
 744      * @see #putInt(Object, long, int)
 745      */
 746     public native int arrayIndexScale(Class<?> arrayClass);
 747 
 748     /** The value of {@code arrayIndexScale(boolean[].class)} */
 749     public static final int ARRAY_BOOLEAN_INDEX_SCALE
 750             = theUnsafe.arrayIndexScale(boolean[].class);
 751 
 752     /** The value of {@code arrayIndexScale(byte[].class)} */
 753     public static final int ARRAY_BYTE_INDEX_SCALE
 754             = theUnsafe.arrayIndexScale(byte[].class);
 755 
 756     /** The value of {@code arrayIndexScale(short[].class)} */
 757     public static final int ARRAY_SHORT_INDEX_SCALE
 758             = theUnsafe.arrayIndexScale(short[].class);
 759 
 760     /** The value of {@code arrayIndexScale(char[].class)} */
 761     public static final int ARRAY_CHAR_INDEX_SCALE
 762             = theUnsafe.arrayIndexScale(char[].class);
 763 
 764     /** The value of {@code arrayIndexScale(int[].class)} */
 765     public static final int ARRAY_INT_INDEX_SCALE
 766             = theUnsafe.arrayIndexScale(int[].class);


 788      * fully by their information content.
 789      */
 790     public native int addressSize();
 791 
 792     /** The value of {@code addressSize()} */
 793     public static final int ADDRESS_SIZE = theUnsafe.addressSize();
 794 
 795     /**
 796      * Report the size in bytes of a native memory page (whatever that is).
 797      * This value will always be a power of two.
 798      */
 799     public native int pageSize();
 800 
 801 
 802     /// random trusted operations from JNI:
 803 
 804     /**
 805      * Tell the VM to define a class, without security checks.  By default, the
 806      * class loader and protection domain come from the caller's class.
 807      */
 808     public native Class<?> defineClass(String name, byte[] b, int off, int len,
 809                                        ClassLoader loader,
 810                                        ProtectionDomain protectionDomain);
 811 
 812     public native Class<?> defineClass(String name, byte[] b, int off, int len);
 813 
 814     /**
 815      * Define a class but do not make it known to the class loader or system dictionary.
 816      * <p>
 817      * For each CP entry, the corresponding CP patch must either be null or have
 818      * the a format that matches its tag:
 819      * <ul>
 820      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
 821      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
 822      * <li>Class: any java.lang.Class object
 823      * <li>String: any object (not just a java.lang.String)
 824      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
 825      * </ul>
 826      * @params hostClass context for linkage, access control, protection domain, and class loader
 827      * @params data      bytes of a class file
 828      * @params cpPatches where non-null entries exist, they replace corresponding CP entries in data
 829      */
 830     public native Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches);
 831 
 832 
 833     /** Allocate an instance but do not run any constructor.
 834         Initializes the class if it has not yet been. */
 835     public native Object allocateInstance(Class<?> cls)
 836         throws InstantiationException;
 837 
 838     /** Lock the object.  It must get unlocked via {@link #monitorExit}. */
 839     public native void monitorEnter(Object o);
 840 
 841     /**
 842      * Unlock the object.  It must have been locked via {@link
 843      * #monitorEnter}.
 844      */
 845     public native void monitorExit(Object o);
 846 
 847     /**
 848      * Tries to lock the object.  Returns true or false to indicate
 849      * whether the lock succeeded.  If it did, the object must be
 850      * unlocked via {@link #monitorExit}.
 851      */
 852     public native boolean tryMonitorEnter(Object o);
 853 
 854     /** Throw the exception without telling the verifier. */
 855     public native void throwException(Throwable ee);