Skip to content

Commit c1f9237

Browse files
committed
Make memory-analyzer compatible with i386
We previously hard-coded x86_64 calling conventions for malloc.
1 parent 0794f5b commit c1f9237

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

regression/memory-analyzer/chain.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@ args=$(echo $cmd | cut -d ' ' -f 2-)
1212
name=${name%.gb}
1313
opts=${*:3:$#-3}
1414

15-
$goto_gcc -g -std=c11 -o "${name}.gb" "${name}.c"
15+
bit_width=`$memory_analyzer -h | grep -- -bit | sed 's/-bit.*//' | sed 's/.* //'`
16+
if [[ "$bit_width" != "64" ]] && [[ $(uname -m) = "x86_64" ]]; then
17+
$goto_gcc -g -m32 -std=c11 -o "${name}.gb" "${name}.c"
18+
else
19+
$goto_gcc -g -std=c11 -o "${name}.gb" "${name}.c"
20+
fi
1621
$memory_analyzer $opts "${name}.gb" $args

src/memory-analyzer/analyze_symbol.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ exprt gdb_value_extractort::get_non_char_pointer_value(
393393

394394
// Check if pointer was dynamically allocated (via malloc). If so we will
395395
// replace the pointee with a static array filled with values stored at the
396-
// expected positions. Since the allocated size is over-approximation we may
397-
// end up querying pass the allocated bounds and building larger array with
398-
// meaningless values.
396+
// expected positions. Since the allocated size is an over-approximation we
397+
// may end up querying past the allocated bounds and building a larger array
398+
// with meaningless values.
399399
mp_integer allocated_size = get_malloc_size(c_converter.convert(expr));
400400
// get the sizeof(target_type) and thus the number of elements
401401
const auto number_of_elements = allocated_size / get_type_size(target_type);
@@ -406,7 +406,7 @@ exprt gdb_value_extractort::get_non_char_pointer_value(
406406
for(size_t i = 0; i < number_of_elements; i++)
407407
{
408408
const auto sub_expr_value = get_expr_value(
409-
index_exprt{expr, from_integer(i, index_type())},
409+
dereference_exprt{plus_exprt{expr, from_integer(i, index_type())}},
410410
*zero_expr,
411411
location);
412412
elements.push_back(sub_expr_value);

src/memory-analyzer/gdb_api.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ void gdb_apit::run_gdb_from_core(const std::string &corefile)
287287

288288
void gdb_apit::collect_malloc_calls()
289289
{
290-
// this is what the registers look like at the function call entry:
290+
#if defined(__x86_64__)
291+
// this is what the registers look like at the function call entry for x86-64:
291292
//
292293
// reg. name hex. value dec. value
293294
// 0: rax 0xffffffff 4294967295
@@ -303,6 +304,17 @@ void gdb_apit::collect_malloc_calls()
303304
write_to_gdb("-data-list-register-values d 5");
304305
auto record = get_most_recent_record("^done", true);
305306
auto allocated_size = safe_string2size_t(get_register_value(record));
307+
#elif defined(__i386__)
308+
// x86 32-bit Linux calling conventions use the stack to pass arguments. The
309+
// top of the stack is the return address, so look at the next element (+4 as
310+
// the stack grows downwards).
311+
write_to_gdb("-data-evaluate-expression \"*(unsigned long*)($esp + 4)\"");
312+
auto record = get_most_recent_record("^done", true);
313+
auto allocated_size =
314+
safe_string2size_t(get_value_from_record(record, "value"));
315+
#else
316+
# error malloc calling conventions not know for current platform
317+
#endif
306318

307319
write_to_gdb("-exec-finish");
308320
if(!most_recent_line_has_tag("*running"))
@@ -324,7 +336,7 @@ void gdb_apit::collect_malloc_calls()
324336
record = get_most_recent_record("*stopped");
325337
}
326338

327-
// now we can read the rax register to the the allocated memory address
339+
// now we can read the eax/rax register to the allocated memory address
328340
write_to_gdb("-data-list-register-values x 0");
329341
record = get_most_recent_record("^done", true);
330342
allocated_memory[get_register_value(record)] = allocated_size;

0 commit comments

Comments
 (0)