Ghidra is a software reverse engineering (SRE) framework
GP-6862 Fixed potential native memory growth during compression/decompression of data when Java Heap is large.
emteereb205bc142887
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/zlib/ZLIB.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/zlib/ZLIB.javaindex 0f33a57538..ff7b6d04b0 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/zlib/ZLIB.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/zlib/ZLIB.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -155,23 +155,27 @@ public class ZLIB {Deflater deflater = new Deflater( 0, noWrap );- deflater.setInput( decompressedBytes, offset, decompressedBytes.length - offset );-- deflater.finish();-- if ( deflater.needsInput() ) {- System.out.println( "needs input??" );- }-- int nDeflated = deflater.deflate( tempBuffer );-- if ( nDeflated == 0 ) {- break;+ try {+ deflater.setInput( decompressedBytes, offset, decompressedBytes.length - offset );++ deflater.finish();++ if ( deflater.needsInput() ) {+ System.out.println( "needs input??" );+ }++ int nDeflated = deflater.deflate( tempBuffer );++ if ( nDeflated == 0 ) {+ break;+ }++ compressedBOS.write( tempBuffer, 0, nDeflated );++ offset += deflater.getTotalIn();+ } finally {+ deflater.end();}-- compressedBOS.write( tempBuffer, 0, nDeflated );-- offset += deflater.getTotalIn();}return compressedBOS;diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteInputBlockStreamHandle.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteInputBlockStreamHandle.javaindex 6c347417b2..c8e532428c 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteInputBlockStreamHandle.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteInputBlockStreamHandle.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -56,19 +56,28 @@ public class RemoteInputBlockStreamHandle extends RemoteBlockStreamHandle<InputBprivate final Socket socket;private final InputStream in;-+ private final Inflater inf;+private int blocksRemaining = getBlockCount();ClientInputBlockStream(Socket socket) throws IOException {this.socket = socket;- in = compressed ? new InflaterInputStream(socket.getInputStream())- : socket.getInputStream();+ if (compressed) {+ inf = new Inflater();+ in = new InflaterInputStream(socket.getInputStream(), inf);+ } else {+ in = socket.getInputStream();+ inf = null;+ }}@Overridepublic void close() throws IOException {in.close();socket.close();+ if (inf != null) {+ inf.end(); // get rid of any native memory rather than waiting for GC+ }}@Override@@ -153,24 +162,33 @@ public class RemoteInputBlockStreamHandle extends RemoteBlockStreamHandle<InputBprivate void copyBlockData(InputBlockStream inputBlockStream, OutputStream out)throws IOException {- if (compressed) {- out = new DeflaterOutputStream(out, new Deflater(Deflater.BEST_SPEED));- }-- int blocksRemaining = getBlockCount();-- BufferFileBlock block;- while ((block = inputBlockStream.readBlock()) != null) {- if (blocksRemaining == 0) {- throw new IOException("unexpected data in stream");+ Deflater def = null;++ try {+ if (compressed) {+ def = new Deflater(Deflater.BEST_SPEED);+ out = new DeflaterOutputStream(out, def);+ }++ int blocksRemaining = getBlockCount();++ BufferFileBlock block;+ while ((block = inputBlockStream.readBlock()) != null) {+ if (blocksRemaining == 0) {+ throw new IOException("unexpected data in stream");+ }+ out.write(block.toBytes());+ --blocksRemaining;+ }++ // done with compressed stream, force compressed data to flush+ if (out instanceof DeflaterOutputStream) {+ ((DeflaterOutputStream) out).finish();+ }+ } finally {+ if (def != null) {+ def.end();}- out.write(block.toBytes());- --blocksRemaining;- }-- // done with compressed stream, force compressed data to flush- if (out instanceof DeflaterOutputStream) {- ((DeflaterOutputStream) out).finish();}}diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteOutputBlockStreamHandle.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteOutputBlockStreamHandle.javaindex 99a0032ee6..c64834e48a 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteOutputBlockStreamHandle.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/stream/RemoteOutputBlockStreamHandle.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -55,15 +55,19 @@ public class RemoteOutputBlockStreamHandle extends RemoteBlockStreamHandle<Outpuprivate final Socket socket;private final OutputStream out;+ private final Deflater def;private int blocksRemaining = getBlockCount();ClientOutputBlockStream(Socket socket) throws IOException {this.socket = socket;- out = compressed- ? new DeflaterOutputStream(socket.getOutputStream(),- new Deflater(Deflater.BEST_SPEED))- : socket.getOutputStream();+ if (compressed) {+ def = new Deflater(Deflater.BEST_SPEED);+ out = new DeflaterOutputStream(socket.getOutputStream(), def);+ } else {+ def = null;+ out = socket.getOutputStream();+ }}@Override@@ -80,6 +84,9 @@ public class RemoteOutputBlockStreamHandle extends RemoteBlockStreamHandle<Outpupublic void close() throws IOException {out.close();socket.close();+ if (def != null) {+ def.end(); // get rid of any native memory rather than waiting for GC+ }}@Override@@ -151,30 +158,40 @@ public class RemoteOutputBlockStreamHandle extends RemoteBlockStreamHandle<Outpuprivate void copyBlockData(OutputBlockStream outputBlockStream, InputStream in)throws IOException, EOFException {- if (compressed) {- in = new InflaterInputStream(in);- }-- int blocksRemaining = getBlockCount();-- byte[] bytes = new byte[getBlockSize() + 4]; // include space for index- while (blocksRemaining > 0) {- int total = 0;- while (total < bytes.length) {- int readlen = in.read(bytes, total, bytes.length - total);- if (readlen < 0) {- throw new EOFException("unexpected end of stream");+ Inflater inf = null;+ try {+ if (compressed) {+ inf = new Inflater(); // not necessary, but doing it this way+ // allows end to be called, which frees native memory+ // before GC is called+ in = new InflaterInputStream(in, inf);+ }++ int blocksRemaining = getBlockCount();++ byte[] bytes = new byte[getBlockSize() + 4]; // include space for index+ while (blocksRemaining > 0) {+ int total = 0;+ while (total < bytes.length) {+ int readlen = in.read(bytes, total, bytes.length - total);+ if (readlen < 0) {+ throw new EOFException("unexpected end of stream");+ }+ total += readlen;}- total += readlen;++ BufferFileBlock block = new BufferFileBlock(bytes);+ outputBlockStream.writeBlock(block);+ --blocksRemaining;+ }+ if (compressed && in.read() != -1) {+ // failed to properly exhaust compressed stream+ throw new IOException("expected end of compressed stream");+ }+ } finally {+ if (inf != null) {+ inf.end(); // get rid of any native memory rather than waiting for GC}-- BufferFileBlock block = new BufferFileBlock(bytes);- outputBlockStream.writeBlock(block);- --blocksRemaining;- }- if (compressed && in.read() != -1) {- // failed to properly exhaust compressed stream- throw new IOException("expected end of compressed stream");}}}diff --git a/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.java b/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.javaindex 6bba19ef4f..a12b825af6 100644--- a/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.java+++ b/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -331,22 +331,26 @@ public class DataBuffer implements Buffer, Externalizable {private static int deflateData(byte[] data, byte[] compressedData) {Deflater deflate = new Deflater(Deflater.BEST_COMPRESSION, true);- deflate.setStrategy(Deflater.HUFFMAN_ONLY);- deflate.setInput(data, 0, data.length);- deflate.finish();-- int compressedDataOffset = 0;-- while (!deflate.finished() && compressedDataOffset < compressedData.length) {- compressedDataOffset += deflate.deflate(compressedData, compressedDataOffset,- compressedData.length - compressedDataOffset, Deflater.SYNC_FLUSH);- }-- if (!deflate.finished()) {- return -1;+ try {+ deflate.setStrategy(Deflater.HUFFMAN_ONLY);+ deflate.setInput(data, 0, data.length);+ deflate.finish();++ int compressedDataOffset = 0;++ while (!deflate.finished() && compressedDataOffset < compressedData.length) {+ compressedDataOffset += deflate.deflate(compressedData, compressedDataOffset,+ compressedData.length - compressedDataOffset, Deflater.SYNC_FLUSH);+ }++ if (!deflate.finished()) {+ return -1;+ }++ return compressedDataOffset;+ } finally {+ deflate.end(); // get rid of any native memory rather than waiting for GC}-- return compressedDataOffset;}@Override@@ -430,6 +434,9 @@ public class DataBuffer implements Buffer, Externalizable {catch (DataFormatException e) {throw new IOException("DataBuffer inflation failed", e);}+ finally {+ inflater.end(); // get rid of any native memory rather than waiting for GC+ }}}