[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:31:16 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: puneeth_aditya_5656 (mugiwaraluffy56)

<details>
<summary>Changes</summary>

## 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

---
Full diff: https://github.com/llvm/llvm-project/pull/178936.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+6-2) 
- (modified) clang/test/SemaCXX/alloc-token.cpp (+5) 


``````````diff
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}}
 }

``````````

</details>


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


More information about the cfe-commits mailing list