Blame SOURCES/tomcat-9.0-memory-leak.patch

9d02a3
diff --git a/java/org/apache/catalina/loader/WebappClassLoaderBase.java b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
9d02a3
index 8746b6b..dc878c6 100644
9d02a3
--- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java
9d02a3
+++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
9d02a3
@@ -1820,41 +1820,13 @@ public abstract class WebappClassLoaderBase extends URLClassLoader
9d02a3
                     // shutting down the executor
9d02a3
                     boolean usingExecutor = false;
9d02a3
                     try {
9d02a3
-
9d02a3
-                        // Runnable wrapped by Thread
9d02a3
-                        // "target" in Sun/Oracle JDK
9d02a3
-                        // "runnable" in IBM JDK
9d02a3
-                        // "action" in Apache Harmony
9d02a3
-                        Object target = null;
9d02a3
-                        for (String fieldName : new String[] { "target", "runnable", "action" }) {
9d02a3
-                            try {
9d02a3
-                                Field targetField = thread.getClass().getDeclaredField(fieldName);
9d02a3
-                                targetField.setAccessible(true);
9d02a3
-                                target = targetField.get(thread);
9d02a3
-                                break;
9d02a3
-                            } catch (NoSuchFieldException nfe) {
9d02a3
-                                continue;
9d02a3
-                            }
9d02a3
-                        }
9d02a3
-
9d02a3
-                        // "java.util.concurrent" code is in public domain,
9d02a3
-                        // so all implementations are similar including our
9d02a3
-                        // internal fork.
9d02a3
-                        if (target != null && target.getClass().getCanonicalName() != null &&
9d02a3
-                                (target.getClass().getCanonicalName().equals(
9d02a3
-                                        "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
9d02a3
-                                        target.getClass().getCanonicalName().equals(
9d02a3
-                                                "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
9d02a3
-                            Field executorField = target.getClass().getDeclaredField("this$0");
9d02a3
-                            executorField.setAccessible(true);
9d02a3
-                            Object executor = executorField.get(target);
9d02a3
-                            if (executor instanceof ThreadPoolExecutor) {
9d02a3
-                                ((ThreadPoolExecutor) executor).shutdownNow();
9d02a3
-                                usingExecutor = true;
9d02a3
-                            } else if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
9d02a3
-                                ((java.util.concurrent.ThreadPoolExecutor) executor).shutdownNow();
9d02a3
-                                usingExecutor = true;
9d02a3
-                            }
9d02a3
+			Object executor = JreCompat.getInstance().getExecutor(thread);
9d02a3
+                        if (executor instanceof ThreadPoolExecutor) {
9d02a3
+                            ((ThreadPoolExecutor) executor).shutdownNow();
9d02a3
+                            usingExecutor = true;
9d02a3
+                        } else if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
9d02a3
+                            ((java.util.concurrent.ThreadPoolExecutor) executor).shutdownNow();
9d02a3
+                            usingExecutor = true;
9d02a3
                         }
9d02a3
                     } catch (NoSuchFieldException | IllegalAccessException | RuntimeException e) {
9d02a3
                         // InaccessibleObjectException is only available in Java 9+,
9d02a3
@@ -2306,6 +2278,12 @@ public abstract class WebappClassLoaderBase extends URLClassLoader
9d02a3
 
9d02a3
 
9d02a3
     private void clearReferencesObjectStreamClassCaches() {
9d02a3
+	if (JreCompat.isJre19Available()) {
9d02a3
+            // The memory leak this fixes has been fixed in Java 19 onwards,
9d02a3
+            // 17.0.4 onwards and 11.0.16 onwards
9d02a3
+            // See https://bugs.openjdk.java.net/browse/JDK-8277072
9d02a3
+            return;
9d02a3
+        }
9d02a3
         try {
9d02a3
             Class clazz = Class.forName("java.io.ObjectStreamClass$Caches");
9d02a3
             clearCache(clazz, "localDescs");
9d02a3
@@ -2333,14 +2311,19 @@ public abstract class WebappClassLoaderBase extends URLClassLoader
9d02a3
             throws ReflectiveOperationException, SecurityException, ClassCastException {
9d02a3
         Field f = target.getDeclaredField(mapName);
9d02a3
         f.setAccessible(true);
9d02a3
-        Map map = (Map) f.get(null);
9d02a3
-        Iterator keys = map.keySet().iterator();
9d02a3
-        while (keys.hasNext()) {
9d02a3
-            Object key = keys.next();
9d02a3
-            if (key instanceof Reference) {
9d02a3
-                Object clazz = ((Reference) key).get();
9d02a3
-                if (loadedByThisOrChild(clazz)) {
9d02a3
-                    keys.remove();
9d02a3
+	Object map = f.get(null);
9d02a3
+        // Avoid trying to clear references if Tomcat is running on a JRE that
9d02a3
+        // includes the fix for this memory leak
9d02a3
+        // See https://bugs.openjdk.java.net/browse/JDK-8277072
9d02a3
+        if (map instanceof Map) {
9d02a3
+            Iterator keys = ((Map) map).keySet().iterator();
9d02a3
+            while (keys.hasNext()) {
9d02a3
+                Object key = keys.next();
9d02a3
+                if (key instanceof Reference) {
9d02a3
+                    Object clazz = ((Reference) key).get();
9d02a3
+                    if (loadedByThisOrChild(clazz)) {
9d02a3
+                        keys.remove();
9d02a3
+                    }
9d02a3
                 }
9d02a3
             }
9d02a3
         }
9d02a3
diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java b/java/org/apache/tomcat/util/compat/JreCompat.java
9d02a3
index 62df145..e5df728 100644
9d02a3
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
9d02a3
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
9d02a3
@@ -19,6 +19,7 @@ package org.apache.tomcat.util.compat;
9d02a3
 import java.io.File;
9d02a3
 import java.io.IOException;
9d02a3
 import java.lang.reflect.AccessibleObject;
9d02a3
+import java.lang.reflect.Field;
9d02a3
 import java.lang.reflect.InvocationTargetException;
9d02a3
 import java.lang.reflect.Method;
9d02a3
 import java.net.SocketAddress;
9d02a3
@@ -45,6 +46,7 @@ public class JreCompat {
9d02a3
 
9d02a3
     private static final JreCompat instance;
9d02a3
     private static final boolean graalAvailable;
9d02a3
+    private static final boolean jre19Available;
9d02a3
     private static final boolean jre16Available;
9d02a3
     private static final boolean jre11Available;
9d02a3
     private static final boolean jre9Available;
9d02a3
@@ -67,18 +69,26 @@ public class JreCompat {
9d02a3
 
9d02a3
         // This is Tomcat 9 with a minimum Java version of Java 8.
9d02a3
         // Look for the highest supported JVM first
9d02a3
-        if (Jre16Compat.isSupported()) {
9d02a3
+	if (Jre19Compat.isSupported()) {
9d02a3
+            instance = new Jre19Compat();
9d02a3
+            jre9Available = true;
9d02a3
+            jre16Available = true;
9d02a3
+            jre19Available = true;
9d02a3
+        } else if (Jre16Compat.isSupported()) {
9d02a3
             instance = new Jre16Compat();
9d02a3
             jre9Available = true;
9d02a3
             jre16Available = true;
9d02a3
+            jre19Available = false;
9d02a3
         } else if (Jre9Compat.isSupported()) {
9d02a3
             instance = new Jre9Compat();
9d02a3
             jre9Available = true;
9d02a3
             jre16Available = false;
9d02a3
+            jre19Available = false;
9d02a3
         } else {
9d02a3
             instance = new JreCompat();
9d02a3
             jre9Available = false;
9d02a3
             jre16Available = false;
9d02a3
+            jre19Available = false;
9d02a3
         }
9d02a3
         jre11Available = instance.jarFileRuntimeMajorVersion() >= 11;
9d02a3
 
9d02a3
@@ -124,6 +134,9 @@ public class JreCompat {
9d02a3
         return jre16Available;
9d02a3
     }
9d02a3
 
9d02a3
+    public static boolean isJre19Available() {
9d02a3
+        return jre19Available;
9d02a3
+    }
9d02a3
 
9d02a3
     // Java 8 implementation of Java 9 methods
9d02a3
 
9d02a3
@@ -303,6 +316,8 @@ public class JreCompat {
9d02a3
     }
9d02a3
 
9d02a3
 
9d02a3
+    // Java 8 implementations of Java 16 methods
9d02a3
+
9d02a3
     /**
9d02a3
      * Return Unix domain socket address for given path.
9d02a3
      * @param path The path
9d02a3
@@ -329,4 +344,63 @@ public class JreCompat {
9d02a3
     public SocketChannel openUnixDomainSocketChannel() {
9d02a3
         throw new UnsupportedOperationException(sm.getString("jreCompat.noUnixDomainSocket"));
9d02a3
     }
9d02a3
+
9d02a3
+
9d02a3
+    // Java 8 implementations of Java 19 methods
9d02a3
+
9d02a3
+    /**
9d02a3
+     * Obtains the executor, if any, used to create the provided thread.
9d02a3
+     *
9d02a3
+     * @param thread    The thread to examine
9d02a3
+     *
9d02a3
+     * @return  The executor, if any, that created the provided thread
9d02a3
+     *
9d02a3
+     * @throws NoSuchFieldException
9d02a3
+     *              If a field used via reflection to obtain the executor cannot
9d02a3
+     *              be found
9d02a3
+     * @throws SecurityException
9d02a3
+     *              If a security exception occurs while trying to identify the
9d02a3
+     *              executor
9d02a3
+     * @throws IllegalArgumentException
9d02a3
+     *              If the instance object does not match the class of the field
9d02a3
+     *              when obtaining a field value via reflection
9d02a3
+     * @throws IllegalAccessException
9d02a3
+     *              If a field is not accessible due to access restrictions
9d02a3
+     */
9d02a3
+    public Object getExecutor(Thread thread)
9d02a3
+            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
9d02a3
+
9d02a3
+        Object result = null;
9d02a3
+
9d02a3
+        // Runnable wrapped by Thread
9d02a3
+        // "target" in Sun/Oracle JDK
9d02a3
+        // "runnable" in IBM JDK
9d02a3
+        // "action" in Apache Harmony
9d02a3
+        Object target = null;
9d02a3
+        for (String fieldName : new String[] { "target", "runnable", "action" }) {
9d02a3
+            try {
9d02a3
+                Field targetField = thread.getClass().getDeclaredField(fieldName);
9d02a3
+                targetField.setAccessible(true);
9d02a3
+                target = targetField.get(thread);
9d02a3
+                break;
9d02a3
+            } catch (NoSuchFieldException nfe) {
9d02a3
+                continue;
9d02a3
+            }
9d02a3
+        }
9d02a3
+
9d02a3
+        // "java.util.concurrent" code is in public domain,
9d02a3
+        // so all implementations are similar including our
9d02a3
+        // internal fork.
9d02a3
+        if (target != null && target.getClass().getCanonicalName() != null &&
9d02a3
+                (target.getClass().getCanonicalName().equals(
9d02a3
+                        "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
9d02a3
+                        target.getClass().getCanonicalName().equals(
9d02a3
+                                "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
9d02a3
+            Field executorField = target.getClass().getDeclaredField("this$0");
9d02a3
+            executorField.setAccessible(true);
9d02a3
+            result = executorField.get(target);
9d02a3
+        }
9d02a3
+
9d02a3
+        return result;
9d02a3
+    }
9d02a3
 }
9d02a3
diff --git a/java/org/apache/tomcat/util/compat/LocalStrings.properties b/java/org/apache/tomcat/util/compat/LocalStrings.properties
9d02a3
index 79427da..c4c2f7d 100644
9d02a3
--- a/java/org/apache/tomcat/util/compat/LocalStrings.properties
9d02a3
+++ b/java/org/apache/tomcat/util/compat/LocalStrings.properties
9d02a3
@@ -16,6 +16,8 @@
9d02a3
 jre16Compat.javaPre16=Class not found so assuming code is running on a pre-Java 16 JVM
9d02a3
 jre16Compat.unexpected=Failed to create references to Java 16 classes and methods
9d02a3
 
9d02a3
+jre19Compat.javaPre19=Class not found so assuming code is running on a pre-Java 19 JVM
9d02a3
+
9d02a3
 jre9Compat.invalidModuleUri=The module URI provided [{0}] could not be converted to a URL for the JarScanner to process
9d02a3
 jre9Compat.javaPre9=Class not found so assuming code is running on a pre-Java 9 JVM
9d02a3
 jre9Compat.unexpected=Failed to create references to Java 9 classes and methods
9d02a3
diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml
9d02a3
index d118196..42dfe38 100644
9d02a3
--- a/webapps/docs/config/context.xml
9d02a3
+++ b/webapps/docs/config/context.xml
9d02a3
@@ -769,7 +769,11 @@
9d02a3
         therefore requires that the command line option
9d02a3
         -XaddExports:java.base/java.io=ALL-UNNAMED is set
9d02a3
         when running on Java 9 and above. If not specified, the default value of
9d02a3
-        true will be used.

9d02a3
+	true will be used.

9d02a3
+	

The memory leak associated with ObjectStreamClass has

9d02a3
+        been fixed in Java 19 onwards, Java 17.0.4 onwards and Java 11.0.16
9d02a3
+        onwards. The check will be disabled when running on a version
9d02a3
+        of Java that contains the fix.

9d02a3
       </attribute>
9d02a3
 
9d02a3
       <attribute name="clearReferencesRmiTargets" required="false">
9d02a3
diff --git a/java/org/apache/tomcat/util/compat/Jre19Compat.java b/java/org/apache/tomcat/util/compat/Jre19Compat.java
9d02a3
new file mode 100644
9d02a3
index 0000000000..fb94810b40
9d02a3
--- /dev/null
9d02a3
+++ b/java/org/apache/tomcat/util/compat/Jre19Compat.java
9d02a3
@@ -0,0 +1,84 @@
9d02a3
+/*
9d02a3
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
9d02a3
+ *  contributor license agreements.  See the NOTICE file distributed with
9d02a3
+ *  this work for additional information regarding copyright ownership.
9d02a3
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
9d02a3
+ *  (the "License"); you may not use this file except in compliance with
9d02a3
+ *  the License.  You may obtain a copy of the License at
9d02a3
+ *
9d02a3
+ *      http://www.apache.org/licenses/LICENSE-2.0
9d02a3
+ *
9d02a3
+ *  Unless required by applicable law or agreed to in writing, software
9d02a3
+ *  distributed under the License is distributed on an "AS IS" BASIS,
9d02a3
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9d02a3
+ *  See the License for the specific language governing permissions and
9d02a3
+ *  limitations under the License.
9d02a3
+ */
9d02a3
+package org.apache.tomcat.util.compat;
9d02a3
+
9d02a3
+import java.lang.reflect.Field;
9d02a3
+
9d02a3
+import org.apache.juli.logging.Log;
9d02a3
+import org.apache.juli.logging.LogFactory;
9d02a3
+import org.apache.tomcat.util.res.StringManager;
9d02a3
+
9d02a3
+public class Jre19Compat extends Jre16Compat {
9d02a3
+
9d02a3
+    private static final Log log = LogFactory.getLog(Jre19Compat.class);
9d02a3
+    private static final StringManager sm = StringManager.getManager(Jre19Compat.class);
9d02a3
+
9d02a3
+    private static final boolean supported;
9d02a3
+
9d02a3
+    static {
9d02a3
+        // Don't need any Java 19 specific classes (yet) so just test for one of
9d02a3
+        // the new ones for now.
9d02a3
+        Class c1 = null;
9d02a3
+        try {
9d02a3
+            c1 = Class.forName("java.lang.WrongThreadException");
9d02a3
+        } catch (ClassNotFoundException cnfe) {
9d02a3
+            // Must be pre-Java 16
9d02a3
+            log.debug(sm.getString("jre19Compat.javaPre19"), cnfe);
9d02a3
+        }
9d02a3
+
9d02a3
+        supported = (c1 != null);
9d02a3
+    }
9d02a3
+
9d02a3
+    static boolean isSupported() {
9d02a3
+        return supported;
9d02a3
+    }
9d02a3
+
9d02a3
+    @Override
9d02a3
+    public Object getExecutor(Thread thread)
9d02a3
+            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
9d02a3
+
9d02a3
+        Object result = super.getExecutor(thread);
9d02a3
+
9d02a3
+        if (result == null) {
9d02a3
+            Object holder = null;
9d02a3
+            Object task = null;
9d02a3
+            try {
9d02a3
+                Field holderField = thread.getClass().getDeclaredField("holder");
9d02a3
+                holderField.setAccessible(true);
9d02a3
+                holder = holderField.get(thread);
9d02a3
+
9d02a3
+                Field taskField = holder.getClass().getDeclaredField("task");
9d02a3
+                taskField.setAccessible(true);
9d02a3
+                task = taskField.get(holder);
9d02a3
+            } catch (NoSuchFieldException nfe) {
9d02a3
+                return null;
9d02a3
+            }
9d02a3
+
9d02a3
+            if (task!= null && task.getClass().getCanonicalName() != null &&
9d02a3
+                    (task.getClass().getCanonicalName().equals(
9d02a3
+                            "org.apache.tomcat.util.threads.ThreadPoolExecutor.Worker") ||
9d02a3
+                            task.getClass().getCanonicalName().equals(
9d02a3
+                                    "java.util.concurrent.ThreadPoolExecutor.Worker"))) {
9d02a3
+                Field executorField = task.getClass().getDeclaredField("this$0");
9d02a3
+                executorField.setAccessible(true);
9d02a3
+                result = executorField.get(task);
9d02a3
+            }
9d02a3
+        }
9d02a3
+
9d02a3
+        return result;
9d02a3
+    }
9d02a3
+}