pytorch - 💡(How to fix) Fix CPU SEGV crash in `torch.Tensor.to_dense()` for malformed torch.sparse_coo_tensor [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#182662Fetched 2026-05-07 03:30:44
View on GitHub
Comments
2
Participants
3
Timeline
24
Reactions
0
Author
Timeline (top)
mentioned ×8subscribed ×8labeled ×5commented ×2

Calling torch.Tensor.to_dense() on CPU with a synthetically constructed malformed sparse_coo_tensor can trigger an AddressSanitizer SEGV (segmentation fault) with illegal memory read access in the sparse-to-dense conversion kernel. The bug bypasses upper-layer parameter validation when check_invariants=False is set, leading to a direct memory safety violation and process abort.

Error Message

==2583004==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x7fffdab6ffdf bp 0x7fffffffcfc0 sp 0x7fffffffce20 T0) ==2583004==The signal is caused by a READ memory access. ==2583004==Hint: this fault was caused by a dereference of a high value address. #0 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)::operator()(long, long) const /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:613:20 #1 0x7fffdab6ffdf in void at::parallel_for<void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)>(long, long, long, void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long) const&) /root/pytorch/aten/src/ATen/Parallel-inl.h:29:5 #2 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:607:3 #3 0x7fffdab5c673 in at::native::add_out_dense_sparse_cpu(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Scalar const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:754:7 #4 0x7fffdab5c673 in at::native::add_out_sparse_cpu(at::Tensor const&, at::Tensor const&, c10::Scalar const&, at::Tensor&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:558:12 #5 0x7fffda6a52c6 in at::native::sparse_to_dense(at::Tensor const&, std::optionalc10::ScalarType, std::optional<bool>) /root/pytorch/aten/src/ATen/native/TensorConversions.cpp:697:14 #6 0x7ffff1e10fde in at::Tensor::to_dense(std::optionalc10::ScalarType, std::optional<bool>) const /root/pytorch/build/aten/src/ATen/core/TensorBody.h:4060:12 #7 0x5555556dfc16 (/usr/bin/python3.10+0x18bc16) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #8 0x5555556c9966 in _PyEval_EvalFrameDefault (/usr/bin/python3.10+0x175966) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #9 0x5555557ae565 (/usr/bin/python3.10+0x25a565) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #10 0x5555557ae435 in PyEval_EvalCode (/usr/bin/python3.10+0x25a435) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #11 0x5555557d4ed7 (/usr/bin/python3.10+0x280ed7) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #12 0x5555557cf6de (/usr/bin/python3.10+0x27b6de) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #13 0x5555557d4c74 (/usr/bin/python3.10+0x280c74) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #14 0x5555557d4257 in _PyRun_SimpleFileObject (/usr/bin/python3.10+0x280257) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #15 0x5555557d3f36 in _PyRun_AnyFileObject (/usr/bin/python3.10+0x27ff36) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #16 0x5555557c83ad in Py_RunMain (/usr/bin/python3.10+0x2743ad) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #17 0x5555557a247c in Py_BytesMain (/usr/bin/python3.10+0x24e47c) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad) #18 0x7ffff7106d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16 #19 0x7ffff7106e3f in __libc_start_main csu/../csu/libc-start.c:392:3 #20 0x5555557a2374 in _start (/usr/bin/python3.10+0x24e374) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)

AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: SEGV /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:613:20 in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)::operator()(long, long) const ==2583004==ABORTING

Root Cause

