A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html below:

UndefinedBehaviorSanitizer — Clang 22.0.0git documentation

UndefinedBehaviorSanitizer¶ Introduction¶

UndefinedBehaviorSanitizer (UBSan) is a fast undefined behavior detector. UBSan modifies the program at compile-time to catch various kinds of undefined behavior during program execution, for example:

See the full list of available checks below.

UBSan has an optional run-time library which provides better error reporting. The checks have small runtime cost and no impact on address space layout or ABI.

How to build¶

Build LLVM/Clang with CMake.

Usage¶

Use clang++ to compile and link your program with the -fsanitize=undefined option. Make sure to use clang++ (not ld) as a linker, so that your executable is linked with proper UBSan runtime libraries, unless all enabled checks use trap mode. You can use clang instead of clang++ if you’re compiling/linking C code.

% cat test.cc
int main(int argc, char **argv) {
  int k = 0x7fffffff;
  k += argc;
  return 0;
}
% clang++ -fsanitize=undefined test.cc
% ./a.out
test.cc:3:5: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'

You can use -fsanitize=... and -fno-sanitize= to enable and disable one check or one check group. For an individual check, the last option that enabling or disabling it wins.

# Enable all checks in the "undefined" group, but disable "alignment".
% clang -fsanitize=undefined -fno-sanitize=alignment a.c

# Enable just "alignment".
% clang -fsanitize=alignment a.c

# The same. -fno-sanitize=undefined nullifies the previous -fsanitize=undefined.
% clang -fsanitize=undefined -fno-sanitize=undefined -fsanitize=alignment a.c

For most checks (checks), the instrumented program prints a verbose error report and continues execution upon a failed check. You can use the following options to change the error reporting behavior:

For example:

% clang++ -fsanitize=signed-integer-overflow,null,alignment -fno-sanitize-recover=null -fsanitize-trap=alignment a.cc

The program will continue execution after signed integer overflows, exit after the first invalid use of a null pointer, and trap after the first use of misaligned pointer.

% clang++ -fsanitize=undefined -fsanitize-trap=all a.cc

All checks in the “undefined” group are put into trap mode. Since no check needs run-time support, the UBSan run-time library it not linked. Note that some other sanitizers also support trap mode and -fsanitize-trap=all enables trap mode for them.

% clang -fsanitize-trap=undefined -fsanitize-recover=all a.c

-fsanitize-trap= and -fsanitize-recover= are a no-op in the absence of a -fsanitize= option. There is no unused command line option warning.

Available checks¶

Available checks are:

You can also use the following check groups:
Volatile¶

The null, alignment, object-size, local-bounds, and vptr checks do not apply to pointers to types with the volatile qualifier.

Minimal Runtime¶

There is a minimal UBSan runtime available suitable for use in production environments. This runtime has a small attack surface. It only provides very basic issue logging and deduplication, and does not support -fsanitize=vptr checking.

To use the minimal runtime, add -fsanitize-minimal-runtime to the clang command line options. For example, if you’re used to compiling with -fsanitize=undefined, you could enable the minimal runtime with -fsanitize=undefined -fsanitize-minimal-runtime.

Stack traces and report symbolization¶

If you want UBSan to print symbolized stack trace for each error report, you will need to:

  1. Compile with -g, -fno-sanitize-merge and -fno-omit-frame-pointer to get proper debug information in your binary.

  2. Run your program with environment variable UBSAN_OPTIONS=print_stacktrace=1.

  3. Make sure llvm-symbolizer binary is in PATH.

Logging¶

The default log file for diagnostics is “stderr”. To log diagnostics to another file, you can set UBSAN_OPTIONS=log_path=....

Silencing Unsigned Integer Overflow¶

To silence reports from unsigned integer overflow, you can set UBSAN_OPTIONS=silence_unsigned_overflow=1. This feature, combined with -fsanitize-recover=unsigned-integer-overflow, is particularly useful for providing fuzzing signal without blowing up logs.

Disabling instrumentation for common overflow patterns¶

There are certain overflow-dependent or overflow-prone code patterns which produce a lot of noise for integer overflow/truncation sanitizers. Negated unsigned constants, post-decrements in a while loop condition and simple overflow checks are accepted and pervasive code patterns. However, the signal received from sanitizers instrumenting these code patterns may be too noisy for some projects. To disable instrumentation for these common patterns one should use -fsanitize-undefined-ignore-overflow-pattern=.

