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