1 /*
  2  * Copyright (c) 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.  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 
 26 package jdk.internal.foreign;
 27 
 28 import jdk.incubator.foreign.MemorySegment;
 29 import jdk.incubator.foreign.NativeSymbol;
 30 import jdk.incubator.foreign.ResourceScope;
 31 import jdk.incubator.foreign.SymbolLookup;
 32 import jdk.incubator.foreign.MemoryAddress;
 33 import jdk.internal.loader.NativeLibraries;
 34 import jdk.internal.loader.NativeLibrary;
 35 
 36 import java.nio.file.Files;
 37 import java.nio.file.Path;
 38 import java.util.Objects;
 39 import java.util.Optional;
 40 import java.util.function.Function;
 41 
 42 import static jdk.incubator.foreign.ValueLayout.ADDRESS;
 43 
 44 public class SystemLookup implements SymbolLookup {
 45 
 46     private SystemLookup() { }
 47 
 48     final static SystemLookup INSTANCE = new SystemLookup();
 49 
 50     /*
 51      * On POSIX systems, dlsym will allow us to lookup symbol in library dependencies; the same trick doesn't work
 52      * on Windows. For this reason, on Windows we do not generate any side-library, and load msvcrt.dll directly instead.
 53      */
 54     private static final SymbolLookup syslookup = switch (CABI.current()) {
 55         case SysV, LinuxAArch64, MacOsAArch64 -> libLookup(libs -> libs.loadLibrary("syslookup"));
 56         case Win64 -> makeWindowsLookup(); // out of line to workaround javac crash
 57     };
 58 
 59     private static SymbolLookup makeWindowsLookup() {
 60         Path system32 = Path.of(System.getenv("SystemRoot"), "System32");
 61         Path ucrtbase = system32.resolve("ucrtbase.dll");
 62         Path msvcrt = system32.resolve("msvcrt.dll");
 63 
 64         boolean useUCRT = Files.exists(ucrtbase);
 65         Path stdLib = useUCRT ? ucrtbase : msvcrt;
 66         SymbolLookup lookup = libLookup(libs -> libs.loadLibrary(null, stdLib.toFile()));
 67 
 68         if (useUCRT) {
 69             // use a fallback lookup to look up inline functions from fallback lib
 70 
 71             SymbolLookup fallbackLibLookup = libLookup(libs -> libs.loadLibrary("WinFallbackLookup"));
 72 
 73             int numSymbols = WindowsFallbackSymbols.values().length;
 74             MemorySegment funcs = MemorySegment.ofAddressNative(fallbackLibLookup.lookup("funcs").orElseThrow().address(),
 75                 ADDRESS.byteSize() * numSymbols, ResourceScope.globalScope());
 76 
 77             SymbolLookup fallbackLookup = name -> Optional.ofNullable(WindowsFallbackSymbols.valueOfOrNull(name))
 78                 .map(symbol -> NativeSymbol.ofAddress(symbol.name(), funcs.getAtIndex(ADDRESS, symbol.ordinal()), ResourceScope.globalScope()));
 79 
 80             final SymbolLookup finalLookup = lookup;
 81             lookup = name -> finalLookup.lookup(name).or(() -> fallbackLookup.lookup(name));
 82         }
 83 
 84         return lookup;
 85     }
 86 
 87     private static SymbolLookup libLookup(Function<NativeLibraries, NativeLibrary> loader) {
 88         NativeLibrary lib = loader.apply(NativeLibraries.rawNativeLibraries(SystemLookup.class, false));
 89         return name -> {
 90             Objects.requireNonNull(name);
 91             try {
 92                 long addr = lib.lookup(name);
 93                 return addr == 0 ?
 94                         Optional.empty() :
 95                         Optional.of(NativeSymbol.ofAddress(name, MemoryAddress.ofLong(addr), ResourceScope.globalScope()));
 96             } catch (NoSuchMethodException e) {
 97                 return Optional.empty();
 98             }
 99         };
100     }
101 
102     @Override
103     public Optional<NativeSymbol> lookup(String name) {
104         return syslookup.lookup(name);
105     }
106 
107     public static SystemLookup getInstance() {
108         return INSTANCE;
109     }
110 
111     // fallback symbols missing from ucrtbase.dll
112     // this list has to be kept in sync with the table in the companion native library
113     private enum WindowsFallbackSymbols {
114         // stdio
115         fprintf,
116         fprintf_s,
117         fscanf,
118         fscanf_s,
119         fwprintf,
120         fwprintf_s,
121         fwscanf,
122         fwscanf_s,
123         printf,
124         printf_s,
125         scanf,
126         scanf_s,
127         snprintf,
128         sprintf,
129         sprintf_s,
130         sscanf,
131         sscanf_s,
132         swprintf,
133         swprintf_s,
134         swscanf,
135         swscanf_s,
136         vfprintf,
137         vfprintf_s,
138         vfscanf,
139         vfscanf_s,
140         vfwprintf,
141         vfwprintf_s,
142         vfwscanf,
143         vfwscanf_s,
144         vprintf,
145         vprintf_s,
146         vscanf,
147         vscanf_s,
148         vsnprintf,
149         vsnprintf_s,
150         vsprintf,
151         vsprintf_s,
152         vsscanf,
153         vsscanf_s,
154         vswprintf,
155         vswprintf_s,
156         vswscanf,
157         vswscanf_s,
158         vwprintf,
159         vwprintf_s,
160         vwscanf,
161         vwscanf_s,
162         wprintf,
163         wprintf_s,
164         wscanf,
165         wscanf_s,
166 
167         // time
168         gmtime
169         ;
170 
171         static WindowsFallbackSymbols valueOfOrNull(String name) {
172             try {
173                 return valueOf(name);
174             } catch (IllegalArgumentException e) {
175                 return null;
176             }
177         }
178     }
179 }