jnr / jnr-posix

Java Posix layer
Other
241 stars 91 forks source link

LinuxLibC interface is incompatible with musl libc #187

Closed betalb closed 9 months ago

betalb commented 9 months ago

Starting from Alpine 3.19 LFS64 symbols are no longer present in musl, so jnr is not able to load libc on this platform. Also they are not present in gcompat alpine package.

Some useful details can be found here https://github.com/sbt/sbt/issues/7455

Affected symbols

__fxstat64
__lxstat64
__xstat64

I've used below class to trigger the problem

import jnr.posix.POSIXFactory;
import jnr.posix.util.DefaultPOSIXHandler;

class Scratch {
    public static void main(String[] args) {
        System.out.println(POSIXFactory.getPOSIX(new VerbosePosixHandler(), true).getpid());
    }

    public static class VerbosePosixHandler extends DefaultPOSIXHandler {
        @Override
        public boolean isVerbose() {
            return true;
        }
    }
}

Launch command

java -cp './:jffi-1.3.12.jar:jffi-1.3.12-native.jar:jnr-constants-0.10.4.jar:jnr-ffi-2.2.15.jar:jnr-posix-3.1.18.jar:asm-9.3.jar' Scratch

And the output

Failed to load native POSIX impl; falling back on Java impl. Stacktrace follows.
java.lang.UnsatisfiedLinkError: unknown
    at jnr.ffi.provider.jffi.AsmRuntime.newUnsatisifiedLinkError(AsmRuntime.java:48)
    at jnr.posix.LinuxLibC$jnr$ffi$0.__xstat64(Unknown Source)
    at jnr.posix.LinuxPOSIX.<init>(LinuxPOSIX.java:31)
    at jnr.posix.POSIXFactory.loadLinuxPOSIX(POSIXFactory.java:149)
    at jnr.posix.POSIXFactory.loadNativePOSIX(POSIXFactory.java:124)
    at jnr.posix.POSIXFactory.loadPOSIX(POSIXFactory.java:93)
    at jnr.posix.LazyPOSIX.loadPOSIX(LazyPOSIX.java:40)
    at jnr.posix.LazyPOSIX.posix(LazyPOSIX.java:34)
    at jnr.posix.LazyPOSIX.getpid(LazyPOSIX.java:184)
    at Scratch.main(scratch_96.java:7)
0
betalb commented 9 months ago

Looking at the code of LinuxPOSIX, it seems that such situation is handled with use_fxstat64/use_lxstat64/use_xstat64 flags, but not in constructor, where UnsatisfiedLinkError is not handled.

headius commented 9 months ago

Good find, I will review PR.