Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

[FFI]Inline the missing libc functions for symbol lookup on AIX #75

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions closed/src/java.base/aix/native/libsyslookup/syslookup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*
* 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.
*
* IBM designates this particular file as subject to the "Classpath" exception
* as provided by IBM in the LICENSE file that accompanied this code.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* ===========================================================================
*/

#include <fstab.h>
#include <setjmp.h>
#include <string.h>
#include <strings.h>

/* The function list inlines the libc functions which
* are missing in loading the default library.
*/
__attribute__((visibility("default"))) void* funcs[] = {
&bcopy, &endfsent, &getfsent, &getfsfile, &getfsspec, &longjmp,
&memcpy, &memmove, &setfsent, &setjmp, &siglongjmp, &strcat,
&strcpy, &strncat, &strncpy
};
45 changes: 37 additions & 8 deletions src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
* (c) Copyright IBM Corp. 2022, 2023 All Rights Reserved
* ===========================================================================
*/

Expand Down Expand Up @@ -86,14 +86,27 @@ private static SymbolLookup makeAixLookup() {
NativeLibrary lib = NativeLibraries.defaultLibrary;
return name -> {
Objects.requireNonNull(name);
try {
long addr = lib.lookup(name);
return (addr == 0) ?
Optional.empty() :
Optional.of(MemorySegment.ofAddress(MemoryAddress.ofLong(addr), 0, MemorySession.global()));
} catch (NoSuchMethodException e) {
return Optional.empty();
MemoryAddress funcAddr = null;
AixFuncSymbols symbol = AixFuncSymbols.valueOfOrNull(name);
if (symbol == null) {
try {
/* Look up the libc functions in the default library. */
funcAddr = MemoryAddress.ofLong(lib.lookup(name));
} catch (NoSuchMethodException e) {
return Optional.empty();
}
} else {
/* Directly load the specified library with the libc functions
* missing in the default library.
*/
SymbolLookup funcsLibLookup =
libLookup(libs -> libs.load(jdkLibraryPath("syslookup")));
MemorySegment funcs = MemorySegment.ofAddress(funcsLibLookup.lookup("funcs").orElseThrow().address(),
ADDRESS.byteSize() * AixFuncSymbols.values().length, MemorySession.global());
funcAddr = funcs.getAtIndex(ADDRESS, symbol.ordinal());
}

return Optional.of(MemorySegment.ofAddress(funcAddr, 0L, MemorySession.global()));
};
}

Expand Down Expand Up @@ -232,4 +245,20 @@ static WindowsFallbackSymbols valueOfOrNull(String name) {
}
}
}

/* Inlined libc function symbols missing in the default library. */
public enum AixFuncSymbols {
bcopy, endfsent, getfsent, getfsfile, getfsspec, longjmp,
memcpy, memmove, setfsent, setjmp, siglongjmp, strcat,
strcpy, strncat, strncpy
;

static AixFuncSymbols valueOfOrNull(String name) {
try {
return valueOf(name);
} catch (IllegalArgumentException e) {
return null;
}
}
}
}