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

Commit

Permalink
[FFI]Inline the missing libc functions for symbol lookup on AIX
Browse files Browse the repository at this point in the history
The changes aim to support the symbol lookup via FFI for
the missing libc functions in loading the default library
on AIX by inlining their address into a function list in
native to ensure they are correctly invoked with the
native address fetched in the FFI code.

Fixes: #eclipse-openj9/openj9/issues/16386

Signed-off-by: ChengJin01 <jincheng@ca.ibm.com>
  • Loading branch information
ChengJin01 committed Mar 3, 2023
1 parent 3c83ead commit f1526bb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
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;
}
}
}
}

0 comments on commit f1526bb

Please sign in to comment.