summaryrefslogtreecommitdiff
path: root/java/openjdk6/files/icedtea/openjdk/5102804-memory_leak.patch
blob: 01dd35c1bee23605b555ae9208bfc8af5f5b2997 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# HG changeset patch
# User andrew
# Date 1365704033 -3600
# Node ID 06255d9f82761abc74c30f31fda00968ffef4bc3
# Parent  a939f541de9af5ccb78225c27cd46cd7dc6bcf87
5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param (includes WeakCache from 6397609)
Reviewed-by: peterz

diff --git a/src/share/classes/com/sun/beans/WeakCache.java b/src/share/classes/com/sun/beans/WeakCache.java
new file mode 100644
--- /dev/null
+++ jdk/src/share/classes/com/sun/beans/WeakCache.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  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.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+package com.sun.beans;
+
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
+
+import java.util.Map;
+import java.util.WeakHashMap;
+
+/**
+ * A hashtable-based cache with weak keys and weak values.
+ * An entry in the map will be automatically removed
+ * when its key is no longer in the ordinary use.
+ * A value will be automatically removed as well
+ * when it is no longer in the ordinary use.
+ *
+ * @since 1.7
+ *
+ * @author Sergey A. Malenkov
+ */
+public final class WeakCache<K, V> {
+    private final Map<K, Reference<V>> map = new WeakHashMap<K, Reference<V>>();
+
+    /**
+     * Returns a value to which the specified {@code key} is mapped,
+     * or {@code null} if this map contains no mapping for the {@code key}.
+     *
+     * @param key  the key whose associated value is returned
+     * @return a value to which the specified {@code key} is mapped
+     */
+    public V get(K key) {
+        Reference<V> reference = this.map.get(key);
+        if (reference == null) {
+            return null;
+        }
+        V value = reference.get();
+        if (value == null) {
+            this.map.remove(key);
+        }
+        return value;
+    }
+
+    /**
+     * Associates the specified {@code value} with the specified {@code key}.
+     * Removes the mapping for the specified {@code key} from this cache
+     * if it is present and the specified {@code value} is {@code null}.
+     * If the cache previously contained a mapping for the {@code key},
+     * the old value is replaced by the specified {@code value}.
+     *
+     * @param key    the key with which the specified value is associated
+     * @param value  the value to be associated with the specified key
+     */
+    public void put(K key, V value) {
+        if (value != null) {
+            this.map.put(key, new WeakReference<V>(value));
+        }
+        else {
+            this.map.remove(key);
+        }
+    }
+
+    /**
+     * Removes all of the mappings from this cache.
+     */
+    public void clear() {
+        this.map.clear();
+    }
+}
diff --git a/src/share/classes/java/beans/Introspector.java b/src/share/classes/java/beans/Introspector.java
--- jdk/src/share/classes/java/beans/Introspector.java
+++ jdk/src/share/classes/java/beans/Introspector.java
@@ -25,25 +25,18 @@
 
 package java.beans;
 
+import com.sun.beans.WeakCache;
 import com.sun.beans.finder.ClassFinder;
 
-import java.lang.ref.Reference;
-import java.lang.ref.SoftReference;
-
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-
-import java.util.Collections;
 import java.util.Map;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.EventListener;
 import java.util.List;
-import java.util.WeakHashMap;
 import java.util.TreeMap;
 import sun.awt.AppContext;
 import sun.reflect.misc.ReflectUtil;
@@ -110,8 +103,8 @@
     public final static int IGNORE_ALL_BEANINFO        = 3;
 
     // Static Caches to speed up introspection.
-    private static Map declaredMethodCache =
-        Collections.synchronizedMap(new WeakHashMap());
+    private static WeakCache<Class<?>, Method[]> declaredMethodCache =
+            new WeakCache<Class<?>, Method[]>();
 
     private static final Object BEANINFO_CACHE = new Object();
 
@@ -177,20 +170,21 @@
         if (!ReflectUtil.isPackageAccessible(beanClass)) {
             return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
         }
