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

8336382: Fixes error reporting in loading AWT and fonts #20169

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Karm
Copy link
Contributor

@Karm Karm commented Jul 13, 2024

If there is a problem with finding and calling e.g. java/awt/GraphicsEnvironment in AWTIsHeadless, the env' Exception remains set and it is not cleared. Later, that manifests as:

Fatal error reported via JNI: Could not allocate library name

Which is misleading. The code path is perhaps rare in a normal JDK usage, but it has been complicating our users' bug reports in the GraalVM/native-image ecosystem for quite some time.

Instead of failing later indicating that the user has incorrectly configured JNI, it bails out very soon with a message that seems as if a jstring could not have been allocated. It sends users on wild goose chases where it appears JNU_NewStringPlatform calls failed, e.g.

This commit fixes the error reporting in the AWTIsHeadless.

Furthermore, when AOT compiled, there is little sense for having a JAVA_HOME, yet some parts of AWT code look for it to search fonts. In such case, an empty directory structure is enough to accommodate it, e.g.

/tmp/JAVA_HOME/
/tmp/JAVA_HOME/conf
/tmp/JAVA_HOME/conf/fonts
/tmp/JAVA_HOME/lib

The exception is somewhat cryptic for users again, merely stating:

Exception in thread "main" java.io.IOException: Problem reading font data.
    at [email protected]/java.awt.Font.createFont0(Font.java:1205)
    at [email protected]/java.awt.Font.createFont(Font.java:1076)
    at imageio.Main.loadFonts(Main.java:139

Adding the cause there makes it clearer, i.e. that JAVA_HOME might be missing:

Exception in thread "main" java.io.IOException: Problem reading font data.
    at java.desktop@23-internal/java.awt.Font.createFont0(Font.java:1206)
    at java.desktop@23-internal/java.awt.Font.createFont(Font.java:1076)
    at imageio.Main.loadFonts(Main.java:139)
    at imageio.Main.paintRectangles(Main.java:97)
    at imageio.Main.main(Main.java:195)
    at java.base@23-internal/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
Caused by: java.lang.Error: java.home property not set
    at java.desktop@23-internal/sun.awt.FontConfiguration.findFontConfigFile(FontConfiguration.java:180)
    at java.desktop@23-internal/sun.awt.FontConfiguration.<init>(FontConfiguration.java:97)

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8336382: Fixes error reporting in loading AWT and fonts (Bug - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/20169/head:pull/20169
$ git checkout pull/20169

Update a local copy of the PR:
$ git checkout pull/20169
$ git pull https://git.openjdk.org/jdk.git pull/20169/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 20169

View PR using the GUI difftool:
$ git pr show -t 20169

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/20169.diff

Webrev

Link to Webrev Comment

If there is problem with finding and calling e.g. java/awt/GraphicsEnvironment
in AWTIsHeadless, the env' Exception remains set and it not cleared.
Later, that manifests as:

    Fatal error reported via JNI: Could not allocate library name

Which is misleading. The code path is perhaps rare in normal JDK usage,
but it has been complicating our users' bug reports in the GraalVM/native-image
ecosystem for quite some time.

Instead of failing later with some clear message that indicates that the
user has incorrectly configured JNI, it bails out very soon with a message
that seems as if a jstring could not have been allocated. It sends users
on wild goose chases, e.g.

oracle/graal#9138
oracle/graal#8475
oracle/graal#9300
quarkusio/quarkus#31596
graalvm/mandrel#292
Karm/mandrel-integration-tests#262

This commit fixes the error reporting in the AWTIsHeadless.

Furthermore, when AOT compiled, there is little sense for having a JAVA_HOME,
yet some parts of AWT code look for it to search fonts. In such case, an
empty directory structure is enough to accommodate it, e.g.

/tmp/JAVA_HOME/
/tmp/JAVA_HOME/conf
/tmp/JAVA_HOME/conf/fonts
/tmp/JAVA_HOME/lib

The exception is somewhat cryptic for users again, merely stating:

    Exception in thread "main" java.io.IOException: Problem reading font data.
        at [email protected]/java.awt.Font.createFont0(Font.java:1205)
        at [email protected]/java.awt.Font.createFont(Font.java:1076)
        at imageio.Main.loadFonts(Main.java:139

Adding the cause there makes it clearer, i.e. that JAVA_HOME might be missing:

    Exception in thread "main" java.io.IOException: Problem reading font data.
        at java.desktop@23-internal/java.awt.Font.createFont0(Font.java:1206)
        at java.desktop@23-internal/java.awt.Font.createFont(Font.java:1076)
        at imageio.Main.loadFonts(Main.java:139)
        at imageio.Main.paintRectangles(Main.java:97)
        at imageio.Main.main(Main.java:195)
        at java.base@23-internal/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
    Caused by: java.lang.Error: java.home property not set
        at java.desktop@23-internal/sun.awt.FontConfiguration.findFontConfigFile(FontConfiguration.java:180)
        at java.desktop@23-internal/sun.awt.FontConfiguration.<init>(FontConfiguration.java:97)
@bridgekeeper
Copy link

bridgekeeper bot commented Jul 13, 2024

👋 Welcome back Karm! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jul 13, 2024

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Jul 13, 2024

@Karm The following label will be automatically applied to this pull request:

  • client

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@Karm Karm changed the title Fixes error reporting in loading AWT and fonts 8336382: Fixes error reporting in loading AWT and fonts Jul 15, 2024
@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 15, 2024
@mlbridge
Copy link

mlbridge bot commented Jul 15, 2024

Copy link
Contributor

@avu avu left a comment

Choose a reason for hiding this comment

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

Overall LGTM, please update copyright years in the changed files.

src/java.desktop/share/classes/java/awt/Font.java Outdated Show resolved Hide resolved
Copy link
Contributor

@prrace prrace left a comment

Choose a reason for hiding this comment

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

There is nothing in this PR that I would accept. It should be withdrawn.

@Karm
Copy link
Contributor Author

Karm commented Jul 24, 2024

Hello @prrace

TBH, I got somewhat bewildered by your comments on how this PR makes no sense. Then I realized I might have been erroneously treating the AWTIsHeadless function's contents as correct, merely building on the previous error.

If you take a look at the flow of awt_LoadLibrary.c, my problem with it is that errors originating in AWTIsHeadless are reported way later in AWT_OnLoad and that is wrong.

awt_LoadLibrary.c:
JDK-pr-20169-drawing

Previous fix

So I made this PR, thinking that for some headless JDK distributions it is O.K. to be entirely missing java/awt/GraphicsEnvironment class or isHeadless method.

@@ -62,15 +62,24 @@ JNIEXPORT jboolean JNICALL AWTIsHeadless() {
         graphicsEnvClass = (*env)->FindClass(env,
                                              "java/awt/GraphicsEnvironment");
         if (graphicsEnvClass == NULL) {
+            // Not finding the class is not necessarily an error.
+            if ((*env)->ExceptionCheck(env)) {
+                (*env)->ExceptionClear(env);
+            }
             return JNI_TRUE;
         }
         headlessFn = (*env)->GetStaticMethodID(env,
                                                graphicsEnvClass, "isHeadless", "()Z");
         if (headlessFn == NULL) {
+            // If we can't find the method, we assume headless mode.
+            if ((*env)->ExceptionCheck(env)) {
+                (*env)->ExceptionClear(env);
+            }
             return JNI_TRUE;
         }
         isHeadless = (*env)->CallStaticBooleanMethod(env, graphicsEnvClass,
                                                      headlessFn);
+        // If an exception occurred, we assume headless mode.
         if ((*env)->ExceptionCheck(env)) {
             (*env)->ExceptionClear(env);
             return JNI_TRUE;

New fix

The new fix I propose treats the missing class or missing method as fatal errors. The crash makes sense, the error message is correct:

$ ./target/imageio   -Djava.awt.headless=true
Fatal error reported via JNI: java/awt/GraphicsEnvironment class not found

Printing instructions (ip=0x000000000048f8d9):
...

It's a little more invasive though:

diff --git a/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c b/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c
index 0fc44bfca71..7ef6dab8682 100644
--- a/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c
+++ b/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c
@@ -43,6 +43,12 @@
 #define VERBOSE_AWT_DEBUG
 #endif
 
+#define CHECK_EXCEPTION_FATAL(env, message) \
+    if ((*env)->ExceptionCheck(env)) { \
+        (*env)->ExceptionClear(env); \
+        (*env)->FatalError(env, message); \
+    }
+
 static void *awtHandle = NULL;
 
 typedef jint JNICALL JNI_OnLoad_type(JavaVM *vm, void *reserved);
@@ -61,25 +67,13 @@ JNIEXPORT jboolean JNICALL AWTIsHeadless() {
         env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
         graphicsEnvClass = (*env)->FindClass(env,
                                              "java/awt/GraphicsEnvironment");
-        if (graphicsEnvClass == NULL) {
-            // Not finding the class is not necessarily an error.
-            if ((*env)->ExceptionCheck(env)) {
-                (*env)->ExceptionClear(env);
-            }
-            return JNI_TRUE;
-        }
+        CHECK_EXCEPTION_FATAL(env, "java/awt/GraphicsEnvironment class not found");
         headlessFn = (*env)->GetStaticMethodID(env,
                                                graphicsEnvClass, "isHeadless", "()Z");
-        if (headlessFn == NULL) {
-            // If we can't find the method, we assume headless mode.
-            if ((*env)->ExceptionCheck(env)) {
-                (*env)->ExceptionClear(env);
-            }
-            return JNI_TRUE;
-        }
+        CHECK_EXCEPTION_FATAL(env, "isHeadless method not found");
         isHeadless = (*env)->CallStaticBooleanMethod(env, graphicsEnvClass,
                                                      headlessFn);
-        // If an exception occurred, we assume headless mode.
+        // If an exception occurred, we assume headless mode and carry on.
         if ((*env)->ExceptionCheck(env)) {
             (*env)->ExceptionClear(env);
             return JNI_TRUE;
@@ -88,12 +82,6 @@ JNIEXPORT jboolean JNICALL AWTIsHeadless() {
     return isHeadless;
 }
 
-#define CHECK_EXCEPTION_FATAL(env, message) \
-    if ((*env)->ExceptionCheck(env)) { \
-        (*env)->ExceptionClear(env); \
-        (*env)->FatalError(env, message); \
-    }
-
 /*
  * Pathnames to the various awt toolkits
  */

Thanks for your time. I hope the PR makes more sense now or at least that I managed to get my point across.

If you think there is a better way to handle it, tell me and I'll do it. I don't dwell on a particular implementation, I merely want the misleading error gone.

Thx
K.

@Karm Karm requested a review from prrace July 24, 2024 12:46
@theRealAph
Copy link
Contributor

There is nothing in this PR that I would accept. It should be withdrawn.

I think we're all finding it a bit hard to understand what to do.

Is the problem that

  • This is not a bug?
  • This is not a bug that should be fixed?
  • The fix is wrong?
  • There is a much better way to fix it?
  • This is an enhancement, but it is not a worthwhile one?

Please, help us here.

@theRealAph
Copy link
Contributor

P.S. There surely is an actual problem here: the error message that is currently output misrepresents the actual cause of the failure.

@Karm
Copy link
Contributor Author

Karm commented Sep 13, 2024

ping @prrace

@jerboaa
Copy link
Contributor

jerboaa commented Sep 23, 2024

@Karm There are a few things that have not yet been addressed:

  • @avu mentioned here that copyright years of the file need updating
  • There is a title mismatch between this PR and the JBS entry. Which one should it be if any? (and I'll help you fix that one).

I'd also merge in latest master since x86 tests have since been disabled. That should fix the GHA issue.

Thanks!

@Karm
Copy link
Contributor Author

Karm commented Sep 28, 2024

Hello @jerboaa,

  • @avu mentioned here that copyright years of the file need updating

The year there is 2024 for both edited files.

  • There is a title mismatch between this PR and the JBS entry. Which one should it be if any? (and I'll help you fix that one).

Use the title from this PR, please.

I'd also merge in latest master since x86 tests have since been disabled. That should fix the GHA issue.

Done.

@Karm
Copy link
Contributor Author

Karm commented Sep 28, 2024

Font.java and information leakage

@prrace @jerboaa

Would this solution address the concern? Instead of passing on the whole throwable, we just inspect it and if it is this particular one, we pass on the information to the user?

+++ b/src/java.desktop/share/classes/java/awt/Font.java
@@ -971,6 +971,9 @@ public static Font[] createFonts(InputStream fontStream)
             }
             return createFont0(fontFormat, fontStream, true, tracker);
         } catch (InterruptedException e) {
+            if (e.getCause().getMessage().contains("java.home property not set")) {
+                throw new IOException("Problem reading font data. java.home property not set.");
+            }
             throw new IOException("Problem reading font data.");

@Karm
Copy link
Contributor Author

Karm commented Sep 29, 2024

Font.java and information leakage

@prrace @jerboaa

Would this solution address the concern? Instead of passing on the whole throwable, we just inspect it and if it is this particular one, we pass on the information to the user?

+++ b/src/java.desktop/share/classes/java/awt/Font.java
@@ -971,6 +971,9 @@ public static Font[] createFonts(InputStream fontStream)
             }
             return createFont0(fontFormat, fontStream, true, tracker);
         } catch (InterruptedException e) {
+            if (e.getCause().getMessage().contains("java.home property not set")) {
+                throw new IOException("Problem reading font data. java.home property not set.");
+            }
             throw new IOException("Problem reading font data.");

I retract this. It's not that inconvenient and I don't want to complicate this PR.

@jerboaa
Copy link
Contributor

jerboaa commented Sep 30, 2024

  • There is a title mismatch between this PR and the JBS entry. Which one should it be if any? (and I'll help you fix that one).

Use the title from this PR, please.

Done.

@Karm
Copy link
Contributor Author

Karm commented Oct 3, 2024

@prrace I added a test that triggers the described issue.

Unpatched awt_LoadLibrary.c

$ TIME=`date +%s`;mkdir -p test.${TIME}/jdk/JTwork test.${TIME}/jdk/JTreport; jtreg -a -ignore:quiet -w:test.${TIME}/jdk/JTwork -r:test.${TIME}/jdk/JTr
eport -jdk:/home/karm/workspaceRH/jdk/build/linux-x86_64-server-release/images/graal-builder-jdk/ /home/karm/workspaceRH/jdk/test/jdk/java/awt/Headless/;
Test results: passed: 14; failed: 1
...
Output and diagnostic info for process 578718 was saved into 'pid-578718-output.log'
----------System.err:(38/2940)----------
 stdout: [Transforming java.awt.GraphicsEnvironment.
isHeadless removed from java.awt.GraphicsEnvironment.
FATAL ERROR in native method: Could not allocate library name
        at jdk.internal.loader.NativeLibraries.load(java.base@24-internal/Native Method)
        at jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(java.base@24-internal/NativeLibraries.java:331)
...
test result: Failed. Execution failed: `main' threw exception: java.lang.RuntimeException: 'FATAL ERROR in native method: isHeadless method not found' missing from stdout/stderr

Patched awt_LoadLibrary.c

$ TIME=`date +%s`;mkdir -p test.${TIME}/jdk/JTwork test.${TIME}/jdk/JTreport; jtreg -a -ignore:quiet -w:test.${TIME}/jdk/JTwork -r:test.${TIME}/jdk/JTreport -jdk:/home/karm/workspaceRH/jdk/build/linux-x86_64-server-release/images/graal-builder-jdk/ /home/karm/workspaceRH/jdk/test/jdk/java/awt/Headless/;
Test results: passed: 15

@Karm Karm requested a review from avu October 3, 2024 12:31
@Karm
Copy link
Contributor Author

Karm commented Oct 11, 2024

Hello @mrserb, could you take a look, please?

if (graphicsEnvClass == NULL) {
return JNI_TRUE;
}
CHECK_EXCEPTION_FATAL(env, "java/awt/GraphicsEnvironment class not found");
Copy link
Member

@mrserb mrserb Oct 18, 2024

Choose a reason for hiding this comment

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

Taking out any discussion about graal/etc as well as absent of any classes and methods, I agree that the current code is questionable.

But the solution to always print "java/awt/GraphicsEnvironment class not found" does not seems to be correct as well.

One of the problem with this code is an actual conflict between two patches. Both are related to warning caused by pending java exceptions, but implemented differently:

Both of that patches only changes the code paths which were reported by "some tools"/checkjni - this is the reason why the next code was not reported and was not patched(which is wrong):

       graphicsEnvClass = (*env)->FindClass(env,
                                             "java/awt/GraphicsEnvironment");
        if (graphicsEnvClass == NULL) {
            return JNI_TRUE;
        }
        headlessFn = (*env)->GetStaticMethodID(env,
                                               graphicsEnvClass, "isHeadless", "()Z");
        if (headlessFn == NULL) {
            return JNI_TRUE;
        }

If we would like to follow JDK-8031001 approach we should use fatal error here and fail fast, for example if OOM is occurred.
If we would like to follow JDK-8130507 we should clear an exceptions and try to use headless mode.

I guess ignoring an exceptions is not that good and the fix for JDK-8130507 should be rethinking.

Copy link
Member

@mrserb mrserb Oct 23, 2024

Choose a reason for hiding this comment

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

You can change the CHECK_EXCEPTION_FATAL macro to something like

#define CHECK_EXCEPTION_FATAL(env, message) \
    if ((*env)->ExceptionCheck(env)) { \
        (*env)->ExceptionDescribe(env);
        (*env)->FatalError(env, message); \
    }

In this case the root cause of the bug will always be printed.
And then update the fatal message to some generic text.

Note that the FatalError is used in this code since we always should load the library(libawt_xawt or libawt_headless) or fail fast, otherwise we most probably will get an error later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @mrserb for your time. Let me amend the PR and run tests again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

6 participants