Skip to content

Commit 75d3fa3

Browse files
committed
Add TSAN annotations to pool_scalable
To fix false positives from TSAN.
1 parent 9bf7a0d commit 75d3fa3

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);
@@ -179,6 +180,7 @@ static void *tbb_malloc(void *pool, size_t size) {
179180
}
180181
return NULL;
181182
}
183+
utils_annotate_acquire(pool);
182184
return ptr;
183185
}
184186

@@ -205,6 +207,7 @@ static void *tbb_realloc(void *pool, void *ptr, size_t size) {
205207
}
206208
return NULL;
207209
}
210+
utils_annotate_acquire(pool);
208211
return new_ptr;
209212
}
210213

@@ -219,6 +222,7 @@ static void *tbb_aligned_malloc(void *pool, size_t size, size_t alignment) {
219222
}
220223
return NULL;
221224
}
225+
utils_annotate_acquire(pool);
222226
return ptr;
223227
}
224228

@@ -229,6 +233,12 @@ static umf_result_t tbb_free(void *pool, void *ptr) {
229233

230234
TLS_last_free_error = UMF_RESULT_SUCCESS;
231235

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