mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:30:53 +01:00
[PR #1935] perf(bcpack): precompute rANS advance tables and guard stream widths, codecs, and sizes (RT-BCPACK-STREAM-PERF-AND-GUARDS) #1941
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#1941
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/1935
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open
Base:
main← Head:fix/rt-bcpack-stream-perf-and-guards📝 Commits (1)
1dc85d9perf(bcpack): precompute rANS advance tables and guard stream widths, codecs, and sizes (RT-BCPACK-STREAM-PERF-AND-GUARDS)📊 Changes
3 files changed (+194 additions, -28 deletions)
View changed files
📝
runtime/CMakeLists.txt(+17 -0)📝
runtime/src/bcpack_stream.h(+48 -28)➕
runtime/test/bcpack_stream_test.cpp(+129 -0)📄 Description
Item ID:
RT-BCPACK-STREAM-PERF-AND-GUARDS(claim: #1608, comment 7167621176)Problem
In the shared BCPack rANS block stream (
runtime/src/bcpack_stream.h, used byruntime/src/bcpack.cppfor DirectStorage texture decompression andtools/bcpack_history_train.cpp):Lane::stepline 109):model = &model_for(width - (byte >> 6), byte & 15)passedw = width - (byte >> 6)directly intomodels[w - 1]without checking1 <= w <= 8. Whenwidth <= (byte >> 6)orwidth > 8,models[w - 1]reads out of bounds (verified under UBSan on unpatchedmain: unsigned offset overflow instd::array::operator[]).visit_blocks():Direct callers of
visit_blocks()with an unsupported codec (0,2,>= 8) indexedwidths[codec]out of bounds or read zero widths; unalignedsizefor 16-byte block codecs (3, 5, 6, 7) silently dropped the trailing half-block while returningtrue.Model::advance()andLane::step():Every decoded rANS symbol recomputed
bit_width(sym), chasedfrequency[p]andbase[p], calculatedbase[p] + f * (sym - (1 << (p-1))), and branched on!c(despite there being at most 256 symbols per model).Model::symbol()also checkedp < widthon every bucket step, andLane::step()checkedPhase::headerandPhase::lengthahead of the >99%-takenPhase::symbolsbranch.Fix
runtime/src/bcpack_stream.h:Model::Step { uint16_t freq, cdf; }table inModel::init()(1 KiB per model, 128 KiB total across the 128 static models) soModel::advance()becomes a single table lookup, multiply, and subtract.base[w + 1] = 65536uinModel::init()soModel::symbol()fast-path loop drops thep < unsigned(width)bound check.Phase::symbolsfirst with[[likely]]inLane::step().model_width = width - (byte >> 6)in[1, 8],model != nullptr, and0 <= symbol < (1 << model->width)inLane::step().codecin{1, 3, 4, 5, 6, 7}and block-alignedsize(% 8for BC1/BC4,% 16for BC3/BC5/BC6H/BC7) invisit_blocks().runtime/test/bcpack_stream_test.cpp&runtime/CMakeLists.txt:bcpack_streamverifying exhaustive bit-exactness ofsymbol(fast)vssymbol(slow)and tableadvance()vs scalaradvance()across all 128 models (128 * 65536slots + all symbols), negative/out-of-range header width rejection, invalid codec & unaligned size rejection, and 1,200 iterations of deterministic adversarial fuzzing across all 6 codecs.Benchmark (macOS clang++
-O2, 8,000 iterations across all 6 synthetic Python-encoded codecs = 39.06 MiB decoded per run, bit-exact withtools/bcpack.py)main)Verification
clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined runtime/test/bcpack_stream_test.cpp runtime/src/bcpack.cpp-> PASSclang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread runtime/test/bcpack_stream_test.cpp runtime/src/bcpack.cpp-> PASSclang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/bcpack_stream_test.cpp runtime/src/bcpack.cpp(x3) -> PASSclang++ -std=c++20 -fsyntax-only tools/bcpack_history_train.cpp-> PASS-O2benchmark against synthetic vectors generated byruntime/test/dstorage_vectors.py(all 6 codecs) -> PASS (+20–21% faster)bcpack_stream_test.cppagainst unpatchedmain:runtime/src/bcpack_stream.hunder ASan+UBSan -> FAILED with UBSan OOB pointer overflow atstd::array::operator[]+ line 68 failure as expectedpython3 -I scripts/check_no_game_data.py runtime/src/bcpack_stream.h runtime/test/bcpack_stream_test.cpp runtime/CMakeLists.txt-> PASSpython3 -I tests/test_text_write_line_endings.py-> PASS (3/3)🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.