[clang] [clang][bytecode] Fix crash on __builtin_infer_alloc_token with struct argument (PR #178936)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 30 10:30:43 PST 2026


https://github.com/mugiwaraluffy56 created https://github.com/llvm/llvm-project/pull/178936

## Summary
- Fix crash when passing non primitive types (structs) to `__builtin_infer_alloc_token`
- The bytecode interpreter's discard loop dereferenced an empty `OptPrimType` for non primitive arguments

## Test plan
- Added regression test in `clang/test/SemaCXX/alloc-token.cpp`
- Existing tests continue to pass

Fixes #178892

>From e35c3452f39c19855ede8937bd1bbd115e1e71d9 Mon Sep 17 00:00:00 2001
From: mugiwaraluffy56 <myakampuneeth at gmail.com>
Date: Fri, 30 Jan 2026 23:59:32 +0530
Subject: [PATCH] [clang][bytecode] Fix crash on __builtin_infer_alloc_token
 with non-primitive arguments

The discard loop assumed all arguments would have primitive types,
but struct/class arguments are passed as pointers on the stack.
Check if the type is classifiable before discarding.

Fixes #178892
---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp | 8 ++++++--
 clang/test/SemaCXX/alloc-token.cpp       | 5 +++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index fb7c51608f85b..3e783953d3e64 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1398,8 +1398,12 @@ static bool interp__builtin_infer_alloc_token(InterpState &S, CodePtr OpPC,
       MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
 
   // We do not read any of the arguments; discard them.
-  for (int I = Call->getNumArgs() - 1; I >= 0; --I)
-    discard(S.Stk, *S.getContext().classify(Call->getArg(I)));
+  for (int I = Call->getNumArgs() - 1; I >= 0; --I) {
+    if (std::optional<PrimType> T = S.getContext().classify(Call->getArg(I)))
+      discard(S.Stk, *T);
+    else
+      S.Stk.discard<Pointer>(); // Non-primitive types are passed as pointers.
+  }
 
   // Note: Type inference from a surrounding cast is not supported in
   // constexpr evaluation.
diff --git a/clang/test/SemaCXX/alloc-token.cpp b/clang/test/SemaCXX/alloc-token.cpp
index 2a11e3366d5fb..aae25720d4329 100644
--- a/clang/test/SemaCXX/alloc-token.cpp
+++ b/clang/test/SemaCXX/alloc-token.cpp
@@ -79,4 +79,9 @@ void negative_tests() {
   negative_template_test<void>(); // expected-note {{in instantiation of function template specialization 'negative_template_test<void>' requested here}}
   constexpr auto inference_fail = __builtin_infer_alloc_token(123); // expected-error {{must be initialized by a constant expression}} \
                                                                     // expected-note {{could not infer allocation type for __builtin_infer_alloc_token}}
+
+  // PR178892: Ensure struct arguments don't crash the bytecode interpreter.
+  struct S {};
+  constexpr auto struct_arg = __builtin_infer_alloc_token(S()); // expected-error {{must be initialized by a constant expression}} \
+                                                                // expected-note {{could not infer allocation type for __builtin_infer_alloc_token}}
 }



More information about the cfe-commits mailing list