GNUnet  0.17.6
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 
158 void *
159 GNUNET_xmemdup_ (const void *buf,
160  size_t size,
161  const char *filename,
162  int linenumber)
163 {
164  void *ret;
165 
166  /* As a security precaution, we generally do not allow very large
167  * allocations here */
169  GNUNET_assert_at (size < INT_MAX, filename, linenumber);
170  ret = malloc (size);
171  if (ret == NULL)
172  {
174  GNUNET_assert (0);
175  }
177  return ret;
178 }
179 
180 
190 void *
191 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
192 {
193  void *result;
194 
195  (void) filename;
196  (void) linenumber;
197 
198  result = malloc (size);
199  if (NULL == result)
200  return NULL;
201  memset (result, 0, size);
202 
203  return result;
204 }
205 
206 
218 void *
219 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
220 {
221  (void) filename;
222  (void) linenumber;
223 
224 #if defined(M_SIZE)
225 #if ENABLE_POISONING
226  {
227  uint64_t *base = ptr;
228  size_t s = M_SIZE (ptr);
229 
230  if (s > n)
231  {
232  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
233  char *cbase = ptr;
234 
235  GNUNET_memcpy (&cbase[n],
236  &baadfood,
237  GNUNET_MIN (8 - (n % 8),
238  s - n));
239  for (size_t i = 1 + (n + 7) / 8; i < s / 8; i++)
240  base[i] = baadfood;
241  GNUNET_memcpy (&base[s / 8],
242  &baadfood,
243  s % 8);
244  }
245  }
246 #endif
247 #endif
248  ptr = realloc (ptr, n);
249  if ((NULL == ptr) && (n > 0))
250  {
252  GNUNET_assert (0);
253  }
254  return ptr;
255 }
256 
257 
258 #if __BYTE_ORDER == __LITTLE_ENDIAN
259 #define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
260 #endif
261 #if __BYTE_ORDER == __BIG_ENDIAN
262 #define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
263 #endif
264 
265 #if HAVE_MALLOC_NP_H
266 #include <malloc_np.h>
267 #endif
268 #if HAVE_MALLOC_USABLE_SIZE
269 #define M_SIZE(p) malloc_usable_size (p)
270 #elif HAVE_MALLOC_SIZE
271 #define M_SIZE(p) malloc_size (p)
272 #endif
273 
282 void
283 GNUNET_xfree_ (void *ptr,
284  const char *filename,
285  int linenumber)
286 {
287  if (NULL == ptr)
288  return;
289 #if defined(M_SIZE)
290 #if ENABLE_POISONING
291  {
292  const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
293  uint64_t *base = ptr;
294  size_t s = M_SIZE (ptr);
295 
296  for (size_t i = 0; i < s / 8; i++)
297  base[i] = baadfood;
298  GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
299  }
300 #endif
301 #endif
302  free (ptr);
303 }
304 
305 
314 char *
315 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
316 {
317  char *res;
318  size_t slen;
319 
320  GNUNET_assert_at (str != NULL, filename, linenumber);
321  slen = strlen (str) + 1;
322  res = GNUNET_xmalloc_ (slen, filename, linenumber);
323  GNUNET_memcpy (res, str, slen);
324  return res;
325 }
326 
327 
328 #if ! HAVE_STRNLEN
329 static size_t
330 strnlen (const char *s, size_t n)
331 {
332  const char *e;
333 
334  e = memchr (s, '\0', n);
335  if (NULL == e)
336  return n;
337  return e - s;
338 }
339 
340 
341 #endif
342 
343 
353 char *
354 GNUNET_xstrndup_ (const char *str,
355  size_t len,
356  const char *filename,
357  int linenumber)
358 {
359  char *res;
360 
361  if (0 == len)
362  return GNUNET_strdup ("");
363  GNUNET_assert_at (NULL != str, filename, linenumber);
364  len = strnlen (str, len);
365  res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
366  GNUNET_memcpy (res, str, len);
367  /* res[len] = '\0'; 'malloc' zeros out anyway */
368  return res;
369 }
370 
371 
384 void
385 GNUNET_xgrow_ (void **old,
386  size_t elementSize,
387  unsigned int *oldCount,
388  unsigned int newCount,
389  const char *filename,
390  int linenumber)
391 {
392  void *tmp;
393  size_t size;
394 
395  GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
396  size = newCount * elementSize;
397  if (0 == size)
398  {
399  tmp = NULL;
400  }
401  else
402  {
403  tmp = GNUNET_xmalloc_ (size, filename, linenumber);
404  if (NULL != *old)
405  {
406  GNUNET_memcpy (tmp, *old, elementSize * GNUNET_MIN (*oldCount, newCount));
407  }
408  }
409 
410  if (NULL != *old)
411  {
412  GNUNET_xfree_ (*old, filename, linenumber);
413  }
414  *old = tmp;
415  *oldCount = newCount;
416 }
417 
418 
427 int
428 GNUNET_asprintf (char **buf, const char *format, ...)
429 {
430  int ret;
431  va_list args;
432 
433  va_start (args, format);
434  ret = vsnprintf (NULL, 0, format, args);
435  va_end (args);
436  GNUNET_assert (ret >= 0);
437  *buf = GNUNET_malloc (ret + 1);
438  va_start (args, format);
439  ret = vsprintf (*buf, format, args);
440  va_end (args);
441  return ret;
442 }
443 
444 
454 int
455 GNUNET_snprintf (char *buf, size_t size, const char *format, ...)
456 {
457  int ret;
458  va_list args;
459 
460  va_start (args, format);
461  ret = vsnprintf (buf, size, format, args);
462  va_end (args);
463  GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
464  return ret;
465 }
466 
467 
468 struct GNUNET_MessageHeader *
470 {
471  struct GNUNET_MessageHeader *ret;
472  uint16_t msize;
473 
474  msize = ntohs (msg->size);
475  GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
476  ret = GNUNET_malloc (msize);
477  GNUNET_memcpy (ret, msg, msize);
478  return ret;
479 }
480 
481 
491 GNUNET_is_zero_ (const void *a,
492  size_t n)
493 {
494  const char *b = a;
495 
496  for (size_t i = 0; i < n; i++)
497  if (b[i])
498  return GNUNET_NO;
499  return GNUNET_YES;
500 }
501 
502 
503 /* end of common_allocation.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static size_t strnlen(const char *s, size_t n)
#define INT_MAX
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.
#define LOG_STRERROR(kind, syscall)
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]
cryptographic primitives for GNUnet
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.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
void GNUNET_xfree_(void *ptr, const char *filename, int linenumber)
Free memory.
void * GNUNET_xmemdup_(const void *buf, size_t size, const char *filename, int linenumber)
Allocate and initialize memory.
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.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
char * GNUNET_xstrndup_(const char *str, size_t len, const char *filename, int linenumber)
Dup partially a string (same semantics as strndup).
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:96
#define GNUNET_MIN(a, b)
void * GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber)
Wrapper around malloc().
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98
#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.