Skip to content

Commit a7aae94

Browse files
authored
Merge pull request diffblue#209 from diffblue/smowton/feature/lvsa_json_dump
Add LVSA domain JSON dump support
2 parents e565ae2 + 7ece542 commit a7aae94

File tree

9 files changed

+123
-10
lines changed

9 files changed

+123
-10
lines changed

cbmc/src/analyses/static_analysis.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <cassert>
1616
#include <memory>
1717

18+
#include <util/json_irep.h>
1819
#include <util/std_expr.h>
1920
#include <util/std_code.h>
2021

@@ -110,6 +111,34 @@ void static_analysis_baset::output(
110111
}
111112
}
112113

114+
jsont static_analysis_baset::output_json(
115+
const goto_programt &goto_program,
116+
const irep_idt &identifier) const
117+
{
118+
json_arrayt retval;
119+
json_irept irep_converter(true);
120+
121+
forall_goto_program_instructions(i_it, goto_program)
122+
{
123+
json_objectt instruction_object;
124+
instruction_object["location_number"]=
125+
json_numbert(std::to_string(i_it->location_number));
126+
instruction_object["source_location"]=
127+
irep_converter.convert_from_irep(i_it->source_location);
128+
instruction_object["code"]=
129+
irep_converter.convert_from_irep(i_it->code);
130+
{
131+
std::ostringstream pretty_type;
132+
pretty_type << i_it->type;
133+
instruction_object["instruction_type"]=json_stringt(pretty_type.str());
134+
}
135+
instruction_object["state"]=get_state(i_it).output_json(ns);
136+
retval.push_back(instruction_object);
137+
}
138+
139+
return retval;
140+
}
141+
113142
void static_analysis_baset::generate_states(
114143
const goto_functionst &goto_functions)
115144
{

cbmc/src/analyses/static_analysis.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Author: Daniel Kroening, [email protected]
2121
#include <memory>
2222
#include <unordered_set>
2323

24+
#include <util/json.h>
2425
#include <util/make_unique.h>
2526

2627
#include <goto-programs/goto_functions.h>
@@ -67,6 +68,11 @@ class domain_baset
6768
{
6869
}
6970

71+
virtual jsont output_json(const namespacet &ns) const
72+
{
73+
return json_stringt("");
74+
}
75+
7076
typedef std::unordered_set<exprt, irep_hash> expr_sett;
7177

7278
// will go away
@@ -157,6 +163,11 @@ class static_analysis_baset
157163
output(goto_program, "", out);
158164
}
159165

166+
jsont output_json(const goto_programt &goto_program) const
167+
{
168+
return output_json(goto_program, "");
169+
}
170+
160171
virtual bool has_location(locationt l) const=0;
161172

162173
void insert(locationt l)
@@ -183,6 +194,10 @@ class static_analysis_baset
183194
const irep_idt &identifier,
184195
std::ostream &out) const;
185196

197+
virtual jsont output_json(
198+
const goto_programt &goto_program,
199+
const irep_idt &identifier) const;
200+
186201
typedef std::map<unsigned, locationt> working_sett;
187202

188203
locationt get_next(working_sett &working_set);

cbmc/src/pointer-analysis/value_set.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <util/arith_tools.h>
2121
#include <util/pointer_offset_size.h>
2222
#include <util/cprover_prefix.h>
23+
#include <util/json_irep.h>
2324

2425
#include <util/c_types.h>
2526

@@ -192,6 +193,33 @@ void value_sett::output(
192193
}
193194
}
194195

196+
jsont value_sett::output_json(const namespacet &) const
197+
{
198+
json_arrayt retval;
199+
json_irept irep_converter(true);
200+
201+
for(const auto &name_values : values)
202+
{
203+
json_objectt json_entry;
204+
205+
json_entry["id"]=json_stringt(id2string(name_values.second.identifier));
206+
json_entry["suffix"]=json_stringt(id2string(name_values.second.suffix));
207+
208+
json_arrayt vals;
209+
for(const auto &obj_entry : name_values.second.object_map.read())
210+
{
211+
vals.push_back(
212+
irep_converter.convert_from_irep(object_numbering[obj_entry.first]));
213+
}
214+
215+
json_entry["values"]=vals;
216+
217+
retval.push_back(json_entry);
218+
}
219+
220+
return retval;
221+
}
222+
195223
exprt value_sett::to_expr(const object_map_dt::value_type &it) const
196224
{
197225
const exprt &object=object_numbering[it.first];

cbmc/src/pointer-analysis/value_set.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <set>
1616

17+
#include <util/json.h>
1718
#include <util/mp_arith.h>
1819
#include <util/reference_counting.h>
1920

@@ -206,6 +207,8 @@ class value_sett
206207
const namespacet &ns,
207208
std::ostream &out) const;
208209

