CBMC
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 
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 
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 \"kind\" which is "
68  "\"source\" or \"sink\" or \"sanitizer\""
69  << messaget::eom;
70  return true;
71  }
72 
73  const std::string function = taint_spec["function"].value;
74 
75  if(function.empty())
76  {
77  messaget message(message_handler);
78  message.error() << "taint rule must have \"function\""
79  << messaget::eom;
80  return true;
81  }
82  else
83  rule.function_identifier=function;
84 
85  const std::string where = taint_spec["where"].value;
86 
87  if(where=="return_value")
88  {
90  }
91  else if(where == id2string(ID_this))
92  {
94  }
95  else if(std::string(where, 0, 9)=="parameter")
96  {
98  rule.parameter_number=
99  safe_string2unsigned(std::string(where, 9, std::string::npos));
100  }
101  else
102  {
103  messaget message(message_handler);
104  message.error() << "taint rule must have \"where\""
105  << " which is \"return_value\" or \"this\" "
106  << "or \"parameter1\"..."
107  << messaget::eom;
108  return true;
109  }
110 
111  rule.taint = taint_spec["taint"].value;
112  rule.message = taint_spec["message"].value;
113  rule.id = taint_spec["id"].value;
114 
115  dest.rules.push_back(rule);
116  }
117 
118  return false;
119 }
120 
121 void taint_parse_treet::rulet::output(std::ostream &out) const
122 {
123  if(!id.empty())
124  out << id << ": ";
125 
126  switch(kind)
127  {
128  case SOURCE: out << "SOURCE "; break;
129  case SINK: out << "SINK "; break;
130  case SANITIZER: out << "SANITIZER "; break;
131  }
132 
133  out << taint << " on ";
134 
135  switch(where)
136  {
137  case THIS: out << "this in " << function_identifier; break;
138  case PARAMETER: out << "parameter " << parameter_number << " of "
139  << function_identifier; break;
140  case RETURN_VALUE: out << "return value of " << function_identifier; break;
141  }
142 
143  out << '\n';
144 }
145 
146 void taint_parse_treet::output(std::ostream &out) const
147 {
148  for(const auto &rule : rules)
149  rule.output(out);
150 }
Definition: json.h:27
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
mstreamt & error() const
Definition: message.h:391
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)
Definition: json_parser.cpp:27
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:120
unsigned safe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:16
bool taint_parser(const std::string &file_name, taint_parse_treet &dest, message_handlert &message_handler)
Taint Parser.