Path: blob/master/test/jdk/sun/misc/CopyMemory.java
41145 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 656554325* @summary Minimal test for unsafe.copyMemory() and unsafe.setMemory()26* @modules java.base/sun.nio.ch27* jdk.unsupported28* @key randomness29*/3031import java.util.*;32import java.lang.reflect.*;33import java.nio.*;3435import sun.misc.Unsafe;3637import sun.nio.ch.DirectBuffer;3839public class CopyMemory {4041private final static int BUFFER_SIZE = 1024;42private final static int N = 16 * 1024;4344private final static int FILLER = 0x55;45private final static int FILLER2 = 0x33;4647private final static Random random = new Random();4849private static void set(byte[] b, int ofs, int len, int value) {50for (int i = 0; i < len; i++) {51b[ofs + i] = (byte)value;52}53}5455private static void check(byte[] b, int ofs, int len, int value) {56for (int i = 0; i < len; i++) {57int r = b[ofs + i] & 0xff;58if (r != value) {59throw new RuntimeException("mismatch");60}61}62}6364private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {65for (int i = 0; i < len; i++) {66unsafe.putByte(null, addr + ofs + i, (byte)value);67}68}6970private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {71for (int i = 0; i < len; i++) {72int r = unsafe.getByte(null, addr + ofs + i) & 0xff;73if (r != value) {74throw new RuntimeException("mismatch");75}76}77}7879private static final List<ByteBuffer> buffers = new ArrayList<ByteBuffer>();8081private static long getMemory(int n) {82ByteBuffer b = ByteBuffer.allocateDirect(n);83if (b instanceof DirectBuffer == false) {84throw new RuntimeException("Not a direct buffer");85}86buffers.add(b); // make sure the buffer does not get GCed87return ((DirectBuffer)b).address();88}8990private static void testSetByteArray(Unsafe unsafe) throws Exception {91System.out.println("Testing setMemory() for byte[]...");92byte[] b = new byte[BUFFER_SIZE];93for (int i = 0; i < N; i++) {94set(b, 0, BUFFER_SIZE, FILLER);95int ofs = random.nextInt(BUFFER_SIZE / 2);96int len = random.nextInt(BUFFER_SIZE / 2);97int val = random.nextInt(256);98unsafe.setMemory(b, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs, len, (byte)val);99check(b, 0, ofs - 1, FILLER);100check(b, ofs, len, val);101check(b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER);102}103}104105private static void testSetRawMemory(Unsafe unsafe) throws Exception {106System.out.println("Testing setMemory() for raw memory...");107long b = getMemory(BUFFER_SIZE);108for (int i = 0; i < N; i++) {109set(unsafe, b, 0, BUFFER_SIZE, FILLER);110int ofs = random.nextInt(BUFFER_SIZE / 2);111int len = random.nextInt(BUFFER_SIZE / 2);112int val = random.nextInt(256);113unsafe.setMemory(null, b + ofs, len, (byte)val);114check(unsafe, b, 0, ofs - 1, FILLER);115check(unsafe, b, ofs, len, val);116check(unsafe, b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER);117}118}119120private static void testCopyByteArrayToByteArray(Unsafe unsafe) throws Exception {121System.out.println("Testing copyMemory() for byte[] to byte[]...");122byte[] b1 = new byte[BUFFER_SIZE];123byte[] b2 = new byte[BUFFER_SIZE];124for (int i = 0; i < N; i++) {125set(b1, 0, BUFFER_SIZE, FILLER);126set(b2, 0, BUFFER_SIZE, FILLER2);127int ofs = random.nextInt(BUFFER_SIZE / 2);128int len = random.nextInt(BUFFER_SIZE / 2);129int val = random.nextInt(256);130set(b1, ofs, len, val);131int ofs2 = random.nextInt(BUFFER_SIZE / 2);132unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,133b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);134check(b2, 0, ofs2 - 1, FILLER2);135check(b2, ofs2, len, val);136check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);137}138}139140private static void testCopyByteArrayToRawMemory(Unsafe unsafe) throws Exception {141System.out.println("Testing copyMemory() for byte[] to raw memory...");142byte[] b1 = new byte[BUFFER_SIZE];143long b2 = getMemory(BUFFER_SIZE);144for (int i = 0; i < N; i++) {145set(b1, 0, BUFFER_SIZE, FILLER);146set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);147int ofs = random.nextInt(BUFFER_SIZE / 2);148int len = random.nextInt(BUFFER_SIZE / 2);149int val = random.nextInt(256);150set(b1, ofs, len, val);151int ofs2 = random.nextInt(BUFFER_SIZE / 2);152unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,153null, b2 + ofs2, len);154check(unsafe, b2, 0, ofs2 - 1, FILLER2);155check(unsafe, b2, ofs2, len, val);156check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);157}158}159160private static void testCopyRawMemoryToByteArray(Unsafe unsafe) throws Exception {161System.out.println("Testing copyMemory() for raw memory to byte[]...");162long b1 = getMemory(BUFFER_SIZE);163byte[] b2 = new byte[BUFFER_SIZE];164for (int i = 0; i < N; i++) {165set(unsafe, b1, 0, BUFFER_SIZE, FILLER);166set(b2, 0, BUFFER_SIZE, FILLER2);167int ofs = random.nextInt(BUFFER_SIZE / 2);168int len = random.nextInt(BUFFER_SIZE / 2);169int val = random.nextInt(256);170set(unsafe, b1, ofs, len, val);171int ofs2 = random.nextInt(BUFFER_SIZE / 2);172unsafe.copyMemory(null, b1 + ofs,173b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);174check(b2, 0, ofs2 - 1, FILLER2);175check(b2, ofs2, len, val);176check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);177}178}179180private static void testCopyRawMemoryToRawMemory(Unsafe unsafe) throws Exception {181System.out.println("Testing copyMemory() for raw memory to raw memory...");182long b1 = getMemory(BUFFER_SIZE);183long b2 = getMemory(BUFFER_SIZE);184for (int i = 0; i < N; i++) {185set(unsafe, b1, 0, BUFFER_SIZE, FILLER);186set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);187int ofs = random.nextInt(BUFFER_SIZE / 2);188int len = random.nextInt(BUFFER_SIZE / 2);189int val = random.nextInt(256);190set(unsafe, b1, ofs, len, val);191int ofs2 = random.nextInt(BUFFER_SIZE / 2);192unsafe.copyMemory(null, b1 + ofs,193null, b2 + ofs2, len);194check(unsafe, b2, 0, ofs2 - 1, FILLER2);195check(unsafe, b2, ofs2, len, val);196check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);197}198}199200private static Unsafe getUnsafe() throws Exception {201Field f = Unsafe.class.getDeclaredField("theUnsafe");202f.setAccessible(true);203return (Unsafe)f.get(null);204}205206public static void main(String[] args) throws Exception {207Unsafe unsafe = getUnsafe();208209testSetByteArray(unsafe);210testSetRawMemory(unsafe);211testCopyByteArrayToByteArray(unsafe);212testCopyByteArrayToRawMemory(unsafe);213testCopyRawMemoryToByteArray(unsafe);214testCopyRawMemoryToRawMemory(unsafe);215216System.out.println("OK");217}218219}220221222