Skip to content

Commit 877f03a

Browse files
author
owen-jones-diffblue
authored
Merge pull request diffblue#260 from diffblue/owen/evs-changes
SEC-105 Cleanup LVSA code (nothing functional)
2 parents 16c8c75 + 826d001 commit 877f03a

File tree

6 files changed

+70
-65
lines changed

6 files changed

+70
-65
lines changed

cbmc/src/util/irep_ids.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,8 @@ IREP_ID_TWO(generic_types, #generic_types)
842842
IREP_ID_TWO(type_variables, #type_variables)
843843
IREP_ID_ONE(havoc_object)
844844
IREP_ID_TWO(overflow_shl, overflow-shl)
845+
IREP_ID_ONE(lvsa_mode)
846+
IREP_ID_ONE(is_initializer)
845847

846848
#undef IREP_ID_ONE
847849
#undef IREP_ID_TWO

src/pointer-analysis/evs_pretty_printer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ std::string evs_pretty_printert::convert(const exprt &src)
2121
result << top_pretty_printer->convert(*it);
2222
}
2323
result << ")";
24-
if(src.get_bool("is_initializer"))
24+
if(src.get_bool(ID_is_initializer))
2525
result << "<is-initializer>";
2626
result << " ["
2727
<< top_pretty_printer->convert(src.type())

src/pointer-analysis/external_value_set_expr.h

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ class external_value_set_exprt:public exprt
8181
// The default-initalised version of EVS doesn't represent anything sane;
8282
// the caller should set label() before using an instance constructed
8383
// this way.
84-
operands().resize(1);
85-
operands()[0]=constant_exprt();
84+
operands().resize(2);
85+
op0()=constant_exprt();
86+
/// No need to initialise op1(). op1().operands() will hold the vector of
87+
/// access path entries.
8688
}
8789

8890
external_value_set_exprt(
@@ -92,15 +94,17 @@ class external_value_set_exprt:public exprt
9294
bool is_initializer):
9395
exprt(ID_external_value_set, type)
9496
{
95-
operands().resize(1);
96-
operands()[0]=_label;
97-
set("#lva_mode", std::to_string(static_cast<int>(mode)));
98-
set("is_initializer", std::to_string(is_initializer ? 1 : 0));
97+
operands().resize(2);
98+
op0()=_label;
99+
/// No need to initialise op1(). op1().operands() will hold the vector of
100+
/// access path entries.
101+
set(ID_lvsa_mode, std::to_string(static_cast<int>(mode)));
102+
set(ID_is_initializer, std::to_string(is_initializer ? 1 : 0));
99103
}
100104

101105
local_value_set_analysis_modet analysis_mode() const
102106
{
103-
return (local_value_set_analysis_modet)get_int("#lva_mode");
107+
return (local_value_set_analysis_modet)get_int(ID_lvsa_mode);
104108
}
105109

106110
/// At the beginning of a function, the value set for EVS("A.b") is
@@ -112,38 +116,32 @@ class external_value_set_exprt:public exprt
112116
/// from a different A.b.
113117
bool is_initializer() const
114118
{
115-
return get_bool("is_initializer");
119+
return get_bool(ID_is_initializer);
116120
}
117121

118122
constant_exprt &label() { return to_constant_expr(op0()); }
119123
const constant_exprt &label() const { return to_constant_expr(op0()); }
120124

121-
size_t access_path_size() const { return operands().size()-1; }
122-
access_path_entry_exprt &access_path_entry(size_t index)
123-
{
124-
return to_access_path_entry(operands()[index+1]);
125-
}
126-
const access_path_entry_exprt &access_path_entry(size_t index) const
127-
{
128-
return to_access_path_entry(operands()[index+1]);
129-
}
130-
const access_path_entry_exprt &access_path_back() const
125+
typedef std::vector<access_path_entry_exprt> access_path_entriest;
126+
127+
access_path_entriest &access_path_entries()
131128
{
132-
assert(access_path_size()!=0);
133-
return to_access_path_entry(operands().back());
129+
return reinterpret_cast<access_path_entriest &>(op1().operands());
134130
}
135-
void access_path_push_back(const access_path_entry_exprt &newentry)
131+
132+
const access_path_entriest &access_path_entries() const
136133
{
137-
copy_to_operands(newentry);
134+
return reinterpret_cast<const access_path_entriest &>(op1().operands());
138135
}
136+
139137
std::string get_access_path_label() const
140138
{
141139
if(analysis_mode()==LOCAL_VALUE_SET_ANALYSIS_SINGLE_EXTERNAL_SET)
142140
return "external_objects";
143141
std::ostringstream ret;
144142
ret << id2string(label().get_value());
145-
for(size_t i=0; i!=access_path_size(); ++i)
146-
ret << id2string(access_path_entry(i).label());
143+
for(const access_path_entry_exprt &entry : access_path_entries())
144+
ret << id2string(entry.label());
147145
return ret.str();
148146
}
149147

