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 102663 2024-06-24 13:38:59Z ivanov $
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 #endif
49 
50 // Fallback for other compilers.
51 // __has_feature(x) doesn't always work for sanitizers as well,
52 // even if it is supported
53 
54 #if !defined(NCBI_USE_ASAN) && defined(__SANITIZE_ADDRESS__)
55 # define NCBI_USE_ASAN
56 #endif
57 #if !defined(NCBI_USE_LSAN) && defined(__SANITIZE_LEAK__)
58 # define NCBI_USE_LSAN
59 #endif
60 #if !defined(NCBI_USE_MSAN) && defined(__SANITIZE_MEMORY__)
61 # define NCBI_USE_MSAN
62 #endif
63 #if !defined(NCBI_USE_TSAN) && defined(__SANITIZE_THREAD__)
64 # define NCBI_USE_TSAN
65 #endif
66 
67 #if defined(NCBI_USE_ASAN) && !defined(NCBI_USE_LSAN)
68 # define NCBI_USE_LSAN
69 #endif
70 
71 
72 // Define NCBI_USE_SANITIZER if running under ANY sanitizer
73 #if defined(NCBI_USE_ASAN) || defined(NCBI_USE_LSAN) || defined(NCBI_USE_MSAN) || defined(NCBI_USE_TSAN)
74 # define NCBI_USE_SANITIZER
75 #endif
76 
77 
78 /////////////////////////////////////////////////////////////////////////////
79 ///
80 /// LeakSanitazer
81 ///
82 
83 #if defined(NCBI_USE_LSAN)
84 # include <sanitizer/lsan_interface.h>
85 
86 /// Disable/enable LeakSanitizer.
87 ///
88 /// Allocations made between calls to NCBI_LSAN_DISABLE and NCBI_LSAN_ENABLE
89 /// will be treated as non-leaks. Disable/enable pairs may be nested,
90 /// but always should match.
91 /// @sa NCBI_LSAN_DISABLE_GUARD
92 
93 # define NCBI_LSAN_DISABLE __lsan_disable()
94 # define NCBI_LSAN_ENABLE __lsan_enable()
95 
96 /// Disable LeakSanitizer for a current scope.
97 ///
98 /// All memory allocations between NCBI_LSAN_DISABLE_GUARD and the rest
99 /// of the current scope will be treated as non-leaks.
100 /// @sa NCBI_LSAN_DISABLE, NCBI_LSAN_ENABLE
101 
102 # define NCBI_LSAN_DISABLE_GUARD \
103  __lsan::ScopedDisabler _lsan_scoped_disabler
104 
105 #else
106 # define NCBI_LSAN_ENABLE ((void)0)
107 # define NCBI_LSAN_DISABLE ((void)0)
108 # define NCBI_LSAN_DISABLE_GUARD ((void)0)
109 #endif
110 
111 
112 #endif /*COMMON___NCBI_SANITIZERS__H */
Modified on Fri Sep 20 14:57:06 2024 by modify_doxy.py rev. 669887