| 1 |
========================== |
| 2 |
UndefinedBehaviorSanitizer |
| 3 |
========================== |
| 4 |
|
| 5 |
.. contents:: |
| 6 |
:local: |
| 7 |
|
| 8 |
Introduction |
| 9 |
============ |
| 10 |
|
| 11 |
UndefinedBehaviorSanitizer (UBSan) is a fast undefined behavior detector. |
| 12 |
UBSan modifies the program at compile-time to catch various kinds of undefined |
| 13 |
behavior during program execution, for example: |
| 14 |
|
| 15 |
* Array subscript out of bounds, where the bounds can be statically determined |
| 16 |
* Bitwise shifts that are out of bounds for their data type |
| 17 |
* Dereferencing misaligned or null pointers |
| 18 |
* Signed integer overflow |
| 19 |
* Conversion to, from, or between floating-point types which would |
| 20 |
overflow the destination |
| 21 |
|
| 22 |
See the full list of available :ref:`checks <ubsan-checks>` below. |
| 23 |
|
| 24 |
UBSan has an optional run-time library which provides better error reporting. |
| 25 |
The checks have small runtime cost and no impact on address space layout or ABI. |
| 26 |
|
| 27 |
How to build |
| 28 |
============ |
| 29 |
|
| 30 |
Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>`_. |
| 31 |
|
| 32 |
Usage |
| 33 |
===== |
| 34 |
|
| 35 |
Use ``clang++`` to compile and link your program with the ``-fsanitize=undefined`` |
| 36 |
option. Make sure to use ``clang++`` (not ``ld``) as a linker, so that your |
| 37 |
executable is linked with proper UBSan runtime libraries, unless all enabled |
| 38 |
checks use trap mode. You can use ``clang`` instead of ``clang++`` if you're |
| 39 |
compiling/linking C code. |
| 40 |
|
| 41 |
.. code-block:: console |
| 42 |
|
| 43 |
% cat test.cc |
| 44 |
int main(int argc, char **argv) { |
| 45 |
int k = 0x7fffffff; |
| 46 |
k += argc; |
| 47 |
return 0; |
| 48 |
} |
| 49 |
% clang++ -fsanitize=undefined test.cc |
| 50 |
% ./a.out |
| 51 |
test.cc:3:5: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' |
| 52 |
|
| 53 |
You can use ``-fsanitize=...`` and ``-fno-sanitize=`` to enable and disable one |
| 54 |
check or one check group. For an individual check, the last option that enabling |
| 55 |
or disabling it wins. |
| 56 |
|
| 57 |
.. code-block:: console |
| 58 |
|
| 59 |
# Enable all checks in the "undefined" group, but disable "alignment". |
| 60 |
% clang -fsanitize=undefined -fno-sanitize=alignment a.c |
| 61 |
|
| 62 |
# Enable just "alignment". |
| 63 |
% clang -fsanitize=alignment a.c |
| 64 |
|
| 65 |
# The same. -fno-sanitize=undefined nullifies the previous -fsanitize=undefined. |
| 66 |
% clang -fsanitize=undefined -fno-sanitize=undefined -fsanitize=alignment a.c |
| 67 |
|
| 68 |
For most checks (:ref:`checks <ubsan-checks>`), the instrumented program prints |
| 69 |
a verbose error report and continues execution upon a failed check. |
| 70 |
You can use the following options to change the error reporting behavior: |
| 71 |
|
| 72 |
* ``-fno-sanitize-recover=...``: print a verbose error report and exit the program; |
| 73 |
* ``-fsanitize-trap=...``: execute a trap instruction (doesn't require UBSan |
| 74 |
run-time support). If the signal is not caught, the program will typically |
| 75 |
terminate due to a ``SIGILL`` or ``SIGTRAP`` signal. |
| 76 |
|
| 77 |
For example: |
| 78 |
|
| 79 |
.. code-block:: console |
| 80 |
|
| 81 |
% clang++ -fsanitize=signed-integer-overflow,null,alignment -fno-sanitize-recover=null -fsanitize-trap=alignment a.cc |
| 82 |
|
| 83 |
The program will continue execution after signed integer overflows, exit after |
| 84 |
the first invalid use of a null pointer, and trap after the first use of misaligned |
| 85 |
pointer. |
| 86 |
|
| 87 |
.. code-block:: console |
| 88 |
|
| 89 |
% clang++ -fsanitize=undefined -fsanitize-trap=all a.cc |
| 90 |
|
| 91 |
All checks in the "undefined" group are put into trap mode. Since no check |
| 92 |
needs run-time support, the UBSan run-time library it not linked. Note that |
| 93 |
some other sanitizers also support trap mode and ``-fsanitize-trap=all`` |
| 94 |
enables trap mode for them. |
| 95 |
|
| 96 |
.. code-block:: console |
| 97 |
|
| 98 |
% clang -fsanitize-trap=undefined -fsanitize-recover=all a.c |
| 99 |
|
| 100 |
``-fsanitize-trap=`` and ``-fsanitize-recover=`` are a no-op in the absence of |
| 101 |
a ``-fsanitize=`` option. There is no unused command line option warning. |
| 102 |
|
| 103 |
.. _ubsan-checks: |
| 104 |
|
| 105 |
Available checks |
| 106 |
================ |
| 107 |
|
| 108 |
Available checks are: |
| 109 |
|
| 110 |
- ``-fsanitize=alignment``: Use of a misaligned pointer or creation |
| 111 |
of a misaligned reference. Also sanitizes assume_aligned-like attributes. |
| 112 |
- ``-fsanitize=bool``: Load of a ``bool`` value which is neither |
| 113 |
``true`` nor ``false``. |
| 114 |
- ``-fsanitize=builtin``: Passing invalid values to compiler builtins. |
| 115 |
- ``-fsanitize=bounds``: Out of bounds array indexing, in cases |
| 116 |
where the array bound can be statically determined. The check includes |
| 117 |
``-fsanitize=array-bounds`` and ``-fsanitize=local-bounds``. Note that |
| 118 |
``-fsanitize=local-bounds`` is not included in ``-fsanitize=undefined``. |
| 119 |
- ``-fsanitize=enum``: Load of a value of an enumerated type which |
| 120 |
is not in the range of representable values for that enumerated |
| 121 |
type. |
| 122 |
- ``-fsanitize=float-cast-overflow``: Conversion to, from, or |
| 123 |
between floating-point types which would overflow the |
| 124 |
destination. Because the range of representable values for all |
| 125 |
floating-point types supported by Clang is [-inf, +inf], the only |
| 126 |
cases detected are conversions from floating point to integer types. |
| 127 |
- ``-fsanitize=float-divide-by-zero``: Floating point division by |
| 128 |
zero. This is undefined per the C and C++ standards, but is defined |
| 129 |
by Clang (and by ISO/IEC/IEEE 60559 / IEEE 754) as producing either an |
| 130 |
infinity or NaN value, so is not included in ``-fsanitize=undefined``. |
| 131 |
- ``-fsanitize=function``: Indirect call of a function through a |
| 132 |
function pointer of the wrong type. |
| 133 |
- ``-fsanitize=implicit-unsigned-integer-truncation``, |
| 134 |
``-fsanitize=implicit-signed-integer-truncation``: Implicit conversion from |
| 135 |
integer of larger bit width to smaller bit width, if that results in data |
| 136 |
loss. That is, if the demoted value, after casting back to the original |
| 137 |
width, is not equal to the original value before the downcast. |
| 138 |
The ``-fsanitize=implicit-unsigned-integer-truncation`` handles conversions |
| 139 |
between two ``unsigned`` types, while |
| 140 |
``-fsanitize=implicit-signed-integer-truncation`` handles the rest of the |
| 141 |
conversions - when either one, or both of the types are signed. |
| 142 |
Issues caught by these sanitizers are not undefined behavior, |
| 143 |
but are often unintentional. |
| 144 |
- ``-fsanitize=implicit-integer-sign-change``: Implicit conversion between |
| 145 |
integer types, if that changes the sign of the value. That is, if the |
| 146 |
original value was negative and the new value is positive (or zero), |
| 147 |
or the original value was positive, and the new value is negative. |
| 148 |
Issues caught by this sanitizer are not undefined behavior, |
| 149 |
but are often unintentional. |
| 150 |
- ``-fsanitize=integer-divide-by-zero``: Integer division by zero. |
| 151 |
- ``-fsanitize=implicit-bitfield-conversion``: Implicit conversion from |
| 152 |
integer of larger bit width to smaller bitfield, if that results in data |
| 153 |
loss. This includes unsigned/signed truncations and sign changes, similarly |
| 154 |
to how the ``-fsanitize=implicit-integer-conversion`` group works, but |
| 155 |
explicitly for bitfields. |
| 156 |
- ``-fsanitize=nonnull-attribute``: Passing null pointer as a function |
| 157 |
parameter which is declared to never be null. |
| 158 |
- ``-fsanitize=null``: Use of a null pointer or creation of a null |
| 159 |
reference. |
| 160 |
- ``-fsanitize=nullability-arg``: Passing null as a function parameter |
| 161 |
which is annotated with ``_Nonnull``. |
| 162 |
- ``-fsanitize=nullability-assign``: Assigning null to an lvalue which |
| 163 |
is annotated with ``_Nonnull``. |
| 164 |
- ``-fsanitize=nullability-return``: Returning null from a function with |
| 165 |
a return type annotated with ``_Nonnull``. |
| 166 |
- ``-fsanitize=objc-cast``: Invalid implicit cast of an ObjC object pointer |
| 167 |
to an incompatible type. This is often unintentional, but is not undefined |
| 168 |
behavior, therefore the check is not a part of the ``undefined`` group. |
| 169 |
Currently only supported on Darwin. |
| 170 |
- ``-fsanitize=object-size``: An attempt to potentially use bytes which |
| 171 |
the optimizer can determine are not part of the object being accessed. |
| 172 |
This will also detect some types of undefined behavior that may not |
| 173 |
directly access memory, but are provably incorrect given the size of |
| 174 |
the objects involved, such as invalid downcasts and calling methods on |
| 175 |
invalid pointers. These checks are made in terms of |
| 176 |
``__builtin_object_size``, and consequently may be able to detect more |
| 177 |
problems at higher optimization levels. |
| 178 |
- ``-fsanitize=pointer-overflow``: Performing pointer arithmetic which |
| 179 |
overflows, or where either the old or new pointer value is a null pointer |
| 180 |
(or in C, when they both are). |
| 181 |
- ``-fsanitize=return``: In C++, reaching the end of a |
| 182 |
value-returning function without returning a value. |
| 183 |
- ``-fsanitize=returns-nonnull-attribute``: Returning null pointer |
| 184 |
from a function which is declared to never return null. |
| 185 |
- ``-fsanitize=shift``: Shift operators where the amount shifted is |
| 186 |
greater or equal to the promoted bit-width of the left hand side |
| 187 |
or less than zero, or where the left hand side is negative. For a |
| 188 |
signed left shift, also checks for signed overflow in C, and for |
| 189 |
unsigned overflow in C++. You can use ``-fsanitize=shift-base`` or |
| 190 |
``-fsanitize=shift-exponent`` to check only left-hand side or |
| 191 |
right-hand side of shift operation, respectively. |
| 192 |
- ``-fsanitize=unsigned-shift-base``: check that an unsigned left-hand side of |
| 193 |
a left shift operation doesn't overflow. Issues caught by this sanitizer are |
| 194 |
not undefined behavior, but are often unintentional. |
| 195 |
- ``-fsanitize=signed-integer-overflow``: Signed integer overflow, where the |
| 196 |
result of a signed integer computation cannot be represented in its type. |
| 197 |
This includes all the checks covered by ``-ftrapv``, as well as checks for |
| 198 |
signed division overflow (``INT_MIN/-1``). Note that checks are still |
| 199 |
added even when ``-fwrapv`` is enabled. This sanitizer does not check for |
| 200 |
lossy implicit conversions performed before the computation (see |
| 201 |
``-fsanitize=implicit-integer-conversion``). Both of these two issues are handled |
| 202 |
by ``-fsanitize=implicit-integer-conversion`` group of checks. |
| 203 |
- ``-fsanitize=unreachable``: If control flow reaches an unreachable |
| 204 |
program point. |
| 205 |
- ``-fsanitize=unsigned-integer-overflow``: Unsigned integer overflow, where |
| 206 |
the result of an unsigned integer computation cannot be represented in its |
| 207 |
type. Unlike signed integer overflow, this is not undefined behavior, but |
| 208 |
it is often unintentional. This sanitizer does not check for lossy implicit |
| 209 |
conversions performed before such a computation |
| 210 |
(see ``-fsanitize=implicit-integer-conversion``). |
| 211 |
- ``-fsanitize=vla-bound``: A variable-length array whose bound |
| 212 |
does not evaluate to a positive value. |
| 213 |
- ``-fsanitize=vptr``: Use of an object whose vptr indicates that it is of |
| 214 |
the wrong dynamic type, or that its lifetime has not begun or has ended. |
| 215 |
Incompatible with ``-fno-rtti``. Link must be performed by ``clang++``, not |
| 216 |
``clang``, to make sure C++-specific parts of the runtime library and C++ |
| 217 |
standard libraries are present. |
| 218 |
|
| 219 |
You can also use the following check groups: |
| 220 |
- ``-fsanitize=undefined``: All of the checks listed above other than |
| 221 |
``float-divide-by-zero``, ``unsigned-integer-overflow``, |
| 222 |
``implicit-conversion``, ``local-bounds`` and the ``nullability-*`` group |
| 223 |
of checks. |
| 224 |
- ``-fsanitize=undefined-trap``: Deprecated alias of |
| 225 |
``-fsanitize=undefined``. |
| 226 |
- ``-fsanitize=implicit-integer-truncation``: Catches lossy integral |
| 227 |
conversions. Enables ``implicit-signed-integer-truncation`` and |
| 228 |
``implicit-unsigned-integer-truncation``. |
| 229 |
- ``-fsanitize=implicit-integer-arithmetic-value-change``: Catches implicit |
| 230 |
conversions that change the arithmetic value of the integer. Enables |
| 231 |
``implicit-signed-integer-truncation`` and ``implicit-integer-sign-change``. |
| 232 |
- ``-fsanitize=implicit-integer-conversion``: Checks for suspicious |
| 233 |
behavior of implicit integer conversions. Enables |
| 234 |
``implicit-unsigned-integer-truncation``, |
| 235 |
``implicit-signed-integer-truncation``, and |
| 236 |
``implicit-integer-sign-change``. |
| 237 |
- ``-fsanitize=implicit-conversion``: Checks for suspicious |
| 238 |
behavior of implicit conversions. Enables |
| 239 |
``implicit-integer-conversion``, and |
| 240 |
``implicit-bitfield-conversion``. |
| 241 |
- ``-fsanitize=integer``: Checks for undefined or suspicious integer |
| 242 |
behavior (e.g. unsigned integer overflow). |
| 243 |
Enables ``signed-integer-overflow``, ``unsigned-integer-overflow``, |
| 244 |
``shift``, ``integer-divide-by-zero``, |
| 245 |
``implicit-unsigned-integer-truncation``, |
| 246 |
``implicit-signed-integer-truncation``, and |
| 247 |
``implicit-integer-sign-change``. |
| 248 |
- ``-fsanitize=nullability``: Enables ``nullability-arg``, |
| 249 |
``nullability-assign``, and ``nullability-return``. While violating |
| 250 |
nullability does not have undefined behavior, it is often unintentional, |
| 251 |
so UBSan offers to catch it. |
| 252 |
|
| 253 |
Volatile |
| 254 |
-------- |
| 255 |
|
| 256 |
The ``null``, ``alignment``, ``object-size``, ``local-bounds``, and ``vptr`` checks do not apply |
| 257 |
to pointers to types with the ``volatile`` qualifier. |
| 258 |
|
| 259 |
Minimal Runtime |
| 260 |
=============== |
| 261 |
|
| 262 |
There is a minimal UBSan runtime available suitable for use in production |
| 263 |
environments. This runtime has a small attack surface. It only provides very |
| 264 |
basic issue logging and deduplication, and does not support ``-fsanitize=vptr`` |
| 265 |
checking. |
| 266 |
|
| 267 |
To use the minimal runtime, add ``-fsanitize-minimal-runtime`` to the clang |
| 268 |
command line options. For example, if you're used to compiling with |
| 269 |
``-fsanitize=undefined``, you could enable the minimal runtime with |
| 270 |
``-fsanitize=undefined -fsanitize-minimal-runtime``. |
| 271 |
|
| 272 |
Stack traces and report symbolization |
| 273 |
===================================== |
| 274 |
If you want UBSan to print symbolized stack trace for each error report, you |
| 275 |
will need to: |
| 276 |
|
| 277 |
#. Compile with ``-g`` and ``-fno-omit-frame-pointer`` to get proper debug |
| 278 |
information in your binary. |
| 279 |
#. Run your program with environment variable |
| 280 |
``UBSAN_OPTIONS=print_stacktrace=1``. |
| 281 |
#. Make sure ``llvm-symbolizer`` binary is in ``PATH``. |
| 282 |
|
| 283 |
Logging |
| 284 |
======= |
| 285 |
|
| 286 |
The default log file for diagnostics is "stderr". To log diagnostics to another |
| 287 |
file, you can set ``UBSAN_OPTIONS=log_path=...``. |
| 288 |
|
| 289 |
Silencing Unsigned Integer Overflow |
| 290 |
=================================== |
| 291 |
To silence reports from unsigned integer overflow, you can set |
| 292 |
``UBSAN_OPTIONS=silence_unsigned_overflow=1``. This feature, combined with |
| 293 |
``-fsanitize-recover=unsigned-integer-overflow``, is particularly useful for |
| 294 |
providing fuzzing signal without blowing up logs. |
| 295 |
|
| 296 |
Issue Suppression |
| 297 |
================= |
| 298 |
|
| 299 |
UndefinedBehaviorSanitizer is not expected to produce false positives. |
| 300 |
If you see one, look again; most likely it is a true positive! |
| 301 |
|
| 302 |
Disabling Instrumentation with ``__attribute__((no_sanitize("undefined")))`` |
| 303 |
---------------------------------------------------------------------------- |
| 304 |
|
| 305 |
You disable UBSan checks for particular functions with |
| 306 |
``__attribute__((no_sanitize("undefined")))``. You can use all values of |
| 307 |
``-fsanitize=`` flag in this attribute, e.g. if your function deliberately |
| 308 |
contains possible signed integer overflow, you can use |
| 309 |
``__attribute__((no_sanitize("signed-integer-overflow")))``. |
| 310 |
|
| 311 |
This attribute may not be |
| 312 |
supported by other compilers, so consider using it together with |
| 313 |
``#if defined(__clang__)``. |
| 314 |
|
| 315 |
Suppressing Errors in Recompiled Code (Ignorelist) |
| 316 |
-------------------------------------------------- |
| 317 |
|
| 318 |
UndefinedBehaviorSanitizer supports ``src`` and ``fun`` entity types in |
| 319 |
:doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports |
| 320 |
in the specified source files or functions. |
| 321 |
|
| 322 |
Runtime suppressions |
| 323 |
-------------------- |
| 324 |
|
| 325 |
Sometimes you can suppress UBSan error reports for specific files, functions, |
| 326 |
or libraries without recompiling the code. You need to pass a path to |
| 327 |
suppression file in a ``UBSAN_OPTIONS`` environment variable. |
| 328 |
|
| 329 |
.. code-block:: bash |
| 330 |
|
| 331 |
UBSAN_OPTIONS=suppressions=MyUBSan.supp |
| 332 |
|
| 333 |
You need to specify a :ref:`check <ubsan-checks>` you are suppressing and the |
| 334 |
bug location. For example: |
| 335 |
|
| 336 |
.. code-block:: bash |
| 337 |
|
| 338 |
signed-integer-overflow:file-with-known-overflow.cpp |
| 339 |
alignment:function_doing_unaligned_access |
| 340 |
vptr:shared_object_with_vptr_failures.so |
| 341 |
|
| 342 |
There are several limitations: |
| 343 |
|
| 344 |
* Sometimes your binary must have enough debug info and/or symbol table, so |
| 345 |
that the runtime could figure out source file or function name to match |
| 346 |
against the suppression. |
| 347 |
* It is only possible to suppress recoverable checks. For the example above, |
| 348 |
you can additionally pass |
| 349 |
``-fsanitize-recover=signed-integer-overflow,alignment,vptr``, although |
| 350 |
most of UBSan checks are recoverable by default. |
| 351 |
* Check groups (like ``undefined``) can't be used in suppressions file, only |
| 352 |
fine-grained checks are supported. |
| 353 |
|
| 354 |
Supported Platforms |
| 355 |
=================== |
| 356 |
|
| 357 |
UndefinedBehaviorSanitizer is supported on the following operating systems: |
| 358 |
|
| 359 |
* Android |
| 360 |
* Linux |
| 361 |
* NetBSD |
| 362 |
* FreeBSD |
| 363 |
* OpenBSD |
| 364 |
* macOS |
| 365 |
* Windows |
| 366 |
|
| 367 |
The runtime library is relatively portable and platform independent. If the OS |
| 368 |
you need is not listed above, UndefinedBehaviorSanitizer may already work for |
| 369 |
it, or could be made to work with a minor porting effort. |
| 370 |
|
| 371 |
Current Status |
| 372 |
============== |
| 373 |
|
| 374 |
UndefinedBehaviorSanitizer is available on selected platforms starting from LLVM |
| 375 |
3.3. The test suite is integrated into the CMake build and can be run with |
| 376 |
``check-ubsan`` command. |
| 377 |
|
| 378 |
Additional Configuration |
| 379 |
======================== |
| 380 |
|
| 381 |
UndefinedBehaviorSanitizer adds static check data for each check unless it is |
| 382 |
in trap mode. This check data includes the full file name. The option |
| 383 |
``-fsanitize-undefined-strip-path-components=N`` can be used to trim this |
| 384 |
information. If ``N`` is positive, file information emitted by |
| 385 |
UndefinedBehaviorSanitizer will drop the first ``N`` components from the file |
| 386 |
path. If ``N`` is negative, the last ``N`` components will be kept. |
| 387 |
|
| 388 |
Example |
| 389 |
------- |
| 390 |
|
| 391 |
For a file called ``/code/library/file.cpp``, here is what would be emitted: |
| 392 |
|
| 393 |
* Default (No flag, or ``-fsanitize-undefined-strip-path-components=0``): ``/code/library/file.cpp`` |
| 394 |
* ``-fsanitize-undefined-strip-path-components=1``: ``code/library/file.cpp`` |
| 395 |
* ``-fsanitize-undefined-strip-path-components=2``: ``library/file.cpp`` |
| 396 |
* ``-fsanitize-undefined-strip-path-components=-1``: ``file.cpp`` |
| 397 |
* ``-fsanitize-undefined-strip-path-components=-2``: ``library/file.cpp`` |
| 398 |
|
| 399 |
More Information |
| 400 |
================ |
| 401 |
|
| 402 |
* From Oracle blog, including a discussion of error messages: |
| 403 |
`Improving Application Security with UndefinedBehaviorSanitizer (UBSan) and GCC |
| 404 |
<https://blogs.oracle.com/linux/improving-application-security-with-undefinedbehaviorsanitizer-ubsan-and-gcc>`_ |
| 405 |
* From LLVM project blog: |
| 406 |
`What Every C Programmer Should Know About Undefined Behavior |
| 407 |
<http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html>`_ |
| 408 |
* From John Regehr's *Embedded in Academia* blog: |
| 409 |
`A Guide to Undefined Behavior in C and C++ |
| 410 |
<https://blog.regehr.org/archives/213>`_ |