-
Notifications
You must be signed in to change notification settings - Fork 274
Add generic gcc overflow builtins #5249
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
hannes-steffenhagen-diffblue
merged 1 commit into
diffblue:develop
from
hannes-steffenhagen-diffblue:feature/gcc-builtin-overflow-generic
Mar 4, 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ Author: Daniel Kroening, [email protected] | |
#include "c_typecheck_base.h" | ||
|
||
#include <cassert> | ||
#include <sstream> | ||
|
||
#include <util/arith_tools.h> | ||
#include <util/c_types.h> | ||
|
@@ -33,6 +34,7 @@ Author: Daniel Kroening, [email protected] | |
#include "builtin_factory.h" | ||
#include "c_qualifiers.h" | ||
#include "c_typecast.h" | ||
#include "expr2c.h" | ||
#include "padding.h" | ||
#include "type2name.h" | ||
|
||
|
@@ -2801,6 +2803,102 @@ exprt c_typecheck_baset::do_special_functions( | |
overflow.type() = bool_typet{}; | ||
return overflow; | ||
} | ||
else if( | ||
identifier == "__builtin_add_overflow" || | ||
identifier == "__builtin_sub_overflow" || | ||
identifier == "__builtin_mul_overflow") | ||
{ | ||
// check function signature | ||
if(expr.arguments().size() != 3) | ||
{ | ||
std::ostringstream error_message; | ||
error_message << expr.source_location().as_string() << ": " << identifier | ||
<< " takes exactly 3 arguments, but " | ||
<< expr.arguments().size() << " were provided"; | ||
throw invalid_source_file_exceptiont{error_message.str()}; | ||
} | ||
|
||
auto lhs = expr.arguments()[0]; | ||
auto rhs = expr.arguments()[1]; | ||
auto result_ptr = expr.arguments()[2]; | ||
|
||
typecheck_expr(lhs); | ||
typecheck_expr(rhs); | ||
typecheck_expr(result_ptr); | ||
|
||
{ | ||
auto const raise_wrong_argument_error = | ||
[this, | ||
identifier](const exprt &wrong_argument, std::size_t argument_number) { | ||
std::ostringstream error_message; | ||
error_message << wrong_argument.source_location().as_string() << ": " | ||
<< identifier << " has signature " << identifier | ||
<< "(integral, integral, integral*), " | ||
<< "but argument " << argument_number << " (" | ||
<< expr2c(wrong_argument, *this) << ") has type `" | ||
<< type2c(wrong_argument.type(), *this) << '`'; | ||
throw invalid_source_file_exceptiont{error_message.str()}; | ||
}; | ||
for(auto const arg_index : {0, 1}) | ||
{ | ||
auto const &argument = expr.arguments()[arg_index]; | ||
|
||
if(!is_signed_or_unsigned_bitvector(argument.type())) | ||
{ | ||
raise_wrong_argument_error(argument, arg_index + 1); | ||
} | ||
} | ||
if( | ||
result_ptr.type().id() != ID_pointer || | ||
!is_signed_or_unsigned_bitvector(result_ptr.type().subtype())) | ||
{ | ||
raise_wrong_argument_error(result_ptr, 3); | ||
} | ||
} | ||
|
||
// actual logic implementing the operators | ||
auto const make_operation = [&identifier](exprt lhs, exprt rhs) -> exprt { | ||
if(identifier == "__builtin_add_overflow") | ||
{ | ||
return plus_exprt{lhs, rhs}; | ||
} | ||
else if(identifier == "__builtin_sub_overflow") | ||
{ | ||
return minus_exprt{lhs, rhs}; | ||
} | ||
else | ||
{ | ||
INVARIANT( | ||
identifier == "__builtin_mul_overflow", | ||
"the three overflow operations are add, sub and mul"); | ||
return mult_exprt{lhs, rhs}; | ||
} | ||
}; | ||
|
||
// we’re basically generating this expression | ||
// (*result = (result_type)((integer)lhs OP (integer)rhs)), | ||
// ((integer)result == (integer)lhs OP (integer)rhs) | ||
// i.e. perform the operation (+, -, *) on arbitrary length integer, | ||
// cast to result type, check if the casted result is still equivalent | ||
// to the arbitrary length result. | ||
auto operation = make_operation( | ||
typecast_exprt{lhs, integer_typet{}}, | ||
typecast_exprt{rhs, integer_typet{}}); | ||
|
||
auto operation_result = | ||
typecast_exprt{operation, result_ptr.type().subtype()}; | ||
typecheck_expr_typecast(operation_result); | ||
auto overflow_check = notequal_exprt{ | ||
typecast_exprt{dereference_exprt{result_ptr}, integer_typet{}}, | ||
operation}; | ||
typecheck_expr(overflow_check); | ||
return exprt{ID_comma, | ||
bool_typet{}, | ||
{side_effect_expr_assignt{dereference_exprt{result_ptr}, | ||
operation_result, | ||
expr.source_location()}, | ||
overflow_check}}; | ||
} | ||
else | ||
return nil_exprt(); | ||
} | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.