11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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 jdk.internal.misc;
27
28 import java.lang.reflect.Field;
29 import java.security.ProtectionDomain;
30
31 import sun.reflect.CallerSensitive;
32 import sun.reflect.Reflection;
33 import jdk.internal.misc.VM;
34
35 import jdk.internal.HotSpotIntrinsicCandidate;
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 resposibility 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!
1140 * <li>Class: any java.lang.Class object
1141 * <li>String: any object (not just a java.lang.String)
1142 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
1143 * </ul>
1144 * @param hostClass context for linkage, access control, protection domain, and class loader
1145 * @param data bytes of a class file
1146 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
1147 */
1148 public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
1149 if (hostClass == null || data == null) {
1150 throw new NullPointerException();
1151 }
1152
1153 return defineAnonymousClass0(hostClass, data, cpPatches);
1154 }
1155
1156 /**
1157 * Allocates an instance but does not run any constructor.
1158 * Initializes the class if it has not yet been.
1159 */
1160 @HotSpotIntrinsicCandidate
1161 public native Object allocateInstance(Class<?> cls)
1162 throws InstantiationException;
1163
1164 /**
1165 * Allocates an array of a given type, but does not do zeroing.
1166 * <p>
1167 * This method should only be used in the very rare cases where a high-performance code
1168 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1169 * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1170 * <p>
1171 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1172 * before allowing untrusted code, or code in other threads, to observe the reference
1173 * to the newly allocated array. In addition, the publication of the array reference must be
1174 * safe according to the Java Memory Model requirements.
1175 * <p>
1176 * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1177 * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1178 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1179 * or issuing a {@link #storeFence} before publishing the reference.
1180 * <p>
1181 * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1182 * elements that could break heap integrity.
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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 jdk.internal.misc;
27
28 import java.lang.reflect.Field;
29 import java.security.ProtectionDomain;
30
31 import jdk.internal.vm.annotation.ForceInline;
32 import sun.reflect.CallerSensitive;
33 import sun.reflect.Reflection;
34 import jdk.internal.misc.VM;
35
36 import jdk.internal.HotSpotIntrinsicCandidate;
37
38
39 /**
40 * A collection of methods for performing low-level, unsafe operations.
41 * Although the class and all methods are public, use of this class is
42 * limited because only trusted code can obtain instances of it.
43 *
44 * <em>Note:</em> It is the resposibility of the caller to make sure
45 * arguments are checked before methods of this class are
46 * called. While some rudimentary checks are performed on the input,
47 * the checks are best effort and when performance is an overriding
48 * priority, as when methods of this class are optimized by the
49 * runtime compiler, some or all checks (if any) may be elided. Hence,
50 * the caller must not rely on the checks and corresponding
51 * exceptions!
1141 * <li>Class: any java.lang.Class object
1142 * <li>String: any object (not just a java.lang.String)
1143 * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
1144 * </ul>
1145 * @param hostClass context for linkage, access control, protection domain, and class loader
1146 * @param data bytes of a class file
1147 * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
1148 */
1149 public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
1150 if (hostClass == null || data == null) {
1151 throw new NullPointerException();
1152 }
1153
1154 return defineAnonymousClass0(hostClass, data, cpPatches);
1155 }
1156
1157 /**
1158 * Allocates an instance but does not run any constructor.
1159 * Initializes the class if it has not yet been.
1160 */
1161 @ForceInline
1162 public Object allocateInstance(Class<?> cls) throws InstantiationException {
1163 // Interfaces and abstract classes are handled by the intrinsic.
1164 if (cls.isPrimitive() || cls.isArray()) {
1165 throw new InstantiationException(cls.getName());
1166 }
1167 return allocateInstance0(cls);
1168 }
1169
1170 @HotSpotIntrinsicCandidate
1171 private native Object allocateInstance0(Class<?> cls) throws InstantiationException;
1172
1173 /**
1174 * Allocates an array of a given type, but does not do zeroing.
1175 * <p>
1176 * This method should only be used in the very rare cases where a high-performance code
1177 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1178 * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1179 * <p>
1180 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1181 * before allowing untrusted code, or code in other threads, to observe the reference
1182 * to the newly allocated array. In addition, the publication of the array reference must be
1183 * safe according to the Java Memory Model requirements.
1184 * <p>
1185 * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1186 * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1187 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1188 * or issuing a {@link #storeFence} before publishing the reference.
1189 * <p>
1190 * @implnote This method can only allocate primitive arrays, to avoid garbage reference
1191 * elements that could break heap integrity.
|