Skip to content

fix(runtime): prevent scripted item corruption, truncated-string leaks, and lost pixel writes (7/10) - #18

Open
shlifedev wants to merge 1 commit into
mainfrom
fix/stability-audit
Open

shlifedev wants to merge 1 commit into
mainfrom
fix/stability-audit

Conversation

@shlifedev

Copy link
Copy Markdown
Owner

Summary

Fixes three independently reproduced defects found during a parallel stability review of the shared game engine and native runtime:

  • Item transfers can operate on an unrelated item after a drop script changes TCitem, producing duplicated items and negative quantities. Instance-level drop vetoes were also queued instead of executing before the transfer.
  • ReadString can append uninitialized heap bytes after reading a truncated payload, and allocates the entire declared length before receiving any payload.
  • CPU pixel writes to the RGB10A2 framebuffer are silently discarded. Writes now reach the GPU without quantizing untouched ten-bit texels.

Changes are shared by Stranded II and Extended. No edition resources, saved settings, reflection cadence, grass density, view distance, or release configuration are changed. Existing local renderer work was preserved and excluded.

Review coverage

Area Reviewed Outcome
Rendering correctness Framebuffer locks and pixel formats, C++/WGSL layouts, draw order, mesh versions Reproduced and fixed discarded RGB10A2 pixel writes
Performance and resource lifetime Adjacent batching, arena exhaustion, terrain/culling caches, texture lifetimes No additional confirmed severe defect; existing batching/cache work belongs to #4
Gameplay and scripting Inventory transfers, definition/instance callbacks, QOL transfer rules Reproduced and fixed item redirection/quantity corruption and deferred instance vetoes
Save and data integrity Binary string fields, save staging/backups/queue and slot selection Reproduced and fixed uninitialized string tails and declared-size preallocation; device-loss handling remains in #15
Compiler and native ABI 32-bit numeric/handle boundaries, wrapper generation, object lifetime, bank/string operations No additional confirmed severe defect; original reference-output comparison passes
Timing and edition isolation Full-frame elapsed time, cap/fractional accumulation, per-edition modules and QOL defaults No new confirmed severe defect; both edition profiles pass isolated logic checks

This is a targeted review, not proof that every gameplay or platform path is defect-free. Already merged B3D/audio/Replace fixes and open #12/#15 were not duplicated.

Reproduction and changes

1. Scripted item transfers

exchange_item() runs scripts which may create, delete, consume, move, or select items, then previously reused the global TCitem pointer unchanged.

In an actual native Blitz fixture, transferring a stack of five type-1 items whose drop callback creates one type-2 item left the original stack in place, changed the new item's quantity to -4, and created five type-2 items in storage.

  • Preserve the source ID/type/parent and resolve it again after callbacks.
  • Cancel if the source was deleted, replaced, or moved; recheck remaining quantity and both capacities when a callback ran.
  • Execute instance-level drop tasks through parse_sel() before applying the transfer, preserving veto return codes and the definition callback order.
  • Ordinary transfers without callbacks retain one capacity scan.
  • Add eight full-engine scenarios using real parser commands and live entities, plus the helper dependencies required by the existing QOL fixture.

2. Truncated length-prefixed strings

With a declared length of 16 and only ABC in the payload, the original native ReadString returned a 16-byte string. With allocator scribbling enabled, its last 13 bytes were all 0xAA, proving that unreceived heap contents entered the result.

  • Read in fixed-size chunks and append only the bytes actually received.
  • Preserve the 32-bit length prefix, embedded NUL/non-ASCII bytes, and the position of subsequent fields for valid data.
  • Stop on short reads/errors; do not add a retry loop after a short read or impose a new maximum valid string length.
  • Add a 32-case production-runtime fixture and run it from Windows/macOS native ABI checks.

This does not make every malformed map/save valid or safe to load; it fixes this runtime string-reading defect.

3. RGB10A2 framebuffer writes

uploadData() previously returned immediately for wide-format canvases, so WritePixel, WritePixelFast, and CopyPixel edits never reached the framebuffer.

  • Retain native packed texels while locked and convert only explicitly edited texels.
  • Upload on the final unlock after edits; skip uploads for read-only locks.
  • Preserve untouched ten-bit values, nested locks, pending clears and two-bit alpha.

The same new Metal fixture fails four assertions against the original runtime and passes after the fix. This is a public framebuffer API compatibility defect. The game's potential dot-particle callers are currently disabled; ordinary rain/snow or campaign breakage was not established.

Validation

Completed on macOS ARM64:

  • Actual dev.py build --platform macos and dev.py check --platform macos --suite full passed: 204 full-game checks, actual Metal rendering/readback and timestamp profiling, isolated save/bundle preservation, and 613 bytes matching the original Blitz3D 1.118 ABI reference.
  • All 32 binary-stream assertions passed through the native console runtime, including debug on/off, truncated headers/payloads, read errors, embedded NULs, multi-chunk data and INT_MAX declarations with tiny/empty payloads.
  • The modified production stream source and test also passed ASan/UBSan; pre-existing linked runtime libraries were not sanitizer-instrumented.
  • All 7 new actual-GPU framebuffer assertions passed, including native bit-for-bit preservation of untouched texels. The original runtime failed four of those assertions.
  • Both edition profiles passed 503 QOL logic assertions each, compiled for the macOS console runtime with explicit entity/script/file-service test doubles. These checks do not establish native settings atomicity, rendering, or Windows execution.
  • A separate full-engine fixture selecting the Extended entrypoint also passed 204 checks on actual Metal, including the eight new transfer scenarios. It used shared prepared base-game assets and a disposable profile; this is not official macOS Extended support or a Windows Extended build.
  • English/Korean documentation metadata, hashes, navigation, local links, Python syntax and git diff --check passed. The user-maintained Korean README remains untouched.

Not completed:

  • Windows builds/checks for either edition, D3D12 rendering, and Windows input/focus behavior. CI only covers the base game; Extended CI remains disabled.
  • Web build/browser execution. The existing web self-blit test could not start its compile because the Emscripten SDK is absent locally.
  • Foreground FPS, interactive IME/Alt-Tab, and a full campaign playthrough. Synthetic-time game checks and GPU pixel comparisons do not establish these results.

Score rationale

7/10. Actual runtime fixtures reproduce inventory corruption and uninitialized string data, and the GPU regression has a failing original-runtime control. The fixes preserve normal binary/numeric behavior and avoid speculative optimization. Platform and full-playthrough coverage remain incomplete, and the framebuffer issue has not been shown to affect ordinary base-game play.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T08:36:29.824872Z 36fadde PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
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.

1 participant