Skip to content

Commit 5a0fccb

Browse files
committed
Restore member-of-union expressions in trace output
byte_extract operations precisely capture the semantics, but aren't meaningful to users.
1 parent 99f1a2e commit 5a0fccb

File tree

6 files changed

+80
-5
lines changed

6 files changed

+80
-5
lines changed

regression/cbmc/xml-trace/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ VERIFICATION FAILED
77
<assignment assignment_type="state" base_name="u" display_name="test::u" hidden="false" identifier="test::u" mode="C" step_nr="\d+" thread="0">
88
<location file=".*" function="test" line="\d+" working-directory=".*"/>
99
<type>union myunion</type>
10-
<full_lhs>byte_extract_little_endian\(u, 0ll?, .*int.*\)</full_lhs>
10+
<full_lhs>u</full_lhs>
1111
<full_lhs_value binary="[01]+">\d+ll?</full_lhs_value>
1212
<value>\{ \.i=\d+ll? \}</value>
1313
<value_expression>

src/goto-programs/goto_trace.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void goto_trace_stept::output(
128128
if(!comment.empty())
129129
out << " " << comment << '\n';
130130

131-
out << " " << format(pc->get_condition()) << '\n';
131+
out << " " << format(original_condition) << '\n';
132132
out << '\n';
133133
}
134134
}
@@ -414,7 +414,7 @@ void show_compact_goto_trace(
414414
if(step.pc->is_assert())
415415
{
416416
out << " "
417-
<< from_expr(ns, step.function_id, step.pc->get_condition())
417+
<< from_expr(ns, step.function_id, step.original_condition)
418418
<< '\n';
419419
}
420420

@@ -541,7 +541,7 @@ void show_full_goto_trace(
541541
if(step.pc->is_assert())
542542
{
543543
out << " "
544-
<< from_expr(ns, step.function_id, step.pc->get_condition())
544+
<< from_expr(ns, step.function_id, step.original_condition)
545545
<< '\n';
546546
}
547547

@@ -558,7 +558,7 @@ void show_full_goto_trace(
558558
if(!step.pc->source_location.is_nil())
559559
out << " " << step.pc->source_location << '\n';
560560

561-
out << " " << from_expr(ns, step.function_id, step.pc->get_condition())
561+
out << " " << from_expr(ns, step.function_id, step.original_condition)
562562
<< '\n';
563563
}
564564
break;

src/goto-programs/goto_trace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class goto_trace_stept
119119
// for assume, assert, goto
120120
bool cond_value;
121121
exprt cond_expr;
122+
exprt original_condition;
122123

123124
// for assert
124125
irep_idt property_id;
@@ -163,6 +164,7 @@ class goto_trace_stept
163164
full_lhs.make_nil();
164165
full_lhs_value.make_nil();
165166
cond_expr.make_nil();
167+
original_condition.make_nil();
166168
}
167169

168170
/// Use \p dest to establish sharing among ireps.

src/goto-programs/rewrite_union.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/byte_operators.h>
1616
#include <util/c_types.h>
1717
#include <util/pointer_expr.h>
18+
#include <util/pointer_offset_size.h>
1819
#include <util/std_code.h>
1920
#include <util/std_expr.h>
2021

@@ -124,3 +125,63 @@ void rewrite_union(goto_modelt &goto_model)
124125
{
125126
rewrite_union(goto_model.goto_functions);
126127
}
128+
129+
/// Undo the union access -> byte_extract replacement that rewrite_union did for
130+
/// the purpose of displaying expressions to users.
131+
/// \param expr: expression to inspect and possibly transform
132+
/// \param ns: namespace
133+
/// \return True if, and only if, the expression is unmodified
134+
static bool restore_union_rec(exprt &expr, const namespacet &ns)
135+
{
136+
bool unmodified = true;
137+
138+
Forall_operands(it, expr)
139+
unmodified &= restore_union_rec(*it, ns);
140+
141+
if(expr.id() == byte_extract_id())
142+
{
143+
byte_extract_exprt &be = to_byte_extract_expr(expr);
144+
if(be.op().type().id() == ID_union || be.op().type().id() == ID_union_tag)
145+
{
146+
const union_typet &union_type = to_union_type(ns.follow(be.op().type()));
147+
148+
for(const auto &comp : union_type.components())
149+
{
150+
if(be.offset().is_zero() && be.type() == comp.type())
151+
{
152+
expr = member_exprt{be.op(), comp.get_name(), be.type()};
153+
return false;
154+
}
155+
else if(
156+
comp.type().id() == ID_array || comp.type().id() == ID_struct ||
157+
comp.type().id() == ID_struct_tag)
158+
{
159+
optionalt<exprt> result = get_subexpression_at_offset(
160+
member_exprt{be.op(), comp.get_name(), comp.type()},
161+
be.offset(),
162+
be.type(),
163+
ns);
164+
if(result.has_value())
165+
{
166+
expr = *result;
167+
return false;
168+
}
169+
}
170+
}
171+
}
172+
}
173+
174+
return unmodified;
175+
}
176+
177+
/// Undo the union access -> byte_extract replacement that rewrite_union did for
178+
/// the purpose of displaying expressions to users.
179+
/// \param expr: expression to inspect and possibly transform
180+
/// \param ns: namespace
181+
void restore_union(exprt &expr, const namespacet &ns)
182+
{
183+
exprt tmp = expr;
184+
185+
if(!restore_union_rec(tmp, ns))
186+
expr.swap(tmp);
187+
}

src/goto-programs/rewrite_union.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ void rewrite_union(goto_functionst::goto_functiont &);
2424
void rewrite_union(goto_functionst &);
2525
void rewrite_union(goto_modelt &);
2626

27+
void restore_union(exprt &, const namespacet &);
28+
2729
#endif // CPROVER_GOTO_PROGRAMS_REWRITE_UNION_H

src/goto-symex/build_goto_trace.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Author: Daniel Kroening
2222

2323
#include <solvers/decision_procedure.h>
2424

25+
#include <goto-programs/rewrite_union.h>
26+
2527
#include "partial_order_concurrency.h"
2628

2729
static exprt build_full_lhs_rec(
@@ -378,6 +380,7 @@ void build_goto_trace(
378380
SSA_step.ssa_full_lhs),
379381
ns);
380382
replace_nondet_in_type(goto_trace_step.full_lhs, decision_procedure);
383+
restore_union(goto_trace_step.full_lhs, ns);
381384
}
382385

383386
if(SSA_step.ssa_full_lhs.is_not_nil())
@@ -410,6 +413,13 @@ void build_goto_trace(
410413
decision_procedure.get(SSA_step.cond_handle).is_true();
411414
}
412415

416+
if(SSA_step.source.pc->is_assert() || SSA_step.source.pc->is_assume())
417+
{
418+
goto_trace_step.original_condition =
419+
SSA_step.source.pc->get_condition();
420+
restore_union(goto_trace_step.original_condition, ns);
421+
}
422+
413423
if(ssa_step_it == last_step_to_keep)
414424
return;
415425
}

0 commit comments

Comments
 (0)