GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Auto-port 5.0: Add maxWindowLog parameter to ZstdDecoder to bound mem… · netty/netty@61813fa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-compression/src
    • main/java/io/netty/handler/codec/compression
      • ZstdDecoder.java
    • test/java/io/netty/handler/codec/compression
      • ZstdDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecoder.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecoder.java
+47-3Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.netty.handler.codec.compression;
1717

18-
import com.github.luben.zstd.ZstdIOException;
1918
import com.github.luben.zstd.ZstdInputStreamNoFinalizer;
2019
import io.netty.buffer.ByteBuf;
2120
import io.netty.channel.ChannelHandlerContext;
@@ -42,9 +41,20 @@ public final class ZstdDecoder extends ByteToMessageDecoder {
4241
}
4342

4443
private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES;
45-
44+
/**
45+
* Default maximum size of a single output buffer, in bytes (4 MiB).
46+
*/
47+
public static final int DEFAULT_MAXIMUM_ALLOCATION_SIZE = 4 * 1024 * 1024;
48+
/**
49+
* Default upper bound on the {@code Window_Log} accepted by the decoder.
50+
* {@code 27} corresponds to a 128 MiB decompression window.
51+
*/
52+
public static final int DEFAULT_MAX_WINDOW_LOG = 27;
53+
private static final int MIN_WINDOW_LOG = 10;
54+
private static final int MAX_WINDOW_LOG = 31;
4655
private final int maximumAllocationSize;
4756
private final int maxForwardBytes;
57+
private final int maxWindowLog;
4858
private final MutableByteBufInputStream inputStream = new MutableByteBufInputStream();
4959
private ZstdInputStreamNoFinalizer zstdIs;
5060

@@ -59,13 +69,44 @@ private enum State {
5969
CORRUPTED
6070
}
6171

72+
/**
73+
* Creates a new decoder with the {@link #DEFAULT_MAXIMUM_ALLOCATION_SIZE},
74+
* and the {@link #DEFAULT_MAX_WINDOW_LOG} window log size.
75+
* <p>
76+
* The window log size bounds the memory usage of the sliding window for ZSTD frame decompression.
77+
* Frames declaring a larger window will be rejected to bound the memory the decoder may allocate per stream.
78+
*
79+
*/
6280
public ZstdDecoder() {
63-
this(4 * 1024 * 1024);
81+
this(DEFAULT_MAXIMUM_ALLOCATION_SIZE, DEFAULT_MAX_WINDOW_LOG);
6482
}
6583

84+
/**
85+
* Creates a new decoder with the given maximum allocation size,
86+
* and the {@link #DEFAULT_MAX_WINDOW_LOG} window log size.
87+
* <p>
88+
* The window log size bounds the memory usage of the sliding window for ZSTD frame decompression.
89+
* Frames declaring a larger window will be rejected to bound the memory the decoder may allocate per stream.
90+
*
91+
* @param maximumAllocationSize maximum size of a single output buffer.
92+
*/
6693
public ZstdDecoder(int maximumAllocationSize) {
94+
this(maximumAllocationSize, DEFAULT_MAX_WINDOW_LOG);
95+
}
96+
97+
/**
98+
* Creates a new decoder with an explicit upper bound on the accepted {@code Window_Log}.
99+
*
100+
* @param maximumAllocationSize maximum size of a single output buffer.
101+
* @param maxWindowLog upper bound on the {@code Window_Log} field of incoming
102+
* frames; must be in {@code [10, 31]}. Frames declaring a
103+
* larger window will be rejected to bound the memory the
104+
* decoder may allocate per stream.
105+
*/
106+
public ZstdDecoder(int maximumAllocationSize, int maxWindowLog) {
67107
this.maximumAllocationSize = ObjectUtil.checkPositiveOrZero(maximumAllocationSize, "maximumAllocationSize");
68108
this.maxForwardBytes = maximumAllocationSize > 0 ? maximumAllocationSize : DEFAULT_MAX_FORWARD_BYTES;
109+
this.maxWindowLog = ObjectUtil.checkInRange(maxWindowLog, MIN_WINDOW_LOG, MAX_WINDOW_LOG, "maxWindowLog");
69110
}
70111

