[libc-commits] [libc] [libc][GPU] Reject overflowing calloc requests (PR #238822)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 5 14:03:10 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: 0z5a (0z5a)
<details>
<summary>Changes</summary>
GPU libc `calloc` multiplies `num * size` without checking overflow. For example, on a 64-bit target `(SIZE_MAX / 2 + 2) * 2` wraps to 2, which can result in a non-null pointer to a much smaller allocation.
Check the multiplication before calling the GPU allocator and return null if it overflows. Add an integration test for a nonzero wrapped product.
Fixes #<!-- -->238819.
## Testing
- Built the GPU integration executable for `nvptx64-nvidia-cuda` / `sm_90` using an LLVM 24 development compiler and CUDA 13.2.
- On GH200, the overflow-only fix passed the new test. With the overflow check absent and the separate NVPTX allocator compatibility fix applied, the same device assertion failed as expected.
- Compiled the GPU source with a host-side mock allocator: before the check it received a 2-byte request and returned a pointer; with the check it was not called and `calloc` returned null.
- AMDGPU and B300 were not tested.
---
Full diff: https://github.com/llvm/llvm-project/pull/238822.diff
3 Files Affected:
- (modified) libc/src/stdlib/gpu/calloc.cpp (+2-2)
- (modified) libc/test/integration/src/stdlib/gpu/CMakeLists.txt (+12)
- (added) libc/test/integration/src/stdlib/gpu/calloc_overflow.cpp (+16)
``````````diff
diff --git a/libc/src/stdlib/gpu/calloc.cpp b/libc/src/stdlib/gpu/calloc.cpp
index 9150affc7c2005..984b10f5473a89 100644
--- a/libc/src/stdlib/gpu/calloc.cpp
+++ b/libc/src/stdlib/gpu/calloc.cpp
@@ -16,8 +16,8 @@
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
- size_t bytes = num * size;
- if (bytes == 0)
+ size_t bytes;
+ if (__builtin_mul_overflow(num, size, &bytes) || bytes == 0)
return nullptr;
void *ptr = gpu::allocate(bytes);
diff --git a/libc/test/integration/src/stdlib/gpu/CMakeLists.txt b/libc/test/integration/src/stdlib/gpu/CMakeLists.txt
index 1a6c900e22cb86..08225ede428785 100644
--- a/libc/test/integration/src/stdlib/gpu/CMakeLists.txt
+++ b/libc/test/integration/src/stdlib/gpu/CMakeLists.txt
@@ -1,5 +1,17 @@
add_libc_testsuite(libc-stdlib-gpu-integration-tests PARENT libc-stdlib-integration-tests)
+add_integration_test(
+ calloc_overflow
+ SUITE
+ libc-stdlib-gpu-integration-tests
+ SRCS
+ calloc_overflow.cpp
+ DEPENDS
+ libc.src.stdlib.calloc
+ LOADER_ARGS
+ --threads 1 --blocks 1
+)
+
# TODO: Test on NVPTX, requires CUDA VMEM API.
if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
add_integration_test(
diff --git a/libc/test/integration/src/stdlib/gpu/calloc_overflow.cpp b/libc/test/integration/src/stdlib/gpu/calloc_overflow.cpp
new file mode 100644
index 00000000000000..30f34269dd8867
--- /dev/null
+++ b/libc/test/integration/src/stdlib/gpu/calloc_overflow.cpp
@@ -0,0 +1,16 @@
+//===-- GPU calloc overflow test ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/calloc.h"
+#include "test/IntegrationTest/test.h"
+
+TEST_MAIN(int, char **, char **) {
+ size_t large = static_cast<size_t>(-1) / 2 + 2;
+ ASSERT_EQ(LIBC_NAMESPACE::calloc(large, 2), nullptr);
+ return 0;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/238822
More information about the libc-commits
mailing list