1 /*
   2  * Copyright (c) 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.foreign.abi.x64.windows;
  26 
  27 import jdk.internal.foreign.ScopeImpl;
  28 import jdk.internal.foreign.Util;
  29 import jdk.internal.foreign.abi.*;
  30 import jdk.internal.foreign.memory.LayoutTypeImpl;
  31 import jdk.internal.foreign.memory.Types;
  32 
  33 import java.foreign.Library;
  34 import java.foreign.NativeMethodType;
  35 import java.foreign.NativeTypes;
  36 import java.foreign.Scope;
  37 import java.foreign.layout.Function;
  38 import java.foreign.layout.Layout;
  39 import java.foreign.layout.Value;
  40 import java.foreign.memory.LayoutType;
  41 import java.foreign.memory.Pointer;
  42 import java.foreign.memory.Struct;
  43 import java.lang.invoke.MethodHandle;
  44 import java.lang.invoke.WrongMethodTypeException;
  45 import java.util.Collection;
  46 import java.util.List;
  47 
  48 /**
  49  * ABI implementation based on Windows ABI AMD64 supplement v.0.99.6
  50  */
  51 public class Windowsx64ABI implements SystemABI {
  52 
  53     private static Windowsx64ABI instance;
  54 
  55     public static Windowsx64ABI getInstance() {
  56         if (instance == null) {
  57             instance = new Windowsx64ABI();
  58         }
  59         return instance;
  60     }
  61 
  62     static CallingSequence arrangeCall(NativeMethodType nmt) {
  63         return arrangeCall(nmt, Integer.MAX_VALUE);
  64     }
  65 
  66     static CallingSequence arrangeCall(NativeMethodType nmt, int varArgsStart) {
  67         Function f = nmt.function();
  68         CallingSequenceBuilderImpl builder = new CallingSequenceBuilderImpl(
  69                 f.returnLayout().map(x -> new Argument(-1, x, "__retval")).orElse(null));
  70         for (int i = 0; i < f.argumentLayouts().size(); i++) {
  71             Layout type = f.argumentLayouts().get(i);
  72             builder.addArgument(type, i >= varArgsStart, "arg" + i);
  73         }
  74         if (f.isVariadic()) {
  75             builder.addArgument(Types.POINTER, false, null);
  76         }
  77 
  78         return builder.build();
  79     }
  80 
  81     @Override
  82     public MethodHandle downcallHandle(CallingConvention cc, Library.Symbol symbol, NativeMethodType nmt) {
  83         Util.checkNoArrays(nmt.methodType());
  84         if (nmt.isVarArgs()) {
  85             return VarargsInvokerImpl.make(symbol, nmt);
  86         }
  87 
  88         return UniversalNativeInvokerImpl.make(symbol, arrangeCall(nmt), nmt).getBoundMethodHandle();
  89     }
  90 
  91     @Override
  92     public Library.Symbol upcallStub(CallingConvention cc, MethodHandle target, NativeMethodType nmt) {
  93         Util.checkNoArrays(nmt.methodType());
  94         if (!target.type().equals(nmt.methodType())) {
  95             throw new WrongMethodTypeException("Native method type has wrong type: " + nmt.methodType());
  96         }
  97 
  98         return UpcallStubs.registerUpcallStub(new UniversalUpcallHandlerImpl(target, arrangeCall(nmt), nmt));
  99     }
 100 
 101     @Override
 102     public CallingConvention defaultCallingConvention() {
 103         return null;
 104     }
 105 
 106     @Override
 107     public CallingConvention namedCallingConvention(String name) throws IllegalArgumentException {
 108         return null;
 109     }
 110 
 111     @Override
 112     public Collection<CallingConvention> callingConventions() {
 113         return null;
 114     }
 115 
 116     static void unboxValue(Object o, LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> dstPtrFunc,
 117                            List<ArgumentBinding> bindings) throws Throwable {
 118         if (o instanceof Struct) {
 119             assert (bindings.size() == 1); // always for structs on windows
 120 
 121             Pointer<?> structPtr = ((Struct<?>) o).ptr();
 122             LayoutType<?> structType = structPtr.type();
 123             Pointer<Long> src = Util.unsafeCast(structPtr, NativeTypes.UINT64);
 124             ArgumentBinding binding = bindings.get(0);
 125 
 126             if (CallingSequenceBuilderImpl.isRegisterAggregate(binding.getMember().getType())) { // pass value
 127                 Pointer.copy(src, dstPtrFunc.apply(binding), structType.bytesSize());
 128             } else { // pass a pointer
 129                 /*
 130                  * Leak memory for now
 131                  */
 132                 Scope scope = Scope.globalScope().fork();
 133                 Pointer<?> copy = scope.allocate(structType);
 134                 Pointer.copy(src, copy, structType.bytesSize());
 135 
 136                 Pointer<?> dst = dstPtrFunc.apply(binding);
 137                 Util.unsafeCast(dst, NativeTypes.UINT64).type().setter().invoke(dst, copy.addr());
 138             }
 139         } else {
 140             assert bindings.size() <= 2;
 141             Pointer<?> dst = Util.unsafeCast(dstPtrFunc.apply(bindings.get(0)), type);
 142             dst.type().setter().invoke(dst, o);
 143         }
 144     }
 145 
 146     static Object boxValue(LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> srcPtrFunc,
 147                            List<ArgumentBinding> bindings) throws IllegalAccessException {
 148         assert (bindings.size() == 1); // always on windows
 149         ArgumentBinding binding = bindings.get(0);
 150         Class<?> carrier = ((LayoutTypeImpl<?>) type).carrier();
 151         if (Util.isCStruct(carrier)) {
 152 
 153             /*
 154              * Leak memory for now
 155              */
 156             Scope scope = Scope.globalScope().fork();
 157 
 158             @SuppressWarnings({"rawtypes", "unchecked"})
 159             Pointer<?> rtmp = ((ScopeImpl) scope).allocate(type, 8);
 160 
 161             if (CallingSequenceBuilderImpl.isRegisterAggregate(type.layout())) {
 162                 Pointer<Long> dst = Util.unsafeCast(rtmp, NativeTypes.UINT64).offset(binding.getOffset() / NativeTypes.UINT64.bytesSize());
 163                 Pointer.copy(srcPtrFunc.apply(binding), dst, binding.getStorage().getSize());
 164             } else {
 165                 Pointer<?> local = Util.unsafeCast(srcPtrFunc.apply(binding), type.pointer()).get();
 166                 // need defensive copy since Structs don't have value semantics on the Java side.
 167                 Pointer.copy(local, rtmp, type.bytesSize());
 168             }
 169 
 170             return rtmp.get();
 171         }
 172         return Util.unsafeCast(srcPtrFunc.apply(binding), type).get();
 173     }
 174 
 175 }