Skip to content

Commit 02e4655

Browse files
author
thk123
committed
Modified the write stack to not throw away certain pointers
Pointers into arrays was creating a top stack even though the result was valid. Corrected so didn't throw them away (need to look at the base of the stack, not the top). Also made the tests verify that they don't create a top stack when they shouldn't. Also made it impossible to get the expression of a top stack.
1 parent a9f96e7 commit 02e4655

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/analyses/variable-sensitivity/write_stack.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ void write_stackt::construct_stack_to_pointer(
105105

106106
if(!top_stack)
107107
{
108-
// check the top of the stack is pointing at an array - we don't support
109-
// offset apart from within arrays
110-
std::shared_ptr<const write_stack_entryt> entry=
111-
*std::prev(stack.cend());
108+
// check the symbol at the bottom of the stack
109+
std::shared_ptr<const write_stack_entryt> entry=*stack.cbegin();
110+
INVARIANT(
111+
entry->get_access_expr().id()==ID_symbol,
112+
"The base should be an addressable location (i.e. symbol)");
113+
112114
if(entry->get_access_expr().type().id()!=ID_array)
113115
{
114116
top_stack=true;
@@ -175,6 +177,8 @@ void write_stackt::construct_stack_to_array_index(
175177
/// for top elements.
176178
exprt write_stackt::to_expression() const
177179
{
180+
// A top stack is useless and its expression should not be evaluated
181+
PRECONDITION(!is_top_value());
178182
exprt access_expr=nil_exprt();
179183
for(const std::shared_ptr<write_stack_entryt> &entry : stack)
180184
{

unit/analyses/variable-sensitivity/write_stack.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ SCENARIO("Constructing write stacks",
6464
THEN("The constructed stack should be &x")
6565
{
6666
auto stack=write_stackt(in_expr, environment, ns);
67+
REQUIRE_FALSE(stack.is_top_value());
6768
const exprt &out_expr=stack.to_expression();
6869

6970
CAPTURE(expr2c(out_expr, ns));
@@ -86,6 +87,7 @@ SCENARIO("Constructing write stacks",
8687
THEN("The constructed stack should be &a[0]")
8788
{
8889
auto stack=write_stackt(in_expr, environment, ns);
90+
REQUIRE_FALSE(stack.is_top_value());
8991
const exprt &out_expr=stack.to_expression();
9092

9193
CAPTURE(expr2c(out_expr, ns));
@@ -103,6 +105,7 @@ SCENARIO("Constructing write stacks",
103105
THEN("The constructed stack should be &a")
104106
{
105107
auto stack=write_stackt(in_expr, environment, ns);
108+
REQUIRE_FALSE(stack.is_top_value());
106109
const exprt &out_expr=stack.to_expression();
107110

108111
CAPTURE(expr2c(out_expr, ns));
@@ -120,6 +123,7 @@ SCENARIO("Constructing write stacks",
120123
THEN("The constructed stack should be &a[0]")
121124
{
122125
auto stack=write_stackt(in_expr, environment, ns);
126+
REQUIRE_FALSE(stack.is_top_value());
123127
const exprt &out_expr=stack.to_expression();
124128

125129
CAPTURE(expr2c(out_expr, ns));
@@ -139,6 +143,7 @@ SCENARIO("Constructing write stacks",
139143
THEN("The constructed stack should be &a[1]")
140144
{
141145
auto stack=write_stackt(in_expr, environment, ns);
146+
REQUIRE_FALSE(stack.is_top_value());
142147
const exprt &out_expr=stack.to_expression();
143148

144149
CAPTURE(expr2c(out_expr, ns));
@@ -157,6 +162,7 @@ SCENARIO("Constructing write stacks",
157162
THEN("The constructed stack should be &a[1]")
158163
{
159164
auto stack=write_stackt(in_expr, environment, ns);
165+
REQUIRE_FALSE(stack.is_top_value());
160166
const exprt &out_expr=stack.to_expression();
161167

162168
CAPTURE(expr2c(out_expr, ns));
@@ -176,6 +182,7 @@ SCENARIO("Constructing write stacks",
176182
THEN("The constructed stack should be &a[2]")
177183
{
178184
auto stack=write_stackt(in_expr, environment, ns);
185+
REQUIRE_FALSE(stack.is_top_value());
179186
const exprt &out_expr=stack.to_expression();
180187

181188
CAPTURE(expr2c(out_expr, ns));
@@ -195,6 +202,7 @@ SCENARIO("Constructing write stacks",
195202
THEN("The constructed stack should be &a[0]")
196203
{
197204
auto stack=write_stackt(in_expr, environment, ns);
205+
REQUIRE_FALSE(stack.is_top_value());
198206
const exprt &out_expr=stack.to_expression();
199207

200208
CAPTURE(expr2c(out_expr, ns));
@@ -214,6 +222,7 @@ SCENARIO("Constructing write stacks",
214222
THEN("The constructed stack should be &a[2]")
215223
{
216224
auto stack=write_stackt(in_expr, environment, ns);
225+
REQUIRE_FALSE(stack.is_top_value());
217226
const exprt &out_expr=stack.to_expression();
218227

219228
CAPTURE(expr2c(out_expr, ns));
@@ -239,6 +248,7 @@ SCENARIO("Constructing write stacks",
239248
THEN("The constructed stack should be &a[TOP]")
240249
{
241250
auto stack=write_stackt(in_expr, environment, ns);
251+
REQUIRE_FALSE(stack.is_top_value());
242252
const exprt &out_expr=stack.to_expression();
243253

244254
CAPTURE(expr2c(out_expr, ns));
@@ -265,6 +275,7 @@ SCENARIO("Constructing write stacks",
265275
THEN("The constructed stack should be &a[2]")
266276
{
267277
auto stack=write_stackt(in_expr, environment, ns);
278+
REQUIRE_FALSE(stack.is_top_value());
268279
const exprt &out_expr=stack.to_expression();
269280

270281
CAPTURE(expr2c(out_expr, ns));
@@ -308,6 +319,7 @@ SCENARIO("Constructing write stacks",
308319
THEN("The constructed stack should be &s.comp")
309320
{
310321
auto stack=write_stackt(in_expr, environment, ns);
322+
REQUIRE_FALSE(stack.is_top_value());
311323
const exprt &out_expr=stack.to_expression();
312324

313325
CAPTURE(expr2c(out_expr, ns));
@@ -327,6 +339,7 @@ SCENARIO("Constructing write stacks",
327339
THEN("The constructed stack should be &s.comp2")
328340
{
329341
auto stack=write_stackt(in_expr, environment, ns);
342+
REQUIRE_FALSE(stack.is_top_value());
330343
const exprt &out_expr=stack.to_expression();
331344

332345
CAPTURE(expr2c(out_expr, ns));
@@ -384,6 +397,7 @@ SCENARIO("Constructing write stacks",
384397
THEN("The constructed stack should be &arr_s[1].comp")
385398
{
386399
auto stack=write_stackt(in_expr, environment, ns);
400+
REQUIRE_FALSE(stack.is_top_value());
387401
const exprt &out_expr=stack.to_expression();
388402

389403
CAPTURE(expr2c(out_expr, ns));
@@ -424,6 +438,7 @@ SCENARIO("Constructing write stacks",
424438
THEN("The constructed stack should be &arr_s[TOP].comp")
425439
{
426440
auto stack=write_stackt(in_expr, environment, ns);
441+
REQUIRE_FALSE(stack.is_top_value());
427442
const exprt &out_expr=stack.to_expression();
428443

429444
CAPTURE(expr2c(out_expr, ns));

0 commit comments

Comments
 (0)