1// Copyright (C) 2017 Intel Corporation.
2// Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QTSAN_IMPL_H
7#define QTSAN_IMPL_H
8
9#include <QtCore/qglobal.h>
10
11#if (__has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)) && __has_include(<sanitizer/tsan_interface.h>)
12# define QT_BUILDING_UNDER_TSAN
13# include <sanitizer/tsan_interface.h>
14#endif
15
16QT_BEGIN_NAMESPACE
17
18namespace QtTsan {
19#ifdef QT_BUILDING_UNDER_TSAN
20inline void futexAcquire(void *addr, void *addr2 = nullptr)
21{
22 // A futex call ensures total ordering on the futex words
23 // (in either success or failure of the call). Instruct TSAN accordingly,
24 // as TSAN does not understand the futex(2) syscall (or equivalent).
25 ::__tsan_acquire(addr);
26 if (addr2)
27 ::__tsan_acquire(addr2);
28}
29
30inline void futexRelease(void *addr, void *addr2 = nullptr)
31{
32 if (addr2)
33 ::__tsan_release(addr2);
34 ::__tsan_release(addr);
35}
36
37inline void mutexPreLock(void *addr, unsigned flags)
38{
39 ::__tsan_mutex_pre_lock(addr, flags);
40}
41
42inline void mutexPostLock(void *addr, unsigned flags, int recursion)
43{
44 ::__tsan_mutex_post_lock(addr, flags, recursion);
45}
46
47inline void mutexPreUnlock(void *addr, unsigned flags)
48{
49 ::__tsan_mutex_pre_unlock(addr, flags);
50}
51
52inline void mutexPostUnlock(void *addr, unsigned flags)
53{
54 ::__tsan_mutex_post_unlock(addr, flags);
55}
56
57enum : unsigned {
58 MutexWriteReentrant = ::__tsan_mutex_write_reentrant,
59 TryLock = ::__tsan_mutex_try_lock,
60 TryLockFailed = ::__tsan_mutex_try_lock_failed,
61};
62#else
63inline void futexAcquire(void *, void * = nullptr) {}
64inline void futexRelease(void *, void * = nullptr) {}
65
66enum : unsigned {
67 MutexWriteReentrant,
68 TryLock,
69 TryLockFailed,
70};
71inline void mutexPreLock(void *, unsigned) {}
72inline void mutexPostLock(void *, unsigned, int) {}
73inline void mutexPreUnlock(void *, unsigned) {}
74inline void mutexPostUnlock(void *, unsigned) {}
75#endif // QT_BUILDING_UNDER_TSAN
76} // namespace QtTsan
77
78QT_END_NAMESPACE
79
80#endif // QTSAN_IMPL_H
81

source code of qtbase/src/corelib/thread/qtsan_impl.h