==2583004==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x7fffdab6ffdf bp 0x7fffffffcfc0 sp 0x7fffffffce20 T0)
==2583004==The signal is caused by a READ memory access.
==2583004==Hint: this fault was caused by a dereference of a high value address.
    #0 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)::operator()(long, long) const /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:613:20
    #1 0x7fffdab6ffdf in void at::parallel_for<void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)>(long, long, long, void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long) const&) /root/pytorch/aten/src/ATen/Parallel-inl.h:29:5
    #2 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:607:3
    #3 0x7fffdab5c673 in at::native::add_out_dense_sparse_cpu(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Scalar const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:754:7
    #4 0x7fffdab5c673 in at::native::add_out_sparse_cpu(at::Tensor const&, at::Tensor const&, c10::Scalar const&, at::Tensor&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:558:12
    #5 0x7fffda6a52c6 in at::native::sparse_to_dense(at::Tensor const&, std::optional<c10::ScalarType>, std::optional<bool>) /root/pytorch/aten/src/ATen/native/TensorConversions.cpp:697:14
    #6 0x7ffff1e10fde in at::Tensor::to_dense(std::optional<c10::ScalarType>, std::optional<bool>) const /root/pytorch/build/aten/src/ATen/core/TensorBody.h:4060:12
    #7 0x5555556dfc16  (/usr/bin/python3.10+0x18bc16) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #8 0x5555556c9966 in _PyEval_EvalFrameDefault (/usr/bin/python3.10+0x175966) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #9 0x5555557ae565  (/usr/bin/python3.10+0x25a565) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #10 0x5555557ae435 in PyEval_EvalCode (/usr/bin/python3.10+0x25a435) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #11 0x5555557d4ed7  (/usr/bin/python3.10+0x280ed7) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #12 0x5555557cf6de  (/usr/bin/python3.10+0x27b6de) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #13 0x5555557d4c74  (/usr/bin/python3.10+0x280c74) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #14 0x5555557d4257 in _PyRun_SimpleFileObject (/usr/bin/python3.10+0x280257) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #15 0x5555557d3f36 in _PyRun_AnyFileObject (/usr/bin/python3.10+0x27ff36) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #16 0x5555557c83ad in Py_RunMain (/usr/bin/python3.10+0x2743ad) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #17 0x5555557a247c in Py_BytesMain (/usr/bin/python3.10+0x24e47c) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #18 0x7ffff7106d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #19 0x7ffff7106e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #20 0x5555557a2374 in _start (/usr/bin/python3.10+0x24e374) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 48 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 128 On-line CPU(s) list: 0-127 Vendor ID: AuthenticAMD Model name: AMD Ryzen Threadripper PRO 5995WX 64-Cores CPU family: 25 Model: 8 Thread(s) per core: 2 Core(s) per socket: 64 Socket(s): 1 Stepping: 2 Frequency boost: enabled CPU max MHz: 2700.0000 CPU min MHz: 1800.0000 BogoMIPS: 5389.77 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca Virtualization: AMD-V L1d cache: 2 MiB (64 instances) L1i cache: 2 MiB (64 instances) L2 cache: 32 MiB (64 instances) L3 cache: 256 MiB (8 instances) NUMA node(s): 1 NUMA node0 CPU(s): 0-127 Vulnerability Gather data sampling: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

Code Example

#!/usr/bin/env python3
import torch

print("torch version:", torch.__version__)

# Malformed parameters: negative out-of-bounds index with valid tensor size
# Index value is far outside the legal range [0, size[0]-1]
indices = torch.tensor([[-4702111234474983746]], dtype=torch.int64)
values = torch.tensor([-16.0], dtype=torch.float32)
size = (1,)

print(f"[*] Indices shape: {indices.shape}")
print(f"[*] Values shape: {values.shape}")
print(f"[*] Tensor size: {size}")

# Create sparse tensor with invariant checks disabled
t = torch.sparse_coo_tensor(
    indices=indices,
    values=values,
    size=size,
    dtype=torch.float32,
    device='cpu',
    check_invariants=False
)

# Trigger crash
print("[*] Triggering to_dense()...")
result = t.to_dense()
print("Success:", result.shape)

---

==2583004==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x7fffdab6ffdf bp 0x7fffffffcfc0 sp 0x7fffffffce20 T0)
==2583004==The signal is caused by a READ memory access.
==2583004==Hint: this fault was caused by a dereference of a high value address.
    #0 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)::operator()(long, long) const /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:613:20
    #1 0x7fffdab6ffdf in void at::parallel_for<void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)>(long, long, long, void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long) const&) /root/pytorch/aten/src/ATen/Parallel-inl.h:29:5
    #2 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:607:3
    #3 0x7fffdab5c673 in at::native::add_out_dense_sparse_cpu(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Scalar const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:754:7
    #4 0x7fffdab5c673 in at::native::add_out_sparse_cpu(at::Tensor const&, at::Tensor const&, c10::Scalar const&, at::Tensor&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:558:12
    #5 0x7fffda6a52c6 in at::native::sparse_to_dense(at::Tensor const&, std::optional<c10::ScalarType>, std::optional<bool>) /root/pytorch/aten/src/ATen/native/TensorConversions.cpp:697:14
    #6 0x7ffff1e10fde in at::Tensor::to_dense(std::optional<c10::ScalarType>, std::optional<bool>) const /root/pytorch/build/aten/src/ATen/core/TensorBody.h:4060:12
    #7 0x5555556dfc16  (/usr/bin/python3.10+0x18bc16) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #8 0x5555556c9966 in _PyEval_EvalFrameDefault (/usr/bin/python3.10+0x175966) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #9 0x5555557ae565  (/usr/bin/python3.10+0x25a565) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #10 0x5555557ae435 in PyEval_EvalCode (/usr/bin/python3.10+0x25a435) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #11 0x5555557d4ed7  (/usr/bin/python3.10+0x280ed7) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #12 0x5555557cf6de  (/usr/bin/python3.10+0x27b6de) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #13 0x5555557d4c74  (/usr/bin/python3.10+0x280c74) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #14 0x5555557d4257 in _PyRun_SimpleFileObject (/usr/bin/python3.10+0x280257) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #15 0x5555557d3f36 in _PyRun_AnyFileObject (/usr/bin/python3.10+0x27ff36) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #16 0x5555557c83ad in Py_RunMain (/usr/bin/python3.10+0x2743ad) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #17 0x5555557a247c in Py_BytesMain (/usr/bin/python3.10+0x24e47c) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #18 0x7ffff7106d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #19 0x7ffff7106e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #20 0x5555557a2374 in _start (/usr/bin/python3.10+0x24e374) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:613:20 in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)::operator()(long, long) const
