< prev index next >

src/java.base/share/classes/java/lang/Runtime.java

Print this page
rev 63884 : Add cookie offset to Runtime::addressOf calculations


  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 
  27 package java.lang;
  28 
  29 import java.io.*;

  30 import java.math.BigInteger;

  31 import java.util.regex.Matcher;
  32 import java.util.regex.Pattern;
  33 import java.util.stream.Collectors;


  34 import java.util.List;

  35 import java.util.Optional;
  36 import java.util.StringTokenizer;
  37 

  38 import jdk.internal.access.SharedSecrets;
  39 import jdk.internal.loader.NativeLibrary;
  40 import jdk.internal.reflect.CallerSensitive;
  41 import jdk.internal.reflect.Reflection;


  42 
  43 /**
  44  * Every Java application has a single instance of class
  45  * {@code Runtime} that allows the application to interface with
  46  * the environment in which the application is running. The current
  47  * runtime can be obtained from the {@code getRuntime} method.
  48  * <p>
  49  * An application cannot create its own instance of this class.
  50  *
  51  * @author  unascribed
  52  * @see     java.lang.Runtime#getRuntime()
  53  * @since   1.0
  54  */
  55 
  56 public class Runtime {
  57     private static final Runtime currentRuntime = new Runtime();
  58 
  59     private static Version version;
  60 
  61     /**


 791      *             {@code null}
 792      * @see        java.lang.SecurityException
 793      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 794      */
 795     @CallerSensitive
 796     public void loadLibrary(String libname) {
 797         loadLibrary0(Reflection.getCallerClass(), libname);
 798     }
 799 
 800     void loadLibrary0(Class<?> fromClass, String libname) {
 801         SecurityManager security = System.getSecurityManager();
 802         if (security != null) {
 803             security.checkLink(libname);
 804         }
 805         if (libname.indexOf((int)File.separatorChar) != -1) {
 806             throw new UnsatisfiedLinkError(
 807                 "Directory separator should not appear in library name: " + libname);
 808         }
 809         ClassLoader.loadLibrary(fromClass, libname);
 810     }












































































































































































































































































































































 811 
 812     /**
 813      * Returns the version of the Java Runtime Environment as a {@link Version}.
 814      *
 815      * @return  the {@link Version} of the Java Runtime Environment
 816      *
 817      * @since  9
 818      */
 819     public static Version version() {
 820         if (version == null) {
 821             version = new Version(VersionProps.versionNumbers(),
 822                     VersionProps.pre(), VersionProps.build(),
 823                     VersionProps.optional());
 824         }
 825         return version;
 826     }
 827 
 828     /**
 829      * A representation of a version string for an implementation of the
 830      * Java&nbsp;SE Platform.  A version string consists of a version number




  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 
  27 package java.lang;
  28 
  29 import java.io.*;
  30 import java.lang.reflect.Field;
  31 import java.math.BigInteger;
  32 import java.util.function.ToLongFunction;
  33 import java.util.regex.Matcher;
  34 import java.util.regex.Pattern;
  35 import java.util.stream.Collectors;
  36 import java.util.Arrays;
  37 import java.util.ArrayDeque;
  38 import java.util.List;
  39 import java.util.Objects;
  40 import java.util.Optional;
  41 import java.util.StringTokenizer;
  42 
  43 import jdk.internal.HotSpotIntrinsicCandidate;
  44 import jdk.internal.access.SharedSecrets;
  45 import jdk.internal.loader.NativeLibrary;
  46 import jdk.internal.reflect.CallerSensitive;
  47 import jdk.internal.reflect.Reflection;
  48 import jdk.internal.vm.annotation.DontInline;
  49 import sun.security.util.SecurityConstants;
  50 
  51 /**
  52  * Every Java application has a single instance of class
  53  * {@code Runtime} that allows the application to interface with
  54  * the environment in which the application is running. The current
  55  * runtime can be obtained from the {@code getRuntime} method.
  56  * <p>
  57  * An application cannot create its own instance of this class.
  58  *
  59  * @author  unascribed
  60  * @see     java.lang.Runtime#getRuntime()
  61  * @since   1.0
  62  */
  63 
  64 public class Runtime {
  65     private static final Runtime currentRuntime = new Runtime();
  66 
  67     private static Version version;
  68 
  69     /**


 799      *             {@code null}
 800      * @see        java.lang.SecurityException
 801      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 802      */
 803     @CallerSensitive
 804     public void loadLibrary(String libname) {
 805         loadLibrary0(Reflection.getCallerClass(), libname);
 806     }
 807 
 808     void loadLibrary0(Class<?> fromClass, String libname) {
 809         SecurityManager security = System.getSecurityManager();
 810         if (security != null) {
 811             security.checkLink(libname);
 812         }
 813         if (libname.indexOf((int)File.separatorChar) != -1) {
 814             throw new UnsatisfiedLinkError(
 815                 "Directory separator should not appear in library name: " + libname);
 816         }
 817         ClassLoader.loadLibrary(fromClass, libname);
 818     }
 819 
 820     /**
 821      * Returns the implementation-specific estimate of the amount of storage
 822      * consumed by the specified object.
 823      * <p>
 824      * The estimate may change during a single invocation of the JVM. JVM may
 825      * answer the "don't know" value if it does not know the size of the object.
 826      * JVM may answer the "don't know" value if it refuses to provide the estimate.
 827      *
 828      * @param obj object to estimate the size of
 829      * @return storage size in bytes, or {@code -1} if storage size is unknown
 830      * @throws NullPointerException if {@code obj} is {@code null}
 831      * @since 16
 832      */
 833     @DontInline // Semantics: make sure the object is not scalar replaced.
 834     public static long sizeOf(Object obj) {
 835         Objects.requireNonNull(obj);
 836         return sizeOf0(obj);
 837     }
 838 
 839     @HotSpotIntrinsicCandidate
 840     private static native long sizeOf0(Object obj);
 841 
 842     /**
 843      * Bit value for {@link #deepSizeOf(Object, ToLongFunction)}'s callback
 844      * return value to continue traversal ("go deep") of the references of
 845      * the object passed to the callback.
 846      */
 847     public static final long DEEP_SIZE_OF_TRAVERSE = 1;
 848     /**
 849      * Bit value for {@link #deepSizeOf(Object, ToLongFunction)}'s callback
 850      * return value to consider the shallow size of the object passed to the
 851      * callback.
 852      */
 853     public static final long DEEP_SIZE_OF_SHALLOW = 2;
 854 
 855     /**
 856      * Returns the implementation-specific estimate of the amount of storage
 857      * consumed by the specified object and all objects referenced by it.
 858      * <p>
 859      * The estimate may change during a single invocation of the JVM. Notably,
 860      * the estimate is not guaranteed to remain stable if the object references in
 861      * the walked subgraph change when {@code deepSizeOf} is running.
 862      * <p>
 863      * JVM may answer the "don't know" value if it does not know the size of the
 864      * specified object or any of objects referenced from it. JVM may answer
 865      * "don't know" value if it refuses to provide the estimate.
 866      *
 867      * @param obj root object to start the estimate from
 868      * @return storage size in bytes, or {@code -1} if storage size is unknown
 869      * @throws NullPointerException if {@code obj} is {@code null}
 870      * @since 16
 871      */
 872     public static long deepSizeOf(Object obj) {
 873         return deepSizeOf(obj, (o) -> DEEP_SIZE_OF_TRAVERSE | DEEP_SIZE_OF_SHALLOW);
 874     }
 875 
 876     private static long handleIncludeCheck(ArrayDeque<Object> q, Object o, ToLongFunction<Object> ic, long ts, long os) {
 877         long t = ic.applyAsLong(o);
 878         if (t > 0) {
 879             if ((t & DEEP_SIZE_OF_TRAVERSE) != 0) {
 880                 q.push(o);
 881             }
 882             if ((t & DEEP_SIZE_OF_SHALLOW) != 0) {
 883                 ts += os;
 884             }
 885         } else {
 886             ts -= t;
 887         }
 888         return ts;
 889     }
 890 
 891     /**
 892      * Returns the implementation-specific estimate of the amount of storage
 893      * consumed by the specified object and all objects referenced by it.
 894      * <p>
 895      * The estimate may change during a single invocation of the JVM. Notably,
 896      * the estimate is not guaranteed to remain stable if the object references in
 897      * the walked subgraph change when {@code deepSizeOf} is running.
 898      * <p>
 899      * JVM may answer the "don't know" value if it does not know the size of the
 900      * specified object or any of objects referenced from it. JVM may answer
 901      * "don't know" value if it refuses to provide the estimate.
 902      *
 903      * @param obj root object to start the estimate from
 904      * @param includeCheck callback to evaluate an object's size. The callback can
 905      * return a positive value as a bitmask - valid values are
 906      * {@link #DEEP_SIZE_OF_SHALLOW} to consider the object's shallow sise and
 907      * {@link #DEEP_SIZE_OF_TRAVERSE} to traverse ("go deeper") the object's
 908      * references. A negative value means that the absolute return value is
 909      * considered and the object's references are not considered.
 910      * @return storage size in bytes, or {@code -1} if storage size is unknown
 911      * @throws NullPointerException if {@code obj} is {@code null}
 912      * @since 16
 913      */
 914     @DontInline // Semantics: make sure the object is not scalar replaced.
 915     public static long deepSizeOf(Object obj, ToLongFunction<Object> includeCheck) {
 916         Objects.requireNonNull(obj);
 917 
 918         // We are potentially leaking the objects to includeCheck callback.
 919         // These objects are peeled from the private fields as well, which
 920         // circumvents the normal Java access rules. Check we have the privilege.
 921         SecurityManager sm = System.getSecurityManager();
 922         if (sm != null) {
 923             sm.checkPermission(SecurityConstants.ACCESS_PERMISSION);
 924         }
 925 
 926         long rootSize = sizeOf0(obj);
 927         if (rootSize < 0) {
 928             return rootSize;
 929         }
 930 
 931         IdentityHashSet visited = new IdentityHashSet(IdentityHashSet.MINIMUM_CAPACITY);
 932         ArrayDeque<Object> q = new ArrayDeque<>();
 933 
 934         visited.add(obj);
 935         long totalSize = handleIncludeCheck(q, obj, includeCheck, 0, rootSize);
 936 
 937         Object[] refBuf = new Object[1];
 938 
 939         while (!q.isEmpty()) {
 940             Object o = q.pop();
 941             Class<?> cl = o.getClass();
 942             if (cl.isArray()) {
 943                 // Separate array path avoids adding a lot of (potentially large) array
 944                 // contents on the queue. No need to handle primitive arrays too.
 945 
 946                 if (cl.getComponentType().isPrimitive()) {
 947                     continue;
 948                 }
 949 
 950                 for (Object e : (Object[])o) {
 951                     if (e != null && visited.add(e)) {
 952                         long size = sizeOf0(e);
 953                         if (size < 0) {
 954                             return size;
 955                         }
 956                         totalSize = handleIncludeCheck(q, e, includeCheck, totalSize, size);
 957                     }
 958                 }
 959             } else {
 960                 int objs;
 961                 while ((objs = getReferencedObjects(o, refBuf)) < 0) {
 962                     refBuf = new Object[refBuf.length * 2];
 963                 }
 964 
 965                 for (int c = 0; c < objs; c++) {
 966                     Object e = refBuf[c];
 967                     if (visited.add(e)) {
 968                         long size = sizeOf0(e);
 969                         if (size < 0) {
 970                             return size;
 971                         }
 972                         totalSize = handleIncludeCheck(q, e, includeCheck, totalSize, size);
 973                     }
 974                 }
 975 
 976                 // Null out the buffer: do not keep these objects referenced until next
 977                 // buffer fill, and help the VM code to avoid full SATB barriers on existing
 978                 // buffer elements in getReferencedObjects.
 979                 Arrays.fill(refBuf, 0, objs, null);
 980             }
 981         }
 982 
 983         return totalSize;
 984     }
 985 
 986     /**
 987      * Peels the referenced objects from the given object and puts them
 988      * into the reference buffer. Never returns nulls in reference buffer.
 989      * Returns the number of valid elements in the buffer. If reference bufffer
 990      * is too small, returns -1.
 991      *
 992      * @param obj object to peel
 993      * @param refBuf reference buffer
 994      * @return number of valid elements in buffer, -1 if buffer is too small
 995      */
 996     @HotSpotIntrinsicCandidate
 997     private static native int getReferencedObjects(Object obj, Object[] refBuf);
 998 
 999     private static final class IdentityHashSet {
1000         private static final int MINIMUM_CAPACITY = 4;
1001         private static final int MAXIMUM_CAPACITY = 1 << 29;
1002 
1003         private Object[] table;
1004         private int size;
1005 
1006         public IdentityHashSet(int expectedMaxSize) {
1007             table = new Object[capacity(expectedMaxSize)];
1008         }
1009 
1010         private static int capacity(int expectedMaxSize) {
1011             return
1012                 (expectedMaxSize > MAXIMUM_CAPACITY / 3) ? MAXIMUM_CAPACITY :
1013                 (expectedMaxSize <= 2 * MINIMUM_CAPACITY / 3) ? MINIMUM_CAPACITY :
1014                 Integer.highestOneBit(expectedMaxSize + (expectedMaxSize << 1));
1015         }
1016 
1017         private static int nextIndex(int i, int len) {
1018             return (i + 1 < len ? i + 1 : 0);
1019         }
1020 
1021         public boolean add(Object o) {
1022             while (true) {
1023                 final Object[] tab = table;
1024                 final int len = tab.length;
1025                 int i = System.identityHashCode(o) & (len - 1);
1026 
1027                 for (Object item; (item = tab[i]) != null; i = nextIndex(i, len)) {
1028                     if (item == o) {
1029                         return false;
1030                     }
1031                 }
1032 
1033                 final int s = size + 1;
1034                 if (s*3 > len && resize()) continue;
1035 
1036                 tab[i] = o;
1037                 size = s;
1038                 return true;
1039             }
1040         }
1041 
1042         private boolean resize() {
1043             Object[] oldTable = table;
1044             int oldLength = oldTable.length;
1045             if (oldLength == MAXIMUM_CAPACITY) {
1046                 throw new IllegalStateException("Capacity exhausted.");
1047             }
1048 
1049             int newLength = oldLength * 2;
1050             if (newLength <= oldLength) {
1051                 return false;
1052             }
1053 
1054             Object[] newTable = new Object[newLength];
1055             for (Object o : oldTable) {
1056                 if (o != null) {
1057                     int i = System.identityHashCode(o) & (newLength - 1);
1058                     while (newTable[i] != null) {
1059                         i = nextIndex(i, newLength);
1060                     }
1061                     newTable[i] = o;
1062                 }
1063             }
1064             table = newTable;
1065             return true;
1066         }
1067     }
1068 
1069     /**
1070      * Returns the implementation-specific representation of the memory address
1071      * where the specified object resides.
1072      * <p>
1073      * The estimate may change during a single invocation of the JVM. Notably,
1074      * in the presence of moving garbage collector, the address can change at any
1075      * time, including during the call. As such, this method is only useful for
1076      * low-level debugging and heap introspection in a quiescent application.
1077      * <p>
1078      * The JVM is also free to provide non-verbatim answers, for example, adding
1079      * the random offset in order to hide the real object addresses. Because of this,
1080      * this method is useful to compare the relative Object positions, or inspecting
1081      * the object alignments up to some high threshold, but not for accessing the objects
1082      * via the naked native address.
1083      * <p>
1084      * JVM may answer the "don't know" value if it does not know the address of
1085      * the specified object, or refuses to provide this information.
1086      *
1087      * @param obj object to get the address of
1088      * @return current object address, or {@code -1} if address is unknown
1089      * @since 16
1090      */
1091     @DontInline // Semantics: make sure the object is not scalar replaced.
1092     public static long addressOf(Object obj) {
1093         Objects.requireNonNull(obj);
1094         return addressOf0(obj);
1095     }
1096 
1097     @HotSpotIntrinsicCandidate
1098     private static native long addressOf0(Object obj);
1099 
1100     /**
1101      * Returns the implementation-specific estimate of the offset of the field
1102      * within the holding container.
1103      * <p>
1104      * For the instance fields, the offset is from the beginning of the holder
1105      * object. For the static fields, the offset is from the beginning of the
1106      * unspecified holder area. As such, these offsets are useful for comparing
1107      * the offsets of two fields, not for any kind of absolute addressing.
1108      * <p>
1109      * The estimate may change during a single invocation of the JVM, for example
1110      * during class redefinition.
1111      * <p>
1112      * JVM may answer the "don't know" value if it does not know the address of
1113      * the specified object, or refuses to provide this information.
1114      *
1115      * @param field field to poll
1116      * @return the field offset in bytes, or {@code -1} if field offset is unknown
1117      * @throws NullPointerException if {@code field} is {@code null}
1118      * @since 16
1119      */
1120     public static long fieldOffsetOf(Field field) {
1121         Objects.requireNonNull(field);
1122         return fieldOffsetOf0(field);
1123     }
1124 
1125     // Reflection-like call, is not supposed to be fast?
1126     private static native long fieldOffsetOf0(Field field);
1127 
1128     /**
1129      * Returns the implementation-specific estimate of the field slot size for
1130      * the specified object field.
1131      * <p>
1132      * The estimate may change during a single invocation of the JVM.
1133      * <p>
1134      * JVM may answer the "don't know" value if it does not know the address of
1135      * the specified object, or refuses to provide this information.
1136      *
1137      * TODO: Split by staticness?
1138      *
1139      * @param field field to poll
1140      * @return the field size in bytes, or {@code -1} if field size is unknown
1141      * @throws NullPointerException if {@code field} is {@code null}
1142      * @since 16
1143      */
1144     public static long fieldSizeOf(Field field) {
1145         Objects.requireNonNull(field);
1146         return fieldSizeOf0(field);
1147     }
1148 
1149     // Reflection-like call, is not supposed to be fast?
1150     private static native long fieldSizeOf0(Field field);
1151 
1152     /**
1153      * Returns the version of the Java Runtime Environment as a {@link Version}.
1154      *
1155      * @return  the {@link Version} of the Java Runtime Environment
1156      *
1157      * @since  9
1158      */
1159     public static Version version() {
1160         if (version == null) {
1161             version = new Version(VersionProps.versionNumbers(),
1162                     VersionProps.pre(), VersionProps.build(),
1163                     VersionProps.optional());
1164         }
1165         return version;
1166     }
1167 
1168     /**
1169      * A representation of a version string for an implementation of the
1170      * Java&nbsp;SE Platform.  A version string consists of a version number


< prev index next >