Repository navigation
fix(parquet): return error instead of panicking on overlong VLQ integers - #12474
Conversation
`BitReader::get_vlq_int` asserted that a VLQ integer is at most `MAX_VLQ_BYTE_LEN` bytes, and in debug builds overflowed the shift before reaching the assert, so corrupt RLE run headers or DELTA_BINARY_PACKED headers in page data panicked the reader. A 10th byte with bits beyond 64 was also silently truncated. `get_vlq_int` and `get_zigzag_vlq_int` now return `Result<Option<i64>>`: `Ok(None)` still means the buffer ran out, while an overlong or overflowing value is reported as an error. The RLE and DELTA_BINARY_PACKED decoders propagate the error. A single-byte fast path keeps the common RLE run header case cheap. Also make the thrift `read_vlq` reject overlong/overflowing varints with `IntegerOverflow` instead of silently wrapping. Co-authored-by: Isaac <[email protected]>
Cover valid values up to u64::MAX, a 10th byte that overflows or carries a continuation bit (IntegerOverflow), and truncated input (EOF), for both the slice and Read-based thrift input protocols. Co-authored-by: Isaac <[email protected]>
alamb
left a comment
There was a problem hiding this comment.
Thank you @viirya -- this looks pretty good to me
Can you please run the metadata reader benchmarks to make sure this doesn't cause a slowdown? I thinkt he vlq deocding was pretty optimized by @etseidl and @jhorstmann and it would be good to avoid slowing it down
| if shift == 63 && byte > 1 { | ||
| return Err(ThriftProtocolError::IntegerOverflow); | ||
| } | ||
| in_progress |= ((byte & 0x7F) as u64) << shift; |
There was a problem hiding this comment.
I burned a lot of tokens trying to get this call both safe and fast. In the end this wound up being the least bad. Happy to post a report of all the things I've tried 😉. IIRC this wound up regressing a percent or two in a few of the benches, but not across the board.
|
run benchmark metadata |
|
🤖 Benchmark starting (GKE) | trigger Target: metadata Sharding factor: 1 (1 workers). Comparing fix-parquet-vlq-panic-2fc417 (0bd6e94) to 85b1e33 (merge-base) diff Run configurationrun benchmark metadata
shards: 1
Trigger commentResults will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark metadata baseline:
ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c |
|
🤖 Benchmark starting (GKE) | trigger Target: metadata Sharding factor: 1 (1 workers). Comparing 85b1e33 (85b1e33) to 0bd6e94 diff Run configurationrun benchmark metadata
baseline:
ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c
shards: 1
Trigger commentResults will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: metadata Comparing fix-parquet-vlq-panic-2fc417 (0bd6e94) to 85b1e33 (merge-base) diff Run configurationrun benchmark metadata
shards: 1
Trigger commentDetailsPer-runner informationmetadata — shard 1/1Node: gk3-benchmark-cluster-nap-117yldmt-0fefe1dc-sp9r Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench metadataCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: metadata Comparing 85b1e33 (85b1e33) to 0bd6e94 diff Run configurationrun benchmark metadata
baseline:
ref: 0bd6e94221cfb5b4c8da000597d0a52304d63d7e
changed:
ref: 85b1e33ea591776e0aa469009cb3d3e2f5b8525c
shards: 1
Trigger commentDetailsPer-runner informationmetadata — shard 1/1Node: gk3-benchmark-cluster-nap-1l1ev5kf-8c42c942-qm24 Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench metadataCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
Benches look like I remember...I think this is good to go from my perspective. |
|
Thanks @alamb and @etseidl! The metadata benchmarks @etseidl ran in both directions show no consistent regression: the only ≥2% slowdown in the first run ( |
|
Thanks @etseidl, and sorry I missed #12099 when filing #12473. Good to see we landed on the same |
Thanks @viirya, sounds good. I think this PR can close #12099 since the issue doesn't mention |
Which issue does this PR close?
read_vlqaccepts varints longer than 10 bytes and wraps the value #12099.Rationale for this change
Corrupt RLE run headers or DELTA_BINARY_PACKED headers in page data make
BitReader::get_vlq_intpanic: anassert!fires in release builds and a shift overflow fires in debug builds. The reader should return aParquetErrorinstead.What changes are included in this PR?
BitReader::get_vlq_intandget_zigzag_vlq_intnow returnResult<Option<i64>>instead ofOption<i64>.Ok(None)still means the buffer ran out. An encoding longer thanMAX_VLQ_BYTE_LENbytes, or a 10th byte that would overflow 64 bits, now returns an error. On error the reader is not advanced.u64with a shift of at most 63.RleDecoder::reloadandDeltaBitPackDecoderpropagate the error with?. Before this change, mapping a malformed value toNonewould have madeRleDecodertreat it as a normal end of data.read_vlqnow returnsThriftProtocolError::IntegerOverflowfor an overlong or overflowing varint instead of silently wrapping.Are these changes tested?
Yes. New unit tests cover:
BitReader: an overlong varint (10 and 11+ continuation bytes), a 10th byte that overflows,u64::MAX(the largest valid value), and truncated buffers.RleDecoder: a corrupt header inset_data, and a corrupt header after a valid run inget_batch.DeltaBitPackDecoder: a corruptblock_size, an overflowingfirst_value, and a corrupt blockmin_delta.All
cargo test -p parquettests pass.Are there any user-facing changes?
BitReaderis public only with theexperimentalfeature. For users of that feature, the return types ofBitReader::get_vlq_intandBitReader::get_zigzag_vlq_intchange fromOption<i64>toResult<Option<i64>>. Without that feature, the only visible change is that corrupt files now return an error instead of panicking.