==2583004==ABORTING

---

PyTorch version: 2.10.0a0+gitf2bb22f
Is debug build: False
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 4.1.2
Libc version: glibc-2.35

Python version: 3.10.12 (main, Aug 15 2025, 14:32:43) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-200-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: 
Nvidia driver version: Could not collect
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False
Caching allocator config: N/A

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      48 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             128
On-line CPU(s) list:                0-127
Vendor ID:                          AuthenticAMD
Model name:                         AMD Ryzen Threadripper PRO 5995WX 64-Cores
CPU family:                         25
Model:                              8
Thread(s) per core:                 2
Core(s) per socket:                 64
Socket(s):                          1
Stepping:                           2
Frequency boost:                    enabled
CPU max MHz:                        2700.0000
CPU min MHz:                        1800.0000
BogoMIPS:                           5389.77
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca
Virtualization:                     AMD-V
L1d cache:                          2 MiB (64 instances)
L1i cache:                          2 MiB (64 instances)
L2 cache:                           32 MiB (64 instances)
L3 cache:                           256 MiB (8 instances)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-127
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==2.2.6
[pip3] optree==0.17.0
[pip3] torch==2.10.0a0+gitf2bb22f
[conda] Could not collect
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Summary

Calling torch.Tensor.to_dense() on CPU with a synthetically constructed malformed sparse_coo_tensor can trigger an AddressSanitizer SEGV (segmentation fault) with illegal memory read access in the sparse-to-dense conversion kernel. The bug bypasses upper-layer parameter validation when check_invariants=False is set, leading to a direct memory safety violation and process abort.

Poc

Run following command:

poc.sparse_coo_tensor.py

#!/usr/bin/env python3
import torch

print("torch version:", torch.__version__)

# Malformed parameters: negative out-of-bounds index with valid tensor size
# Index value is far outside the legal range [0, size[0]-1]
indices = torch.tensor([[-4702111234474983746]], dtype=torch.int64)
values = torch.tensor([-16.0], dtype=torch.float32)
size = (1,)

print(f"[*] Indices shape: {indices.shape}")
print(f"[*] Values shape: {values.shape}")
print(f"[*] Tensor size: {size}")

# Create sparse tensor with invariant checks disabled
t = torch.sparse_coo_tensor(
    indices=indices,
    values=values,
    size=size,
    dtype=torch.float32,
    device='cpu',
    check_invariants=False
)

# Trigger crash
print("[*] Triggering to_dense()...")
result = t.to_dense()
print("Success:", result.shape)

ASAN-report

