1 /*
2 * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
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 jdk.internal.vm.annotation.AOTRuntimeSetup;
29 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
30 import jdk.internal.vm.annotation.ForceInline;
31 import jdk.internal.vm.annotation.IntrinsicCandidate;
32 import sun.nio.Cleaner;
33 import sun.nio.ch.DirectBuffer;
34
35 import java.lang.reflect.Field;
36 import java.security.ProtectionDomain;
37
38 import static jdk.internal.misc.UnsafeConstants.*;
39
40 /**
41 * A collection of methods for performing low-level, unsafe operations.
42 * Although the class and all methods are public, use of this class is
43 * limited because only trusted code can obtain instances of it.
44 *
45 * <em>Note:</em> It is the responsibility of the caller to make sure
46 * arguments are checked before methods of this class are
47 * called. While some rudimentary checks are performed on the input,
48 * the checks are best effort and when performance is an overriding
49 * priority, as when methods of this class are optimized by the
50 * runtime compiler, some or all checks (if any) may be elided. Hence,
51 * the caller must not rely on the checks and corresponding
52 * exceptions!
53 *
54 * @author John R. Rose
55 * @see #getUnsafe
56 */
57 @AOTSafeClassInitializer
58 public final class Unsafe {
59
60 private static native void registerNatives();
61 static {
62 runtimeSetup();
63 }
64
65 /// BASE_OFFSET, INDEX_SCALE, and ADDRESS_SIZE fields are equivalent if the
66 /// AOT initialized heap is reused, so just register natives
67 @AOTRuntimeSetup
68 private static void runtimeSetup() {
69 registerNatives();
70 }
71
72 private Unsafe() {}
169 * The first two parameters are interpreted exactly as with
170 * {@link #getInt(Object, long)} to refer to a specific
171 * Java variable (field or array element). The given value
172 * is stored into that variable.
173 * <p>
174 * The variable must be of the same type as the method
175 * parameter {@code x}.
176 *
177 * @param o Java heap object in which the variable resides, if any, else
178 * null
179 * @param offset indication of where the variable resides in a Java heap
180 * object, if any, else a memory address locating the variable
181 * statically
182 * @param x the value to store into the indicated Java variable
183 * @throws RuntimeException No defined exceptions are thrown, not even
184 * {@link NullPointerException}
185 */
186 @IntrinsicCandidate
187 public native void putInt(Object o, long offset, int x);
188
189 /**
190 * Fetches a reference value from a given Java variable.
191 * @see #getInt(Object, long)
192 */
193 @IntrinsicCandidate
194 public native Object getReference(Object o, long offset);
195
196 /**
197 * Stores a reference value into a given Java variable.
198 * <p>
199 * Unless the reference {@code x} being stored is either null
200 * or matches the field type, the results are undefined.
201 * If the reference {@code o} is non-null, card marks or
202 * other store barriers for that object (if the VM requires them)
203 * are updated.
204 * @see #putInt(Object, long, int)
205 */
206 @IntrinsicCandidate
207 public native void putReference(Object o, long offset, Object x);
208
209 /** @see #getInt(Object, long) */
210 @IntrinsicCandidate
211 public native boolean getBoolean(Object o, long offset);
212
213 /** @see #putInt(Object, long, int) */
214 @IntrinsicCandidate
215 public native void putBoolean(Object o, long offset, boolean x);
216
217 /** @see #getInt(Object, long) */
218 @IntrinsicCandidate
219 public native byte getByte(Object o, long offset);
220
221 /** @see #putInt(Object, long, int) */
222 @IntrinsicCandidate
223 public native void putByte(Object o, long offset, byte x);
224
225 /** @see #getInt(Object, long) */
226 @IntrinsicCandidate
227 public native short getShort(Object o, long offset);
228
1147 * #staticFieldOffset}.
1148 * <p>Fetch the base "Object", if any, with which static fields of the
1149 * given class can be accessed via methods like {@link #getInt(Object,
1150 * long)}. This value may be null. This value may refer to an object
1151 * which is a "cookie", not guaranteed to be a real Object, and it should
1152 * not be used in any way except as argument to the get and put routines in
1153 * this class.
1154 *
1155 * @throws NullPointerException if the field is {@code null}
1156 * @throws IllegalArgumentException if the field is not static
1157 */
1158 public Object staticFieldBase(Field f) {
1159 if (f == null) {
1160 throw new NullPointerException();
1161 }
1162
1163 return staticFieldBase0(f);
1164 }
1165
1166 /**
1167 * Detects if the given class may need to be initialized. This is often
1168 * needed in conjunction with obtaining the static field base of a
1169 * class.
1170 * @return false only if a call to {@code ensureClassInitialized} would have no effect
1171 */
1172 public boolean shouldBeInitialized(Class<?> c) {
1173 if (c == null) {
1174 throw new NullPointerException();
1175 }
1176
1177 return shouldBeInitialized0(c);
1178 }
1179
1180 /**
1181 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1182 * This is often needed in conjunction with obtaining the static field base
1183 * of a class.
1184 *
1185 * The call returns when either class {@code c} is fully initialized or
1186 * class {@code c} is being initialized and the call is performed from
1187 * the initializing thread. In the latter case a subsequent call to
1188 * {@link #shouldBeInitialized} will return {@code true}.
1189 */
1190 public void ensureClassInitialized(Class<?> c) {
1191 if (c == null) {
1192 throw new NullPointerException();
1193 }
1194
1195 ensureClassInitialized0(c);
1196 }
1197
1198 /**
1199 * Reports the offset of the first element in the storage allocation of a
1200 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1201 * for the same class, you may use that scale factor, together with this
1202 * base offset, to form new offsets to access elements of arrays of the
1203 * given class.
1204 * <p>
1205 * The return value is in the range of a {@code int}. The return type is
1206 * {@code long} to emphasize that long arithmetic should always be used
1207 * for offset calculations to avoid overflows.
1208 *
1209 * @see #getInt(Object, long)
1210 * @see #putInt(Object, long, int)
1211 */
1212 public long arrayBaseOffset(Class<?> arrayClass) {
1213 if (arrayClass == null) {
1214 throw new NullPointerException();
1215 }
1216
1217 return arrayBaseOffset0(arrayClass);
1218 }
1219
1220
1221 /** The value of {@code arrayBaseOffset(boolean[].class)} */
1222 public static final long ARRAY_BOOLEAN_BASE_OFFSET
1223 = theUnsafe.arrayBaseOffset(boolean[].class);
1224
1225 /** The value of {@code arrayBaseOffset(byte[].class)} */
1226 public static final long ARRAY_BYTE_BASE_OFFSET
1227 = theUnsafe.arrayBaseOffset(byte[].class);
1228
1229 /** The value of {@code arrayBaseOffset(short[].class)} */
1230 public static final long ARRAY_SHORT_BASE_OFFSET
1231 = theUnsafe.arrayBaseOffset(short[].class);
1232
1233 /** The value of {@code arrayBaseOffset(char[].class)} */
1234 public static final long ARRAY_CHAR_BASE_OFFSET
1235 = theUnsafe.arrayBaseOffset(char[].class);
1236
1237 /** The value of {@code arrayBaseOffset(int[].class)} */
1238 public static final long ARRAY_INT_BASE_OFFSET
1239 = theUnsafe.arrayBaseOffset(int[].class);
1246 public static final long ARRAY_FLOAT_BASE_OFFSET
1247 = theUnsafe.arrayBaseOffset(float[].class);
1248
1249 /** The value of {@code arrayBaseOffset(double[].class)} */
1250 public static final long ARRAY_DOUBLE_BASE_OFFSET
1251 = theUnsafe.arrayBaseOffset(double[].class);
1252
1253 /** The value of {@code arrayBaseOffset(Object[].class)} */
1254 public static final long ARRAY_OBJECT_BASE_OFFSET
1255 = theUnsafe.arrayBaseOffset(Object[].class);
1256
1257 /**
1258 * Reports the scale factor for addressing elements in the storage
1259 * allocation of a given array class. However, arrays of "narrow" types
1260 * will generally not work properly with accessors like {@link
1261 * #getByte(Object, long)}, so the scale factor for such classes is reported
1262 * as zero.
1263 * <p>
1264 * The computation of the actual memory offset should always use {@code
1265 * long} arithmetic to avoid overflows.
1266 *
1267 * @see #arrayBaseOffset
1268 * @see #getInt(Object, long)
1269 * @see #putInt(Object, long, int)
1270 */
1271 public int arrayIndexScale(Class<?> arrayClass) {
1272 if (arrayClass == null) {
1273 throw new NullPointerException();
1274 }
1275
1276 return arrayIndexScale0(arrayClass);
1277 }
1278
1279
1280 /** The value of {@code arrayIndexScale(boolean[].class)} */
1281 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1282 = theUnsafe.arrayIndexScale(boolean[].class);
1283
1284 /** The value of {@code arrayIndexScale(byte[].class)} */
1285 public static final int ARRAY_BYTE_INDEX_SCALE
1286 = theUnsafe.arrayIndexScale(byte[].class);
1287
1288 /** The value of {@code arrayIndexScale(short[].class)} */
1289 public static final int ARRAY_SHORT_INDEX_SCALE
1290 = theUnsafe.arrayIndexScale(short[].class);
1291
1292 /** The value of {@code arrayIndexScale(char[].class)} */
1293 public static final int ARRAY_CHAR_INDEX_SCALE
1294 = theUnsafe.arrayIndexScale(char[].class);
1295
1296 /** The value of {@code arrayIndexScale(int[].class)} */
1297 public static final int ARRAY_INT_INDEX_SCALE
1298 = theUnsafe.arrayIndexScale(int[].class);
1362 public Class<?> defineClass(String name, byte[] b, int off, int len,
1363 ClassLoader loader,
1364 ProtectionDomain protectionDomain) {
1365 if (b == null) {
1366 throw new NullPointerException();
1367 }
1368 if (len < 0) {
1369 throw new ArrayIndexOutOfBoundsException();
1370 }
1371
1372 return defineClass0(name, b, off, len, loader, protectionDomain);
1373 }
1374
1375 public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1376 ClassLoader loader,
1377 ProtectionDomain protectionDomain);
1378
1379 /**
1380 * Allocates an instance but does not run any constructor.
1381 * Initializes the class if it has not yet been.
1382 */
1383 @IntrinsicCandidate
1384 public native Object allocateInstance(Class<?> cls)
1385 throws InstantiationException;
1386
1387 /**
1388 * Allocates an array of a given type, but does not do zeroing.
1389 * <p>
1390 * This method should only be used in the very rare cases where a high-performance code
1391 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1392 * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1393 * <p>
1394 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1395 * before allowing untrusted code, or code in other threads, to observe the reference
1396 * to the newly allocated array. In addition, the publication of the array reference must be
1397 * safe according to the Java Memory Model requirements.
1398 * <p>
1399 * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1400 * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1401 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1437 return null;
1438 }
1439
1440 /** Throws the exception without telling the verifier. */
1441 public native void throwException(Throwable ee);
1442
1443 /**
1444 * Atomically updates Java variable to {@code x} if it is currently
1445 * holding {@code expected}.
1446 *
1447 * <p>This operation has memory semantics of a {@code volatile} read
1448 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1449 *
1450 * @return {@code true} if successful
1451 */
1452 @IntrinsicCandidate
1453 public final native boolean compareAndSetReference(Object o, long offset,
1454 Object expected,
1455 Object x);
1456
1457 @IntrinsicCandidate
1458 public final native Object compareAndExchangeReference(Object o, long offset,
1459 Object expected,
1460 Object x);
1461
1462 @IntrinsicCandidate
1463 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1464 Object expected,
1465 Object x) {
1466 return compareAndExchangeReference(o, offset, expected, x);
1467 }
1468
1469 @IntrinsicCandidate
1470 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1471 Object expected,
1472 Object x) {
1473 return compareAndExchangeReference(o, offset, expected, x);
1474 }
1475
1476 @IntrinsicCandidate
1477 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1478 Object expected,
1479 Object x) {
1480 return compareAndSetReference(o, offset, expected, x);
1481 }
1482
1483 @IntrinsicCandidate
1484 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1485 Object expected,
1486 Object x) {
1487 return compareAndSetReference(o, offset, expected, x);
1488 }
1489
1490 @IntrinsicCandidate
1491 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1492 Object expected,
1493 Object x) {
1494 return compareAndSetReference(o, offset, expected, x);
1495 }
1496
1497 @IntrinsicCandidate
1498 public final boolean weakCompareAndSetReference(Object o, long offset,
1499 Object expected,
1500 Object x) {
1501 return compareAndSetReference(o, offset, expected, x);
1502 }
1503
1504 /**
1505 * Atomically updates Java variable to {@code x} if it is currently
1506 * holding {@code expected}.
1507 *
1508 * <p>This operation has memory semantics of a {@code volatile} read
1509 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1510 *
1511 * @return {@code true} if successful
1512 */
1513 @IntrinsicCandidate
1514 public final native boolean compareAndSetInt(Object o, long offset,
1515 int expected,
1516 int x);
1517
1518 @IntrinsicCandidate
1519 public final native int compareAndExchangeInt(Object o, long offset,
1520 int expected,
1521 int x);
1522
1523 @IntrinsicCandidate
2096 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2097 long expected,
2098 long x) {
2099 return compareAndSetLong(o, offset, expected, x);
2100 }
2101
2102 @IntrinsicCandidate
2103 public final boolean weakCompareAndSetLong(Object o, long offset,
2104 long expected,
2105 long x) {
2106 return compareAndSetLong(o, offset, expected, x);
2107 }
2108
2109 /**
2110 * Fetches a reference value from a given Java variable, with volatile
2111 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2112 */
2113 @IntrinsicCandidate
2114 public native Object getReferenceVolatile(Object o, long offset);
2115
2116 /**
2117 * Stores a reference value into a given Java variable, with
2118 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2119 */
2120 @IntrinsicCandidate
2121 public native void putReferenceVolatile(Object o, long offset, Object x);
2122
2123 /** Volatile version of {@link #getInt(Object, long)} */
2124 @IntrinsicCandidate
2125 public native int getIntVolatile(Object o, long offset);
2126
2127 /** Volatile version of {@link #putInt(Object, long, int)} */
2128 @IntrinsicCandidate
2129 public native void putIntVolatile(Object o, long offset, int x);
2130
2131 /** Volatile version of {@link #getBoolean(Object, long)} */
2132 @IntrinsicCandidate
2133 public native boolean getBooleanVolatile(Object o, long offset);
2134
2135 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2136 @IntrinsicCandidate
2137 public native void putBooleanVolatile(Object o, long offset, boolean x);
2138
2139 /** Volatile version of {@link #getByte(Object, long)} */
2140 @IntrinsicCandidate
2141 public native byte getByteVolatile(Object o, long offset);
2142
2175 /** Volatile version of {@link #putFloat(Object, long, float)} */
2176 @IntrinsicCandidate
2177 public native void putFloatVolatile(Object o, long offset, float x);
2178
2179 /** Volatile version of {@link #getDouble(Object, long)} */
2180 @IntrinsicCandidate
2181 public native double getDoubleVolatile(Object o, long offset);
2182
2183 /** Volatile version of {@link #putDouble(Object, long, double)} */
2184 @IntrinsicCandidate
2185 public native void putDoubleVolatile(Object o, long offset, double x);
2186
2187
2188
2189 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2190 @IntrinsicCandidate
2191 public final Object getReferenceAcquire(Object o, long offset) {
2192 return getReferenceVolatile(o, offset);
2193 }
2194
2195 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2196 @IntrinsicCandidate
2197 public final boolean getBooleanAcquire(Object o, long offset) {
2198 return getBooleanVolatile(o, offset);
2199 }
2200
2201 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2202 @IntrinsicCandidate
2203 public final byte getByteAcquire(Object o, long offset) {
2204 return getByteVolatile(o, offset);
2205 }
2206
2207 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2208 @IntrinsicCandidate
2209 public final short getShortAcquire(Object o, long offset) {
2210 return getShortVolatile(o, offset);
2211 }
2212
2213 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2214 @IntrinsicCandidate
2239 public final double getDoubleAcquire(Object o, long offset) {
2240 return getDoubleVolatile(o, offset);
2241 }
2242
2243 /*
2244 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2245 * that do not guarantee immediate visibility of the store to
2246 * other threads. This method is generally only useful if the
2247 * underlying field is a Java volatile (or if an array cell, one
2248 * that is otherwise only accessed using volatile accesses).
2249 *
2250 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2251 */
2252
2253 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2254 @IntrinsicCandidate
2255 public final void putReferenceRelease(Object o, long offset, Object x) {
2256 putReferenceVolatile(o, offset, x);
2257 }
2258
2259 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2260 @IntrinsicCandidate
2261 public final void putBooleanRelease(Object o, long offset, boolean x) {
2262 putBooleanVolatile(o, offset, x);
2263 }
2264
2265 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2266 @IntrinsicCandidate
2267 public final void putByteRelease(Object o, long offset, byte x) {
2268 putByteVolatile(o, offset, x);
2269 }
2270
2271 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2272 @IntrinsicCandidate
2273 public final void putShortRelease(Object o, long offset, short x) {
2274 putShortVolatile(o, offset, x);
2275 }
2276
2277 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2278 @IntrinsicCandidate
2295 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2296 @IntrinsicCandidate
2297 public final void putLongRelease(Object o, long offset, long x) {
2298 putLongVolatile(o, offset, x);
2299 }
2300
2301 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2302 @IntrinsicCandidate
2303 public final void putDoubleRelease(Object o, long offset, double x) {
2304 putDoubleVolatile(o, offset, x);
2305 }
2306
2307 // ------------------------------ Opaque --------------------------------------
2308
2309 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2310 @IntrinsicCandidate
2311 public final Object getReferenceOpaque(Object o, long offset) {
2312 return getReferenceVolatile(o, offset);
2313 }
2314
2315 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2316 @IntrinsicCandidate
2317 public final boolean getBooleanOpaque(Object o, long offset) {
2318 return getBooleanVolatile(o, offset);
2319 }
2320
2321 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2322 @IntrinsicCandidate
2323 public final byte getByteOpaque(Object o, long offset) {
2324 return getByteVolatile(o, offset);
2325 }
2326
2327 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2328 @IntrinsicCandidate
2329 public final short getShortOpaque(Object o, long offset) {
2330 return getShortVolatile(o, offset);
2331 }
2332
2333 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2334 @IntrinsicCandidate
2349 }
2350
2351 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2352 @IntrinsicCandidate
2353 public final long getLongOpaque(Object o, long offset) {
2354 return getLongVolatile(o, offset);
2355 }
2356
2357 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2358 @IntrinsicCandidate
2359 public final double getDoubleOpaque(Object o, long offset) {
2360 return getDoubleVolatile(o, offset);
2361 }
2362
2363 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2364 @IntrinsicCandidate
2365 public final void putReferenceOpaque(Object o, long offset, Object x) {
2366 putReferenceVolatile(o, offset, x);
2367 }
2368
2369 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2370 @IntrinsicCandidate
2371 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2372 putBooleanVolatile(o, offset, x);
2373 }
2374
2375 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2376 @IntrinsicCandidate
2377 public final void putByteOpaque(Object o, long offset, byte x) {
2378 putByteVolatile(o, offset, x);
2379 }
2380
2381 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2382 @IntrinsicCandidate
2383 public final void putShortOpaque(Object o, long offset, short x) {
2384 putShortVolatile(o, offset, x);
2385 }
2386
2387 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2388 @IntrinsicCandidate
2397 }
2398
2399 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2400 @IntrinsicCandidate
2401 public final void putFloatOpaque(Object o, long offset, float x) {
2402 putFloatVolatile(o, offset, x);
2403 }
2404
2405 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2406 @IntrinsicCandidate
2407 public final void putLongOpaque(Object o, long offset, long x) {
2408 putLongVolatile(o, offset, x);
2409 }
2410
2411 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2412 @IntrinsicCandidate
2413 public final void putDoubleOpaque(Object o, long offset, double x) {
2414 putDoubleVolatile(o, offset, x);
2415 }
2416
2417 /**
2418 * Unblocks the given thread blocked on {@code park}, or, if it is
2419 * not blocked, causes the subsequent call to {@code park} not to
2420 * block. Note: this operation is "unsafe" solely because the
2421 * caller must somehow ensure that the thread has not been
2422 * destroyed. Nothing special is usually required to ensure this
2423 * when called from Java (in which there will ordinarily be a live
2424 * reference to the thread) but this is not nearly-automatically
2425 * so when calling from native code.
2426 *
2427 * @param thread the thread to unpark.
2428 */
2429 @IntrinsicCandidate
2430 public native void unpark(Object thread);
2431
2432 /**
2433 * Blocks current thread, returning when a balancing
2434 * {@code unpark} occurs, or a balancing {@code unpark} has
2435 * already occurred, or the thread is interrupted, or, if not
2436 * absolute and time is not zero, the given time nanoseconds have
2783 /**
2784 * Atomically exchanges the given reference value with the current
2785 * reference value of a field or array element within the given
2786 * object {@code o} at the given {@code offset}.
2787 *
2788 * @param o object/array to update the field/element in
2789 * @param offset field/element offset
2790 * @param newValue new value
2791 * @return the previous value
2792 * @since 1.8
2793 */
2794 @IntrinsicCandidate
2795 public final Object getAndSetReference(Object o, long offset, Object newValue) {
2796 Object v;
2797 do {
2798 v = getReferenceVolatile(o, offset);
2799 } while (!weakCompareAndSetReference(o, offset, v, newValue));
2800 return v;
2801 }
2802
2803 @ForceInline
2804 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
2805 Object v;
2806 do {
2807 v = getReference(o, offset);
2808 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
2809 return v;
2810 }
2811
2812 @ForceInline
2813 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
2814 Object v;
2815 do {
2816 v = getReferenceAcquire(o, offset);
2817 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
2818 return v;
2819 }
2820
2821 @IntrinsicCandidate
2822 public final byte getAndSetByte(Object o, long offset, byte newValue) {
2823 byte v;
2824 do {
2825 v = getByteVolatile(o, offset);
2826 } while (!weakCompareAndSetByte(o, offset, v, newValue));
2827 return v;
2828 }
2829
2830 @ForceInline
2831 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
2832 byte v;
2833 do {
2834 v = getByte(o, offset);
2835 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
2836 return v;
2837 }
2838
2839 @ForceInline
2840 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
3856 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
3857 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
3858 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
3859
3860
3861
3862 private native long allocateMemory0(long bytes);
3863 private native long reallocateMemory0(long address, long bytes);
3864 private native void freeMemory0(long address);
3865 @IntrinsicCandidate
3866 private native void setMemory0(Object o, long offset, long bytes, byte value);
3867 @IntrinsicCandidate
3868 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3869 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3870 private native long objectFieldOffset0(Field f); // throws IAE
3871 private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
3872 private native long staticFieldOffset0(Field f); // throws IAE
3873 private native Object staticFieldBase0(Field f); // throws IAE
3874 private native boolean shouldBeInitialized0(Class<?> c);
3875 private native void ensureClassInitialized0(Class<?> c);
3876 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
3877 private native int arrayIndexScale0(Class<?> arrayClass);
3878 private native int getLoadAverage0(double[] loadavg, int nelems);
3879
3880
3881 /**
3882 * Invokes the given direct byte buffer's cleaner, if any.
3883 *
3884 * @param directBuffer a direct byte buffer
3885 * @throws NullPointerException if {@code directBuffer} is null
3886 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
3887 * or is a {@link java.nio.Buffer#slice slice}, or is a
3888 * {@link java.nio.Buffer#duplicate duplicate}
3889 */
3890 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
3891 if (!directBuffer.isDirect())
3892 throw new IllegalArgumentException("buffer is non-direct");
3893
3894 DirectBuffer db = (DirectBuffer) directBuffer;
3895 if (db.attachment() != null)
3896 throw new IllegalArgumentException("duplicate or slice");
3897
3898 Cleaner cleaner = db.cleaner();
|
1 /*
2 * Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
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 jdk.internal.vm.annotation.AOTRuntimeSetup;
29 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
30 import jdk.internal.vm.annotation.ForceInline;
31 import jdk.internal.vm.annotation.IntrinsicCandidate;
32 import jdk.internal.value.ValueClass;
33 import sun.nio.Cleaner;
34 import sun.nio.ch.DirectBuffer;
35
36 import java.lang.reflect.Field;
37 import java.security.ProtectionDomain;
38
39 import static jdk.internal.misc.UnsafeConstants.*;
40
41 /**
42 * A collection of methods for performing low-level, unsafe operations.
43 * Although the class and all methods are public, use of this class is
44 * limited because only trusted code can obtain instances of it.
45 *
46 * <h2><a id="undefined-behavior">Undefined Behavior</a></h2>
47 * For performance reasons, {@code Unsafe} is allowed to work outside the
48 * restrictions enforced by the JVM. As a result, it is the responsibility of
49 * the caller to ensure that an invocation of an {@code Unsafe} method is
50 * conformant, and failure to do so will result in undefined behavior. The
51 * runtime and the JIT compiler may assume that undefined behavior never
52 * happens, and operate accordingly. For example, the runtime assumes that each
53 * object has a header with a particular layout, and if the users use
54 * {@code Unsafe} to overwrite this header with invalid data, the behavior of
55 * the runtime becomes unpredictable. Another example is that the JIT compiler
56 * may assume that accesses on separate objects are unrelated, and schedule
57 * each of them without taking into consideration the others. If there is an
58 * {@code Unsafe} access that is out of bounds and points to object different
59 * from the declared base, the program may execute in a way that a variable
60 * seems to have multiple values at the same time. As a result, when a program
61 * exhibits undefined behavior, there is no restrictions on its behaviors. Such
62 * behaviors may include but not be limited to:
63 *
64 * <ul>
65 * <li>Working as expected.
66 * <li>Crashing the VM.
67 * <li>Corruption of the heap or JVM memory.
68 * <li>Nonsensical variable value. E.g. an {@code int} may appear to be
69 * simultaneously 0 and 1.
70 * <li>Impossible code execution. E.g. the branches of an {@code if} are
71 * both executed or both not executed.
72 * <li>Wiping out the hard drive.
73 * </ul>
74 *
75 * Undefined behavior, as described in this class, is analogous to the
76 * terminology with the same name in the C++ language.
77 * <p>
78 * Some methods (e.g. {@link #getInt}) exhibit undefined behavior if they
79 * are invoked at runtime with illegal arguments. This means that they will
80 * never exhibit undefined behavior if they are not actually reachable at
81 * runtime. On the other hands, other methods (e.g.
82 * {@link #allocateInstance(Class)}) exhibit undefined behavior if they are
83 * used incorrectly, even if the invocation may not be reachable at runtime.
84 * The analogous terminology in C++ is that such programs are ill-formed.
85 * <p>
86 * For methods exhibiting undefined behavior if they are invoked at runtime
87 * with illegal arguments, undefined behavior may time travel. That is, if a
88 * control path may eventually reach an invocation of an {@code Unsafe} method
89 * with illegal arguments, the symptoms of undefined behavior may be present
90 * even before the invocation of the {@code Unsafe} method. This is because the
91 * JIT compiler may have certain assumptions about the inputs of an
92 * {@code Unsafe} invocation, these assumptions may propagate backward to
93 * previous statements, leading to wrong executions if the assumptions are
94 * invalid.
95 *
96 * @author John R. Rose
97 * @see #getUnsafe
98 */
99 @AOTSafeClassInitializer
100 public final class Unsafe {
101
102 private static native void registerNatives();
103 static {
104 runtimeSetup();
105 }
106
107 /// BASE_OFFSET, INDEX_SCALE, and ADDRESS_SIZE fields are equivalent if the
108 /// AOT initialized heap is reused, so just register natives
109 @AOTRuntimeSetup
110 private static void runtimeSetup() {
111 registerNatives();
112 }
113
114 private Unsafe() {}
211 * The first two parameters are interpreted exactly as with
212 * {@link #getInt(Object, long)} to refer to a specific
213 * Java variable (field or array element). The given value
214 * is stored into that variable.
215 * <p>
216 * The variable must be of the same type as the method
217 * parameter {@code x}.
218 *
219 * @param o Java heap object in which the variable resides, if any, else
220 * null
221 * @param offset indication of where the variable resides in a Java heap
222 * object, if any, else a memory address locating the variable
223 * statically
224 * @param x the value to store into the indicated Java variable
225 * @throws RuntimeException No defined exceptions are thrown, not even
226 * {@link NullPointerException}
227 */
228 @IntrinsicCandidate
229 public native void putInt(Object o, long offset, int x);
230
231 /**
232 * Returns true if the given field is flattened.
233 */
234 public boolean isFlatField(Field f) {
235 if (f == null) {
236 throw new NullPointerException();
237 }
238 return isFlatField0(f);
239 }
240
241 private native boolean isFlatField0(Object o);
242
243 /* Returns true if the given field has a null marker
244 * <p>
245 * Nullable flat fields are stored in a flattened representation
246 * and have an associated null marker to indicate if the the field value is
247 * null or the one stored with the flat representation
248 */
249
250 public boolean hasNullMarker(Field f) {
251 if (f == null) {
252 throw new NullPointerException();
253 }
254 return hasNullMarker0(f);
255 }
256
257 private native boolean hasNullMarker0(Object o);
258
259 /* Returns the offset of the null marker of the field,
260 * or -1 if the field doesn't have a null marker
261 */
262
263 public int nullMarkerOffset(Field f) {
264 if (f == null) {
265 throw new NullPointerException();
266 }
267 return nullMarkerOffset0(f);
268 }
269
270 private native int nullMarkerOffset0(Object o);
271
272 /**
273 * The layout value for a "reference pointer" layout. This is the only
274 * value layout values should check against; all other layout values
275 * represent some kind of flat layout opaque to Java code.
276 *
277 * @see #arrayLayout
278 * @see #fieldLayout
279 */
280 public static final int NON_FLAT_LAYOUT = 0;
281
282 /* Reports the kind of layout used for an element in the storage
283 * allocation of the given array. Do not expect to perform any logic
284 * or layout control with this value, it is just an opaque token
285 * used for performance reasons.
286 *
287 * A layout of 0 indicates this array is not flat.
288 */
289 public int arrayLayout(Object[] array) {
290 if (array == null) {
291 throw new NullPointerException();
292 }
293 return arrayLayout0(array);
294 }
295
296 @IntrinsicCandidate
297 private native int arrayLayout0(Object[] array);
298
299
300 /* Reports the kind of layout used for a given field in the storage
301 * allocation of its class. Do not expect to perform any logic
302 * or layout control with this value, it is just an opaque token
303 * used for performance reasons.
304 *
305 * A layout of 0 indicates this field is not flat.
306 */
307 public int fieldLayout(Field f) {
308 if (f == null) {
309 throw new NullPointerException();
310 }
311 return fieldLayout0(f);
312 }
313
314 private native int fieldLayout0(Object o);
315
316 public native Object[] newSpecialArray(Class<?> componentType,
317 int length, int layoutKind);
318
319 /**
320 * Fetches a reference value from a given Java variable.
321 * This method can return a reference to either an object or value
322 * or a null reference.
323 *
324 * @see #getInt(Object, long)
325 */
326 @IntrinsicCandidate
327 public native Object getReference(Object o, long offset);
328
329 /**
330 * Stores a reference value into a given Java variable.
331 * This method can store a reference to either an object or value
332 * or a null reference.
333 * <p>
334 * Unless the reference {@code x} being stored is either null
335 * or matches the field type, the results are undefined.
336 * If the reference {@code o} is non-null, card marks or
337 * other store barriers for that object (if the VM requires them)
338 * are updated.
339 * @see #putInt(Object, long, int)
340 */
341 @IntrinsicCandidate
342 public native void putReference(Object o, long offset, Object x);
343
344 /**
345 * Fetches a value of type {@code <V>} from a given Java variable.
346 * More specifically, fetches a field or array element within the given
347 * {@code o} object at the given offset, or (if {@code o} is null)
348 * from the memory address whose numerical value is the given offset.
349 *
350 * @apiNote
351 * The returned object is newly allocated into the heap, because flat
352 * values lack object headers and thus can't be used as objects directly.
353 *
354 * @param o Java heap object in which the variable resides, if any, else
355 * null
356 * @param offset indication of where the variable resides in a Java heap
357 * object, if any, else a memory address locating the variable
358 * statically
359 * @param layoutKind opaque value used by the VM to know the layout
360 * the field or array element. This value must be retrieved with
361 * {@link #fieldLayout} or {@link #arrayLayout}.
362 * @param valueType value type
363 * @param <V> the type of a value
364 * @return the value fetched from the indicated Java variable
365 * @throws RuntimeException No defined exceptions are thrown, not even
366 * {@link NullPointerException}
367 */
368 @IntrinsicCandidate
369 public native <V> V getFlatValue(Object o, long offset, int layoutKind, Class<?> valueType);
370
371 /**
372 * Stores the given value into a given Java variable.
373 *
374 * Unless the reference {@code o} being stored is either null
375 * or matches the field type, the results are undefined.
376 *
377 * @param o Java heap object in which the variable resides, if any, else
378 * null
379 * @param offset indication of where the variable resides in a Java heap
380 * object, if any, else a memory address locating the variable
381 * statically
382 * @param layoutKind opaque value used by the VM to know the layout
383 * the field or array element. This value must be retrieved with
384 * {@link #fieldLayout} or {@link #arrayLayout}.
385 * @param valueType value type
386 * @param v the value to store into the indicated Java variable
387 * @param <V> the type of a value
388 * @throws RuntimeException No defined exceptions are thrown, not even
389 * {@link NullPointerException}
390 */
391 @IntrinsicCandidate
392 public native <V> void putFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, V v);
393
394 /**
395 * Returns the header size of the given value type.
396 *
397 * @param valueType value type
398 * @return the header size of the value type
399 */
400 public native <V> long valueHeaderSize(Class<V> valueType);
401
402 /** @see #getInt(Object, long) */
403 @IntrinsicCandidate
404 public native boolean getBoolean(Object o, long offset);
405
406 /** @see #putInt(Object, long, int) */
407 @IntrinsicCandidate
408 public native void putBoolean(Object o, long offset, boolean x);
409
410 /** @see #getInt(Object, long) */
411 @IntrinsicCandidate
412 public native byte getByte(Object o, long offset);
413
414 /** @see #putInt(Object, long, int) */
415 @IntrinsicCandidate
416 public native void putByte(Object o, long offset, byte x);
417
418 /** @see #getInt(Object, long) */
419 @IntrinsicCandidate
420 public native short getShort(Object o, long offset);
421
1340 * #staticFieldOffset}.
1341 * <p>Fetch the base "Object", if any, with which static fields of the
1342 * given class can be accessed via methods like {@link #getInt(Object,
1343 * long)}. This value may be null. This value may refer to an object
1344 * which is a "cookie", not guaranteed to be a real Object, and it should
1345 * not be used in any way except as argument to the get and put routines in
1346 * this class.
1347 *
1348 * @throws NullPointerException if the field is {@code null}
1349 * @throws IllegalArgumentException if the field is not static
1350 */
1351 public Object staticFieldBase(Field f) {
1352 if (f == null) {
1353 throw new NullPointerException();
1354 }
1355
1356 return staticFieldBase0(f);
1357 }
1358
1359 /**
1360 * Detects if the given class is not yet fully initialized. This is often
1361 * needed in conjunction with obtaining the static field base of a
1362 * class.
1363 * @return false only if a call to {@code ensureClassInitialized} would have no effect
1364 */
1365 public boolean shouldBeInitialized(Class<?> c) {
1366 if (c == null) {
1367 throw new NullPointerException();
1368 }
1369
1370 return shouldBeInitialized0(c);
1371 }
1372
1373 /**
1374 * Ensures the given class has been initialized (see JVMS-5.5 for details).
1375 * This is often needed in conjunction with obtaining the static field base
1376 * of a class.
1377 *
1378 * The call returns when either class {@code c} is fully initialized or
1379 * class {@code c} is being initialized and the call is performed from
1380 * the initializing thread. In the latter case a subsequent call to
1381 * {@link #shouldBeInitialized} will return {@code true}.
1382 */
1383 public void ensureClassInitialized(Class<?> c) {
1384 if (c == null) {
1385 throw new NullPointerException();
1386 }
1387
1388 ensureClassInitialized0(c);
1389 }
1390
1391 /**
1392 * The reading or writing of strict static fields may require
1393 * special processing. Notify the VM that such an event is about
1394 * to happen. The VM may respond by throwing an exception, in the
1395 * case of a read of an uninitialized field. If the VM allows the
1396 * method to return normally, no further calls are needed, with
1397 * the same arguments.
1398 */
1399 public void notifyStrictStaticAccess(Class<?> c, long staticFieldOffset, boolean writing) {
1400 if (c == null) {
1401 throw new NullPointerException();
1402 }
1403 notifyStrictStaticAccess0(c, staticFieldOffset, writing);
1404 }
1405
1406 /**
1407 * Reports the offset of the first element in the storage allocation of a
1408 * given array class. If {@link #arrayIndexScale} returns a non-zero value
1409 * for the same class, you may use that scale factor, together with this
1410 * base offset, to form new offsets to access elements of arrays of the
1411 * given class.
1412 * <p>
1413 * The return value is in the range of a {@code int}. The return type is
1414 * {@code long} to emphasize that long arithmetic should always be used
1415 * for offset calculations to avoid overflows.
1416 * <p>
1417 * This method doesn't support arrays with an element type that is
1418 * a value class, because this type of array can have multiple layouts.
1419 * For these arrays, {@code arrayInstanceBaseOffset(Object[] array)}
1420 * must be used instead.
1421 *
1422 * @see #getInt(Object, long)
1423 * @see #putInt(Object, long, int)
1424 */
1425 public long arrayBaseOffset(Class<?> arrayClass) {
1426 if (arrayClass == null) {
1427 throw new NullPointerException();
1428 }
1429
1430 return arrayBaseOffset0(arrayClass);
1431 }
1432
1433 public long arrayInstanceBaseOffset(Object[] array) {
1434 if (array == null) {
1435 throw new NullPointerException();
1436 }
1437
1438 return arrayInstanceBaseOffset0(array);
1439 }
1440
1441 /** The value of {@code arrayBaseOffset(boolean[].class)} */
1442 public static final long ARRAY_BOOLEAN_BASE_OFFSET
1443 = theUnsafe.arrayBaseOffset(boolean[].class);
1444
1445 /** The value of {@code arrayBaseOffset(byte[].class)} */
1446 public static final long ARRAY_BYTE_BASE_OFFSET
1447 = theUnsafe.arrayBaseOffset(byte[].class);
1448
1449 /** The value of {@code arrayBaseOffset(short[].class)} */
1450 public static final long ARRAY_SHORT_BASE_OFFSET
1451 = theUnsafe.arrayBaseOffset(short[].class);
1452
1453 /** The value of {@code arrayBaseOffset(char[].class)} */
1454 public static final long ARRAY_CHAR_BASE_OFFSET
1455 = theUnsafe.arrayBaseOffset(char[].class);
1456
1457 /** The value of {@code arrayBaseOffset(int[].class)} */
1458 public static final long ARRAY_INT_BASE_OFFSET
1459 = theUnsafe.arrayBaseOffset(int[].class);
1466 public static final long ARRAY_FLOAT_BASE_OFFSET
1467 = theUnsafe.arrayBaseOffset(float[].class);
1468
1469 /** The value of {@code arrayBaseOffset(double[].class)} */
1470 public static final long ARRAY_DOUBLE_BASE_OFFSET
1471 = theUnsafe.arrayBaseOffset(double[].class);
1472
1473 /** The value of {@code arrayBaseOffset(Object[].class)} */
1474 public static final long ARRAY_OBJECT_BASE_OFFSET
1475 = theUnsafe.arrayBaseOffset(Object[].class);
1476
1477 /**
1478 * Reports the scale factor for addressing elements in the storage
1479 * allocation of a given array class. However, arrays of "narrow" types
1480 * will generally not work properly with accessors like {@link
1481 * #getByte(Object, long)}, so the scale factor for such classes is reported
1482 * as zero.
1483 * <p>
1484 * The computation of the actual memory offset should always use {@code
1485 * long} arithmetic to avoid overflows.
1486 * <p>
1487 * This method doesn't support arrays with an element type that is
1488 * a value class, because this type of array can have multiple layouts.
1489 * For these arrays, {@code arrayInstanceIndexScale(Object[] array)}
1490 * must be used instead.
1491 *
1492 * @see #arrayBaseOffset
1493 * @see #getInt(Object, long)
1494 * @see #putInt(Object, long, int)
1495 */
1496 public int arrayIndexScale(Class<?> arrayClass) {
1497 if (arrayClass == null) {
1498 throw new NullPointerException();
1499 }
1500
1501 return arrayIndexScale0(arrayClass);
1502 }
1503
1504 public int arrayInstanceIndexScale(Object[] array) {
1505 if (array == null) {
1506 throw new NullPointerException();
1507 }
1508
1509 return arrayInstanceIndexScale0(array);
1510 }
1511
1512 /**
1513 * Returns the acmp map of this class, which must be a concrete value class.
1514 * Intended to be used by substitutability test in ValueObjectMethods only.
1515 * The format is subject to change.
1516 */
1517 public int[] getFieldMap(Class<?> c) {
1518 if (c == null) {
1519 throw new NullPointerException();
1520 }
1521 return getFieldMap0(c);
1522 }
1523
1524 /**
1525 * For a field of type {@code c}, returns true if and only if there is
1526 * a possible flat layout that contains no oop.
1527 * Required for numerical CAS safety.
1528 */
1529 public boolean isFlatPayloadBinary(Class<?> c) {
1530 int[] map = getFieldMap(c);
1531 int nbNonRef = map[0];
1532 return nbNonRef * 2 + 1 == map.length;
1533 }
1534
1535 /**
1536 * Return the size of the object in the heap.
1537 * @param o an object
1538 * @return the objects's size
1539 */
1540 public long getObjectSize(Object o) {
1541 if (o == null)
1542 throw new NullPointerException();
1543 return getObjectSize0(o);
1544 }
1545
1546 /** The value of {@code arrayIndexScale(boolean[].class)} */
1547 public static final int ARRAY_BOOLEAN_INDEX_SCALE
1548 = theUnsafe.arrayIndexScale(boolean[].class);
1549
1550 /** The value of {@code arrayIndexScale(byte[].class)} */
1551 public static final int ARRAY_BYTE_INDEX_SCALE
1552 = theUnsafe.arrayIndexScale(byte[].class);
1553
1554 /** The value of {@code arrayIndexScale(short[].class)} */
1555 public static final int ARRAY_SHORT_INDEX_SCALE
1556 = theUnsafe.arrayIndexScale(short[].class);
1557
1558 /** The value of {@code arrayIndexScale(char[].class)} */
1559 public static final int ARRAY_CHAR_INDEX_SCALE
1560 = theUnsafe.arrayIndexScale(char[].class);
1561
1562 /** The value of {@code arrayIndexScale(int[].class)} */
1563 public static final int ARRAY_INT_INDEX_SCALE
1564 = theUnsafe.arrayIndexScale(int[].class);
1628 public Class<?> defineClass(String name, byte[] b, int off, int len,
1629 ClassLoader loader,
1630 ProtectionDomain protectionDomain) {
1631 if (b == null) {
1632 throw new NullPointerException();
1633 }
1634 if (len < 0) {
1635 throw new ArrayIndexOutOfBoundsException();
1636 }
1637
1638 return defineClass0(name, b, off, len, loader, protectionDomain);
1639 }
1640
1641 public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1642 ClassLoader loader,
1643 ProtectionDomain protectionDomain);
1644
1645 /**
1646 * Allocates an instance but does not run any constructor.
1647 * Initializes the class if it has not yet been.
1648 * <p>
1649 * This method returns an uninitialized instance. In general, this is undefined behavior, this
1650 * method is treated specially by the JVM to allow this behavior. The returned value must be
1651 * passed into a constructor using {@link MethodHandle#linkToSpecial} before any other
1652 * operation can be performed on it. Otherwise, the program is ill-formed.
1653 */
1654 @IntrinsicCandidate
1655 public native Object allocateInstance(Class<?> cls)
1656 throws InstantiationException;
1657
1658 /**
1659 * Allocates an array of a given type, but does not do zeroing.
1660 * <p>
1661 * This method should only be used in the very rare cases where a high-performance code
1662 * overwrites the destination array completely, and compilers cannot assist in zeroing elimination.
1663 * In an overwhelming majority of cases, a normal Java allocation should be used instead.
1664 * <p>
1665 * Users of this method are <b>required</b> to overwrite the initial (garbage) array contents
1666 * before allowing untrusted code, or code in other threads, to observe the reference
1667 * to the newly allocated array. In addition, the publication of the array reference must be
1668 * safe according to the Java Memory Model requirements.
1669 * <p>
1670 * The safest approach to deal with an uninitialized array is to keep the reference to it in local
1671 * variable at least until the initialization is complete, and then publish it <b>once</b>, either
1672 * by writing it to a <em>volatile</em> field, or storing it into a <em>final</em> field in constructor,
1708 return null;
1709 }
1710
1711 /** Throws the exception without telling the verifier. */
1712 public native void throwException(Throwable ee);
1713
1714 /**
1715 * Atomically updates Java variable to {@code x} if it is currently
1716 * holding {@code expected}.
1717 *
1718 * <p>This operation has memory semantics of a {@code volatile} read
1719 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1720 *
1721 * @return {@code true} if successful
1722 */
1723 @IntrinsicCandidate
1724 public final native boolean compareAndSetReference(Object o, long offset,
1725 Object expected,
1726 Object x);
1727
1728 @ForceInline
1729 private final boolean isValueObject(Class<?> valueType, Object o) {
1730 return ValueClass.isValueObjectCompatible(valueType)
1731 && o != null && o.getClass().isValue();
1732 }
1733
1734 /*
1735 * For value type, CAS should do substitutability test as opposed
1736 * to two pointers comparison.
1737 */
1738 @ForceInline
1739 public final <V> boolean compareAndSetReference(Object o, long offset,
1740 Class<?> type,
1741 V expected,
1742 V x) {
1743 if (isValueObject(type, expected)) {
1744 while (true) {
1745 Object witness = getReferenceVolatile(o, offset);
1746 if (witness != expected) {
1747 return false;
1748 }
1749 if (compareAndSetReference(o, offset, witness, x)) {
1750 return true;
1751 }
1752 }
1753 } else {
1754 return compareAndSetReference(o, offset, expected, x);
1755 }
1756 }
1757
1758 @ForceInline
1759 public final <V> boolean compareAndSetFlatValue(Object o, long offset,
1760 int layout,
1761 Class<?> valueType,
1762 V expected,
1763 V x) {
1764 Object[] array = newSpecialArray(valueType, 2, layout);
1765 return compareAndSetFlatValueAsBytes(array, o, offset, layout, valueType, expected, x);
1766 }
1767
1768 @IntrinsicCandidate
1769 public final native Object compareAndExchangeReference(Object o, long offset,
1770 Object expected,
1771 Object x);
1772
1773 @ForceInline
1774 public final <V> Object compareAndExchangeReference(Object o, long offset,
1775 Class<?> valueType,
1776 V expected,
1777 V x) {
1778 if (isValueObject(valueType, expected)) {
1779 while (true) {
1780 Object witness = getReferenceVolatile(o, offset);
1781 if (witness != expected) {
1782 return witness;
1783 }
1784 if (compareAndSetReference(o, offset, witness, x)) {
1785 return witness;
1786 }
1787 }
1788 } else {
1789 return compareAndExchangeReference(o, offset, expected, x);
1790 }
1791 }
1792
1793 @ForceInline
1794 public final <V> Object compareAndExchangeFlatValue(Object o, long offset,
1795 int layout,
1796 Class<?> valueType,
1797 V expected,
1798 V x) {
1799 Object[] array = newSpecialArray(valueType, 2, layout);
1800 compareAndSetFlatValueAsBytes(array, o, offset, layout, valueType, expected, x);
1801 return array[0];
1802 }
1803
1804 @IntrinsicCandidate
1805 public final Object compareAndExchangeReferenceAcquire(Object o, long offset,
1806 Object expected,
1807 Object x) {
1808 return compareAndExchangeReference(o, offset, expected, x);
1809 }
1810
1811 public final <V> Object compareAndExchangeReferenceAcquire(Object o, long offset,
1812 Class<?> valueType,
1813 V expected,
1814 V x) {
1815 return compareAndExchangeReference(o, offset, valueType, expected, x);
1816 }
1817
1818 @ForceInline
1819 public final <V> Object compareAndExchangeFlatValueAcquire(Object o, long offset,
1820 int layout,
1821 Class<?> valueType,
1822 V expected,
1823 V x) {
1824 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1825 }
1826
1827 @IntrinsicCandidate
1828 public final Object compareAndExchangeReferenceRelease(Object o, long offset,
1829 Object expected,
1830 Object x) {
1831 return compareAndExchangeReference(o, offset, expected, x);
1832 }
1833
1834 public final <V> Object compareAndExchangeReferenceRelease(Object o, long offset,
1835 Class<?> valueType,
1836 V expected,
1837 V x) {
1838 return compareAndExchangeReference(o, offset, valueType, expected, x);
1839 }
1840
1841 @ForceInline
1842 public final <V> Object compareAndExchangeFlatValueRelease(Object o, long offset,
1843 int layout,
1844 Class<?> valueType,
1845 V expected,
1846 V x) {
1847 return compareAndExchangeFlatValue(o, offset, layout, valueType, expected, x);
1848 }
1849
1850 @IntrinsicCandidate
1851 public final boolean weakCompareAndSetReferencePlain(Object o, long offset,
1852 Object expected,
1853 Object x) {
1854 return compareAndSetReference(o, offset, expected, x);
1855 }
1856
1857 public final <V> boolean weakCompareAndSetReferencePlain(Object o, long offset,
1858 Class<?> valueType,
1859 V expected,
1860 V x) {
1861 if (isValueObject(valueType, expected)) {
1862 // Reusing a stronger operation for now, still compliant
1863 return compareAndSetReference(o, offset, valueType, expected, x);
1864 } else {
1865 return weakCompareAndSetReferencePlain(o, offset, expected, x);
1866 }
1867 }
1868
1869 @ForceInline
1870 public final <V> boolean weakCompareAndSetFlatValuePlain(Object o, long offset,
1871 int layout,
1872 Class<?> valueType,
1873 V expected,
1874 V x) {
1875 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1876 }
1877
1878 @IntrinsicCandidate
1879 public final boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1880 Object expected,
1881 Object x) {
1882 return compareAndSetReference(o, offset, expected, x);
1883 }
1884
1885 public final <V> boolean weakCompareAndSetReferenceAcquire(Object o, long offset,
1886 Class<?> valueType,
1887 V expected,
1888 V x) {
1889 if (isValueObject(valueType, expected)) {
1890 // Reusing a stronger operation for now, still compliant
1891 return compareAndSetReference(o, offset, valueType, expected, x);
1892 } else {
1893 return weakCompareAndSetReferenceAcquire(o, offset, expected, x);
1894 }
1895 }
1896
1897 @ForceInline
1898 public final <V> boolean weakCompareAndSetFlatValueAcquire(Object o, long offset,
1899 int layout,
1900 Class<?> valueType,
1901 V expected,
1902 V x) {
1903 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1904 }
1905
1906 @IntrinsicCandidate
1907 public final boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1908 Object expected,
1909 Object x) {
1910 return compareAndSetReference(o, offset, expected, x);
1911 }
1912
1913 public final <V> boolean weakCompareAndSetReferenceRelease(Object o, long offset,
1914 Class<?> valueType,
1915 V expected,
1916 V x) {
1917 if (isValueObject(valueType, expected)) {
1918 // Reusing a stronger operation for now, still compliant
1919 return compareAndSetReference(o, offset, valueType, expected, x);
1920 } else {
1921 return weakCompareAndSetReferenceRelease(o, offset, expected, x);
1922 }
1923 }
1924
1925 @ForceInline
1926 public final <V> boolean weakCompareAndSetFlatValueRelease(Object o, long offset,
1927 int layout,
1928 Class<?> valueType,
1929 V expected,
1930 V x) {
1931 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1932 }
1933
1934 @IntrinsicCandidate
1935 public final boolean weakCompareAndSetReference(Object o, long offset,
1936 Object expected,
1937 Object x) {
1938 return compareAndSetReference(o, offset, expected, x);
1939 }
1940
1941 public final <V> boolean weakCompareAndSetReference(Object o, long offset,
1942 Class<?> valueType,
1943 V expected,
1944 V x) {
1945 if (isValueObject(valueType, expected)) {
1946 // Reusing a stronger operation for now, still compliant
1947 return compareAndSetReference(o, offset, valueType, expected, x);
1948 } else {
1949 return weakCompareAndSetReference(o, offset, expected, x);
1950 }
1951 }
1952
1953 @ForceInline
1954 public final <V> boolean weakCompareAndSetFlatValue(Object o, long offset,
1955 int layout,
1956 Class<?> valueType,
1957 V expected,
1958 V x) {
1959 return compareAndSetFlatValue(o, offset, layout, valueType, expected, x);
1960 }
1961
1962 /**
1963 * Atomically updates Java variable to {@code x} if it is currently
1964 * holding {@code expected}.
1965 *
1966 * <p>This operation has memory semantics of a {@code volatile} read
1967 * and write. Corresponds to C11 atomic_compare_exchange_strong.
1968 *
1969 * @return {@code true} if successful
1970 */
1971 @IntrinsicCandidate
1972 public final native boolean compareAndSetInt(Object o, long offset,
1973 int expected,
1974 int x);
1975
1976 @IntrinsicCandidate
1977 public final native int compareAndExchangeInt(Object o, long offset,
1978 int expected,
1979 int x);
1980
1981 @IntrinsicCandidate
2554 public final boolean weakCompareAndSetLongRelease(Object o, long offset,
2555 long expected,
2556 long x) {
2557 return compareAndSetLong(o, offset, expected, x);
2558 }
2559
2560 @IntrinsicCandidate
2561 public final boolean weakCompareAndSetLong(Object o, long offset,
2562 long expected,
2563 long x) {
2564 return compareAndSetLong(o, offset, expected, x);
2565 }
2566
2567 /**
2568 * Fetches a reference value from a given Java variable, with volatile
2569 * load semantics. Otherwise identical to {@link #getReference(Object, long)}
2570 */
2571 @IntrinsicCandidate
2572 public native Object getReferenceVolatile(Object o, long offset);
2573
2574 @ForceInline
2575 public final <V> Object getFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType) {
2576 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2577 Object res = getFlatValue(o, offset, layout, valueType);
2578 fullFence();
2579 return res;
2580 }
2581
2582 /**
2583 * Stores a reference value into a given Java variable, with
2584 * volatile store semantics. Otherwise identical to {@link #putReference(Object, long, Object)}
2585 */
2586 @IntrinsicCandidate
2587 public native void putReferenceVolatile(Object o, long offset, Object x);
2588
2589 @ForceInline
2590 public final <V> void putFlatValueVolatile(Object o, long offset, int layout, Class<?> valueType, V x) {
2591 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2592 putFlatValueRelease(o, offset, layout, valueType, x);
2593 fullFence();
2594 }
2595
2596 /** Volatile version of {@link #getInt(Object, long)} */
2597 @IntrinsicCandidate
2598 public native int getIntVolatile(Object o, long offset);
2599
2600 /** Volatile version of {@link #putInt(Object, long, int)} */
2601 @IntrinsicCandidate
2602 public native void putIntVolatile(Object o, long offset, int x);
2603
2604 /** Volatile version of {@link #getBoolean(Object, long)} */
2605 @IntrinsicCandidate
2606 public native boolean getBooleanVolatile(Object o, long offset);
2607
2608 /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
2609 @IntrinsicCandidate
2610 public native void putBooleanVolatile(Object o, long offset, boolean x);
2611
2612 /** Volatile version of {@link #getByte(Object, long)} */
2613 @IntrinsicCandidate
2614 public native byte getByteVolatile(Object o, long offset);
2615
2648 /** Volatile version of {@link #putFloat(Object, long, float)} */
2649 @IntrinsicCandidate
2650 public native void putFloatVolatile(Object o, long offset, float x);
2651
2652 /** Volatile version of {@link #getDouble(Object, long)} */
2653 @IntrinsicCandidate
2654 public native double getDoubleVolatile(Object o, long offset);
2655
2656 /** Volatile version of {@link #putDouble(Object, long, double)} */
2657 @IntrinsicCandidate
2658 public native void putDoubleVolatile(Object o, long offset, double x);
2659
2660
2661
2662 /** Acquire version of {@link #getReferenceVolatile(Object, long)} */
2663 @IntrinsicCandidate
2664 public final Object getReferenceAcquire(Object o, long offset) {
2665 return getReferenceVolatile(o, offset);
2666 }
2667
2668 @ForceInline
2669 public final <V> Object getFlatValueAcquire(Object o, long offset, int layout, Class<?> valueType) {
2670 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2671 Object res = getFlatValue(o, offset, layout, valueType);
2672 loadFence();
2673 return res;
2674 }
2675
2676 /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
2677 @IntrinsicCandidate
2678 public final boolean getBooleanAcquire(Object o, long offset) {
2679 return getBooleanVolatile(o, offset);
2680 }
2681
2682 /** Acquire version of {@link #getByteVolatile(Object, long)} */
2683 @IntrinsicCandidate
2684 public final byte getByteAcquire(Object o, long offset) {
2685 return getByteVolatile(o, offset);
2686 }
2687
2688 /** Acquire version of {@link #getShortVolatile(Object, long)} */
2689 @IntrinsicCandidate
2690 public final short getShortAcquire(Object o, long offset) {
2691 return getShortVolatile(o, offset);
2692 }
2693
2694 /** Acquire version of {@link #getCharVolatile(Object, long)} */
2695 @IntrinsicCandidate
2720 public final double getDoubleAcquire(Object o, long offset) {
2721 return getDoubleVolatile(o, offset);
2722 }
2723
2724 /*
2725 * Versions of {@link #putReferenceVolatile(Object, long, Object)}
2726 * that do not guarantee immediate visibility of the store to
2727 * other threads. This method is generally only useful if the
2728 * underlying field is a Java volatile (or if an array cell, one
2729 * that is otherwise only accessed using volatile accesses).
2730 *
2731 * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
2732 */
2733
2734 /** Release version of {@link #putReferenceVolatile(Object, long, Object)} */
2735 @IntrinsicCandidate
2736 public final void putReferenceRelease(Object o, long offset, Object x) {
2737 putReferenceVolatile(o, offset, x);
2738 }
2739
2740 @ForceInline
2741 public final <V> void putFlatValueRelease(Object o, long offset, int layout, Class<?> valueType, V x) {
2742 // we translate using fences (see: https://gee.cs.oswego.edu/dl/html/j9mm.html)
2743 storeFence();
2744 putFlatValue(o, offset, layout, valueType, x);
2745 }
2746
2747 /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
2748 @IntrinsicCandidate
2749 public final void putBooleanRelease(Object o, long offset, boolean x) {
2750 putBooleanVolatile(o, offset, x);
2751 }
2752
2753 /** Release version of {@link #putByteVolatile(Object, long, byte)} */
2754 @IntrinsicCandidate
2755 public final void putByteRelease(Object o, long offset, byte x) {
2756 putByteVolatile(o, offset, x);
2757 }
2758
2759 /** Release version of {@link #putShortVolatile(Object, long, short)} */
2760 @IntrinsicCandidate
2761 public final void putShortRelease(Object o, long offset, short x) {
2762 putShortVolatile(o, offset, x);
2763 }
2764
2765 /** Release version of {@link #putCharVolatile(Object, long, char)} */
2766 @IntrinsicCandidate
2783 /** Release version of {@link #putLongVolatile(Object, long, long)} */
2784 @IntrinsicCandidate
2785 public final void putLongRelease(Object o, long offset, long x) {
2786 putLongVolatile(o, offset, x);
2787 }
2788
2789 /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
2790 @IntrinsicCandidate
2791 public final void putDoubleRelease(Object o, long offset, double x) {
2792 putDoubleVolatile(o, offset, x);
2793 }
2794
2795 // ------------------------------ Opaque --------------------------------------
2796
2797 /** Opaque version of {@link #getReferenceVolatile(Object, long)} */
2798 @IntrinsicCandidate
2799 public final Object getReferenceOpaque(Object o, long offset) {
2800 return getReferenceVolatile(o, offset);
2801 }
2802
2803 @ForceInline
2804 public final <V> Object getFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType) {
2805 // this is stronger than opaque semantics
2806 return getFlatValueAcquire(o, offset, layout, valueType);
2807 }
2808
2809 /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
2810 @IntrinsicCandidate
2811 public final boolean getBooleanOpaque(Object o, long offset) {
2812 return getBooleanVolatile(o, offset);
2813 }
2814
2815 /** Opaque version of {@link #getByteVolatile(Object, long)} */
2816 @IntrinsicCandidate
2817 public final byte getByteOpaque(Object o, long offset) {
2818 return getByteVolatile(o, offset);
2819 }
2820
2821 /** Opaque version of {@link #getShortVolatile(Object, long)} */
2822 @IntrinsicCandidate
2823 public final short getShortOpaque(Object o, long offset) {
2824 return getShortVolatile(o, offset);
2825 }
2826
2827 /** Opaque version of {@link #getCharVolatile(Object, long)} */
2828 @IntrinsicCandidate
2843 }
2844
2845 /** Opaque version of {@link #getLongVolatile(Object, long)} */
2846 @IntrinsicCandidate
2847 public final long getLongOpaque(Object o, long offset) {
2848 return getLongVolatile(o, offset);
2849 }
2850
2851 /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
2852 @IntrinsicCandidate
2853 public final double getDoubleOpaque(Object o, long offset) {
2854 return getDoubleVolatile(o, offset);
2855 }
2856
2857 /** Opaque version of {@link #putReferenceVolatile(Object, long, Object)} */
2858 @IntrinsicCandidate
2859 public final void putReferenceOpaque(Object o, long offset, Object x) {
2860 putReferenceVolatile(o, offset, x);
2861 }
2862
2863 @ForceInline
2864 public final <V> void putFlatValueOpaque(Object o, long offset, int layout, Class<?> valueType, V x) {
2865 // this is stronger than opaque semantics
2866 putFlatValueRelease(o, offset, layout, valueType, x);
2867 }
2868
2869 /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
2870 @IntrinsicCandidate
2871 public final void putBooleanOpaque(Object o, long offset, boolean x) {
2872 putBooleanVolatile(o, offset, x);
2873 }
2874
2875 /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
2876 @IntrinsicCandidate
2877 public final void putByteOpaque(Object o, long offset, byte x) {
2878 putByteVolatile(o, offset, x);
2879 }
2880
2881 /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
2882 @IntrinsicCandidate
2883 public final void putShortOpaque(Object o, long offset, short x) {
2884 putShortVolatile(o, offset, x);
2885 }
2886
2887 /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
2888 @IntrinsicCandidate
2897 }
2898
2899 /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
2900 @IntrinsicCandidate
2901 public final void putFloatOpaque(Object o, long offset, float x) {
2902 putFloatVolatile(o, offset, x);
2903 }
2904
2905 /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
2906 @IntrinsicCandidate
2907 public final void putLongOpaque(Object o, long offset, long x) {
2908 putLongVolatile(o, offset, x);
2909 }
2910
2911 /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
2912 @IntrinsicCandidate
2913 public final void putDoubleOpaque(Object o, long offset, double x) {
2914 putDoubleVolatile(o, offset, x);
2915 }
2916
2917 @ForceInline
2918 private boolean compareAndSetFlatValueAsBytes(Object[] array, Object o, long offset, int layout, Class<?> valueType, Object expected, Object x) {
2919 // We can convert between a value object and a binary value (of suitable size) using array elements.
2920 // This only works if the payload contains no oops (see VarHandles::isAtomicFlat).
2921 // Thus, we can implement the CAS with a plain numeric CAS.
2922
2923 // array[0]: witness (put as binary, get as object), at base
2924 // array[1]: x (put as object, get as binary), at base + scale
2925 // When witness == expected, the witness binary may be different from the expected binary.
2926 // This happens when compiler does not zero unused positions in the witness.
2927 // So we must obtain the witness binary and use it as expected binary for the numeric CAS.
2928 long base = arrayInstanceBaseOffset(array);
2929 int scale = arrayInstanceIndexScale(array);
2930 putFlatValue(array, base + scale, layout, valueType, x); // put x as object
2931 switch (scale) {
2932 case 1: {
2933 do {
2934 byte witnessByte = getByteVolatile(o, offset);
2935 putByte(array, base, witnessByte); // put witness as binary
2936 Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2937 if (witness != expected) {
2938 return false;
2939 }
2940 byte xByte = getByte(array, base + scale); // get x as binary
2941 if (compareAndSetByte(o, offset, witnessByte, xByte)) {
2942 return true;
2943 }
2944 } while (true);
2945 }
2946 case 2: {
2947 do {
2948 short witnessShort = getShortVolatile(o, offset);
2949 putShort(array, base, witnessShort); // put witness as binary
2950 Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2951 if (witness != expected) {
2952 return false;
2953 }
2954 short xShort = getShort(array, base + scale); // get x as binary
2955 if (compareAndSetShort(o, offset, witnessShort, xShort)) {
2956 return true;
2957 }
2958 } while (true);
2959 }
2960 case 4: {
2961 do {
2962 int witnessInt = getIntVolatile(o, offset);
2963 putInt(array, base, witnessInt); // put witness as binary
2964 Object witness = getFlatValue(array, base, layout, valueType); // get witness as object
2965 if (witness != expected) {
2966 return false;
2967 }
2968 int xInt = getInt(array, base + scale); // get x as binary
2969 if (compareAndSetInt(o, offset, witnessInt, xInt)) {
2970 return true;
2971 }
2972 } while (true);
2973 }
2974 case 8: {
2975 do {
2976 long witnessLong = getLongVolatile(o, offset);
2977 putLong(array, base, witnessLong); // put witness as binary
2978 Object witness = getFlatValue(array, base, layout, valueType);
2979 if (witness != expected) {
2980 return false;
2981 }
2982 long xLong = getLong(array, base + scale); // get x as binary
2983 if (compareAndSetLong(o, offset, witnessLong, xLong)) {
2984 return true;
2985 }
2986 } while (true);
2987 }
2988 default: {
2989 throw new UnsupportedOperationException();
2990 }
2991 }
2992 }
2993
2994 /**
2995 * Unblocks the given thread blocked on {@code park}, or, if it is
2996 * not blocked, causes the subsequent call to {@code park} not to
2997 * block. Note: this operation is "unsafe" solely because the
2998 * caller must somehow ensure that the thread has not been
2999 * destroyed. Nothing special is usually required to ensure this
3000 * when called from Java (in which there will ordinarily be a live
3001 * reference to the thread) but this is not nearly-automatically
3002 * so when calling from native code.
3003 *
3004 * @param thread the thread to unpark.
3005 */
3006 @IntrinsicCandidate
3007 public native void unpark(Object thread);
3008
3009 /**
3010 * Blocks current thread, returning when a balancing
3011 * {@code unpark} occurs, or a balancing {@code unpark} has
3012 * already occurred, or the thread is interrupted, or, if not
3013 * absolute and time is not zero, the given time nanoseconds have
3360 /**
3361 * Atomically exchanges the given reference value with the current
3362 * reference value of a field or array element within the given
3363 * object {@code o} at the given {@code offset}.
3364 *
3365 * @param o object/array to update the field/element in
3366 * @param offset field/element offset
3367 * @param newValue new value
3368 * @return the previous value
3369 * @since 1.8
3370 */
3371 @IntrinsicCandidate
3372 public final Object getAndSetReference(Object o, long offset, Object newValue) {
3373 Object v;
3374 do {
3375 v = getReferenceVolatile(o, offset);
3376 } while (!weakCompareAndSetReference(o, offset, v, newValue));
3377 return v;
3378 }
3379
3380 @ForceInline
3381 public final Object getAndSetReference(Object o, long offset, Class<?> valueType, Object newValue) {
3382 Object v;
3383 do {
3384 v = getReferenceVolatile(o, offset);
3385 } while (!compareAndSetReference(o, offset, valueType, v, newValue));
3386 return v;
3387 }
3388
3389 @ForceInline
3390 public Object getAndSetFlatValue(Object o, long offset, int layoutKind, Class<?> valueType, Object newValue) {
3391 Object v;
3392 do {
3393 v = getFlatValueVolatile(o, offset, layoutKind, valueType);
3394 } while (!compareAndSetFlatValue(o, offset, layoutKind, valueType, v, newValue));
3395 return v;
3396 }
3397
3398 @ForceInline
3399 public final Object getAndSetReferenceRelease(Object o, long offset, Object newValue) {
3400 Object v;
3401 do {
3402 v = getReference(o, offset);
3403 } while (!weakCompareAndSetReferenceRelease(o, offset, v, newValue));
3404 return v;
3405 }
3406
3407 @ForceInline
3408 public final Object getAndSetReferenceRelease(Object o, long offset, Class<?> valueType, Object newValue) {
3409 return getAndSetReference(o, offset, valueType, newValue);
3410 }
3411
3412 @ForceInline
3413 public Object getAndSetFlatValueRelease(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3414 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3415 }
3416
3417 @ForceInline
3418 public final Object getAndSetReferenceAcquire(Object o, long offset, Object newValue) {
3419 Object v;
3420 do {
3421 v = getReferenceAcquire(o, offset);
3422 } while (!weakCompareAndSetReferenceAcquire(o, offset, v, newValue));
3423 return v;
3424 }
3425
3426 @ForceInline
3427 public final Object getAndSetReferenceAcquire(Object o, long offset, Class<?> valueType, Object newValue) {
3428 return getAndSetReference(o, offset, valueType, newValue);
3429 }
3430
3431 @ForceInline
3432 public Object getAndSetFlatValueAcquire(Object o, long offset, int layoutKind, Class<?> valueType, Object x) {
3433 return getAndSetFlatValue(o, offset, layoutKind, valueType, x);
3434 }
3435
3436 @IntrinsicCandidate
3437 public final byte getAndSetByte(Object o, long offset, byte newValue) {
3438 byte v;
3439 do {
3440 v = getByteVolatile(o, offset);
3441 } while (!weakCompareAndSetByte(o, offset, v, newValue));
3442 return v;
3443 }
3444
3445 @ForceInline
3446 public final byte getAndSetByteRelease(Object o, long offset, byte newValue) {
3447 byte v;
3448 do {
3449 v = getByte(o, offset);
3450 } while (!weakCompareAndSetByteRelease(o, offset, v, newValue));
3451 return v;
3452 }
3453
3454 @ForceInline
3455 public final byte getAndSetByteAcquire(Object o, long offset, byte newValue) {
4471 private static short convEndian(boolean big, short n) { return big == BIG_ENDIAN ? n : Short.reverseBytes(n) ; }
4472 private static int convEndian(boolean big, int n) { return big == BIG_ENDIAN ? n : Integer.reverseBytes(n) ; }
4473 private static long convEndian(boolean big, long n) { return big == BIG_ENDIAN ? n : Long.reverseBytes(n) ; }
4474
4475
4476
4477 private native long allocateMemory0(long bytes);
4478 private native long reallocateMemory0(long address, long bytes);
4479 private native void freeMemory0(long address);
4480 @IntrinsicCandidate
4481 private native void setMemory0(Object o, long offset, long bytes, byte value);
4482 @IntrinsicCandidate
4483 private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4484 private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
4485 private native long objectFieldOffset0(Field f); // throws IAE
4486 private native long knownObjectFieldOffset0(Class<?> c, String name); // error code: -1 not found, -2 static
4487 private native long staticFieldOffset0(Field f); // throws IAE
4488 private native Object staticFieldBase0(Field f); // throws IAE
4489 private native boolean shouldBeInitialized0(Class<?> c);
4490 private native void ensureClassInitialized0(Class<?> c);
4491 private native void notifyStrictStaticAccess0(Class<?> c, long staticFieldOffset, boolean writing);
4492 private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetic
4493 @IntrinsicCandidate
4494 private native int arrayInstanceBaseOffset0(Object[] array);
4495 private native int arrayIndexScale0(Class<?> arrayClass);
4496 @IntrinsicCandidate
4497 private native int arrayInstanceIndexScale0(Object[] array);
4498 private native long getObjectSize0(Object o);
4499 private native int getLoadAverage0(double[] loadavg, int nelems);
4500 @IntrinsicCandidate
4501 private native int[] getFieldMap0(Class <?> c);
4502
4503
4504 /**
4505 * Invokes the given direct byte buffer's cleaner, if any.
4506 *
4507 * @param directBuffer a direct byte buffer
4508 * @throws NullPointerException if {@code directBuffer} is null
4509 * @throws IllegalArgumentException if {@code directBuffer} is non-direct,
4510 * or is a {@link java.nio.Buffer#slice slice}, or is a
4511 * {@link java.nio.Buffer#duplicate duplicate}
4512 */
4513 public void invokeCleaner(java.nio.ByteBuffer directBuffer) {
4514 if (!directBuffer.isDirect())
4515 throw new IllegalArgumentException("buffer is non-direct");
4516
4517 DirectBuffer db = (DirectBuffer) directBuffer;
4518 if (db.attachment() != null)
4519 throw new IllegalArgumentException("duplicate or slice");
4520
4521 Cleaner cleaner = db.cleaner();
|