Skip to content

Commit f47dcdb

Browse files
authored
Merge pull request #132 from igchor/tsan_fixes
Add TSAN annotations to pool_scalable
2 parents 6503c1c + 75d3fa3 commit f47dcdb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/pool/pool_scalable.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2023 Intel Corporation
3+
* Copyright (C) 2023-2024 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -24,6 +24,7 @@
2424
#include <tbb/scalable_allocator.h>
2525

2626
#include "utils_common.h"
27+
#include "utils_sanitizers.h"
2728

2829
typedef void *(*raw_alloc_tbb_type)(intptr_t, size_t *);
2930
typedef void (*raw_free_tbb_type)(intptr_t, void *, size_t);
@@ -180,6 +181,7 @@ static void *tbb_malloc(void *pool, size_t size) {
180181
}
181182
return NULL;
182183
}
184+
utils_annotate_acquire(pool);
183185
return ptr;
184186
}
185187

@@ -206,6 +208,7 @@ static void *tbb_realloc(void *pool, void *ptr, size_t size) {
206208
}
207209
return NULL;
208210
}
211+
utils_annotate_acquire(pool);
209212
return new_ptr;
210213
}
211214

@@ -220,6 +223,7 @@ static void *tbb_aligned_malloc(void *pool, size_t size, size_t alignment) {
220223
}
221224
return NULL;
222225
}
226+
utils_annotate_acquire(pool);
223227
return ptr;
224228
}
225229

@@ -230,6 +234,12 @@ static umf_result_t tbb_free(void *pool, void *ptr) {
230234

231235
TLS_last_free_error = UMF_RESULT_SUCCESS;
232236

237+
// Establishes happens-before order with tbb_*alloc functions.
238+
// Makes sure that writes to the memory pointed to by 'ptr'
239+
// are not reported as data races whenever 'ptr' reused by
240+
// other threads.
241+
utils_annotate_release(pool);
242+
233243
struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool;
234244
if (g_tbb_ops.pool_free(pool_data->tbb_pool, ptr)) {
235245
return UMF_RESULT_SUCCESS;

src/utils/utils_sanitizers.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#if __SANITIZE_THREAD__
11+
#include <sanitizer/tsan_interface.h>
12+
#endif
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
void utils_annotate_acquire(void *ptr) {
19+
#if __SANITIZE_THREAD__
20+
__tsan_acquire(ptr);
21+
#else
22+
(void)ptr;
23+
#endif
24+
}
25+
26+
void utils_annotate_release(void *ptr) {
27+
#if __SANITIZE_THREAD__
28+
__tsan_release(ptr);
29+
#else
30+
(void)ptr;
31+
#endif
32+
}
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif

0 commit comments

Comments
 (0)