==2583004==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x7fffdab6ffdf bp 0x7fffffffcfc0 sp 0x7fffffffce20 T0)
==2583004==The signal is caused by a READ memory access.
==2583004==Hint: this fault was caused by a dereference of a high value address.
    #0 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)::operator()(long, long) const /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:613:20
    #1 0x7fffdab6ffdf in void at::parallel_for<void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)>(long, long, long, void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long) const&) /root/pytorch/aten/src/ATen/Parallel-inl.h:29:5
    #2 0x7fffdab6ffdf in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:607:3
    #3 0x7fffdab5c673 in at::native::add_out_dense_sparse_cpu(at::Tensor&, at::Tensor const&, at::Tensor const&, c10::Scalar const&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:754:7
    #4 0x7fffdab5c673 in at::native::add_out_sparse_cpu(at::Tensor const&, at::Tensor const&, c10::Scalar const&, at::Tensor&) /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:558:12
    #5 0x7fffda6a52c6 in at::native::sparse_to_dense(at::Tensor const&, std::optional<c10::ScalarType>, std::optional<bool>) /root/pytorch/aten/src/ATen/native/TensorConversions.cpp:697:14
    #6 0x7ffff1e10fde in at::Tensor::to_dense(std::optional<c10::ScalarType>, std::optional<bool>) const /root/pytorch/build/aten/src/ATen/core/TensorBody.h:4060:12
    #7 0x5555556dfc16  (/usr/bin/python3.10+0x18bc16) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #8 0x5555556c9966 in _PyEval_EvalFrameDefault (/usr/bin/python3.10+0x175966) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #9 0x5555557ae565  (/usr/bin/python3.10+0x25a565) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #10 0x5555557ae435 in PyEval_EvalCode (/usr/bin/python3.10+0x25a435) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #11 0x5555557d4ed7  (/usr/bin/python3.10+0x280ed7) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #12 0x5555557cf6de  (/usr/bin/python3.10+0x27b6de) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #13 0x5555557d4c74  (/usr/bin/python3.10+0x280c74) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #14 0x5555557d4257 in _PyRun_SimpleFileObject (/usr/bin/python3.10+0x280257) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #15 0x5555557d3f36 in _PyRun_AnyFileObject (/usr/bin/python3.10+0x27ff36) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #16 0x5555557c83ad in Py_RunMain (/usr/bin/python3.10+0x2743ad) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #17 0x5555557a247c in Py_BytesMain (/usr/bin/python3.10+0x24e47c) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)
    #18 0x7ffff7106d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #19 0x7ffff7106e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #20 0x5555557a2374 in _start (/usr/bin/python3.10+0x24e374) (BuildId: b2fd9010dc75aa747aee5296c31a07d210d124ad)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /root/pytorch/aten/src/ATen/native/sparse/SparseTensorMath.cpp:613:20 in void at::native::add_dense_sparse_worker_non_hybrid_cpu<float>(at::Tensor&, c10::Scalar const&, at::Tensor const&, at::Tensor const&, at::Tensor const&)::'lambda'(long, long)::operator()(long, long) const
==2583004==ABORTING

Suspected root cause

From the observed behavior and the ASan backtraces, the failure appears to be related to how the sparse-to-dense conversion kernel handles malformed index data from sparse_coo_tensor. In the PoC, the sparse tensor is intentionally synthetic: although the tensor size is valid (1,), the indices tensor contains a negative out-of-bounds value -4702111234474983746, which is far outside the legal index range [0, 0] for this dimension.

The check_invariants=False parameter skips the upper-layer index boundary validation when creating the sparse tensor, but the underlying add_dense_sparse_worker_non_hybrid_cpu kernel (core implementation of to_dense()) does not add a second layer of defensive boundary checks. The kernel directly uses the raw index values to calculate memory offsets, leading to an illegal memory read access when the index is out of bounds. This ultimately results in a SEGV crash and process abort, which can be exploited to cause a denial-of-service (DoS) in untrusted input scenarios.

Versions

Environment

PyTorch version: 2.10.0a0+gitf2bb22f
Is debug build: False
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 4.1.2
Libc version: glibc-2.35

Python version: 3.10.12 (main, Aug 15 2025, 14:32:43) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-200-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: 
Nvidia driver version: Could not collect
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False
Caching allocator config: N/A

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      48 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             128
On-line CPU(s) list:                0-127
Vendor ID:                          AuthenticAMD
Model name:                         AMD Ryzen Threadripper PRO 5995WX 64-Cores
CPU family:                         25
Model:                              8
Thread(s) per core:                 2
Core(s) per socket:                 64
Socket(s):                          1
Stepping:                           2
Frequency boost:                    enabled
CPU max MHz:                        2700.0000
CPU min MHz:                        1800.0000
BogoMIPS:                           5389.77
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca
Virtualization:                     AMD-V
L1d cache:                          2 MiB (64 instances)
L1i cache:                          2 MiB (64 instances)
L2 cache:                           32 MiB (64 instances)
L3 cache:                           256 MiB (8 instances)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-127
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==2.2.6
[pip3] optree==0.17.0
[pip3] torch==2.10.0a0+gitf2bb22f
[conda] Could not collect

cc @nikitaved @pearu @cpuhrsch @amjames @bhosmer @jcaip

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING