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

Register String(byte[]) constructor for runtime JNI access #3680

Merged
merged 2 commits into from
Nov 6, 2021
Merged

Register String(byte[]) constructor for runtime JNI access #3680

merged 2 commits into from
Nov 6, 2021

Conversation

jaikiran
Copy link
Contributor

More than one user in the Quarkus project has reported that they run into the following exception when building a native image for a Quarkus application:

java.lang.NoSuchMethodError: java.lang.String.<init>([B)V 
 at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1113) 
 at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1098) 
 at com.oracle.svm.jni.functions.JNIFunctions.GetMethodID(JNIFunctions.java:411) 
 at java.io.UnixFileSystem.canonicalize0(UnixFileSystem.java) 
 at java.io.UnixFileSystem.canonicalize(UnixFileSystem.java:178) 
 at java.io.File.getCanonicalPath(File.java:626) 
 at java.io.File.getCanonicalFile(File.java:651) 
 at sun.security.provider.FileInputStreamPool.getInputStream(FileInputStreamPool.java:84) 
 at sun.security.provider.NativePRNG$RandomIO.<init>(NativePRNG.java:388) 
 at sun.security.provider.NativePRNG$1.run(NativePRNG.java:190) 
 at sun.security.provider.NativePRNG$1.run(NativePRNG.java:130) 
 at java.security.AccessController.doPrivileged(AccessController.java:82) 
 at sun.security.provider.NativePRNG.initIO(NativePRNG.java:129) 
 at sun.security.provider.NativePRNG.<clinit>(NativePRNG.java:93) 
 at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:375) 
 at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:295) 
 at java.lang.reflect.Constructor.newInstance(Constructor.java:490) 
 at java.security.Provider.newInstanceUtil(Provider.java:176) 
 at java.security.Provider$Service.newInstance(Provider.java:1894) 
 at java.security.SecureRandom.getDefaultPRNG(SecureRandom.java:290) 
 at java.security.SecureRandom.<init>(SecureRandom.java:219) 
 at java.util.UUID$Holder.<clinit>(UUID.java:101) 
 at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:375) 
 at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:295) 
 at java.util.UUID.randomUUID(UUID.java:147) 
 

The previous report of this was in [1] but reproducing that wasn't working out. The more recent report is here [2] and has more clear instructions on how to reproduce this. Looking at the code in these stacktraces and then looking at the JRE code, it comes down to a code path in jni_util.c which can lead to a call to the java.lang.String constructor which just takes the byte[] as a param (as uses the platform encoding internally). This is the place in jni_util.c where that call happens https://github.com/openjdk/jdk11u-dev/blob/master/src/java.base/share/native/libjava/jni_util.c#L732 :

/*If the encoding specified in sun.jnu.encoding is not endorsed
  by "Charset.isSupported" we have to fall back to use String(byte[])
  explicitly here without specifying the encoding name, in which the
  StringCoding class will pickup the iso-8859-1 as the fallback
  converter for us.
 */
jmethodID mid = (*env)->GetMethodID(env, strClazz,
                                    "<init>", "([B)V");
if (mid != NULL) {
    result = (*env)->NewObject(env, strClazz, mid, bytes);
}

The commit in this PR registers this constructor for JNI runtime access to prevent this exception.

[1] quarkusio/quarkus#10682 (comment)
[2] quarkusio/quarkus#19082

Not registering this causes failures reported in Quarkus native applications:
quarkusio/quarkus#19082
quarkusio/quarkus#10682 (comment)
@jaikiran
Copy link
Contributor Author

/cc @zakkak

@zakkak zakkak added the quarkus quarkus related issue label Aug 16, 2021
Copy link
Collaborator

@zakkak zakkak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @jaikiran

@jaikiran
Copy link
Contributor Author

Hello @cstancu, would you be able to help review and merge this one, if this looks fine?

@pokusak
Copy link

pokusak commented Sep 10, 2021

Hi, @cstancu @zakkak would it be possible to merge this one?

@jaikiran
Copy link
Contributor Author

jaikiran commented Oct 1, 2021

Hello @dougxc, would you be able to help to have someone review this PR please?

@dougxc
Copy link
Member

dougxc commented Oct 1, 2021

@peter-hofer can you please review and merge this if it looks good.

@pokusak
Copy link

pokusak commented Nov 5, 2021

@dougxc , @peter-hofer what needs to be done to approve this single line change?

@graalvmbot graalvmbot merged commit 6127e51 into oracle:master Nov 6, 2021
@jaikiran
Copy link
Contributor Author

jaikiran commented Nov 8, 2021

Thank you @dougxc and @peter-hofer for helping in the review and merge.

@jaikiran jaikiran deleted the qk-19082 branch November 8, 2021 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oca-signed quarkus quarkus related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants