Back to home page

OSCL-LXR

 
 

    


0001 The Undefined Behavior Sanitizer - UBSAN
0002 ========================================
0003 
0004 UBSAN is a runtime undefined behaviour checker.
0005 
0006 UBSAN uses compile-time instrumentation to catch undefined behavior (UB).
0007 Compiler inserts code that perform certain kinds of checks before operations
0008 that may cause UB. If check fails (i.e. UB detected) __ubsan_handle_*
0009 function called to print error message.
0010 
0011 GCC has that feature since 4.9.x [1_] (see ``-fsanitize=undefined`` option and
0012 its suboptions). GCC 5.x has more checkers implemented [2_].
0013 
0014 Report example
0015 --------------
0016 
0017 ::
0018 
0019          ================================================================================
0020          UBSAN: Undefined behaviour in ../include/linux/bitops.h:110:33
0021          shift exponent 32 is to large for 32-bit type 'unsigned int'
0022          CPU: 0 PID: 0 Comm: swapper Not tainted 4.4.0-rc1+ #26
0023           0000000000000000 ffffffff82403cc8 ffffffff815e6cd6 0000000000000001
0024           ffffffff82403cf8 ffffffff82403ce0 ffffffff8163a5ed 0000000000000020
0025           ffffffff82403d78 ffffffff8163ac2b ffffffff815f0001 0000000000000002
0026          Call Trace:
0027           [<ffffffff815e6cd6>] dump_stack+0x45/0x5f
0028           [<ffffffff8163a5ed>] ubsan_epilogue+0xd/0x40
0029           [<ffffffff8163ac2b>] __ubsan_handle_shift_out_of_bounds+0xeb/0x130
0030           [<ffffffff815f0001>] ? radix_tree_gang_lookup_slot+0x51/0x150
0031           [<ffffffff8173c586>] _mix_pool_bytes+0x1e6/0x480
0032           [<ffffffff83105653>] ? dmi_walk_early+0x48/0x5c
0033           [<ffffffff8173c881>] add_device_randomness+0x61/0x130
0034           [<ffffffff83105b35>] ? dmi_save_one_device+0xaa/0xaa
0035           [<ffffffff83105653>] dmi_walk_early+0x48/0x5c
0036           [<ffffffff831066ae>] dmi_scan_machine+0x278/0x4b4
0037           [<ffffffff8111d58a>] ? vprintk_default+0x1a/0x20
0038           [<ffffffff830ad120>] ? early_idt_handler_array+0x120/0x120
0039           [<ffffffff830b2240>] setup_arch+0x405/0xc2c
0040           [<ffffffff830ad120>] ? early_idt_handler_array+0x120/0x120
0041           [<ffffffff830ae053>] start_kernel+0x83/0x49a
0042           [<ffffffff830ad120>] ? early_idt_handler_array+0x120/0x120
0043           [<ffffffff830ad386>] x86_64_start_reservations+0x2a/0x2c
0044           [<ffffffff830ad4f3>] x86_64_start_kernel+0x16b/0x17a
0045          ================================================================================
0046 
0047 Usage
0048 -----
0049 
0050 To enable UBSAN configure kernel with::
0051 
0052         CONFIG_UBSAN=y
0053 
0054 and to check the entire kernel::
0055 
0056         CONFIG_UBSAN_SANITIZE_ALL=y
0057 
0058 To enable instrumentation for specific files or directories, add a line
0059 similar to the following to the respective kernel Makefile:
0060 
0061 - For a single file (e.g. main.o)::
0062 
0063     UBSAN_SANITIZE_main.o := y
0064 
0065 - For all files in one directory::
0066 
0067     UBSAN_SANITIZE := y
0068 
0069 To exclude files from being instrumented even if
0070 ``CONFIG_UBSAN_SANITIZE_ALL=y``, use::
0071 
0072   UBSAN_SANITIZE_main.o := n
0073 
0074 and::
0075 
0076   UBSAN_SANITIZE := n
0077 
0078 Detection of unaligned accesses controlled through the separate option -
0079 CONFIG_UBSAN_ALIGNMENT. It's off by default on architectures that support
0080 unaligned accesses (CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS=y). One could
0081 still enable it in config, just note that it will produce a lot of UBSAN
0082 reports.
0083 
0084 References
0085 ----------
0086 
0087 .. _1: https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Debugging-Options.html
0088 .. _2: https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
0089 .. _3: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html