From 94c3b169785c0a0ae650c724dcf2c22ff65f5e24 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Sun, 24 Jul 2022 11:56:12 +0100 Subject: [PATCH] Fix crash in ObjC codegen introduced with 5ab6ee75994d645725264e757d67bbb1c96fb2b6 5ab6ee75994d645725264e757d67bbb1c96fb2b6 assumed that if `RValue::isScalar()` returns true then `RValue::getScalarVal` will return a valid value. This is not the case when the return value is `void` and so void message returns would crash if they hit this path. This is triggered only for cases where the nil-handling path needs to do something non-trivial (destroy arguments that should be consumed by the callee). Reviewed By: triplef Differential Revision: https://reviews.llvm.org/D123898 --- clang/lib/CodeGen/CGObjCGNU.cpp | 12 +++++++----- .../gnustep2-nontrivial-destructor-argument.mm | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 clang/test/CodeGenObjC/gnustep2-nontrivial-destructor-argument.mm diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp index ec459f0..7bbe9af 100644 --- a/clang/lib/CodeGen/CGObjCGNU.cpp +++ b/clang/lib/CodeGen/CGObjCGNU.cpp @@ -2837,11 +2837,13 @@ CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF, // Enter the continuation block and emit a phi if required. CGF.EmitBlock(continueBB); if (msgRet.isScalar()) { - llvm::Value *v = msgRet.getScalarVal(); - llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); - phi->addIncoming(v, nonNilPathBB); - phi->addIncoming(CGM.EmitNullConstant(ResultType), nilPathBB); - msgRet = RValue::get(phi); + // If the return type is void, do nothing + if (llvm::Value *v = msgRet.getScalarVal()) { + llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); + phi->addIncoming(v, nonNilPathBB); + phi->addIncoming(CGM.EmitNullConstant(ResultType), nilPathBB); + msgRet = RValue::get(phi); + } } else if (msgRet.isAggregate()) { // Aggregate zeroing is handled in nilCleanupBB when it's required. } else /* isComplex() */ { diff --git a/clang/test/CodeGenObjC/gnustep2-nontrivial-destructor-argument.mm b/clang/test/CodeGenObjC/gnustep2-nontrivial-destructor-argument.mm new file mode 100644 index 0000000..a7de79b --- /dev/null +++ b/clang/test/CodeGenObjC/gnustep2-nontrivial-destructor-argument.mm @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 -triple x86_64-unknow-windows-msvc -S -emit-llvm -fobjc-runtime=gnustep-2.0 -o - %s + +// Regression test. Ensure that C++ arguments with non-trivial destructors +// don't crash the compiler. + +struct X +{ + int a; + ~X(); +}; + +@protocol Y +- (void)foo: (X)bar; +@end + + +void test(id obj) +{ + X a{12}; + [obj foo: a]; +} + -- 2.7.4