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 extends CopyCommon {
  33     private CopyMemory() {
  34     }
  35 
  36     /**
  37      * Run positive tests
  38      *
  39      * @throws RuntimeException if an error is found
  40      */
  41     private void testPositive() {
  42         testSmallCopy(false);
  43         testLargeCopy(false);
  44     }
  45 
  46     /**
  47      * Run negative tests, testing corner cases and the various exceptions
  48      *
  49      * @throws RuntimeException if an error is found
  50      */
  51     private void testNegative() {
  52         long bufRaw = 0;
  53 
  54         try {
  55             bufRaw = UNSAFE.allocateMemory(1024);
  56             long buf = CopyCommon.alignUp(bufRaw, CopyCommon.BASE_ALIGNMENT);
  57             short[] arr = new short[16];
  58 
  59             // Check illegal sizes
  60             System.out.println("Testing negative size");
  61             try {
  62                 UNSAFE.copyMemory(null, buf, null, buf, -1);
  63                 throw new RuntimeException("copyMemory failed to throw IAE for size=-1");
  64             } catch (IllegalArgumentException e) {
  65                 // good
  66             }
  67 
  68             System.out.println("Testing negative srcOffset");
  69             try {
  70                 // Check that negative srcOffset throws an IAE
  71                 UNSAFE.copyMemory(arr, -1, arr, UNSAFE.arrayBaseOffset(arr.getClass()), 16);
  72                 throw new RuntimeException("copyMemory failed to throw IAE for srcOffset=-1");
  73             } catch (IllegalArgumentException e) {
  74                 // good
  75             }
  76 
  77             System.out.println("Testing negative destOffset");
  78             try {
  79                 // Check that negative dstOffset throws an IAE
  80                 UNSAFE.copyMemory(arr, UNSAFE.arrayBaseOffset(arr.getClass()), arr, -1, 16);
  81                 throw new RuntimeException("copyMemory failed to throw IAE for destOffset=-1");
  82             } catch (IllegalArgumentException e) {
  83                 // good
  84             }
  85 
  86             System.out.println("Testing reference array");
  87             try {
  88                 // Check that a reference array destination throws IAE
  89                 UNSAFE.copyMemory(null, buf, new Object[16], UNSAFE.arrayBaseOffset(Object[].class), 16);
  90                 throw new RuntimeException("copyMemory failed to throw IAE");
  91             } catch (IllegalArgumentException e) {
  92                 // good
  93             }
  94 
  95             // Check that invalid source & dest pointers throw IAEs (only relevant on 32-bit platforms)
  96             if (UNSAFE.addressSize() == 4) {
  97                 long invalidPtr = (long)1 << 35; // Pick a random bit in upper 32 bits
  98 
  99                 try {
 100                     // Check that an invalid (not 32-bit clean) source pointer throws IAE
 101                     UNSAFE.copyMemory(null, invalidPtr, null, buf, 16);
 102                     throw new RuntimeException("copyMemory failed to throw IAE for srcOffset 0x" +
 103                                                Long.toHexString(invalidPtr));
 104                 } catch (IllegalArgumentException e) {
 105                     // good
 106                 }
 107 
 108                 try {
 109                     // Check that an invalid (not 32-bit clean) source pointer throws IAE
 110                     UNSAFE.copyMemory(null, buf, null, invalidPtr, 16);
 111                     throw new RuntimeException("copyMemory failed to throw IAE for destOffset 0x" +
 112                                                Long.toHexString(invalidPtr));
 113                 } catch (IllegalArgumentException e) {
 114                     // good
 115                 }
 116             }
 117         } finally {
 118             if (bufRaw != 0) {
 119                 UNSAFE.freeMemory(bufRaw);
 120             }
 121         }
 122     }
 123 
 124     /**
 125      * Run all tests
 126      *
 127      * @throws RuntimeException if an error is found
 128      */
 129     private void test() {
 130         testPositive();
 131         testNegative();
 132     }
 133 
 134     public static void main(String[] args) {
 135         CopyMemory cs = new CopyMemory();
 136         cs.test();
 137     }
 138 }