|
15 | 15 | */ |
16 | 16 | package io.netty.handler.codec.compression; |
17 | 17 |
|
18 | | -import com.github.luben.zstd.ZstdIOException; |
19 | 18 | import com.github.luben.zstd.ZstdInputStreamNoFinalizer; |
20 | 19 | import io.netty.buffer.ByteBuf; |
21 | 20 | import io.netty.channel.ChannelHandlerContext; |
@@ -42,9 +41,20 @@ public final class ZstdDecoder extends ByteToMessageDecoder { |
42 | 41 | } |
43 | 42 |
|
44 | 43 | 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; |
46 | 55 | private final int maximumAllocationSize; |
47 | 56 | private final int maxForwardBytes; |
| 57 | + private final int maxWindowLog; |
48 | 58 | private final MutableByteBufInputStream inputStream = new MutableByteBufInputStream(); |
49 | 59 | private ZstdInputStreamNoFinalizer zstdIs; |
50 | 60 |
|
@@ -59,13 +69,44 @@ private enum State { |
59 | 69 | CORRUPTED |
60 | 70 | } |
61 | 71 |
|
| 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 | + */ |
62 | 80 | public ZstdDecoder() { |
63 | | - this(4 * 1024 * 1024); |
| 81 | + this(DEFAULT_MAXIMUM_ALLOCATION_SIZE, DEFAULT_MAX_WINDOW_LOG); |
64 | 82 | } |
65 | 83 |
|
| 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 | + */ |
66 | 93 | 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) { |
67 | 107 | this.maximumAllocationSize = ObjectUtil.checkPositiveOrZero(maximumAllocationSize, "maximumAllocationSize"); |
68 | 108 | this.maxForwardBytes = maximumAllocationSize > 0 ? maximumAllocationSize : DEFAULT_MAX_FORWARD_BYTES; |
| 109 | + this.maxWindowLog = ObjectUtil.checkInRange(maxWindowLog, MIN_WINDOW_LOG, MAX_WINDOW_LOG, "maxWindowLog"); |
69 | 110 | } |
70 | 111 |
|
71 | 112 | @Override |
@@ -146,6 +187,9 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
146 | 187 | super.handlerAdded(ctx); |
147 | 188 | zstdIs = new ZstdInputStreamNoFinalizer(inputStream); |
148 | 189 | 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); |
149 | 193 | } |
150 | 194 |
|
151 | 195 | @Override |
|