Skip to content

Commit 1e3dd59

Browse files
fangerertimfel
authored andcommitted
Compute canonical path for native libs and C exts
(cherry picked from commit fa8c8af)
1 parent 2df1173 commit 1e3dd59

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
import java.io.IOException;
4949
import java.io.PrintStream;
50+
import java.nio.file.LinkOption;
5051
import java.util.ArrayList;
5152
import java.util.Arrays;
5253
import java.util.HashMap;
@@ -539,7 +540,7 @@ public static CApiContext ensureCapiWasLoaded(Node node, PythonContext context,
539540
TruffleFile homePath = env.getInternalTruffleFile(context.getCAPIHome().toJavaStringUncached());
540541
// e.g. "libpython-native.so"
541542
String libName = context.getLLVMSupportExt("python");
542-
TruffleFile capiFile = homePath.resolve(libName);
543+
TruffleFile capiFile = homePath.resolve(libName).getCanonicalFile(LinkOption.NOFOLLOW_LINKS);
543544
try {
544545
SourceBuilder capiSrcBuilder;
545546
final boolean useNative;
@@ -558,7 +559,7 @@ public static CApiContext ensureCapiWasLoaded(Node node, PythonContext context,
558559
if (!env.getInternalLanguages().containsKey(J_NFI_LANGUAGE)) {
559560
throw PRaiseNode.raiseUncached(node, PythonBuiltinClassType.SystemError, ErrorMessages.NFI_NOT_AVAILABLE, "NativeModules", "true");
560561
}
561-
capiSrcBuilder = Source.newBuilder(J_NFI_LANGUAGE, "load(RTLD_GLOBAL) \"" + capiFile.getAbsoluteFile().getPath() + "\"", "<libpython>");
562+
capiSrcBuilder = Source.newBuilder(J_NFI_LANGUAGE, "load(RTLD_GLOBAL) \"" + capiFile.getPath() + "\"", "<libpython>");
562563
} else {
563564
if (!env.getInternalLanguages().containsKey(J_LLVM_LANGUAGE)) {
564565
throw PRaiseNode.raiseUncached(node, PythonBuiltinClassType.SystemError, ErrorMessages.LLVM_NOT_AVAILABLE);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common/CExtContext.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
5353

5454
import java.io.IOException;
55+
import java.nio.file.LinkOption;
5556

57+
import com.oracle.truffle.api.TruffleFile;
5658
import org.graalvm.shadowed.com.ibm.icu.impl.Punycode;
5759
import org.graalvm.shadowed.com.ibm.icu.text.StringPrepParseException;
5860

@@ -319,10 +321,12 @@ public static Object loadCExtModule(Node location, PythonContext context, Module
319321
CApiContext cApiContext = CApiContext.ensureCapiWasLoaded(location, context, spec.name, spec.path);
320322
Object library = null;
321323

324+
322325
if (cApiContext.useNativeBackend) {
323326
GraalHPyJNIContext.loadJNIBackend();
324-
getLogger().config("loading module " + spec.path + " as native");
325-
String loadExpr = String.format("load(%s) \"%s\"", dlopenFlagsToString(context.getDlopenFlags()), spec.path);
327+
TruffleFile realPath = context.getPublicTruffleFileRelaxed(spec.path, context.getSoAbi()).getCanonicalFile(LinkOption.NOFOLLOW_LINKS);
328+
getLogger().config(String.format("loading module %s (real path: %s) as native", spec.path, realPath));
329+
String loadExpr = String.format("load(%s) \"%s\"", dlopenFlagsToString(context.getDlopenFlags()), realPath);
326330
if (PythonOptions.UsePanama.getValue(context.getEnv().getOptions())) {
327331
loadExpr = "with panama " + loadExpr;
328332
}
@@ -359,7 +363,8 @@ public static Object loadLLVMLibrary(Node location, PythonContext context, Truff
359363
Env env = context.getEnv();
360364
try {
361365
TruffleString extSuffix = context.getSoAbi();
362-
CallTarget callTarget = env.parseInternal(Source.newBuilder(J_LLVM_LANGUAGE, context.getPublicTruffleFileRelaxed(path, extSuffix)).build());
366+
TruffleFile realPath = context.getPublicTruffleFileRelaxed(path, extSuffix).getCanonicalFile(LinkOption.NOFOLLOW_LINKS);
367+
CallTarget callTarget = env.parseInternal(Source.newBuilder(J_LLVM_LANGUAGE, realPath).build());
363368
return callTarget.call();
364369
} catch (SecurityException e) {
365370
throw new ImportException(CExtContext.wrapJavaException(e, location), name, path, ErrorMessages.CANNOT_LOAD_M, path, e);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/jni/GraalHPyJNIContext.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
5757
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
5858

59-
import java.nio.file.Paths;
59+
import java.io.IOException;
60+
import java.nio.file.LinkOption;
6061
import java.util.Arrays;
6162
import java.util.LinkedList;
6263
import java.util.List;
@@ -353,9 +354,10 @@ protected void initNativeFastPaths() {
353354

354355
public static void loadJNIBackend() {
355356
if (!(ImageInfo.inImageBuildtimeCode() || jniBackendLoaded)) {
356-
String pythonJNIPath = getJNILibrary();
357-
LOGGER.fine("Loading HPy JNI backend from " + pythonJNIPath);
357+
String pythonJNIPath;
358+
pythonJNIPath = getJNILibrary();
358359
try {
360+
LOGGER.fine("Loading HPy JNI backend from " + pythonJNIPath);
359361
System.load(pythonJNIPath);
360362
jniBackendLoaded = true;
361363
} catch (NullPointerException | UnsatisfiedLinkError e) {
@@ -367,7 +369,14 @@ public static void loadJNIBackend() {
367369

368370
public static String getJNILibrary() {
369371
CompilerAsserts.neverPartOfCompilation();
370-
return Paths.get(PythonContext.get(null).getJNIHome().toJavaStringUncached(), PythonContext.J_PYTHON_JNI_LIBRARY_NAME).toString();
372+
PythonContext context = PythonContext.get(null);
373+
TruffleFile libPath = context.getPublicTruffleFileRelaxed(context.getJNIHome()).resolve(PythonContext.J_PYTHON_JNI_LIBRARY_NAME).getAbsoluteFile();
374+
try {
375+
return libPath.getCanonicalFile(LinkOption.NOFOLLOW_LINKS).toString();
376+
} catch (IOException e) {
377+
LOGGER.severe(String.format("Cannot determine canonical path for %s: %s", libPath, e));
378+
throw new IllegalStateException(e);
379+
}
371380
}
372381

373382
@Override

0 commit comments

Comments
 (0)