Skip to content

Commit e0e0244

Browse files
committed
[TSan][libdispatch] Remove dependency on system headers
Including <dispatch/dispatch.h> and <Blocks.h> transitively pulls in other system headers. Let's try to avoid that. Blocks.h: compiler-rt already includes a blocks runtime. Just use the header file that comes with it. dispatch.h: Declare the bare minimum required for our implementation, i.e., everything needed to define the interceptors, but not the interceptors themselves. See tsan_dispatch_defs.h. I spotted a few other places in compile-rt, where we declare libdispatch types. Maybe this file can be moved to sanitizer_common if we deem it useful enough. tsan_libdispatch.cc now compiles on Linux/Clang (requires support for -fblocks). Linking still requires some manual configuration. Reviewed By: kubamracek Differential Revision: https://reviews.llvm.org/D59145 llvm-svn: 356201
1 parent 9fd1848 commit e0e0244

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===-- tsan_dispatch_defs.h ------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file is a part of ThreadSanitizer (TSan), a race detector.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef TSAN_DISPATCH_DEFS_H
13+
#define TSAN_DISPATCH_DEFS_H
14+
15+
#include "sanitizer_common/sanitizer_internal_defs.h"
16+
17+
typedef struct dispatch_object_s {} *dispatch_object_t;
18+
19+
#define DISPATCH_DECL(name) \
20+
typedef struct name##_s : public dispatch_object_s {} *name##_t
21+
22+
DISPATCH_DECL(dispatch_queue);
23+
DISPATCH_DECL(dispatch_source);
24+
DISPATCH_DECL(dispatch_group);
25+
DISPATCH_DECL(dispatch_data);
26+
DISPATCH_DECL(dispatch_semaphore);
27+
DISPATCH_DECL(dispatch_io);
28+
29+
typedef void (*dispatch_function_t)(void *arg);
30+
typedef void (^dispatch_block_t)(void);
31+
typedef void (^dispatch_io_handler_t)(bool done, dispatch_data_t data,
32+
int error);
33+
34+
typedef long dispatch_once_t; // NOLINT
35+
typedef __sanitizer::u64 dispatch_time_t;
36+
typedef int dispatch_fd_t; // NOLINT
37+
typedef unsigned long dispatch_io_type_t; // NOLINT
38+
typedef unsigned long dispatch_io_close_flags_t; // NOLINT
39+
40+
extern "C" {
41+
void *dispatch_get_context(dispatch_object_t object);
42+
void dispatch_retain(dispatch_object_t object);
43+
void dispatch_release(dispatch_object_t object);
44+
45+
extern const dispatch_block_t _dispatch_data_destructor_free;
46+
extern const dispatch_block_t _dispatch_data_destructor_munmap;
47+
} // extern "C"
48+
49+
#define DISPATCH_DATA_DESTRUCTOR_DEFAULT nullptr
50+
#define DISPATCH_DATA_DESTRUCTOR_FREE _dispatch_data_destructor_free
51+
#define DISPATCH_DATA_DESTRUCTOR_MUNMAP _dispatch_data_destructor_munmap
52+
53+
#if __has_attribute(noescape)
54+
#define DISPATCH_NOESCAPE __attribute__((__noescape__))
55+
#else
56+
#define DISPATCH_NOESCAPE
57+
#endif
58+
59+
// Data types used in dispatch APIs
60+
typedef unsigned long size_t; // NOLINT
61+
typedef unsigned long uintptr_t; // NOLINT
62+
typedef __sanitizer::s64 off_t;
63+
typedef __sanitizer::u16 mode_t;
64+
typedef long long_t; // NOLINT
65+
66+
#endif // TSAN_DISPATCH_DEFS_H

compiler-rt/lib/tsan/rtl/tsan_libdispatch.cc

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,16 @@
1111
// Support for intercepting libdispatch (GCD).
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "sanitizer_common/sanitizer_platform.h"
15-
1614
#include "sanitizer_common/sanitizer_common.h"
1715
#include "interception/interception.h"
1816
#include "tsan_interceptors.h"
19-
#include "tsan_platform.h"
2017
#include "tsan_rtl.h"
2118

22-
#include <Block.h>
23-
#include <dispatch/dispatch.h>
24-
25-
// DISPATCH_NOESCAPE is only defined on Apple platforms with at least Xcode 8.
26-
#ifndef DISPATCH_NOESCAPE
27-
#define DISPATCH_NOESCAPE
28-
#endif
29-
30-
typedef long long_t; // NOLINT
19+
#include "BlocksRuntime/Block.h"
20+
#include "tsan_dispatch_defs.h"
3121

3222
namespace __tsan {
23+
typedef u16 uint16_t;
3324

3425
typedef struct {
3526
dispatch_queue_t queue;
@@ -322,9 +313,12 @@ TSAN_INTERCEPTOR(long_t, dispatch_group_wait, dispatch_group_t group,
322313
return result;
323314
}
324315

316+
// Used, but not intercepted.
317+
extern "C" void dispatch_group_enter(dispatch_group_t group);
318+
325319
TSAN_INTERCEPTOR(void, dispatch_group_leave, dispatch_group_t group) {
326320
SCOPED_TSAN_INTERCEPTOR(dispatch_group_leave, group);
327-
// Acquired in the group noticifaction callback in dispatch_group_notify[_f].
321+
// Acquired in the group notification callback in dispatch_group_notify[_f].
328322
Release(thr, pc, (uptr)group);
329323
REAL(dispatch_group_leave)(group);
330324
}
@@ -334,10 +328,10 @@ TSAN_INTERCEPTOR(void, dispatch_group_async, dispatch_group_t group,
334328
SCOPED_TSAN_INTERCEPTOR(dispatch_group_async, group, queue, block);
335329
dispatch_retain(group);
336330
dispatch_group_enter(group);
337-
__block dispatch_block_t block_copy = (dispatch_block_t)_Block_copy(block);
331+
__block dispatch_block_t block_copy = (dispatch_block_t)Block_copy(block);
338332
WRAP(dispatch_async)(queue, ^(void) {
339333
block_copy();
340-
_Block_release(block_copy);
334+
Block_release(block_copy);
341335
WRAP(dispatch_group_leave)(group);
342336
dispatch_release(group);
343337
});

0 commit comments

Comments
 (0)