71112
@Override
@@ -146,6 +187,9 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
146187
super.handlerAdded(ctx);
147188
zstdIs = new ZstdInputStreamNoFinalizer(inputStream);
148189
zstdIs.setContinuous(true);
190+
// Bound the decompression window to mitigate memory amplification from frames that
191+
// declare an oversized Window_Size.
192+
zstdIs.setLongMax(maxWindowLog);
149193
}
150194

151195
@Override
Collapse file

‎codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdDecoderTest.java
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@
1616
package io.netty.handler.codec.compression;
1717

1818
import com.github.luben.zstd.Zstd;
19+
import com.github.luben.zstd.ZstdCompressCtx;
20+
import io.netty.buffer.ByteBuf;
21+
import io.netty.buffer.Unpooled;
1922
import io.netty.channel.embedded.EmbeddedChannel;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.util.Random;
26+
27+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
2030

2131

2232
public class ZstdDecoderTest extends AbstractDecoderTest {
@@ -33,4 +43,69 @@ public EmbeddedChannel createChannel() {
3343
protected byte[] compress(byte[] data) throws Exception {
3444
return Zstd.compress(data);
3545
}
46+
47+
@Test
48+
public void testFrameWithWindowLogAboveCapIsRejected() throws Exception {
49+
// Incompressible random data so libzstd actually has to use the declared window
50+
// (highly compressible content lets libzstd shrink the effective window to the
51+
// content size, making setLongMax ineffective for the test).
52+
byte[] payload = new byte[256 * 1024];
53+
new Random(12345L).nextBytes(payload);
54+
55+
// Compressed with windowLog = 21 (2 MiB window).
56+
byte[] compressed = compressWithWindowLog(payload, 21);
57+
58+
// Decoder caps Window_Log at 15 (32 KiB) -> the frame must be rejected.
59+
EmbeddedChannel ch = new EmbeddedChannel(new ZstdDecoder(4 * 1024 * 1024, 15));
60+
try {
61+
assertThrows(DecompressionException.class,
62+
() -> ch.writeInbound(Unpooled.wrappedBuffer(compressed)));
63+
} finally {
64+
ch.finishAndReleaseAll();
65+
}
66+
}
67+
68+
@Test
69+
public void testFrameWithWindowLogWithinCapIsAccepted() throws Exception {
70+
byte[] payload = new byte[256 * 1024];
71+
new Random(12345L).nextBytes(payload);
72+
73+
byte[] compressed = compressWithWindowLog(payload, 18); // 256 KiB window
74+
75+
EmbeddedChannel ch = new EmbeddedChannel(new ZstdDecoder(4 * 1024 * 1024, 20));
76+
try {
77+
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(compressed)));
78+
79+
ByteBuf acc = Unpooled.buffer();
80+
try {
81+
ByteBuf b;
82+
while ((b = ch.readInbound()) != null) {
83+
try {
84+
acc.writeBytes(b);
85+
} finally {
86+
b.release();
87+
}
88+
}
89+
byte[] actual = new byte[acc.readableBytes()];
90+
acc.readBytes(actual);
91+
assertArrayEquals(payload, actual);
92+
} finally {
93+
acc.release();
94+
}
95+
} finally {
96+
ch.finishAndReleaseAll();
97+
}
98+
}
99+
100+
private static byte[] compressWithWindowLog(byte[] data, int windowLog) {
101+
try (ZstdCompressCtx ctx = new ZstdCompressCtx()) {
102+
ctx.setLevel(Zstd.defaultCompressionLevel());
103+
ctx.setWindowLog(windowLog);
104+
byte[] dst = new byte[(int) Zstd.compressBound(data.length)];
105+
int written = ctx.compressByteArray(dst, 0, dst.length, data, 0, data.length);
106+
byte[] out = new byte[written];
107+
System.arraycopy(dst, 0, out, 0, written);
108+
return out;
109+
}
110+
}
36111
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.