1 /*
   2  * Copyright (c) 2000, 2016, 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 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!
  51  *
  52  * @author John R. Rose
  53  * @see #getUnsafe
  54  */
  55 
  56 public final class Unsafe {
  57 
  58     private static native void registerNatives();
  59     static {
  60         registerNatives();
  61         sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
  62     }
  63 
  64     private Unsafe() {}
  65 
  66     private static final Unsafe theUnsafe = new Unsafe();
  67 
  68     /**
  69      * Provides the caller with the capability of performing unsafe
  70      * operations.
  71      *
  72      * <p>The returned {@code Unsafe} object should be carefully guarded
  73      * by the caller, since it can be used to read and write data at arbitrary
  74      * memory addresses.  It must never be passed to untrusted code.
  75      *
  76      * <p>Most methods in this class are very low-level, and correspond to a
  77      * small number of hardware instructions (on typical machines).  Compilers
  78      * are encouraged to optimize these methods accordingly.
  79      *
  80      * <p>Here is a suggested idiom for using unsafe operations:
  81      *
  82      * <pre> {@code
  83      * class MyTrustedClass {
  84      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  85      *   ...
  86      *   private long myCountAddress = ...;
  87      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  88      * }}</pre>
  89      *
  90      * (It may assist compilers to make the local variable {@code final}.)
  91      *
  92      * @throws  SecurityException  if a security manager exists and its
  93      *          {@code checkPropertiesAccess} method doesn't allow
  94      *          access to the system properties.
  95      */
  96     @CallerSensitive
  97     public static Unsafe getUnsafe() {
  98         Class<?> caller = Reflection.getCallerClass();
  99         if (!VM.isSystemDomainLoader(caller.getClassLoader()))
 100             throw new SecurityException("Unsafe");
 101         return theUnsafe;
 102     }
 103 
 104     /// peek and poke operations
 105     /// (compilers should optimize these to memory ops)
 106 
 107     // These work on object fields in the Java heap.
 108     // They will not work on elements of packed arrays.
 109 
 110     /**
 111      * Fetches a value from a given Java variable.
 112      * More specifically, fetches a field or array element within the given
 113      * object {@code o} at the given offset, or (if {@code o} is null)
 114      * from the memory address whose numerical value is the given offset.
 115      * <p>
 116      * The results are undefined unless one of the following cases is true:
 117      * <ul>
 118      * <li>The offset was obtained from {@link #objectFieldOffset} on
 119      * the {@link java.lang.reflect.Field} of some Java field and the object
 120      * referred to by {@code o} is of a class compatible with that
 121      * field's class.
 122      *
 123      * <li>The offset and object reference {@code o} (either null or
 124      * non-null) were both obtained via {@link #staticFieldOffset}
 125      * and {@link #staticFieldBase} (respectively) from the
 126      * reflective {@link Field} representation of some Java field.
 127      *
 128      * <li>The object referred to by {@code o} is an array, and the offset
 129      * is an integer of the form {@code B+N*S}, where {@code N} is
 130      * a valid index into the array, and {@code B} and {@code S} are
 131      * the values obtained by {@link #arrayBaseOffset} and {@link
 132      * #arrayIndexScale} (respectively) from the array's class.  The value
 133      * referred to is the {@code N}<em>th</em> element of the array.
 134      *
 135      * </ul>
 136      * <p>
 137      * If one of the above cases is true, the call references a specific Java
 138      * variable (field or array element).  However, the results are undefined
 139      * if that variable is not in fact of the type returned by this method.
 140      * <p>
 141      * This method refers to a variable by means of two parameters, and so
 142      * it provides (in effect) a <em>double-register</em> addressing mode
 143      * for Java variables.  When the object reference is null, this method
 144      * uses its offset as an absolute address.  This is similar in operation
 145      * to methods such as {@link #getInt(long)}, which provide (in effect) a
 146      * <em>single-register</em> addressing mode for non-Java variables.
 147      * However, because Java variables may have a different layout in memory
 148      * from non-Java variables, programmers should not assume that these
 149      * two addressing modes are ever equivalent.  Also, programmers should
 150      * remember that offsets from the double-register addressing mode cannot
 151      * be portably confused with longs used in the single-register addressing
 152      * mode.
 153      *
 154      * @param o Java heap object in which the variable resides, if any, else
 155      *        null
 156      * @param offset indication of where the variable resides in a Java heap
 157      *        object, if any, else a memory address locating the variable
 158      *        statically
 159      * @return the value fetched from the indicated Java variable
 160      * @throws RuntimeException No defined exceptions are thrown, not even
 161      *         {@link NullPointerException}
 162      */
 163     @HotSpotIntrinsicCandidate
 164     public native int getInt(Object o, long offset);
 165 
 166     /**
 167      * Stores a value into a given Java variable.
 168      * <p>
 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     @HotSpotIntrinsicCandidate
 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     @HotSpotIntrinsicCandidate
 194     public native Object getObject(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     @HotSpotIntrinsicCandidate
 207     public native void putObject(Object o, long offset, Object x);
 208 
 209     /** @see #getInt(Object, long) */
 210     @HotSpotIntrinsicCandidate
 211     public native boolean getBoolean(Object o, long offset);
 212     /** @see #putInt(Object, long, int) */
 213     @HotSpotIntrinsicCandidate
 214     public native void    putBoolean(Object o, long offset, boolean x);
 215     /** @see #getInt(Object, long) */
 216     @HotSpotIntrinsicCandidate
 217     public native byte    getByte(Object o, long offset);
 218     /** @see #putInt(Object, long, int) */
 219     @HotSpotIntrinsicCandidate
 220     public native void    putByte(Object o, long offset, byte x);
 221     /** @see #getInt(Object, long) */
 222     @HotSpotIntrinsicCandidate
 223     public native short   getShort(Object o, long offset);
 224     /** @see #putInt(Object, long, int) */
 225     @HotSpotIntrinsicCandidate
 226     public native void    putShort(Object o, long offset, short x);
 227     /** @see #getInt(Object, long) */
 228     @HotSpotIntrinsicCandidate
 229     public native char    getChar(Object o, long offset);
 230     /** @see #putInt(Object, long, int) */
 231     @HotSpotIntrinsicCandidate
 232     public native void    putChar(Object o, long offset, char x);
 233     /** @see #getInt(Object, long) */
 234     @HotSpotIntrinsicCandidate
 235     public native long    getLong(Object o, long offset);
 236     /** @see #putInt(Object, long, int) */
 237     @HotSpotIntrinsicCandidate
 238     public native void    putLong(Object o, long offset, long x);
 239     /** @see #getInt(Object, long) */
 240     @HotSpotIntrinsicCandidate
 241     public native float   getFloat(Object o, long offset);
 242     /** @see #putInt(Object, long, int) */
 243     @HotSpotIntrinsicCandidate
 244     public native void    putFloat(Object o, long offset, float x);
 245     /** @see #getInt(Object, long) */
 246     @HotSpotIntrinsicCandidate
 247     public native double  getDouble(Object o, long offset);
 248     /** @see #putInt(Object, long, int) */
 249     @HotSpotIntrinsicCandidate
 250     public native void    putDouble(Object o, long offset, double x);
 251 
 252     // These read VM internal data.
 253 
 254     /**
 255      * Fetches an uncompressed reference value from a given native variable
 256      * ignoring the VM's compressed references mode.
 257      *
 258      * @param address a memory address locating the variable
 259      * @return the value fetched from the indicated native variable
 260      */
 261     public native Object getUncompressedObject(long address);
 262 
 263     /**
 264      * Fetches the {@link java.lang.Class} Java mirror for the given native
 265      * metaspace {@code Klass} pointer.
 266      *
 267      * @param metaspaceKlass a native metaspace {@code Klass} pointer
 268      * @return the {@link java.lang.Class} Java mirror
 269      */
 270     public native Class<?> getJavaMirror(long metaspaceKlass);
 271 
 272     /**
 273      * Fetches a native metaspace {@code Klass} pointer for the given Java
 274      * object.
 275      *
 276      * @param o Java heap object for which to fetch the class pointer
 277      * @return a native metaspace {@code Klass} pointer
 278      */
 279     public native long getKlassPointer(Object o);
 280 
 281     // These work on values in the C heap.
 282 
 283     /**
 284      * Fetches a value from a given memory address.  If the address is zero, or
 285      * does not point into a block obtained from {@link #allocateMemory}, the
 286      * results are undefined.
 287      *
 288      * @see #allocateMemory
 289      */
 290     @HotSpotIntrinsicCandidate
 291     public native byte    getByte(long address);
 292 
 293     /**
 294      * Stores a value into a given memory address.  If the address is zero, or
 295      * does not point into a block obtained from {@link #allocateMemory}, the
 296      * results are undefined.
 297      *
 298      * @see #getByte(long)
 299      */
 300     @HotSpotIntrinsicCandidate
 301     public native void    putByte(long address, byte x);
 302 
 303     /** @see #getByte(long) */
 304     @HotSpotIntrinsicCandidate
 305     public native short   getShort(long address);
 306     /** @see #putByte(long, byte) */
 307     @HotSpotIntrinsicCandidate
 308     public native void    putShort(long address, short x);
 309     /** @see #getByte(long) */
 310     @HotSpotIntrinsicCandidate
 311     public native char    getChar(long address);
 312     /** @see #putByte(long, byte) */
 313     @HotSpotIntrinsicCandidate
 314     public native void    putChar(long address, char x);
 315     /** @see #getByte(long) */
 316     @HotSpotIntrinsicCandidate
 317     public native int     getInt(long address);
 318     /** @see #putByte(long, byte) */
 319     @HotSpotIntrinsicCandidate
 320     public native void    putInt(long address, int x);
 321     /** @see #getByte(long) */
 322     @HotSpotIntrinsicCandidate
 323     public native long    getLong(long address);
 324     /** @see #putByte(long, byte) */
 325     @HotSpotIntrinsicCandidate
 326     public native void    putLong(long address, long x);
 327     /** @see #getByte(long) */
 328     @HotSpotIntrinsicCandidate
 329     public native float   getFloat(long address);
 330     /** @see #putByte(long, byte) */
 331     @HotSpotIntrinsicCandidate
 332     public native void    putFloat(long address, float x);
 333     /** @see #getByte(long) */
 334     @HotSpotIntrinsicCandidate
 335     public native double  getDouble(long address);
 336     /** @see #putByte(long, byte) */
 337     @HotSpotIntrinsicCandidate
 338     public native void    putDouble(long address, double x);
 339 
 340     /**
 341      * Fetches a native pointer from a given memory address.  If the address is
 342      * zero, or does not point into a block obtained from {@link
 343      * #allocateMemory}, the results are undefined.
 344      *
 345      * <p>If the native pointer is less than 64 bits wide, it is extended as
 346      * an unsigned number to a Java long.  The pointer may be indexed by any
 347      * given byte offset, simply by adding that offset (as a simple integer) to
 348      * the long representing the pointer.  The number of bytes actually read
 349      * from the target address may be determined by consulting {@link
 350      * #addressSize}.
 351      *
 352      * @see #allocateMemory
 353      */
 354     @HotSpotIntrinsicCandidate
 355     public native long getAddress(long address);
 356 
 357     /**
 358      * Stores a native pointer into a given memory address.  If the address is
 359      * zero, or does not point into a block obtained from {@link
 360      * #allocateMemory}, the results are undefined.
 361      *
 362      * <p>The number of bytes actually written at the target address may be
 363      * determined by consulting {@link #addressSize}.
 364      *
 365      * @see #getAddress(long)
 366      */
 367     @HotSpotIntrinsicCandidate
 368     public native void putAddress(long address, long x);
 369 
 370 
 371 
 372     /// helper methods for validating various types of objects/values
 373 
 374     /**
 375      * Create an exception reflecting that some of the input was invalid
 376      *
 377      * <em>Note:</em> It is the resposibility of the caller to make
 378      * sure arguments are checked before the methods are called. While
 379      * some rudimentary checks are performed on the input, the checks
 380      * are best effort and when performance is an overriding priority,
 381      * as when methods of this class are optimized by the runtime
 382      * compiler, some or all checks (if any) may be elided. Hence, the
 383      * caller must not rely on the checks and corresponding
 384      * exceptions!
 385      *
 386      * @return an exception object
 387      */
 388     private RuntimeException invalidInput() {
 389         return new IllegalArgumentException();
 390     }
 391 
 392     /**
 393      * Check if a value is 32-bit clean (32 MSB are all zero)
 394      *
 395      * @param value the 64-bit value to check
 396      *
 397      * @return true if the value is 32-bit clean
 398      */
 399     private boolean is32BitClean(long value) {
 400         return value >>> 32 == 0;
 401     }
 402 
 403     /**
 404      * Check the validity of a size (the equivalent of a size_t)
 405      *
 406      * @throws RuntimeException if the size is invalid
 407      *         (<em>Note:</em> after optimization, invalid inputs may
 408      *         go undetected, which will lead to unpredictable
 409      *         behavior)
 410      */
 411     private void checkSize(long size) {
 412         if (ADDRESS_SIZE == 4) {
 413             // Note: this will also check for negative sizes
 414             if (!is32BitClean(size)) {
 415                 throw invalidInput();
 416             }
 417         } else if (size < 0) {
 418             throw invalidInput();
 419         }
 420     }
 421 
 422     /**
 423      * Check the validity of a native address (the equivalent of void*)
 424      *
 425      * @throws RuntimeException if the address is invalid
 426      *         (<em>Note:</em> after optimization, invalid inputs may
 427      *         go undetected, which will lead to unpredictable
 428      *         behavior)
 429      */
 430     private void checkNativeAddress(long address) {
 431         if (ADDRESS_SIZE == 4) {
 432             // Accept both zero and sign extended pointers. A valid
 433             // pointer will, after the +1 below, either have produced
 434             // the value 0x0 or 0x1. Masking off the low bit allows
 435             // for testing against 0.
 436             if ((((address >> 32) + 1) & ~1) != 0) {
 437                 throw invalidInput();
 438             }
 439         }
 440     }
 441 
 442     /**
 443      * Check the validity of an offset, relative to a base object
 444      *
 445      * @param o the base object
 446      * @param offset the offset to check
 447      *
 448      * @throws RuntimeException if the size is invalid
 449      *         (<em>Note:</em> after optimization, invalid inputs may
 450      *         go undetected, which will lead to unpredictable
 451      *         behavior)
 452      */
 453     private void checkOffset(Object o, long offset) {
 454         if (ADDRESS_SIZE == 4) {
 455             // Note: this will also check for negative offsets
 456             if (!is32BitClean(offset)) {
 457                 throw invalidInput();
 458             }
 459         } else if (offset < 0) {
 460             throw invalidInput();
 461         }
 462     }
 463 
 464     /**
 465      * Check the validity of a double-register pointer
 466      *
 467      * Note: This code deliberately does *not* check for NPE for (at
 468      * least) three reasons:
 469      *
 470      * 1) NPE is not just NULL/0 - there is a range of values all
 471      * resulting in an NPE, which is not trivial to check for
 472      *
 473      * 2) It is the responsibility of the callers of Unsafe methods
 474      * to verify the input, so throwing an exception here is not really
 475      * useful - passing in a NULL pointer is a critical error and the
 476      * must not expect an exception to be thrown anyway.
 477      *
 478      * 3) the actual operations will detect NULL pointers anyway by
 479      * means of traps and signals (like SIGSEGV).
 480      *
 481      * @param o Java heap object, or null
 482      * @param offset indication of where the variable resides in a Java heap
 483      *        object, if any, else a memory address locating the variable
 484      *        statically
 485      *
 486      * @throws RuntimeException if the pointer is invalid
 487      *         (<em>Note:</em> after optimization, invalid inputs may
 488      *         go undetected, which will lead to unpredictable
 489      *         behavior)
 490      */
 491     private void checkPointer(Object o, long offset) {
 492         if (o == null) {
 493             checkNativeAddress(offset);
 494         } else {
 495             checkOffset(o, offset);
 496         }
 497     }
 498 
 499     /**
 500      * Check if a type is a primitive array type
 501      *
 502      * @param c the type to check
 503      *
 504      * @return true if the type is a primitive array type
 505      */
 506     private void checkPrimitiveArray(Class<?> c) {
 507         Class<?> componentType = c.getComponentType();
 508         if (componentType == null || !componentType.isPrimitive()) {
 509             throw invalidInput();
 510         }
 511     }
 512 
 513     /**
 514      * Check that a pointer is a valid primitive array type pointer
 515      *
 516      * Note: pointers off-heap are considered to be primitive arrays
 517      *
 518      * @throws RuntimeException if the pointer is invalid
 519      *         (<em>Note:</em> after optimization, invalid inputs may
 520      *         go undetected, which will lead to unpredictable
 521      *         behavior)
 522      */
 523     private void checkPrimitivePointer(Object o, long offset) {
 524         checkPointer(o, offset);
 525 
 526         if (o != null) {
 527             // If on heap, it it must be a primitive array
 528             checkPrimitiveArray(o.getClass());
 529         }
 530     }
 531 
 532 
 533     /// wrappers for malloc, realloc, free:
 534 
 535     /**
 536      * Allocates a new block of native memory, of the given size in bytes.  The
 537      * contents of the memory are uninitialized; they will generally be
 538      * garbage.  The resulting native pointer will never be zero, and will be
 539      * aligned for all value types.  Dispose of this memory by calling {@link
 540      * #freeMemory}, or resize it with {@link #reallocateMemory}.
 541      *
 542      * <em>Note:</em> It is the resposibility of the caller to make
 543      * sure arguments are checked before the methods are called. While
 544      * some rudimentary checks are performed on the input, the checks
 545      * are best effort and when performance is an overriding priority,
 546      * as when methods of this class are optimized by the runtime
 547      * compiler, some or all checks (if any) may be elided. Hence, the
 548      * caller must not rely on the checks and corresponding
 549      * exceptions!
 550      *
 551      * @throws RuntimeException if the size is negative or too large
 552      *         for the native size_t type
 553      *
 554      * @throws OutOfMemoryError if the allocation is refused by the system
 555      *
 556      * @see #getByte(long)
 557      * @see #putByte(long, byte)
 558      */
 559     public long allocateMemory(long bytes) {
 560         allocateMemoryChecks(bytes);
 561 
 562         if (bytes == 0) {
 563             return 0;
 564         }
 565 
 566         long p = allocateMemory0(bytes);
 567         if (p == 0) {
 568             throw new OutOfMemoryError();
 569         }
 570 
 571         return p;
 572     }
 573 
 574     /**
 575      * Validate the arguments to allocateMemory
 576      *
 577      * @throws RuntimeException if the arguments are invalid
 578      *         (<em>Note:</em> after optimization, invalid inputs may
 579      *         go undetected, which will lead to unpredictable
 580      *         behavior)
 581      */
 582     private void allocateMemoryChecks(long bytes) {
 583         checkSize(bytes);
 584     }
 585 
 586     /**
 587      * Resizes a new block of native memory, to the given size in bytes.  The
 588      * contents of the new block past the size of the old block are
 589      * uninitialized; they will generally be garbage.  The resulting native
 590      * pointer will be zero if and only if the requested size is zero.  The
 591      * resulting native pointer will be aligned for all value types.  Dispose
 592      * of this memory by calling {@link #freeMemory}, or resize it with {@link
 593      * #reallocateMemory}.  The address passed to this method may be null, in
 594      * which case an allocation will be performed.
 595      *
 596      * <em>Note:</em> It is the resposibility of the caller to make
 597      * sure arguments are checked before the methods are called. While
 598      * some rudimentary checks are performed on the input, the checks
 599      * are best effort and when performance is an overriding priority,
 600      * as when methods of this class are optimized by the runtime
 601      * compiler, some or all checks (if any) may be elided. Hence, the
 602      * caller must not rely on the checks and corresponding
 603      * exceptions!
 604      *
 605      * @throws RuntimeException if the size is negative or too large
 606      *         for the native size_t type
 607      *
 608      * @throws OutOfMemoryError if the allocation is refused by the system
 609      *
 610      * @see #allocateMemory
 611      */
 612     public long reallocateMemory(long address, long bytes) {
 613         reallocateMemoryChecks(address, bytes);
 614 
 615         if (bytes == 0) {
 616             freeMemory(address);
 617             return 0;
 618         }
 619 
 620         long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
 621         if (p == 0) {
 622             throw new OutOfMemoryError();
 623         }
 624 
 625         return p;
 626     }
 627 
 628     /**
 629      * Validate the arguments to reallocateMemory
 630      *
 631      * @throws RuntimeException if the arguments are invalid
 632      *         (<em>Note:</em> after optimization, invalid inputs may
 633      *         go undetected, which will lead to unpredictable
 634      *         behavior)
 635      */
 636     private void reallocateMemoryChecks(long address, long bytes) {
 637         checkPointer(null, address);
 638         checkSize(bytes);
 639     }
 640 
 641     /**
 642      * Sets all bytes in a given block of memory to a fixed value
 643      * (usually zero).
 644      *
 645      * <p>This method determines a block's base address by means of two parameters,
 646      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 647      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 648      * the offset supplies an absolute base address.
 649      *
 650      * <p>The stores are in coherent (atomic) units of a size determined
 651      * by the address and length parameters.  If the effective address and
 652      * length are all even modulo 8, the stores take place in 'long' units.
 653      * If the effective address and length are (resp.) even modulo 4 or 2,
 654      * the stores take place in units of 'int' or 'short'.
 655      *
 656      * <em>Note:</em> It is the resposibility of the caller to make
 657      * sure arguments are checked before the methods are called. While
 658      * some rudimentary checks are performed on the input, the checks
 659      * are best effort and when performance is an overriding priority,
 660      * as when methods of this class are optimized by the runtime
 661      * compiler, some or all checks (if any) may be elided. Hence, the
 662      * caller must not rely on the checks and corresponding
 663      * exceptions!
 664      *
 665      * @throws RuntimeException if any of the arguments is invalid
 666      *
 667      * @since 1.7
 668      */
 669     public void setMemory(Object o, long offset, long bytes, byte value) {
 670         setMemoryChecks(o, offset, bytes, value);
 671 
 672         if (bytes == 0) {
 673             return;
 674         }
 675 
 676         setMemory0(o, offset, bytes, value);
 677     }
 678 
 679     /**
 680      * Sets all bytes in a given block of memory to a fixed value
 681      * (usually zero).  This provides a <em>single-register</em> addressing mode,
 682      * as discussed in {@link #getInt(Object,long)}.
 683      *
 684      * <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
 685      */
 686     public void setMemory(long address, long bytes, byte value) {
 687         setMemory(null, address, bytes, value);
 688     }
 689 
 690     /**
 691      * Validate the arguments to setMemory
 692      *
 693      * @throws RuntimeException if the arguments are invalid
 694      *         (<em>Note:</em> after optimization, invalid inputs may
 695      *         go undetected, which will lead to unpredictable
 696      *         behavior)
 697      */
 698     private void setMemoryChecks(Object o, long offset, long bytes, byte value) {
 699         checkPrimitivePointer(o, offset);
 700         checkSize(bytes);
 701     }
 702 
 703     /**
 704      * Sets all bytes in a given block of memory to a copy of another
 705      * block.
 706      *
 707      * <p>This method determines each block's base address by means of two parameters,
 708      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 709      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 710      * the offset supplies an absolute base address.
 711      *
 712      * <p>The transfers are in coherent (atomic) units of a size determined
 713      * by the address and length parameters.  If the effective addresses and
 714      * length are all even modulo 8, the transfer takes place in 'long' units.
 715      * If the effective addresses and length are (resp.) even modulo 4 or 2,
 716      * the transfer takes place in units of 'int' or 'short'.
 717      *
 718      * <em>Note:</em> It is the resposibility of the caller to make
 719      * sure arguments are checked before the methods are called. While
 720      * some rudimentary checks are performed on the input, the checks
 721      * are best effort and when performance is an overriding priority,
 722      * as when methods of this class are optimized by the runtime
 723      * compiler, some or all checks (if any) may be elided. Hence, the
 724      * caller must not rely on the checks and corresponding
 725      * exceptions!
 726      *
 727      * @throws RuntimeException if any of the arguments is invalid
 728      *
 729      * @since 1.7
 730      */
 731     public void copyMemory(Object srcBase, long srcOffset,
 732                            Object destBase, long destOffset,
 733                            long bytes) {
 734         copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes);
 735 
 736         if (bytes == 0) {
 737             return;
 738         }
 739 
 740         copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes);
 741     }
 742 
 743     /**
 744      * Sets all bytes in a given block of memory to a copy of another
 745      * block.  This provides a <em>single-register</em> addressing mode,
 746      * as discussed in {@link #getInt(Object,long)}.
 747      *
 748      * Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
 749      */
 750     public void copyMemory(long srcAddress, long destAddress, long bytes) {
 751         copyMemory(null, srcAddress, null, destAddress, bytes);
 752     }
 753 
 754     /**
 755      * Validate the arguments to copyMemory
 756      *
 757      * @throws RuntimeException if any of the arguments is invalid
 758      *         (<em>Note:</em> after optimization, invalid inputs may
 759      *         go undetected, which will lead to unpredictable
 760      *         behavior)
 761      */
 762     private void copyMemoryChecks(Object srcBase, long srcOffset,
 763                                   Object destBase, long destOffset,
 764                                   long bytes) {
 765         checkSize(bytes);
 766         checkPrimitivePointer(srcBase, srcOffset);
 767         checkPrimitivePointer(destBase, destOffset);
 768     }
 769 
 770     /**
 771      * Copies all elements from one block of memory to another block,
 772      * *unconditionally* byte swapping the elements on the fly.
 773      *
 774      * <p>This method determines each block's base address by means of two parameters,
 775      * and so it provides (in effect) a <em>double-register</em> addressing mode,
 776      * as discussed in {@link #getInt(Object,long)}.  When the object reference is null,
 777      * the offset supplies an absolute base address.
 778      *
 779      * <em>Note:</em> It is the resposibility of the caller to make
 780      * sure arguments are checked before the methods are called. While
 781      * some rudimentary checks are performed on the input, the checks
 782      * are best effort and when performance is an overriding priority,
 783      * as when methods of this class are optimized by the runtime
 784      * compiler, some or all checks (if any) may be elided. Hence, the
 785      * caller must not rely on the checks and corresponding
 786      * exceptions!
 787      *
 788      * @throws RuntimeException if any of the arguments is invalid
 789      *
 790      * @since 9
 791      */
 792     public void copySwapMemory(Object srcBase, long srcOffset,
 793                                Object destBase, long destOffset,
 794                                long bytes, long elemSize) {
 795         copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
 796 
 797         if (bytes == 0) {
 798             return;
 799         }
 800 
 801         copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
 802     }
 803 
 804     private void copySwapMemoryChecks(Object srcBase, long srcOffset,
 805                                       Object destBase, long destOffset,
 806                                       long bytes, long elemSize) {
 807         checkSize(bytes);
 808 
 809         if (elemSize != 2 && elemSize != 4 && elemSize != 8) {
 810             throw invalidInput();
 811         }
 812         if (bytes % elemSize != 0) {
 813             throw invalidInput();
 814         }
 815 
 816         checkPrimitivePointer(srcBase, srcOffset);
 817         checkPrimitivePointer(destBase, destOffset);
 818     }
 819 
 820    /**
 821      * Copies all elements from one block of memory to another block, byte swapping the
 822      * elements on the fly.
 823      *
 824      * This provides a <em>single-register</em> addressing mode, as
 825      * discussed in {@link #getInt(Object,long)}.
 826      *
 827      * Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}.
 828      */
 829     public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) {
 830         copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize);
 831     }
 832 
 833     /**
 834      * Disposes of a block of native memory, as obtained from {@link
 835      * #allocateMemory} or {@link #reallocateMemory}.  The address passed to
 836      * this method may be null, in which case no action is taken.
 837      *
 838      * <em>Note:</em> It is the resposibility of the caller to make
 839      * sure arguments are checked before the methods are called. While
 840      * some rudimentary checks are performed on the input, the checks
 841      * are best effort and when performance is an overriding priority,
 842      * as when methods of this class are optimized by the runtime
 843      * compiler, some or all checks (if any) may be elided. Hence, the
 844      * caller must not rely on the checks and corresponding
 845      * exceptions!
 846      *
 847      * @throws RuntimeException if any of the arguments is invalid
 848      *
 849      * @see #allocateMemory
 850      */
 851     public void freeMemory(long address) {
 852         freeMemoryChecks(address);
 853 
 854         if (address == 0) {
 855             return;
 856         }
 857 
 858         freeMemory0(address);
 859     }
 860 
 861     /**
 862      * Validate the arguments to freeMemory
 863      *
 864      * @throws RuntimeException if the arguments are invalid
 865      *         (<em>Note:</em> after optimization, invalid inputs may
 866      *         go undetected, which will lead to unpredictable
 867      *         behavior)
 868      */
 869     private void freeMemoryChecks(long address) {
 870         checkPointer(null, address);
 871     }
 872 
 873     /// random queries
 874 
 875     /**
 876      * This constant differs from all results that will ever be returned from
 877      * {@link #staticFieldOffset}, {@link #objectFieldOffset},
 878      * or {@link #arrayBaseOffset}.
 879      */
 880     public static final int INVALID_FIELD_OFFSET = -1;
 881 
 882     /**
 883      * Reports the location of a given field in the storage allocation of its
 884      * class.  Do not expect to perform any sort of arithmetic on this offset;
 885      * it is just a cookie which is passed to the unsafe heap memory accessors.
 886      *
 887      * <p>Any given field will always have the same offset and base, and no
 888      * two distinct fields of the same class will ever have the same offset
 889      * and base.
 890      *
 891      * <p>As of 1.4.1, offsets for fields are represented as long values,
 892      * although the Sun JVM does not use the most significant 32 bits.
 893      * However, JVM implementations which store static fields at absolute
 894      * addresses can use long offsets and null base pointers to express
 895      * the field locations in a form usable by {@link #getInt(Object,long)}.
 896      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 897      * must preserve all bits of static field offsets.
 898      * @see #getInt(Object, long)
 899      */
 900     public long objectFieldOffset(Field f) {
 901         if (f == null) {
 902             throw new NullPointerException();
 903         }
 904 
 905         return objectFieldOffset0(f);
 906     }
 907 
 908     /**
 909      * Reports the location of a given static field, in conjunction with {@link
 910      * #staticFieldBase}.
 911      * <p>Do not expect to perform any sort of arithmetic on this offset;
 912      * it is just a cookie which is passed to the unsafe heap memory accessors.
 913      *
 914      * <p>Any given field will always have the same offset, and no two distinct
 915      * fields of the same class will ever have the same offset.
 916      *
 917      * <p>As of 1.4.1, offsets for fields are represented as long values,
 918      * although the Sun JVM does not use the most significant 32 bits.
 919      * It is hard to imagine a JVM technology which needs more than
 920      * a few bits to encode an offset within a non-array object,
 921      * However, for consistency with other methods in this class,
 922      * this method reports its result as a long value.
 923      * @see #getInt(Object, long)
 924      */
 925     public long staticFieldOffset(Field f) {
 926         if (f == null) {
 927             throw new NullPointerException();
 928         }
 929 
 930         return staticFieldOffset0(f);
 931     }
 932 
 933     /**
 934      * Reports the location of a given static field, in conjunction with {@link
 935      * #staticFieldOffset}.
 936      * <p>Fetch the base "Object", if any, with which static fields of the
 937      * given class can be accessed via methods like {@link #getInt(Object,
 938      * long)}.  This value may be null.  This value may refer to an object
 939      * which is a "cookie", not guaranteed to be a real Object, and it should
 940      * not be used in any way except as argument to the get and put routines in
 941      * this class.
 942      */
 943     public Object staticFieldBase(Field f) {
 944         if (f == null) {
 945             throw new NullPointerException();
 946         }
 947 
 948         return staticFieldBase0(f);
 949     }
 950 
 951     /**
 952      * Detects if the given class may need to be initialized. This is often
 953      * needed in conjunction with obtaining the static field base of a
 954      * class.
 955      * @return false only if a call to {@code ensureClassInitialized} would have no effect
 956      */
 957     public boolean shouldBeInitialized(Class<?> c) {
 958         if (c == null) {
 959             throw new NullPointerException();
 960         }
 961 
 962         return shouldBeInitialized0(c);
 963     }
 964 
 965     /**
 966      * Ensures the given class has been initialized. This is often
 967      * needed in conjunction with obtaining the static field base of a
 968      * class.
 969      */
 970     public void ensureClassInitialized(Class<?> c) {
 971         if (c == null) {
 972             throw new NullPointerException();
 973         }
 974 
 975         ensureClassInitialized0(c);
 976     }
 977 
 978     /**
 979      * Reports the offset of the first element in the storage allocation of a
 980      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 981      * for the same class, you may use that scale factor, together with this
 982      * base offset, to form new offsets to access elements of arrays of the
 983      * given class.
 984      *
 985      * @see #getInt(Object, long)
 986      * @see #putInt(Object, long, int)
 987      */
 988     public int arrayBaseOffset(Class<?> arrayClass) {
 989         if (arrayClass == null) {
 990             throw new NullPointerException();
 991         }
 992 
 993         return arrayBaseOffset0(arrayClass);
 994     }
 995 
 996 
 997     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 998     public static final int ARRAY_BOOLEAN_BASE_OFFSET
 999             = theUnsafe.arrayBaseOffset(boolean[].class);
