This patch disables moving a __block variable to return it from a function, which was causing a crash that occurred when compiling and executing the following code:
#include <dispatch/dispatch.h>
#include <memory>
#include <cstdio>
class A {
public:
A(int x) : internal(x) {}
int internal;
};
dispatch_semaphore_t sema;
dispatch_queue_t q;
std::shared_ptr<A> dispatch_function(int x) {
__block std::shared_ptr<A> ret = std::shared_ptr<A>(new A(x));
dispatch_async(q, ^{
printf("%d\n", ret->internal); // segfaults here
dispatch_semaphore_signal(sema);
});
return ret;
}
int main() {
q = dispatch_queue_create("com.example.MyCustomQueue", NULL);
sema = dispatch_semaphore_create(0);
dispatch_function(100);
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
return 0;
}This is how the crash occurs:
- When dispatch_async is called, the block passed to it is copied to the heap, and when that happens the shared_ptr is moved to the heap. Note that the shared_ptr is moved, not copied, in @__Block_byref_object_copy_ because Sema::CheckCompleteVariableDeclaration passes " /*AllowNRVO=*/true" to the call to PerformMoveOrCopyInitialization. I'm not sure whether this is correct or not, but even after changing it to pass AllowNRVO=false, the code still crashes.
- "ret" is moved to the return aggregate. Since "ret" is a __block variable, it refers to the shared_ptr on the heap, not on the stack, so the one on the heap is moved.
- The destructor for the shared_ptr on the stack is called when dispatch_function exits.
- When dispatch_function returns in "main", the returned shared_ptr is immediately destructed, and the object it points to is destructed too when the reference count drops to zero.
- The code crashes when the block passed to dispatch_async is executed since the object ret used to point to has been destructed.
An alternate solution that fixes the crash is to somehow pass followForward=false to CodeGenFunction::emitBlockByrefAddress so that it emits the address of the shared_ptr on the stack, not on the heap, but I guess that is not always correct and can break some other code.
rdar://problem/28181080