@@ -162,26 +160,27 @@ class external_value_set_exprt:public exprt
162160
{
163161
if(analysis_mode()==LOCAL_VALUE_SET_ANALYSIS_SINGLE_EXTERNAL_SET)
164162
return "external_objects"+type_to_basename(declared_on_type);
165-
assert(access_path_size()!=0);
163+
assert(!access_path_entries().empty());
166164
std::ostringstream ret;
167165
ret << id2string(label().get_value());
168-
for(size_t i=0; i!=access_path_size()-1; ++i)
169-
ret << id2string(access_path_entry(i).label());
166+
for(
167+
auto it=access_path_entries().begin();
168+
it!=std::prev(access_path_entries().end());
169+
++it)
170+
{
171+
ret << id2string(it->label());
172+
}
170173
return ret.str();
171174
}
172175
bool access_path_loops() const
173176
{
174-
if(access_path_size()<2)
177+
if(access_path_entries().size()<2)
175178
return false;
176-
return access_path_entry(access_path_size()-2).is_loop();
179+
return access_path_entries()[access_path_entries().size()-2].is_loop();
177180
}
178181
void create_access_path_loop()
179182
{
180-
copy_to_operands(access_path_entry_exprt::get_loop_tag());
181-
}
182-
void replace_access_path_back(const access_path_entry_exprt &newtail)
183-
{
184-
operands()[operands().size()-1]=newtail;
183+
access_path_entries().push_back(access_path_entry_exprt::get_loop_tag());
185184
}
186185

187186
void extend_access_path(const access_path_entry_exprt &newentry)
@@ -194,29 +193,29 @@ class external_value_set_exprt:public exprt
194193
// In this case entries created at different
195194
// locations are not distinguished:
196195
toadd.drop_loc();
197-
if(access_path_size()==0)
198-
access_path_push_back(toadd);
196+
if(access_path_entries().empty())
197+
access_path_entries().push_back(toadd);
199198
else
200-
replace_access_path_back(toadd);
199+
access_path_entries().back()=toadd;
201200
}
202201
else
203202
{
204203
if(access_path_loops())
205204
{
206205
// Replace the existing tail field with this one.
207-
replace_access_path_back(newentry);
206+
access_path_entries().back()=newentry;
208207
}
209208
else
210209
{
211-
for(size_t i=0; i!=access_path_size(); ++i)
210+
for(const auto &entry : access_path_entries())
212211
{
213-
if(access_path_entry(i).label()==newentry.label())
212+
if(entry.label()==newentry.label())
214213
{
215214
create_access_path_loop();
216215
break;
217216
}
218217
}
219-
access_path_push_back(newentry);
218+
access_path_entries().push_back(newentry);
220219
}
221220
}
222221
}
@@ -228,7 +227,7 @@ class external_value_set_exprt:public exprt
228227
else
229228
{
230229
external_value_set_exprt copy=*this;
231-
copy.set("is_initializer", ID_0);
230+
copy.set(ID_is_initializer, ID_0);
232231
return copy;
233232
}
234233
}

