Skip to content

Commit c09787b

Browse files
FLZ101jprotze
andauthored
[OMPT] Set default values for tsan function pointers (#93568)
Avoid calling NULL function pointers in cases where ompt_start_tool succeeds but those tsan functions do not really exist. Fix #93524 --------- Co-authored-by: Joachim <[email protected]>
1 parent f69b6d2 commit c09787b

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

openmp/tools/archer/ompt-tsan.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <cassert>
2020
#include <cstdlib>
2121
#include <cstring>
22+
#include <dlfcn.h>
2223
#include <inttypes.h>
2324
#include <iostream>
2425
#include <list>
@@ -29,7 +30,6 @@
2930
#include <unistd.h>
3031
#include <unordered_map>
3132
#include <vector>
32-
#include <dlfcn.h>
3333

3434
#include "omp-tools.h"
3535

@@ -146,18 +146,28 @@ void __attribute__((weak)) __tsan_flush_memory() {}
146146
static ArcherFlags *archer_flags;
147147

148148
#ifndef TsanHappensBefore
149+
150+
template <typename... Args> static void __ompt_tsan_func(Args...) {}
151+
152+
#define DECLARE_TSAN_FUNCTION(name, ...) \
153+
static void (*name)(__VA_ARGS__) = __ompt_tsan_func<__VA_ARGS__>;
154+
149155
// Thread Sanitizer is a tool that finds races in code.
150156
// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
151157
// tsan detects these exact functions by name.
152158
extern "C" {
153-
static void (*AnnotateHappensAfter)(const char *, int, const volatile void *);
154-
static void (*AnnotateHappensBefore)(const char *, int, const volatile void *);
155-
static void (*AnnotateIgnoreWritesBegin)(const char *, int);
156-
static void (*AnnotateIgnoreWritesEnd)(const char *, int);
157-
static void (*AnnotateNewMemory)(const char *, int, const volatile void *,
158-
size_t);
159-
static void (*__tsan_func_entry)(const void *);
160-
static void (*__tsan_func_exit)(void);
159+
DECLARE_TSAN_FUNCTION(AnnotateHappensAfter, const char *, int,
160+
const volatile void *)
161+
DECLARE_TSAN_FUNCTION(AnnotateHappensBefore, const char *, int,
162+
const volatile void *)
163+
DECLARE_TSAN_FUNCTION(AnnotateIgnoreWritesBegin, const char *, int)
164+
DECLARE_TSAN_FUNCTION(AnnotateIgnoreWritesEnd, const char *, int)
165+
DECLARE_TSAN_FUNCTION(AnnotateNewMemory, const char *, int,
166+
const volatile void *, size_t)
167+
DECLARE_TSAN_FUNCTION(__tsan_func_entry, const void *)
168+
DECLARE_TSAN_FUNCTION(__tsan_func_exit)
169+
170+
// RunningOnValgrind is used to detect absence of TSan and must intentionally be a nullptr.
161171
static int (*RunningOnValgrind)(void);
162172
}
163173

@@ -1142,7 +1152,10 @@ static void ompt_tsan_mutex_released(ompt_mutex_t kind, ompt_wait_id_t wait_id,
11421152

11431153
#define findTsanFunction(f, fSig) \
11441154
do { \
1145-
if (NULL == (f = fSig dlsym(RTLD_DEFAULT, #f))) \
1155+
void *fp = dlsym(RTLD_DEFAULT, #f); \
1156+
if (fp) \
1157+
f = fSig fp; \
1158+
else \
11461159
printf("Unable to find TSan function " #f ".\n"); \
11471160
} while (0)
11481161

0 commit comments

Comments
 (0)