[clang] 94c3b16 - Fix crash in ObjC codegen introduced with 5ab6ee75994d645725264e757d67bbb1c96fb2b6
David Chisnall via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 24 06:02:03 PDT 2022
Author: David Chisnall
Date: 2022-07-24T13:59:45+01:00
New Revision: 94c3b169785c0a0ae650c724dcf2c22ff65f5e24
URL: https://github.com/llvm/llvm-project/commit/94c3b169785c0a0ae650c724dcf2c22ff65f5e24
DIFF: https://github.com/llvm/llvm-project/commit/94c3b169785c0a0ae650c724dcf2c22ff65f5e24.diff
LOG: 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
Added:
clang/test/CodeGenObjC/gnustep2-nontrivial-destructor-argument.mm
Modified:
clang/lib/CodeGen/CGObjCGNU.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index ec459f07f307d..7bbe9af7ed593 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 0000000000000..a7de79bf79940
--- /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();
+};
+
+ at protocol Y
+- (void)foo: (X)bar;
+ at end
+
+
+void test(id<Y> obj)
+{
+ X a{12};
+ [obj foo: a];
+}
+
More information about the cfe-commits
mailing list