Add safety checks for edge cases and memory handling#1004
Open
jeongseok-meta wants to merge 1 commit intomainfrom
Open
Add safety checks for edge cases and memory handling#1004jeongseok-meta wants to merge 1 commit intomainfrom
jeongseok-meta wants to merge 1 commit intomainfrom
Conversation
Summary: **WHY:** The momentum/pymomentum libraries contain edge cases where improper inputs or unusual runtime conditions could cause crashes, undefined behavior, or memory leaks. These include division by zero in SIMD collision functions, out-of-bounds array access in character collision mapping, memory leaks in C API exception paths, and use-after-free in Python bindings. **WHAT:** Adds comprehensive safety checks across 8 files in momentum and pymomentum libraries to handle edge cases gracefully with proper error messages and fallback behaviors. | Issue | Fix | |-------|-----| | Division by zero (collision solver) | Safe distance clamping with `kMinSafeDistance` | | Division by zero (rasterizer) | Select fallback to infinity for invalid recipW | | Out-of-bounds access (character) | `MT_THROW_IF` bounds validation | | Memory leak (C API) | Pre-allocate buffer + cleanup on exception | | Integer overflow (Python arrays) | Size validation before `pybind11::array_t` creation | | Use-after-free (PyBytesStreamBuffer) | Store `bytes_` member to preserve lifetime | **HOW:** - `c_api.cpp`: Pre-allocate output buffer; add `delete[]` in catch blocks - `character.cpp`: Add 3 `MT_THROW_IF` checks for collision parent bounds - `simd_collision_error_function.cpp`: Add `kMinSafeDistance`/`kMaxInverseDistance` constants; use `drjit::maximum/minimum` for safe division - `legacy_json_io.cpp`: Add empty-array checks before `size() - 1` operations - `rasterizer.cpp`: Add `validRecipW` check; use `drjit::select` for safe fallback - `momentum_io.cpp`: Add `MT_THROW_IF` for size overflow; use `static_cast<pybind11::ssize_t>` - `python_utility.cpp`: Add null check for `PyList_GetItem`; store `bytes_` copy - `python_utility.h`: Add `bytes_` member to `PyBytesStreamBuffer` Differential Revision: D91828286
Contributor
|
@jeongseok-meta has exported this pull request. If you are a Meta employee, you can view the originating Diff in D91828286. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
WHY:
The momentum/pymomentum libraries contain edge cases where improper inputs or unusual runtime conditions could cause crashes, undefined behavior, or memory leaks. These include division by zero in SIMD collision functions, out-of-bounds array access in character collision mapping, memory leaks in C API exception paths, and use-after-free in Python bindings.
WHAT:
Adds comprehensive safety checks across 8 files in momentum and pymomentum libraries to handle edge cases gracefully with proper error messages and fallback behaviors.
kMinSafeDistanceMT_THROW_IFbounds validationpybind11::array_tcreationbytes_member to preserve lifetimeHOW:
c_api.cpp: Pre-allocate output buffer; adddelete[]in catch blockscharacter.cpp: Add 3MT_THROW_IFchecks for collision parent boundssimd_collision_error_function.cpp: AddkMinSafeDistance/kMaxInverseDistanceconstants; usedrjit::maximum/minimumfor safe divisionlegacy_json_io.cpp: Add empty-array checks beforesize() - 1operationsrasterizer.cpp: AddvalidRecipWcheck; usedrjit::selectfor safe fallbackmomentum_io.cpp: AddMT_THROW_IFfor size overflow; usestatic_cast<pybind11::ssize_t>python_utility.cpp: Add null check forPyList_GetItem; storebytes_copypython_utility.h: Addbytes_member toPyBytesStreamBufferDifferential Revision: D91828286