changeset 3343:63f5c7704faa

6958221: java.awt.Font.getFamily() leads to JVM crash on Linux on JDK7 for "custom" fonts Reviewed-by: igor, jgodinez
author prr
date Wed, 12 Jan 2011 15:58:15 -0800
parents 9320dcfb5398
children 5aae8b3162d0
files make/sun/awt/mapfile-mawt-vers make/sun/awt/mapfile-vers-linux make/sun/headless/mapfile-vers make/sun/xawt/mapfile-vers src/solaris/classes/sun/awt/X11FontManager.java src/solaris/classes/sun/awt/motif/MToolkit.java src/solaris/native/sun/awt/fontpath.c test/java/awt/FontClass/X11FontPathCrashTest.java
diffstat 8 files changed, 58 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/make/sun/awt/mapfile-mawt-vers	Wed Jan 05 11:21:00 2011 -0800
+++ b/make/sun/awt/mapfile-mawt-vers	Wed Jan 12 15:58:15 2011 -0800
@@ -515,8 +515,7 @@
 		getDefaultConfig;
                 Java_sun_font_FontConfigManager_getFontConfig;
                 Java_sun_font_FontConfigManager_getFontConfigAASettings;
-		Java_sun_awt_X11FontManager_getFontPath;
-		Java_sun_awt_X11FontManager_setNativeFontPath;
+		Java_sun_awt_X11FontManager_getFontPathNative;
 		Java_sun_font_SunFontManager_populateFontFileNameMap;
 
 		# CDE private entry point
--- a/make/sun/awt/mapfile-vers-linux	Wed Jan 05 11:21:00 2011 -0800
+++ b/make/sun/awt/mapfile-vers-linux	Wed Jan 12 15:58:15 2011 -0800
@@ -537,8 +537,7 @@
 		getDefaultConfig;
                 Java_sun_font_FontConfigManager_getFontConfig;
                 Java_sun_font_FontConfigManager_getFontConfigAASettings;
-		Java_sun_awt_X11FontManager_getFontPath;
-		Java_sun_awt_X11FontManager_setNativeFontPath;
+		Java_sun_awt_X11FontManager_getFontPathNative;
 		Java_sun_font_SunFontManager_populateFontFileNameMap;
 
 		# CDE private entry point
--- a/make/sun/headless/mapfile-vers	Wed Jan 05 11:21:00 2011 -0800
+++ b/make/sun/headless/mapfile-vers	Wed Jan 12 15:58:15 2011 -0800
@@ -65,7 +65,6 @@
                 Java_sun_font_FontConfigManager_getFontConfig;
                 Java_sun_font_FontConfigManager_getFontConfigAASettings;
                 Java_sun_font_FontConfigManager_getFontConfigVersion;
-		Java_sun_awt_X11FontManager_getFontPath;
 
 		Java_sun_awt_FontDescriptor_initIDs;
 		Java_sun_awt_PlatformFont_initIDs;
--- a/make/sun/xawt/mapfile-vers	Wed Jan 05 11:21:00 2011 -0800
+++ b/make/sun/xawt/mapfile-vers	Wed Jan 12 15:58:15 2011 -0800
@@ -188,8 +188,7 @@
         Java_sun_font_FontConfigManager_getFontConfig;
         Java_sun_font_FontConfigManager_getFontConfigAASettings;
         Java_sun_font_FontConfigManager_getFontConfigVersion;
-	Java_sun_awt_X11FontManager_getFontPath;
-	Java_sun_font_X11FontManager_setNativeFontPath;
+	Java_sun_awt_X11FontManager_getFontPathNative;
         Java_sun_awt_X11GraphicsEnvironment_initDisplay;
         Java_sun_awt_X11GraphicsEnvironment_initGLX;
 	Java_sun_awt_X11GraphicsEnvironment_initXRender;
--- a/src/solaris/classes/sun/awt/X11FontManager.java	Wed Jan 05 11:21:00 2011 -0800
+++ b/src/solaris/classes/sun/awt/X11FontManager.java	Wed Jan 12 15:58:15 2011 -0800
@@ -718,25 +718,6 @@
         fontdirs = (String[])fontConfigDirs.toArray(new String[0]);
     }
 
-    /* Called by MToolkit to set the X11 font path */
-    public static void setNativeFontPath() {
-        if (fontdirs == null) {
-            return;
-        }
-
-        // need to register these individually rather than by one call
-        // to ensure that one bad directory doesn't cause all to be rejected
-        for (int i=0; i<fontdirs.length; i++) {
-            if (FontUtilities.debugFonts()) {
-                FontUtilities.getLogger().info("Add " + fontdirs[i] + " to X11 fontpath");
-            }
-            setNativeFontPath(fontdirs[i]);
-        }
-    }
-
-    private synchronized static native void setNativeFontPath(String fontPath);
-
-
     // Implements SunGraphicsEnvironment.createFontConfiguration.
     protected FontConfiguration createFontConfiguration() {
         /* The logic here decides whether to use a preconfigured
@@ -780,7 +761,12 @@
                                       preferLocaleFonts, preferPropFonts);
     }
 
-    public synchronized native String getFontPath(boolean noType1Fonts);
+    public synchronized native String getFontPathNative(boolean noType1Fonts);
+
+    public synchronized String getFontPath(boolean noType1Fonts) {
+        isHeadless(); // make sure GE is inited, as its the X11 lock.
+        return getFontPathNative(noType1Fonts);
+    }
 
     public String[] getDefaultPlatformFont() {
         if (defaultPlatformFont != null) {
--- a/src/solaris/classes/sun/awt/motif/MToolkit.java	Wed Jan 05 11:21:00 2011 -0800
+++ b/src/solaris/classes/sun/awt/motif/MToolkit.java	Wed Jan 12 15:58:15 2011 -0800
@@ -120,13 +120,6 @@
                              getDefaultConfiguration());
         }
 
-        /* Add font properties font directories to the X11 font path.
-         * Its called here *after* the X connection has been initialised
-         * and when we know that MToolkit is the one that will be used,
-         * since XToolkit doesn't need the X11 font path set
-         */
-        X11FontManager.getInstance().setNativeFontPath();
-
         motifdnd = ((Boolean)java.security.AccessController.doPrivileged(
             new GetBooleanAction("awt.dnd.motifdnd"))).booleanValue();
     }
