[clang] dd9f796 - [ObjC] avoid crashing when emitting synthesized getter/setter and ptrdiff_t is smaller than long
Matt Jacobson via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 9 23:10:58 PST 2022
Author: Matt Jacobson
Date: 2022-11-10T02:10:30-05:00
New Revision: dd9f7963e434a53e8d70f607392ee9abf76f1d99
URL: https://github.com/llvm/llvm-project/commit/dd9f7963e434a53e8d70f607392ee9abf76f1d99
DIFF: https://github.com/llvm/llvm-project/commit/dd9f7963e434a53e8d70f607392ee9abf76f1d99.diff
LOG: [ObjC] avoid crashing when emitting synthesized getter/setter and ptrdiff_t is smaller than long
On targets where ptrdiff_t is smaller than long, clang crashes when emitting
synthesized getters/setters that call objc_[gs]etProperty. Explicitly emit a
zext/trunc of the ivar offset value (which is defined to long) to ptrdiff_t,
which objc_[gs]etProperty takes.
Add a test using the AVR target, where ptrdiff_t is smaller than long. Test
failed previously and passes now.
Differential Revision: https://reviews.llvm.org/D112049
Added:
clang/test/CodeGen/avr/objc-property.m
Modified:
clang/lib/CodeGen/CGExpr.cpp
clang/lib/CodeGen/CGObjC.cpp
clang/lib/CodeGen/CodeGenFunction.h
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 40d9b8f37b4a6..ee09a8566c371 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -5242,6 +5242,15 @@ llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
}
+llvm::Value *
+CodeGenFunction::EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface,
+ const ObjCIvarDecl *Ivar) {
+ llvm::Value *OffsetValue = EmitIvarOffset(Interface, Ivar);
+ QualType PointerDiffType = getContext().getPointerDiffType();
+ return Builder.CreateZExtOrTrunc(OffsetValue,
+ getTypes().ConvertType(PointerDiffType));
+}
+
LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
llvm::Value *BaseValue,
const ObjCIvarDecl *Ivar,
diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp
index 5ec2fdb5c1708..3eb530d3e9417 100644
--- a/clang/lib/CodeGen/CGObjC.cpp
+++ b/clang/lib/CodeGen/CGObjC.cpp
@@ -1228,7 +1228,7 @@ CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
llvm::Value *cmd = emitCmdValueForGetterSetterBody(*this, getterMethod);
llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
llvm::Value *ivarOffset =
- EmitIvarOffset(classImpl->getClassInterface(), ivar);
+ EmitIvarOffsetAsPointerDiff(classImpl->getClassInterface(), ivar);
CallArgList args;
args.add(RValue::get(self), getContext().getObjCIdType());
@@ -1532,7 +1532,7 @@ CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
llvm::Value *self =
Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
llvm::Value *ivarOffset =
- EmitIvarOffset(classImpl->getClassInterface(), ivar);
+ EmitIvarOffsetAsPointerDiff(classImpl->getClassInterface(), ivar);
Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
llvm::Value *arg = Builder.CreateLoad(argAddr, "arg");
arg = Builder.CreateBitCast(arg, VoidPtrTy);
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 05e5bad21d46f..7ef22eb064efd 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -3974,6 +3974,8 @@ class CodeGenFunction : public CodeGenTypeCache {
llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
const ObjCIvarDecl *Ivar);
+ llvm::Value *EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface,
+ const ObjCIvarDecl *Ivar);
LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
LValue EmitLValueForLambdaField(const FieldDecl *Field);
diff --git a/clang/test/CodeGen/avr/objc-property.m b/clang/test/CodeGen/avr/objc-property.m
new file mode 100644
index 0000000000000..a3eaea2e0375a
--- /dev/null
+++ b/clang/test/CodeGen/avr/objc-property.m
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple avr -emit-llvm -fobjc-runtime=macosx %s -o /dev/null
+
+__attribute__((objc_root_class))
+ at interface Foo
+
+ at property(strong) Foo *f;
+
+ at end
+
+ at implementation Foo
+
+ at synthesize f = _f;
+
+ at end
More information about the cfe-commits
mailing list