CBMC
Loading...
Searching...
No Matches
taint_parser.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Taint Parser
4
5Author: 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
21 const std::string &file_name,
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"
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
51 return true;
52 }
53
55
56 const std::string kind = taint_spec["kind"].value;
57
58 if(kind=="source")
60 else if(kind=="sink")
62 else if(kind=="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"
71 << "sink" << messaget::quote_end << " or "
72 << messaget::quote_begin << "sanitizer"
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 {
94 }
95 else if(where == id2string(ID_this))
96 {
98 }
99 else if(std::string(where, 0, 9)=="parameter")
100 {
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"
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
128void 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
153void taint_parse_treet::output(std::ostream &out) const
154{
155 for(const auto &rule : rules)
156 rule.output(out);
157}
virtual void output(const namespacet &ns, const irep_idt &function_id, const goto_programt &goto_program, std::ostream &out) const
Output the abstract states for a single function.
Definition ai.cpp:39
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
Definition json.h:27
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:154
static const commandt quote_begin
Start quoted text.
Definition message.h:389
mstreamt & error() const
Definition message.h:401
static const commandt quote_end
End quoted text.
Definition message.h:393
static eomt eom
Definition message.h:289
enum taint_parse_treet::rulet::@2 where
void output(std::ostream &) const
enum taint_parse_treet::rulet::@1 kind
void output(std::ostream &) const
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
json_arrayt & to_json_array(jsont &json)
Definition json.h:424
bool parse_json(std::istream &in, const std::string &filename, message_handlert &message_handler, jsont &dest)
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
unsigned safe_string2unsigned(std::string_view str, int base)
bool taint_parser(const std::string &file_name, taint_parse_treet &dest, message_handlert &message_handler)
Taint Parser.