Skip to content

Commit 44b395b

Browse files
authored
Merge branch 'master' into feature/add-script-listener
2 parents abfbc35 + 4bd517d commit 44b395b

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

src/main/java/org/jenkinsci/plugins/scriptsecurity/sandbox/groovy/SecureGroovyScript.java

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import hudson.model.Run;
3737
import hudson.model.TaskListener;
3838
import hudson.util.FormValidation;
39+
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
3940

4041
import java.beans.Introspector;
4142
import java.io.Serializable;
@@ -207,6 +208,7 @@ private static void cleanUpClass(Class<?> clazz, Set<ClassLoader> encounteredLoa
207208
if (encounteredClasses.add(clazz)) {
208209
LOGGER.log(Level.FINER, "found {0}", clazz.getName());
209210
Introspector.flushFromCaches(clazz);
211+
cleanUpClassInfoCache(clazz);
210212
cleanUpGlobalClassSet(clazz);
211213
cleanUpClassHelperCache(clazz);
212214
cleanUpObjectStreamClassCaches(clazz);
@@ -267,6 +269,45 @@ private static void cleanUpGlobalClassValue(@NonNull ClassLoader loader) throws
267269
}
268270
}
269271

272+
private static void cleanUpClassInfoCache(Class<?> clazz) {
273+
JavaSpecificationVersion current = JavaSpecificationVersion.forCurrentJVM();
274+
if (current.isNewerThan(new JavaSpecificationVersion("1.8"))
275+
&& current.isOlderThan(new JavaSpecificationVersion("16"))) {
276+
try {
277+
// TODO Work around JDK-8231454.
278+
Class<?> classInfoC = Class.forName("com.sun.beans.introspect.ClassInfo");
279+
Field cacheF = classInfoC.getDeclaredField("CACHE");
280+
try {
281+
cacheF.setAccessible(true);
282+
} catch (RuntimeException e) { // TODO Java 9+ InaccessibleObjectException
283+
/*
284+
* Not running with "--add-opens java.desktop/com.sun.beans.introspect=ALL-UNNAMED".
285+
* Until core adds this to its --add-opens configuration, and until that core
286+
* change is widely adopted, avoid unnecessary log spam and return early.
287+
*/
288+
if (LOGGER.isLoggable(Level.FINER)) {
289+
LOGGER.log(Level.FINER, "Failed to clean up " + clazz.getName() + " from ClassInfo#CACHE. A metaspace leak may have occurred.", e);
290+
}
291+
return;
292+
}
293+
Object cache = cacheF.get(null);
294+
Class<?> cacheC = Class.forName("com.sun.beans.util.Cache");
295+
if (LOGGER.isLoggable(Level.FINER)) {
296+
LOGGER.log(Level.FINER, "Cleaning up " + clazz.getName() + " from ClassInfo#CACHE.");
297+
}
298+
Method removeM = cacheC.getMethod("remove", Object.class);
299+
removeM.invoke(cache, clazz);
300+
} catch (ReflectiveOperationException e) {
301+
/*
302+
* Should never happen, but if it does, ensure the failure is isolated to this
303+
* method and does not prevent other cleanup logic from executing.
304+
*/
305+
LOGGER.log(Level.WARNING, "Failed to clean up " + clazz.getName() + " from ClassInfo#CACHE. A metaspace leak may have occurred.", e);
306+
}
307+
}
308+
}
309+
310+
270311
private static void cleanUpGlobalClassSet(@NonNull Class<?> clazz) throws Exception {
271312
Class<?> classInfoC = Class.forName("org.codehaus.groovy.reflection.ClassInfo"); // or just ClassInfo.class, but unclear whether this will always be there
272313
Field globalClassSetF = classInfoC.getDeclaredField("globalClassSet");
@@ -314,15 +355,18 @@ private static void cleanUpObjectStreamClassCaches(@NonNull Class<?> clazz) thro
314355
for (String cacheFName : new String[] {"localDescs", "reflectors"}) {
315356
Field cacheF = cachesC.getDeclaredField(cacheFName);
316357
cacheF.setAccessible(true);
317-
ConcurrentMap<Reference<Class<?>>, ?> cache = (ConcurrentMap) cacheF.get(null);
318-
Iterator<? extends Map.Entry<Reference<Class<?>>, ?>> iterator = cache.entrySet().iterator();
319-
while (iterator.hasNext()) {
320-
if (iterator.next().getKey().get() == clazz) {
321-
iterator.remove();
322-
if (LOGGER.isLoggable(Level.FINER)) {
323-
LOGGER.log(Level.FINER, "cleaning up {0} from ObjectStreamClass.Caches.{1}", new Object[] {clazz.getName(), cacheFName});
358+
Object cache = cacheF.get(null);
359+
if (cache instanceof ConcurrentMap) {
360+
// Prior to JDK-8277072
361+
Iterator<? extends Map.Entry<Reference<Class<?>>, ?>> iterator = ((ConcurrentMap) cache).entrySet().iterator();
362+
while (iterator.hasNext()) {
363+
if (iterator.next().getKey().get() == clazz) {
364+
iterator.remove();
365+
if (LOGGER.isLoggable(Level.FINER)) {
366+
LOGGER.log(Level.FINER, "cleaning up {0} from ObjectStreamClass.Caches.{1}", new Object[]{clazz.getName(), cacheFName});
367+
}
368+
break;
324369
}
325-
break;
326370
}
327371
}
328372
}

0 commit comments

Comments
 (0)