NCBI C++ ToolKit
ncbi_sanitizers.h
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef COMMON___NCBI_SANITIZERS__H
2 #define COMMON___NCBI_SANITIZERS__H
3 /* $Id: ncbi_sanitizers.h 98757 2023-01-03 17:01:54Z ucko $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Author: Vladimir Ivanov
29  *
30  */
31 
32 /// @file ncbi_sanitizers.h
33 /// Common macro to detect used sanitizers and suppress memory leaks if run under LeakSanitizer.
34 
35 #if defined(__has_feature)
36 # if __has_feature(address_sanitizer)
37 # define NCBI_USE_ASAN
38 # endif
39 # if __has_feature(leak_sanitizer)
40 # define NCBI_USE_LSAN
41 # endif
42 # if __has_feature(memory_sanitizer)
43 # define NCBI_USE_MSAN
44 # endif
45 # if __has_feature(thread_sanitizer)
46 # define NCBI_USE_TSAN
47 # endif
48 #else
49 // Fallback for other compilers
50 # if defined(__SANITIZE_ADDRESS__)
51 # define NCBI_USE_ASAN
52 # endif
53 # if defined(__SANITIZE_LEAK__)
54 # define NCBI_USE_LSAN
55 # endif
56 # if defined(__SANITIZE_MEMORY__)
57 # define NCBI_USE_MSAN
58 # endif
59 # if defined(__SANITIZE_THREAD__)
60 # define NCBI_USE_TSAN
61 # endif
62 #endif
63 
64 
65 #if defined(NCBI_USE_ASAN) && !defined(NCBI_USE_LSAN)
66 # define NCBI_USE_LSAN
67 #endif
68 
69 
70 /////////////////////////////////////////////////////////////////////////////
71 ///
72 /// LeakSanitazer
73 ///
74 
75 #if defined(NCBI_USE_LSAN)
76 # include <sanitizer/lsan_interface.h>
77 
78 /// Disable/enable LeakSanitizer.
79 ///
80 /// Allocations made between calls to NCBI_LSAN_DISABLE and NCBI_LSAN_ENABLE
81 /// will be treated as non-leaks. Disable/enable pairs may be nested,
82 /// but always should match.
83 /// @sa NCBI_LSAN_DISABLE_GUARD
84 
85 # define NCBI_LSAN_DISABLE __lsan_disable()
86 # define NCBI_LSAN_ENABLE __lsan_enable()
87 
88 /// Disable LeakSanitizer for a current scope.
89 ///
90 /// All memory allocations between NCBI_LSAN_DISABLE_GUARD and the rest
91 /// of the current scope will be treated as non-leaks.
92 /// @sa NCBI_LSAN_DISABLE, NCBI_LSAN_ENABLE
93 
94 # define NCBI_LSAN_DISABLE_GUARD \
95  __lsan::ScopedDisabler _lsan_scoped_disabler
96 
97 #else
98 # define NCBI_LSAN_ENABLE ((void)0)
99 # define NCBI_LSAN_DISABLE ((void)0)
100 # define NCBI_LSAN_DISABLE_GUARD ((void)0)
101 #endif
102 
103 
104 #endif /*COMMON___NCBI_SANITIZERS__H */
Modified on Sun Mar 03 03:14:15 2024 by modify_doxy.py rev. 669887