Skip to content

runtime(cached_pso): guard null pointers/spans and reject empty or overlong keys - #6211

Open
Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:fix/cached-pso-null-and-key-bounds
Open

Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:fix/cached-pso-null-and-key-bounds

Conversation

@Persudes

Copy link
Copy Markdown

Summary

Hardens runtime/src/cached_pso.h (fnv1a, check, KeyBuilder) against null data pointers and key length truncation:

  1. Key length bounds in check():
    • Reject an empty key (key.empty()) with Check::invalid so an uninitialized cache_key_ never matches an all-zero key payload.
    • Return Check::other_description when key.size() > key_bytes (64) instead of silently truncating key via padded.resize(key_bytes, '\0') (which caused any overlong key sharing a 64-byte prefix to return Check::accepted).
  2. Null-pointer guards (fnv1a, check, KeyBuilder):
    • Guard fnv1a(const std::uint8_t* data, std::size_t size) against !data when size > 0.
    • Guard check() against !blob.data() when a std::span is constructed with nullptr and blob_bytes size.
    • Guard KeyBuilder::tag(const char* name) against name == nullptr (avoiding std::strlen(nullptr)) and KeyBuilder::bytes(const void* p, std::size_t n) against p == nullptr && n > 0.

Hardware / Environment

  • OS: Windows 11 x64 (10.0.26100)
  • CPU / GPU: AMD Radeon RX 6900 XT (gfx1030, native wave64 subgroup 64), AMD driver 26.11.2 / 2.0.353 (32.0.21045.29003), Vulkan SDK 1.4.363.1
  • Toolchain: MSVC 19.52 (VS2026 Build Tools + Ninja), Python 3.15.2

Verification

  • Red-first (before runtime/src/cached_pso.h edits):
    • ctest --test-dir runtime/build -R "^d3d12_compute$" --output-on-failure -V -> ***Failed at line 427: leonida::cached_pso::check(test_id, valid_key + "b", encoded) == leonida::cached_pso::Check::other_description
  • Green (after runtime/src/cached_pso.h edits):
    • ctest --test-dir runtime/build -R "^d3d12_compute$" --output-on-failure -V -> 1/1 Test #128: d3d12_compute Passed on device: AMD Radeon RX 6900 XT (cached PSO: 2 host adapter(s) gave the same 11 answers)
    • python -I -B tests/test_ci_registration.py -> Ran 8 tests OK
    • python -I -B tests/test_text_write_line_endings.py -> Ran 3 tests OK
    • python -I -B scripts/check_no_game_data.py -> exit 0
    • git diff --check -> exit 0
    • git merge-tree --write-tree upstream/main HEAD -> exit 0

AI-assisted (Gemini, Antigravity); no copyrighted game files or dumps used.

@SirKingg SirKingg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent AI-assisted review. The current pipeline call sites produce 64-character SHA-256 keys and supply non-null data to KeyBuilder::bytes, so the synthetic collision cases below are not shown to be reachable from valid current PSO descriptions. They do leave the proposed malformed-input hardening incomplete; the new null-span test also has undefined behavior.

Windows/MSVC 19.44, Debug with /bigobj, RTX 4070 Ti, driver 32.0.16.1088: ctest --test-dir <build> -C Debug -R "^d3d12_(compute|draw)$" --output-on-failure -V passed 2/2; the compute test matched 11 cached-PSO responses on two host adapters. A separate synthetic MSVC probe reproduced the key and null-input collisions. The default Debug build first failed with C1128 before adding /bigobj. No game data used; no other GPUs or release build tested.

Comment thread runtime/src/cached_pso.h
if (get32(p + 8) != identity.vendor || get32(p + 12) != identity.device) return Check::other_adapter;
if (get32(p + 16) != identity.driver || std::memcmp(p + 20, identity.cache_uuid, 16) != 0)
return Check::other_driver;
if (key.size() > key_bytes) return Check::other_description;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks the lookup key: encode(id, std::string(64, 'a') + "b") still truncates at resize, and check(id, std::string(64, 'a'), that_blob) returns accepted. Short keys also collide through zero padding (e.g. "a" and std::string("a\0", 2)). Please validate the encoding boundary too, or enforce the documented 64-character SHA-256 key invariant on both paths, and test the reverse direction. Current pipeline callers produce 64-character hex strings, so this is a malformed-key API issue rather than a demonstrated collision for valid PSOs.

Comment thread runtime/src/cached_pso.h
KeyBuilder& bytes(const void* p, std::size_t n) {
put32(data_, std::uint32_t(n));
if (n) data_.insert(data_.end(), static_cast<const std::uint8_t*>(p), static_cast<const std::uint8_t*>(p) + n);
const std::size_t count = p ? n : 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes(nullptr, 16) now serializes exactly the same bytes as bytes(nullptr, 0), silently turning malformed data into an empty field instead of propagating an error. Similarly, fnv1a(nullptr, 16) returns the valid empty-input hash. This can make distinct descriptions indistinguishable if a caller supplies a missing nonempty field; can these APIs signal invalid input and have their callers propagate it rather than producing a usable key? Existing production bytes call sites currently pass non-null pointers.

leonida::cached_pso::Check::invalid);
EXPECT(leonida::cached_pso::check(
test_id, valid_key,
std::span<const std::uint8_t>(static_cast<const std::uint8_t*>(nullptr),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructing a nonempty std::span from nullptr violates the constructor's valid-range precondition, so this test has undefined behavior before check is called; the !blob.data() guard cannot safely validate that case. MSVC here happened to return invalid even with STL hardening, but that does not make the construction valid. If null plus nonzero size needs to be rejected, validate a raw pointer and size before constructing the span and test that boundary instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants