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 
58 void *
59 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
60 {
61  void *ret;
62 
63  /* As a security precaution, we generally do not allow very large
64  * allocations using the default 'GNUNET_malloc()' macro */
66  ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber);
67  if (NULL == ret)
68  {
70  GNUNET_assert (0);
71  }
72  return ret;
73 }
74 
75 
90 void **
92  size_t m,
93  size_t elementSize,
94  const char *filename,
95  int linenumber)
96 {
97  /* use char pointer internally to avoid void pointer arithmetic warnings */
98  char **ret = GNUNET_xmalloc_ (n * sizeof(void *) /* 1. dim header */
99  + n * m * elementSize, /* element data */
100  filename,
101  linenumber);
102 
103  for (size_t i = 0; i < n; i++)
104  ret[i] = (char *) ret /* base address */
105  + n * sizeof(void *) /* skip 1. dim header */
106  + i * m * elementSize; /* skip to 2. dim row header */
107  return (void **) ret;
108 }
109 
110 
126 void ***
128  size_t m,
129  size_t o,
130  size_t elementSize,
131  const char *filename,
132  int linenumber)
133 {
134  /* use char pointer internally to avoid void pointer arithmetic warnings */
135  char ***ret = GNUNET_xmalloc_ (n * sizeof(void **) /* 1. dim header */
136  + n * m * sizeof(void *) /* 2. dim header */
137  + n * m * o * elementSize, /* element data */
138  filename,
139  linenumber);
140 
141  for (size_t i = 0; i < n; i++)
142  {
143  /* need to cast to (char *) temporarily for byte level accuracy */
144  ret[i] = (char **) ((char *) ret /* base address */
145  + n * sizeof(void **) /* skip 1. dim header */
146  + i * m * sizeof(void *)); /* skip to 2. dim header */
147  for (size_t j = 0; j < m; j++)
148  ret[i][j] = (char *) ret /* base address */
149  + n * sizeof(void **) /* skip 1. dim header */
150  + n * m * sizeof(void *) /* skip 2. dim header */
151  + i * m * o * elementSize /* skip to 2. dim part */
152  + j * o * elementSize; /* skip to 3. dim row data */
153  }
154  return (void ***) ret;
155 }
156 
157 
169 void *
170 GNUNET_xmemdup_ (const void *buf,
171  size_t size,
172  const char *filename,
173  int linenumber)
174 {
175  void *ret;
176 
177  /* As a security precaution, we generally do not allow very large
178  * allocations here */
180  GNUNET_assert_at (size < INT_MAX, filename, linenumber);
181  ret = malloc (size);
182  if (ret == NULL)
183  {
185  GNUNET_assert (0);
186  }
188  return ret;
189 }
190 
191 
201 void *
202 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
203 {
204  void *result;
205 
206  (void) filename;
207  (void) linenumber;
208 
209  result = malloc (size);
210  if (NULL == result)
211  return NULL;
212  memset (result, 0, size);
213 
214  return result;
215 }
216 
217 
229 void *
230 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
231 {
232  (void) filename;
233  (void) linenumber;
234 
235 #if defined(M_SIZE)
236 #if ENABLE_POISONING
237  {
238  uint64_t *base = ptr;
239  size_t s = M_SIZE (ptr);
240 
241  if (s > n)
242  {
243  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
244  char *cbase = ptr;
245 
246  GNUNET_memcpy (&cbase[n],
247  &baadfood,
248  GNUNET_MIN (8 - (n % 8),
249  s - n));
250  for (size_t i = 1 + (n + 7) / 8; i < s / 8; i++)
251  base[i] = baadfood;
252  GNUNET_memcpy (&base[s / 8],
253  &baadfood,
254  s % 8);
255  }
256  }
257 #endif
258 #endif
259  ptr = realloc (ptr, n);
260  if ((NULL == ptr) && (n > 0))
261  {
263  GNUNET_assert (0);
264  }
265  return ptr;
266 }
267 
268 
269 #if __BYTE_ORDER == __LITTLE_ENDIAN
270 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
271 #endif
272 #if __BYTE_ORDER == __BIG_ENDIAN
273 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
274 #endif
275 
276 #if HAVE_MALLOC_NP_H
277 #include <malloc_np.h>
278 #endif
279 #if HAVE_MALLOC_USABLE_SIZE
280 #define M_SIZE(p) malloc_usable_size (p)
281 #elif HAVE_MALLOC_SIZE
282 #define M_SIZE(p) malloc_size (p)
283 #endif
284 
293 void
294 GNUNET_xfree_ (void *ptr,
295  const char *filename,
296  int linenumber)
297 {
298  if (NULL == ptr)
299  return;
300 #if defined(M_SIZE)
301 #if ENABLE_POISONING
302  {
303  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
304  uint64_t *base = ptr;
305  size_t s = M_SIZE (ptr);
306 
307  for (size_t i = 0; i < s / 8; i++)
308  base[i] = baadfood;
309  GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
310  }
311 #endif
312 #endif
313  free (ptr);
314 }
315 
316 
325 char *
326 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
327 {
328  char *res;
329  size_t slen;
330 
331  GNUNET_assert_at (str != NULL, filename, linenumber);
332  slen = strlen (str) + 1;
333  res = GNUNET_xmalloc_ (slen, filename, linenumber);
334  GNUNET_memcpy (res, str, slen);
335  return res;
336 }
337 
338 
339 #if ! HAVE_STRNLEN
340 static size_t
341 strnlen (const char *s, size_t n)
342 {
343  const char *e;
344 
345  e = memchr (s, '\0', n);
346  if (NULL == e)
347  return n;
348  return e - s;
349 }
350 
351 
352 #endif
353 
354 
364 char *
365 GNUNET_xstrndup_ (const char *str,
366  size_t len,
367  const char *filename,
368  int linenumber)
369 {
370  char *res;
371 
372  if (0 == len)
373  return GNUNET_strdup ("");
374  GNUNET_assert_at (NULL != str, filename, linenumber);
375  len = strnlen (str, len);
376  res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
377  GNUNET_memcpy (res, str, len);
378  /* res[len] = '\0'; 'malloc' zeros out anyway */
379  return res;
380 }
381 
382 
395 void
396 GNUNET_xgrow_ (void **old,
397  size_t elementSize,
398  unsigned int *oldCount,
399  unsigned int newCount,
400  const char *filename,
401  int linenumber)
402 {
403  void *tmp;
404  size_t size;
405 
406  GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
407  size = newCount * elementSize;
408  if (0 == size)
409  {
410  tmp = NULL;
411  }
412  else
413  {
414  tmp = GNUNET_xmalloc_ (size, filename, linenumber);
415  if (NULL != *old)
416  {
417  GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN (*oldCount, newCount));
418  }
419  }
420 
421  if (NULL != *old)
422  {
423  GNUNET_xfree_ (*old, filename, linenumber);
424  }
425  *old = tmp;
426  *oldCount = newCount;
427 }
428 
429 
438 int
439 GNUNET_asprintf (char **buf, const char *format, ...)
440 {
441  int ret;
442  va_list args;
443 
444  va_start (args, format);
445  ret = vsnprintf (NULL, 0, format, args);
446  va_end (args);
447  GNUNET_assert (ret >= 0);
448  *buf = GNUNET_malloc (ret + 1);
449  va_start (args, format);
450  ret = vsprintf (*buf, format, args);
451  va_end (args);
452  return ret;
453 }
454 
455 
465 int
466 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
467 {
468  int ret;
469  va_list args;
470 
471  va_start (args, format);
472  ret = vsnprintf (buf, size, format, args);
473  va_end (args);
474  GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
475  return ret;
476 }
477 
478 
485 struct GNUNET_MessageHeader *
487 {
488  struct GNUNET_MessageHeader *ret;
489  uint16_t msize;
490 
491  msize = ntohs (msg->size);
492  GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
493  ret = GNUNET_malloc (msize);
494  GNUNET_memcpy (ret, msg, msize);
495  return ret;
496 }
497 
498 
508 GNUNET_is_zero_ (const void *a,
509  size_t n)
510 {
511  const char *b = a;
512 
513  for (size_t i = 0; i < n; i++)
514  if (b[i])
515  return GNUNET_NO;
516  return GNUNET_YES;
517 }
518 
519 
520 /* end of common_allocation.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
enum GNUNET_GenericReturnValue GNUNET_is_zero_(const void *a, size_t n)
Check that memory in a is all zeros.
char * GNUNET_xstrdup_(const char *str, const char *filename, int linenumber)
Dup a string (same semantics as strdup).
void * GNUNET_xrealloc_(void *ptr, size_t n, const char *filename, int linenumber)
Reallocate memory.
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.
void GNUNET_xgrow_(void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber)
Grow an array.
static size_t strnlen(const char *s, size_t n)
void GNUNET_xfree_(void *ptr, const char *filename, int linenumber)
Free memory.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
#define INT_MAX
void * GNUNET_xmemdup_(const void *buf, size_t size, const char *filename, int linenumber)
Allocate and initialize memory.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf(), just aborts if the buffer is of insufficient size.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf(), just portable.
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.
char * GNUNET_xstrndup_(const char *str, size_t len, const char *filename, int linenumber)
Dup partially a string (same semantics as strndup).
#define LOG_STRERROR(kind, syscall)
void * GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber)
Wrapper around malloc().
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
static struct Experiment * e
static int res
static char * filename
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static int result
Global testing status.
static char buf[2048]
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:92
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
#define GNUNET_MIN(a, b)
cryptographic primitives for GNUnet
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_malloc(size)
Wrapper around malloc.
struct GNUNET_MessageHeader * GNUNET_copy_message(const struct GNUNET_MessageHeader *msg)
Create a copy of the given message.
static unsigned int size
Size of the "table".
Definition: peer.c:67
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.