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