|
From: George Y. <gy...@ul...> - 2014-09-19 00:11:03
|
Good afternoon,
This small patch adds the functionality to Memgrind to fail all memory
allocations after the specific count (meaning, if you use
--fail-memory-alloc=100 any memory allocation attempt after 100th will
fail). This is very useful to see whether the application crashes or
behaves abnormally when handling failed memory allocations. It is
supposed to be wrapped up in the loop, which runs the application
increasing the counter value by 1 after each successive run.
Signed-off-by: George Yunavev <gy...@ul...>
---
diff -upr valgrind-3.10.0.orig/memcheck/mc_include.h valgrind-3.10.0/memcheck/mc_include.h
--- valgrind-3.10.0.orig/memcheck/mc_include.h 2014-09-08 05:28:18.000000000 -0700
+++ valgrind-3.10.0/memcheck/mc_include.h 2014-09-17 23:54:34.372494648 -0700
@@ -501,6 +501,10 @@ extern UInt MC_(clo_show_leak_kinds);
Default : R2S(Possible) | R2S(Unreached). */
extern UInt MC_(clo_errors_for_leak_kinds);
+/* If nonzero, specifies which memory allocation sequence call (and all
+ subsequent calls) will return the failure. Default: none (0) */
+extern Long MC_(clo_fail_alloc_number);
+
/* Various leak check heuristics which can be activated/deactivated. */
typedef
enum {
diff -upr valgrind-3.10.0.orig/memcheck/mc_main.c valgrind-3.10.0/memcheck/mc_main.c
--- valgrind-3.10.0.orig/memcheck/mc_main.c 2014-09-08 05:28:17.000000000 -0700
+++ valgrind-3.10.0/memcheck/mc_main.c 2014-09-18 15:24:21.430193157 -0700
@@ -5195,6 +5195,7 @@ Int MC_(clo_free_fill)
KeepStacktraces MC_(clo_keep_stacktraces) = KS_alloc_then_free;
Int MC_(clo_mc_level) = 2;
Bool MC_(clo_show_mismatched_frees) = True;
+Long MC_(clo_fail_alloc_number) = 0;
static const HChar * MC_(parse_leak_heuristics_tokens) =
"-,stdstring,length64,newarray,multipleinheritance";
@@ -5342,6 +5343,10 @@ static Bool mc_process_cmd_line_options(
else if VG_BOOL_CLO(arg, "--show-mismatched-frees",
MC_(clo_show_mismatched_frees)) {}
+ else if VG_BINT_CLO(arg, "--fail-memory-alloc",
+ MC_(clo_fail_alloc_number),
+ 0, 10*1000*1000*1000LL) {}
+
else
return VG_(replacement_malloc_process_cmd_line_option)(arg);
@@ -5390,6 +5395,8 @@ static void mc_print_usage(void)
" --keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none\n"
" stack trace(s) to keep for malloc'd/free'd areas [alloc-then-free]\n"
" --show-mismatched-frees=no|yes show frees that don't match the allocator? [yes]\n"
+" --fail-memory-alloc=<number> starting from <number> force all subsequent\n"
+" memory allocations to fail\n"
, plo_default
);
}
diff -upr valgrind-3.10.0.orig/memcheck/mc_malloc_wrappers.c valgrind-3.10.0/memcheck/mc_malloc_wrappers.c
--- valgrind-3.10.0.orig/memcheck/mc_malloc_wrappers.c 2014-09-08 05:28:05.000000000 -0700
+++ valgrind-3.10.0/memcheck/mc_malloc_wrappers.c 2014-09-18 15:22:34.752195646 -0700
@@ -347,6 +347,12 @@ void* MC_(new_block) ( ThreadId tid,
tl_assert(MC_AllocCustom == kind);
} else {
tl_assert(MC_AllocCustom != kind);
+
+ // Make the memory allocation fail if the counter exceeds
+ if ( MC_(clo_fail_alloc_number) != 0 && cmalloc_n_mallocs >= MC_(clo_fail_alloc_number) ) {
+ return NULL;
+ }
+
p = (Addr)VG_(cli_malloc)( alignB, szB );
if (!p) {
return NULL;
|