1 ==========================
2 UndefinedBehaviorSanitizer
3 ==========================
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:
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
22 See the full list of available :ref:`checks <ubsan-checks>` below.
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.
30 Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>`_.
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.
41 .. code-block:: console
44 int main(int argc, char **argv) {
49 % clang++ -fsanitize=undefined test.cc
51 test.cc:3:5: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
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
57 .. code-block:: console
59 # Enable all checks in the "undefined" group, but disable "alignment".
60 % clang -fsanitize=undefined -fno-sanitize=alignment a.c
62 # Enable just "alignment".
63 % clang -fsanitize=alignment a.c
65 # The same. -fno-sanitize=undefined nullifies the previous -fsanitize=undefined.
66 % clang -fsanitize=undefined -fno-sanitize=undefined -fsanitize=alignment a.c
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:
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.
79 .. code-block:: console
81 % clang++ -fsanitize=signed-integer-overflow,null,alignment -fno-sanitize-recover=null -fsanitize-trap=alignment a.cc
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
87 .. code-block:: console
89 % clang++ -fsanitize=undefined -fsanitize-trap=all a.cc
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.
96 .. code-block:: console
98 % clang -fsanitize-trap=undefined -fsanitize-recover=all a.c
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.
108 Available checks are:
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
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
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
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.
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
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.
256 The ``null``, ``alignment``, ``object-size``, ``local-bounds``, and ``vptr`` checks do not apply
257 to pointers to types with the ``volatile`` qualifier.
264 There is a minimal UBSan runtime available suitable for use in production
265 environments. This runtime has a small attack surface. It only provides very
266 basic issue logging and deduplication, and does not support ``-fsanitize=vptr``
269 To use the minimal runtime, add ``-fsanitize-minimal-runtime`` to the clang
270 command line options. For example, if you're used to compiling with
271 ``-fsanitize=undefined``, you could enable the minimal runtime with
272 ``-fsanitize=undefined -fsanitize-minimal-runtime``.
274 Stack traces and report symbolization
275 =====================================
276 If you want UBSan to print symbolized stack trace for each error report, you
279 #. Compile with ``-g`` and ``-fno-omit-frame-pointer`` to get proper debug
280 information in your binary.
281 #. Run your program with environment variable
282 ``UBSAN_OPTIONS=print_stacktrace=1``.
283 #. Make sure ``llvm-symbolizer`` binary is in ``PATH``.
288 The default log file for diagnostics is "stderr". To log diagnostics to another
289 file, you can set ``UBSAN_OPTIONS=log_path=...``.
291 Silencing Unsigned Integer Overflow
292 ===================================
293 To silence reports from unsigned integer overflow, you can set
294 ``UBSAN_OPTIONS=silence_unsigned_overflow=1``. This feature, combined with
295 ``-fsanitize-recover=unsigned-integer-overflow``, is particularly useful for
296 providing fuzzing signal without blowing up logs.
298 Disabling instrumentation for common overflow patterns
299 ------------------------------------------------------
301 There are certain overflow-dependent or overflow-prone code patterns which
302 produce a lot of noise for integer overflow/truncation sanitizers. Negated
303 unsigned constants, post-decrements in a while loop condition and simple
304 overflow checks are accepted and pervasive code patterns. However, the signal
305 received from sanitizers instrumenting these code patterns may be too noisy for
306 some projects. To disable instrumentation for these common patterns one should
307 use ``-fsanitize-undefined-ignore-overflow-pattern=``.
309 Currently, this option supports three overflow-dependent code idioms:
311 ``negated-unsigned-const``
315 /// -fsanitize-undefined-ignore-overflow-pattern=negated-unsigned-const
316 unsigned long foo = -1UL; // No longer causes a negation overflow warning
317 unsigned long bar = -2UL; // and so on...
319 ``unsigned-post-decr-while``
323 /// -fsanitize-undefined-ignore-overflow-pattern=unsigned-post-decr-while
324 unsigned char count = 16;
325 while (count--) { /* ... */ } // No longer causes unsigned-integer-overflow sanitizer to trip
327 ``add-signed-overflow-test,add-unsigned-overflow-test``
331 /// -fsanitize-undefined-ignore-overflow-pattern=add-(signed|unsigned)-overflow-test
332 if (base + offset < base) { /* ... */ } // The pattern of `a + b < a`, and other re-orderings,
333 // won't be instrumented (signed or unsigned types)
335 .. list-table:: Overflow Pattern Types
341 * - negated-unsigned-const
342 - unsigned-integer-overflow
343 * - unsigned-post-decr-while
344 - unsigned-integer-overflow
345 * - add-unsigned-overflow-test
346 - unsigned-integer-overflow
347 * - add-signed-overflow-test
348 - signed-integer-overflow
352 Note: ``add-signed-overflow-test`` suppresses only the check for Undefined
353 Behavior. Eager Undefined Behavior optimizations are still possible. One may
354 remedy this with ``-fwrapv`` or ``-fno-strict-overflow``.
356 You can enable all exclusions with
357 ``-fsanitize-undefined-ignore-overflow-pattern=all`` or disable all exclusions
358 with ``-fsanitize-undefined-ignore-overflow-pattern=none``. If
359 ``-fsanitize-undefined-ignore-overflow-pattern`` is not specified ``none`` is
360 implied. Specifying ``none`` alongside other values also implies ``none`` as
361 ``none`` has precedence over other values -- including ``all``.
366 UndefinedBehaviorSanitizer is not expected to produce false positives.
367 If you see one, look again; most likely it is a true positive!
369 Disabling Instrumentation with ``__attribute__((no_sanitize("undefined")))``
370 ----------------------------------------------------------------------------
372 You disable UBSan checks for particular functions with
373 ``__attribute__((no_sanitize("undefined")))``. You can use all values of
374 ``-fsanitize=`` flag in this attribute, e.g. if your function deliberately
375 contains possible signed integer overflow, you can use
376 ``__attribute__((no_sanitize("signed-integer-overflow")))``.
378 This attribute may not be
379 supported by other compilers, so consider using it together with
380 ``#if defined(__clang__)``.
382 Suppressing Errors in Recompiled Code (Ignorelist)
383 --------------------------------------------------
385 UndefinedBehaviorSanitizer supports ``src`` and ``fun`` entity types in
386 :doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports
387 in the specified source files or functions.
392 Sometimes you can suppress UBSan error reports for specific files, functions,
393 or libraries without recompiling the code. You need to pass a path to
394 suppression file in a ``UBSAN_OPTIONS`` environment variable.
398 UBSAN_OPTIONS=suppressions=MyUBSan.supp
400 You need to specify a :ref:`check <ubsan-checks>` you are suppressing and the
401 bug location. For example:
405 signed-integer-overflow:file-with-known-overflow.cpp
406 alignment:function_doing_unaligned_access
407 vptr:shared_object_with_vptr_failures.so
409 There are several limitations:
411 * Sometimes your binary must have enough debug info and/or symbol table, so
412 that the runtime could figure out source file or function name to match
413 against the suppression.
414 * It is only possible to suppress recoverable checks. For the example above,
415 you can additionally pass
416 ``-fsanitize-recover=signed-integer-overflow,alignment,vptr``, although
417 most of UBSan checks are recoverable by default.
418 * Check groups (like ``undefined``) can't be used in suppressions file, only
419 fine-grained checks are supported.
421 Security Considerations
422 =======================
424 UndefinedBehaviorSanitizer's runtime is meant for testing purposes and its usage
425 in production environment should be carefully considered from security
426 perspective as it may compromise the security of the resulting executable.
427 For security-sensitive applications consider using :ref:`Minimal Runtime
428 <minimal-runtime>` or trap mode for all checks.
433 UndefinedBehaviorSanitizer is supported on the following operating systems:
443 The runtime library is relatively portable and platform independent. If the OS
444 you need is not listed above, UndefinedBehaviorSanitizer may already work for
445 it, or could be made to work with a minor porting effort.
450 UndefinedBehaviorSanitizer is available on selected platforms starting from LLVM
451 3.3. The test suite is integrated into the CMake build and can be run with
452 ``check-ubsan`` command.
454 Additional Configuration
455 ========================
457 UndefinedBehaviorSanitizer adds static check data for each check unless it is
458 in trap mode. This check data includes the full file name. The option
459 ``-fsanitize-undefined-strip-path-components=N`` can be used to trim this
460 information. If ``N`` is positive, file information emitted by
461 UndefinedBehaviorSanitizer will drop the first ``N`` components from the file
462 path. If ``N`` is negative, the last ``N`` components will be kept.
467 For a file called ``/code/library/file.cpp``, here is what would be emitted:
469 * Default (No flag, or ``-fsanitize-undefined-strip-path-components=0``): ``/code/library/file.cpp``
470 * ``-fsanitize-undefined-strip-path-components=1``: ``code/library/file.cpp``
471 * ``-fsanitize-undefined-strip-path-components=2``: ``library/file.cpp``
472 * ``-fsanitize-undefined-strip-path-components=-1``: ``file.cpp``
473 * ``-fsanitize-undefined-strip-path-components=-2``: ``library/file.cpp``
478 * From Oracle blog, including a discussion of error messages:
479 `Improving Application Security with UndefinedBehaviorSanitizer (UBSan) and GCC
480 <https://blogs.oracle.com/linux/improving-application-security-with-undefinedbehaviorsanitizer-ubsan-and-gcc>`_
481 * From LLVM project blog:
482 `What Every C Programmer Should Know About Undefined Behavior
483 <http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html>`_
484 * From John Regehr's *Embedded in Academia* blog:
485 `A Guide to Undefined Behavior in C and C++
486 <https://blog.regehr.org/archives/213>`_