-        Map<Class<?>, BeanInfo> map;
         synchronized (BEANINFO_CACHE) {
-            map = (Map<Class<?>, BeanInfo>) AppContext.getAppContext().get(BEANINFO_CACHE);
-            if (map == null) {
-                map = Collections.synchronizedMap(new WeakHashMap<Class<?>, BeanInfo>());
-                AppContext.getAppContext().put(BEANINFO_CACHE, map);
+            WeakCache<Class<?>, BeanInfo> beanInfoCache =
+                    (WeakCache<Class<?>, BeanInfo>) AppContext.getAppContext().get(BEANINFO_CACHE);
+
+            if (beanInfoCache == null) {
+                beanInfoCache = new WeakCache<Class<?>, BeanInfo>();
+                AppContext.getAppContext().put(BEANINFO_CACHE, beanInfoCache);
             }
+            BeanInfo beanInfo = beanInfoCache.get(beanClass);
+            if (beanInfo == null) {
+                beanInfo = (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
+                beanInfoCache.put(beanClass, beanInfo);
+            }
+            return beanInfo;
         }
-        BeanInfo bi = map.get(beanClass);
-        if (bi == null) {
-            bi = (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
-            map.put(beanClass, bi);
-        }
-        return bi;
     }
 
     /**
@@ -337,11 +331,13 @@
      */
 
     public static void flushCaches() {
-        Map map = (Map) AppContext.getAppContext().get(BEANINFO_CACHE);
-        if (map != null) {
-            map.clear();
+        synchronized (BEANINFO_CACHE) {
+            WeakCache beanInfoCache = (WeakCache) AppContext.getAppContext().get(BEANINFO_CACHE);
+            if (beanInfoCache != null) {
+                beanInfoCache.clear();
+            }
+            declaredMethodCache.clear();
         }
-        declaredMethodCache.clear();
     }
 
     /**
@@ -363,11 +359,13 @@
         if (clz == null) {
             throw new NullPointerException();
         }
-        Map map = (Map) AppContext.getAppContext().get(BEANINFO_CACHE);
-        if (map != null) {
-            map.remove(clz);
+        synchronized (BEANINFO_CACHE) {
+            WeakCache beanInfoCache = (WeakCache) AppContext.getAppContext().get(BEANINFO_CACHE);
+            if (beanInfoCache != null) {
+                beanInfoCache.put(clz, null);
+            }
+            declaredMethodCache.put(clz, null);
         }
-        declaredMethodCache.remove(clz);
     }
 
     //======================================================================
@@ -1306,41 +1304,26 @@
     /*
      * Internal method to return *public* methods within a class.
      */
-    private static synchronized Method[] getPublicDeclaredMethods(Class clz) {
+    private static Method[] getPublicDeclaredMethods(Class clz) {
         // Looking up Class.getDeclaredMethods is relatively expensive,
         // so we cache the results.
-        Method[] result = null;
         if (!ReflectUtil.isPackageAccessible(clz)) {
             return new Method[0];
         }
-        final Class fclz = clz;
-        Reference ref = (Reference)declaredMethodCache.get(fclz);
-        if (ref != null) {
-            result = (Method[])ref.get();
-            if (result != null) {
-                return result;
+        synchronized (BEANINFO_CACHE) {
+            Method[] result = declaredMethodCache.get(clz);
+            if (result == null) {
+                result = clz.getMethods();
+                for (int i = 0; i < result.length; i++) {
+                    Method method = result[i];
+                    if (!method.getDeclaringClass().equals(clz)) {
+                        result[i] = null;
+                    }
+                }
+                declaredMethodCache.put(clz, result);
             }
+            return result;
         }
-
-        // We have to raise privilege for getDeclaredMethods
-        result = (Method[]) AccessController.doPrivileged(new PrivilegedAction() {
-                public Object run() {
-                    return fclz.getDeclaredMethods();
-                }
-            });
-
-
-        // Null out any non-public methods.
-        for (int i = 0; i < result.length; i++) {
-            Method method = result[i];
-            int mods = method.getModifiers();
-            if (!Modifier.isPublic(mods)) {
-                result[i] = null;
-            }
-        }
-        // Add it to the cache.
-        declaredMethodCache.put(fclz, new SoftReference(result));
-        return result;
     }
 
     //======================================================================
diff --git a/test/java/beans/Introspector/Test5102804.java b/test/java/beans/Introspector/Test5102804.java
new file mode 100644
--- /dev/null
+++ jdk/test/java/beans/Introspector/Test5102804.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 5102804
+ * @summary Tests memory leak
+ * @author Sergey Malenkov
+ */
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.beans.SimpleBeanInfo;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+public class Test5102804 {
+    private static final String BEAN_NAME = "Test5102804$Example";
+    private static final String BEAN_INFO_NAME = BEAN_NAME + "BeanInfo";
+
+    public static void main(String[] args) {
+        if (!isCollectible(getReference()))
+            throw new Error("Reference is not collected");
+    }
+
+    private static Reference getReference() {
+        try {
+            ClassLoader loader = new Loader();
+            Class type = Class.forName(BEAN_NAME, true, loader);
+            if (!type.getClassLoader().equals(loader)) {
+                throw new Error("Wrong class loader");
+            }
+            BeanInfo info = Introspector.getBeanInfo(type);
+            if (0 != info.getDefaultPropertyIndex()) {
+                throw new Error("Wrong bean info found");
+            }
+            return new WeakReference<Class>(type);
+        }
+        catch (IntrospectionException exception) {
+            throw new Error("Introspection Error", exception);
+        }
+        catch (ClassNotFoundException exception) {
+            throw new Error("Class Not Found", exception);
+        }
+    }
+
+    private static boolean isCollectible(Reference reference) {
+        int[] array = new int[10];
+        while (true) {
+            try {
+                array = new int[array.length + array.length / 3];
+            }
+            catch (OutOfMemoryError error) {
+                return null == reference.get();
+            }
+        }
+    }
+
+    /**
+     * Custom class loader to load the Example class by itself.
+     * Could also load it from a different code source, but this is easier to set up.
+     */
+    private static final class Loader extends URLClassLoader {
+        Loader() {
+            super(new URL[] {
+                    Test5102804.class.getProtectionDomain().getCodeSource().getLocation()
+            });
+        }
+
+        @Override
+        protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
+            Class c = findLoadedClass(name);
+            if (c == null) {
+                if (BEAN_NAME.equals(name) || BEAN_INFO_NAME.equals(name)) {
+                    c = findClass(name);
+                }
+                else try {
+                    c = getParent().loadClass(name);
+                }
+                catch (ClassNotFoundException exception) {
+                    c = findClass(name);
+                }
+            }
+            if (resolve) {
+                resolveClass(c);
+            }
+            return c;
+        }
+    }
+
+    /**
+     * A simple bean to load from the Loader class, not main class loader.
+     */
+    public static final class Example {
+        private int value;
+
+        public int getValue() {
+            return value;
+        }
+
+        public void setValue(int value) {
+            this.value = value;
+        }
+    }
+
+    /**
+     * The BeanInfo for the Example class.
+     * It is also loaded from the Loader class.
+     */
+    public static final class ExampleBeanInfo extends SimpleBeanInfo {
+        @Override
+        public int getDefaultPropertyIndex() {
+            return 0;
+        }
+
+        @Override
+        public PropertyDescriptor[] getPropertyDescriptors() {
+            try {
+                return new PropertyDescriptor[] {
+                        new PropertyDescriptor("value", Class.forName(BEAN_NAME))
+                };
+            }
+            catch (ClassNotFoundException exception) {
+                return null;
+            }
+            catch (IntrospectionException exception) {
+                return null;
+            }
+        }
+    }
+}