Path: blob/master/test/jdk/jdk/internal/misc/Unsafe/CopyMemory.java
43942 views
/*1* Copyright (c) 2016, 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*/2223import jdk.internal.misc.Unsafe;2425/*26* @test27* @summary Test Unsafe.copyMemory28* @modules java.base/jdk.internal.misc29*/30public class CopyMemory extends CopyCommon {31private CopyMemory() {32}3334/**35* Run positive tests36*37* @throws RuntimeException if an error is found38*/39private void testPositive() {40testSmallCopy(false);41}4243/**44* Run negative tests, testing corner cases and the various exceptions45*46* @throws RuntimeException if an error is found47*/48private void testNegative() {49long bufRaw = 0;5051try {52bufRaw = UNSAFE.allocateMemory(1024);53long buf = CopyCommon.alignUp(bufRaw, CopyCommon.BASE_ALIGNMENT);54short[] arr = new short[16];5556// Check illegal sizes57System.out.println("Testing negative size");58try {59UNSAFE.copyMemory(null, buf, null, buf, -1);60throw new RuntimeException("copyMemory failed to throw IAE for size=-1");61} catch (IllegalArgumentException e) {62// good63}6465System.out.println("Testing negative srcOffset");66try {67// Check that negative srcOffset throws an IAE68UNSAFE.copyMemory(arr, -1, arr, UNSAFE.arrayBaseOffset(arr.getClass()), 16);69throw new RuntimeException("copyMemory failed to throw IAE for srcOffset=-1");70} catch (IllegalArgumentException e) {71// good72}7374System.out.println("Testing negative destOffset");75try {76// Check that negative dstOffset throws an IAE77UNSAFE.copyMemory(arr, UNSAFE.arrayBaseOffset(arr.getClass()), arr, -1, 16);78throw new RuntimeException("copyMemory failed to throw IAE for destOffset=-1");79} catch (IllegalArgumentException e) {80// good81}8283System.out.println("Testing reference array");84try {85// Check that a reference array destination throws IAE86UNSAFE.copyMemory(null, buf, new Object[16], UNSAFE.arrayBaseOffset(Object[].class), 16);87throw new RuntimeException("copyMemory failed to throw IAE");88} catch (IllegalArgumentException e) {89// good90}9192// Check that invalid source & dest pointers throw IAEs (only relevant on 32-bit platforms)93if (UNSAFE.addressSize() == 4) {94long invalidPtr = (long)1 << 35; // Pick a random bit in upper 32 bits9596try {97// Check that an invalid (not 32-bit clean) source pointer throws IAE98UNSAFE.copyMemory(null, invalidPtr, null, buf, 16);99throw new RuntimeException("copyMemory failed to throw IAE for srcOffset 0x" +100Long.toHexString(invalidPtr));101} catch (IllegalArgumentException e) {102// good103}104105try {106// Check that an invalid (not 32-bit clean) source pointer throws IAE107UNSAFE.copyMemory(null, buf, null, invalidPtr, 16);108throw new RuntimeException("copyMemory failed to throw IAE for destOffset 0x" +109Long.toHexString(invalidPtr));110} catch (IllegalArgumentException e) {111// good112}113}114} finally {115if (bufRaw != 0) {116UNSAFE.freeMemory(bufRaw);117}118}119}120121/**122* Run all tests123*124* @throws RuntimeException if an error is found125*/126private void test() {127testPositive();128testNegative();129}130131public static void main(String[] args) {132CopyMemory cs = new CopyMemory();133cs.test();134}135}136137138