GNUnet  0.11.x
common_allocation.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2001, 2002, 2003, 2005, 2006 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_crypto_lib.h"
28 #if HAVE_MALLOC_H
29 #include <malloc.h>
30 #endif
31 #if HAVE_MALLOC_MALLOC_H
32 #include <malloc/malloc.h>
33 #endif
34 
35 #define LOG(kind, ...) \
36  GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)
37 
38 #define LOG_STRERROR(kind, syscall) \
39  GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
40 
41 #ifndef INT_MAX
42 #define INT_MAX 0x7FFFFFFF
43 #endif
44 
45 #if 0
46 #define W32_MEM_LIMIT 200000000
47 #endif
48 
49 #ifdef W32_MEM_LIMIT
50 static LONG mem_used = 0;
51 #endif
52 
65 void *
66 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
67 {
68  void *ret;
69 
70  /* As a security precaution, we generally do not allow very large
71  * allocations using the default 'GNUNET_malloc()' macro */
72  GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
73  ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
74  if (NULL == ret)
75  {
77  GNUNET_assert (0);
78  }
79  return ret;
80 }
81 
82 
97 void **
99  size_t m,
100  size_t elementSize,
101  const char *filename,
102  int linenumber)
103 {
104  /* use char pointer internally to avoid void pointer arithmetic warnings */
105  char **ret = GNUNET_xmalloc_ (n * sizeof(void *) /* 1. dim header */
106  + n * m * elementSize, /* element data */
107  filename,
108  linenumber);
109 
110  for (size_t i = 0; i < n; i++)
111  ret[i] = (char *) ret /* base address */
112  + n * sizeof(void *) /* skip 1. dim header */
113  + i * m * elementSize; /* skip to 2. dim row header */
114  return (void **) ret;
115 }
116 
117 
133 void ***
135  size_t m,
136  size_t o,
137  size_t elementSize,
138  const char *filename,
139  int linenumber)
140 {
141  /* use char pointer internally to avoid void pointer arithmetic warnings */
142  char ***ret = GNUNET_xmalloc_ (n * sizeof(void **) /* 1. dim header */
143  + n * m * sizeof(void *) /* 2. dim header */
144  + n * m * o * elementSize, /* element data */
145  filename,
146  linenumber);
147 
148  for (size_t i = 0; i < n; i++)
149  {
150  /* need to cast to (char *) temporarily for byte level accuracy */
151  ret[i] = (char **) ((char *) ret /* base address */
152  + n * sizeof(void **) /* skip 1. dim header */
153  + i * m * sizeof(void *)); /* skip to 2. dim header */
154  for (size_t j = 0; j < m; j++)
155  ret[i][j] = (char *) ret /* base address */
156  + n * sizeof(void **) /* skip 1. dim header */
157  + n * m * sizeof(void *) /* skip 2. dim header */
158  + i * m * o * elementSize /* skip to 2. dim part */
159  + j * o * elementSize; /* skip to 3. dim row data */
160  }
161  return (void ***) ret;
162 }
163 
164 
176 void *
177 GNUNET_xmemdup_ (const void *buf,
178  size_t size,
179  const char *filename,
180  int linenumber)
181 {
182  void *ret;
183 
184  /* As a security precaution, we generally do not allow very large
185  * allocations here */
186  GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber);
187 #ifdef W32_MEM_LIMIT
188  size += sizeof(size_t);
189  if (mem_used + size > W32_MEM_LIMIT)
190  return NULL;
191 #endif
192  GNUNET_assert_at (size < INT_MAX, filename, linenumber);
193  ret = malloc (size);
194  if (ret == NULL)
195  {
197  GNUNET_assert (0);
198  }
199 #ifdef W32_MEM_LIMIT
200  *((size_t *) ret) = size;
201  ret = &((size_t *) ret)[1];
202  mem_used += size;
203 #endif
204  GNUNET_memcpy (ret, buf, size);
205  return ret;
206 }
207 
208 
218 void *
219 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
220 {
221  void *result;
222 
223  (void) filename;
224  (void) linenumber;
225 #ifdef W32_MEM_LIMIT
226  size += sizeof(size_t);
227  if (mem_used + size > W32_MEM_LIMIT)
228  return NULL;
229 #endif
230 
231  result = malloc (size);
232  if (NULL == result)
233  return NULL;
234  memset (result, 0, size);
235 
236 #ifdef W32_MEM_LIMIT
237  *((size_t *) result) = size;
238  result = &((size_t *) result)[1];
239  mem_used += size;
240 #endif
241 
242  return result;
243 }
244 
245 
257 void *
258 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
259 {
260  (void) filename;
261  (void) linenumber;
262 
263 #ifdef W32_MEM_LIMIT
264  n += sizeof(size_t);
265  ptr = &((size_t *) ptr)[-1];
266  mem_used = mem_used - *((size_t *) ptr) + n;
267 #endif
268 #if defined(M_SIZE)
269 #if ENABLE_POISONING
270  {
271  uint64_t *base = ptr;
272  size_t s = M_SIZE (ptr);
273 
274  if (s > n)
275  {
276  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
277  char *cbase = ptr;
278 
279  GNUNET_memcpy (&cbase[n],
280  &baadfood,
281  GNUNET_MIN (8 - (n % 8),
282  s - n));
283  for (size_t i = 1 + (n + 7) / 8; i < s / 8; i++)
284  base[i] = baadfood;
285  GNUNET_memcpy (&base[s / 8],
286  &baadfood,
287  s % 8);
288  }
289  }
290 #endif
291 #endif
292  ptr = realloc (ptr, n);
293  if ((NULL == ptr) && (n > 0))
294  {
296  GNUNET_assert (0);
297  }
298 #ifdef W32_MEM_LIMIT
299  ptr = &((size_t *) ptr)[1];
300 #endif
301  return ptr;
302 }
303 
304 
305 #if __BYTE_ORDER == __LITTLE_ENDIAN
306 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
307 #endif
308 #if __BYTE_ORDER == __BIG_ENDIAN
309 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
310 #endif
311 
312 #if HAVE_MALLOC_NP_H
313 #include <malloc_np.h>
314 #endif
315 #if HAVE_MALLOC_USABLE_SIZE
316 #define M_SIZE(p) malloc_usable_size (p)
317 #elif HAVE_MALLOC_SIZE
318 #define M_SIZE(p) malloc_size (p)
319 #endif
320 
329 void
330 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
331 {
332 #ifdef W32_MEM_LIMIT
333  ptr = &((size_t *) ptr)[-1];
334  mem_used -= *((size_t *) ptr);
335 #endif
336 #if defined(M_SIZE)
337 #if ENABLE_POISONING
338  {
339  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
340  uint64_t *base = ptr;
341  size_t s = M_SIZE (ptr);
342 
343  for (size_t i = 0; i < s / 8; i++)
344  base[i] = baadfood;
345  GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
346  }
347 #endif
348 #endif
349  free (ptr);
350 }
351 
352 
361 char *
362 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
363 {
364  char *res;
365  size_t slen;
366 
367  GNUNET_assert_at (str != NULL, filename, linenumber);
368  slen = strlen (str) + 1;
369  res = GNUNET_xmalloc_ (slen, filename, linenumber);
370  GNUNET_memcpy (res, str, slen);
371  return res;
372 }
373 
374 
375 #if ! HAVE_STRNLEN
376 static size_t
377 strnlen (const char *s, size_t n)
378 {
379  const char *e;
380 
381  e = memchr (s, '\0', n);
382  if (NULL == e)
383  return n;
384  return e - s;
385 }
386 
387 
388 #endif
389 
390 
400 char *
401 GNUNET_xstrndup_ (const char *str,
402  size_t len,
403  const char *filename,
404  int linenumber)
405 {
406  char *res;
407 
408  if (0 == len)
409  return GNUNET_strdup ("");
410  GNUNET_assert_at (NULL != str, filename, linenumber);
411  len = strnlen (str, len);
412  res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
413  GNUNET_memcpy (res, str, len);
414  /* res[len] = '\0'; 'malloc' zeros out anyway */
415  return res;
416 }
417 
418 
431 void
432 GNUNET_xgrow_ (void **old,
433  size_t elementSize,
434  unsigned int *oldCount,
435  unsigned int newCount,
436  const char *filename,
437  int linenumber)
438 {
439  void *tmp;
440  size_t size;
441 
442  GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
443  size = newCount * elementSize;
444  if (0 == size)
445  {
446  tmp = NULL;
447  }
448  else
449  {
450  tmp = GNUNET_xmalloc_ (size, filename, linenumber);
451  if (NULL != *old)
452  {
453  GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN (*oldCount, newCount));
454  }
455  }
456 
457  if (NULL != *old)
458  {
459  GNUNET_xfree_ (*old, filename, linenumber);
460  }
461  *old = tmp;
462  *oldCount = newCount;
463 }
464 
465 
474 int
475 GNUNET_asprintf (char **buf, const char *format, ...)
476 {
477  int ret;
478  va_list args;
479 
480  va_start (args, format);
481  ret = vsnprintf (NULL, 0, format, args);
482  va_end (args);
483  GNUNET_assert (ret >= 0);
484  *buf = GNUNET_malloc (ret + 1);
485  va_start (args, format);
486  ret = vsprintf (*buf, format, args);
487  va_end (args);
488  return ret;
489 }
490 
491 
501 int
502 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
503 {
504  int ret;
505  va_list args;
506 
507  va_start (args, format);
508  ret = vsnprintf (buf, size, format, args);
509  va_end (args);
510  GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
511  return ret;
512 }
513 
514 
521 struct GNUNET_MessageHeader *
523 {
524  struct GNUNET_MessageHeader *ret;
525  uint16_t msize;
526 
527  msize = ntohs (msg->size);
528  GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
529  ret = GNUNET_malloc (msize);
530  GNUNET_memcpy (ret, msg, msize);
531  return ret;
532 }
533 
534 
544 GNUNET_is_zero_ (const void *a,
545  size_t n)
546 {
547  const char *b = a;
548 
549  for (size_t i = 0; i < n; i++)
550  if (b[i])
551  return GNUNET_NO;
552  return GNUNET_YES;
553 }
554 
555 
556 /* end of common_allocation.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf(), just portable.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:83
static struct Experiment * e
char * GNUNET_xstrndup_(const char *str, size_t len, const char *filename, int linenumber)
Dup partially a string (same semantics as strndup).
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
#define INT_MAX
char * GNUNET_xstrdup_(const char *str, const char *filename, int linenumber)
Dup a string (same semantics as strdup).
cryptographic primitives for GNUnet
void * GNUNET_xrealloc_(void *ptr, size_t n, const char *filename, int linenumber)
Reallocate memory.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf(), just aborts if the buffer is of insufficient size.
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:93
static char buf[2048]
void GNUNET_xfree_(void *ptr, const char *filename, int linenumber)
Free memory.
static char * filename
static int result
Global testing status.
#define LOG_STRERROR(kind, syscall)
void * GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber)
Wrapper around malloc().
void ** GNUNET_xnew_array_2d_(size_t n, size_t m, size_t elementSize, const char *filename, int linenumber)
Allocate memory for a two dimensional array in one block and set up pointers.
static int res
void * GNUNET_xmemdup_(const void *buf, size_t size, const char *filename, int linenumber)
Allocate and initialize memory.
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
enum GNUNET_GenericReturnValue GNUNET_is_zero_(const void *a, size_t n)
Check that memory in a is all zeros.
void *** GNUNET_xnew_array_3d_(size_t n, size_t m, size_t o, size_t elementSize, const char *filename, int linenumber)
Allocate memory for a three dimensional array in one block and set up pointers.
Header for all communications.
static size_t strnlen(const char *s, size_t n)
#define GNUNET_malloc(size)
Wrapper around malloc.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
void GNUNET_xgrow_(void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber)
Grow an array.
struct GNUNET_MessageHeader * GNUNET_copy_message(const struct GNUNET_MessageHeader *msg)
Create a copy of the given message.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...