Skip to content

Harden worker/buffer pools, DGC0 scratch dims, and PJRT shape/HBM accounting against overflow and partial-publish leaks - #49

Merged
saariuslystoned merged 3 commits into
mainfrom
fix/state-machine-freetpu-runtime-guards
Dec 1, 2026
Merged

saariuslystoned merged 3 commits into
mainfrom
fix/state-machine-freetpu-runtime-guards

Conversation

@saariuslystoned

@saariuslystoned saariuslystoned commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes 6 integer-overflow, wraparound, HBM-accounting, and partial-publish bugs across libopenbuenos and llama-cpp-cloudtpu runtime code (merged cleanly with origin/main at d36c4d55), plus post-audit repair of all 3 review blockers and hardened FlatBuffer/pool/mock-PJRT invariants:

  1. libopenbuenos/src/ob_worker_pool.cpp & ob_internal.h: Replaced uint32_t fetch_add cursor wraparound with saturating claim_work_range, eliminated duplicated range clamping, made WorkerPool::num_threads atomic across the fast-path read and lazy startup write, and reset job_gen/task pointers on shutdown() so restarted workers cannot replay stale jobs.
  2. libopenbuenos/src/ob_buffer_pool.cpp: ob_buffer_alloc(SIZE_MAX - 100) page-alignment previously wrapped to a 0-byte slab and returned success; now returns OB_ERR_OVERFLOW.
  3. libopenbuenos/src/ob_dgc0.cpp & ob_graph.cpp:
    • Guarded DGC0 scratchpad dimension products against overflow AND enforced strict < UINT32_MAX in both parse_dgc0_buffer and upload_and_register_graph (plus ext_dive_image.size <= UINT32_MAX) so 4 GiB ({65536, 65536}) never narrows/wraps to 0 at litebuf_register_model_params.dive_scratch_size_bytes.
    • Hardened FbView::table_field_status / follow_uoffset to reject self-overlapping uoffsets (rel < 4), odd/out-of-bounds vtables, truncated field extents, and corrupt external_buffers/scratchpad tensor fields (OB_ERR_BAD_DGC0) instead of silently defaulting to 1 element.
  4. llama-cpp-cloudtpu/pjrt_runtime.cpp (shape/byte overflow & mock cleanup):
    • Fixed pjrt_dtype_nbytes to check byte-count overflow (q * bits + ceil(r * bits / 8)) instead of intermediate bit-count overflow, preserving valid large counts (2^59 F32 -> 2^61 B, SIZE_MAX S4 -> 2^63 B) while throwing std::overflow_error when host bytes exceed SIZE_MAX.
    • Made shape_num_elements validate negative dims and zero-element short-circuit order-independently (including {INT64_MAX, 3, 0} on 32-bit/64-bit) and tightened RAII ownership in mock_buffer_from_host / mock_client_compile.
  5. llama-cpp-cloudtpu/pjrt_runtime.cpp (device-id HBM accounting): Keyed mock executable counters by hardware device ID, rejected out-of-range 64-bit replica_device_ids before int narrowing, and errored on unknown assigned device IDs even for zero-input executables.
  6. llama-cpp-cloudtpu/pjrt_runtime.cpp (atomic Execute output publication): Staged outputs and completion events with rollback on missing SSA return / allocation failure.
  7. CI & Build Wiring: Wired openbuenos_pool_guards_test into libopenbuenos/CMakeLists.txt (ctest) and libopenbuenos/Makefile (make test), and wired pjrt_runtime_guards_test into llama-cpp-cloudtpu/CMakeLists.txt (ctest), so .github/workflows/ci.yml builds and runs both guard suites automatically.

Proof Block (Offline Host Verification — Head c15c8805)

  • CMake + CTest (libopenbuenos): cmake -S libopenbuenos -B build-cmake -G Ninja -DCMAKE_BUILD_TYPE=Release && cmake --build build-cmake && ctest --test-dir build-cmake --output-on-failure --verbose -> 4/4 PASSED (openbuenos_host_test 27/27, tpu_soc_probe_mock, ob_quant_unpack_test, openbuenos_pool_guards_test: ok).
  • Makefile (libopenbuenos): make -C libopenbuenos verify-header-host host test -> PASS (-Wall -Wextra -Wpedantic -Werror -fno-exceptions -fno-rtti, 27/27 + ob_quant_unpack_test + openbuenos_pool_guards_test: ok).
  • CMake + CTest (llama-cpp-cloudtpu): cmake -S llama-cpp-cloudtpu -B build-cloudtpu -G Ninja -DCMAKE_BUILD_TYPE=Release && cmake --build build-cloudtpu && ctest --test-dir build-cloudtpu --output-on-failure --verbose -> 2/2 PASSED (cloudtpu_host_tests, pjrt_runtime_guards_test: ok).
  • CMake + CTest (freetile): cmake -S freetile -B build-freetile -G Ninja -DCMAKE_BUILD_TYPE=Release && cmake --build build-freetile && ctest --test-dir build-freetile --output-on-failure -> 4/4 PASSED (freetile_unit_tests, freetile_splines_flash_attn_tests, freetile_buenos_parity_tests, freetile_twin_tests).
  • ASan + UBSan (-Werror): Both openbuenos_pool_guards_test and pjrt_runtime_guards_test built with -fsanitize=address,undefined -fno-omit-frame-pointer and PASSED (ok).
  • Hygiene & Clean-Room Boundary: git diff --check origin/main clean; clean-room boundary audit clean (zero private paths/codenames/secrets/binaries).
  • Scope/Limits: Offline host and mock PJRT/OpenBuenos verification only (zero live TPU hardware touched).

@saariuslystoned
saariuslystoned merged commit a4a74b2 into main Dec 1, 2026
2 checks passed
@saariuslystoned
saariuslystoned deleted the fix/state-machine-freetpu-runtime-guards branch December 1, 2026 18:20
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