~ubuntu-branches/ubuntu/karmic/openjdk-6b18/karmic-updates

« back to all changes in this revision

Viewing changes to debian/patches/jdk-freetypeScaler-crash.diff

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-09-07 22:16:41 UTC
  • Revision ID: james.westby@ubuntu.com-20100907221641-gilxobgxpaprajd4
Tags: 6b18-1.8.1-2ubuntu1
* Build as separate source, just for armel.
* Merge the -lib package into the -jre-headless package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Description:
 
2
 
 
3
        Fixing the bad JNI code in the font manager code. Two issues:
 
4
        
 
5
          o The JNIEnv is unique to the thread. It cannot be saved by one thread and
 
6
            reused by another. Use GetEnv instead.
 
7
        
 
8
          o The 'font2D' jobject needs to be converted into a global reference because
 
9
            its lifetime exceeds the lifetime of a native method call.
 
10
        
 
11
Evaluation:
 
12
 
 
13
Appropriately register/free everything with the garbage collector.
 
14
 
 
15
Fix:
 
16
 
 
17
# HG changeset patch
 
18
# User martin
 
19
# Date 1224202830 25200
 
20
# Node ID 3c9d6001d8a90698a3540a2a483717f26a98db78
 
21
# Parent  68730f05449cd4f39ce1cb82adc6c4e57f87554f
 
22
Crash in freetypeScaler.c due to insufficient GC protection
 
23
Summary: NewGlobalRef/DeleteGlobalRef as needed.
 
24
Reviewed-by:
 
25
Contributed-by: yamauchi@google.com
 
26
 
 
27
diff --git a/make/sun/font/mapfile-vers.openjdk b/jdk/make/sun/font/mapfile-vers.openjdk
 
28
--- openjdk/jdk/make/sun/font/mapfile-vers.openjdk
 
29
+++ openjdk/jdk/make/sun/font/mapfile-vers.openjdk
 
30
@@ -29,6 +29,7 @@
 
31
 
 
32
 SUNWprivate_1.1 {
 
33
        global:
 
34
+                JNI_OnLoad;
 
35
                 getSunFontIDs;
 
36
                 newLayoutTableCache;
 
37
                 freeLayoutTableCache;
 
38
diff --git a/src/share/native/sun/font/freetypeScaler.c b/src/share/native/sun/font/freetypeScaler.c
 
39
--- openjdk/jdk/src/share/native/sun/font/freetypeScaler.c
 
40
+++ openjdk/jdk/src/share/native/sun/font/freetypeScaler.c
 
41
@@ -48,16 +48,6 @@
 
42
 #define  ROUND(x) ((int) (x+0.5))
 
43
 
 
44
 typedef struct {
 
45
-    /* Important note:
 
46
-         JNI forbids sharing same env between different threads.
 
47
-         We are safe, because pointer is overwritten every time we get into
 
48
-         JNI call (see setupFTContext).
 
49
-
 
50
-         Pointer is used by font data reading callbacks
 
51
-         such as ReadTTFontFileFunc.
 
52
-
 
53
-         NB: We may consider switching to JNI_GetEnv. */
 
54
-    JNIEnv* env;
 
55
     FT_Library library;
 
56
     FT_Face face;
 
57
     jobject font2D;
 
58
@@ -90,6 +80,13 @@
 
59
 void z_error(char *s) {}
 
60
 #endif
 
61
 
 
62
+static JavaVM* jvm = NULL;
 
63
+
 
64
+JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
 
65
+    jvm = vm;
 
66
+    return JNI_VERSION_1_2;
 
67
+}
 
68
+
 
69
 /**************** Error handling utilities *****************/
 
70
 
 
71
 static jmethodID invalidateScalerMID;
 
72
@@ -107,6 +104,10 @@
 
73
 
 
74
     FT_Done_Face(scalerInfo->face);
 
75
     FT_Done_FreeType(scalerInfo->library);
 
76
+
 
77
+    if (scalerInfo->font2D != NULL) {
 
78
+        (*env)->DeleteGlobalRef(env, scalerInfo->font2D);
 
79
+    }
 
80
 
 
81
     if (scalerInfo->directBuffer != NULL) {
 
82
         (*env)->DeleteGlobalRef(env, scalerInfo->directBuffer);
 
83
@@ -131,10 +132,9 @@
 
84
 
 
85
 #define FILEDATACACHESIZE 1024
 
86
 
 
87
-/* NB: is it ever called? */
 
88
 static void CloseTTFontFileFunc(FT_Stream stream) {
 
89
+    JNIEnv* env = (JNIEnv*) JNU_GetEnv(jvm, JNI_VERSION_1_2);
 
90
     FTScalerInfo *scalerInfo = (FTScalerInfo *) stream->pathname.pointer;
 
91
-    JNIEnv* env = scalerInfo->env;
 
92
     jclass tmpClass = (*env)->FindClass(env, "sun/font/TrueTypeFont");
 
93
     jfieldID platNameField =
 
94
          (*env)->GetFieldID(env, tmpClass, "platName", "Ljava/lang/String;");
 
95
@@ -150,8 +150,8 @@
 
96
                                         unsigned char* destBuffer,
 
97
                                         unsigned long numBytes)
 
98
 {
 
99
+    JNIEnv* env = (JNIEnv*) JNU_GetEnv(jvm, JNI_VERSION_1_2);
 
100
     FTScalerInfo *scalerInfo = (FTScalerInfo *) stream->pathname.pointer;
 
101
-    JNIEnv* env = scalerInfo->env;
 
102
     jobject bBuffer;
 
103
     int bread = 0;
 
104
 
 
105
@@ -245,8 +245,7 @@
 
106
     if (scalerInfo == NULL)
 
107
         return 0;
 
108
 
 
109
-    scalerInfo->env = env;
 
110
-    scalerInfo->font2D = font2D;
 
111
+    scalerInfo->font2D = (*env)->NewGlobalRef(env, font2D);
 
112
     scalerInfo->fontDataOffset = 0;
 
113
     scalerInfo->fontDataLength = 0;
 
114
     scalerInfo->fileSize = filesize;
 
115
@@ -263,6 +262,7 @@
 
116
     */
 
117
     error = FT_Init_FreeType(&scalerInfo->library);
 
118
     if (error) {
 
119
+        (*env)->DeleteGlobalRef(env, scalerInfo->font2D);
 
120
         free(scalerInfo);
 
121
         return 0;
 
122
     }
 
123
@@ -331,6 +331,7 @@
 
124
         }
 
125
         if (scalerInfo->fontData != NULL)
 
126
             free(scalerInfo->fontData);
 
127
+        (*env)->DeleteGlobalRef(env, scalerInfo->font2D);
 
128
         free(scalerInfo);
 
129
         return 0;
 
130
     }
 
131
@@ -391,8 +392,10 @@
 
132
                           FTScalerContext *context) {
 
133
     int errCode = 0;
 
134
 
 
135
-    scalerInfo->env = env;
 
136
-    scalerInfo->font2D = font2D;
 
137
+    if (scalerInfo->font2D != NULL) {
 
138
+        (*env)->DeleteGlobalRef(env, scalerInfo->font2D);
 
139
+    }
 
140
+    scalerInfo->font2D = (*env)->NewGlobalRef(env, font2D);
 
141
 
 
142
     FT_Set_Transform(scalerInfo->face, &context->transform, NULL);