[BUG]: Potential signed int64 overflow prior to bounds check in make_tma_descriptor.h
Is this a duplicate?
- [x] I confirmed there appear to be no duplicate issues for this bug and that I agree to the Code of Conduct
Type of Bug
Silent Failure
Component
libcu++
Describe the bug
In libcudacxx/include/cuda/__tma/make_tma_descriptor.h, tensor extent multiplication can overflow int64_t prior to reaching the bounds check that verifies stride_bytes < 2^40. Signed integer overflow is undefined behavior in C++, and under compiler optimization, signed overflow UB can cause the subsequent boundary checks to be miscompiled or eliminated.
Specifically, in __get_tensor_strides():
-
In the
__input_strides == nullptr(DLPack < 1.2) branch (lines 417-425):for (int __i = 0; __i < __rank - 1; ++__i) { // TODO(fbusato): check mul overflow __cumulative_size *= __tensor_sizes[__i]; const auto __stride_bytes = ::cuda::__driver::__cutensormap_size_bytes(__cumulative_size, __data_type); ... if (__stride_bytes >= __max_allowed_stride_bytes) // 2^40 check ... }Each individual tensor size is validated to be within
[1, 2^31]by__get_tensor_sizes(). However, if multiple dimensions pass individual checks (e.g. rank 4 or 5),__cumulative_sizecan exceedINT64_MAXbefore reaching the__max_allowed_stride_bytescheck. -
In the user-provided strides branch (lines 440-445):
for (int __i = __rank - 2; __i >= 0; --__i) { const auto __next_stride = (__i == __rank - 2) ? int64_t{1} : __input_strides[__i + 1]; // TODO(fbusato): check mul overflow if (__input_strides[__i] != 0 && (__input_strides[__i] < __input_sizes[__i + 1] * __next_stride))Both
__input_sizes[__i + 1]and__next_strideare user-supplied metadata. Their product can overflowint64_tbefore the comparison, which is undefined behavior and can result in invalid strides bypassing validation.
Both locations already carry // TODO(fbusato): check mul overflow.
How to Reproduce
Passing a multi-dimensional tensor (e.g., rank 5) where individual dimension sizes are within allowed per-dimension bounds (<= 2^31), but the product of cumulative sizes exceeds INT64_MAX triggers signed integer overflow during validation.
Expected behavior
Extents multiplication should be overflow-checked. If the product exceeds int64_t maximum, _CCCL_THROW(::std::invalid_argument, ...) should be raised cleanly rather than triggering signed overflow UB.
Proposed Solution
Add overflow checks prior to multiplication:
--- a/libcudacxx/include/cuda/__tma/make_tma_descriptor.h
+++ b/libcudacxx/include/cuda/__tma/make_tma_descriptor.h
@@ -414,8 +414,14 @@ inline __tma_strides_array_t __get_tensor_strides(
# else // ^^^ _CCCL_DLPACK_AT_LEAST(1, 2) ^^^ / vvv _CCCL_DLPACK_BELOW(1, 2) vvv
for (int __i = 0; __i < __rank - 1; ++__i)
{
- // TODO(fbusato): check mul overflow
- __cumulative_size *= __tensor_sizes[__i];
+ int64_t __next_cumulative = 0;
+#if defined(__GNUC__) || defined(__clang__)
+ if (__builtin_mul_overflow(__cumulative_size, __tensor_sizes[__i], &__next_cumulative))
+#else
+ if (__tensor_sizes[__i] > 0 && __cumulative_size > ::cuda::std::numeric_limits<int64_t>::max() / __tensor_sizes[__i])
+#endif
+ {
+ _CCCL_THROW(::std::invalid_argument, "Cumulative tensor size exceeds int64 maximum representable value");
+ }
+ __cumulative_size = __next_cumulative;
const auto __stride_bytes = ::cuda::__driver::__cutensormap_size_bytes(__cumulative_size, __data_type);
if (__stride_bytes % __alignment != 0)
{
@@ -438,8 +444,15 @@ inline __tma_strides_array_t __get_tensor_strides(
for (int __i = __rank - 2; __i >= 0; --__i)
{
const auto __next_stride = (__i == __rank - 2) ? int64_t{1} : __input_strides[__i + 1];
- // TODO(fbusato): check mul overflow
- if (__input_strides[__i] != 0 && (__input_strides[__i] < __input_sizes[__i + 1] * __next_stride))
+ int64_t __min_required_stride = 0;
+#if defined(__GNUC__) || defined(__clang__)
+ if (__builtin_mul_overflow(__input_sizes[__i + 1], __next_stride, &__min_required_stride))
+#else
+ if (__next_stride > 0 && __input_sizes[__i + 1] > ::cuda::std::numeric_limits<int64_t>::max() / __next_stride)
+#endif
+ {
+ _CCCL_THROW(::std::invalid_argument, "Input stride product exceeds int64 maximum representable value");
+ }
+ if (__input_strides[__i] != 0 && (__input_strides[__i] < __min_required_stride))
{
_CCCL_THROW(::std::invalid_argument,
"Stride must be 0 or greater than or equal to the product of the next stride and the size of the "
Hi @3453-315h!
Thanks for submitting this issue - the CCCL team has been notified and we'll get back to you as soon as we can! In the mean time, feel free to add any relevant information to this issue.
@3453-315h thanks for reporting the issue. It is an edge case but it is worth to address it
only a pleasure enjoy your day ahead =]