1// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8// -----------------------------------------------------------------------------
9//
10// Misc. common utility functions
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include <stdlib.h>
15#include "./utils.h"
16
17// If PRINT_MEM_INFO is defined, extra info (like total memory used, number of
18// alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,
19// and not multi-thread safe!).
20// An interesting alternative is valgrind's 'massif' tool:
21//    http://valgrind.org/docs/manual/ms-manual.html
22// Here is an example command line:
23/*    valgrind --tool=massif --massif-out-file=massif.out \
24               --stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc
25      ms_print massif.out
26*/
27// In addition:
28// * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles
29//   are printed.
30// * if MALLOC_FAIL_AT is defined, the global environment variable
31//   $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc
32//   is called for the nth time. Example usage:
33//   export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png
34// * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT
35//   sets the maximum amount of memory (in bytes) made available to libwebp.
36//   This can be used to emulate environment with very limited memory.
37//   Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp
38
39// #define PRINT_MEM_INFO
40// #define PRINT_MEM_TRAFFIC
41// #define MALLOC_FAIL_AT
42// #define MALLOC_LIMIT
43
44//------------------------------------------------------------------------------
45// Checked memory allocation
46
47#if defined(PRINT_MEM_INFO)
48
49#include <stdio.h>
50#include <stdlib.h>  // for abort()
51
52static int num_malloc_calls = 0;
53static int num_calloc_calls = 0;
54static int num_free_calls = 0;
55static int countdown_to_fail = 0;     // 0 = off
56
57typedef struct MemBlock MemBlock;
58struct MemBlock {
59  void* ptr_;
60  size_t size_;
61  MemBlock* next_;
62};
63
64static MemBlock* all_blocks = NULL;
65static size_t total_mem = 0;
66static size_t total_mem_allocated = 0;
67static size_t high_water_mark = 0;
68static size_t mem_limit = 0;
69
70static int exit_registered = 0;
71
72static void PrintMemInfo(void) {
73  fprintf(stderr, "\nMEMORY INFO:\n");
74  fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);
75  fprintf(stderr, "              calloc = %4d\n", num_calloc_calls);
76  fprintf(stderr, "              free   = %4d\n", num_free_calls);
77  fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);
78  fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated);
79  fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);
80  while (all_blocks != NULL) {
81    MemBlock* b = all_blocks;
82    all_blocks = b->next_;
83    free(b);
84  }
85}
86
87static void Increment(int* const v) {
88  if (!exit_registered) {
89#if defined(MALLOC_FAIL_AT)
90    {
91      const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT");
92      if (malloc_fail_at_str != NULL) {
93        countdown_to_fail = atoi(malloc_fail_at_str);
94      }
95    }
96#endif
97#if defined(MALLOC_LIMIT)
98    {
99      const char* const malloc_limit_str = getenv("MALLOC_LIMIT");
100      if (malloc_limit_str != NULL) {
101        mem_limit = atoi(malloc_limit_str);
102      }
103    }
104#endif
105    (void)countdown_to_fail;
106    (void)mem_limit;
107    atexit(PrintMemInfo);
108    exit_registered = 1;
109  }
110  ++*v;
111}
112
113static void AddMem(void* ptr, size_t size) {
114  if (ptr != NULL) {
115    MemBlock* const b = (MemBlock*)malloc(sizeof(*b));
116    if (b == NULL) abort();
117    b->next_ = all_blocks;
118    all_blocks = b;
119    b->ptr_ = ptr;
120    b->size_ = size;
121    total_mem += size;
122    total_mem_allocated += size;
123#if defined(PRINT_MEM_TRAFFIC)
124#if defined(MALLOC_FAIL_AT)
125    fprintf(stderr, "fail-count: %5d [mem=%u]\n",
126            num_malloc_calls + num_calloc_calls, (uint32_t)total_mem);
127#else
128    fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);
129#endif
130#endif
131    if (total_mem > high_water_mark) high_water_mark = total_mem;
132  }
133}
134
135static void SubMem(void* ptr) {
136  if (ptr != NULL) {
137    MemBlock** b = &all_blocks;
138    // Inefficient search, but that's just for debugging.
139    while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_;
140    if (*b == NULL) {
141      fprintf(stderr, "Invalid pointer free! (%p)\n", ptr);
142      abort();
143    }
144    {
145      MemBlock* const block = *b;
146      *b = block->next_;
147      total_mem -= block->size_;
148#if defined(PRINT_MEM_TRAFFIC)
149      fprintf(stderr, "Mem: %u (-%u)\n",
150              (uint32_t)total_mem, (uint32_t)block->size_);
151#endif
152      free(block);
153    }
154  }
155}
156
157#else
158#define Increment(v) do {} while(0)
159#define AddMem(p, s) do {} while(0)
160#define SubMem(p)    do {} while(0)
161#endif
162
163// Returns 0 in case of overflow of nmemb * size.
164static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
165  const uint64_t total_size = nmemb * size;
166  if (nmemb == 0) return 1;
167  if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
168  if (total_size != (size_t)total_size) return 0;
169#if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT)
170  if (countdown_to_fail > 0 && --countdown_to_fail == 0) {
171    return 0;    // fake fail!
172  }
173#endif
174#if defined(MALLOC_LIMIT)
175  if (mem_limit > 0 && total_mem + total_size >= mem_limit) {
176    return 0;   // fake fail!
177  }
178#endif
179
180  return 1;
181}
182
183void* WebPSafeMalloc(uint64_t nmemb, size_t size) {
184  void* ptr;
185  Increment(&num_malloc_calls);
186  if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
187  assert(nmemb * size > 0);
188  ptr = malloc((size_t)(nmemb * size));
189  AddMem(ptr, (size_t)(nmemb * size));
190  return ptr;
191}
192
193void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
194  void* ptr;
195  Increment(&num_calloc_calls);
196  if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
197  assert(nmemb * size > 0);
198  ptr = calloc((size_t)nmemb, size);
199  AddMem(ptr, (size_t)(nmemb * size));
200  return ptr;
201}
202
203void WebPSafeFree(void* const ptr) {
204  if (ptr != NULL) {
205    Increment(&num_free_calls);
206    SubMem(ptr);
207  }
208  free(ptr);
209}
210
211//------------------------------------------------------------------------------
212