From 930eaba2d535414005cec7783fa122ea7647a929 Mon Sep 17 00:00:00 2001 From: jrm Date: Tue, 17 Nov 2015 15:34:57 -0800 Subject: [PATCH] model Inflater and Deflater as resources Summary: public The classes `java.util.zip.{Inflater/Deflater}` where not modelled as resources. In practice, bad memory leak can happen using these classes and forcing the call to `end()` can help to avoid waisting native memory. Reviewed By: sblackshear Differential Revision: D2661249 fb-gh-sync-id: 1e33316 --- .../java/src/java/util/zip/Deflater.java | 32 +++++++++++++++++++ .../java/src/java/util/zip/Inflater.java | 28 ++++++++++++++++ infer/tests/ant_report.json | 10 ++++++ infer/tests/buck_report.json | 10 ++++++ .../java/infer/ResourceLeaks.java | 20 ++++++++++++ .../java/infer/ResourceLeaksTest.java | 2 ++ 6 files changed, 102 insertions(+) create mode 100644 infer/models/java/src/java/util/zip/Deflater.java create mode 100644 infer/models/java/src/java/util/zip/Inflater.java diff --git a/infer/models/java/src/java/util/zip/Deflater.java b/infer/models/java/src/java/util/zip/Deflater.java new file mode 100644 index 000000000..77acbeb89 --- /dev/null +++ b/infer/models/java/src/java/util/zip/Deflater.java @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2013 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package java.util.zip; + +import com.facebook.infer.models.InferBuiltins; + +public class Deflater { + + public Deflater() { + InferBuiltins.__set_file_attribute(this); + } + + public Deflater(int level) { + this(); + } + + public Deflater(int level, boolean nowrap) { + this(); + } + + public void end() { + InferBuiltins.__set_mem_attribute(this); + } + +} diff --git a/infer/models/java/src/java/util/zip/Inflater.java b/infer/models/java/src/java/util/zip/Inflater.java new file mode 100644 index 000000000..f070892a4 --- /dev/null +++ b/infer/models/java/src/java/util/zip/Inflater.java @@ -0,0 +1,28 @@ +/* +* Copyright (c) 2013 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package java.util.zip; + +import com.facebook.infer.models.InferBuiltins; + +public class Inflater { + + public Inflater() { + InferBuiltins.__set_file_attribute(this); + } + + public Inflater(boolean nowrap) { + this(); + } + + public void end() { + InferBuiltins.__set_mem_attribute(this); + } + +} diff --git a/infer/tests/ant_report.json b/infer/tests/ant_report.json index 29581b79e..0fdb87867 100644 --- a/infer/tests/ant_report.json +++ b/infer/tests/ant_report.json @@ -229,6 +229,11 @@ "file": "codetoanalyze/java/infer/FilterInputStreamLeaks.java", "bug_type": "RESOURCE_LEAK" }, + { + "procedure": "void ResourceLeaks.deflaterLeak()", + "file": "codetoanalyze/java/infer/ResourceLeaks.java", + "bug_type": "RESOURCE_LEAK" + }, { "procedure": "void FilterOutputStreamLeaks.deflaterOutputStreamNotClosedAfterWrite()", "file": "codetoanalyze/java/infer/FilterOutputStreamLeaks.java", @@ -364,6 +369,11 @@ "file": "codetoanalyze/java/infer/FilterInputStreamLeaks.java", "bug_type": "RESOURCE_LEAK" }, + { + "procedure": "void ResourceLeaks.inflaterLeak()", + "file": "codetoanalyze/java/infer/ResourceLeaks.java", + "bug_type": "RESOURCE_LEAK" + }, { "procedure": "void FilterOutputStreamLeaks.inflaterOutputStreamNotClosedAfterWrite()", "file": "codetoanalyze/java/infer/FilterOutputStreamLeaks.java", diff --git a/infer/tests/buck_report.json b/infer/tests/buck_report.json index ef546f5bb..0d874db80 100644 --- a/infer/tests/buck_report.json +++ b/infer/tests/buck_report.json @@ -229,6 +229,11 @@ "file": "infer/tests/codetoanalyze/java/infer/FilterInputStreamLeaks.java", "bug_type": "RESOURCE_LEAK" }, + { + "procedure": "void ResourceLeaks.deflaterLeak()", + "file": "infer/tests/codetoanalyze/java/infer/ResourceLeaks.java", + "bug_type": "RESOURCE_LEAK" + }, { "procedure": "void FilterOutputStreamLeaks.deflaterOutputStreamNotClosedAfterWrite()", "file": "infer/tests/codetoanalyze/java/infer/FilterOutputStreamLeaks.java", @@ -364,6 +369,11 @@ "file": "infer/tests/codetoanalyze/java/infer/FilterInputStreamLeaks.java", "bug_type": "RESOURCE_LEAK" }, + { + "procedure": "void ResourceLeaks.inflaterLeak()", + "file": "infer/tests/codetoanalyze/java/infer/ResourceLeaks.java", + "bug_type": "RESOURCE_LEAK" + }, { "procedure": "void FilterOutputStreamLeaks.inflaterOutputStreamNotClosedAfterWrite()", "file": "infer/tests/codetoanalyze/java/infer/FilterOutputStreamLeaks.java", diff --git a/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java b/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java index 16ac42ab2..7aada8017 100644 --- a/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java +++ b/infer/tests/codetoanalyze/java/infer/ResourceLeaks.java @@ -42,8 +42,10 @@ import java.util.Scanner; import java.util.jar.JarFile; import java.util.jar.JarInputStream; import java.util.jar.JarOutputStream; +import java.util.zip.Deflater; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; +import java.util.zip.Inflater; import java.util.zip.ZipFile; import javax.net.ssl.HttpsURLConnection; @@ -912,4 +914,22 @@ public class ResourceLeaks { f.close(); } + public void deflaterLeak() { + Deflater comp = new Deflater(); + } + + public void deflaternoLeak() { + Deflater comp = new Deflater(); + comp.end(); + } + + public void inflaterLeak() { + Inflater decomp = new Inflater(); + } + + public void inflaterNoLeak() { + Inflater decomp = new Inflater(); + decomp.end(); + } + } diff --git a/infer/tests/endtoend/java/infer/ResourceLeaksTest.java b/infer/tests/endtoend/java/infer/ResourceLeaksTest.java index 7536df28f..f27a015a4 100644 --- a/infer/tests/endtoend/java/infer/ResourceLeaksTest.java +++ b/infer/tests/endtoend/java/infer/ResourceLeaksTest.java @@ -74,6 +74,8 @@ public class ResourceLeaksTest { "copyFileLeak", "copyFileLeak", "scannerNotClosed", + "deflaterLeak", + "inflaterLeak", }; assertThat( "Results should contain the following resource leak errors",