-
Notifications
You must be signed in to change notification settings - Fork 284
Add __builtin_add_overflow primitives #5233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NlightNFotis
merged 2 commits into
diffblue:develop
from
hannes-steffenhagen-diffblue:feature/gcc_overflow_builtins
Feb 14, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include <assert.h> | ||
#include <limits.h> | ||
|
||
void check_int(void) | ||
{ | ||
int const one = 1; | ||
int result; | ||
|
||
assert(!__builtin_sadd_overflow(one, one, &result)); | ||
assert(result == 2); | ||
assert(__builtin_sadd_overflow(one, INT_MAX, &result)); | ||
assert(!__builtin_sadd_overflow(INT_MAX / 2, INT_MAX / 2, &result)); | ||
assert(result + 1 == INT_MAX); | ||
assert(__builtin_sadd_overflow(INT_MAX / 2 + 2, INT_MAX / 2 + 2, &result)); | ||
assert(__builtin_sadd_overflow(-one, INT_MIN, &result)); | ||
assert(0 && "reachability"); | ||
} | ||
|
||
void check_long(void) | ||
{ | ||
long const one = 1l; | ||
long result; | ||
|
||
assert(!__builtin_saddl_overflow(one, one, &result)); | ||
assert(result == 2l); | ||
assert(__builtin_saddl_overflow(one, LONG_MAX, &result)); | ||
assert(!__builtin_saddl_overflow(LONG_MAX / 2l, LONG_MAX / 2l, &result)); | ||
assert(result + 1l == LONG_MAX); | ||
assert( | ||
__builtin_saddl_overflow(LONG_MAX / 2l + 2l, LONG_MAX / 2l + 2l, &result)); | ||
assert(__builtin_saddl_overflow(-one, LONG_MIN, &result)); | ||
assert(0 && "reachability"); | ||
} | ||
|
||
void check_long_long(void) | ||
{ | ||
long long const one = 1ll; | ||
long long result; | ||
|
||
assert(!__builtin_saddll_overflow(one, one, &result)); | ||
assert(result == 2ll); | ||
assert(__builtin_saddll_overflow(one, LLONG_MAX, &result)); | ||
assert(!__builtin_saddll_overflow(LLONG_MAX / 2ll, LLONG_MAX / 2ll, &result)); | ||
assert(result + 1ll == LLONG_MAX); | ||
assert(__builtin_saddll_overflow( | ||
LLONG_MAX / 2ll + 2ll, LLONG_MAX / 2ll + 2ll, &result)); | ||
assert(__builtin_saddll_overflow(-one, LLONG_MIN, &result)); | ||
assert(0 && "reachability"); | ||
} | ||
|
||
void check_unsigned(void) | ||
{ | ||
unsigned const one = 1u; | ||
unsigned result; | ||
|
||
assert(!__builtin_uadd_overflow(one, one, &result)); | ||
assert(result == 2u); | ||
assert(!__builtin_uadd_overflow(UINT_MAX / 2, UINT_MAX / 2, &result)); | ||
assert(result + 1u == UINT_MAX); | ||
assert(__builtin_uadd_overflow(UINT_MAX / 2 + 2, UINT_MAX / 2 + 2, &result)); | ||
assert(__builtin_uadd_overflow(one, UINT_MAX, &result)); | ||
assert(0 && "reachability"); | ||
} | ||
|
||
void check_unsigned_long(void) | ||
{ | ||
unsigned long const one = 1ul; | ||
unsigned long result; | ||
|
||
assert(!__builtin_uaddl_overflow(one, one, &result)); | ||
assert(result == 2ul); | ||
assert(!__builtin_uaddl_overflow(ULONG_MAX / 2, ULONG_MAX / 2, &result)); | ||
assert(result + 1ul == ULONG_MAX); | ||
assert( | ||
__builtin_uaddl_overflow(ULONG_MAX / 2 + 2, ULONG_MAX / 2 + 2, &result)); | ||
assert(__builtin_uaddl_overflow(one, ULONG_MAX, &result)); | ||
assert(0 && "reachability"); | ||
} | ||
|
||
void check_unsigned_long_long(void) | ||
{ | ||
unsigned long long const one = 1ull; | ||
unsigned long long result; | ||
|
||
assert(!__builtin_uaddl_overflow(one, one, &result)); | ||
assert(result == 2ull); | ||
assert(!__builtin_uaddll_overflow(ULLONG_MAX / 2, ULLONG_MAX / 2, &result)); | ||
assert(result + 1ull == ULLONG_MAX); | ||
assert( | ||
__builtin_uaddll_overflow(ULLONG_MAX / 2 + 2, ULLONG_MAX / 2 + 2, &result)); | ||
assert(__builtin_uaddl_overflow(one, ULLONG_MAX, &result)); | ||
assert(0 && "reachability"); | ||
} | ||
|
||
int main(void) | ||
{ | ||
check_int(); | ||
check_long(); | ||
check_long_long(); | ||
check_unsigned(); | ||
check_unsigned_long(); | ||
check_unsigned_long_long(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
CORE gcc-only | ||
main.c | ||
|
||
\[check_int.assertion.1\] line \d+ assertion !__builtin_sadd_overflow\(one, one, &result\): SUCCESS | ||
\[check_int.assertion.2\] line \d+ assertion result == 2: SUCCESS | ||
\[check_int.assertion.3\] line \d+ assertion __builtin_sadd_overflow\(one, .*, &result\): SUCCESS | ||
\[check_int.assertion.4\] line \d+ assertion !__builtin_sadd_overflow\(.* / 2, .* / 2, &result\): SUCCESS | ||
\[check_int.assertion.5\] line \d+ assertion result \+ 1 == .*: SUCCESS | ||
\[check_int.assertion.6\] line \d+ assertion __builtin_sadd_overflow\(.* / 2 \+ 2, .* / 2 \+ 2, &result\): SUCCESS | ||
\[check_int.assertion.7\] line \d+ assertion __builtin_sadd_overflow\(-one, .*, &result\): SUCCESS | ||
\[check_int.assertion.8\] line \d+ assertion 0 && "reachability": FAILURE | ||
\[check_long.assertion.1\] line \d+ assertion !__builtin_saddl_overflow\(one, one, &result\): SUCCESS | ||
\[check_long.assertion.2\] line \d+ assertion result == 2l: SUCCESS | ||
\[check_long.assertion.3\] line \d+ assertion __builtin_saddl_overflow\(one, .*, &result\): SUCCESS | ||
\[check_long.assertion.4\] line \d+ assertion !__builtin_saddl_overflow\(.* / 2l, .* / 2l, &result\): SUCCESS | ||
\[check_long.assertion.5\] line \d+ assertion result \+ 1l == .*: SUCCESS | ||
\[check_long.assertion.6\] line \d+ assertion __builtin_saddl_overflow\(.* / 2l \+ 2l, .* / 2l \+ 2l, &result\): SUCCESS | ||
\[check_long.assertion.7\] line \d+ assertion __builtin_saddl_overflow\(-one, .*, &result\): SUCCESS | ||
\[check_long.assertion.8\] line \d+ assertion 0 && "reachability": FAILURE | ||
\[check_long_long.assertion.1\] line \d+ assertion !__builtin_saddll_overflow\(one, one, &result\): SUCCESS | ||
\[check_long_long.assertion.2\] line \d+ assertion result == 2ll: SUCCESS | ||
\[check_long_long.assertion.3\] line \d+ assertion __builtin_saddll_overflow\(one, .*, &result\): SUCCESS | ||
\[check_long_long.assertion.4\] line \d+ assertion !__builtin_saddll_overflow\(.* / 2ll, .* / 2ll, &result\): SUCCESS | ||
\[check_long_long.assertion.5\] line \d+ assertion result \+ 1ll == .*: SUCCESS | ||
\[check_long_long.assertion.6\] line \d+ assertion __builtin_saddll_overflow\(.* / 2ll \+ 2ll, .* / 2ll \+ 2ll, &result\): SUCCESS | ||
\[check_long_long.assertion.7\] line \d+ assertion __builtin_saddll_overflow\(-one, .*, &result\): SUCCESS | ||
\[check_long_long.assertion.8\] line \d+ assertion 0 && "reachability": FAILURE | ||
\[check_unsigned.assertion.1\] line \d+ assertion !__builtin_uadd_overflow\(one, one, &result\): SUCCESS | ||
\[check_unsigned.assertion.2\] line \d+ assertion result == 2u: SUCCESS | ||
\[check_unsigned.assertion.3\] line \d+ assertion !__builtin_uadd_overflow\(.* / 2, .* / 2, &result\): SUCCESS | ||
\[check_unsigned.assertion.4\] line \d+ assertion result \+ 1u == .*: SUCCESS | ||
\[check_unsigned.assertion.5\] line \d+ assertion __builtin_uadd_overflow\(.* / 2 \+ 2, .* / 2 \+ 2, &result\): SUCCESS | ||
\[check_unsigned.assertion.6\] line \d+ assertion __builtin_uadd_overflow\(one, .*, &result\): SUCCESS | ||
\[check_unsigned.assertion.7\] line \d+ assertion 0 && "reachability": FAILURE | ||
\[check_unsigned_long.assertion.1\] line \d+ assertion !__builtin_uaddl_overflow\(one, one, &result\): SUCCESS | ||
\[check_unsigned_long.assertion.2\] line \d+ assertion result == 2ul: SUCCESS | ||
\[check_unsigned_long.assertion.3\] line \d+ assertion !__builtin_uaddl_overflow\(.* / 2, .* / 2, &result\): SUCCESS | ||
\[check_unsigned_long.assertion.4\] line \d+ assertion result \+ 1ul == .*: SUCCESS | ||
\[check_unsigned_long.assertion.5\] line \d+ assertion __builtin_uaddl_overflow\(.* / 2 \+ 2, .* / 2 \+ 2, &result\): SUCCESS | ||
\[check_unsigned_long.assertion.6\] line \d+ assertion __builtin_uaddl_overflow\(one, .*, &result\): SUCCESS | ||
\[check_unsigned_long.assertion.7\] line \d+ assertion 0 && "reachability": FAILURE | ||
\[check_unsigned_long_long.assertion.1\] line \d+ assertion !__builtin_uaddl_overflow\(one, one, &result\): SUCCESS | ||
\[check_unsigned_long_long.assertion.2\] line \d+ assertion result == 2ull: SUCCESS | ||
\[check_unsigned_long_long.assertion.3\] line \d+ assertion !__builtin_uaddll_overflow\(.* / 2, .* / 2, &result\): SUCCESS | ||
\[check_unsigned_long_long.assertion.4\] line \d+ assertion result \+ 1ull == .*: SUCCESS | ||
\[check_unsigned_long_long.assertion.5\] line \d+ assertion __builtin_uaddll_overflow\(.* / 2 \+ 2, .* / 2 \+ 2, &result\): SUCCESS | ||
\[check_unsigned_long_long.assertion.6\] line \d+ assertion __builtin_uaddl_overflow\(one, .*, &result\): SUCCESS | ||
\[check_unsigned_long_long.assertion.7\] line \d+ assertion 0 && "reachability": FAILURE | ||
VERIFICATION FAILED | ||
^EXIT=10$ | ||
^SIGNAL=0$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests look good, but could we add a few more cases? In particular, these tests only cover some of the "easily optimised" cases (e.g value of 1, adding/subtracting to MAX/MIN), but it would be great to see some tests that also cover "mid-range value x + mid-range value y == overflow" and "slightly smaller mid-range value x + mid-range value y != overflow" cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chrisr-diffblue I’ve added some more cases to that end