JoeManiaci
JoeManiaci

Reputation: 475

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 __ubsan_handle_nonnull_arg
U __ubsan_handle_nonnull_return
U __ubsan_handle_out_of_bounds
U __ubsan_handle_shift_out_of_bounds
U __ubsan_handle_sub_overflow
U __ubsan_handle_type_mismatch
U __ubsan_handle_vla_bound_not_positive
U __ubsan_vptr_type_cache

I'm assuming __ubsan_handle_add_overflow is the instrumentation that checks for addition overflow. To my code I added:

auto test = UINT_MAX;
test += 15;

Yet, I see no 'runtime error:' messages related to it.

Our codebase does strip out debug symbols using:

strip --strip-debug --strip-unneeded

I found out that '--strip-unneeded' strips out sanitizer related symbols because calling 'nm' was blank with it present. If I just use 'strip --strip-debug' I get the same nm output as above. Do I perhaps also need debug symbols present in order for sanitizer symbols to function? I can see an increase in memory consumption for my program from ~175MB to ~265MB.

All I've done to enable ubsan is -fvisibility=default and -fsanitize=undefined

I'm in an ARM embedded environment that sadly doesn't provide enough space for me to just quickly test having sanitizer and debug symbols present to test this theory. Our x86 build technically functions and I see runtime errors from the sanitizer, so perhaps that proves it right there since it has sanitizer and debug symbols present?

Upvotes: 0

Views: 911

Answers (1)

Sneftel
Sneftel

Reputation: 41513

-fsanitize-undefined enables checks for undefined behavior. But unsigned integer overflow (as you're invoking here) is not undefined behavior. If you want to check for unsigned overflow (probably a bad idea) you need to pass -fsanitize=unsigned-integer-overflow. If you want to invoke signed overflow, which is UB, do addition on a signed integer instead.

Upvotes: 2

Related Questions