Skip to content

goto-symex-is-constant: treat x * sizeof(t) and sizeof(t) * x alike #4574

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
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
6 changes: 4 additions & 2 deletions src/goto-symex/goto_symex_is_constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ class goto_symex_is_constantt : public is_constantt
{
if(expr.id() == ID_mult)
{
bool found_non_constant = false;

// propagate stuff with sizeof in it
forall_operands(it, expr)
{
if(it->find(ID_C_c_sizeof_type).is_not_nil())
return true;
else if(!is_constant(*it))
return false;
found_non_constant = true;
}

return true;
return !found_non_constant;
}
else if(expr.id() == ID_with)
{
Expand Down
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ SRC += analyses/ai/ai.cpp \
goto-programs/goto_trace_output.cpp \
goto-programs/xml_expr.cpp \
goto-symex/ssa_equation.cpp \
goto-symex/is_constant.cpp \
interpreter/interpreter.cpp \
json/json_parser.cpp \
json_symbol_table.cpp \
Expand Down
55 changes: 55 additions & 0 deletions unit/goto-symex/is_constant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************\

Module: Unit tests for goto_symex_is_constantt

Author: Diffblue Ltd.

\*******************************************************************/

#include <testing-utils/message.h>
#include <testing-utils/use_catch.h>

#include <goto-symex/goto_symex_is_constant.h>
#include <util/std_expr.h>
#include <util/std_types.h>

SCENARIO("goto-symex-is-constant", "[core][goto-symex][is_constant]")
{
signedbv_typet int_type(32);
constant_exprt sizeof_constant("4", int_type);
sizeof_constant.set(ID_C_c_sizeof_type, int_type);
symbol_exprt non_constant("x", int_type);

goto_symex_is_constantt is_constant;

GIVEN("Sizeof expression multiplied by a non-constant")
{
mult_exprt non_const_by_sizeof(non_constant, sizeof_constant);
mult_exprt sizeof_by_non_const(sizeof_constant, non_constant);
WHEN("We check whether goto-symex regards the expression as constant")
{
bool result1 = is_constant(non_const_by_sizeof);
bool result2 = is_constant(sizeof_by_non_const);

THEN("Should be constant")
{
REQUIRE(result1);
REQUIRE(result2);
}
}
}

GIVEN("Non-multiply expression involving a sizeof expression")
{
plus_exprt non_const_plus_sizeof(non_constant, sizeof_constant);
WHEN("We check whether goto-symex regards the expression as constant")
{
bool result = is_constant(non_const_plus_sizeof);

THEN("Should not be constant")
{
REQUIRE(!result);
}
}
}
}