Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/foreign/TestIntrinsics.java
41145 views
1
/*
2
* Copyright (c) 2020, 2021, 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
/*
25
* @test
26
* @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="aarch64"
27
* @run testng/othervm
28
* -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true
29
* -Djdk.internal.foreign.ProgrammableInvoker.USE_INTRINSICS=true
30
* --enable-native-access=ALL-UNNAMED
31
* -Xbatch
32
* TestIntrinsics
33
*/
34
35
import jdk.incubator.foreign.CLinker;
36
import jdk.incubator.foreign.FunctionDescriptor;
37
38
import java.lang.invoke.MethodHandle;
39
import java.lang.invoke.MethodType;
40
import java.util.ArrayList;
41
import java.util.List;
42
43
import jdk.incubator.foreign.MemoryAddress;
44
import jdk.incubator.foreign.MemoryLayout;
45
import jdk.incubator.foreign.SymbolLookup;
46
import org.testng.annotations.*;
47
48
import static java.lang.invoke.MethodType.methodType;
49
import static jdk.incubator.foreign.CLinker.*;
50
import static jdk.incubator.foreign.FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME;
51
import static org.testng.Assert.assertEquals;
52
53
public class TestIntrinsics {
54
55
static final CLinker abi = CLinker.getInstance();
56
static {
57
System.loadLibrary("Intrinsics");
58
}
59
60
static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();
61
62
private interface RunnableX {
63
void run() throws Throwable;
64
}
65
66
@Test(dataProvider = "tests")
67
public void testIntrinsics(RunnableX test) throws Throwable {
68
for (int i = 0; i < 20_000; i++) {
69
test.run();
70
}
71
}
72
73
@DataProvider
74
public Object[][] tests() {
75
List<RunnableX> testsList = new ArrayList<>();
76
77
interface AddTest {
78
void add(MethodHandle target, Object expectedResult, Object... args);
79
}
80
81
AddTest tests = (mh, expectedResult, args) -> testsList.add(() -> {
82
Object actual = mh.invokeWithArguments(args);
83
assertEquals(actual, expectedResult);
84
});
85
86
interface AddIdentity {
87
void add(String name, Class<?> carrier, MemoryLayout layout, Object arg);
88
}
89
90
AddIdentity addIdentity = (name, carrier, layout, arg) -> {
91
MemoryAddress ma = LOOKUP.lookup(name).get();
92
MethodType mt = methodType(carrier, carrier);
93
FunctionDescriptor fd = FunctionDescriptor.of(layout, layout);
94
95
tests.add(abi.downcallHandle(ma, mt, fd), arg, arg);
96
tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), arg, arg);
97
tests.add(abi.downcallHandle(mt, fd), arg, ma, arg);
98
};
99
100
{ // empty
101
MemoryAddress ma = LOOKUP.lookup("empty").get();
102
MethodType mt = methodType(void.class);
103
FunctionDescriptor fd = FunctionDescriptor.ofVoid();
104
tests.add(abi.downcallHandle(ma, mt, fd), null);
105
tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), null);
106
}
107
108
addIdentity.add("identity_char", byte.class, C_CHAR, (byte) 10);
109
addIdentity.add("identity_short", short.class, C_SHORT, (short) 10);
110
addIdentity.add("identity_int", int.class, C_INT, 10);
111
addIdentity.add("identity_long", long.class, C_LONG_LONG, 10L);
112
addIdentity.add("identity_float", float.class, C_FLOAT, 10F);
113
addIdentity.add("identity_double", double.class, C_DOUBLE, 10D);
114
115
{ // identity_va
116
MemoryAddress ma = LOOKUP.lookup("identity_va").get();
117
MethodType mt = methodType(int.class, int.class, double.class, int.class, float.class, long.class);
118
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, asVarArg(C_DOUBLE),
119
asVarArg(C_INT), asVarArg(C_FLOAT), asVarArg(C_LONG_LONG));
120
tests.add(abi.downcallHandle(ma, mt, fd), 1, 1, 10D, 2, 3F, 4L);
121
tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), 1, 1, 10D, 2, 3F, 4L);
122
}
123
124
{ // high_arity
125
MethodType baseMT = methodType(void.class, int.class, double.class, long.class, float.class, byte.class,
126
short.class, char.class);
127
FunctionDescriptor baseFD = FunctionDescriptor.ofVoid(C_INT, C_DOUBLE, C_LONG_LONG, C_FLOAT, C_CHAR,
128
C_SHORT, C_SHORT);
129
Object[] args = {1, 10D, 2L, 3F, (byte) 0, (short) 13, 'a'};
130
for (int i = 0; i < args.length; i++) {
131
MemoryAddress ma = LOOKUP.lookup("invoke_high_arity" + i).get();
132
MethodType mt = baseMT.changeReturnType(baseMT.parameterType(i));
133
FunctionDescriptor fd = baseFD.withReturnLayout(baseFD.argumentLayouts().get(i));
134
Object expected = args[i];
135
tests.add(abi.downcallHandle(ma, mt, fd), expected, args);
136
tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), expected, args);
137
}
138
}
139
140
return testsList.stream().map(rx -> new Object[]{ rx }).toArray(Object[][]::new);
141
}
142
}
143
144