Skip to content

Commit

Permalink
Fix crash in ObjC codegen introduced with 5ab6ee7
Browse files Browse the repository at this point in the history
5ab6ee7 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
  • Loading branch information
davidchisnall committed Jul 24, 2022
1 parent 0708771 commit 94c3b16
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
12 changes: 7 additions & 5 deletions clang/lib/CodeGen/CGObjCGNU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() */ {
Expand Down
22 changes: 22 additions & 0 deletions clang/test/CodeGenObjC/gnustep2-nontrivial-destructor-argument.mm
Original file line number Diff line number Diff line change
@@ -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<Y> obj)
{
X a{12};
[obj foo: a];
}

0 comments on commit 94c3b16

Please sign in to comment.