Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix segfault and invalid cast in UntypedStorage#186207

Open
vishalgoyal316 wants to merge 1 commit into
pytorch:mainfrom
vishalgoyal316:fix_169210
Open

Fix segfault and invalid cast in UntypedStorage#186207
vishalgoyal316 wants to merge 1 commit into
pytorch:mainfrom
vishalgoyal316:fix_169210

Conversation

@vishalgoyal316
Copy link
Copy Markdown
Collaborator

Summary

Fixes two error-checking gaps reported in #169210 where invalid usage of UntypedStorage caused a segfault rather than a clear Python error.

Fix 1 — Segfault on invalid allocator= argument (c10/, torch/csrc/)

Passing tensor.data_ptr() as allocator= to UntypedStorage() caused a segfault because the integer was blindly cast to c10::Allocator* and allocator->allocate() was called on it. Adds c10::isKnownAllocator() which validates the pointer against PyTorch's registered allocators (CPU default, all device slots via allocator_array, CUDA caching allocator) before use. THPStorage_pynew now calls TORCH_CHECK(isKnownAllocator(allocator)) and raises RuntimeError with an actionable message instead of crashing.

Fix 2 — Silent invalid cast on quantized storage (torch/storage.py)

TypedStorage._to() silently attempted dtype conversion for quantized storage types (qint8, qint32, quint8, quint4x2, quint2x4), which is semantically invalid because quantized values require scale and zero_point
to be meaningful. Adds _raise_if_quantized_storage_cast() guard in TypedStorage._to() which raises RuntimeError directing users to dequantize first.

Also fixes two pre-existing clang-tidy warnings in Storage.cpp (unmatched NOLINTEND and PyGetSetDef designated initializer warnings) that were surfaced when the file was touched.

Test plan

New tests in test/test_torch.py:

  • test_untyped_storage_invalid_allocator_raises — verifies RuntimeError is raised when data_ptr() is passed as allocator=
  • test_quantized_storage_dtype_cast_raises — verifies all 12 dtype cast methods raise RuntimeError across all 5 quantized storage types
python test/test_torch.py TestTorch.test_quantized_storage_dtype_cast_raises
python test/test_torch.py TestTorch.test_untyped_storage_invalid_allocator_raises

Co-authored-with: AI assistant (Cursor/Claude Sonnet)

Fixes: #169210

Passing tensor.data_ptr() as allocator= to UntypedStorage() caused a
segfault because the integer was cast to c10::Allocator* and used to
call allocate(). Fix: add c10::isKnownAllocator() which validates the
pointer against PyTorch's registered allocators before use, raising
RuntimeError instead of crashing.

Also guard TypedStorage._to() against quantized dtype casts (qint8,
qint32, quint8, etc.) which are semantically invalid without scale and
zero_point; users should dequantize first. Added test to verify both changes.
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Jun 4, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/186207

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 50 Pending

As of commit 3828d2c with merge base 35e0ad6 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Jun 4, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation Fault when Converting QInt32Storage to bfloat16

2 participants