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:

65 questions
1
vote
1 answer

Outside the range of representable values when casting double to long long with UBSAN enabled

Compiling the code below with UBSAN enabled causes this error: Runtime error: value 9.22337e+18 is outside the range of representable values of type 'long long' double a = (double)LLONG_MAX; // or (double)LLONG_MAX - 1 long long b = (long…
samanca
  • 153
  • 3
  • 11
1
vote
3 answers

Logging control, for Address Sanitizer PLUS Undefined Behavior Sanitizer?

Several of the Sanitizers (from either GCC or Clang) cannot be combined – i.e. used simultaneously in the same build, but Asan and Ubsan are combinable – i.e. I can build with “-fsanitize=address,undefined -fsanitize-recover=all …” and have an exe…
Gordon Mc
  • 43
  • 4
1
vote
2 answers

Misaligned pointer use with std::shared_ptr dereference

I am working in a legacy codebase with a large amount of Objective-C++ written using manual retain/release. Memory is managed using lots of C++ std::shared_ptr, with a suitable deleter passed in on construction that calls…
1
vote
1 answer

Use UBSAN with dynamically loaded shared libraries

I tried to use UBSAN in a project and run into an issue which seems impossible to fix: The project uses a plugin system implemented via shared libraries. That is each plugin provides a factory method which returns an instance of some abstract class…
Flamefire
  • 5,313
  • 3
  • 35
  • 70
1
vote
0 answers

gcc/clang UBSan runtime error -- false positive?

I have a little project (~2k lines of code), which compiles with both clang and gcc. gcc gives the following error (-O0 optimization level): /home/nikita/projects/curse_dim/bellman/include/bellman/bellman_operators/qfunc.hpp:10:7: runtime error:…
Nikita Petrenko
  • 1,068
  • 1
  • 7
  • 10
1
vote
2 answers

Pointer addition and integer overflow with Clang 5.0 and UBsan?

I'm trying t understand a problem we cleared recently when using Clang 5.0 and Undefined Behavior Sanitizer (UBsan). We have code that processes a buffer in the forward or backwards direction. The reduced case is similar to the code shown below. The…
jww
  • 97,681
  • 90
  • 411
  • 885
1
vote
0 answers

Clang's UBsan Warning on exported virtual Class on shared object

I'm trying to apply clang's UBSan on my application which uses dlopen/dlsym heavily. However, When I apply UBSan, some warnings occured which I couldn't understand fully. Sample code with one header / two files. Foo.h #ifndef FOO_H #define…
Byoungchan Lee
  • 1,372
  • 13
  • 28
1
vote
1 answer

How to use MATRIX_EVAL to enable a build matrix row based on a compiler?

Our Travis build matrix includes a row that builds with Undefined Behavior sanitizer: matrix: - BUILD_MODE="all" - BUILD_MODE="no-asm" - BUILD_MODE="asan" - BUILD_MODE="ubsan" GCC requires 4.9 (or maybe 5.0) for UBsan, and its…
jww
  • 97,681
  • 90
  • 411
  • 885
1
vote
1 answer

Catching type conversion overflows using gcc

I have a legacy c project with many type conversions and castings. Normally to check the overflow dynamic behavior, gcc (and C lang) defines a builtin function for that: Built-in Function: bool __builtin_add_overflow (type1 a, type2 b, type3…
Tarek Eldeeb
  • 588
  • 2
  • 6
  • 24
0
votes
1 answer

Issue with UBA sanitizer casting negative double to unsigned long long on C++

I've been working with C++ and have been utilizing the UBA sanitizer to convert a double to an unsigned long long. However, I've been encountering an issue when the value is negative, which results in the error message: "runtime error: value -2 is…
Raz Cohen
  • 79
  • 1
  • 7
0
votes
1 answer

Confusing UBSan results for integer overflows

bool addSigned(int a) { return a + 10 > a; } bool addUnsigned(unsigned int a) { return a + 10 > a; } int main() { // UB reported only with -fsanitize=undefined: bool res1 = addSigned (0x7ffffffe); // UB reported only with…
Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101
0
votes
1 answer

Undefined Behavior Sanitizer missing addition overflow check

When I use nm | grep '__ubsan', it returns: U __ubsan_handle_add_overflow U __ubsan_handle_divrem_overflow U __ubsan_handle_dynamic_type_cache_miss U __ubsan_handle_load_invalid_value U __ubsan_handle_mul_overflow U __ubsan_handle_negate_overflow U…
JoeManiaci
  • 435
  • 3
  • 15
0
votes
1 answer

Why does `-fno-omit-frame-pointer` interfere with ASAN?

During a recent project, I've tested combinations of different compiler flags and sanitizers to evaluate the relevance for debugging my C-code. By testing the impact of these combinations, I stumbled across a behavior that I did not…
0
votes
1 answer

Suppress UndefinedBehaviorSanitizer warnings from external libraries

I have an UndefinedBehaviorSanitizer build (-fsanitize=undefined), and I am trying to suppress a warning for UB in an external library that is out of my control. The clang/gcc docs mention __attribute__((no_sanitize("undefined"))), but to my…
0x5453
  • 12,753
  • 1
  • 32
  • 61
0
votes
2 answers

How can I detect off-by-one errors (OBOEs) in C++ code?

Consider this simple program: #include #include #include int main(int argc, char* argv[]) { std::array arr = {0, 1, 2, 3, 4}; int idx = std::atoi(argv[1]); int val = std::atoi(argv[2]); arr[idx]…
nickos556
  • 337
  • 3
  • 16