Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8282215: Handle failures when initializing SystemLookup
Reviewed-by: jvernee
  • Loading branch information
mcimadamore committed Feb 22, 2022
1 parent a225c6e commit 5620d82
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
Expand Up @@ -91,7 +91,7 @@ public MemoryLayout elementLayout() {
}

/**
* {@return the element count of this sequence layout (if any)}
* {@return the element count of this sequence layout}
*/
public long elementCount() {
return elemCount;
Expand Down
22 changes: 18 additions & 4 deletions src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java
Expand Up @@ -48,14 +48,28 @@ private SystemLookup() { }

static final SystemLookup INSTANCE = new SystemLookup();

/* A fallback lookup, used when creation of system lookup fails. */
private static final Function<String, Optional<NativeSymbol>> fallbackLookup = name -> Optional.empty();

/*
* On POSIX systems, dlsym will allow us to lookup symbol in library dependencies; the same trick doesn't work
* on Windows. For this reason, on Windows we do not generate any side-library, and load msvcrt.dll directly instead.
*/
private static final Function<String, Optional<NativeSymbol>> syslookup = switch (CABI.current()) {
case SysV, LinuxAArch64, MacOsAArch64 -> libLookup(libs -> libs.load(jdkLibraryPath("syslookup")));
case Win64 -> makeWindowsLookup(); // out of line to workaround javac crash
};
private static final Function<String, Optional<NativeSymbol>> syslookup = makeSystemLookup();

private static final Function<String, Optional<NativeSymbol>> makeSystemLookup() {
try {
return switch (CABI.current()) {
case SysV, LinuxAArch64, MacOsAArch64 -> libLookup(libs -> libs.load(jdkLibraryPath("syslookup")));
case Win64 -> makeWindowsLookup(); // out of line to workaround javac crash
};
} catch (Throwable ex) {
// This can happen in the event of a library loading failure - e.g. if one of the libraries the
// system lookup depends on cannot be loaded for some reason. In such extreme cases, rather than
// fail, return a dummy lookup.
return fallbackLookup;
}
}

private static Function<String, Optional<NativeSymbol>> makeWindowsLookup() {
Path system32 = Path.of(System.getenv("SystemRoot"), "System32");
Expand Down
44 changes: 44 additions & 0 deletions test/jdk/java/foreign/TestFallbackLookup.java
@@ -0,0 +1,44 @@
/*
* Copyright (c) 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.
*/

/*
* @test
* @enablePreview
* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64")
* @run testng/othervm -Dos.name=Windows --enable-native-access=ALL-UNNAMED TestFallbackLookup
*/

import org.testng.annotations.*;
import static org.testng.Assert.*;

import java.lang.foreign.CLinker;

public class TestFallbackLookup {
@Test
void testBadSystemLookupRequest() {
// we request a CLinker, forcing OS name to be "Windows". This should trigger an exception when
// attempting to load a non-existent ucrtbase.dll. Make sure that no error is generated at this stage.
CLinker linker = CLinker.systemCLinker();
assertTrue(linker.lookup("nonExistentSymbol").isEmpty());
}
}

0 comments on commit 5620d82

Please sign in to comment.