Skip to content

Commit 0d11eb4

Browse files
Merge pull request diffblue#505 from diffblue/feature/in-memory-lvsa-summaries
In-memory LVSA summaries
2 parents 9e6fa7c + 69d8943 commit 0d11eb4

File tree

6 files changed

+233
-154
lines changed

6 files changed

+233
-154
lines changed

src/driver/sec_driver_parse_options.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ int sec_driver_parse_optionst::doit()
349349
return CPROVER_EXIT_USAGE_ERROR;
350350
}
351351

352-
json_map_serializert<irep_idt, lvsaa_single_external_set_summaryt>
353-
serializer(dbpath, id2string, get_message_handler());
354-
local_value_set_analysist::dbt summarydb(serializer, 30);
352+
local_value_set_analysist::dbt summarydb;
355353
namespacet ns(goto_model.symbol_table);
356354

357355
// The last parameter of LVSA's constructor creates LVSA in debug mode,

src/pointer-analysis/local_value_set_analysis.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#ifndef LOCAL_VALUE_SET_ANALYSIS_H
77
#define LOCAL_VALUE_SET_ANALYSIS_H
88

9-
#include <pointer-analysis/value_set_analysis.h>
109
#include "local_value_set.h"
1110
#include "local_value_set_domain.h"
11+
#include <goto-programs/class_hierarchy.h>
1212
#include <pointer-analysis/external_value_set_expr.h>
13+
#include <pointer-analysis/value_set_analysis.h>
1314
#include <summaries/summary.h>
15+
#include <util/in_memory_map.h>
1416
#include <util/message.h>
15-
#include <util/cached_map.h>
16-
#include <goto-programs/class_hierarchy.h>
1717

