1 /*
   2  * Copyright (c) 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import jdk.internal.misc.Unsafe;
  25 import java.lang.reflect.Field;
  26 
  27 /*
  28  * @test
  29  * @summary Test Unsafe.copyMemory
  30  * @modules java.base/jdk.internal.misc
  31  */
  32 public class CopyMemory {
  33     private static final Unsafe UNSAFE;
  34 
  35     static {
  36         try {
  37             Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
  38             f.setAccessible(true);
  39             UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
  40         } catch (Exception e) {
  41             throw new RuntimeException("Unable to get Unsafe instance.", e);
  42         }
  43     }
  44 
  45     private CopyMemory() {
  46     }
  47 
  48     /**
  49      * Run positive tests
  50      *
  51      * @throws RuntimeException if an error is found
  52      */
  53     private void testPositive() {
  54         CopyCommon cc = new CopyCommon();
  55         cc.testSmallCopy(false);
  56         cc.testLargeCopy(false);
  57     }
  58 
  59     /**
  60      * Run negative tests, testing corner cases and the various exceptions
  61      *
  62      * @throws RuntimeException if an error is found
  63      */
  64     private void testNegative() {
  65         long bufRaw = 0;
  66 
  67         try {
  68             bufRaw = UNSAFE.allocateMemory(1024);
  69             long buf = CopyCommon.alignUp(bufRaw, CopyCommon.BASE_ALIGNMENT);
  70             short[] arr = new short[16];
  71 
  72             // Check illegal sizes
  73             System.out.println("Testing negative size");
  74             try {
  75                 UNSAFE.copyMemory(null, buf, null, buf, -1);
  76                 throw new RuntimeException("copyMemory failed to throw IAE for size=-1");
  77             } catch (IllegalArgumentException e) {
  78                 // good
  79             }
  80 
  81             System.out.println("Testing negative srcOffset");
  82             try {
  83                 // Check that negative srcOffset throws an IAE
  84                 UNSAFE.copyMemory(arr, -1, arr, UNSAFE.arrayBaseOffset(arr.getClass()), 16);
  85                 throw new RuntimeException("copyMemory failed to throw IAE for srcOffset=-1");
  86             } catch (IllegalArgumentException e) {
  87                 // good
  88             }
  89 
  90             System.out.println("Testing negative destOffset");
  91             try {
  92                 // Check that negative dstOffset throws an IAE
  93                 UNSAFE.copyMemory(arr, UNSAFE.arrayBaseOffset(arr.getClass()), arr, -1, 16);
  94                 throw new RuntimeException("copyMemory failed to throw IAE for destOffset=-1");
  95             } catch (IllegalArgumentException e) {
  96                 // good
  97             }
  98 
  99             System.out.println("Testing reference array");
 100             try {
 101                 // Check that a reference array destination throws IAE
 102                 UNSAFE.copyMemory(null, buf, new Object[16], UNSAFE.arrayBaseOffset(Object[].class), 16);
 103                 throw new RuntimeException("copyMemory failed to throw IAE");
 104             } catch (IllegalArgumentException e) {
 105                 // good
 106             }
 107 
 108             // Check that invalid source & dest pointers throw IAEs (only relevant on 32-bit platforms)
 109             if (UNSAFE.addressSize() == 4) {
 110                 long invalidPtr = (long)1 << 35; // Pick a random bit in upper 32 bits
 111 
 112                 try {
 113                     // Check that an invalid (not 32-bit clean) source pointer throws IAE
 114                     UNSAFE.copyMemory(null, invalidPtr, null, buf, 16);
 115                     throw new RuntimeException("copyMemory failed to throw IAE for srcOffset 0x" +
 116                                                Long.toHexString(invalidPtr));
 117                 } catch (IllegalArgumentException e) {
 118                     // good
 119                 }
 120 
 121                 try {
 122                     // Check that an invalid (not 32-bit clean) source pointer throws IAE
 123                     UNSAFE.copyMemory(null, buf, null, invalidPtr, 16);
 124                     throw new RuntimeException("copyMemory failed to throw IAE for destOffset 0x" +
 125                                                Long.toHexString(invalidPtr));
 126                 } catch (IllegalArgumentException e) {
 127                     // good
 128                 }
 129             }
 130         } finally {
 131             if (bufRaw != 0) {
 132                 UNSAFE.freeMemory(bufRaw);
 133             }
 134         }
 135     }
 136 
 137     /**
 138      * Run all tests
 139      *
 140      * @throws RuntimeException if an error is found
 141      */
 142     private void test() {
 143         testPositive();
 144         testNegative();
 145     }
 146 
 147     public static void main(String[] args) {
 148         CopyMemory cs = new CopyMemory();
 149         cs.test();
 150     }
 151 }