Home

UBSan

UBSan, short for Undefined Behavior Sanitizer, is a runtime analysis tool in the LLVM project used with the Clang compiler to detect undefined behavior in C, C++, and other languages supported by LLVM during program execution. It works by instrumenting generated code to insert checks that validate assumptions required by the language standard; when a check fails, UBSan emits a diagnostic with the location in the source and the type of undefined behavior, and may terminate the program depending on configuration.

UBSan focuses on runtime detection of undefined behavior rather than memory errors. Common checks include signed

Usage typically involves compiling with -fsanitize=undefined and running the program as usual to obtain diagnostics. Recovery

Overall, UBSan provides a practical means to surface subtle undefined behaviors in C and C++ code, aiding

integer
overflow,
integer
division
by
zero,
shifts
that
are
out
of
range
or
overflow,
null
pointer
dereference,
misaligned
memory
access,
and
out-of-bounds
array
indexing.
The
set
of
detectors
can
vary
by
language
version
and
toolchain,
and
newer
releases
may
add
additional
checks
for
other
UB
scenarios.
It
is
designed
to
complement
other
sanitizers
in
the
LLVM
ecosystem,
such
as
AddressSanitizer
and
ThreadSanitizer,
by
targeting
semantic
violations
rather
than
memory
safety
alone.
behavior
can
be
controlled
with
flags
like
-fsanitize-recover=undefined
to
continue
after
a
first
error,
or
by
setting
the
UBSan
runtime
option
via
the
UBSAN_OPTIONS
environment
variable
(for
example
to
enable
stack
traces).
Instrumentation
adds
runtime
overhead,
so
UBSan
is
commonly
used
in
development
and
testing
rather
than
production
builds.
debugging
and
verification
efforts
without
requiring
static
guarantees.