Add a method to get the malloc(ed) size estimate

Returning 1 if variable was not malloc(ed). The estimation is described in the
documentation.
This commit is contained in:
Petr Bauch 2019-05-20 11:00:10 +01:00
parent 1e8185c03e
commit 40bd28b921
2 changed files with 53 additions and 0 deletions

View File

@ -24,6 +24,7 @@ Author: Malte Mues <mail.mues@gmail.com>
#include <goto-programs/goto_model.h>
#include <util/prefix.h>
#include <util/string2int.h>
#include <util/string_utils.h>
#include <sys/wait.h>
@ -56,6 +57,47 @@ gdb_apit::~gdb_apit()
wait(NULL);
}
size_t gdb_apit::query_malloc_size(const std::string &pointer_expr)
{
#ifdef __linux__
write_to_gdb("-var-create tmp * malloc_usable_size(" + pointer_expr + ")");
#elif __APPLE__
write_to_gdb("-var-create tmp * malloc_size(" + pointer_expr + ")");
#else
// Under non-linux/OSX system we simple return 1, i.e. as if the \p
// pointer_expr was not dynamically allocated.
return 1;
#endif
if(!was_command_accepted())
{
return 1;
}
write_to_gdb("-var-evaluate-expression tmp");
gdb_output_recordt record = get_most_recent_record("^done", true);
write_to_gdb("-var-delete tmp");
check_command_accepted();
const auto it = record.find("value");
CHECK_RETURN(it != record.end());
const std::string value = it->second;
INVARIANT(
value.back() != '"' ||
(value.length() >= 2 && value[value.length() - 2] == '\\'),
"quotes should have been stripped off from value");
INVARIANT(value.back() != '\n', "value should not end in a newline");
const auto result = string2optional_size_t(value);
if(result.has_value())
return *result;
else
return 1;
}
void gdb_apit::create_gdb_process()
{
PRECONDITION(gdb_state == gdb_statet::NOT_CREATED);

View File

@ -90,6 +90,17 @@ public:
}
};
/// Get the allocated size estimate for a pointer by evaluating
/// `malloc_usable_size'. The function returns the number of usable bytes in
/// the block pointed to by the pointer to a block of memory allocated by
/// `malloc' or a related function. The value may be greater than the
/// requested size of the allocation because of alignment and minimum size
/// constraints.
/// \param pointer_expr: expression with a pointer name
/// \return 1 if the pointer was not allocated with malloc otherwise return
/// the result of calling `malloc_usable_size'
size_t query_malloc_size(const std::string &pointer_expr);
/// Create a new gdb process for analysing the binary indicated by the member
/// variable `binary`
void create_gdb_process();