mirror of
https://github.com/webmproject/libwebp.git
synced 2025-02-13 15:32:53 +01:00
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:
parent
ca3d746e39
commit
f948d08c81
@ -14,9 +14,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "./utils.h"
|
#include "./utils.h"
|
||||||
|
|
||||||
// If defined, will print extra info like total memory used, number of
|
// If PRINT_MEM_INFO is defined, extra info (like total memory used, number of
|
||||||
// alloc/free etc. For debugging/tuning purpose only (it's slow, and not
|
// alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,
|
||||||
// multi-thread safe!).
|
// and not multi-thread safe!).
|
||||||
// An interesting alternative is valgrind's 'massif' tool:
|
// An interesting alternative is valgrind's 'massif' tool:
|
||||||
// http://valgrind.org/docs/manual/ms-manual.html
|
// http://valgrind.org/docs/manual/ms-manual.html
|
||||||
// Here is an example command line:
|
// Here is an example command line:
|
||||||
@ -24,9 +24,22 @@
|
|||||||
--stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc
|
--stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc
|
||||||
ms_print massif.out
|
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_INFO
|
||||||
#define PRINT_MEM_TRAFFIC // print fine traffic details
|
// #define PRINT_MEM_TRAFFIC
|
||||||
|
// #define MALLOC_FAIL_AT
|
||||||
|
// #define MALLOC_LIMIT
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Checked memory allocation
|
// Checked memory allocation
|
||||||
@ -39,6 +52,7 @@
|
|||||||
static int num_malloc_calls = 0;
|
static int num_malloc_calls = 0;
|
||||||
static int num_calloc_calls = 0;
|
static int num_calloc_calls = 0;
|
||||||
static int num_free_calls = 0;
|
static int num_free_calls = 0;
|
||||||
|
static int countdown_to_fail = 0; // 0 = off
|
||||||
|
|
||||||
typedef struct MemBlock MemBlock;
|
typedef struct MemBlock MemBlock;
|
||||||
struct MemBlock {
|
struct MemBlock {
|
||||||
@ -49,15 +63,19 @@ struct MemBlock {
|
|||||||
|
|
||||||
static MemBlock* all_blocks = NULL;
|
static MemBlock* all_blocks = NULL;
|
||||||
static size_t total_mem = 0;
|
static size_t total_mem = 0;
|
||||||
|
static size_t total_mem_allocated = 0;
|
||||||
static size_t high_water_mark = 0;
|
static size_t high_water_mark = 0;
|
||||||
|
static size_t mem_limit = 0;
|
||||||
|
|
||||||
static int exit_registered = 0;
|
static int exit_registered = 0;
|
||||||
|
|
||||||
static void PrintMemInfo(void) {
|
static void PrintMemInfo(void) {
|
||||||
fprintf(stderr, "\nMEMORY INFO:\n");
|
fprintf(stderr, "\nMEMORY INFO:\n");
|
||||||
fprintf(stderr, "num calls to: malloc = %d\n", num_malloc_calls);
|
fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);
|
||||||
fprintf(stderr, " calloc = %d\n", num_calloc_calls);
|
fprintf(stderr, " calloc = %4d\n", num_calloc_calls);
|
||||||
fprintf(stderr, " free = %d\n", num_free_calls);
|
fprintf(stderr, " free = %4d\n", num_free_calls);
|
||||||
fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);
|
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);
|
fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);
|
||||||
while (all_blocks != NULL) {
|
while (all_blocks != NULL) {
|
||||||
MemBlock* b = all_blocks;
|
MemBlock* b = all_blocks;
|
||||||
@ -68,6 +86,24 @@ static void PrintMemInfo(void) {
|
|||||||
|
|
||||||
static void Increment(int* const v) {
|
static void Increment(int* const v) {
|
||||||
if (!exit_registered) {
|
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);
|
atexit(PrintMemInfo);
|
||||||
exit_registered = 1;
|
exit_registered = 1;
|
||||||
}
|
}
|
||||||
@ -76,15 +112,21 @@ static void Increment(int* const v) {
|
|||||||
|
|
||||||
static void AddMem(void* ptr, size_t size) {
|
static void AddMem(void* ptr, size_t size) {
|
||||||
if (ptr != NULL) {
|
if (ptr != NULL) {
|
||||||
MemBlock* b = (MemBlock*)malloc(sizeof(*b));
|
MemBlock* const b = (MemBlock*)malloc(sizeof(*b));
|
||||||
if (b == NULL) abort();
|
if (b == NULL) abort();
|
||||||
b->next_ = all_blocks;
|
b->next_ = all_blocks;
|
||||||
all_blocks = b;
|
all_blocks = b;
|
||||||
b->ptr_ = ptr;
|
b->ptr_ = ptr;
|
||||||
b->size_ = size;
|
b->size_ = size;
|
||||||
total_mem += size;
|
total_mem += size;
|
||||||
|
total_mem_allocated += size;
|
||||||
#if defined(PRINT_MEM_TRAFFIC)
|
#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);
|
fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
if (total_mem > high_water_mark) high_water_mark = total_mem;
|
if (total_mem > high_water_mark) high_water_mark = total_mem;
|
||||||
}
|
}
|
||||||
@ -100,7 +142,7 @@ static void SubMem(void* ptr) {
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
MemBlock* block = *b;
|
MemBlock* const block = *b;
|
||||||
*b = block->next_;
|
*b = block->next_;
|
||||||
total_mem -= block->size_;
|
total_mem -= block->size_;
|
||||||
#if defined(PRINT_MEM_TRAFFIC)
|
#if defined(PRINT_MEM_TRAFFIC)
|
||||||
@ -124,6 +166,17 @@ static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
|
|||||||
if (nmemb == 0) return 1;
|
if (nmemb == 0) return 1;
|
||||||
if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
|
if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
|
||||||
if (total_size != (size_t)total_size) 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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,8 +201,10 @@ void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WebPSafeFree(void* const ptr) {
|
void WebPSafeFree(void* const ptr) {
|
||||||
Increment(&num_free_calls);
|
if (ptr != NULL) {
|
||||||
SubMem(ptr);
|
Increment(&num_free_calls);
|
||||||
|
SubMem(ptr);
|
||||||
|
}
|
||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user