This is an archive of the discontinued LLVM Phabricator instance.

[ObjC] avoid crashing when emitting synthesized getter/setter and ptrdiff_t is smaller than long
ClosedPublic

Authored by mhjacobson on Oct 18 2021, 7:28 PM.

Details

Summary

On targets where ptrdiff_t is smaller than long, clang crashes (backtrace below) 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.

Backtrace:

Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file /Users/matt/src/llvm/llvm/lib/IR/Instructions.cpp, line 3042.

...

7  libsystem_c.dylib        0x00007fff6ec09ac6 err + 0
8  clang-14                 0x000000010acf104c llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::Instruction*) + 124
9  clang-14                 0x000000010beb140a llvm::IRBuilderBase::CreateCast(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&) + 234
10 clang-14                 0x000000010bebdec2 llvm::IRBuilderBase::CreateZExt(llvm::Value*, llvm::Type*, llvm::Twine const&) + 50
11 clang-14                 0x000000010cab50aa clang::CodeGen::CodeGenFunction::EmitCall(clang::CodeGen::CGFunctionInfo const&, clang::CodeGen::CGCallee const&, clang::CodeGen::ReturnValueSlot, clang::CodeGen::CallArgList const&, llvm::CallBase**, bool, clang::SourceLocation) + 8442
12 clang-14                 0x000000010ceadeb8 clang::CodeGen::CodeGenFunction::EmitCall(clang::CodeGen::CGFunctionInfo const&, clang::CodeGen::CGCallee const&, clang::CodeGen::ReturnValueSlot, clang::CodeGen::CallArgList const&, llvm::CallBase**, bool) + 216
13 clang-14                 0x000000010cc76fe3 clang::CodeGen::CodeGenFunction::generateObjCGetterBody(clang::ObjCImplementationDecl const*, clang::ObjCPropertyImplDecl const*, clang::ObjCMethodDecl const*, llvm::Constant*) + 2595
14 clang-14                 0x000000010cc75387 clang::CodeGen::CodeGenFunction::GenerateObjCGetter(clang::ObjCImplementationDecl*, clang::ObjCPropertyImplDecl const*) + 343
15 clang-14                 0x000000010cf1c4c0 clang::CodeGen::CodeGenModule::EmitObjCPropertyImplementations(clang::ObjCImplementationDecl const*) + 336
16 clang-14                 0x000000010cf17799 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) + 2009
17 clang-14                 0x000000010d114bf2 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) + 146
18 clang-14                 0x000000010cec0014 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) + 228
19 clang-14                 0x0000000110436425 clang::ParseAST(clang::Sema&, bool, bool) + 533

Diff Detail

Event Timeline

mhjacobson created this revision.Oct 18 2021, 7:28 PM
mhjacobson requested review of this revision.Oct 18 2021, 7:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 18 2021, 7:28 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

The test is failing for a different reason (that I'd forgotten I worked around locally). I'll look into fixing that first. Sorry for noise.

mhjacobson updated this revision to Diff 451085.Aug 9 2022, 3:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 9 2022, 3:12 AM

Hello, and sorry for the delay.

The blocking bug was fixed by D112113. This is ready for review. Please let me know if you have any feedback.

This revision was not accepted when it landed; it landed in state Needs Review.Nov 9 2022, 11:11 PM
This revision was automatically updated to reflect the committed changes.