cprover
java_class_loader_limit.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: limit class path loading
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
13 
14 #include <json/json_parser.h>
15 
19  const std::string &java_cp_include_files)
20 {
21  if(java_cp_include_files.empty())
22  throw "class regexp cannot be empty, `get_language_options` not called?";
23 
24  // '@' signals file reading with list of class files to load
25  use_regex_match = java_cp_include_files[0] != '@';
26  if(use_regex_match)
27  {
28  regex_matcher=std::regex(java_cp_include_files);
29  debug() << "Limit loading to classes matching '" << java_cp_include_files
30  << "'" << eom;
31  }
32  else
33  {
34  assert(java_cp_include_files.length()>1);
35  jsont json_cp_config;
36  if(parse_json(
37  java_cp_include_files.substr(1),
39  json_cp_config))
40  throw "cannot read JSON input configuration for JAR loading";
41  if(!json_cp_config.is_object())
42  throw "the JSON file has a wrong format";
43  jsont include_files=json_cp_config["classFiles"];
44  if(!include_files.is_null() && !include_files.is_array())
45  throw "the JSON file has a wrong format";
46  for(const jsont &file_entry : include_files.array)
47  {
48  assert(file_entry.is_string());
49  set_matcher.insert(file_entry.value);
50  }
51  }
52 }
53 
56 bool java_class_loader_limitt::load_class_file(const std::string &file_name)
57 {
58  if(use_regex_match)
59  {
60  std::smatch string_matches;
61  if(std::regex_match(file_name, string_matches, regex_matcher))
62  return true;
63  debug() << file_name + " discarded since not matching loader regexp" << eom;
64  return false;
65  }
66  else
67  // load .class file only if it is in the match set
68  return set_matcher.find(file_name) != set_matcher.end();
69 }
Definition: json.h:23
static mstreamt & eom(mstreamt &m)
Definition: message.h:272
bool parse_json(std::istream &in, const std::string &filename, message_handlert &message_handler, jsont &dest)
Definition: json_parser.cpp:16
std::set< std::string > set_matcher
bool is_null() const
Definition: json.h:69
message_handlert & get_message_handler()
Definition: message.h:153
bool is_string() const
Definition: json.h:39
bool is_array() const
Definition: json.h:54
std::string value
Definition: json.h:137
bool load_class_file(const std::string &class_file_name)
mstreamt & debug() const
Definition: message.h:332
arrayt array
Definition: json.h:129
bool use_regex_match
Whether to use regex_matcher instead of set_matcher.
void setup_class_load_limit(const std::string &)
initializes class with either regex matcher or match set
limit class path loading