Skip to content

Commit c8a7e27

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 9da9a47 commit c8a7e27

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
@@ -66,6 +66,7 @@ SCENARIO("Constructing write stacks",
6666
THEN("The constructed stack should be &x")
6767
{
6868
auto stack=write_stackt(in_expr, environment, ns);
69+
REQUIRE_FALSE(stack.is_top_value());
6970
const exprt &out_expr=stack.to_expression();
7071

7172
CAPTURE(expr2c(out_expr, ns));
@@ -88,6 +89,7 @@ SCENARIO("Constructing write stacks",
8889
THEN("The constructed stack should be &a[0]")
8990
{
9091
auto stack=write_stackt(in_expr, environment, ns);
92+
REQUIRE_FALSE(stack.is_top_value());
9193
const exprt &out_expr=stack.to_expression();
9294

9395
CAPTURE(expr2c(out_expr, ns));
@@ -105,6 +107,7 @@ SCENARIO("Constructing write stacks",
105107
THEN("The constructed stack should be &a")
106108
{
107109
auto stack=write_stackt(in_expr, environment, ns);
110+
REQUIRE_FALSE(stack.is_top_value());
108111
const exprt &out_expr=stack.to_expression();
109112

110113
CAPTURE(expr2c(out_expr, ns));
@@ -122,6 +125,7 @@ SCENARIO("Constructing write stacks",
122125
THEN("The constructed stack should be &a[0]")
123126
{
124127
auto stack=write_stackt(in_expr, environment, ns);
128+
REQUIRE_FALSE(stack.is_top_value());
125129
const exprt &out_expr=stack.to_expression();
126130

127131
CAPTURE(expr2c(out_expr, ns));
@@ -141,6 +145,7 @@ SCENARIO("Constructing write stacks",
141145
THEN("The constructed stack should be &a[1]")
142146
{
143147
auto stack=write_stackt(in_expr, environment, ns);
148+
REQUIRE_FALSE(stack.is_top_value());
144149
const exprt &out_expr=stack.to_expression();
145150

146151
CAPTURE(expr2c(out_expr, ns));
@@ -159,6 +164,7 @@ SCENARIO("Constructing write stacks",
159164
THEN("The constructed stack should be &a[1]")
160165
{
161166
auto stack=write_stackt(in_expr, environment, ns);
167+
REQUIRE_FALSE(stack.is_top_value());
162168
const exprt &out_expr=stack.to_expression();
163169

164170
CAPTURE(expr2c(out_expr, ns));
@@ -178,6 +184,7 @@ SCENARIO("Constructing write stacks",
178184
THEN("The constructed stack should be &a[2]")
179185
{
180186
auto stack=write_stackt(in_expr, environment, ns);
187+
REQUIRE_FALSE(stack.is_top_value());
181188
const exprt &out_expr=stack.to_expression();
182189

183190
CAPTURE(expr2c(out_expr, ns));
@@ -197,6 +204,7 @@ SCENARIO("Constructing write stacks",
197204
THEN("The constructed stack should be &a[0]")
198205
{
199206
auto stack=write_stackt(in_expr, environment, ns);
207+
REQUIRE_FALSE(stack.is_top_value());
200208
const exprt &out_expr=stack.to_expression();
201209

202210
CAPTURE(expr2c(out_expr, ns));
@@ -216,6 +224,7 @@ SCENARIO("Constructing write stacks",
216224
THEN("The constructed stack should be &a[2]")
217225
{
218226
auto stack=write_stackt(in_expr, environment, ns);
227+
REQUIRE_FALSE(stack.is_top_value());
219228
const exprt &out_expr=stack.to_expression();
220229

221230
CAPTURE(expr2c(out_expr, ns));
@@ -241,6 +250,7 @@ SCENARIO("Constructing write stacks",
241250
THEN("The constructed stack should be &a[TOP]")
242251
{
243252
auto stack=write_stackt(in_expr, environment, ns);
253+
REQUIRE_FALSE(stack.is_top_value());
244254
const exprt &out_expr=stack.to_expression();
245255

246256
CAPTURE(expr2c(out_expr, ns));
@@ -267,6 +277,7 @@ SCENARIO("Constructing write stacks",
267277
THEN("The constructed stack should be &a[2]")
268278
{
269279
auto stack=write_stackt(in_expr, environment, ns);
280+
REQUIRE_FALSE(stack.is_top_value());
270281
const exprt &out_expr=stack.to_expression();
271282

272283
CAPTURE(expr2c(out_expr, ns));
@@ -310,6 +321,7 @@ SCENARIO("Constructing write stacks",
310321
THEN("The constructed stack should be &s.comp")
311322
{
312323
auto stack=write_stackt(in_expr, environment, ns);
324+
REQUIRE_FALSE(stack.is_top_value());
313325
const exprt &out_expr=stack.to_expression();
314326

315327
CAPTURE(expr2c(out_expr, ns));
@@ -329,6 +341,7 @@ SCENARIO("Constructing write stacks",
329341
THEN("The constructed stack should be &s.comp2")
330342
{
331343
auto stack=write_stackt(in_expr, environment, ns);
344+
REQUIRE_FALSE(stack.is_top_value());
332345
const exprt &out_expr=stack.to_expression();
333346

334347
CAPTURE(expr2c(out_expr, ns));
@@ -386,6 +399,7 @@ SCENARIO("Constructing write stacks",
386399
THEN("The constructed stack should be &arr_s[1].comp")
387400
{
388401
auto stack=write_stackt(in_expr, environment, ns);
402+
REQUIRE_FALSE(stack.is_top_value());
389403
const exprt &out_expr=stack.to_expression();
390404

391405
CAPTURE(expr2c(out_expr, ns));
@@ -426,6 +440,7 @@ SCENARIO("Constructing write stacks",
426440
THEN("The constructed stack should be &arr_s[TOP].comp")
427441
{
428442
auto stack=write_stackt(in_expr, environment, ns);
443+
REQUIRE_FALSE(stack.is_top_value());
429444
const exprt &out_expr=stack.to_expression();
430445

431446
CAPTURE(expr2c(out_expr, ns));

0 commit comments

Comments
 (0)