Skip to content

C front-end: type check arguments to built-in functions #4628

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
merged 1 commit into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions regression/cbmc/r_w_ok4/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <assert.h>
#include <stdlib.h>

int main()
{
int A[1];

assert(__CPROVER_r_ok(A, sizeof(int)));
}
8 changes: 8 additions & 0 deletions regression/cbmc/r_w_ok4/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c

^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
63 changes: 59 additions & 4 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt same_object_expr=
same_object(expr.arguments()[0], expr.arguments()[1]);
same_object_expr.add_source_location()=source_location;
Expand Down Expand Up @@ -2105,6 +2107,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt same_object_expr = is_invalid_pointer_exprt{expr.arguments().front()};
same_object_expr.add_source_location()=source_location;

Expand All @@ -2119,6 +2123,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt buffer_size_expr("buffer_size", size_type());
buffer_size_expr.operands()=expr.arguments();
buffer_size_expr.add_source_location()=source_location;
Expand All @@ -2134,6 +2140,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

predicate_exprt is_zero_string_expr("is_zero_string");
is_zero_string_expr.operands()=expr.arguments();
is_zero_string_expr.set(ID_C_lvalue, true); // make it an lvalue
Expand All @@ -2150,6 +2158,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt zero_string_length_expr("zero_string_length", size_type());
zero_string_length_expr.operands()=expr.arguments();
zero_string_length_expr.set(ID_C_lvalue, true); // make it an lvalue
Expand All @@ -2166,6 +2176,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt is_dynamic_object_expr = is_dynamic_object_exprt(expr.arguments()[0]);
is_dynamic_object_expr.add_source_location() = source_location;

Expand All @@ -2180,6 +2192,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt pointer_offset_expr=pointer_offset(expr.arguments().front());
pointer_offset_expr.add_source_location()=source_location;

Expand All @@ -2194,6 +2208,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

unary_exprt object_size_expr(
ID_object_size, expr.arguments()[0], size_type());
object_size_expr.add_source_location() = source_location;
Expand All @@ -2209,6 +2225,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt pointer_object_expr = pointer_object(expr.arguments().front());
pointer_object_expr.add_source_location() = source_location;

Expand All @@ -2218,15 +2236,15 @@ exprt c_typecheck_baset::do_special_functions(
identifier=="__builtin_bswap32" ||
identifier=="__builtin_bswap64")
{
typecheck_function_call_arguments(expr);

if(expr.arguments().size()!=1)
{
error().source_location = f_op.source_location();
error() << identifier << " expects one operand" << eom;
throw 0;
}

typecheck_function_call_arguments(expr);

// these are hard-wired to 8 bits according to the gcc manual
bswap_exprt bswap_expr(expr.arguments().front(), 8, expr.type());
bswap_expr.add_source_location()=source_location;
Expand All @@ -2235,15 +2253,15 @@ exprt c_typecheck_baset::do_special_functions(
}
else if(identifier=="__builtin_nontemporal_load")
{
typecheck_function_call_arguments(expr);

if(expr.arguments().size()!=1)
{
error().source_location = f_op.source_location();
error() << identifier << " expects one operand" << eom;
throw 0;
}

typecheck_function_call_arguments(expr);

// these return the subtype of the argument
exprt &ptr_arg=expr.arguments().front();

Expand All @@ -2269,6 +2287,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

// This gets 5 integers followed by a float or double.
// The five integers are the return values for the cases
// FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL and FP_ZERO.
Expand Down Expand Up @@ -2315,6 +2335,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

isnan_exprt isnan_expr(expr.arguments().front());
isnan_expr.add_source_location()=source_location;

Expand All @@ -2331,6 +2353,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

isfinite_exprt isfinite_expr(expr.arguments().front());
isfinite_expr.add_source_location()=source_location;

Expand Down Expand Up @@ -2378,6 +2402,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

abs_exprt abs_expr(expr.arguments().front());
abs_expr.add_source_location()=source_location;

Expand All @@ -2392,6 +2418,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

side_effect_exprt malloc_expr(ID_allocate, expr.type(), source_location);
malloc_expr.operands()=expr.arguments();

Expand All @@ -2407,6 +2435,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

irep_idt id = identifier == CPROVER_PREFIX "r_ok" ? ID_r_ok : ID_w_ok;

binary_predicate_exprt ok_expr(
Expand All @@ -2427,6 +2457,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

isinf_exprt isinf_expr(expr.arguments().front());
isinf_expr.add_source_location()=source_location;

Expand All @@ -2441,6 +2473,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

// returns 1 for +inf and -1 for -inf, and 0 otherwise

const exprt &fp_value = expr.arguments().front();
Expand Down Expand Up @@ -2468,6 +2502,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

const exprt &fp_value = expr.arguments()[0];

if(fp_value.type().id() != ID_floatbv)
Expand Down Expand Up @@ -2496,6 +2532,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

sign_exprt sign_expr(expr.arguments().front());
sign_expr.add_source_location()=source_location;

Expand All @@ -2515,6 +2553,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

popcount_exprt popcount_expr(expr.arguments().front(), expr.type());
popcount_expr.add_source_location()=source_location;

Expand All @@ -2529,6 +2569,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

equal_exprt equality_expr(
expr.arguments().front(), expr.arguments().back());
equality_expr.add_source_location()=source_location;
Expand Down Expand Up @@ -2557,6 +2599,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

return typecast_exprt(expr.arguments()[0], expr.type());
}
else if(identifier=="__builtin_object_size")
Expand All @@ -2572,6 +2616,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

make_constant(expr.arguments()[1]);

mp_integer arg1;
Expand Down Expand Up @@ -2614,6 +2660,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt arg0 =
typecast_exprt::conditional_cast(expr.arguments()[0], bool_typet());
make_constant(arg0);
Expand All @@ -2634,6 +2682,9 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

// do not typecheck the argument - it is never evaluated, and thus side
// effects must not show up either

// try to produce constant
exprt tmp1=expr.arguments().front();
simplify(tmp1, *this);
Expand Down Expand Up @@ -2671,6 +2722,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt object=expr.arguments()[0];

// The value doesn't matter at all, we only care about the type.
Expand Down Expand Up @@ -2743,6 +2796,8 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

typecheck_function_call_arguments(expr);

exprt &ptr_arg=expr.arguments().front();

if(ptr_arg.type().id()!=ID_pointer)
Expand Down