--- a/src/solaris/native/sun/awt/fontpath.c	Wed Jan 05 11:21:00 2011 -0800
+++ b/src/solaris/native/sun/awt/fontpath.c	Wed Jan 12 15:58:15 2011 -0800
@@ -502,6 +502,13 @@
 #ifdef __linux__        /* There's no headless build on linux ... */
     if (!AWTIsHeadless()) { /* .. so need to call a function to check */
 #endif
+      /* Using the X11 font path to locate font files is now a fallback
+       * useful only if fontconfig failed, or is incomplete. So we could
+       * remove this code completely and the consequences should be rare
+       * and non-fatal. If this happens, then the calling Java code can
+       * be modified to no longer require that the AWT lock (the X11GE)
+       * be initialised prior to calling this code.
+       */
     AWT_LOCK();
     if (isDisplayLocal(env)) {
         x11dirs = getX11FontPath();
@@ -527,7 +534,7 @@
     return path;
 }
 
-JNIEXPORT jstring JNICALL Java_sun_awt_X11FontManager_getFontPath
+JNIEXPORT jstring JNICALL Java_sun_awt_X11FontManager_getFontPathNative
 (JNIEnv *env, jobject thiz, jboolean noType1) {
     jstring ret;
     static char *ptr = NULL; /* retain result across calls */
@@ -539,71 +546,6 @@
     return ret;
 }
 
-/*
- * In general setting the font path in a remote display situation is
- * problematic. But for Solaris->Solaris the paths needed by the JRE should
- * also be available to the server, although we have no way to check this
- * for sure.
- * So set the font path if we think its safe to do so:
- * All Solaris X servers at least back to 2.6 and up to Solaris 10
- * define the exact same vendor string.
- * The version number for Solaris 2.6 is 3600, for 2.7 is 3610 and
- * for Solaris 8 6410
- * we want to set the font path only for 2.8 and onwards. Earlier releases
- * are unlikely to have the right fonts and can't install "all locales"
- * as needed to be sure. Also Solaris 8 is the earliest release supported
- * by 1.5.
- */
-#ifndef HEADLESS
-static int isSunXServer() {
-#ifdef __solaris__
-  return ((strncmp(ServerVendor(awt_display), "Sun Microsystems, Inc.", 22) == 0) ||
-          (strncmp(ServerVendor(awt_display), "Oracle Corporation", 18) == 0) &&
-          VendorRelease(awt_display) >= 6410);
-#else
-  return 0;
-#endif /* __solaris__ */
-}
-
-/* Avoid re-doing work for every call to setNativeFontPath */
-static int doSetFontPath = -1;
-static int shouldSetXFontPath(JNIEnv *env) {
-  if (doSetFontPath == -1) {
-     doSetFontPath =
-       awt_display != NULL && (isDisplayLocal(env) || isSunXServer());
-  }
-  return doSetFontPath;
-}
-#endif /* !HEADLESS */
-
-JNIEXPORT void JNICALL Java_sun_font_X11FontManager_setNativeFontPath
-(JNIEnv *env, jclass obj, jstring theString) {
-#ifdef HEADLESS
-    return;
-#else
-    fDirRecord fDir;
-    const char *theChars;
-
-    if (awt_display == NULL) {
-        return;
-    }
-    AWT_LOCK();
-    if (shouldSetXFontPath(env)) {
-        theChars = (*env)->GetStringUTFChars (env, theString, 0);
-        fDir.num = 1;
-        fDir.name[0] = theChars;
-        /* printf ("Registering the font path here %s \n", theChars ); */
-        AddFontsToX11FontPath ( &fDir );
-        if (theChars) {
-            (*env)->ReleaseStringUTFChars (env,
-                                           theString, (const char*)theChars);
-        }
-    }
-    AWT_UNLOCK();
-
-#endif
-}
-
 #include <dlfcn.h>
 #ifndef __linux__ /* i.e. is solaris */
 #include <link.h>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/FontClass/X11FontPathCrashTest.java	Wed Jan 12 15:58:15 2011 -0800
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6958221
+ * @summary Test no crashing getting fonts on X11
+ * @run main X11FontPathCrashTest
+ */
+
+import java.awt.*;
+import java.awt.font.*;
+import java.awt.geom.*;
+
+
+public class X11FontPathCrashTest {
+    public static void main(String[] args) {
+        new Font("nonexistentfont", Font.PLAIN, 12).getFamily();
+    }
+}
+