42707
|
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 |
/* @test
|
|
25 |
* @bug 8171377
|
|
26 |
* @summary Basic test for Unsafe::invokeCleaner
|
|
27 |
* @modules jdk.unsupported
|
|
28 |
* @run testng/othervm InvokeCleaner
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.Closeable;
|
|
32 |
import java.lang.reflect.Field;
|
|
33 |
import java.nio.ByteBuffer;
|
|
34 |
import java.nio.MappedByteBuffer;
|
|
35 |
import java.nio.channels.FileChannel;
|
|
36 |
import java.nio.file.Files;
|
|
37 |
import java.nio.file.Path;
|
|
38 |
import java.nio.file.Paths;
|
|
39 |
import java.util.ArrayList;
|
|
40 |
import java.util.List;
|
|
41 |
import sun.misc.Unsafe;
|
|
42 |
import org.testng.annotations.AfterClass;
|
|
43 |
import org.testng.annotations.BeforeClass;
|
|
44 |
import org.testng.annotations.Test;
|
|
45 |
import org.testng.annotations.DataProvider;
|
|
46 |
|
|
47 |
public class InvokeCleaner {
|
|
48 |
|
|
49 |
static Unsafe UNSAFE;
|
|
50 |
static Path bob = Paths.get("bob");
|
|
51 |
static List<Closeable> closeables = new ArrayList<>();
|
|
52 |
|
|
53 |
@BeforeClass
|
|
54 |
static void init() throws Exception {
|
|
55 |
UNSAFE = getUnsafe();
|
|
56 |
|
|
57 |
byte[] srcData = new byte[20];
|
|
58 |
for (int i=0; i<20; i++)
|
|
59 |
srcData[i] = (byte)i;
|
|
60 |
Files.write(bob, srcData);
|
|
61 |
}
|
|
62 |
|
|
63 |
@DataProvider(name = "badBuffers")
|
|
64 |
static Object[][] createBadBuffers() throws Exception {
|
|
65 |
FileChannel fc = FileChannel.open(bob);
|
|
66 |
closeables.add(fc);
|
|
67 |
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);
|
|
68 |
|
|
69 |
return new Object[][] {
|
|
70 |
{ ByteBuffer.allocate(0) },
|
|
71 |
{ ByteBuffer.allocate(10) },
|
|
72 |
{ ByteBuffer.allocate(10).duplicate() },
|
|
73 |
{ ByteBuffer.allocate(10).slice() },
|
|
74 |
{ ByteBuffer.allocateDirect(10).duplicate() },
|
|
75 |
{ ByteBuffer.allocateDirect(10).slice() },
|
|
76 |
{ ByteBuffer.allocateDirect(0).duplicate() },
|
|
77 |
{ ByteBuffer.allocateDirect(0).slice() },
|
|
78 |
{ mbb.duplicate() },
|
|
79 |
{ mbb.slice() }
|
|
80 |
};
|
|
81 |
}
|
|
82 |
|
|
83 |
@Test(dataProvider="badBuffers",
|
|
84 |
expectedExceptions = IllegalArgumentException.class)
|
|
85 |
public void badBuffers(ByteBuffer buffer) throws Exception {
|
|
86 |
UNSAFE.invokeCleaner(buffer);
|
|
87 |
}
|
|
88 |
|
|
89 |
@DataProvider(name = "goodBuffers")
|
|
90 |
static Object[][] createGoodBuffers() throws Exception {
|
|
91 |
FileChannel fc = FileChannel.open(bob);
|
|
92 |
closeables.add(fc);
|
|
93 |
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);
|
|
94 |
mbb.load();
|
|
95 |
|
|
96 |
return new Object[][] {
|
|
97 |
{ ByteBuffer.allocateDirect(0) },
|
|
98 |
{ ByteBuffer.allocateDirect(10) },
|
|
99 |
{ mbb },
|
|
100 |
{ fc.map(FileChannel.MapMode.READ_ONLY, 1, 11) }
|
|
101 |
};
|
|
102 |
}
|
|
103 |
|
|
104 |
@Test(dataProvider="goodBuffers")
|
|
105 |
public void goodBuffers(ByteBuffer buffer) throws Exception {
|
|
106 |
UNSAFE.invokeCleaner(buffer);
|
|
107 |
}
|
|
108 |
|
|
109 |
@Test(expectedExceptions = NullPointerException.class)
|
|
110 |
public void npe() throws Exception {
|
|
111 |
UNSAFE.invokeCleaner(null);
|
|
112 |
}
|
|
113 |
|
|
114 |
static Unsafe getUnsafe() throws ReflectiveOperationException {
|
|
115 |
Field f = Unsafe.class.getDeclaredField("theUnsafe");
|
|
116 |
f.setAccessible(true);
|
|
117 |
return (Unsafe)f.get(null);
|
|
118 |
}
|
|
119 |
|
|
120 |
@AfterClass
|
|
121 |
public void cleanup() throws Exception {
|
|
122 |
for(Closeable fc : closeables)
|
|
123 |
fc.close();
|
|
124 |
}
|
|
125 |
}
|