210+
jsont output_json(const namespacet &) const;
211+
209212
valuest values;
210213

211214
// true = added something new

cbmc/src/pointer-analysis/value_set_domain.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,39 @@ class value_set_domaint:public domain_baset
3030
return value_set.make_union(other.value_set);
3131
}
3232

33-
virtual void output(
33+
void output(
3434
const namespacet &ns,
35-
std::ostream &out) const
35+
std::ostream &out) const override
3636
{
3737
value_set.output(ns, out);
3838
}
3939

40-
virtual void initialize(
40+
jsont output_json(const namespacet &ns) const override
41+
{
42+
return value_set.output_json(ns);
43+
}
44+
45+
void initialize(
4146
const namespacet &ns,
42-
locationt l)
47+
locationt l) override
4348
{
4449
value_set.clear();
4550
value_set.location_number=l->location_number;
4651
value_set.function=l->function;
4752
}
4853

49-
virtual void get_reference_set(
54+
void get_reference_set(
5055
const namespacet &ns,
5156
const exprt &expr,
52-
value_setst::valuest &dest)
57+
value_setst::valuest &dest) override
5358
{
5459
value_set.get_reference_set(expr, dest, ns);
5560
}
5661

57-
virtual void transform(
62+
void transform(
5863
const namespacet &ns,
5964
locationt from_l,
60-
locationt to_l)
65+
locationt to_l) override
6166
{
6267
switch(from_l->type)
6368
{

cbmc/src/util/ui_message.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include "message.h"
1414

15+
#define UI_MESSAGE_OPTIONS \
16+
"(json-ui)" \
17+
"(xml-ui)" \
18+
// End of options
19+
1520
class ui_message_handlert:public message_handlert
1621
{
1722
public:

src/driver/sec_driver_parse_options.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ int sec_driver_parse_optionst::doit()
201201
get_inverted_topological_order(call_graph,goto_model.goto_functions,process_order);
202202
size_t total_funcs=process_order.size();
203203
size_t processed=0;
204+
json_objectt json_function_descriptions;
204205
for(const auto& fname : process_order)
205206
{
206207
++processed;
@@ -217,12 +218,37 @@ int sec_driver_parse_optionst::doit()
217218
value_set_analysis(gf.body);
218219
if(cmdline.isset("show-value-sets"))
219220
{
220-
status() << "*** function " << fname << eom;
221-
value_set_analysis.output(gf.body, std::cout);
221+
switch(get_ui())
222+
{
223+
case ui_message_handlert::uit::PLAIN:
224+
{
225+
status() << "*** function " << fname << '\n';
226+
value_set_analysis.output(gf.body, status());
227+
status() << eom;
228+
break;
229+
}
230+
case ui_message_handlert::uit::JSON_UI:
231+
{
232+
json_function_descriptions[id2string(fname)]=
233+
value_set_analysis.output_json(gf.body);
234+
break;
235+
}
236+
default:
237+
INVARIANT(false, "LVSA summary dump: UI type not handled");
238+
}
222239
}
223240
if(dbpath.size()!=0)
224241
value_set_analysis.save_summary(gf.body);
225242
}
243+
244+
if(cmdline.isset("show-value-sets") &&
245+
get_ui()==ui_message_handlert::uit::JSON_UI)
246+
{
247+
json_objectt dump_message;
248+
dump_message["messageType"]=json_stringt("LVSA-ALL-FUNCTIONS-DUMP");
249+
dump_message["functions"]=json_function_descriptions;
250+
status() << dump_message << eom;
251+
}
226252
}
227253
return 0;
228254
}

src/driver/sec_driver_parse_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class optionst;
3939
"(local-value-set-analysis)(show-value-sets)(lvsa-function):" \
4040
"(security-scanner):" \
4141
"(rebuild-taint-cache)" \
42+
UI_MESSAGE_OPTIONS \
4243
// End of options
4344

4445

src/pointer-analysis/local_value_set.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define SECURITY_SCANNER_POINTER_ANALYSIS_LOCAL_VALUE_SET_H
88

99
#include <pointer-analysis/value_set.h>
10+
#include <util/json.h>
1011
#include "external_value_set_expr.h"
1112

1213
class local_value_sett:public value_sett

0 commit comments

Comments
 (0)