cprover
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(jsont::arrayt::const_iterator
44  it=json.array.begin();
45  it!=json.array.end();
46  it++)
47  {
48  if(!it->is_object())
49  {
50  messaget message(message_handler);
51  message.error() << "expecting an array of objects "
52  << "in the taint file, but got "
53  << *it << messaget::eom;
54  return true;
55  }
56 
58 
59  const std::string kind=(*it)["kind"].value;
60  const std::string function=(*it)["function"].value;
61  const std::string where=(*it)["where"].value;
62  const std::string taint=(*it)["taint"].value;
63  const std::string message=(*it)["message"].value;
64  const std::string id=(*it)["id"].value;
65 
66  if(kind=="source")
68  else if(kind=="sink")
70  else if(kind=="sanitizer")
72  else
73  {
74  messaget message(message_handler);
75  message.error() << "taint rule must have \"kind\" which is "
76  "\"source\" or \"sink\" or \"sanitizer\""
77  << messaget::eom;
78  return true;
79  }
80 
81  if(function.empty())
82  {
83  messaget message(message_handler);
84  message.error() << "taint rule must have \"function\""
85  << messaget::eom;
86  return true;
87  }
88  else
89  rule.function_identifier=function;
90 
91  if(where=="return_value")
92  {
94  }
95  else if(where=="this")
96  {
98  }
99  else if(std::string(where, 0, 9)=="parameter")
100  {
102  rule.parameter_number=
103  safe_string2unsigned(std::string(where, 9, std::string::npos));
104  }
105  else
106  {
107  messaget message(message_handler);
108  message.error() << "taint rule must have \"where\""
109  << " which is \"return_value\" or \"this\" "
110  << "or \"parameter1\"..."
111  << messaget::eom;
112  return true;
113  }
114 
115  rule.taint=taint;
116  rule.message=message;
117  rule.id=id;
118 
119  dest.rules.push_back(rule);
120  }
121 
122  return false;
123 }
124 
125 void taint_parse_treet::rulet::output(std::ostream &out) const
126 {
127  if(!id.empty())
128  out << id << ": ";
129 
130  switch(kind)
131  {
132  case SOURCE: out << "SOURCE "; break;
133  case SINK: out << "SINK "; break;
134  case SANITIZER: out << "SANITIZER "; break;
135  }
136 
137  out << taint << " on ";
138 
139  switch(where)
140  {
141  case THIS: out << "this in " << function_identifier; break;
142  case PARAMETER: out << "parameter " << parameter_number << " of "
143  << function_identifier; break;
144  case RETURN_VALUE: out << "return value of " << function_identifier; break;
145  }
146 
147  out << '\n';
148 }
149 
150 void taint_parse_treet::output(std::ostream &out) const
151 {
152  for(const auto &rule : rules)
153  rule.output(out);
154 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:144
taint_parse_treet::rulet::taint
irep_idt taint
Definition: taint_parser.h:48
taint_parse_treet::rules
rulest rules
Definition: taint_parser.h:62
json
json_objectt json(const source_locationt &location)
Definition: json_expr.cpp:87
taint_parser.h
taint_parse_treet::rulet::parameter_number
unsigned parameter_number
Definition: taint_parser.h:49
taint_parse_treet
Definition: taint_parser.h:22
taint_parse_treet::rulet::RETURN_VALUE
Definition: taint_parser.h:29
messaget::eom
static eomt eom
Definition: message.h:284
taint_parse_treet::rulet::id
irep_idt id
Definition: taint_parser.h:46
jsont
Definition: json.h:23
json_parser.h
taint_parse_treet::rulet::function_identifier
irep_idt function_identifier
Definition: taint_parser.h:47
string2int.h
messaget::error
mstreamt & error() const
Definition: message.h:386
taint_parse_treet::rulet::THIS
Definition: taint_parser.h:29
taint_parse_treet::rulet::SANITIZER
Definition: taint_parser.h:28
message_handlert
Definition: message.h:24
taint_parser
bool taint_parser(const std::string &file_name, taint_parse_treet &dest, message_handlert &message_handler)
Definition: taint_parser.cpp:20
taint_parse_treet::rulet::output
void output(std::ostream &) const
Definition: taint_parser.cpp:125
taint_parse_treet::rulet::PARAMETER
Definition: taint_parser.h:29
safe_string2unsigned
unsigned safe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:54
taint_parse_treet::rulet::where
enum taint_parse_treet::rulet::@2 where
taint_parse_treet::rulet
Definition: taint_parser.h:25
taint_parse_treet::output
void output(std::ostream &) const
Definition: taint_parser.cpp:150
jsont::array
arrayt array
Definition: json.h:129
taint_parse_treet::rulet::message
std::string message
Definition: taint_parser.h:50
jsont::is_array
bool is_array() const
Definition: json.h:54
taint_parse_treet::rulet::SOURCE
Definition: taint_parser.h:28
taint_parse_treet::rulet::kind
enum taint_parse_treet::rulet::@1 kind
taint_parse_treet::rulet::SINK
Definition: taint_parser.h:28
parse_json
bool parse_json(std::istream &in, const std::string &filename, message_handlert &message_handler, jsont &dest)
Definition: json_parser.cpp:16