/
MinimizedTestVarArgs.java
580 lines (516 loc) · 20.4 KB
/
MinimizedTestVarArgs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
/*
* Extracted from https://github.com/openjdk/jdk/blob/18cd16d2eae2ee624827eb86621f3a4ffd98fe8c/test/jdk/java/foreign/TestVarArgs.java
* by removing base classes and testng dependencies
*
* - javac --enable-preview --release 20 MinimizedTestVarArgs.java
* - java --enable-preview MinimizedTestVarArgs
*
* Related Documentation:
* - https://nipafx.dev/enable-preview-language-features/
*/
import java.lang.foreign.Addressable;
import java.lang.foreign.Linker;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.MemoryAddress;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.MemorySession;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.function.Consumer;
import static java.lang.foreign.MemoryLayout.PathElement.*;
public class MinimizedTestVarArgs {
public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT.withBitAlignment(32);
public static final ValueLayout.OfAddress C_POINTER = ValueLayout.ADDRESS.withBitAlignment(64);
public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT.withBitAlignment(32);
public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE.withBitAlignment(64);
static final int SAMPLE_FACTOR = Integer.parseInt((String)System.getProperties().getOrDefault("generator.sample.factor", "-1"));
static final int MAX_FIELDS = 3;
static final int MAX_PARAMS = 3;
static final int CHUNK_SIZE = 600;
static final VarHandle VH_IntArray = C_INT.arrayElementVarHandle();
static final MethodHandle MH_CHECK;
static final Linker LINKER = Linker.nativeLinker();
static {
System.loadLibrary("VarArgs");
try {
MH_CHECK = MethodHandles.lookup().findStatic(MinimizedTestVarArgs.class, "check",
MethodType.methodType(void.class, int.class, MemoryAddress.class, List.class));
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Addressable findNativeOrThrow(String name) {
return SymbolLookup.loaderLookup().lookup(name).orElseThrow();
}
static final Addressable VARARGS_ADDR = findNativeOrThrow("varargs");
private static void assertEquals(Object found, Object expected) throws Exception {
if (found != expected &&
((found != null && !found.equals(expected)) ||
(expected != null && !expected.equals(found)))) {
throw new Exception("Expected " + expected + " but found " + found);
}
}
private static void assertTrue(boolean condition) throws Exception {
assertEquals(condition, true);
}
public static boolean isIntegral(MemoryLayout layout) {
return layout instanceof ValueLayout valueLayout && isIntegral(valueLayout.carrier());
}
static boolean isIntegral(Class<?> clazz) {
return clazz == byte.class || clazz == char.class || clazz == short.class
|| clazz == int.class || clazz == long.class;
}
public static boolean isPointer(MemoryLayout layout) {
return layout instanceof ValueLayout valueLayout && valueLayout.carrier() == MemoryAddress.class;
}
public static void main(String[] args) throws Throwable {
var tests = functions();
for (int i = 0; i < tests.length; i++) {
var params = tests[i];
int count = (int)params[0];
String fName = (String)params[1];
Ret ret = (Ret)params[2];
List<ParamType> paramTypes = (List<ParamType>)params[3];
List<StructFieldType> fields = (List<StructFieldType>)params[4];
try {
testVarArgs(count, fName, ret, paramTypes, fields);
}
catch (Exception ex) {
System.out.println(ex);
}
}
System.out.println("Test complete");
}
public static void testVarArgs(int count, String fName, Ret ret, // ignore this stuff
List<ParamType> paramTypes, List<StructFieldType> fields) throws Throwable {
List<Arg> args = makeArgs(paramTypes, fields);
try (MemorySession session = MemorySession.openConfined()) {
MethodHandle checker = MethodHandles.insertArguments(MH_CHECK, 2, args);
MemorySegment writeBack = LINKER.upcallStub(checker, FunctionDescriptor.ofVoid(C_INT, C_POINTER), session);
MemorySegment callInfo = MemorySegment.allocateNative(CallInfo.LAYOUT, session);
MemorySegment argIDs = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(args.size(), C_INT), session);
MemoryAddress callInfoPtr = callInfo.address();
CallInfo.writeback(callInfo, writeBack);
CallInfo.argIDs(callInfo, argIDs);
for (int i = 0; i < args.size(); i++) {
VH_IntArray.set(argIDs, (long) i, args.get(i).id.ordinal());
}
List<MemoryLayout> argLayouts = new ArrayList<>();
argLayouts.add(C_POINTER); // call info
argLayouts.add(C_INT); // size
FunctionDescriptor desc = FunctionDescriptor.ofVoid(argLayouts.toArray(MemoryLayout[]::new))
.asVariadic(args.stream().map(a -> a.layout).toArray(MemoryLayout[]::new));
MethodHandle downcallHandle = LINKER.downcallHandle(VARARGS_ADDR, desc);
List<Object> argValues = new ArrayList<>();
argValues.add(callInfoPtr); // call info
argValues.add(args.size()); // size
args.forEach(a -> argValues.add(a.value));
downcallHandle.invokeWithArguments(argValues);
// args checked by upcall
}
}
private static List<Arg> makeArgs(List<ParamType> paramTypes, List<StructFieldType> fields) throws Exception {
List<Arg> args = new ArrayList<>();
for (ParamType pType : paramTypes) {
MemoryLayout layout = pType.layout(fields);
List<Consumer<Object>> checks = new ArrayList<>();
Object arg = makeArg(layout, checks, true);
Arg.NativeType type = Arg.NativeType.of(pType.type(fields));
args.add(pType == ParamType.STRUCT
? Arg.structArg(type, layout, arg, checks)
: Arg.primitiveArg(type, layout, arg, checks));
}
return args;
}
//helper methods
@SuppressWarnings("unchecked")
static Object makeArg(MemoryLayout layout, List<Consumer<Object>> checks, boolean check) throws Exception {
if (layout instanceof GroupLayout) {
MemorySegment segment = MemorySegment.allocateNative(layout, MemorySession.openImplicit());
initStruct(segment, (GroupLayout)layout, checks, check);
return segment;
} else if (isPointer(layout)) {
MemorySegment segment = MemorySegment.allocateNative(1, MemorySession.openImplicit());
if (check) {
checks.add(o -> {
try {
assertEquals(o, segment.address());
} catch (Throwable ex) {
throw new IllegalStateException(ex);
}
});
}
return segment.address();
} else if (layout instanceof ValueLayout) {
if (isIntegral(layout)) {
if (check) {
checks.add(o -> {
try {
assertEquals(o, 42);
} catch (Exception ex) {
System.out.println(ex);
}
});
}
return 42;
} else if (layout.bitSize() == 32) {
if (check) {
checks.add(o -> {
try {
assertEquals(o, 12f);
} catch (Exception ex) {
System.out.println(ex);
}
});
}
return 12f;
} else {
if (check) {
checks.add(o -> {
try {
assertEquals(o, 24d);
} catch (Exception ex) {
System.out.println(ex);
}
});
}
return 24d;
}
} else {
throw new IllegalStateException("Unexpected layout: " + layout);
}
}
static void initStruct(MemorySegment str, GroupLayout g, List<Consumer<Object>> checks, boolean check) throws Exception {
for (MemoryLayout l : g.memberLayouts()) {
if (l.isPadding()) continue;
VarHandle accessor = g.varHandle(MemoryLayout.PathElement.groupElement(l.name().get()));
List<Consumer<Object>> fieldsCheck = new ArrayList<>();
Object value = makeArg(l, fieldsCheck, check);
//set value
accessor.set(str, value);
//add check
if (check) {
assertTrue(fieldsCheck.size() == 1);
checks.add(o -> {
MemorySegment actual = (MemorySegment)o;
try {
fieldsCheck.get(0).accept(accessor.get(actual));
} catch (Throwable ex) {
throw new IllegalStateException(ex);
}
});
}
}
}
private static void check(int index, MemoryAddress ptr, List<Arg> args) throws Exception {
Arg varArg = args.get(index);
MemoryLayout layout = varArg.layout;
MethodHandle getter = varArg.getter;
List<Consumer<Object>> checks = varArg.checks;
try (MemorySession session = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.ofAddress(ptr, layout.byteSize(), session);
Object obj = getter.invoke(seg);
checks.forEach(check -> check.accept(obj));
} catch (Throwable e) {
throw new Exception(e);
}
}
private static class CallInfo {
static final MemoryLayout LAYOUT = MemoryLayout.structLayout(
C_POINTER.withName("writeback"), // writeback
C_POINTER.withName("argIDs")); // arg ids
static final VarHandle VH_writeback = LAYOUT.varHandle(groupElement("writeback"));
static final VarHandle VH_argIDs = LAYOUT.varHandle(groupElement("argIDs"));
static void writeback(MemorySegment seg, Addressable addr) {
VH_writeback.set(seg, addr.address());
}
static void argIDs(MemorySegment seg, Addressable addr) {
VH_argIDs.set(seg, addr.address());
}
}
private static final class Arg {
final NativeType id;
final MemoryLayout layout;
final Object value;
final MethodHandle getter;
final List<Consumer<Object>> checks;
private Arg(NativeType id, MemoryLayout layout, Object value, MethodHandle getter, List<Consumer<Object>> checks) {
this.id = id;
this.layout = layout;
this.value = value;
this.getter = getter;
this.checks = checks;
}
private static Arg primitiveArg(NativeType id, MemoryLayout layout, Object value, List<Consumer<Object>> checks) {
return new Arg(id, layout, value, layout.varHandle().toMethodHandle(VarHandle.AccessMode.GET), checks);
}
private static Arg structArg(NativeType id, MemoryLayout layout, Object value, List<Consumer<Object>> checks) {
return new Arg(id, layout, value, MethodHandles.identity(MemorySegment.class), checks);
}
enum NativeType {
INT,
FLOAT,
DOUBLE,
POINTER,
S_I,
S_F,
S_D,
S_P,
S_II,
S_IF,
S_ID,
S_IP,
S_FI,
S_FF,
S_FD,
S_FP,
S_DI,
S_DF,
S_DD,
S_DP,
S_PI,
S_PF,
S_PD,
S_PP,
S_III,
S_IIF,
S_IID,
S_IIP,
S_IFI,
S_IFF,
S_IFD,
S_IFP,
S_IDI,
S_IDF,
S_IDD,
S_IDP,
S_IPI,
S_IPF,
S_IPD,
S_IPP,
S_FII,
S_FIF,
S_FID,
S_FIP,
S_FFI,
S_FFF,
S_FFD,
S_FFP,
S_FDI,
S_FDF,
S_FDD,
S_FDP,
S_FPI,
S_FPF,
S_FPD,
S_FPP,
S_DII,
S_DIF,
S_DID,
S_DIP,
S_DFI,
S_DFF,
S_DFD,
S_DFP,
S_DDI,
S_DDF,
S_DDD,
S_DDP,
S_DPI,
S_DPF,
S_DPD,
S_DPP,
S_PII,
S_PIF,
S_PID,
S_PIP,
S_PFI,
S_PFF,
S_PFD,
S_PFP,
S_PDI,
S_PDF,
S_PDD,
S_PDP,
S_PPI,
S_PPF,
S_PPD,
S_PPP,
;
public static NativeType of(String type) {
return NativeType.valueOf(switch (type) {
case "int" -> "INT";
case "float" -> "FLOAT";
case "double" -> "DOUBLE";
case "void*" -> "POINTER";
default -> type.substring("struct ".length());
});
}
}
}
enum Ret {
VOID,
NON_VOID
}
enum StructFieldType {
INT("int", C_INT),
FLOAT("float", C_FLOAT),
DOUBLE("double", C_DOUBLE),
POINTER("void*", C_POINTER);
final String typeStr;
final MemoryLayout layout;
StructFieldType(String typeStr, MemoryLayout layout) {
this.typeStr = typeStr;
this.layout = layout;
}
MemoryLayout layout() {
return layout;
}
@SuppressWarnings("unchecked")
static List<List<StructFieldType>>[] perms = new List[10];
static List<List<StructFieldType>> perms(int i) {
if (perms[i] == null) {
perms[i] = generateTest(i, values());
}
return perms[i];
}
}
enum ParamType {
INT("int", C_INT),
FLOAT("float", C_FLOAT),
DOUBLE("double", C_DOUBLE),
POINTER("void*", C_POINTER),
STRUCT("struct S", null);
private final String typeStr;
private final MemoryLayout layout;
ParamType(String typeStr, MemoryLayout layout) {
this.typeStr = typeStr;
this.layout = layout;
}
String type(List<StructFieldType> fields) {
return this == STRUCT ?
typeStr + "_" + sigCode(fields) :
typeStr;
}
MemoryLayout layout(List<StructFieldType> fields) {
if (this == STRUCT) {
long offset = 0L;
List<MemoryLayout> layouts = new ArrayList<>();
long align = 0;
for (StructFieldType field : fields) {
MemoryLayout l = field.layout();
long padding = offset % l.bitAlignment();
if (padding != 0) {
layouts.add(MemoryLayout.paddingLayout(padding));
offset += padding;
}
layouts.add(l.withName("field" + offset));
align = Math.max(align, l.bitAlignment());
offset += l.bitSize();
}
long padding = offset % align;
if (padding != 0) {
layouts.add(MemoryLayout.paddingLayout(padding));
}
return MemoryLayout.structLayout(layouts.toArray(new MemoryLayout[0]));
} else {
return layout;
}
}
@SuppressWarnings("unchecked")
static List<List<ParamType>>[] perms = new List[10];
static List<List<ParamType>> perms(int i) {
if (perms[i] == null) {
perms[i] = generateTest(i, values());
}
return perms[i];
}
}
static <Z> List<List<Z>> generateTest(int i, Z[] elems) {
List<List<Z>> res = new ArrayList<>();
generateTest(i, new Stack<>(), elems, res);
return res;
}
static <Z> void generateTest(int i, Stack<Z> combo, Z[] elems, List<List<Z>> results) {
if (i == 0) {
results.add(new ArrayList<>(combo));
} else {
for (Z z : elems) {
combo.push(z);
generateTest(i - 1, combo, elems, results);
combo.pop();
}
}
}
public static Object[][] functions() {
int functions = 0;
List<Object[]> downcalls = new ArrayList<>();
for (Ret r : Ret.values()) {
for (int i = 0; i <= MAX_PARAMS; i++) {
if (r != Ret.VOID && i == 0) continue;
for (List<ParamType> ptypes : ParamType.perms(i)) {
String retCode = r == Ret.VOID ? "V" : ptypes.get(0).name().charAt(0) + "";
String sigCode = sigCode(ptypes);
if (ptypes.contains(ParamType.STRUCT)) {
for (int j = 1; j <= MAX_FIELDS; j++) {
for (List<StructFieldType> fields : StructFieldType.perms(j)) {
String structCode = sigCode(fields);
int count = functions;
int fCode = functions++ / CHUNK_SIZE;
String fName = String.format("f%d_%s_%s_%s", fCode, retCode, sigCode, structCode);
if (SAMPLE_FACTOR == -1 || (count % SAMPLE_FACTOR) == 0) {
downcalls.add(new Object[]{count, fName, r, ptypes, fields});
}
}
}
} else {
String structCode = sigCode(List.<StructFieldType>of());
int count = functions;
int fCode = functions++ / CHUNK_SIZE;
String fName = String.format("f%d_%s_%s_%s", fCode, retCode, sigCode, structCode);
if (SAMPLE_FACTOR == -1 || (count % SAMPLE_FACTOR) == 0) {
downcalls.add(new Object[]{count, fName, r, ptypes, List.of()});
}
}
}
}
}
return downcalls.toArray(new Object[0][]);
}
static <Z extends Enum<Z>> String sigCode(List<Z> elems) {
return elems.stream().map(p -> p.name().charAt(0) + "").collect(Collectors.joining());
}
}