Skip to content

Commit 9037d49

Browse files
committed
Introduce facilities for tracking dynamically allocated memory
memory_scopet keeps the starting point and allocated size for a malloc(ed) site. We also include helper methods to query about dynamic allocation.
1 parent 86f4770 commit 9037d49

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/memory-analyzer/analyze_symbol.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Author: Malte Mues <[email protected]>
77
88
\*******************************************************************/
99

10+
#include <cstdlib>
11+
1012
#include "analyze_symbol.h"
1113

1214
#include <util/c_types.h>
@@ -29,6 +31,66 @@ gdb_value_extractort::gdb_value_extractort(
2931
{
3032
}
3133

34+
bool gdb_value_extractort::memory_scopet::contains(
35+
const memory_addresst &point) const
36+
{
37+
size_t begin_int = std::strtoul(begin.address_string.c_str(), NULL, 0);
38+
size_t point_int = std::strtoul(point.address_string.c_str(), NULL, 0);
39+
return point_int >= begin_int && (begin_int + byte_size) > point_int;
40+
}
41+
42+
mp_integer gdb_value_extractort::memory_scopet::distance(
43+
const memory_addresst &point,
44+
mp_integer member_size) const
45+
{
46+
CHECK_RETURN(contains(point));
47+
size_t begin_int = std::strtoul(begin.address_string.c_str(), NULL, 0);
48+
size_t point_int = std::strtoul(point.address_string.c_str(), NULL, 0);
49+
return (point_int - begin_int) / member_size;
50+
}
51+
52+
std::vector<gdb_value_extractort::memory_scopet>::iterator
53+
gdb_value_extractort::find_dynamic_allocation(irep_idt name)
54+
{
55+
return std::find_if(
56+
dynamically_allocated.begin(),
57+
dynamically_allocated.end(),
58+
[&name](const memory_scopet &scope) { return scope.name == name; });
59+
}
60+
61+
std::vector<gdb_value_extractort::memory_scopet>::iterator
62+
gdb_value_extractort::find_dynamic_allocation(const memory_addresst &point)
63+
{
64+
return std::find_if(
65+
dynamically_allocated.begin(),
66+
dynamically_allocated.end(),
67+
[&point](const memory_scopet &memory_scope) {
68+
return memory_scope.contains(point);
69+
});
70+
}
71+
72+
optionalt<mp_integer> gdb_value_extractort::get_malloc_size(irep_idt name)
73+
{
74+
const auto scope_it = find_dynamic_allocation(name);
75+
if(scope_it == dynamically_allocated.end())
76+
return {};
77+
else
78+
return scope_it->byte_size;
79+
}
80+
81+
optionalt<std::string> gdb_value_extractort::get_malloc_pointee(
82+
const memory_addresst &point,
83+
mp_integer member_size)
84+
{
85+
const auto scope_it = find_dynamic_allocation(point);
86+
if(scope_it == dynamically_allocated.end())
87+
return {};
88+
89+
const auto pointer_distance = scope_it->distance(point, member_size);
90+
return id2string(scope_it->name) +
91+
(pointer_distance > 0 ? "+" + integer2string(pointer_distance) : "");
92+
}
93+
3294
void gdb_value_extractort::analyze_symbols(
3395
const std::vector<std::string> &symbols)
3496
{

src/memory-analyzer/analyze_symbol.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,70 @@ class gdb_value_extractort
9191
/// value of `symbol`.
9292
std::map<memory_addresst, exprt> values;
9393

94+
struct memory_scopet
95+
{
96+
memory_addresst begin;
97+
mp_integer byte_size;
98+
irep_idt name;
99+
100+
memory_scopet() = delete;
101+
memory_scopet(
102+
const memory_addresst &begin,
103+
mp_integer byte_size,
104+
irep_idt name)
105+
: begin(begin), byte_size(byte_size), name(name)
106+
{
107+
}
108+
109+
/// Check if \p point points somewhere in this memory scope
110+
/// \param point: memory address to be check for presence
111+
/// \return true if \p point is inside *this
112+
bool contains(const memory_addresst &point) const;
113+
114+
/// Compute the distance of \p point from the beginning of this scope
115+
/// \param point: memory address to have the offset computed
116+
/// \param member_size: size of one element of this scope in bytes
117+
/// \return `n' such that \p point is the n-th element of this scope
118+
mp_integer
119+
distance(const memory_addresst &point, mp_integer member_size) const;
120+
};
121+
122+
/// Keep track of the dynamically allocated memory
123+
std::vector<memory_scopet> dynamically_allocated;
124+
125+
/// Keep track of the memory location for the analyzed symbols
126+
std::map<std::string, pointer_valuet> memory_map;
127+
128+
/// Search for a memory scope allocated under \p name
129+
/// \param name: name of the pointer used during allocation
130+
/// \return iterator to the right memory scope
131+
std::vector<memory_scopet>::iterator find_dynamic_allocation(irep_idt name);
132+
133+
/// Search for a memory scope allocated under \p name
134+
/// \param point: potentially dynamically allocated memory address
135+
/// \return iterator to the right memory scope
136+
std::vector<memory_scopet>::iterator
137+
find_dynamic_allocation(const memory_addresst &point);
138+
139+
/// Search for the size of the allocated memory for \p name
140+
/// \param name: name of the pointer used during allocation
141+
/// \return the size if have a record of \p name's allocation
142+
optionalt<mp_integer> get_malloc_size(irep_idt name);
143+
144+
/// Build the pointee string for address \p point assuming it points to a
145+
/// dynamic allocation of `n' elements each of size \p member_size. E.g.:
146+
///
147+
/// int *p = (int*)malloc(sizeof(int)*4);
148+
/// int *q = &(p[2]);
149+
///
150+
/// get_malloc_pointee(get_memory(q), sizeof(int)) -> "p+8"
151+
///
152+
/// \param point: potentially dynamically allocated memory address
153+
/// \param member_size: size of each allocated element
154+
/// \return pointee as a string if we have a record of the allocation
155+
optionalt<std::string>
156+
get_malloc_pointee(const memory_addresst &point, mp_integer member_size);
157+
94158
/// Assign the gdb-extracted value for \p symbol_name to its symbol
95159
/// expression and then process outstanding assignments that this
96160
/// extraction introduced.

0 commit comments

Comments
 (0)