This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen][ObjC] Emit a primitive store to store a __strong field in ExpandTypeFromArgs
ClosedPublic

Authored by ahatanak on Dec 2 2019, 6:04 PM.

Details

Reviewers
rjmccall
Summary

This fixes a bug in IRGen where a call to llvm.objc.storeStrong was being emitted to initialize a __strong field of an uninitialized temporary struct, which caused crashes at runtime.
rdar://problem/51807365

Diff Detail

Event Timeline

ahatanak created this revision.Dec 2 2019, 6:04 PM

Note that passing isInit=true to EmitStoreThroughLValue to make it emit llvm.objc.retain would be incorrect since the callee destructs the struct argument.

rjmccall added inline comments.Dec 2 2019, 8:38 PM
clang/lib/CodeGen/CGCall.cpp
1056

This definitely deserves a comment.

I don't think the assertion is right; the condition is that the type is legal for a field in a struct that can be passed directly, and while that does exclude __weak (because the struct will have to be passed indirectly) and __autoreleasing (because that's not legal in a struct), it doesn't exclude __unsafe_unretained.

This function is implementing an operation that's broadly meaningful (it's a store-init of an owned value, in contrast to a store-init with an unowned value which is what isInit is implementing) but not extensively used in the C frontend. On some level, I feel like we should probably teach EmitStoreThroughLValue to handle that properly, but that's a more significant refactor.

It does look like this change isn't enough to handle __ptrauth, which will assume that the source value is signed appropriately for the unqualified type when probably it should be signed appropriately for the qualifier (which, like __weak, cannot be address-discriminated because it would stop being passed directly). Probably the default case should be to use EmitStoreOfScalar, and EmitStoreThroughLValue should only be used if the l-value is a bit-field (the only non-simple case that can actually happen from drilling down to a field).

The same logic applies on the load side in the abstract, except that it is only causing problems for __ptrauth (well, if we change the behavior above) because loads from the ARC qualifiers don't implicitly retain. Regardless, analogously to this, EmitRValueForField should only be calling EmitLoadOfLValue for bit-fields and should otherwise call EmitLoadOfScalar. Please add a comment on both sides making it clear that the logic is expected to be parallel.

ahatanak updated this revision to Diff 232027.Dec 3 2019, 7:05 PM
ahatanak marked 2 inline comments as done.

Call EmitStoreThroughLValue and EmitLoadOfLValue only when the lvalue is a bitfield.

clang/lib/CodeGen/CGCall.cpp
1056

Ah right, the assertion isn't correct. My intention was to catch __weak pointers.

Let me know if the comment I added is OK.

rjmccall accepted this revision.Dec 3 2019, 7:41 PM

Yes, that's great, thank you.

This revision is now accepted and ready to land.Dec 3 2019, 7:41 PM