Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/misc/UnsafeFieldOffsets.java
41145 views
1
/*
2
* Copyright (c) 2020, 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 8238358 8247444
26
* @summary Ensure that sun.misc.Unsafe::objectFieldOffset and staticFieldOffset
27
* throw UnsupportedOperationException on Field of a hidden or record class
28
* @modules jdk.unsupported
29
* @compile --enable-preview -source ${jdk.version} UnsafeFieldOffsets.java
30
* @run testng/othervm --enable-preview UnsafeFieldOffsets
31
*/
32
33
import sun.misc.Unsafe;
34
35
import java.io.IOException;
36
import java.io.UncheckedIOException;
37
import java.lang.invoke.MethodHandles;
38
import java.lang.reflect.Field;
39
import java.nio.file.Files;
40
import java.nio.file.Path;
41
import java.nio.file.Paths;
42
43
import org.testng.annotations.Test;
44
import static org.testng.Assert.*;
45
46
public class UnsafeFieldOffsets {
47
static class Fields {
48
static final Object STATIC_FINAL = new Object();
49
static Object STATIC_NON_FINAL = new Object();
50
final Object FINAL = new Object();
51
Object NON_FINAL = new Object();
52
}
53
record TestRecord(int i) {
54
static final Object STATIC_FINAL = new Object();
55
static Object STATIC_NON_FINAL = new Object();
56
}
57
58
private static Unsafe UNSAFE = getUnsafe();
59
private static final Class<?> HIDDEN_CLASS = defineHiddenClass();
60
private static final Class<?> RECORD_CLASS = TestRecord.class;
61
62
private static Unsafe getUnsafe() {
63
try {
64
Field f = Unsafe.class.getDeclaredField("theUnsafe");
65
f.setAccessible(true);
66
return (Unsafe) f.get(null);
67
} catch (ReflectiveOperationException e) {
68
throw new RuntimeException(e);
69
}
70
}
71
72
private static Class<?> defineHiddenClass() {
73
String classes = System.getProperty("test.classes");
74
Path cf = Paths.get(classes, "UnsafeFieldOffsets$Fields.class");
75
try {
76
byte[] bytes = Files.readAllBytes(cf);
77
Class<?> c = MethodHandles.lookup().defineHiddenClass(bytes, true).lookupClass();
78
assertTrue(c.isHidden());
79
return c;
80
} catch (IOException e) {
81
throw new UncheckedIOException(e);
82
} catch (IllegalAccessException e) {
83
throw new RuntimeException(e);
84
}
85
}
86
87
@Test
88
public void testNormalClass() throws Throwable {
89
// hidden class
90
testStaticField(HIDDEN_CLASS, "STATIC_FINAL");
91
testStaticField(HIDDEN_CLASS, "STATIC_NON_FINAL");
92
testInstanceField(HIDDEN_CLASS, "FINAL");
93
testInstanceField(HIDDEN_CLASS, "NON_FINAL");
94
}
95
96
@Test
97
public void testHiddenClass() throws Throwable {
98
// hidden class
99
testStaticField(HIDDEN_CLASS, "STATIC_FINAL");
100
testStaticField(HIDDEN_CLASS, "STATIC_NON_FINAL");
101
testInstanceField(HIDDEN_CLASS, "FINAL");
102
testInstanceField(HIDDEN_CLASS, "NON_FINAL");
103
}
104
105
@Test
106
public void testRecordClass() throws Throwable {
107
// record class
108
testRecordStaticField(RECORD_CLASS, "STATIC_FINAL");
109
testRecordStaticField(RECORD_CLASS, "STATIC_NON_FINAL");
110
testRecordInstanceField(RECORD_CLASS, "i");
111
}
112
113
private static void testStaticField(Class<?> c, String name) throws Exception {
114
Field f = c.getDeclaredField(name);
115
try {
116
UNSAFE.staticFieldOffset(f);
117
assertFalse(c.isHidden(), "Expected UOE thrown: " + c);
118
} catch (UnsupportedOperationException e) {
119
assertTrue(c.isHidden(), "Expected hidden class: " + c);
120
}
121
}
122
123
private static void testInstanceField(Class<?> c, String name) throws Exception {
124
Field f = c.getDeclaredField(name);
125
try {
126
UNSAFE.objectFieldOffset(f);
127
assertFalse(c.isHidden(), "Expected UOE thrown: " + c);
128
} catch (UnsupportedOperationException e) {
129
assertTrue(c.isHidden(), "Expected hidden class: " + c);
130
}
131
}
132
133
@SuppressWarnings("preview")
134
private static void testRecordStaticField(Class<?> c, String name) throws Exception {
135
Field f = c.getDeclaredField(name);
136
try {
137
UNSAFE.staticFieldOffset(f);
138
assertFalse(c.isRecord(), "Expected UOE thrown: " + c);
139
} catch (UnsupportedOperationException e) {
140
assertTrue(c.isRecord(), "Expected record class: " + c);
141
}
142
}
143
144
@SuppressWarnings("preview")
145
private static void testRecordInstanceField(Class<?> c, String name) throws Exception {
146
Field f = c.getDeclaredField(name);
147
try {
148
UNSAFE.objectFieldOffset(f);
149
assertFalse(c.isRecord(), "Expected UOE thrown: " + c);
150
} catch (UnsupportedOperationException e) {
151
assertTrue(c.isRecord(), "Expected record class: " + c);
152
}
153
}
154
}
155
156