Author: puneeth_aditya_5656
Date: 2026-02-02T17:15:46+01:00
New Revision: 5b80848f06203b19dcfdf2408013f42a372bfa9b

URL: 
https://github.com/llvm/llvm-project/commit/5b80848f06203b19dcfdf2408013f42a372bfa9b
DIFF: 
https://github.com/llvm/llvm-project/commit/5b80848f06203b19dcfdf2408013f42a372bfa9b.diff

LOG: [clang][bytecode] Fix crash on __builtin_infer_alloc_token with struct 
argument (#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

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/InterpBuiltin.cpp
    clang/test/SemaCXX/alloc-token.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 2921618b0c630..a6b016c14184a 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1402,7 +1402,7 @@ static bool interp__builtin_infer_alloc_token(InterpState 
&S, CodePtr OpPC,
 
   // 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)));
+    discard(S.Stk, S.getContext().classify(Call->getArg(I)).value_or(PT_Ptr));
 
   // 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}}
 }


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to