From 87f48a945c107539ff2ace92bef65a220b616812 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sat, 16 Oct 2021 12:17:12 +0100 Subject: [PATCH] Fix File/Memory leak in Annotation Processor. (#67) * Fix File/Memory leak in Annotation Processor. Mixin's AP requires the minecraft jar + deps on the AP classpath to generate a refmap. These files are then opened by the classloader. TargetMap uses Java's object Serialization to save/load itself from files. When doing so it gets placed into ObjectStreamClass.Caches, this does not get garbage collected enough to allow the Classloader to close, and thus close the file handles it has open. This commit uses Gson to read/write TargetMap to prevent this class form being added to the global JVM cache. * Manually build/read json structure --- build.gradle | 3 +- .../tools/obfuscation/TargetMap.java | 75 ++++++++++--------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/build.gradle b/build.gradle index a689a2a2..26942408 100644 --- a/build.gradle +++ b/build.gradle @@ -103,7 +103,7 @@ sourceSets { ap { compileClasspath += main.output ext.languageVersion = 8 - ext.compatibility = '1.6' + ext.compatibility = '1.8' } fernflower { compileClasspath += main.output @@ -177,6 +177,7 @@ dependencies { // Annotation Processor apImplementation "org.ow2.asm:asm-tree:$asmVersion" apImplementation guava + apImplementation gson // Fernflower decompiler fernflowerImplementation 'org.jetbrains:intellij-fernflower:1.0.0.9' diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java b/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java index 1cd2fbad..ad1ffe80 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java @@ -24,13 +24,14 @@ */ package org.spongepowered.tools.obfuscation; +import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.Reader; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -44,6 +45,7 @@ import org.spongepowered.tools.obfuscation.mirror.TypeHandle; import org.spongepowered.tools.obfuscation.mirror.TypeReference; import com.google.common.io.Files; +import com.google.gson.*; /** * Serialisable map of classes to their associated mixins, used so that we can @@ -52,6 +54,7 @@ import com.google.common.io.Files; public final class TargetMap extends HashMap> { private static final long serialVersionUID = 1L; + private static final Gson GSON = new Gson(); /** * Session ID, used to identify the temp file @@ -203,26 +206,29 @@ public final class TargetMap extends HashMap> * @param temp Set "delete on exit" for the file */ public void write(boolean temp) { - ObjectOutputStream oos = null; - FileOutputStream fout = null; - try { - File sessionFile = TargetMap.getSessionFile(this.sessionId); - if (temp) { - sessionFile.deleteOnExit(); + JsonObject jsonObject = new JsonObject(); + + for (Entry> entry : this.entrySet()) { + final JsonArray array = new JsonArray(); + + for (TypeReference reference : entry.getValue()) { + array.add(new JsonPrimitive(reference.getName())); } - fout = new FileOutputStream(sessionFile, true); - oos = new ObjectOutputStream(fout); - oos.writeObject(this); + + jsonObject.add(entry.getKey().getName(), array); + } + + String json = GSON.toJson(jsonObject); + + File sessionFile = TargetMap.getSessionFile(this.sessionId); + if (temp) { + sessionFile.deleteOnExit(); + } + + try (FileOutputStream outputStream = new FileOutputStream(sessionFile, true)) { + outputStream.write(json.getBytes(StandardCharsets.UTF_8)); } catch (Exception ex) { ex.printStackTrace(); - } finally { - if (oos != null) { - try { - oos.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } } } @@ -233,22 +239,21 @@ public final class TargetMap extends HashMap> * @return deserialised map or null if deserialisation failed */ private static TargetMap read(File sessionFile) { - ObjectInputStream objectinputstream = null; - FileInputStream streamIn = null; - try { - streamIn = new FileInputStream(sessionFile); - objectinputstream = new ObjectInputStream(streamIn); - return (TargetMap)objectinputstream.readObject(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (objectinputstream != null) { - try { - objectinputstream .close(); - } catch (IOException ex) { - ex.printStackTrace(); + try (Reader reader = new BufferedReader(new FileReader(sessionFile))) { + final JsonObject jsonObject = GSON.fromJson(reader, JsonObject.class); + final TargetMap targetMap = new TargetMap(); + + for (Entry entry : jsonObject.entrySet()) { + JsonArray array = entry.getValue().getAsJsonArray(); + + for (JsonElement element : array) { + targetMap.addMixin(entry.getKey(), element.getAsString()); } - } + } + + return targetMap; + } catch (Exception ex) { + ex.printStackTrace(); } return null; }