Currently, this option supports three overflow-dependent code idioms:

negated-unsigned-const

/// -fsanitize-undefined-ignore-overflow-pattern=negated-unsigned-const
unsigned long foo = -1UL; // No longer causes a negation overflow warning
unsigned long bar = -2UL; // and so on...

unsigned-post-decr-while

/// -fsanitize-undefined-ignore-overflow-pattern=unsigned-post-decr-while
unsigned char count = 16;
while (count--) { /* ... */ } // No longer causes unsigned-integer-overflow sanitizer to trip

add-signed-overflow-test,add-unsigned-overflow-test

/// -fsanitize-undefined-ignore-overflow-pattern=add-(signed|unsigned)-overflow-test
if (base + offset < base) { /* ... */ } // The pattern of `a + b < a`, and other re-orderings,
                                        // won't be instrumented (signed or unsigned types)
Overflow Pattern Types¶

Pattern

Sanitizer

negated-unsigned-const

unsigned-integer-overflow

unsigned-post-decr-while

unsigned-integer-overflow

add-unsigned-overflow-test

unsigned-integer-overflow

add-signed-overflow-test

signed-integer-overflow

Note: add-signed-overflow-test suppresses only the check for Undefined Behavior. Eager Undefined Behavior optimizations are still possible. One may remedy this with -fwrapv or -fno-strict-overflow.

You can enable all exclusions with -fsanitize-undefined-ignore-overflow-pattern=all or disable all exclusions with -fsanitize-undefined-ignore-overflow-pattern=none. If -fsanitize-undefined-ignore-overflow-pattern is not specified none is implied. Specifying none alongside other values also implies none as none has precedence over other values – including all.

Issue Suppression¶

UndefinedBehaviorSanitizer is not expected to produce false positives. If you see one, look again; most likely it is a true positive!

Disabling Instrumentation with __attribute__((no_sanitize("undefined")))¶

You disable UBSan checks for particular functions with __attribute__((no_sanitize("undefined"))). You can use all values of -fsanitize= flag in this attribute, e.g. if your function deliberately contains possible signed integer overflow, you can use __attribute__((no_sanitize("signed-integer-overflow"))).

This attribute may not be supported by other compilers, so consider using it together with #if defined(__clang__).

Suppressing Errors in Recompiled Code (Ignorelist)¶

UndefinedBehaviorSanitizer supports src and fun entity types in Sanitizer special case list, that can be used to suppress error reports in the specified source files or functions.

Runtime suppressions¶

Sometimes you can suppress UBSan error reports for specific files, functions, or libraries without recompiling the code. You need to pass a path to suppression file in a UBSAN_OPTIONS environment variable.

UBSAN_OPTIONS=suppressions=MyUBSan.supp

You need to specify a check you are suppressing and the bug location. For example:

signed-integer-overflow:file-with-known-overflow.cpp
alignment:function_doing_unaligned_access
vptr:shared_object_with_vptr_failures.so

There are several limitations:

Security Considerations¶

UndefinedBehaviorSanitizer’s runtime is meant for testing purposes and its usage in production environment should be carefully considered from security perspective as it may compromise the security of the resulting executable. For security-sensitive applications consider using Minimal Runtime or trap mode for all checks.

Supported Platforms¶

UndefinedBehaviorSanitizer is supported on the following operating systems:

The runtime library is relatively portable and platform independent. If the OS you need is not listed above, UndefinedBehaviorSanitizer may already work for it, or could be made to work with a minor porting effort.

Current Status¶

UndefinedBehaviorSanitizer is available on selected platforms starting from LLVM 3.3. The test suite is integrated into the CMake build and can be run with check-ubsan command.

Additional Configuration¶

UndefinedBehaviorSanitizer adds static check data for each check unless it is in trap mode. This check data includes the full file name. The option -fsanitize-undefined-strip-path-components=N can be used to trim this information. If N is positive, file information emitted by UndefinedBehaviorSanitizer will drop the first N components from the file path. If N is negative, the last N components will be kept.

Example¶

For a file called /code/library/file.cpp, here is what would be emitted:

More Information¶

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4