1000 
1001     /** The value of {@code arrayBaseOffset(byte[].class)} */
1002     public static final int ARRAY_BYTE_BASE_OFFSET
1003             = theUnsafe.arrayBaseOffset(byte[].class);
1004 
1005     /** The value of {@code arrayBaseOffset(short[].class)} */
1006     public static final int ARRAY_SHORT_BASE_OFFSET
1007             = theUnsafe.arrayBaseOffset(short[].class);
1008 
1009     /** The value of {@code arrayBaseOffset(char[].class)} */
1010     public static final int ARRAY_CHAR_BASE_OFFSET
1011             = theUnsafe.arrayBaseOffset(char[].class);
1012 
1013     /** The value of {@code arrayBaseOffset(int[].class)} */
1014     public static final int ARRAY_INT_BASE_OFFSET
1015             = theUnsafe.arrayBaseOffset(int[].class);
1016 
1017     /** The value of {@code arrayBaseOffset(long[].class)} */
1018     public static final int ARRAY_LONG_BASE_OFFSET
1019             = theUnsafe.arrayBaseOffset(long[].class);
1020 
1021     /** The value of {@code arrayBaseOffset(float[].class)} */
1022     public static final int ARRAY_FLOAT_BASE_OFFSET
1023             = theUnsafe.arrayBaseOffset(float[].class);
1024 
1025     /** The value of {@code arrayBaseOffset(double[].class)} */
1026     public static final int ARRAY_DOUBLE_BASE_OFFSET
1027             = theUnsafe.arrayBaseOffset(double[].class);
1028 
1029     /** The value of {@code arrayBaseOffset(Object[].class)} */
1030     public static final int ARRAY_OBJECT_BASE_OFFSET
1031             = theUnsafe.arrayBaseOffset(Object[].class);
1032 
1033     /**
1034      * Reports the scale factor for addressing elements in the storage
1035      * allocation of a given array class.  However, arrays of "narrow" types
1036      * will generally not work properly with accessors like {@link
1037      * #getByte(Object, long)}, so the scale factor for such classes is reported
1038      * as zero.
1039      *
1040      * @see #arrayBaseOffset
1041      * @see #getInt(Object, long)
1042      * @see #putInt(Object, long, int)
1043      */
1044     public int arrayIndexScale(Class<?> arrayClass) {
1045         if (arrayClass == null) {
1046             throw new NullPointerException();
1047         }
1048 
1049         return arrayIndexScale0(arrayClass);
1050     }
1051 
1052 
1053     /** The value of {@code arrayIndexScale(boolean[].class)} */
1054     public static final int ARRAY_BOOLEAN_INDEX_SCALE
1055             = theUnsafe.arrayIndexScale(boolean[].class);
1056 
1057     /** The value of {@code arrayIndexScale(byte[].class)} */
1058     public static final int ARRAY_BYTE_INDEX_SCALE
1059             = theUnsafe.arrayIndexScale(byte[].class);
1060 
1061     /** The value of {@code arrayIndexScale(short[].class)} */
1062     public static final int ARRAY_SHORT_INDEX_SCALE
1063             = theUnsafe.arrayIndexScale(short[].class);
1064 
1065     /** The value of {@code arrayIndexScale(char[].class)} */
1066     public static final int ARRAY_CHAR_INDEX_SCALE
1067             = theUnsafe.arrayIndexScale(char[].class);
1068 
1069     /** The value of {@code arrayIndexScale(int[].class)} */
1070     public static final int ARRAY_INT_INDEX_SCALE
1071             = theUnsafe.arrayIndexScale(int[].class);
1072 
1073     /** The value of {@code arrayIndexScale(long[].class)} */
1074     public static final int ARRAY_LONG_INDEX_SCALE
1075             = theUnsafe.arrayIndexScale(long[].class);
1076 
1077     /** The value of {@code arrayIndexScale(float[].class)} */
1078     public static final int ARRAY_FLOAT_INDEX_SCALE
1079             = theUnsafe.arrayIndexScale(float[].class);
1080 
1081     /** The value of {@code arrayIndexScale(double[].class)} */
1082     public static final int ARRAY_DOUBLE_INDEX_SCALE
1083             = theUnsafe.arrayIndexScale(double[].class);
1084 
1085     /** The value of {@code arrayIndexScale(Object[].class)} */
1086     public static final int ARRAY_OBJECT_INDEX_SCALE
1087             = theUnsafe.arrayIndexScale(Object[].class);
1088 
1089     /**
1090      * Reports the size in bytes of a native pointer, as stored via {@link
1091      * #putAddress}.  This value will be either 4 or 8.  Note that the sizes of
1092      * other primitive types (as stored in native memory blocks) is determined
1093      * fully by their information content.
1094      */
1095     public int addressSize() {
1096         return ADDRESS_SIZE;
1097     }
1098 
1099     /** The value of {@code addressSize()} */
1100     public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
1101 
1102     /**
1103      * Reports the size in bytes of a native memory page (whatever that is).
1104      * This value will always be a power of two.
1105      */
1106     public native int pageSize();
1107 
1108 
1109     /// random trusted operations from JNI:
1110 
1111     /**
1112      * Tells the VM to define a class, without security checks.  By default, the
1113      * class loader and protection domain come from the caller's class.
1114      */
1115     public Class<?> defineClass(String name, byte[] b, int off, int len,
1116                                 ClassLoader loader,
1117                                 ProtectionDomain protectionDomain) {
1118         if (b == null) {
1119             throw new NullPointerException();
1120         }
1121         if (len < 0) {
1122             throw new ArrayIndexOutOfBoundsException();
1123         }
1124 
1125         return defineClass0(name, b, off, len, loader, protectionDomain);
1126     }
1127 
1128     public native Class<?> defineClass0(String name, byte[] b, int off, int len,
1129                                         ClassLoader loader,
1130                                         ProtectionDomain protectionDomain);
1131 
1132     /**
1133      * Defines a class but does not make it known to the class loader or system dictionary.
1134      * <p>
1135      * For each CP entry, the corresponding CP patch must either be null or have
1136      * the a format that matches its tag:
1137      * <ul>
1138      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
1139      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
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.
1183      *
1184      * @param componentType array component type to allocate
1185      * @param length array size to allocate
1186      * @throws IllegalArgumentException if component type is null, or not a primitive class;
1187      *                                  or the length is negative
1188      */
1189     public Object allocateUninitializedArray(Class<?> componentType, int length) {
1190        if (componentType == null) {
1191            throw new IllegalArgumentException("Component type is null");
1192        }
1193        if (!componentType.isPrimitive()) {
1194            throw new IllegalArgumentException("Component type is not primitive");
1195        }
1196        if (length < 0) {
1197            throw new IllegalArgumentException("Negative length");
1198        }
1199        return allocateUninitializedArray0(componentType, length);
1200     }
1201 
1202     @HotSpotIntrinsicCandidate
1203     private Object allocateUninitializedArray0(Class<?> componentType, int length) {
1204        // These fallbacks provide zeroed arrays, but intrinsic is not required to
1205        // return the zeroed arrays.
1206        if (componentType == byte.class)    return new byte[length];
1207        if (componentType == boolean.class) return new boolean[length];
1208        if (componentType == short.class)   return new short[length];
1209        if (componentType == char.class)    return new char[length];
1210        if (componentType == int.class)     return new int[length];
1211        if (componentType == float.class)   return new float[length];
1212        if (componentType == long.class)    return new long[length];
1213        if (componentType == double.class)  return new double[length];
1214        return null;
1215     }
1216 
1217     /** Throws the exception without telling the verifier. */
1218     public native void throwException(Throwable ee);
1219 
1220     /**
1221      * Atomically updates Java variable to {@code x} if it is currently
1222      * holding {@code expected}.
1223      *
1224      * <p>This operation has memory semantics of a {@code volatile} read
1225      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1226      *
1227      * @return {@code true} if successful
1228      */
1229     @HotSpotIntrinsicCandidate
1230     public final native boolean compareAndSwapObject(Object o, long offset,
1231                                                      Object expected,
1232                                                      Object x);
1233 
1234     @HotSpotIntrinsicCandidate
1235     public final native Object compareAndExchangeObjectVolatile(Object o, long offset,
1236                                                                 Object expected,
1237                                                                 Object x);
1238 
1239     @HotSpotIntrinsicCandidate
1240     public final Object compareAndExchangeObjectAcquire(Object o, long offset,
1241                                                                Object expected,
1242                                                                Object x) {
1243         return compareAndExchangeObjectVolatile(o, offset, expected, x);
1244     }
1245 
1246     @HotSpotIntrinsicCandidate
1247     public final Object compareAndExchangeObjectRelease(Object o, long offset,
1248                                                                Object expected,
1249                                                                Object x) {
1250         return compareAndExchangeObjectVolatile(o, offset, expected, x);
1251     }
1252 
1253     @HotSpotIntrinsicCandidate
1254     public final boolean weakCompareAndSwapObject(Object o, long offset,
1255                                                          Object expected,
1256                                                          Object x) {
1257         return compareAndSwapObject(o, offset, expected, x);
1258     }
1259 
1260     @HotSpotIntrinsicCandidate
1261     public final boolean weakCompareAndSwapObjectAcquire(Object o, long offset,
1262                                                                 Object expected,
1263                                                                 Object x) {
1264         return compareAndSwapObject(o, offset, expected, x);
1265     }
1266 
1267     @HotSpotIntrinsicCandidate
1268     public final boolean weakCompareAndSwapObjectRelease(Object o, long offset,
1269                                                                 Object expected,
1270                                                                 Object x) {
1271         return compareAndSwapObject(o, offset, expected, x);
1272     }
1273 
1274     /**
1275      * Atomically updates Java variable to {@code x} if it is currently
1276      * holding {@code expected}.
1277      *
1278      * <p>This operation has memory semantics of a {@code volatile} read
1279      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1280      *
1281      * @return {@code true} if successful
1282      */
1283     @HotSpotIntrinsicCandidate
1284     public final native boolean compareAndSwapInt(Object o, long offset,
1285                                                   int expected,
1286                                                   int x);
1287 
1288     @HotSpotIntrinsicCandidate
1289     public final native int compareAndExchangeIntVolatile(Object o, long offset,
1290                                                           int expected,
1291                                                           int x);
1292 
1293     @HotSpotIntrinsicCandidate
1294     public final int compareAndExchangeIntAcquire(Object o, long offset,
1295                                                          int expected,
1296                                                          int x) {
1297         return compareAndExchangeIntVolatile(o, offset, expected, x);
1298     }
1299 
1300     @HotSpotIntrinsicCandidate
1301     public final int compareAndExchangeIntRelease(Object o, long offset,
1302                                                          int expected,
1303                                                          int x) {
1304         return compareAndExchangeIntVolatile(o, offset, expected, x);
1305     }
1306 
1307     @HotSpotIntrinsicCandidate
1308     public final boolean weakCompareAndSwapInt(Object o, long offset,
1309                                                       int expected,
1310                                                       int x) {
1311         return compareAndSwapInt(o, offset, expected, x);
1312     }
1313 
1314     @HotSpotIntrinsicCandidate
1315     public final boolean weakCompareAndSwapIntAcquire(Object o, long offset,
1316                                                              int expected,
1317                                                              int x) {
1318         return compareAndSwapInt(o, offset, expected, x);
1319     }
1320 
1321     @HotSpotIntrinsicCandidate
1322     public final boolean weakCompareAndSwapIntRelease(Object o, long offset,
1323                                                              int expected,
1324                                                              int x) {
1325         return compareAndSwapInt(o, offset, expected, x);
1326     }
1327 
1328     /**
1329      * Atomically updates Java variable to {@code x} if it is currently
1330      * holding {@code expected}.
1331      *
1332      * <p>This operation has memory semantics of a {@code volatile} read
1333      * and write.  Corresponds to C11 atomic_compare_exchange_strong.
1334      *
1335      * @return {@code true} if successful
1336      */
1337     @HotSpotIntrinsicCandidate
1338     public final native boolean compareAndSwapLong(Object o, long offset,
1339                                                    long expected,
1340                                                    long x);
1341 
1342     @HotSpotIntrinsicCandidate
1343     public final native long compareAndExchangeLongVolatile(Object o, long offset,
1344                                                             long expected,
1345                                                             long x);
1346 
1347     @HotSpotIntrinsicCandidate
1348     public final long compareAndExchangeLongAcquire(Object o, long offset,
1349                                                            long expected,
1350                                                            long x) {
1351         return compareAndExchangeLongVolatile(o, offset, expected, x);
1352     }
1353 
1354     @HotSpotIntrinsicCandidate
1355     public final long compareAndExchangeLongRelease(Object o, long offset,
1356                                                            long expected,
1357                                                            long x) {
1358         return compareAndExchangeLongVolatile(o, offset, expected, x);
1359     }
1360 
1361     @HotSpotIntrinsicCandidate
1362     public final boolean weakCompareAndSwapLong(Object o, long offset,
1363                                                        long expected,
1364                                                        long x) {
1365         return compareAndSwapLong(o, offset, expected, x);
1366     }
1367 
1368     @HotSpotIntrinsicCandidate
1369     public final boolean weakCompareAndSwapLongAcquire(Object o, long offset,
1370                                                               long expected,
1371                                                               long x) {
1372         return compareAndSwapLong(o, offset, expected, x);
1373     }
1374 
1375     @HotSpotIntrinsicCandidate
1376     public final boolean weakCompareAndSwapLongRelease(Object o, long offset,
1377                                                               long expected,
1378                                                               long x) {
1379         return compareAndSwapLong(o, offset, expected, x);
1380     }
1381 
1382     /**
1383      * Fetches a reference value from a given Java variable, with volatile
1384      * load semantics. Otherwise identical to {@link #getObject(Object, long)}
1385      */
1386     @HotSpotIntrinsicCandidate
1387     public native Object getObjectVolatile(Object o, long offset);
1388 
1389     /**
1390      * Stores a reference value into a given Java variable, with
1391      * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
1392      */
1393     @HotSpotIntrinsicCandidate
1394     public native void    putObjectVolatile(Object o, long offset, Object x);
1395 
1396     /** Volatile version of {@link #getInt(Object, long)}  */
1397     @HotSpotIntrinsicCandidate
1398     public native int     getIntVolatile(Object o, long offset);
1399 
1400     /** Volatile version of {@link #putInt(Object, long, int)}  */
1401     @HotSpotIntrinsicCandidate
1402     public native void    putIntVolatile(Object o, long offset, int x);
1403 
1404     /** Volatile version of {@link #getBoolean(Object, long)}  */
1405     @HotSpotIntrinsicCandidate
1406     public native boolean getBooleanVolatile(Object o, long offset);
1407 
1408     /** Volatile version of {@link #putBoolean(Object, long, boolean)}  */
1409     @HotSpotIntrinsicCandidate
1410     public native void    putBooleanVolatile(Object o, long offset, boolean x);
1411 
1412     /** Volatile version of {@link #getByte(Object, long)}  */
1413     @HotSpotIntrinsicCandidate
1414     public native byte    getByteVolatile(Object o, long offset);
1415 
1416     /** Volatile version of {@link #putByte(Object, long, byte)}  */
1417     @HotSpotIntrinsicCandidate
1418     public native void    putByteVolatile(Object o, long offset, byte x);
1419 
1420     /** Volatile version of {@link #getShort(Object, long)}  */
1421     @HotSpotIntrinsicCandidate
1422     public native short   getShortVolatile(Object o, long offset);
1423 
1424     /** Volatile version of {@link #putShort(Object, long, short)}  */
1425     @HotSpotIntrinsicCandidate
1426     public native void    putShortVolatile(Object o, long offset, short x);
1427 
1428     /** Volatile version of {@link #getChar(Object, long)}  */
1429     @HotSpotIntrinsicCandidate
1430     public native char    getCharVolatile(Object o, long offset);
1431 
1432     /** Volatile version of {@link #putChar(Object, long, char)}  */
1433     @HotSpotIntrinsicCandidate
1434     public native void    putCharVolatile(Object o, long offset, char x);
1435 
1436     /** Volatile version of {@link #getLong(Object, long)}  */
1437     @HotSpotIntrinsicCandidate
1438     public native long    getLongVolatile(Object o, long offset);
1439 
1440     /** Volatile version of {@link #putLong(Object, long, long)}  */
1441     @HotSpotIntrinsicCandidate
1442     public native void    putLongVolatile(Object o, long offset, long x);
1443 
1444     /** Volatile version of {@link #getFloat(Object, long)}  */
1445     @HotSpotIntrinsicCandidate
1446     public native float   getFloatVolatile(Object o, long offset);
1447 
1448     /** Volatile version of {@link #putFloat(Object, long, float)}  */
1449     @HotSpotIntrinsicCandidate
1450     public native void    putFloatVolatile(Object o, long offset, float x);
1451 
1452     /** Volatile version of {@link #getDouble(Object, long)}  */
1453     @HotSpotIntrinsicCandidate
1454     public native double  getDoubleVolatile(Object o, long offset);
1455 
1456     /** Volatile version of {@link #putDouble(Object, long, double)}  */
1457     @HotSpotIntrinsicCandidate
1458     public native void    putDoubleVolatile(Object o, long offset, double x);
1459 
1460 
1461 
1462     /** Acquire version of {@link #getObjectVolatile(Object, long)} */
1463     @HotSpotIntrinsicCandidate
1464     public final Object getObjectAcquire(Object o, long offset) {
1465         return getObjectVolatile(o, offset);
1466     }
1467 
1468     /** Acquire version of {@link #getBooleanVolatile(Object, long)} */
1469     @HotSpotIntrinsicCandidate
1470     public final boolean getBooleanAcquire(Object o, long offset) {
1471         return getBooleanVolatile(o, offset);
1472     }
1473 
1474     /** Acquire version of {@link #getByteVolatile(Object, long)} */
1475     @HotSpotIntrinsicCandidate
1476     public final byte getByteAcquire(Object o, long offset) {
1477         return getByteVolatile(o, offset);
1478     }
1479 
1480     /** Acquire version of {@link #getShortVolatile(Object, long)} */
1481     @HotSpotIntrinsicCandidate
1482     public final short getShortAcquire(Object o, long offset) {
1483         return getShortVolatile(o, offset);
1484     }
1485 
1486     /** Acquire version of {@link #getCharVolatile(Object, long)} */
1487     @HotSpotIntrinsicCandidate
1488     public final char getCharAcquire(Object o, long offset) {
1489         return getCharVolatile(o, offset);
1490     }
1491 
1492     /** Acquire version of {@link #getIntVolatile(Object, long)} */
1493     @HotSpotIntrinsicCandidate
1494     public final int getIntAcquire(Object o, long offset) {
1495         return getIntVolatile(o, offset);
1496     }
1497 
1498     /** Acquire version of {@link #getFloatVolatile(Object, long)} */
1499     @HotSpotIntrinsicCandidate
1500     public final float getFloatAcquire(Object o, long offset) {
1501         return getFloatVolatile(o, offset);
1502     }
1503 
1504     /** Acquire version of {@link #getLongVolatile(Object, long)} */
1505     @HotSpotIntrinsicCandidate
1506     public final long getLongAcquire(Object o, long offset) {
1507         return getLongVolatile(o, offset);
1508     }
1509 
1510     /** Acquire version of {@link #getDoubleVolatile(Object, long)} */
1511     @HotSpotIntrinsicCandidate
1512     public final double getDoubleAcquire(Object o, long offset) {
1513         return getDoubleVolatile(o, offset);
1514     }
1515 
1516     /*
1517       * Versions of {@link #putObjectVolatile(Object, long, Object)}
1518       * that do not guarantee immediate visibility of the store to
1519       * other threads. This method is generally only useful if the
1520       * underlying field is a Java volatile (or if an array cell, one
1521       * that is otherwise only accessed using volatile accesses).
1522       *
1523       * Corresponds to C11 atomic_store_explicit(..., memory_order_release).
1524       */
1525 
1526     /** Release version of {@link #putObjectVolatile(Object, long, Object)} */
1527     @HotSpotIntrinsicCandidate
1528     public final void putObjectRelease(Object o, long offset, Object x) {
1529         putObjectVolatile(o, offset, x);
1530     }
1531 
1532     /** Release version of {@link #putBooleanVolatile(Object, long, boolean)} */
1533     @HotSpotIntrinsicCandidate
1534     public final void putBooleanRelease(Object o, long offset, boolean x) {
1535         putBooleanVolatile(o, offset, x);
1536     }
1537 
1538     /** Release version of {@link #putByteVolatile(Object, long, byte)} */
1539     @HotSpotIntrinsicCandidate
1540     public final void putByteRelease(Object o, long offset, byte x) {
1541         putByteVolatile(o, offset, x);
1542     }
1543 
1544     /** Release version of {@link #putShortVolatile(Object, long, short)} */
1545     @HotSpotIntrinsicCandidate
1546     public final void putShortRelease(Object o, long offset, short x) {
1547         putShortVolatile(o, offset, x);
1548     }
1549 
1550     /** Release version of {@link #putCharVolatile(Object, long, char)} */
1551     @HotSpotIntrinsicCandidate
1552     public final void putCharRelease(Object o, long offset, char x) {
1553         putCharVolatile(o, offset, x);
1554     }
1555 
1556     /** Release version of {@link #putIntVolatile(Object, long, int)} */
1557     @HotSpotIntrinsicCandidate
1558     public final void putIntRelease(Object o, long offset, int x) {
1559         putIntVolatile(o, offset, x);
1560     }
1561 
1562     /** Release version of {@link #putFloatVolatile(Object, long, float)} */
1563     @HotSpotIntrinsicCandidate
1564     public final void putFloatRelease(Object o, long offset, float x) {
1565         putFloatVolatile(o, offset, x);
1566     }
1567 
1568     /** Release version of {@link #putLongVolatile(Object, long, long)} */
1569     @HotSpotIntrinsicCandidate
1570     public final void putLongRelease(Object o, long offset, long x) {
1571         putLongVolatile(o, offset, x);
1572     }
1573 
1574     /** Release version of {@link #putDoubleVolatile(Object, long, double)} */
1575     @HotSpotIntrinsicCandidate
1576     public final void putDoubleRelease(Object o, long offset, double x) {
1577         putDoubleVolatile(o, offset, x);
1578     }
1579 
1580     // ------------------------------ Opaque --------------------------------------
1581 
1582     /** Opaque version of {@link #getObjectVolatile(Object, long)} */
1583     @HotSpotIntrinsicCandidate
1584     public final Object getObjectOpaque(Object o, long offset) {
1585         return getObjectVolatile(o, offset);
1586     }
1587 
1588     /** Opaque version of {@link #getBooleanVolatile(Object, long)} */
1589     @HotSpotIntrinsicCandidate
1590     public final boolean getBooleanOpaque(Object o, long offset) {
1591         return getBooleanVolatile(o, offset);
1592     }
1593 
1594     /** Opaque version of {@link #getByteVolatile(Object, long)} */
1595     @HotSpotIntrinsicCandidate
1596     public final byte getByteOpaque(Object o, long offset) {
1597         return getByteVolatile(o, offset);
1598     }
1599 
1600     /** Opaque version of {@link #getShortVolatile(Object, long)} */
1601     @HotSpotIntrinsicCandidate
1602     public final short getShortOpaque(Object o, long offset) {
1603         return getShortVolatile(o, offset);
1604     }
1605 
1606     /** Opaque version of {@link #getCharVolatile(Object, long)} */
1607     @HotSpotIntrinsicCandidate
1608     public final char getCharOpaque(Object o, long offset) {
1609         return getCharVolatile(o, offset);
1610     }
1611 
1612     /** Opaque version of {@link #getIntVolatile(Object, long)} */
1613     @HotSpotIntrinsicCandidate
1614     public final int getIntOpaque(Object o, long offset) {
1615         return getIntVolatile(o, offset);
1616     }
1617 
1618     /** Opaque version of {@link #getFloatVolatile(Object, long)} */
1619     @HotSpotIntrinsicCandidate
1620     public final float getFloatOpaque(Object o, long offset) {
1621         return getFloatVolatile(o, offset);
1622     }
1623 
1624     /** Opaque version of {@link #getLongVolatile(Object, long)} */
1625     @HotSpotIntrinsicCandidate
1626     public final long getLongOpaque(Object o, long offset) {
1627         return getLongVolatile(o, offset);
1628     }
1629 
1630     /** Opaque version of {@link #getDoubleVolatile(Object, long)} */
1631     @HotSpotIntrinsicCandidate
1632     public final double getDoubleOpaque(Object o, long offset) {
1633         return getDoubleVolatile(o, offset);
1634     }
1635 
1636     /** Opaque version of {@link #putObjectVolatile(Object, long, Object)} */
1637     @HotSpotIntrinsicCandidate
1638     public final void putObjectOpaque(Object o, long offset, Object x) {
1639         putObjectVolatile(o, offset, x);
1640     }
1641 
1642     /** Opaque version of {@link #putBooleanVolatile(Object, long, boolean)} */
1643     @HotSpotIntrinsicCandidate
1644     public final void putBooleanOpaque(Object o, long offset, boolean x) {
1645         putBooleanVolatile(o, offset, x);
1646     }
1647 
1648     /** Opaque version of {@link #putByteVolatile(Object, long, byte)} */
1649     @HotSpotIntrinsicCandidate
1650     public final void putByteOpaque(Object o, long offset, byte x) {
1651         putByteVolatile(o, offset, x);
1652     }
1653 
1654     /** Opaque version of {@link #putShortVolatile(Object, long, short)} */
1655     @HotSpotIntrinsicCandidate
1656     public final void putShortOpaque(Object o, long offset, short x) {
1657         putShortVolatile(o, offset, x);
1658     }
1659 
1660     /** Opaque version of {@link #putCharVolatile(Object, long, char)} */
1661     @HotSpotIntrinsicCandidate
1662     public final void putCharOpaque(Object o, long offset, char x) {
1663         putCharVolatile(o, offset, x);
1664     }
1665 
1666     /** Opaque version of {@link #putIntVolatile(Object, long, int)} */
1667     @HotSpotIntrinsicCandidate
1668     public final void putIntOpaque(Object o, long offset, int x) {
1669         putIntVolatile(o, offset, x);
1670     }
1671 
1672     /** Opaque version of {@link #putFloatVolatile(Object, long, float)} */
1673     @HotSpotIntrinsicCandidate
1674     public final void putFloatOpaque(Object o, long offset, float x) {
1675         putFloatVolatile(o, offset, x);
1676     }
1677 
1678     /** Opaque version of {@link #putLongVolatile(Object, long, long)} */
1679     @HotSpotIntrinsicCandidate
1680     public final void putLongOpaque(Object o, long offset, long x) {
1681         putLongVolatile(o, offset, x);
1682     }
1683 
1684     /** Opaque version of {@link #putDoubleVolatile(Object, long, double)} */
1685     @HotSpotIntrinsicCandidate
1686     public final void putDoubleOpaque(Object o, long offset, double x) {
1687         putDoubleVolatile(o, offset, x);
1688     }
1689 
1690     /**
1691      * Unblocks the given thread blocked on {@code park}, or, if it is
1692      * not blocked, causes the subsequent call to {@code park} not to
1693      * block.  Note: this operation is "unsafe" solely because the
1694      * caller must somehow ensure that the thread has not been
1695      * destroyed. Nothing special is usually required to ensure this
1696      * when called from Java (in which there will ordinarily be a live
1697      * reference to the thread) but this is not nearly-automatically
1698      * so when calling from native code.
1699      *
1700      * @param thread the thread to unpark.
1701      */
1702     @HotSpotIntrinsicCandidate
1703     public native void unpark(Object thread);
1704 
1705     /**
1706      * Blocks current thread, returning when a balancing
1707      * {@code unpark} occurs, or a balancing {@code unpark} has
1708      * already occurred, or the thread is interrupted, or, if not
1709      * absolute and time is not zero, the given time nanoseconds have
1710      * elapsed, or if absolute, the given deadline in milliseconds
1711      * since Epoch has passed, or spuriously (i.e., returning for no
1712      * "reason"). Note: This operation is in the Unsafe class only
1713      * because {@code unpark} is, so it would be strange to place it
1714      * elsewhere.
1715      */
1716     @HotSpotIntrinsicCandidate
1717     public native void park(boolean isAbsolute, long time);
1718 
1719     /**
1720      * Gets the load average in the system run queue assigned
1721      * to the available processors averaged over various periods of time.
1722      * This method retrieves the given {@code nelem} samples and
1723      * assigns to the elements of the given {@code loadavg} array.
1724      * The system imposes a maximum of 3 samples, representing
1725      * averages over the last 1,  5,  and  15 minutes, respectively.
1726      *
1727      * @param loadavg an array of double of size nelems
1728      * @param nelems the number of samples to be retrieved and
1729      *        must be 1 to 3.
1730      *
1731      * @return the number of samples actually retrieved; or -1
1732      *         if the load average is unobtainable.
1733      */
1734     public int getLoadAverage(double[] loadavg, int nelems) {
1735         if (nelems < 0 || nelems > 3 || nelems > loadavg.length) {
1736             throw new ArrayIndexOutOfBoundsException();
1737         }
1738 
1739         return getLoadAverage0(loadavg, nelems);
1740     }
1741 
1742     // The following contain CAS-based Java implementations used on
1743     // platforms not supporting native instructions
1744 
1745     /**
1746      * Atomically adds the given value to the current value of a field
1747      * or array element within the given object {@code o}
1748      * at the given {@code offset}.
1749      *
1750      * @param o object/array to update the field/element in
1751      * @param offset field/element offset
1752      * @param delta the value to add
1753      * @return the previous value
1754      * @since 1.8
1755      */
1756     @HotSpotIntrinsicCandidate
1757     public final int getAndAddInt(Object o, long offset, int delta) {
1758         int v;
1759         do {
1760             v = getIntVolatile(o, offset);
1761         } while (!compareAndSwapInt(o, offset, v, v + delta));
1762         return v;
1763     }
1764 
1765     /**
1766      * Atomically adds the given value to the current value of a field
1767      * or array element within the given object {@code o}
1768      * at the given {@code offset}.
1769      *
1770      * @param o object/array to update the field/element in
1771      * @param offset field/element offset
1772      * @param delta the value to add
1773      * @return the previous value
1774      * @since 1.8
1775      */
1776     @HotSpotIntrinsicCandidate
1777     public final long getAndAddLong(Object o, long offset, long delta) {
1778         long v;
1779         do {
1780             v = getLongVolatile(o, offset);
1781         } while (!compareAndSwapLong(o, offset, v, v + delta));
1782         return v;
1783     }
1784 
1785     /**
1786      * Atomically exchanges the given value with the current value of
1787      * a field or array element within the given object {@code o}
1788      * at the given {@code offset}.
1789      *
1790      * @param o object/array to update the field/element in
1791      * @param offset field/element offset
1792      * @param newValue new value
1793      * @return the previous value
1794      * @since 1.8
1795      */
1796     @HotSpotIntrinsicCandidate
1797     public final int getAndSetInt(Object o, long offset, int newValue) {
1798         int v;
1799         do {
1800             v = getIntVolatile(o, offset);
1801         } while (!compareAndSwapInt(o, offset, v, newValue));
1802         return v;
1803     }
1804 
1805     /**
1806      * Atomically exchanges the given value with the current value of
1807      * a field or array element within the given object {@code o}
1808      * at the given {@code offset}.
1809      *
1810      * @param o object/array to update the field/element in
1811      * @param offset field/element offset
1812      * @param newValue new value
1813      * @return the previous value
1814      * @since 1.8
1815      */
1816     @HotSpotIntrinsicCandidate
1817     public final long getAndSetLong(Object o, long offset, long newValue) {
1818         long v;
1819         do {
1820             v = getLongVolatile(o, offset);
1821         } while (!compareAndSwapLong(o, offset, v, newValue));
1822         return v;
1823     }
1824 
1825     /**
1826      * Atomically exchanges the given reference value with the current
1827      * reference value of a field or array element within the given
1828      * object {@code o} at the given {@code offset}.
1829      *
1830      * @param o object/array to update the field/element in
1831      * @param offset field/element offset
1832      * @param newValue new value
1833      * @return the previous value
1834      * @since 1.8
1835      */
1836     @HotSpotIntrinsicCandidate
1837     public final Object getAndSetObject(Object o, long offset, Object newValue) {
1838         Object v;
1839         do {
1840             v = getObjectVolatile(o, offset);
1841         } while (!compareAndSwapObject(o, offset, v, newValue));
1842         return v;
1843     }
1844 
1845 
1846     /**
1847      * Ensures that loads before the fence will not be reordered with loads and
1848      * stores after the fence; a "LoadLoad plus LoadStore barrier".
1849      *
1850      * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
1851      * (an "acquire fence").
1852      *
1853      * A pure LoadLoad fence is not provided, since the addition of LoadStore
1854      * is almost always desired, and most current hardware instructions that
1855      * provide a LoadLoad barrier also provide a LoadStore barrier for free.
1856      * @since 1.8
1857      */
1858     @HotSpotIntrinsicCandidate
1859     public native void loadFence();
1860 
1861     /**
1862      * Ensures that loads and stores before the fence will not be reordered with
1863      * stores after the fence; a "StoreStore plus LoadStore barrier".
1864      *
1865      * Corresponds to C11 atomic_thread_fence(memory_order_release)
1866      * (a "release fence").
1867      *
1868      * A pure StoreStore fence is not provided, since the addition of LoadStore
1869      * is almost always desired, and most current hardware instructions that
1870      * provide a StoreStore barrier also provide a LoadStore barrier for free.
1871      * @since 1.8
1872      */
1873     @HotSpotIntrinsicCandidate
1874     public native void storeFence();
1875 
1876     /**
1877      * Ensures that loads and stores before the fence will not be reordered
1878      * with loads and stores after the fence.  Implies the effects of both
1879      * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
1880      * barrier.
1881      *
1882      * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
1883      * @since 1.8
1884      */
1885     @HotSpotIntrinsicCandidate
1886     public native void fullFence();
1887 
1888     /**
1889      * Ensures that loads before the fence will not be reordered with
1890      * loads after the fence.
1891      */
1892     public final void loadLoadFence() {
1893         loadFence();
1894     }
1895 
1896     /**
1897      * Ensures that stores before the fence will not be reordered with
1898      * stores after the fence.
1899      */
1900     public final void storeStoreFence() {
1901         storeFence();
1902     }
1903 
1904 
1905     /**
1906      * Throws IllegalAccessError; for use by the VM for access control
1907      * error support.
1908      * @since 1.8
1909      */
1910     private static void throwIllegalAccessError() {
1911         throw new IllegalAccessError();
1912     }
1913 
1914     /**
1915      * @return Returns true if the native byte ordering of this
1916      * platform is big-endian, false if it is little-endian.
1917      */
1918     public final boolean isBigEndian() { return BE; }
1919 
1920     /**
1921      * @return Returns true if this platform is capable of performing
1922      * accesses at addresses which are not aligned for the type of the
1923      * primitive type being accessed, false otherwise.
1924      */
1925     public final boolean unalignedAccess() { return unalignedAccess; }
1926 
1927     /**
1928      * Fetches a value at some byte offset into a given Java object.
1929      * More specifically, fetches a value within the given object
1930      * <code>o</code> at the given offset, or (if <code>o</code> is
1931      * null) from the memory address whose numerical value is the
1932      * given offset.  <p>
1933      *
1934      * The specification of this method is the same as {@link
1935      * #getLong(Object, long)} except that the offset does not need to
1936      * have been obtained from {@link #objectFieldOffset} on the
1937      * {@link java.lang.reflect.Field} of some Java field.  The value
1938      * in memory is raw data, and need not correspond to any Java
1939      * variable.  Unless <code>o</code> is null, the value accessed
1940      * must be entirely within the allocated object.  The endianness
1941      * of the value in memory is the endianness of the native platform.
1942      *
1943      * <p> The read will be atomic with respect to the largest power
1944      * of two that divides the GCD of the offset and the storage size.
1945      * For example, getLongUnaligned will make atomic reads of 2-, 4-,
1946      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
1947      * respectively.  There are no other guarantees of atomicity.
1948      * <p>
1949      * 8-byte atomicity is only guaranteed on platforms on which
1950      * support atomic accesses to longs.
1951      *
1952      * @param o Java heap object in which the value resides, if any, else
1953      *        null
1954      * @param offset The offset in bytes from the start of the object
1955      * @return the value fetched from the indicated object
1956      * @throws RuntimeException No defined exceptions are thrown, not even
1957      *         {@link NullPointerException}
1958      * @since 9
1959      */
1960     @HotSpotIntrinsicCandidate
1961     public final long getLongUnaligned(Object o, long offset) {
1962         if ((offset & 7) == 0) {
1963             return getLong(o, offset);
1964         } else if ((offset & 3) == 0) {
1965             return makeLong(getInt(o, offset),
1966                             getInt(o, offset + 4));
1967         } else if ((offset & 1) == 0) {
1968             return makeLong(getShort(o, offset),
1969                             getShort(o, offset + 2),
1970                             getShort(o, offset + 4),
1971                             getShort(o, offset + 6));
1972         } else {
1973             return makeLong(getByte(o, offset),
1974                             getByte(o, offset + 1),
1975                             getByte(o, offset + 2),
1976                             getByte(o, offset + 3),
1977                             getByte(o, offset + 4),
1978                             getByte(o, offset + 5),
1979                             getByte(o, offset + 6),
1980                             getByte(o, offset + 7));
1981         }
1982     }
1983     /**
1984      * As {@link #getLongUnaligned(Object, long)} but with an
1985      * additional argument which specifies the endianness of the value
1986      * as stored in memory.
1987      *
1988      * @param o Java heap object in which the variable resides
1989      * @param offset The offset in bytes from the start of the object
1990      * @param bigEndian The endianness of the value
1991      * @return the value fetched from the indicated object
1992      * @since 9
1993      */
1994     public final long getLongUnaligned(Object o, long offset, boolean bigEndian) {
1995         return convEndian(bigEndian, getLongUnaligned(o, offset));
1996     }
1997 
1998     /** @see #getLongUnaligned(Object, long) */
1999     @HotSpotIntrinsicCandidate
2000     public final int getIntUnaligned(Object o, long offset) {
2001         if ((offset & 3) == 0) {
2002             return getInt(o, offset);
2003         } else if ((offset & 1) == 0) {
2004             return makeInt(getShort(o, offset),
2005                            getShort(o, offset + 2));
2006         } else {
2007             return makeInt(getByte(o, offset),
2008                            getByte(o, offset + 1),
2009                            getByte(o, offset + 2),
2010                            getByte(o, offset + 3));
2011         }
2012     }
2013     /** @see #getLongUnaligned(Object, long, boolean) */
2014     public final int getIntUnaligned(Object o, long offset, boolean bigEndian) {
2015         return convEndian(bigEndian, getIntUnaligned(o, offset));
2016     }
2017 
2018     /** @see #getLongUnaligned(Object, long) */
2019     @HotSpotIntrinsicCandidate
2020     public final short getShortUnaligned(Object o, long offset) {
2021         if ((offset & 1) == 0) {
2022             return getShort(o, offset);
2023         } else {
2024             return makeShort(getByte(o, offset),
2025                              getByte(o, offset + 1));
2026         }
2027     }
2028     /** @see #getLongUnaligned(Object, long, boolean) */
2029     public final short getShortUnaligned(Object o, long offset, boolean bigEndian) {
2030         return convEndian(bigEndian, getShortUnaligned(o, offset));
2031     }
2032 
2033     /** @see #getLongUnaligned(Object, long) */
2034     @HotSpotIntrinsicCandidate
2035     public final char getCharUnaligned(Object o, long offset) {
2036         if ((offset & 1) == 0) {
2037             return getChar(o, offset);
2038         } else {
2039             return (char)makeShort(getByte(o, offset),
2040                                    getByte(o, offset + 1));
2041         }
2042     }
2043 
2044     /** @see #getLongUnaligned(Object, long, boolean) */
2045     public final char getCharUnaligned(Object o, long offset, boolean bigEndian) {
2046         return convEndian(bigEndian, getCharUnaligned(o, offset));
2047     }
2048 
2049     /**
2050      * Stores a value at some byte offset into a given Java object.
2051      * <p>
2052      * The specification of this method is the same as {@link
2053      * #getLong(Object, long)} except that the offset does not need to
2054      * have been obtained from {@link #objectFieldOffset} on the
2055      * {@link java.lang.reflect.Field} of some Java field.  The value
2056      * in memory is raw data, and need not correspond to any Java
2057      * variable.  The endianness of the value in memory is the
2058      * endianness of the native platform.
2059      * <p>
2060      * The write will be atomic with respect to the largest power of
2061      * two that divides the GCD of the offset and the storage size.
2062      * For example, putLongUnaligned will make atomic writes of 2-, 4-,
2063      * or 8-byte storage units if the offset is zero mod 2, 4, or 8,
2064      * respectively.  There are no other guarantees of atomicity.
2065      * <p>
2066      * 8-byte atomicity is only guaranteed on platforms on which
2067      * support atomic accesses to longs.
2068      *
2069      * @param o Java heap object in which the value resides, if any, else
2070      *        null
2071      * @param offset The offset in bytes from the start of the object
2072      * @param x the value to store
2073      * @throws RuntimeException No defined exceptions are thrown, not even
2074      *         {@link NullPointerException}
2075      * @since 9
2076      */
2077     @HotSpotIntrinsicCandidate
2078     public final void putLongUnaligned(Object o, long offset, long x) {
2079         if ((offset & 7) == 0) {
2080             putLong(o, offset, x);
2081         } else if ((offset & 3) == 0) {
2082             putLongParts(o, offset,
2083                          (int)(x >> 0),
2084                          (int)(x >>> 32));
2085         } else if ((offset & 1) == 0) {
2086             putLongParts(o, offset,
2087                          (short)(x >>> 0),
2088                          (short)(x >>> 16),
2089                          (short)(x >>> 32),
2090                          (short)(x >>> 48));
2091         } else {
2092             putLongParts(o, offset,
2093                          (byte)(x >>> 0),
2094                          (byte)(x >>> 8),
2095                          (byte)(x >>> 16),
2096                          (byte)(x >>> 24),
2097                          (byte)(x >>> 32),
2098                          (byte)(x >>> 40),
2099                          (byte)(x >>> 48),
2100                          (byte)(x >>> 56));
2101         }
2102     }
2103 
2104     /**
2105      * As {@link #putLongUnaligned(Object, long, long)} but with an additional
2106      * argument which specifies the endianness of the value as stored in memory.
2107      * @param o Java heap object in which the value resides
2108      * @param offset The offset in bytes from the start of the object
2109      * @param x the value to store
2110      * @param bigEndian The endianness of the value
2111      * @throws RuntimeException No defined exceptions are thrown, not even
2112      *         {@link NullPointerException}
2113      * @since 9
2114      */
2115     public final void putLongUnaligned(Object o, long offset, long x, boolean bigEndian) {
2116         putLongUnaligned(o, offset, convEndian(bigEndian, x));
2117     }
2118 
2119     /** @see #putLongUnaligned(Object, long, long) */
2120     @HotSpotIntrinsicCandidate
2121     public final void putIntUnaligned(Object o, long offset, int x) {
2122         if ((offset & 3) == 0) {
2123             putInt(o, offset, x);
2124         } else if ((offset & 1) == 0) {
2125             putIntParts(o, offset,
2126                         (short)(x >> 0),
2127                         (short)(x >>> 16));
2128         } else {
2129             putIntParts(o, offset,
2130                         (byte)(x >>> 0),
2131                         (byte)(x >>> 8),
2132                         (byte)(x >>> 16),
2133                         (byte)(x >>> 24));
2134         }
2135     }
2136     /** @see #putLongUnaligned(Object, long, long, boolean) */
2137     public final void putIntUnaligned(Object o, long offset, int x, boolean bigEndian) {
2138         putIntUnaligned(o, offset, convEndian(bigEndian, x));
2139     }
2140 
2141     /** @see #putLongUnaligned(Object, long, long) */
2142     @HotSpotIntrinsicCandidate
2143     public final void putShortUnaligned(Object o, long offset, short x) {
2144         if ((offset & 1) == 0) {
2145             putShort(o, offset, x);
2146         } else {
2147             putShortParts(o, offset,
2148                           (byte)(x >>> 0),
2149                           (byte)(x >>> 8));
2150         }
2151     }
2152     /** @see #putLongUnaligned(Object, long, long, boolean) */
2153     public final void putShortUnaligned(Object o, long offset, short x, boolean bigEndian) {
2154         putShortUnaligned(o, offset, convEndian(bigEndian, x));
2155     }
2156 
2157     /** @see #putLongUnaligned(Object, long, long) */
2158     @HotSpotIntrinsicCandidate
2159     public final void putCharUnaligned(Object o, long offset, char x) {
2160         putShortUnaligned(o, offset, (short)x);
2161     }
2162     /** @see #putLongUnaligned(Object, long, long, boolean) */
2163     public final void putCharUnaligned(Object o, long offset, char x, boolean bigEndian) {
2164         putCharUnaligned(o, offset, convEndian(bigEndian, x));
2165     }
2166 
2167     // JVM interface methods
2168     // BE is true iff the native endianness of this platform is big.
2169     private static final boolean BE = theUnsafe.isBigEndian0();
2170 
2171     // unalignedAccess is true iff this platform can perform unaligned accesses.
2172     private static final boolean unalignedAccess = theUnsafe.unalignedAccess0();
2173 
2174     private static int pickPos(int top, int pos) { return BE ? top - pos : pos; }
2175 
2176     // These methods construct integers from bytes.  The byte ordering
2177     // is the native endianness of this platform.
2178     private static long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
2179         return ((toUnsignedLong(i0) << pickPos(56, 0))
2180               | (toUnsignedLong(i1) << pickPos(56, 8))
2181               | (toUnsignedLong(i2) << pickPos(56, 16))
2182               | (toUnsignedLong(i3) << pickPos(56, 24))
2183               | (toUnsignedLong(i4) << pickPos(56, 32))
2184               | (toUnsignedLong(i5) << pickPos(56, 40))
2185               | (toUnsignedLong(i6) << pickPos(56, 48))
2186               | (toUnsignedLong(i7) << pickPos(56, 56)));
2187     }
2188     private static long makeLong(short i0, short i1, short i2, short i3) {
2189         return ((toUnsignedLong(i0) << pickPos(48, 0))
2190               | (toUnsignedLong(i1) << pickPos(48, 16))
2191               | (toUnsignedLong(i2) << pickPos(48, 32))
2192               | (toUnsignedLong(i3) << pickPos(48, 48)));
2193     }
2194     private static long makeLong(int i0, int i1) {
2195         return (toUnsignedLong(i0) << pickPos(32, 0))
2196              | (toUnsignedLong(i1) << pickPos(32, 32));
2197     }
2198     private static int makeInt(short i0, short i1) {
2199         return (toUnsignedInt(i0) << pickPos(16, 0))
2200              | (toUnsignedInt(i1) << pickPos(16, 16));
2201     }
2202     private static int makeInt(byte i0, byte i1, byte i2, byte i3) {
2203         return ((toUnsignedInt(i0) << pickPos(24, 0))
2204               | (toUnsignedInt(i1) << pickPos(24, 8))
2205               | (toUnsignedInt(i2) << pickPos(24, 16))
2206               | (toUnsignedInt(i3) << pickPos(24, 24)));
2207     }
2208     private static short makeShort(byte i0, byte i1) {
2209         return (short)((toUnsignedInt(i0) << pickPos(8, 0))
2210                      | (toUnsignedInt(i1) << pickPos(8, 8)));
2211     }
2212 
2213     private static byte  pick(byte  le, byte  be) { return BE ? be : le; }
2214     private static short pick(short le, short be) { return BE ? be : le; }
2215     private static int   pick(int   le, int   be) { return BE ? be : le; }
2216 
2217     // These methods write integers to memory from smaller parts
2218     // provided by their caller.  The ordering in which these parts
2219     // are written is the native endianness of this platform.
2220     private void putLongParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) {
2221         putByte(o, offset + 0, pick(i0, i7));
2222         putByte(o, offset + 1, pick(i1, i6));
2223         putByte(o, offset + 2, pick(i2, i5));
2224         putByte(o, offset + 3, pick(i3, i4));
2225         putByte(o, offset + 4, pick(i4, i3));
2226         putByte(o, offset + 5, pick(i5, i2));
2227         putByte(o, offset + 6, pick(i6, i1));
2228         putByte(o, offset + 7, pick(i7, i0));
2229     }
2230     private void putLongParts(Object o, long offset, short i0, short i1, short i2, short i3) {
2231         putShort(o, offset + 0, pick(i0, i3));
2232         putShort(o, offset + 2, pick(i1, i2));
2233         putShort(o, offset + 4, pick(i2, i1));
2234         putShort(o, offset + 6, pick(i3, i0));
2235     }
2236     private void putLongParts(Object o, long offset, int i0, int i1) {
2237         putInt(o, offset + 0, pick(i0, i1));
2238         putInt(o, offset + 4, pick(i1, i0));
2239     }
2240     private void putIntParts(Object o, long offset, short i0, short i1) {
2241         putShort(o, offset + 0, pick(i0, i1));
2242         putShort(o, offset + 2, pick(i1, i0));
2243     }
2244     private void putIntParts(Object o, long offset, byte i0, byte i1, byte i2, byte i3) {
2245         putByte(o, offset + 0, pick(i0, i3));
2246         putByte(o, offset + 1, pick(i1, i2));
2247         putByte(o, offset + 2, pick(i2, i1));
2248         putByte(o, offset + 3, pick(i3, i0));
2249     }
2250     private void putShortParts(Object o, long offset, byte i0, byte i1) {
2251         putByte(o, offset + 0, pick(i0, i1));
2252         putByte(o, offset + 1, pick(i1, i0));
2253     }
2254 
2255     // Zero-extend an integer
2256     private static int toUnsignedInt(byte n)    { return n & 0xff; }
2257     private static int toUnsignedInt(short n)   { return n & 0xffff; }
2258     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
2259     private static long toUnsignedLong(short n) { return n & 0xffffl; }
2260     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
2261 
2262     // Maybe byte-reverse an integer
2263     private static char convEndian(boolean big, char n)   { return big == BE ? n : Character.reverseBytes(n); }
2264     private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
2265     private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
2266     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
2267 
2268 
2269 
2270     private native long allocateMemory0(long bytes);
2271     private native long reallocateMemory0(long address, long bytes);
2272     private native void freeMemory0(long address);
2273     private native void setMemory0(Object o, long offset, long bytes, byte value);
2274     @HotSpotIntrinsicCandidate
2275     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
2276     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
2277     private native long objectFieldOffset0(Field f);
2278     private native long staticFieldOffset0(Field f);
2279     private native Object staticFieldBase0(Field f);
2280     private native boolean shouldBeInitialized0(Class<?> c);
2281     private native void ensureClassInitialized0(Class<?> c);
2282     private native int arrayBaseOffset0(Class<?> arrayClass);
2283     private native int arrayIndexScale0(Class<?> arrayClass);
2284     private native int addressSize0();
2285     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
2286     private native int getLoadAverage0(double[] loadavg, int nelems);
2287     private native boolean unalignedAccess0();
2288     private native boolean isBigEndian0();
2289 }