src/pointer-analysis/local_value_set.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ void local_value_sett::make_union_adjusting_evs_types(
3939
std::pair<value_sett::valuest::iterator, bool>
4040
local_value_sett::init_external_value_set(const external_value_set_exprt &evse)
4141
{
42-
const auto &apback=evse.access_path_back();
42+
PRECONDITION(!evse.access_path_entries().empty());
43+
const auto &apback=evse.access_path_entries().back();
4344
std::string basename=evse.get_access_path_basename(apback.declared_on_type());
4445
std::string access_path_suffix=id2string(apback.label());
4546
std::string entryname=basename+access_path_suffix;
@@ -134,9 +135,7 @@ static const typet &type_from_suffix(
134135
return *ret;
135136
}
136137

137-
// Override value_sett to provide behaviour for external value sets:
138-
// Note all overridden cases must *return* if they don't want to fall through
139-
// to value_sett's behaviour afterwards.
138+
// Override value_sett to provide behaviour for external value sets
140139
void local_value_sett::get_value_set_rec(
141140
const exprt &expr,
142141
object_mapt &dest,
@@ -157,7 +156,6 @@ void local_value_sett::get_value_set_rec(
157156
get_value_set_rec(expr.op0(), tmp, suffix, original_type, ns);
158157
// ...except to fix up the types of external objects as they pass through:
159158
make_union_adjusting_evs_types(dest, tmp, expr.type().subtype());
160-
return;
161159
}
162160
}
163161
else if(expr.id()==ID_side_effect)
@@ -170,9 +168,12 @@ void local_value_sett::get_value_set_rec(
170168
assert(expr.type().id()==ID_pointer && "non-pointer-typed malloc?");
171169
malloc_copy.set(ID_C_cxx_alloc_type, expr.type().subtype());
172170
baset::get_value_set_rec(malloc_copy, dest, suffix, original_type, ns);
173-
return;
174171
}
175-
// Other kinds of side-effect fall through to base class handler.
172+
else
173+
{
174+
// For other kinds of side-effect, pass on to the base class handler.
175+
baset::get_value_set_rec(expr, dest, suffix, original_type, ns);
176+
}
176177
}
177178
else if(expr.id()==ID_external_value_set)
178179
{
@@ -218,11 +219,12 @@ void local_value_sett::get_value_set_rec(
218219
// Deref-of-external yields a scalar type.
219220
insert(dest, exprt(ID_unknown, original_type));
220221
}
221-
return;
222222
}
223-
224-
// For everything else, use base behaviour:
225-
baset::get_value_set_rec(expr, dest, suffix, original_type, ns);
223+
else
224+
{
225+
// For everything else, pass on to the base class handler
226+
baset::get_value_set_rec(expr, dest, suffix, original_type, ns);
227+
}
226228
}
227229

228230
/// Applying side effects needed in Recency Analysis.
@@ -511,15 +513,16 @@ void local_value_sett::apply_code(
511513
throw "dead expected to have symbol on lhs";
512514

513515
assign(lhs, exprt(ID_invalid), ns, true, false);
514-
return;
515516
}
516517
else if(statement=="set_may" ||
517518
statement=="get_may" ||
518519
statement=="clear_may")
519520
{
520-
return;
521+
// Do nothing
522+
}
523+
else
524+
{
525+
// Otherwise fall back to base:
526+
baset::apply_code(code, ns);
521527
}
522-
523-
// Otherwise fall back to base:
524-
baset::apply_code(code, ns);
525528
}

src/pointer-analysis/local_value_set_analysis.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ void local_value_set_analysist::transform_function_stub_single_external_set(
162162
{
163163
std::vector<local_value_sett::entryt*> rhs_entries;
164164
const auto &evse=to_external_value_set(assignment.second);
165-
const auto &apback=evse.access_path_back();
165+
PRECONDITION(!evse.access_path_entries().empty());
166+
const auto &apback=evse.access_path_entries().back();
166167
if(!evse.is_initializer())
167168
{
168169
get_all_field_value_sets(
@@ -186,7 +187,7 @@ void local_value_set_analysist::transform_function_stub_single_external_set(
186187
{
187188
// This should be an external value set assigned
188189
// to initialise some global or parameter.
189-
assert(evse.access_path_size()==0);
190+
assert(evse.access_path_entries().empty());
190191
const symbolt &inflow_symbol=
191192
ns.lookup(to_constant_expr(evse.label()).get_value());
192193
exprt inflow_expr;

src/taint-analysis/taint_summary.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ void expand_external_objects(
9292
const auto& label=to_constant_expr(evse.label()).get_value();
9393
if(label=="external_objects")
9494
{
95-
assert(evse.access_path_size()==1);
96-
const auto fieldname=evse.access_path_back().label();
95+
assert(evse.access_path_entries().size()==1);
96+
const auto fieldname=evse.access_path_entries().back().label();
9797
auto findit=by_fieldname.find(fieldname);
9898
if(findit!=by_fieldname.end())
9999
new_keys.insert(
@@ -720,10 +720,10 @@ void taint_algorithm_computing_summary_of_functiont::initialise_domain(
720720
{
721721
const auto& evse=to_external_value_set(
722722
numbering->at(input.first));
723-
if(evse.access_path_size()==1)
723+
if(evse.access_path_entries().size()==1)
724724
numbering_map
725725
->get_object_numbers_by_field()[as_string(function_id)]
726-
.insert({ evse.access_path_back().label(), {} });
726+
.insert({ evse.access_path_entries().back().label(), {} });
727727
}
728728
}
729729
for(const std::pair<taint_lvalue_numbert, taint_sett> &lvalue_taint:

0 commit comments

Comments
 (0)