1818
/// Represents a local-value-set-analysis function summary.
1919
/// This is a set of simultaneous assignments, of the form:
@@ -134,7 +134,7 @@ class local_value_set_analysist
134134
{
135135
public:
136136
typedef value_set_analysis_templatet<local_value_set_domaint> baset;
137-
typedef cached_mapt<irep_idt, lvsaa_single_external_set_summaryt> dbt;
137+
typedef in_memory_mapt<irep_idt, lvsaa_single_external_set_summaryt> dbt;
138138

139139
/// Counts calls to `transform_function_stub`
140140
size_t nstubs;

src/taint-analysis/taint_security_scanner.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,7 @@ bool taint_do_security_scan(
111111
std::shared_ptr<taint_serializer_traitst> traits =
112112
std::make_shared<taint_serializer_traitst>(numbering, named_tokens);
113113

114-
json_map_serializert<irep_idt, lvsaa_single_external_set_summaryt>
115-
lvsa_serializer(
116-
config.get_lvsa_summaries_root_directory(),
117-
id2string,
118-
logger.get_message_handler());
119-
lvsa_serializer.set_traits(traits);
120-
local_value_set_analysist::dbt lvsa_summaries(lvsa_serializer, 100000);
114+
local_value_set_analysist::dbt lvsa_summaries;
121115

122116
statistics.end_loading_lvsa_database();
123117

src/util/cached_map.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class cached_mapt final
212212
Class: gen_iteratort
213213
214214
Purpose:
215-
An iterator implementation for cached_map.
215+
An iterator implementation for cached_mapt.
216216
217217
\*******************************************************************/
218218
template<bool is_const>

src/util/in_memory_map.h

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Copyright 2016-2017 Diffblue Limited. All Rights Reserved.
2+
3+
/// \file
4+
/// A map-like data structure that holds all entries in memory and implements
5+
/// the same interface as cached_mapt.
6+
7+
#ifndef CPROVER_UTIL_IN_MEMORY_MAP_H
8+
#define CPROVER_UTIL_IN_MEMORY_MAP_H
9+
10+
#include "virtual_map.h"
11+
#include <boost/range/adaptor/map.hpp>
12+
#include <map>
13+
#include <memory>
14+
15+
/// A map that holds all entries in memory and implements the same interface as
16+
/// cached_mapt
17+
/// \tparam keyt: The type of keys of the map.
18+
/// \tparam valuet: The type of values of the map.
19+
template <typename keyt, typename valuet>
20+
class in_memory_mapt final
21+
{
22+
public:
23+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
24+
typedef keyt key_type;
25+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
26+
typedef valuet mapped_type;
27+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
28+
typedef std::pair<const key_type, std::shared_ptr<mapped_type>> value_type;
29+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
30+
typedef value_type &reference;
31+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
32+
typedef const value_type &const_reference;
33+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
34+
typedef value_type *pointer;
35+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
36+
typedef const value_type *const_pointer;
37+
typedef std::unordered_map<key_type, std::shared_ptr<mapped_type>> storet;
38+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
39+
typedef typename storet::size_type size_type;
40+
41+
private:
42+
mutable storet store;
43+
44+
private:
45+
in_memory_mapt(const in_memory_mapt &) = delete;
46+
in_memory_mapt &operator=(const in_memory_mapt &) = delete;
47+
48+
public:
49+
/// Creates a in_memory_mapt.
50+
in_memory_mapt()
51+
{
52+
}
53+
54+
/// Returns the size of the cache.
55+
/// \returns The size of the cache.
56+
size_type cache_size() const
57+
{
58+
return store.size();
59+
}
60+
61+
/// Returns the number of locked elements, equal to the size in this class.
62+
/// \returns The number of locked elements.
63+
size_type locked_count() const
64+
{
65+
return store.size();
66+
}
67+
68+
/// Returns the size of the map.
69+
/// \returns The size of the map.
70+
size_type size() const
71+
{
72+
return store.size();
73+
}
74+
75+
/// Returns all the keys in the map.
76+
/// \returns The set of all keys in the map.
77+
typename virtual_mapt<key_type, mapped_type>::keys_ranget keys() const
78+
{
79+
return store.keys();
80+
}
81+
82+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
83+
typedef typename storet::iterator iterator;
84+
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
85+
typedef typename storet::const_iterator const_iterator;
86+
87+
iterator begin()
88+
{
89+
return store.begin();
90+
}
91+
92+
iterator end()
93+
{
94+
return store.end();
95+
}
96+
97+
const_iterator begin() const
98+
{
99+
return store.begin();
100+
}
101+
102+
const_iterator end() const
103+
{
104+
return store.end();
105+
}
106+
107+
/// Checks whether a given key exists in the map.
108+
/// \param key: The key to search for.
109+
/// \returns True if the map contains the given key.
110+
bool contains(const key_type &key) const
111+
{
112+
return store.count(key) != 0;
113+
}
114+
115+
/// Adds a key-value pair to the map.
116+
/// \param key: The key of the key-value pair to add.
117+
/// \param value: The value of the key-value pair to add.
118+
/// \returns
119+
/// A pair, the first element of which is a shared_pointer to the value
120+
/// corresponding to key now in the map and the second element of which
121+
/// is true if and only if the key did not already exist in the map and the
122+
/// new element has been added.
123+
/// \remarks
124+
/// If the value argument is an r-value at the call site it will be moved
125+
/// into the cache. Otherwise you need a copy constructor on mapped_type.
126+
std::pair<std::shared_ptr<mapped_type>, bool> insert(
127+
const key_type &key, mapped_type value)
128+
{
129+
auto it = store.find(key);
130+
if(it != store.end())
131+
return { it->second, false };
132+
// Create and return shared pointer
133+
std::shared_ptr<mapped_type> elt_shared_ptr =
134+
std::make_shared(std::move(value));
135+
store.insert({ key, elt_shared_ptr });
136+
return { elt_shared_ptr, true };
137+
}
138+
139+
/// Gets the value corresponding to a given key, inserting a new key-value
140+
/// pair if one does not already exist.
141+
/// \param key: The key to search for.
142+
/// \returns A shared_pointer to the corresponding to the given key.
143+
/// \remarks Requires a default constructor on mapped_type.
144+
std::shared_ptr<mapped_type> operator[](const key_type &key)
145+
{
146+
auto it = store.find(key);
147+
if(it != store.end())
148+
return it->second;
149+
// Create and return shared pointer
150+
auto elt_shared_ptr = std::make_shared<mapped_type>();
151+
store.insert({ key, elt_shared_ptr });
152+
return elt_shared_ptr;
153+
}
154+
155+
/// Gets the value corresponding to a given key.
156+
/// \param key: The key to search for.
157+
/// \returns A shared_pointer to the value corresponding to the given key.
158+
std::shared_ptr<mapped_type> at(const key_type &key) const
159+
{
160+
return store.at(key);
161+
}
162+
163+
/// Removes a key-value pair from the map.
164+
/// \param key: The key of the key-value pair to remove.
165+
/// \returns Whether the key-value pair could be deleted.
166+
/// \remarks: If this function returns false, no changes are made to the map.
167+
bool erase(const key_type &key)
168+
{
169+
store.erase(key);
170+
return true;
171+
}
172+
173+
/// Removes all key-value pairs from the map.
174+
/// \returns Whether the map could be cleared.
175+
/// \remarks: If this function returns false, no changes are made to the map.
176+
bool clear()
177+
{
178+
store.clear();
179+
return true;
180+
}
181+
182+
/// No effect in this class.
183+
void flush()
184+
{
185+
}
186+
187+
/// Totally removes an element from the map.
188+
/// \param key: A key whose corresponding value should be unloaded.
189+
/// \returns True, if the referenced element was unloaded, and false otherwise.
190+
bool unload(const key_type &key)
191+
{
192+
return erase(key);
193+
}
194+
};
195+
196+
#endif

0 commit comments

Comments
 (0)