| 1 |
=========================== |
| 2 |
Sanitizer special case list |
| 3 |
=========================== |
| 4 |
|
| 5 |
.. contents:: |
| 6 |
:local: |
| 7 |
|
| 8 |
Introduction |
| 9 |
============ |
| 10 |
|
| 11 |
This document describes the way to disable or alter the behavior of |
| 12 |
sanitizer tools for certain source-level entities by providing a special |
| 13 |
file at compile-time. |
| 14 |
|
| 15 |
Goal and usage |
| 16 |
============== |
| 17 |
|
| 18 |
Users of sanitizer tools, such as :doc:`AddressSanitizer`, |
| 19 |
:doc:`HardwareAssistedAddressSanitizerDesign`, :doc:`ThreadSanitizer`, |
| 20 |
:doc:`MemorySanitizer` or :doc:`UndefinedBehaviorSanitizer` may want to disable |
| 21 |
or alter some checks for certain source-level entities to: |
| 22 |
|
| 23 |
* speedup hot function, which is known to be correct; |
| 24 |
* ignore a function that does some low-level magic (e.g. walks through the |
| 25 |
thread stack, bypassing the frame boundaries); |
| 26 |
* ignore a known problem. |
| 27 |
|
| 28 |
To achieve this, user may create a file listing the entities they want to |
| 29 |
ignore, and pass it to clang at compile-time using |
| 30 |
``-fsanitize-ignorelist`` flag. See :doc:`UsersManual` for details. |
| 31 |
|
| 32 |
Example |
| 33 |
======= |
| 34 |
|
| 35 |
.. code-block:: bash |
| 36 |
|
| 37 |
$ cat foo.c |
| 38 |
#include <stdlib.h> |
| 39 |
void bad_foo() { |
| 40 |
int *a = (int*)malloc(40); |
| 41 |
a[10] = 1; |
| 42 |
free(a); |
| 43 |
} |
| 44 |
int main() { bad_foo(); } |
| 45 |
$ cat ignorelist.txt |
| 46 |
# Ignore reports from bad_foo function. |
| 47 |
fun:bad_foo |
| 48 |
$ clang -fsanitize=address foo.c ; ./a.out |
| 49 |
# AddressSanitizer prints an error report. |
| 50 |
$ clang -fsanitize=address -fsanitize-ignorelist=ignorelist.txt foo.c ; ./a.out |
| 51 |
# No error report here. |
| 52 |
|
| 53 |
Usage with UndefinedBehaviorSanitizer |
| 54 |
===================================== |
| 55 |
|
| 56 |
``unsigned-integer-overflow``, ``signed-integer-overflow``, |
| 57 |
``implicit-signed-integer-truncation``, |
| 58 |
``implicit-unsigned-integer-truncation``, and ``enum`` sanitizers support the |
| 59 |
ability to adjust instrumentation based on type. |
| 60 |
|
| 61 |
By default, supported sanitizers will have their instrumentation disabled for |
| 62 |
entries specified within an ignorelist. |
| 63 |
|
| 64 |
.. code-block:: bash |
| 65 |
|
| 66 |
$ cat foo.c |
| 67 |
void foo() { |
| 68 |
int a = 2147483647; // INT_MAX |
| 69 |
++a; // Normally, an overflow with -fsanitize=signed-integer-overflow |
| 70 |
} |
| 71 |
$ cat ignorelist.txt |
| 72 |
[signed-integer-overflow] |
| 73 |
type:int |
| 74 |
$ clang -fsanitize=signed-integer-overflow -fsanitize-ignorelist=ignorelist.txt foo.c ; ./a.out |
| 75 |
# no signed-integer-overflow error |
| 76 |
|
| 77 |
For example, supplying the above ``ignorelist.txt`` to |
| 78 |
``-fsanitize-ignorelist=ignorelist.txt`` disables overflow sanitizer |
| 79 |
instrumentation for arithmetic operations containing values of type ``int``. |
| 80 |
|
| 81 |
The ``=sanitize`` category is also supported. Any ``=sanitize`` category |
| 82 |
entries enable sanitizer instrumentation, even if it was ignored by entries |
| 83 |
before. Entries can be ``src``, ``type``, ``global``, ``fun``, and |
| 84 |
``mainfile``. |
| 85 |
|
| 86 |
With this, one may disable instrumentation for some or all types and |
| 87 |
specifically allow instrumentation for one or many types -- including types |
| 88 |
created via ``typedef``. This is a way to achieve a sort of "allowlist" for |
| 89 |
supported sanitizers. |
| 90 |
|
| 91 |
.. code-block:: bash |
| 92 |
|
| 93 |
$ cat ignorelist.txt |
| 94 |
[implicit-signed-integer-truncation] |
| 95 |
type:* |
| 96 |
type:T=sanitize |
| 97 |
|
| 98 |
$ cat foo.c |
| 99 |
typedef char T; |
| 100 |
typedef char U; |
| 101 |
void foo(int toobig) { |
| 102 |
T a = toobig; // instrumented |
| 103 |
U b = toobig; // not instrumented |
| 104 |
char c = toobig; // also not instrumented |
| 105 |
} |
| 106 |
|
| 107 |
If multiple entries match the source, then the latest entry takes the |
| 108 |
precedence. Here are a few examples. |
| 109 |
|
| 110 |
.. code-block:: bash |
| 111 |
|
| 112 |
$ cat ignorelist1.txt |
| 113 |
# test.cc will not be instrumented. |
| 114 |
src:* |
| 115 |
src:*/mylib/*=sanitize |
| 116 |
src:*/mylib/test.cc |
| 117 |
|
| 118 |
$ cat ignorelist2.txt |
| 119 |
# test.cc will be instrumented. |
| 120 |
src:* |
| 121 |
src:*/mylib/test.cc |
| 122 |
src:*/mylib/*=sanitize |
| 123 |
|
| 124 |
$ cat ignorelist3.txt |
| 125 |
# Type T will not be instrumented. |
| 126 |
type:* |
| 127 |
type:T=sanitize |
| 128 |
type:T |
| 129 |
|
| 130 |
$ cat ignorelist4.txt |
| 131 |
# Function `bad_bar` will be instrumented. |
| 132 |
# Function `good_bar` will not be instrumented. |
| 133 |
fun:* |
| 134 |
fun:*bar |
| 135 |
fun:bad_bar=sanitize |
| 136 |
|
| 137 |
Format |
| 138 |
====== |
| 139 |
|
| 140 |
Ignorelists consist of entries, optionally grouped into sections. Empty lines |
| 141 |
and lines starting with "#" are ignored. |
| 142 |
|
| 143 |
.. note:: |
| 144 |
|
| 145 |
Prior to Clang 18, section names and entries described below use a variant of |
| 146 |
regex where ``*`` is translated to ``.*``. Clang 18 (`D154014 |
| 147 |
<https://reviews.llvm.org/D154014>`) switches to glob and plans to remove |
| 148 |
regex support in Clang 19. |
| 149 |
|
| 150 |
For Clang 18, regex is supported if ``#!special-case-list-v1`` is the first |
| 151 |
line of the file. |
| 152 |
|
| 153 |
Many special case lists use ``.`` to indicate the literal character and do |
| 154 |
not use regex metacharacters such as ``(``, ``)``. They are unaffected by the |
| 155 |
regex to glob transition. For more details, see `this discourse post |
| 156 |
<https://discourse.llvm.org/t/use-glob-instead-of-regex-for-specialcaselists/71666>`_. |
| 157 |
|
| 158 |
Section names are globs written in square brackets that denote |
| 159 |
which sanitizer the following entries apply to. For example, ``[address]`` |
| 160 |
specifies AddressSanitizer while ``[{cfi-vcall,cfi-icall}]`` specifies Control |
| 161 |
Flow Integrity virtual and indirect call checking. Entries without a section |
| 162 |
will be placed under the ``[*]`` section applying to all enabled sanitizers. |
| 163 |
|
| 164 |
Entries contain an entity type, followed by a colon and a glob, |
| 165 |
specifying the names of the entities, optionally followed by an equals sign and |
| 166 |
a tool-specific category, e.g. ``fun:*ExampleFunc=example_category``. |
| 167 |
Two generic entity types are ``src`` and |
| 168 |
``fun``, which allow users to specify source files and functions, respectively. |
| 169 |
Some sanitizer tools may introduce custom entity types and categories - refer to |
| 170 |
tool-specific docs. |
| 171 |
|
| 172 |
.. code-block:: bash |
| 173 |
|
| 174 |
# Lines starting with # are ignored. |
| 175 |
# Turn off checks for the source file |
| 176 |
# Entries without sections are placed into [*] and apply to all sanitizers |
| 177 |
src:path/to/source/file.c |
| 178 |
src:*/source/file.c |
| 179 |
# Turn off checks for this main file, including files included by it. |
| 180 |
# Useful when the main file instead of an included file should be ignored. |
| 181 |
mainfile:file.c |
| 182 |
# Turn off checks for a particular functions (use mangled names): |
| 183 |
fun:_Z8MyFooBarv |
| 184 |
# Glob brace expansions and character ranges are supported |
| 185 |
fun:bad_{foo,bar} |
| 186 |
src:bad_source[1-9].c |
| 187 |
# "*" matches zero or more characters |
| 188 |
src:bad/sources/* |
| 189 |
fun:*BadFunction* |
| 190 |
# Specific sanitizer tools may introduce categories. |
| 191 |
src:/special/path/*=special_sources |
| 192 |
# Sections can be used to limit ignorelist entries to specific sanitizers |
| 193 |
[address] |
| 194 |
fun:*BadASanFunc* |
| 195 |
# Section names are globs |
| 196 |
[{cfi-vcall,cfi-icall}] |
| 197 |
fun:*BadCfiCall |
| 198 |
|
| 199 |
``mainfile`` is similar to applying ``-fno-sanitize=`` to a set of files but |
| 200 |
does not need plumbing into the build system. This works well for internal |
| 201 |
linkage functions but has a caveat for C++ vague linkage functions. |
| 202 |
|
| 203 |
C++ vague linkage functions (e.g. inline functions, template instantiations) are |
| 204 |
deduplicated at link time. A function (in an included file) ignored by a |
| 205 |
specific ``mainfile`` pattern may not be the prevailing copy picked by the |
| 206 |
linker. Therefore, using ``mainfile`` requires caution. It may still be useful, |
| 207 |
e.g. when patterns are picked in a way to ensure the prevailing one is ignored. |
| 208 |
(There is action-at-a-distance risk.) |
| 209 |
|
| 210 |
``mainfile`` can be useful enabling a ubsan check for a large code base when |
| 211 |
finding the direct stack frame triggering the failure for every failure is |
| 212 |
difficult. |