UndefinedBehaviorSanitizer.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ==========================
  2. UndefinedBehaviorSanitizer
  3. ==========================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. UndefinedBehaviorSanitizer (UBSan) is a fast undefined behavior detector.
  9. UBSan modifies the program at compile-time to catch various kinds of undefined
  10. behavior during program execution, for example:
  11. * Using misaligned or null pointer
  12. * Signed integer overflow
  13. * Conversion to, from, or between floating-point types which would
  14. overflow the destination
  15. See the full list of available :ref:`checks <ubsan-checks>` below.
  16. UBSan has an optional run-time library which provides better error reporting.
  17. The checks have small runtime cost and no impact on address space layout or ABI.
  18. How to build
  19. ============
  20. Build LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_.
  21. Usage
  22. =====
  23. Use ``clang++`` to compile and link your program with ``-fsanitize=undefined``
  24. flag. Make sure to use ``clang++`` (not ``ld``) as a linker, so that your
  25. executable is linked with proper UBSan runtime libraries. You can use ``clang``
  26. instead of ``clang++`` if you're compiling/linking C code.
  27. .. code-block:: console
  28. % cat test.cc
  29. int main(int argc, char **argv) {
  30. int k = 0x7fffffff;
  31. k += argc;
  32. return 0;
  33. }
  34. % clang++ -fsanitize=undefined test.cc
  35. % ./a.out
  36. test.cc:3:5: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
  37. You can enable only a subset of :ref:`checks <ubsan-checks>` offered by UBSan,
  38. and define the desired behavior for each kind of check:
  39. * ``-fsanitize=...``: print a verbose error report and continue execution (default);
  40. * ``-fno-sanitize-recover=...``: print a verbose error report and exit the program;
  41. * ``-fsanitize-trap=...``: execute a trap instruction (doesn't require UBSan run-time support).
  42. For example if you compile/link your program as:
  43. .. code-block:: console
  44. % clang++ -fsanitize=signed-integer-overflow,null,alignment -fno-sanitize-recover=null -fsanitize-trap=alignment
  45. the program will continue execution after signed integer overflows, exit after
  46. the first invalid use of a null pointer, and trap after the first use of misaligned
  47. pointer.
  48. .. _ubsan-checks:
  49. Available checks
  50. ================
  51. Available checks are:
  52. - ``-fsanitize=alignment``: Use of a misaligned pointer or creation
  53. of a misaligned reference.
  54. - ``-fsanitize=bool``: Load of a ``bool`` value which is neither
  55. ``true`` nor ``false``.
  56. - ``-fsanitize=builtin``: Passing invalid values to compiler builtins.
  57. - ``-fsanitize=bounds``: Out of bounds array indexing, in cases
  58. where the array bound can be statically determined.
  59. - ``-fsanitize=enum``: Load of a value of an enumerated type which
  60. is not in the range of representable values for that enumerated
  61. type.
  62. - ``-fsanitize=float-cast-overflow``: Conversion to, from, or
  63. between floating-point types which would overflow the
  64. destination.
  65. - ``-fsanitize=float-divide-by-zero``: Floating point division by
  66. zero.
  67. - ``-fsanitize=function``: Indirect call of a function through a
  68. function pointer of the wrong type (Darwin/Linux, C++ and x86/x86_64
  69. only).
  70. - ``-fsanitize=integer-divide-by-zero``: Integer division by zero.
  71. - ``-fsanitize=nonnull-attribute``: Passing null pointer as a function
  72. parameter which is declared to never be null.
  73. - ``-fsanitize=null``: Use of a null pointer or creation of a null
  74. reference.
  75. - ``-fsanitize=nullability-arg``: Passing null as a function parameter
  76. which is annotated with ``_Nonnull``.
  77. - ``-fsanitize=nullability-assign``: Assigning null to an lvalue which
  78. is annotated with ``_Nonnull``.
  79. - ``-fsanitize=nullability-return``: Returning null from a function with
  80. a return type annotated with ``_Nonnull``.
  81. - ``-fsanitize=object-size``: An attempt to potentially use bytes which
  82. the optimizer can determine are not part of the object being accessed.
  83. This will also detect some types of undefined behavior that may not
  84. directly access memory, but are provably incorrect given the size of
  85. the objects involved, such as invalid downcasts and calling methods on
  86. invalid pointers. These checks are made in terms of
  87. ``__builtin_object_size``, and consequently may be able to detect more
  88. problems at higher optimization levels.
  89. - ``-fsanitize=pointer-overflow``: Performing pointer arithmetic which
  90. overflows.
  91. - ``-fsanitize=return``: In C++, reaching the end of a
  92. value-returning function without returning a value.
  93. - ``-fsanitize=returns-nonnull-attribute``: Returning null pointer
  94. from a function which is declared to never return null.
  95. - ``-fsanitize=shift``: Shift operators where the amount shifted is
  96. greater or equal to the promoted bit-width of the left hand side
  97. or less than zero, or where the left hand side is negative. For a
  98. signed left shift, also checks for signed overflow in C, and for
  99. unsigned overflow in C++. You can use ``-fsanitize=shift-base`` or
  100. ``-fsanitize=shift-exponent`` to check only left-hand side or
  101. right-hand side of shift operation, respectively.
  102. - ``-fsanitize=signed-integer-overflow``: Signed integer overflow,
  103. including all the checks added by ``-ftrapv``, and checking for
  104. overflow in signed division (``INT_MIN / -1``).
  105. - ``-fsanitize=unreachable``: If control flow reaches
  106. ``__builtin_unreachable``.
  107. - ``-fsanitize=unsigned-integer-overflow``: Unsigned integer
  108. overflows. Note that unlike signed integer overflow, unsigned integer
  109. is not undefined behavior. However, while it has well-defined semantics,
  110. it is often unintentional, so UBSan offers to catch it.
  111. - ``-fsanitize=vla-bound``: A variable-length array whose bound
  112. does not evaluate to a positive value.
  113. - ``-fsanitize=vptr``: Use of an object whose vptr indicates that it is of
  114. the wrong dynamic type, or that its lifetime has not begun or has ended.
  115. Incompatible with ``-fno-rtti``. Link must be performed by ``clang++``, not
  116. ``clang``, to make sure C++-specific parts of the runtime library and C++
  117. standard libraries are present.
  118. You can also use the following check groups:
  119. - ``-fsanitize=undefined``: All of the checks listed above other than
  120. ``unsigned-integer-overflow`` and the ``nullability-*`` checks.
  121. - ``-fsanitize=undefined-trap``: Deprecated alias of
  122. ``-fsanitize=undefined``.
  123. - ``-fsanitize=integer``: Checks for undefined or suspicious integer
  124. behavior (e.g. unsigned integer overflow).
  125. - ``-fsanitize=nullability``: Enables ``nullability-arg``,
  126. ``nullability-assign``, and ``nullability-return``. While violating
  127. nullability does not have undefined behavior, it is often unintentional,
  128. so UBSan offers to catch it.
  129. Volatile
  130. --------
  131. The ``null``, ``alignment``, ``object-size``, and ``vptr`` checks do not apply
  132. to pointers to types with the ``volatile`` qualifier.
  133. Minimal Runtime
  134. ===============
  135. There is a minimal UBSan runtime available suitable for use in production
  136. environments. This runtime has a small attack surface. It only provides very
  137. basic issue logging and deduplication, and does not support ``-fsanitize=vptr``
  138. checking.
  139. To use the minimal runtime, add ``-fsanitize-minimal-runtime`` to the clang
  140. command line options. For example, if you're used to compiling with
  141. ``-fsanitize=undefined``, you could enable the minimal runtime with
  142. ``-fsanitize=undefined -fsanitize-minimal-runtime``.
  143. Stack traces and report symbolization
  144. =====================================
  145. If you want UBSan to print symbolized stack trace for each error report, you
  146. will need to:
  147. #. Compile with ``-g`` and ``-fno-omit-frame-pointer`` to get proper debug
  148. information in your binary.
  149. #. Run your program with environment variable
  150. ``UBSAN_OPTIONS=print_stacktrace=1``.
  151. #. Make sure ``llvm-symbolizer`` binary is in ``PATH``.
  152. Issue Suppression
  153. =================
  154. UndefinedBehaviorSanitizer is not expected to produce false positives.
  155. If you see one, look again; most likely it is a true positive!
  156. Disabling Instrumentation with ``__attribute__((no_sanitize("undefined")))``
  157. ----------------------------------------------------------------------------
  158. You disable UBSan checks for particular functions with
  159. ``__attribute__((no_sanitize("undefined")))``. You can use all values of
  160. ``-fsanitize=`` flag in this attribute, e.g. if your function deliberately
  161. contains possible signed integer overflow, you can use
  162. ``__attribute__((no_sanitize("signed-integer-overflow")))``.
  163. This attribute may not be
  164. supported by other compilers, so consider using it together with
  165. ``#if defined(__clang__)``.
  166. Suppressing Errors in Recompiled Code (Blacklist)
  167. -------------------------------------------------
  168. UndefinedBehaviorSanitizer supports ``src`` and ``fun`` entity types in
  169. :doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports
  170. in the specified source files or functions.
  171. Runtime suppressions
  172. --------------------
  173. Sometimes you can suppress UBSan error reports for specific files, functions,
  174. or libraries without recompiling the code. You need to pass a path to
  175. suppression file in a ``UBSAN_OPTIONS`` environment variable.
  176. .. code-block:: bash
  177. UBSAN_OPTIONS=suppressions=MyUBSan.supp
  178. You need to specify a :ref:`check <ubsan-checks>` you are suppressing and the
  179. bug location. For example:
  180. .. code-block:: bash
  181. signed-integer-overflow:file-with-known-overflow.cpp
  182. alignment:function_doing_unaligned_access
  183. vptr:shared_object_with_vptr_failures.so
  184. There are several limitations:
  185. * Sometimes your binary must have enough debug info and/or symbol table, so
  186. that the runtime could figure out source file or function name to match
  187. against the suppression.
  188. * It is only possible to suppress recoverable checks. For the example above,
  189. you can additionally pass
  190. ``-fsanitize-recover=signed-integer-overflow,alignment,vptr``, although
  191. most of UBSan checks are recoverable by default.
  192. * Check groups (like ``undefined``) can't be used in suppressions file, only
  193. fine-grained checks are supported.
  194. Supported Platforms
  195. ===================
  196. UndefinedBehaviorSanitizer is supported on the following OS:
  197. * Android
  198. * Linux
  199. * FreeBSD
  200. * OS X 10.6 onwards
  201. and for the following architectures:
  202. * i386/x86\_64
  203. * ARM
  204. * AArch64
  205. * PowerPC64
  206. * MIPS/MIPS64
  207. Current Status
  208. ==============
  209. UndefinedBehaviorSanitizer is available on selected platforms starting from LLVM
  210. 3.3. The test suite is integrated into the CMake build and can be run with
  211. ``check-ubsan`` command.
  212. Additional Configuration
  213. ========================
  214. UndefinedBehaviorSanitizer adds static check data for each check unless it is
  215. in trap mode. This check data includes the full file name. The option
  216. ``-fsanitize-undefined-strip-path-components=N`` can be used to trim this
  217. information. If ``N`` is positive, file information emitted by
  218. UndefinedBehaviorSanitizer will drop the first ``N`` components from the file
  219. path. If ``N`` is negative, the last ``N`` components will be kept.
  220. Example
  221. -------
  222. For a file called ``/code/library/file.cpp``, here is what would be emitted:
  223. * Default (No flag, or ``-fsanitize-undefined-strip-path-components=0``): ``/code/library/file.cpp``
  224. * ``-fsanitize-undefined-strip-path-components=1``: ``code/library/file.cpp``
  225. * ``-fsanitize-undefined-strip-path-components=2``: ``library/file.cpp``
  226. * ``-fsanitize-undefined-strip-path-components=-1``: ``file.cpp``
  227. * ``-fsanitize-undefined-strip-path-components=-2``: ``library/file.cpp``
  228. More Information
  229. ================
  230. * From LLVM project blog:
  231. `What Every C Programmer Should Know About Undefined Behavior
  232. <http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html>`_
  233. * From John Regehr's *Embedded in Academia* blog:
  234. `A Guide to Undefined Behavior in C and C++
  235. <http://blog.regehr.org/archives/213>`_