Skip to content

Commit 1a6d002

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

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/pool/pool_scalable.c

Lines changed: 12 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);
@@ -191,6 +192,13 @@ static void *tbb_calloc(void *pool, size_t num, size_t size) {
191192
return NULL;
192193
}
193194

195+
// synchronizes with release in tbb_free:
196+
// if another thread modified memory allocated from tbb and called free
197+
// and we end up getting this memory from tbb_malloc above we need to
198+
// tell asan that the memset below does not conflict with any preceeding
199+
// stores
200+
utils_annotate_acquire(ptr);
201+
194202
memset(ptr, 0, csize);
195203
return ptr;
196204
}
@@ -229,6 +237,9 @@ static umf_result_t tbb_free(void *pool, void *ptr) {
229237

230238
TLS_last_free_error = UMF_RESULT_SUCCESS;
231239

240+
// synchronizes (happens-before) with acquire in tbb_calloc
241+
utils_annotate_release(ptr);
242+
232243
struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool;
233244
if (g_tbb_ops.pool_free(pool_data->tbb_pool, ptr)) {
234245
return UMF_RESULT_SUCCESS;

src/utils/utils_sanitizers.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#endif
22+
}
23+
24+
void utils_annotate_release(void *ptr) {
25+
#if __SANITIZE_THREAD__
26+
__tsan_release(ptr);
27+
#endif
28+
}
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif

0 commit comments

Comments
 (0)