1 /* 2 * Copyright (c) 2007, 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 /* @test 25 * @bug 6565543 26 * @summary Minimal test for unsafe.copyMemory() and unsafe.setMemory() 27 * @key randomness 28 */ 29 30 import java.util.*; 31 import java.lang.reflect.*; 32 import java.nio.*; 33 34 import sun.misc.Unsafe; 35 36 import sun.nio.ch.DirectBuffer; 37 38 public class CopyMemory { 39 40 private final static int BUFFER_SIZE = 1024; 41 private final static int N = 16 * 1024; 42 43 private final static int FILLER = 0x55; 44 private final static int FILLER2 = 0x33; 45 46 private final static Random random = new Random(); 47 48 private static void set(byte[] b, int ofs, int len, int value) { 49 for (int i = 0; i < len; i++) { 50 b[ofs + i] = (byte)value; 51 } 52 } 53 54 private static void check(byte[] b, int ofs, int len, int value) { 55 for (int i = 0; i < len; i++) { 56 int r = b[ofs + i] & 0xff; 57 if (r != value) { 58 throw new RuntimeException("mismatch"); 59 } 60 } 61 } 62 63 private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) { 64 for (int i = 0; i < len; i++) { 65 unsafe.putByte(null, addr + ofs + i, (byte)value); 66 } 67 } 68 69 private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) { 70 for (int i = 0; i < len; i++) { 71 int r = unsafe.getByte(null, addr + ofs + i) & 0xff; 72 if (r != value) { 73 throw new RuntimeException("mismatch"); 74 } 75 } 76 } 77 78 private static final List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(); 79 80 private static long getMemory(int n) { 81 ByteBuffer b = ByteBuffer.allocateDirect(n); 82 if (b instanceof DirectBuffer == false) { 83 throw new RuntimeException("Not a direct buffer"); 84 } 85 buffers.add(b); // make sure the buffer does not get GCed 86 return ((DirectBuffer)b).address(); 87 } 88 89 private static void testSetByteArray(Unsafe unsafe) throws Exception { 90 System.out.println("Testing setMemory() for byte[]..."); 91 byte[] b = new byte[BUFFER_SIZE]; 92 for (int i = 0; i < N; i++) { 93 set(b, 0, BUFFER_SIZE, FILLER); 94 int ofs = random.nextInt(BUFFER_SIZE / 2); 95 int len = random.nextInt(BUFFER_SIZE / 2); 96 int val = random.nextInt(256); 97 unsafe.setMemory(b, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs, len, (byte)val); 98 check(b, 0, ofs - 1, FILLER); 99 check(b, ofs, len, val); 100 check(b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER); 101 } 102 } 103 104 private static void testSetRawMemory(Unsafe unsafe) throws Exception { 105 System.out.println("Testing setMemory() for raw memory..."); 106 long b = getMemory(BUFFER_SIZE); 107 for (int i = 0; i < N; i++) { 108 set(unsafe, b, 0, BUFFER_SIZE, FILLER); 109 int ofs = random.nextInt(BUFFER_SIZE / 2); 110 int len = random.nextInt(BUFFER_SIZE / 2); 111 int val = random.nextInt(256); 112 unsafe.setMemory(null, b + ofs, len, (byte)val); 113 check(unsafe, b, 0, ofs - 1, FILLER); 114 check(unsafe, b, ofs, len, val); 115 check(unsafe, b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER); 116 } 117 } 118 119 private static void testCopyByteArrayToByteArray(Unsafe unsafe) throws Exception { 120 System.out.println("Testing copyMemory() for byte[] to byte[]..."); 121 byte[] b1 = new byte[BUFFER_SIZE]; 122 byte[] b2 = new byte[BUFFER_SIZE]; 123 for (int i = 0; i < N; i++) { 124 set(b1, 0, BUFFER_SIZE, FILLER); 125 set(b2, 0, BUFFER_SIZE, FILLER2); 126 int ofs = random.nextInt(BUFFER_SIZE / 2); 127 int len = random.nextInt(BUFFER_SIZE / 2); 128 int val = random.nextInt(256); 129 set(b1, ofs, len, val); 130 int ofs2 = random.nextInt(BUFFER_SIZE / 2); 131 unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs, 132 b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len); 133 check(b2, 0, ofs2 - 1, FILLER2); 134 check(b2, ofs2, len, val); 135 check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2); 136 } 137 } 138 139 private static void testCopyByteArrayToRawMemory(Unsafe unsafe) throws Exception { 140 System.out.println("Testing copyMemory() for byte[] to raw memory..."); 141 byte[] b1 = new byte[BUFFER_SIZE]; 142 long b2 = getMemory(BUFFER_SIZE); 143 for (int i = 0; i < N; i++) { 144 set(b1, 0, BUFFER_SIZE, FILLER); 145 set(unsafe, b2, 0, BUFFER_SIZE, FILLER2); 146 int ofs = random.nextInt(BUFFER_SIZE / 2); 147 int len = random.nextInt(BUFFER_SIZE / 2); 148 int val = random.nextInt(256); 149 set(b1, ofs, len, val); 150 int ofs2 = random.nextInt(BUFFER_SIZE / 2); 151 unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs, 152 null, b2 + ofs2, len); 153 check(unsafe, b2, 0, ofs2 - 1, FILLER2); 154 check(unsafe, b2, ofs2, len, val); 155 check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2); 156 } 157 } 158 159 private static void testCopyRawMemoryToByteArray(Unsafe unsafe) throws Exception { 160 System.out.println("Testing copyMemory() for raw memory to byte[]..."); 161 long b1 = getMemory(BUFFER_SIZE); 162 byte[] b2 = new byte[BUFFER_SIZE]; 163 for (int i = 0; i < N; i++) { 164 set(unsafe, b1, 0, BUFFER_SIZE, FILLER); 165 set(b2, 0, BUFFER_SIZE, FILLER2); 166 int ofs = random.nextInt(BUFFER_SIZE / 2); 167 int len = random.nextInt(BUFFER_SIZE / 2); 168 int val = random.nextInt(256); 169 set(unsafe, b1, ofs, len, val); 170 int ofs2 = random.nextInt(BUFFER_SIZE / 2); 171 unsafe.copyMemory(null, b1 + ofs, 172 b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len); 173 check(b2, 0, ofs2 - 1, FILLER2); 174 check(b2, ofs2, len, val); 175 check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2); 176 } 177 } 178 179 private static void testCopyRawMemoryToRawMemory(Unsafe unsafe) throws Exception { 180 System.out.println("Testing copyMemory() for raw memory to raw memory..."); 181 long b1 = getMemory(BUFFER_SIZE); 182 long b2 = getMemory(BUFFER_SIZE); 183 for (int i = 0; i < N; i++) { 184 set(unsafe, b1, 0, BUFFER_SIZE, FILLER); 185 set(unsafe, b2, 0, BUFFER_SIZE, FILLER2); 186 int ofs = random.nextInt(BUFFER_SIZE / 2); 187 int len = random.nextInt(BUFFER_SIZE / 2); 188 int val = random.nextInt(256); 189 set(unsafe, b1, ofs, len, val); 190 int ofs2 = random.nextInt(BUFFER_SIZE / 2); 191 unsafe.copyMemory(null, b1 + ofs, 192 null, b2 + ofs2, len); 193 check(unsafe, b2, 0, ofs2 - 1, FILLER2); 194 check(unsafe, b2, ofs2, len, val); 195 check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2); 196 } 197 } 198 199 private static Unsafe getUnsafe() throws Exception { 200 Field f = Unsafe.class.getDeclaredField("theUnsafe"); 201 f.setAccessible(true); 202 return (Unsafe)f.get(null); 203 } 204 205 public static void main(String[] args) throws Exception { 206 Unsafe unsafe = getUnsafe(); 207 208 testSetByteArray(unsafe); 209 testSetRawMemory(unsafe); 210 testCopyByteArrayToByteArray(unsafe); 211 testCopyByteArrayToRawMemory(unsafe); 212 testCopyRawMemoryToByteArray(unsafe); 213 testCopyRawMemoryToRawMemory(unsafe); 214 215 System.out.println("OK"); 216 } 217 218 }