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.copySwapMemory
  30  * @modules java.base/jdk.internal.misc
  31  */
  32 public class CopySwap extends CopyCommon {
  33     private CopySwap() {
  34     }
  35 
  36     /**
  37      * Run positive tests
  38      *
  39      * @throws RuntimeException if an error is found
  40      */
  41     private void testPositive() {
  42         testSmallCopy(true);
  43         testLargeCopy(true);
  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 various illegal element sizes
  60             for (int elemSize = 2; elemSize <= 8; elemSize <<= 1) {
  61                 long[] illegalSizes = { -1, 1, elemSize - 1, elemSize + 1, elemSize * 2 - 1 };
  62                 for (long size : illegalSizes) {
  63                     try {
  64                         // Check that illegal elemSize throws an IAE
  65                         UNSAFE.copySwapMemory(null, buf, null, buf, size, elemSize);
  66                         throw new RuntimeException("copySwapMemory failed to throw IAE for size=" + size + " elemSize=" + elemSize);
  67                     } catch (IllegalArgumentException e) {
  68                         // good
  69                     }
  70                 }
  71             }
  72 
  73             try {
  74                 // Check that negative srcOffset throws an IAE
  75                 UNSAFE.copySwapMemory(arr, -1, arr, UNSAFE.arrayBaseOffset(arr.getClass()), 16, 2);
  76                 throw new RuntimeException("copySwapMemory failed to throw IAE for srcOffset=-1");
  77             } catch (IllegalArgumentException e) {
  78                 // good
  79             }
  80 
  81             try {
  82                 // Check that negative dstOffset throws an IAE
  83                 UNSAFE.copySwapMemory(arr, UNSAFE.arrayBaseOffset(arr.getClass()), arr, -1, 16, 2);
  84                 throw new RuntimeException("copySwapMemory failed to throw IAE for destOffset=-1");
  85             } catch (IllegalArgumentException e) {
  86                 // good
  87             }
  88 
  89             long illegalElemSizes[] = { 0, 1, 3, 5, 6, 7, 9, 10, -1 };
  90             for (long elemSize : illegalElemSizes) {
  91                 try {
  92                     // Check that elemSize 1 throws an IAE
  93                     UNSAFE.copySwapMemory(null, buf, null, buf, 16, elemSize);
  94                     throw new RuntimeException("copySwapMemory failed to throw NPE");
  95                 } catch (IllegalArgumentException e) {
  96                     // good
  97                 }
  98             }
  99 
 100             try {
 101                 // Check that a reference array destination throws IAE
 102                 UNSAFE.copySwapMemory(null, buf, new Object[16], UNSAFE.arrayBaseOffset(Object[].class), 16, 8);
 103                 throw new RuntimeException("copySwapMemory failed to throw NPE");
 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.copySwapMemory(null, invalidPtr, null, buf, 16, 2);
 115                     throw new RuntimeException("copySwapMemory 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.copySwapMemory(null, buf, null, invalidPtr, 16, 2);
 124                     throw new RuntimeException("copySwapMemory 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         CopySwap cs = new CopySwap();
 149         cs.test();
 150     }
 151 }