forked from OSchip/llvm-project
[tsan] Add XPC support (OS X)
XPC APIs have async callbacks, and we need some more happen-before edges to avoid false positives. This patch add them, plus a test case (sorry for the long boilerplate code, but XPC just needs all that). Differential Revision: http://reviews.llvm.org/D18493 llvm-svn: 265661
This commit is contained in:
parent
cecb7faea2
commit
e316bb61b3
|
@ -19,6 +19,7 @@
|
||||||
#include "tsan_interceptors.h"
|
#include "tsan_interceptors.h"
|
||||||
|
|
||||||
#include <libkern/OSAtomic.h>
|
#include <libkern/OSAtomic.h>
|
||||||
|
#include <xpc/xpc.h>
|
||||||
|
|
||||||
namespace __tsan {
|
namespace __tsan {
|
||||||
|
|
||||||
|
@ -86,6 +87,52 @@ TSAN_INTERCEPTOR(void, os_lock_unlock, void *lock) {
|
||||||
REAL(os_lock_unlock)(lock);
|
REAL(os_lock_unlock)(lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TSAN_INTERCEPTOR(void, xpc_connection_set_event_handler,
|
||||||
|
xpc_connection_t connection, xpc_handler_t handler) {
|
||||||
|
SCOPED_TSAN_INTERCEPTOR(xpc_connection_set_event_handler, connection,
|
||||||
|
handler);
|
||||||
|
Release(thr, pc, (uptr)connection);
|
||||||
|
xpc_handler_t new_handler = ^(xpc_object_t object) {
|
||||||
|
{
|
||||||
|
SCOPED_INTERCEPTOR_RAW(xpc_connection_set_event_handler);
|
||||||
|
Acquire(thr, pc, (uptr)connection);
|
||||||
|
}
|
||||||
|
handler(object);
|
||||||
|
};
|
||||||
|
REAL(xpc_connection_set_event_handler)(connection, new_handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
TSAN_INTERCEPTOR(void, xpc_connection_send_barrier, xpc_connection_t connection,
|
||||||
|
dispatch_block_t barrier) {
|
||||||
|
SCOPED_TSAN_INTERCEPTOR(xpc_connection_send_barrier, connection, barrier);
|
||||||
|
Release(thr, pc, (uptr)connection);
|
||||||
|
dispatch_block_t new_barrier = ^() {
|
||||||
|
{
|
||||||
|
SCOPED_INTERCEPTOR_RAW(xpc_connection_send_barrier);
|
||||||
|
Acquire(thr, pc, (uptr)connection);
|
||||||
|
}
|
||||||
|
barrier();
|
||||||
|
};
|
||||||
|
REAL(xpc_connection_send_barrier)(connection, new_barrier);
|
||||||
|
}
|
||||||
|
|
||||||
|
TSAN_INTERCEPTOR(void, xpc_connection_send_message_with_reply,
|
||||||
|
xpc_connection_t connection, xpc_object_t message,
|
||||||
|
dispatch_queue_t replyq, xpc_handler_t handler) {
|
||||||
|
SCOPED_TSAN_INTERCEPTOR(xpc_connection_send_message_with_reply, connection,
|
||||||
|
message, replyq, handler);
|
||||||
|
Release(thr, pc, (uptr)connection);
|
||||||
|
xpc_handler_t new_handler = ^(xpc_object_t object) {
|
||||||
|
{
|
||||||
|
SCOPED_INTERCEPTOR_RAW(xpc_connection_send_message_with_reply);
|
||||||
|
Acquire(thr, pc, (uptr)connection);
|
||||||
|
}
|
||||||
|
handler(object);
|
||||||
|
};
|
||||||
|
REAL(xpc_connection_send_message_with_reply)
|
||||||
|
(connection, message, replyq, new_handler);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace __tsan
|
} // namespace __tsan
|
||||||
|
|
||||||
#endif // SANITIZER_MAC
|
#endif // SANITIZER_MAC
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
// RUN: %clang_tsan %s -o %t -framework Foundation
|
||||||
|
// RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %deflake %run %t 2>&1 | FileCheck %s
|
||||||
|
|
||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
#import "../test.h"
|
||||||
|
|
||||||
|
long global;
|
||||||
|
|
||||||
|
long received_msgs;
|
||||||
|
xpc_connection_t server_conn;
|
||||||
|
xpc_connection_t client_conns[2];
|
||||||
|
|
||||||
|
int main(int argc, const char *argv[]) {
|
||||||
|
@autoreleasepool {
|
||||||
|
NSLog(@"Hello world.");
|
||||||
|
barrier_init(&barrier, 2);
|
||||||
|
|
||||||
|
dispatch_queue_t server_q = dispatch_queue_create("server.queue", DISPATCH_QUEUE_CONCURRENT);
|
||||||
|
|
||||||
|
server_conn = xpc_connection_create(NULL, server_q);
|
||||||
|
|
||||||
|
xpc_connection_set_event_handler(server_conn, ^(xpc_object_t client) {
|
||||||
|
NSLog(@"server event handler, client = %@", client);
|
||||||
|
|
||||||
|
if (client == XPC_ERROR_CONNECTION_INTERRUPTED || client == XPC_ERROR_CONNECTION_INVALID) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
xpc_connection_set_event_handler(client, ^(xpc_object_t object) {
|
||||||
|
NSLog(@"received message: %@", object);
|
||||||
|
|
||||||
|
barrier_wait(&barrier);
|
||||||
|
global = 42;
|
||||||
|
|
||||||
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||||
|
received_msgs++;
|
||||||
|
|
||||||
|
if (received_msgs >= 2) {
|
||||||
|
xpc_connection_cancel(client_conns[0]);
|
||||||
|
xpc_connection_cancel(client_conns[1]);
|
||||||
|
xpc_connection_cancel(server_conn);
|
||||||
|
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
xpc_connection_resume(client);
|
||||||
|
});
|
||||||
|
xpc_connection_resume(server_conn);
|
||||||
|
xpc_endpoint_t endpoint = xpc_endpoint_create(server_conn);
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
client_conns[i] = xpc_connection_create_from_endpoint(endpoint);
|
||||||
|
xpc_connection_set_event_handler(client_conns[i], ^(xpc_object_t event) {
|
||||||
|
NSLog(@"client event handler, event = %@", event);
|
||||||
|
});
|
||||||
|
|
||||||
|
xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
|
||||||
|
xpc_dictionary_set_string(msg, "hello", "world");
|
||||||
|
NSLog(@"sending message: %@", msg);
|
||||||
|
|
||||||
|
xpc_connection_send_message(client_conns[i], msg);
|
||||||
|
xpc_connection_resume(client_conns[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
CFRunLoopRun();
|
||||||
|
|
||||||
|
NSLog(@"Done.");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK: Hello world.
|
||||||
|
// CHECK: WARNING: ThreadSanitizer: data race
|
||||||
|
// CHECK: Write of size 8
|
||||||
|
// CHECK: #0 {{.*}}xpc-race.mm:33
|
||||||
|
// CHECK: Previous write of size 8
|
||||||
|
// CHECK: #0 {{.*}}xpc-race.mm:33
|
||||||
|
// CHECK: Location is global 'global'
|
||||||
|
// CHECK: Done.
|
|
@ -0,0 +1,73 @@
|
||||||
|
// RUN: %clang_tsan %s -o %t -framework Foundation
|
||||||
|
// RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %run %t 2>&1 | FileCheck %s
|
||||||
|
|
||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
long global;
|
||||||
|
|
||||||
|
int main(int argc, const char *argv[]) {
|
||||||
|
@autoreleasepool {
|
||||||
|
NSLog(@"Hello world.");
|
||||||
|
|
||||||
|
dispatch_queue_t server_q = dispatch_queue_create("server.queue", DISPATCH_QUEUE_CONCURRENT);
|
||||||
|
dispatch_queue_t client_q = dispatch_queue_create("client.queue", DISPATCH_QUEUE_CONCURRENT);
|
||||||
|
|
||||||
|
xpc_connection_t server_conn = xpc_connection_create(NULL, server_q);
|
||||||
|
|
||||||
|
global = 42;
|
||||||
|
|
||||||
|
xpc_connection_set_event_handler(server_conn, ^(xpc_object_t client) {
|
||||||
|
NSLog(@"global = %ld", global);
|
||||||
|
NSLog(@"server event handler, client = %@", client);
|
||||||
|
|
||||||
|
if (client == XPC_ERROR_CONNECTION_INTERRUPTED || client == XPC_ERROR_CONNECTION_INVALID) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
xpc_connection_set_event_handler(client, ^(xpc_object_t object) {
|
||||||
|
NSLog(@"received message: %@", object);
|
||||||
|
|
||||||
|
xpc_object_t reply = xpc_dictionary_create_reply(object);
|
||||||
|
if (!reply)
|
||||||
|
return;
|
||||||
|
xpc_dictionary_set_string(reply, "reply", "value");
|
||||||
|
|
||||||
|
xpc_connection_t remote = xpc_dictionary_get_remote_connection(object);
|
||||||
|
xpc_connection_send_message(remote, reply);
|
||||||
|
});
|
||||||
|
|
||||||
|
xpc_connection_resume(client);
|
||||||
|
});
|
||||||
|
xpc_connection_resume(server_conn);
|
||||||
|
xpc_endpoint_t endpoint = xpc_endpoint_create(server_conn);
|
||||||
|
|
||||||
|
xpc_connection_t client_conn = xpc_connection_create_from_endpoint(endpoint);
|
||||||
|
xpc_connection_set_event_handler(client_conn, ^(xpc_object_t event) {
|
||||||
|
NSLog(@"client event handler, event = %@", event);
|
||||||
|
});
|
||||||
|
|
||||||
|
xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
|
||||||
|
xpc_dictionary_set_string(msg, "hello", "world");
|
||||||
|
NSLog(@"sending message: %@", msg);
|
||||||
|
|
||||||
|
xpc_connection_send_message_with_reply(
|
||||||
|
client_conn, msg, client_q, ^(xpc_object_t object) {
|
||||||
|
NSLog(@"received reply: %@", object);
|
||||||
|
|
||||||
|
xpc_connection_cancel(client_conn);
|
||||||
|
xpc_connection_cancel(server_conn);
|
||||||
|
|
||||||
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||||
|
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
xpc_connection_resume(client_conn);
|
||||||
|
|
||||||
|
CFRunLoopRun();
|
||||||
|
|
||||||
|
NSLog(@"Done.");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK: Done.
|
||||||
|
// CHECK-NOT: WARNING: ThreadSanitizer
|
Loading…
Reference in New Issue