cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
taint_parser.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Taint Parser
4
5
Author: Daniel Kroening, kroening@kroening.com
6
7
\*******************************************************************/
8
11
12
#include "
taint_parser.h
"
13
14
#include <ostream>
15
16
#include <
util/string2int.h
>
17
18
#include <
json/json_parser.h
>
19
20
bool
taint_parser
(
21
const
std::string &file_name,
22
taint_parse_treet
&dest,
23
message_handlert
&message_handler)
24
{
25
jsont
json
;
26
27
if
(
parse_json
(file_name, message_handler,
json
))
28
{
29
messaget
message(message_handler);
30
message.
error
() <<
"taint file is not a valid json file"
31
<<
messaget::eom
;
32
return
true
;
33
}
34
35
if
(!
json
.is_array())
36
{
37
messaget
message(message_handler);
38
message.
error
() <<
"expecting an array in the taint file, but got "
39
<<
json
<<
messaget::eom
;
40
return
true
;
41
}
42
43
for
(
const
auto
&taint_spec :
to_json_array
(
json
))
44
{
45
if
(!taint_spec.is_object())
46
{
47
messaget
message(message_handler);
48
message.
error
() <<
"expecting an array of objects "
49
<<
"in the taint file, but got "
<< taint_spec
50
<<
messaget::eom
;
51
return
true
;
52
}
53
54
taint_parse_treet::rulet
rule;
55
56
const
std::string kind = taint_spec[
"kind"
].value;
57
58
if
(kind==
"source"
)
59
rule.
kind
=
taint_parse_treet::rulet::SOURCE
;
60
else
if
(kind==
"sink"
)
61
rule.
kind
=
taint_parse_treet::rulet::SINK
;
62
else
if
(kind==
"sanitizer"
)
63
rule.
kind
=
taint_parse_treet::rulet::SANITIZER
;
64
else
65
{
66
messaget
message(message_handler);
67
message.
error
() <<
"taint rule must have "
<<
messaget::quote_begin
68
<<
"kind"
<<
messaget::quote_end
<<
" which is "
69
<<
messaget::quote_begin
<<
"source"
70
<<
messaget::quote_end
<<
" or "
<<
messaget::quote_begin
71
<<
"sink"
<<
messaget::quote_end
<<
" or "
72
<<
messaget::quote_begin
<<
"sanitizer"
73
<<
messaget::quote_end
<<
messaget::eom
;
74
return
true
;
75
}
76
77
const
std::string function = taint_spec[
"function"
].value;
78
79
if
(function.empty())
80
{
81
messaget
message(message_handler);
82
message.
error
() <<
"taint rule must have "
<<
messaget::quote_begin
83
<<
"function"
<<
messaget::quote_end
<<
messaget::eom
;
84
return
true
;
85
}
86
else
87
rule.
function_identifier
=function;
88
89
const
std::string where = taint_spec[
"where"
].value;
90
91
if
(where==
"return_value"
)
92
{
93
rule.
where
=
taint_parse_treet::rulet::RETURN_VALUE
;
94
}
95
else
if
(where ==
id2string
(ID_this))
96
{
97
rule.
where
=
taint_parse_treet::rulet::THIS
;
98
}
99
else
if
(std::string(where, 0, 9)==
"parameter"
)
100
{
101
rule.
where
=
taint_parse_treet::rulet::PARAMETER
;
102
rule.
parameter_number
=
103
safe_string2unsigned
(std::string_view{where}.substr(9));
104
}
105
else
106
{
107
messaget
message(message_handler);
108
message.
error
() <<
"taint rule must have "
<<
messaget::quote_begin
109
<<
"where"
<<
messaget::quote_end
<<
" which is "
110
<<
messaget::quote_begin
<<
"return_value"
111
<<
messaget::quote_end
<<
" or "
<<
messaget::quote_begin
112
<<
"this"
<<
messaget::quote_end
<<
" or "
113
<<
messaget::quote_begin
<<
"parameter1"
114
<<
messaget::quote_end
<<
"..."
<<
messaget::eom
;
115
return
true
;
116
}
117
118
rule.
taint
= taint_spec[
"taint"
].value;
119
rule.
message
= taint_spec[
"message"
].value;
120
rule.
id
= taint_spec[
"id"
].value;
121
122
dest.
rules
.push_back(rule);
123
}
124
125
return
false
;
126
}
127
128
void
taint_parse_treet::rulet::output
(std::ostream &out)
const
129
{
130
if
(!
id
.empty())
131
out <<
id
<<
": "
;
132
133
switch
(
kind
)
134
{
135
case
SOURCE
: out <<
"SOURCE "
;
break
;
136
case
SINK
: out <<
"SINK "
;
break
;
137
case
SANITIZER
: out <<
"SANITIZER "
;
break
;
138
}
139
140
out <<
taint
<<
" on "
;
141
142
switch
(
where
)
143
{
144
case
THIS
: out <<
"this in "
<<
function_identifier
;
break
;
145
case
PARAMETER
: out <<
"parameter "
<<
parameter_number
<<
" of "
146
<<
function_identifier
;
break
;
147
case
RETURN_VALUE
: out <<
"return value of "
<<
function_identifier
;
break
;
148
}
149
150
out <<
'\n'
;
151
}
152
153
void
taint_parse_treet::output
(std::ostream &out)
const
154
{
155
for
(
const
auto
&rule :
rules
)
156
rule.output(out);
157
}
jsont
Definition
json.h:27
message_handlert
Definition
message.h:27
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition
message.h:154
messaget::quote_begin
static const commandt quote_begin
Start quoted text.
Definition
message.h:389
messaget::error
mstreamt & error() const
Definition
message.h:401
messaget::quote_end
static const commandt quote_end
End quoted text.
Definition
message.h:393
messaget::eom
static eomt eom
Definition
message.h:289
taint_parse_treet::rulet
Definition
taint_parser.h:27
taint_parse_treet::rulet::message
std::string message
Definition
taint_parser.h:51
taint_parse_treet::rulet::PARAMETER
@ PARAMETER
Definition
taint_parser.h:30
taint_parse_treet::rulet::THIS
@ THIS
Definition
taint_parser.h:30
taint_parse_treet::rulet::RETURN_VALUE
@ RETURN_VALUE
Definition
taint_parser.h:30
taint_parse_treet::rulet::where
enum taint_parse_treet::rulet::@266372352306211172241013055243307244151213274033 where
taint_parse_treet::rulet::taint
irep_idt taint
Definition
taint_parser.h:49
taint_parse_treet::rulet::SANITIZER
@ SANITIZER
Definition
taint_parser.h:29
taint_parse_treet::rulet::SINK
@ SINK
Definition
taint_parser.h:29
taint_parse_treet::rulet::SOURCE
@ SOURCE
Definition
taint_parser.h:29
taint_parse_treet::rulet::id
irep_idt id
Definition
taint_parser.h:47
taint_parse_treet::rulet::kind
enum taint_parse_treet::rulet::@352225073340331062300151275042001006240003257334 kind
taint_parse_treet::rulet::output
void output(std::ostream &) const
Definition
taint_parser.cpp:128
taint_parse_treet::rulet::function_identifier
irep_idt function_identifier
Definition
taint_parser.h:48
taint_parse_treet::rulet::parameter_number
unsigned parameter_number
Definition
taint_parser.h:50
taint_parse_treet
Definition
taint_parser.h:24
taint_parse_treet::output
void output(std::ostream &) const
Definition
taint_parser.cpp:153
taint_parse_treet::rules
rulest rules
Definition
taint_parser.h:63
id2string
const std::string & id2string(const irep_idt &d)
Definition
irep.h:44
to_json_array
json_arrayt & to_json_array(jsont &json)
Definition
json.h:424
parse_json
bool parse_json(std::istream &in, const std::string &filename, message_handlert &message_handler, jsont &dest)
Definition
json_parser.cpp:27
json_parser.h
json
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Definition
properties.cpp:120
safe_string2unsigned
unsigned safe_string2unsigned(std::string_view str, int base)
Definition
string2int.cpp:16
string2int.h
taint_parser
bool taint_parser(const std::string &file_name, taint_parse_treet &dest, message_handlert &message_handler)
Definition
taint_parser.cpp:20
taint_parser.h
Taint Parser.
goto-analyzer
taint_parser.cpp
Generated by
1.17.0