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  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestVarArgs
 29  */
 30 
 31 import jdk.incubator.foreign.CLinker;
 32 import jdk.incubator.foreign.FunctionDescriptor;
 33 import jdk.incubator.foreign.MemoryAddress;
 34 import jdk.incubator.foreign.MemoryLayout;
 35 import jdk.incubator.foreign.MemorySegment;
 36 import jdk.incubator.foreign.ResourceScope;
 37 import jdk.incubator.foreign.SymbolLookup;
 38 import jdk.incubator.foreign.ValueLayout;
 39 import org.testng.annotations.DataProvider;
 40 import org.testng.annotations.Test;
 41 
 42 import java.lang.invoke.MethodHandle;
 43 import java.lang.invoke.MethodType;
 44 import java.lang.invoke.VarHandle;
 45 import java.util.ArrayList;
 46 import java.util.List;
 47 
 48 import static jdk.incubator.foreign.CLinker.*;
 49 import static jdk.incubator.foreign.MemoryLayout.PathElement.*;
 50 import static org.testng.Assert.assertEquals;
 51 
 52 public class TestVarArgs {
 53 
 54     static final MemoryLayout ML_CallInfo = MemoryLayout.structLayout(
 55             C_POINTER.withName("writeback"), // writeback
 56             C_POINTER.withName("argIDs")); // arg ids
 57 
 58     static final VarHandle VH_CallInfo_writeback = ML_CallInfo.varHandle(long.class, groupElement("writeback"));
 59     static final VarHandle VH_CallInfo_argIDs = ML_CallInfo.varHandle(long.class, groupElement("argIDs"));
 60 
 61     static final VarHandle VH_IntArray = MemoryLayout.sequenceLayout(C_INT).varHandle(int.class, sequenceElement());
 62 
 63     static final CLinker abi = CLinker.getInstance();
 64     static {
 65         System.loadLibrary("VarArgs");
 66     }
 67 
 68     static final MemoryAddress VARARGS_ADDR =
 69             SymbolLookup.loaderLookup()
 70                     .lookup("varargs").get();
 71 
 72     static final int WRITEBACK_BYTES_PER_ARG = 8;
 73 
 74     @Test(dataProvider = "args")
 75     public void testVarArgs(List<VarArg> args) throws Throwable {
 76         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 77             MemorySegment writeBack = MemorySegment.allocateNative(args.size() * WRITEBACK_BYTES_PER_ARG, WRITEBACK_BYTES_PER_ARG, scope);
 78             MemorySegment callInfo = MemorySegment.allocateNative(ML_CallInfo, scope);
 79             MemorySegment argIDs = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(args.size(), C_INT), scope);
 80 
 81             MemoryAddress callInfoPtr = callInfo.address();
 82 
 83             VH_CallInfo_writeback.set(callInfo, writeBack.address().toRawLongValue());
 84             VH_CallInfo_argIDs.set(callInfo, argIDs.address().toRawLongValue());
 85 
 86             for (int i = 0; i < args.size(); i++) {
 87                 VH_IntArray.set(argIDs, (long) i, args.get(i).id.ordinal());
 88             }
 89 
 90             List<MemoryLayout> argLayouts = new ArrayList<>();
 91             argLayouts.add(C_POINTER); // call info
 92             argLayouts.add(C_INT); // size
 93             args.forEach(a -> argLayouts.add(asVarArg(a.layout)));
 94 
 95             FunctionDescriptor desc = FunctionDescriptor.ofVoid(argLayouts.toArray(MemoryLayout[]::new));
 96 
 97             List<Class<?>> carriers = new ArrayList<>();
 98             carriers.add(MemoryAddress.class); // call info
 99             carriers.add(int.class); // size
100             args.forEach(a -> carriers.add(a.carrier));
101 
102             MethodType mt = MethodType.methodType(void.class, carriers);
103 
104             MethodHandle downcallHandle = abi.downcallHandle(VARARGS_ADDR, mt, desc);
105 
106             List<Object> argValues = new ArrayList<>();
107             argValues.add(callInfoPtr); // call info
108             argValues.add(args.size());  // size
109             args.forEach(a -> argValues.add(a.value));
110 
111             downcallHandle.invokeWithArguments(argValues);
112 
113             for (int i = 0; i < args.size(); i++) {
114                 VarArg a = args.get(i);
115                 MemorySegment writtenPtr = writeBack.asSlice(i * WRITEBACK_BYTES_PER_ARG);
116                 Object written = a.vh.get(writtenPtr);
117                 assertEquals(written, a.value);
118             }
119         }
120     }
121 
122     @DataProvider
123     public static Object[][] args() {
124         return new Object[][] {
125             new Object[] { List.of(VarArg.intArg(5), VarArg.intArg(10), VarArg.intArg(15)) },
126             new Object[] { List.of(VarArg.doubleArg(5), VarArg.doubleArg(10), VarArg.doubleArg(15)) },
127             new Object[] { List.of(VarArg.intArg(5), VarArg.doubleArg(10), VarArg.intArg(15)) },
128         };
129     }
130 
131     private static final class VarArg {
132         final NativeType id;
133         final Object value;
134         final ValueLayout layout;
135         final Class<?> carrier;
136         final VarHandle vh;
137 
138         private VarArg(NativeType id, ValueLayout layout, Class<?> carrier, Object value) {
139             this.id = id;
140             this.value = value;
141             this.layout = layout;
142             this.carrier = carrier;
143             this.vh = layout.varHandle(carrier);
144         }
145 
146         static VarArg intArg(int value) {
147             return new VarArg(VarArg.NativeType.INT, C_INT, int.class, value);
148         }
149 
150         static VarArg doubleArg(double value) {
151             return new VarArg(VarArg.NativeType.DOUBLE, C_DOUBLE, double.class, value);
152         }
153 
154         enum NativeType {
155             INT,
156             DOUBLE
157         }
158     }
159 
160 }