Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to reload the library in case of UnsatisfiedLinkError #20265

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

JasonFengJ9
Copy link
Member

Attempt to reload the library in case of UnsatisfiedLinkError

When an UnsatisfiedLinkError is thrown because the library was already loaded in another classloader, run gc() to unload the unreachable classloaders, and reload the library again.

Java 8/11 UnsatisfiedLinkError message already loaded in another classloader is from

/* loading a library in multiple class loaders is not permitted */
reportError(errBuf, "Library is already loaded in another ClassLoader", bufLen);

Java 17+ is from https://github.com/ibmruntimes/openj9-openjdk-jdk17/blob/4a311794a42998359c859814c645398be928e19a/src/java.base/share/classes/jdk/internal/loader/NativeLibraries.java#L206-L209

            // cannot be loaded by other class loaders
            if (loadedLibraryNames.contains(name)) {
                throw new UnsatisfiedLinkError("Native Library " + name +
                        " already loaded in another classloader");
            }

closes #19978

Signed-off-by: Jason Feng [email protected]

@pshipton
Copy link
Member

jenkins test sanity amac jdk11,jdk17

@pshipton
Copy link
Member

java/lang/Runtime/loadLibrary/LoadLibraryTest.java

16:49:16  STDOUT:
16:49:16  findLibrary someLibrary
16:49:16  Thread1 load
16:49:16  findLibrary awt
16:49:16  Thread2 load
16:49:16  STDERR:
16:49:16  java.lang.ExceptionInInitializerError
16:49:16  	at java.base/java.lang.J9VMInternals.ensureError(J9VMInternals.java:206)
16:49:16  	at java.base/java.lang.J9VMInternals.recordInitializationFailure(J9VMInternals.java:195)
16:49:16  	at java.base/java.lang.Class.forNameImpl(Native Method)
16:49:16  	at java.base/java.lang.Class.forName(Class.java:418)
16:49:16  	at LoadLibraryTest$2.run(LoadLibraryTest.java:137)
16:49:16  Caused by: java.lang.RuntimeException: someLibrary was loaded
16:49:16  	at Target.<clinit>(Target.java:28)
16:49:16  	... 3 more

@JasonFengJ9
Copy link
Member Author

java/lang/Runtime/loadLibrary/LoadLibraryTest.java

Looking into it.

@pshipton
Copy link
Member

J9vmTest

17:05:25  Able to open BigInteger library in different classLoader
17:05:25  Exception in thread "main" java.lang.RuntimeException
17:05:25  	at j9vm.test.libraryhandle.MultipleLibraryLoadTest.main(MultipleLibraryLoadTest.java:58)
17:05:25  non-zero exit value: 1
17:05:25  *** Test FAILED *** (j9vm.test.libraryhandle.MultipleLibraryLoadTest)

@pshipton
Copy link
Member

jdk17 java/lang/ClassLoader/nativeLibrary/NativeLibraryTest.java

16:48:23  STDOUT:
16:48:23  count: 1
16:48:23  STDERR:
16:48:23  JVMJNCK001I JNI check utility installed. Use -Xcheck:jni:help for usage
16:48:23  java.lang.RuntimeException: should fail to load the native library by another class loader
16:48:23  	at NativeLibraryTest.runTest(NativeLibraryTest.java:97)
16:48:23  	at NativeLibraryTest.main(NativeLibraryTest.java:65)
16:48:23  	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
16:48:23  	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
16:48:23  	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
16:48:23  	at java.base/java.lang.reflect.Method.invoke(Method.java:575)
16:48:23  	at com.sun.javatest.regtest.agent.MainWrapper$MainTask.run(MainWrapper.java:138)
16:48:23  	at java.base/java.lang.Thread.run(Thread.java:857)

@pshipton
Copy link
Member

Ah, if there is an UnsatisfiedLinkError, and it doesn't match the condition, the original UnsatisfiedLinkError needs to be re-thrown.

@JasonFengJ9 JasonFengJ9 marked this pull request as draft September 30, 2024 21:22
When an UnsatisfiedLinkError is thrown because the library was already
loaded in another classloader, run gc() to unload the unreachable
classloaders, and reload the library again.

Signed-off-by: Jason Feng <[email protected]>
@JasonFengJ9
Copy link
Member Author

JasonFengJ9 commented Oct 1, 2024

Re-throwing the UnsatisfiedLinkError still failed java/lang/ClassLoader/nativeLibrary/NativeLibraryTest.java.

23:24:00  java.lang.RuntimeException: should fail to load the native library by another class loader
23:24:00  	at NativeLibraryTest.runTest(NativeLibraryTest.java:97)
23:24:00  	at NativeLibraryTest.main(NativeLibraryTest.java:65)
23:24:00  	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
23:24:00  	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
23:24:00  	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
23:24:00  	at java.base/java.lang.reflect.Method.invoke(Method.java:575)
23:24:00  	at com.sun.javatest.regtest.agent.MainWrapper$MainTask.run(MainWrapper.java:138)
23:24:00  	at java.base/java.lang.Thread.run(Thread.java:857)

The test depends on GC behaviour.
https://github.com/ibmruntimes/openj9-openjdk-jdk17/blob/94ec587a8a2c3e278d3bba093b980ad9ea343e22/test/jdk/java/lang/ClassLoader/nativeLibrary/NativeLibraryTest.java#L97-L98

        Runnable r = newTestRunnable();
        r.run();
        // reload the native library by the same class loader
        r.run();
        // load the native library by another class loader
        Runnable r1 = newTestRunnable();
        try {
            r1.run();
            throw new RuntimeException("should fail to load the native library" +
                    " by another class loader");
        } catch (UnsatisfiedLinkError e) {}

The test need to be modified or excluded after this PR.

@JasonFengJ9
Copy link
Member Author

@pshipton
Copy link
Member

pshipton commented Oct 2, 2024

jenkins compile amac jdknext

@JasonFengJ9 JasonFengJ9 marked this pull request as ready for review October 2, 2024 19:43
@JasonFengJ9
Copy link
Member Author

The test changes have been merged in extension repos.

@pshipton
Copy link
Member

pshipton commented Oct 2, 2024

jenkins test sanity amac jdk11,jdk17

@pshipton pshipton merged commit 6e91d46 into eclipse-openj9:master Oct 3, 2024
10 checks passed
@JasonFengJ9 JasonFengJ9 deleted the nlclerror branch October 3, 2024 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants