Questions tagged [ubsan]

Undefined Behavior sanitizer (ubsan) is a fast undefined behavior detector for Clang and GCC. Various computations will be instrumented to detect undefined behavior at runtime.

Undefined Behavior sanitizer (ubsan) is a fast undefined behavior detector for C and C++ programs and enabled at compile time (but the checks are performed at runtime). It is available since Clang 3.2 and GCC 4.9.

See also:

54 questions
35
votes
3 answers

How can I break on UBSan reports in gdb and continue?

Recent versions of GCC and Clang feature Undefined Behavior Sanitizer (UBSan) which is a compile flag (-fsanitize=undefined) that adds runtime instrumentation code. On errors, a warning such as this one is shown: packet-ber.c:1917:23: runtime…
Lekensteyn
  • 58,351
  • 21
  • 146
  • 179
21
votes
2 answers

Using GCC Undefined Behavior Sanitizer

Today I have read an article about GCC Undefined Behavior Sanitizer (ubsan). However, when I follow steps there (add -fsanitize=undefined to my code), the compiler (GCC 4.9.2 on Ubuntu 15.04) says that some references are not defined: ||=== Build:…
Ilya
  • 642
  • 7
  • 21
16
votes
3 answers

Why does enabling undefined behaviour sanitization interfere with optimizations?

Consider the following code: #include constexpr std::string_view f() { return "hello"; } static constexpr std::string_view g() { auto x = f(); return x.substr(1, 3); } int foo() { return g().length(); } If I compile it with…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
13
votes
1 answer

Segmentation fault on gcc caused by lambda wrapper over variadic template function call

I've spent quite a few hours today trying to understand why this code segfaults on g++6.2 and g++7.0, while happily working as intended on clang++3.9 (and 4.0). I reduced the issue to a 85 lines self-contained code snippet, which does not segfault…
Vittorio Romeo
  • 82,972
  • 25
  • 221
  • 369
12
votes
1 answer

Clang 8 with MinGW-w64: How do I use address- & UB sanitizers?

Clang 8 release notes have this promising line: Allow using Address Sanitizer and Undefined Behaviour Sanitizer on MinGW. However, I unable to figure out how to use those properly. I'm using Clang 8.0.0 with MSYS2 MinGW GCC. Exact details are at…
HolyBlackCat
  • 45,832
  • 5
  • 81
  • 134
10
votes
1 answer

Clang's UBSan & Function Pointer: Is this illegal?

I'm trying to call some C++ functions through a function pointer table which is exported as a C symbol from a shared object. The code is actually working but Clang's undefined behavior sanitizer (= UBSan) sees the call I made is illegal as…
Doofah
  • 335
  • 3
  • 11
9
votes
2 answers

Why does -fsanitize=undefined cause "undefined reference to typeinfo"?

The following test-case, reduced from a real-world application, fails to link with -fsanitize=undefined (using GCC 6.1.1) but links fine without it. Can anyone tell me why? It seems to have something to do with the combination of Qt/QObject,…
John Lindgren
  • 737
  • 5
  • 12
8
votes
2 answers

Load of misaligned address and UBsan finding

This question is not about the definition of unaligned data accesses, but why memcpy silences the UBsan findings whereas type casting does not, despite generating the same assembly code. I have some example code to parse a protocol that sends a byte…
Charles
  • 733
  • 6
  • 16
7
votes
3 answers

How can I determine if UBSAN has been compiled in using clang or gcc?

We use the following code to determine if -fsanitize=address has been specified at compile time for clang and gcc. How do we determine if -fsanitize=undefined has been specified? bool isSanitized = false; #if defined(__has_feature) #if…
6
votes
0 answers

how to use ubsan on gcc (windows 8.1)

In order to have a better protection against UB cases like: #include int f(){ int x; return x; } int main() { f(); while(1); return 0; } I've updated my GCC today so I could use ubsan. My current version is 5.3.0 according…
CIsForCookies
  • 10,156
  • 5
  • 36
  • 74
6
votes
1 answer

Call to function (unknown) through pointer to incorrect function type

I have a program that dynamically links against a library. The program passes a function pointer to that library, to execute. But the ubsan (Undefined Behavior Sanitizer) specified that the pointer is on an incorrect function type. And that occurs…
Galixe
  • 61
  • 3
6
votes
2 answers

How to suppress some unsigned-integer-overflow errors from UBsan?

Most of my -fsanitize=unsigned-integer-overflow errors are bugs, but sometimes I explicitly use it as intended, which results in UBSan producing false positives. Is there a way to turn UBSan unsigned-integer-overflow check off for a particular…
gnzlbg
  • 6,587
  • 3
  • 46
  • 94
5
votes
1 answer

Understanding a runtime error triggered by the undefined behavior sanitizer (UBSan)

I find a run-time error in GNU Scientific Library (GSL) when undefined sanitizer is enabled: deque.c:58:11: runtime error: member access within misaligned address 0x0000024010f4 for type 'struct deque', which requires 8 byte…
zell
  • 8,226
  • 7
  • 41
  • 91
5
votes
1 answer

C++ UBSAN produces false positives with derived objects

I wanted to use UBSAN (undefined behavior sanitizer) but found it completely worthless as it reports to many false positives. E.g. a simple std::make_shared(42); is enough to trigger warnings like member access within address 0x00000236de70…
Flamefire
  • 3,954
  • 2
  • 22
  • 50
5
votes
0 answers

UBSan: boost::program_options with std::string

We are currently investigating a possible undefined behaviour in our program that is flagged by clang7 UBSan in combination with boost::program_option from boost 1.69.0. We have created the following working example that can we compiled and run…
nafur
  • 121
  • 3
1
2 3 4