Validate negative offsets in wp.copy#1553
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR adds input validation to the ChangesCopy Offset Validation
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Signed-off-by: Minh Vu <vuhoangminh97@gmail.com>
316923b to
ec17029
Compare
Greptile SummaryThis PR adds input validation to
Confidence Score: 4/5Safe to merge; the change is a small, targeted defensive guard with no impact on the happy path. Both modified files are straightforward. The two new checks in context.py are placed at the right point in the call flow — after the array-type guard and before any arithmetic on raw pointers — so they correctly protect both the contiguous memcpy path and, as a side-effect, the non-contiguous kernel path. The non-contiguous path has always silently dropped positive offset values too (that gap is pre-existing and out of scope for this PR). Tests cover negative src_offset and dest_offset independently and are registered across all available devices. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["wp.copy(dest, src, dest_offset, src_offset, count)"] --> B{src and dest\nare arrays?}
B -- No --> C[RuntimeError:\nnot arrays]
B -- Yes --> D{src_offset < 0?}
D -- Yes --> E[RuntimeError:\nSource offset must be non-negative]
D -- No --> F{dest_offset < 0?}
F -- Yes --> G[RuntimeError:\nDestination offset must be non-negative]
F -- No --> H{count <= 0?}
H -- Yes --> I[count = src.size]
H -- No --> J{count == 0?}
I --> J
J -- Yes --> K[return early]
J -- No --> L{src.is_contiguous\nand dest.is_contiguous?}
L -- Yes --> M[Compute byte offsets\nsrc_ptr = src.ptr + src_offset_in_bytes\ndst_ptr = dest.ptr + dst_offset_in_bytes]
M --> N{Bounds checks\nin/out of range?}
N -- Out of range --> O[RuntimeError:\nbounds exceeded]
N -- In range --> P[memcpy via runtime core]
L -- No --> Q[Non-contiguous kernel\noffsets not applied]
|
Summary
wp.copy()now rejects negative source and destination offsets before converting them to byte offsets or adding them to raw array pointers. Previously, negative offsets could pass the upper-bound checks and produce pointers before the start of the array allocation.Validation
python build_lib.py --no-cuda --no-use-libmathdx -j 8python -m unittest warp.tests.test_copy.TestCopy.test_copy_negative_offsets_cpupython -m unittest warp.tests.test_copy.TestCopy.test_copy_adjoint_cpuuvx ruff@0.15.9 check warp/_src/context.py warp/tests/test_copy.pyuvx ruff@0.15.9 format --check warp/_src/context.py warp/tests/test_copy.pySummary by CodeRabbit