Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/foreign/TestDowncall.java
66643 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
25
/*
26
* @test
27
* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
28
* @modules jdk.incubator.foreign/jdk.internal.foreign
29
* @build NativeTestHelper CallGeneratorHelper TestDowncall
30
*
31
* @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies
32
* --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=17
33
* TestDowncall
34
*
35
* @run testng/othervm -Xint -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies
36
* --enable-native-access=ALL-UNNAMED -Dgenerator.sample.factor=100000
37
* TestDowncall
38
*/
39
40
import jdk.incubator.foreign.CLinker;
41
import jdk.incubator.foreign.FunctionDescriptor;
42
import jdk.incubator.foreign.SymbolLookup;
43
import jdk.incubator.foreign.MemoryAddress;
44
import jdk.incubator.foreign.MemoryLayout;
45
46
import java.lang.invoke.MethodHandle;
47
import java.lang.invoke.MethodType;
48
import java.util.ArrayList;
49
import java.util.List;
50
import java.util.function.Consumer;
51
52
import jdk.incubator.foreign.MemorySegment;
53
import jdk.incubator.foreign.SegmentAllocator;
54
import org.testng.annotations.*;
55
import static org.testng.Assert.*;
56
57
public class TestDowncall extends CallGeneratorHelper {
58
59
static CLinker abi = CLinker.getInstance();
60
static {
61
System.loadLibrary("TestDowncall");
62
}
63
64
static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();
65
66
@Test(dataProvider="functions", dataProviderClass=CallGeneratorHelper.class)
67
public void testDowncall(int count, String fName, Ret ret, List<ParamType> paramTypes, List<StructFieldType> fields) throws Throwable {
68
List<Consumer<Object>> checks = new ArrayList<>();
69
MemoryAddress addr = LOOKUP.lookup(fName).get();
70
MethodType mt = methodType(ret, paramTypes, fields);
71
FunctionDescriptor descriptor = function(ret, paramTypes, fields);
72
Object[] args = makeArgs(paramTypes, fields, checks);
73
try (NativeScope scope = new NativeScope()) {
74
boolean needsScope = mt.returnType().equals(MemorySegment.class);
75
Object res = doCall(addr, scope, mt, descriptor, args);
76
if (ret == Ret.NON_VOID) {
77
checks.forEach(c -> c.accept(res));
78
if (needsScope) {
79
// check that return struct has indeed been allocated in the native scope
80
assertEquals(((MemorySegment) res).scope(), scope.scope());
81
assertEquals(scope.allocatedBytes(), descriptor.returnLayout().get().byteSize());
82
} else {
83
// if here, there should be no allocation through the scope!
84
assertEquals(scope.allocatedBytes(), 0L);
85
}
86
} else {
87
// if here, there should be no allocation through the scope!
88
assertEquals(scope.allocatedBytes(), 0L);
89
}
90
}
91
}
92
93
@Test(dataProvider="functions", dataProviderClass=CallGeneratorHelper.class)
94
public void testDowncallNoScope(int count, String fName, Ret ret, List<ParamType> paramTypes, List<StructFieldType> fields) throws Throwable {
95
List<Consumer<Object>> checks = new ArrayList<>();
96
MemoryAddress addr = LOOKUP.lookup(fName).get();
97
MethodType mt = methodType(ret, paramTypes, fields);
98
FunctionDescriptor descriptor = function(ret, paramTypes, fields);
99
Object[] args = makeArgs(paramTypes, fields, checks);
100
boolean needsScope = mt.returnType().equals(MemorySegment.class);
101
Object res = doCall(addr, IMPLICIT_ALLOCATOR, mt, descriptor, args);
102
if (ret == Ret.NON_VOID) {
103
checks.forEach(c -> c.accept(res));
104
if (needsScope) {
105
// check that return struct has indeed been allocated in the default scope
106
try {
107
((MemorySegment)res).scope().close(); // should throw
108
fail("Expected exception!");
109
} catch (UnsupportedOperationException ex) {
110
// ok
111
}
112
}
113
}
114
}
115
116
Object doCall(MemoryAddress addr, SegmentAllocator allocator, MethodType type, FunctionDescriptor descriptor, Object[] args) throws Throwable {
117
MethodHandle mh = abi.downcallHandle(addr, allocator, type, descriptor);
118
Object res = mh.invokeWithArguments(args);
119
return res;
120
}
121
122
static MethodType methodType(Ret ret, List<ParamType> params, List<StructFieldType> fields) {
123
MethodType mt = ret == Ret.VOID ?
124
MethodType.methodType(void.class) : MethodType.methodType(paramCarrier(params.get(0).layout(fields)));
125
for (ParamType p : params) {
126
mt = mt.appendParameterTypes(paramCarrier(p.layout(fields)));
127
}
128
return mt;
129
}
130
131
static FunctionDescriptor function(Ret ret, List<ParamType> params, List<StructFieldType> fields) {
132
MemoryLayout[] paramLayouts = params.stream().map(p -> p.layout(fields)).toArray(MemoryLayout[]::new);
133
return ret == Ret.VOID ?
134
FunctionDescriptor.ofVoid(paramLayouts) :
135
FunctionDescriptor.of(paramLayouts[0], paramLayouts);
136
}
137
138
static Object[] makeArgs(List<ParamType> params, List<StructFieldType> fields, List<Consumer<Object>> checks) throws ReflectiveOperationException {
139
Object[] args = new Object[params.size()];
140
for (int i = 0 ; i < params.size() ; i++) {
141
args[i] = makeArg(params.get(i).layout(fields), checks, i == 0);
142
}
143
return args;
144
}
145
}
146
147