Skip to content

Commit

Permalink
[GR-59770] Make PanamaNFILanguage load cleanly on JDK 21
Browse files Browse the repository at this point in the history
PullRequest: graal/20129
  • Loading branch information
eregon committed Feb 25, 2025
2 parents 63a4415 + f8aa58b commit 2851fed
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 123 deletions.
25 changes: 20 additions & 5 deletions truffle/mx.truffle/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,16 +949,32 @@
"graalCompilerSourceEdition": "ignore",
},

# PanamaNFILanguage itself must be 17+ so that PanamaNFILanguageProvider can be loaded without
# breaking the ServiceLoader which unfortunately cannot ignore newer class files (GR-59770)
"com.oracle.truffle.nfi.backend.panama" : {
"subDir" : "src",
"sourceDirs" : ["src"],
"dependencies" : [
"com.oracle.truffle.nfi.backend.spi",
],
"checkstyle" : "com.oracle.truffle.api",
# GR-51699
"javaCompliance" : "17+",
"annotationProcessors" : ["TRUFFLE_DSL_PROCESSOR"],
"workingSets" : "Truffle",
"graalCompilerSourceEdition": "ignore",
},

"com.oracle.truffle.nfi.backend.panama.jdk22" : {
"overlayTarget" : "com.oracle.truffle.nfi.backend.panama",
"multiReleaseJarVersion" : "22",
"subDir" : "src",
"sourceDirs" : ["src"],
"dependencies" : [
"com.oracle.truffle.nfi.backend.panama",
],
"checkstyle" : "com.oracle.truffle.api",
"javaCompliance" : "22+",
"forceJavac": True,
"forceJavac": True, # GR-51699
"annotationProcessors" : ["TRUFFLE_DSL_PROCESSOR"],
"workingSets" : "Truffle",
# disable SpotBugs and Jacoco as long as JDK 22 is unsupported [GR-49566]
Expand Down Expand Up @@ -1906,9 +1922,8 @@
],
},
"subDir" : "src",
"javaCompliance" : "22+",
# GR-51699
"forceJavac": True,
"javaCompliance" : "17+",
"forceJavac": True, # GR-51699
"dependencies" : [
"com.oracle.truffle.nfi.backend.panama",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,18 @@

import com.oracle.truffle.api.InternalResource.OS;

public class ErrorContext {
private static final String ERRNO_LOCATION;
public class ErrorContext extends AbstractErrorContext {

static {
ERRNO_LOCATION = switch (OS.getCurrent()) {
case DARWIN -> "__error";
case LINUX -> "__errno_location";
case WINDOWS -> "_errno";
};
}
private static final String ERRNO_LOCATION = switch (OS.getCurrent()) {
case DARWIN -> "__error";
case LINUX -> "__errno_location";
case WINDOWS -> "_errno";
};

@SuppressWarnings("preview") private MemorySegment errnoLocation;
final PanamaNFIContext ctx;
private MemorySegment errnoLocation;

@SuppressWarnings({"preview", "restricted"})
MemorySegment lookupErrnoLocation() {
@SuppressWarnings("restricted")
private MemorySegment lookupErrnoLocation() {
try {
Linker linker = Linker.nativeLinker();
FunctionDescriptor desc = FunctionDescriptor.of(ValueLayout.JAVA_LONG);
Expand All @@ -79,8 +75,9 @@ MemorySegment lookupErrnoLocation() {
}
}

@Override
void initialize() {
if (this.errnoLocation == null) {
if (errnoLocation == null) {
errnoLocation = lookupErrnoLocation();
}
}
Expand All @@ -90,17 +87,14 @@ private MemorySegment getErrnoLocation() {
return errnoLocation;
}

@SuppressWarnings("preview")
@Override
int getNativeErrno() {
return getErrnoLocation().get(ValueLayout.JAVA_INT, 0);
}

@SuppressWarnings("preview")
@Override
void setNativeErrno(int newErrno) {
getErrnoLocation().set(ValueLayout.JAVA_INT, 0, newErrno);
}

ErrorContext(PanamaNFIContext ctx, Thread thread) {
this.ctx = ctx;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ abstract static class SignatureExecuteNode extends RootNode {
@Override
public abstract Object execute(VirtualFrame frame);

@SuppressWarnings("preview")
MemorySegment getAddress(VirtualFrame frame) {
return MemorySegment.ofAddress((long) frame.getArguments()[0]);
}
Expand All @@ -140,7 +139,6 @@ PanamaSignature getSig(VirtualFrame frame) {
@ExplodeLoop
public Object doGeneric(VirtualFrame frame) {
Object[] args = getArgs(frame);
@SuppressWarnings("preview")
MemorySegment address = getAddress(frame);
PanamaSignature signature = getSig(frame);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.nfi.backend.panama;

import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.nfi.backend.spi.NFIBackend;

public abstract class PanamaAccessor {

private PanamaAccessor() {
// No instances.
}

static boolean isSupported() {
return true;
}

static NFIBackend createNFIBackend(PanamaNFILanguage language) {
return new PanamaNFIBackend(language);
}

static AbstractPanamaNFIContext createPanamaNFIContext(PanamaNFILanguage language, Env env) {
return new PanamaNFIContext(language, env);
}

static AbstractErrorContext createErrorContext() {
return new ErrorContext();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@

@ExportLibrary(InteropLibrary.class)
final class PanamaClosure implements TruffleObject {
@SuppressWarnings("preview") final MemorySegment symbol;
final MemorySegment symbol;

PanamaClosure(@SuppressWarnings("preview") MemorySegment symbol) {
PanamaClosure(MemorySegment symbol) {
this.symbol = symbol;
}

Expand Down Expand Up @@ -239,7 +239,7 @@ public Object execute(VirtualFrame frame) {
// to avoid the JVM clobbering it
PanamaNFILanguage language = PanamaNFILanguage.get(this);
NFIState nfiState = language.getNFIState();
ErrorContext ctx = language.errorContext.get();
ErrorContext ctx = (ErrorContext) language.errorContext.get();
nfiState.setNFIErrno(ctx.getNativeErrno());
try {
Object ret = callClosure.execute(frame);
Expand Down Expand Up @@ -290,7 +290,7 @@ public Object execute(VirtualFrame frame) {
// to avoid the JVM clobbering it
PanamaNFILanguage language = PanamaNFILanguage.get(this);
NFIState nfiState = language.getNFIState();
ErrorContext ctx = language.errorContext.get();
ErrorContext ctx = (ErrorContext) language.errorContext.get();
nfiState.setNFIErrno(ctx.getNativeErrno());
try {
callClosure.execute(frame);
Expand Down Expand Up @@ -337,7 +337,7 @@ public Object execute(VirtualFrame frame) {
// to avoid the JVM clobbering it
PanamaNFILanguage language = PanamaNFILanguage.get(this);
NFIState nfiState = language.getNFIState();
ErrorContext ctx = language.errorContext.get();
ErrorContext ctx = (ErrorContext) language.errorContext.get();
nfiState.setNFIErrno(ctx.getNativeErrno());
try {
Object ret = callClosure.execute(frame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,19 @@ final class PanamaLibrary implements TruffleObject {

private static final EmptyKeysArray KEYS = new EmptyKeysArray();

private final @SuppressWarnings("preview") SymbolLookup library;
private final SymbolLookup library;

static PanamaLibrary createDefault() {
@SuppressWarnings("preview")
SymbolLookup lookup = Linker.nativeLinker().defaultLookup();
return new PanamaLibrary(lookup);
}

static PanamaLibrary create(@SuppressWarnings("preview") SymbolLookup library) {
static PanamaLibrary create(SymbolLookup library) {
assert library != null;
return new PanamaLibrary(library);
}

private PanamaLibrary(@SuppressWarnings("preview") SymbolLookup library) {
private PanamaLibrary(SymbolLookup library) {
this.library = library;
}

Expand All @@ -99,7 +98,6 @@ boolean isMemberReadable(@SuppressWarnings("unused") String member) {
}

@TruffleBoundary
@SuppressWarnings("preview")
Optional<MemorySegment> doLookup(String name) {
return library.find(name);
}
Expand All @@ -108,7 +106,6 @@ Optional<MemorySegment> doLookup(String name) {
Object readMember(String symbol,
@Bind Node node,
@Cached InlinedBranchProfile exception) throws UnknownIdentifierException {
@SuppressWarnings("preview")
Optional<MemorySegment> ret = doLookup(symbol);
if (ret.isEmpty()) {
exception.enter(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected LoadLibraryNode(PanamaNFILanguage language, String name) {
}

@TruffleBoundary
@SuppressWarnings({"preview", "restricted"})
@SuppressWarnings("restricted")
private SymbolLookup doLoad() {
PanamaNFIContext ctx = PanamaNFIContext.get(this);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/
package com.oracle.truffle.nfi.backend.panama;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
Expand All @@ -48,33 +49,34 @@

import java.lang.foreign.Arena;

class PanamaNFIContext {
final class PanamaNFIContext extends AbstractPanamaNFIContext {

final PanamaNFILanguage language;
@SuppressWarnings("preview") Arena arena;
Arena arena;
@CompilationFinal Env env;

PanamaNFIContext(PanamaNFILanguage language, Env env) {
this.language = language;
this.env = env;
}

@SuppressWarnings("preview")
@Override
void initialize() {
arena = Arena.ofShared();
}

@Override
void patchEnv(Env env) {
this.env = env;
}

@Override
void dispose() {
if (arena != null) {
arena.close();
}
}

@SuppressWarnings("preview")
Arena getContextArena() {
return arena;
}
Expand All @@ -85,9 +87,9 @@ PanamaType lookupEnvType() {
return null;
}

private static final ContextReference<PanamaNFIContext> REFERENCE = ContextReference.create(PanamaNFILanguage.class);
private static final ContextReference<AbstractPanamaNFIContext> REFERENCE = ContextReference.create(PanamaNFILanguage.class);

static PanamaNFIContext get(Node node) {
return REFERENCE.get(node);
return CompilerDirectives.castExact(REFERENCE.get(node), PanamaNFIContext.class);
}
}
Loading

0 comments on commit 2851fed

Please sign in to comment.