13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.misc;
27
28 import jdk.internal.vm.annotation.ForceInline;
29 import jdk.internal.misc.VM;
30 import jdk.internal.reflect.CallerSensitive;
31 import jdk.internal.reflect.Reflection;
32
33 import java.lang.reflect.Field;
34 import java.util.Set;
35
36
37 /**
38 * A collection of methods for performing low-level, unsafe operations.
39 * Although the class and all methods are public, use of this class is
40 * limited because only trusted code can obtain instances of it.
41 *
42 * <em>Note:</em> It is the responsibility of the caller to make sure
43 * arguments are checked before methods of this class are
44 * called. While some rudimentary checks are performed on the input,
45 * the checks are best effort and when performance is an overriding
46 * priority, as when methods of this class are optimized by the
47 * runtime compiler, some or all checks (if any) may be elided. Hence,
48 * the caller must not rely on the checks and corresponding
49 * exceptions!
50 *
51 * @author John R. Rose
52 * @see #getUnsafe
619 /**
620 * Reports the location of a given field in the storage allocation of its
621 * class. Do not expect to perform any sort of arithmetic on this offset;
622 * it is just a cookie which is passed to the unsafe heap memory accessors.
623 *
624 * <p>Any given field will always have the same offset and base, and no
625 * two distinct fields of the same class will ever have the same offset
626 * and base.
627 *
628 * <p>As of 1.4.1, offsets for fields are represented as long values,
629 * although the Sun JVM does not use the most significant 32 bits.
630 * However, JVM implementations which store static fields at absolute
631 * addresses can use long offsets and null base pointers to express
632 * the field locations in a form usable by {@link #getInt(Object,long)}.
633 * Therefore, code which will be ported to such JVMs on 64-bit platforms
634 * must preserve all bits of static field offsets.
635 * @see #getInt(Object, long)
636 */
637 @ForceInline
638 public long objectFieldOffset(Field f) {
639 return theInternalUnsafe.objectFieldOffset(f);
640 }
641
642 /**
643 * Reports the location of a given static field, in conjunction with {@link
644 * #staticFieldBase}.
645 * <p>Do not expect to perform any sort of arithmetic on this offset;
646 * it is just a cookie which is passed to the unsafe heap memory accessors.
647 *
648 * <p>Any given field will always have the same offset, and no two distinct
649 * fields of the same class will ever have the same offset.
650 *
651 * <p>As of 1.4.1, offsets for fields are represented as long values,
652 * although the Sun JVM does not use the most significant 32 bits.
653 * It is hard to imagine a JVM technology which needs more than
654 * a few bits to encode an offset within a non-array object,
655 * However, for consistency with other methods in this class,
656 * this method reports its result as a long value.
657 * @see #getInt(Object, long)
658 */
659 @ForceInline
660 public long staticFieldOffset(Field f) {
661 return theInternalUnsafe.staticFieldOffset(f);
662 }
663
664 /**
665 * Reports the location of a given static field, in conjunction with {@link
666 * #staticFieldOffset}.
667 * <p>Fetch the base "Object", if any, with which static fields of the
668 * given class can be accessed via methods like {@link #getInt(Object,
669 * long)}. This value may be null. This value may refer to an object
670 * which is a "cookie", not guaranteed to be a real Object, and it should
671 * not be used in any way except as argument to the get and put routines in
672 * this class.
673 */
674 @ForceInline
675 public Object staticFieldBase(Field f) {
676 return theInternalUnsafe.staticFieldBase(f);
677 }
678
679 /**
680 * Detects if the given class may need to be initialized. This is often
681 * needed in conjunction with obtaining the static field base of a
682 * class.
683 * @return false only if a call to {@code ensureClassInitialized} would have no effect
684 */
685 @ForceInline
686 public boolean shouldBeInitialized(Class<?> c) {
687 return theInternalUnsafe.shouldBeInitialized(c);
688 }
689
690 /**
691 * Ensures the given class has been initialized. This is often
692 * needed in conjunction with obtaining the static field base of a
693 * class.
694 */
695 @ForceInline
803 @ForceInline
804 public int pageSize() {
805 return theInternalUnsafe.pageSize();
806 }
807
808
809 /// random trusted operations from JNI:
810
811 /**
812 * Defines a class but does not make it known to the class loader or system dictionary.
813 * <p>
814 * For each CP entry, the corresponding CP patch must either be null or have
815 * the a format that matches its tag:
816 * <ul>
817 * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
818 * <li>Utf8: a string (must have suitable syntax if used as signature or name)
819 * <li>Class: any java.lang.Class object
820 * <li>String: any object (not just a java.lang.String)
821 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
822 * </ul>
823 * @param hostClass context for linkage, access control, protection domain, and class loader
824 * @param data bytes of a class file
825 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
826 */
827 @ForceInline
828 public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
829 return theInternalUnsafe.defineAnonymousClass(hostClass, data, cpPatches);
830 }
831
832 /**
833 * Allocates an instance but does not run any constructor.
834 * Initializes the class if it has not yet been.
835 */
836 @ForceInline
837 public Object allocateInstance(Class<?> cls)
838 throws InstantiationException {
839 return theInternalUnsafe.allocateInstance(cls);
840 }
841
842 /** Throws the exception without telling the verifier. */
843 @ForceInline
844 public void throwException(Throwable ee) {
845 theInternalUnsafe.throwException(ee);
846 }
847
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.misc;
27
28 import jdk.internal.vm.annotation.ForceInline;
29 import jdk.internal.misc.VM;
30 import jdk.internal.reflect.CallerSensitive;
31 import jdk.internal.reflect.Reflection;
32
33 import java.lang.invoke.MethodHandles;
34 import java.lang.reflect.Field;
35 import java.util.Set;
36
37
38 /**
39 * A collection of methods for performing low-level, unsafe operations.
40 * Although the class and all methods are public, use of this class is
41 * limited because only trusted code can obtain instances of it.
42 *
43 * <em>Note:</em> It is the responsibility of the caller to make sure
44 * arguments are checked before methods of this class are
45 * called. While some rudimentary checks are performed on the input,
46 * the checks are best effort and when performance is an overriding
47 * priority, as when methods of this class are optimized by the
48 * runtime compiler, some or all checks (if any) may be elided. Hence,
49 * the caller must not rely on the checks and corresponding
50 * exceptions!
51 *
52 * @author John R. Rose
53 * @see #getUnsafe
620 /**
621 * Reports the location of a given field in the storage allocation of its
622 * class. Do not expect to perform any sort of arithmetic on this offset;
623 * it is just a cookie which is passed to the unsafe heap memory accessors.
624 *
625 * <p>Any given field will always have the same offset and base, and no
626 * two distinct fields of the same class will ever have the same offset
627 * and base.
628 *
629 * <p>As of 1.4.1, offsets for fields are represented as long values,
630 * although the Sun JVM does not use the most significant 32 bits.
631 * However, JVM implementations which store static fields at absolute
632 * addresses can use long offsets and null base pointers to express
633 * the field locations in a form usable by {@link #getInt(Object,long)}.
634 * Therefore, code which will be ported to such JVMs on 64-bit platforms
635 * must preserve all bits of static field offsets.
636 * @see #getInt(Object, long)
637 */
638 @ForceInline
639 public long objectFieldOffset(Field f) {
640 if (f == null) {
641 throw new NullPointerException();
642 }
643 if (f.getDeclaringClass().isHidden()) {
644 throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
645 }
646 return theInternalUnsafe.objectFieldOffset(f);
647 }
648
649 /**
650 * Reports the location of a given static field, in conjunction with {@link
651 * #staticFieldBase}.
652 * <p>Do not expect to perform any sort of arithmetic on this offset;
653 * it is just a cookie which is passed to the unsafe heap memory accessors.
654 *
655 * <p>Any given field will always have the same offset, and no two distinct
656 * fields of the same class will ever have the same offset.
657 *
658 * <p>As of 1.4.1, offsets for fields are represented as long values,
659 * although the Sun JVM does not use the most significant 32 bits.
660 * It is hard to imagine a JVM technology which needs more than
661 * a few bits to encode an offset within a non-array object,
662 * However, for consistency with other methods in this class,
663 * this method reports its result as a long value.
664 * @see #getInt(Object, long)
665 */
666 @ForceInline
667 public long staticFieldOffset(Field f) {
668 if (f == null) {
669 throw new NullPointerException();
670 }
671 if (f.getDeclaringClass().isHidden()) {
672 throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
673 }
674 return theInternalUnsafe.staticFieldOffset(f);
675 }
676
677 /**
678 * Reports the location of a given static field, in conjunction with {@link
679 * #staticFieldOffset}.
680 * <p>Fetch the base "Object", if any, with which static fields of the
681 * given class can be accessed via methods like {@link #getInt(Object,
682 * long)}. This value may be null. This value may refer to an object
683 * which is a "cookie", not guaranteed to be a real Object, and it should
684 * not be used in any way except as argument to the get and put routines in
685 * this class.
686 */
687 @ForceInline
688 public Object staticFieldBase(Field f) {
689 if (f == null) {
690 throw new NullPointerException();
691 }
692 if (f.getDeclaringClass().isHidden()) {
693 throw new UnsupportedOperationException("can't get base address on a hidden class: " + f);
694 }
695 return theInternalUnsafe.staticFieldBase(f);
696 }
697
698 /**
699 * Detects if the given class may need to be initialized. This is often
700 * needed in conjunction with obtaining the static field base of a
701 * class.
702 * @return false only if a call to {@code ensureClassInitialized} would have no effect
703 */
704 @ForceInline
705 public boolean shouldBeInitialized(Class<?> c) {
706 return theInternalUnsafe.shouldBeInitialized(c);
707 }
708
709 /**
710 * Ensures the given class has been initialized. This is often
711 * needed in conjunction with obtaining the static field base of a
712 * class.
713 */
714 @ForceInline
822 @ForceInline
823 public int pageSize() {
824 return theInternalUnsafe.pageSize();
825 }
826
827
828 /// random trusted operations from JNI:
829
830 /**
831 * Defines a class but does not make it known to the class loader or system dictionary.
832 * <p>
833 * For each CP entry, the corresponding CP patch must either be null or have
834 * the a format that matches its tag:
835 * <ul>
836 * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
837 * <li>Utf8: a string (must have suitable syntax if used as signature or name)
838 * <li>Class: any java.lang.Class object
839 * <li>String: any object (not just a java.lang.String)
840 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
841 * </ul>
842 *
843 * @deprecated Use the {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)}
844 * method.
845 *
846 * @param hostClass context for linkage, access control, protection domain, and class loader
847 * @param data bytes of a class file
848 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
849 */
850 @ForceInline
851 @Deprecated(since = "15", forRemoval = false)
852 public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
853 return theInternalUnsafe.defineAnonymousClass(hostClass, data, cpPatches);
854 }
855
856 /**
857 * Allocates an instance but does not run any constructor.
858 * Initializes the class if it has not yet been.
859 */
860 @ForceInline
861 public Object allocateInstance(Class<?> cls)
862 throws InstantiationException {
863 return theInternalUnsafe.allocateInstance(cls);
864 }
865
866 /** Throws the exception without telling the verifier. */
867 @ForceInline
868 public void throwException(Throwable ee) {
869 theInternalUnsafe.throwException(ee);
870 }
871
|