memory debug: allow setting pre-defined malloc failure points

MALLOC_FAIL_AT flag can be used to set-up a pre-determined failure
point during malloc calls. The counter value is retrieved using
getenv().
Example usage: export MALLOC_FAIL_AT=37 && cwebp input.png
will make 'cwebp' report a memory allocation error the 37th time
malloc() or calloc() is called.

MALLOC_MEM_LIMIT can be used similarly to prevent allocating more
than a given amount of memory. This is usually less convenient to
use than MALLOC_FAIL_AT since one has to know in advance the typical
memory size allocated.

Both these flags are meant to be used for debugging only!

Also: added a 'total_mem_allocated' to record the overall memory allocated

Change-Id: I9d408095ee7d76acba0f3a31b1276fc36478720a
This commit is contained in:
skal 2014-05-05 14:01:33 -07:00
parent ca3d746e39
commit f948d08c81

View File

@ -14,9 +14,9 @@
#include <stdlib.h>
#include "./utils.h"
// If defined, will print extra info like total memory used, number of
// alloc/free etc. For debugging/tuning purpose only (it's slow, and not
// multi-thread safe!).
// If PRINT_MEM_INFO is defined, extra info (like total memory used, number of
// alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,
// and not multi-thread safe!).
// An interesting alternative is valgrind's 'massif' tool:
// http://valgrind.org/docs/manual/ms-manual.html
// Here is an example command line:
@ -24,9 +24,22 @@
--stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc
ms_print massif.out
*/
// In addition:
// * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles
// are printed.
// * if MALLOC_FAIL_AT is defined, the global environment variable
// $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc
// is called for the nth time. Example usage:
// export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png
// * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT
// sets the maximum amount of memory (in bytes) made available to libwebp.
// This can be used to emulate environment with very limited memory.
// Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp
// #define PRINT_MEM_INFO
#define PRINT_MEM_TRAFFIC // print fine traffic details
// #define PRINT_MEM_TRAFFIC
// #define MALLOC_FAIL_AT
// #define MALLOC_LIMIT
//------------------------------------------------------------------------------
// Checked memory allocation
@ -39,6 +52,7 @@
static int num_malloc_calls = 0;
static int num_calloc_calls = 0;
static int num_free_calls = 0;
static int countdown_to_fail = 0; // 0 = off
typedef struct MemBlock MemBlock;
struct MemBlock {
@ -49,15 +63,19 @@ struct MemBlock {
static MemBlock* all_blocks = NULL;
static size_t total_mem = 0;
static size_t total_mem_allocated = 0;
static size_t high_water_mark = 0;
static size_t mem_limit = 0;
static int exit_registered = 0;
static void PrintMemInfo(void) {
fprintf(stderr, "\nMEMORY INFO:\n");
fprintf(stderr, "num calls to: malloc = %d\n", num_malloc_calls);
fprintf(stderr, " calloc = %d\n", num_calloc_calls);
fprintf(stderr, " free = %d\n", num_free_calls);
fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);
fprintf(stderr, " calloc = %4d\n", num_calloc_calls);
fprintf(stderr, " free = %4d\n", num_free_calls);
fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);
fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated);
fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);
while (all_blocks != NULL) {
MemBlock* b = all_blocks;
@ -68,6 +86,24 @@ static void PrintMemInfo(void) {
static void Increment(int* const v) {
if (!exit_registered) {
#if defined(MALLOC_FAIL_AT)
{
const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT");
if (malloc_fail_at_str != NULL) {
countdown_to_fail = atoi(malloc_fail_at_str);
}
}
#endif
#if defined(MALLOC_LIMIT)
{
const char* const malloc_limit_str = getenv("MALLOC_LIMIT");
if (malloc_limit_str != NULL) {
mem_limit = atoi(malloc_limit_str);
}
}
#endif
(void)countdown_to_fail;
(void)mem_limit;
atexit(PrintMemInfo);
exit_registered = 1;
}
@ -76,15 +112,21 @@ static void Increment(int* const v) {
static void AddMem(void* ptr, size_t size) {
if (ptr != NULL) {
MemBlock* b = (MemBlock*)malloc(sizeof(*b));
MemBlock* const b = (MemBlock*)malloc(sizeof(*b));
if (b == NULL) abort();
b->next_ = all_blocks;
all_blocks = b;
b->ptr_ = ptr;
b->size_ = size;
total_mem += size;
total_mem_allocated += size;
#if defined(PRINT_MEM_TRAFFIC)
#if defined(MALLOC_FAIL_AT)
fprintf(stderr, "fail-count: %5d [mem=%u]\n",
num_malloc_calls + num_calloc_calls, (uint32_t)total_mem);
#else
fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);
#endif
#endif
if (total_mem > high_water_mark) high_water_mark = total_mem;
}
@ -100,7 +142,7 @@ static void SubMem(void* ptr) {
abort();
}
{
MemBlock* block = *b;
MemBlock* const block = *b;
*b = block->next_;
total_mem -= block->size_;
#if defined(PRINT_MEM_TRAFFIC)
@ -124,6 +166,17 @@ static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
if (nmemb == 0) return 1;
if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
if (total_size != (size_t)total_size) return 0;
#if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT)
if (countdown_to_fail > 0 && --countdown_to_fail == 0) {
return 0; // fake fail!
}
#endif
#if defined(MALLOC_LIMIT)
if (mem_limit > 0 && total_mem + total_size >= mem_limit) {
return 0; // fake fail!
}
#endif
return 1;
}
@ -148,8 +201,10 @@ void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
}
void WebPSafeFree(void* const ptr) {
Increment(&num_free_calls);
SubMem(ptr);
if (ptr != NULL) {
Increment(&num_free_calls);
SubMem(ptr);
}
free(ptr);
}