1 /**
2  * @file
3  * Dynamic pool memory manager
4  *
5  * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
6  * packet buffers, ...). All these pools are managed here.
7  *
8  * @defgroup mempool Memory pools
9  * @ingroup infrastructure
10  * Custom memory pools
11 
12  */
13 
14 /*
15  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without modification,
19  * are permitted provided that the following conditions are met:
20  *
21  * 1. Redistributions of source code must retain the above copyright notice,
22  *    this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright notice,
24  *    this list of conditions and the following disclaimer in the documentation
25  *    and/or other materials provided with the distribution.
26  * 3. The name of the author may not be used to endorse or promote products
27  *    derived from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
32  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
34  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38  * OF SUCH DAMAGE.
39  *
40  * This file is part of the lwIP TCP/IP stack.
41  *
42  * Author: Adam Dunkels <adam@sics.se>
43  *
44  */
45 
46 #include "lwip/opt.h"
47 
48 #include "lwip/memp.h"
49 #include "lwip/sys.h"
50 #include "lwip/stats.h"
51 
52 #include <string.h>
53 
54 /* Make sure we include everything we need for size calculation required by memp_std.h */
55 #include "lwip/pbuf.h"
56 #include "lwip/raw.h"
57 #include "lwip/udp.h"
58 #include "lwip/tcp.h"
59 #include "lwip/priv/tcp_priv.h"
60 #include "lwip/ip4_frag.h"
61 #include "lwip/netbuf.h"
62 #include "lwip/api.h"
63 #include "lwip/priv/tcpip_priv.h"
64 #include "lwip/priv/api_msg.h"
65 #include "lwip/sockets.h"
66 #include "lwip/netifapi.h"
67 #include "lwip/etharp.h"
68 #include "lwip/igmp.h"
69 #include "lwip/timeouts.h"
70 /* needed by default MEMP_NUM_SYS_TIMEOUT */
71 #include "netif/ppp/ppp_opts.h"
72 #include "lwip/netdb.h"
73 #include "lwip/dns.h"
74 #include "lwip/nd6.h"
75 #include "lwip/ip6_frag.h"
76 #include "lwip/mld6.h"
77 
78 
79 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
80 #include "lwip/priv/memp_std.h"
81 
82 const struct memp_desc* const memp_pools[MEMP_MAX] = {
83 #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
84 #include "lwip/priv/memp_std.h"
85 };
86 
87 #if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2
88 #undef MEMP_OVERFLOW_CHECK
89 /* MEMP_OVERFLOW_CHECK >= 2 does not work with MEMP_MEM_MALLOC, use 1 instead */
90 #define MEMP_OVERFLOW_CHECK 1
91 #endif
92 
93 #if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC
94 /**
95  * Check that memp-lists don't form a circle, using "Floyd's cycle-finding algorithm".
96  */
97 static int
memp_sanity(const struct memp_desc * desc)98 memp_sanity(const struct memp_desc *desc)
99 {
100   struct memp *t, *h;
101 
102   t = *desc->tab;
103   if (t != NULL) {
104     for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
105       h = ((h->next != NULL) ? h->next->next : NULL)) {
106       if (t == h) {
107         return 0;
108       }
109     }
110   }
111 
112   return 1;
113 }
114 #endif /* MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC */
115 
116 #if MEMP_OVERFLOW_CHECK
117 /**
118  * Check if a memp element was victim of an overflow
119  * (e.g. the restricted area after it has been altered)
120  *
121  * @param p the memp element to check
122  * @param desc the pool p comes from
123  */
124 static void
memp_overflow_check_element_overflow(struct memp * p,const struct memp_desc * desc)125 memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
126 {
127 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
128   u16_t k;
129   u8_t *m;
130   m = (u8_t*)p + MEMP_SIZE + desc->size;
131   for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
132     if (m[k] != 0xcd) {
133       char errstr[128] = "detected memp overflow in pool ";
134       strcat(errstr, desc->desc);
135       LWIP_ASSERT(errstr, 0);
136     }
137   }
138 #else /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
139   LWIP_UNUSED_ARG(p);
140   LWIP_UNUSED_ARG(desc);
141 #endif /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
142 }
143 
144 /**
145  * Check if a memp element was victim of an underflow
146  * (e.g. the restricted area before it has been altered)
147  *
148  * @param p the memp element to check
149  * @param desc the pool p comes from
150  */
151 static void
memp_overflow_check_element_underflow(struct memp * p,const struct memp_desc * desc)152 memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
153 {
154 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
155   u16_t k;
156   u8_t *m;
157   m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
158   for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
159     if (m[k] != 0xcd) {
160       char errstr[128] = "detected memp underflow in pool ";
161       strcat(errstr, desc->desc);
162       LWIP_ASSERT(errstr, 0);
163     }
164   }
165 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
166   LWIP_UNUSED_ARG(p);
167   LWIP_UNUSED_ARG(desc);
168 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
169 }
170 
171 /**
172  * Initialize the restricted area of on memp element.
173  */
174 static void
memp_overflow_init_element(struct memp * p,const struct memp_desc * desc)175 memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
176 {
177 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0
178   u8_t *m;
179 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
180   m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
181   memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
182 #endif
183 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
184   m = (u8_t*)p + MEMP_SIZE + desc->size;
185   memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
186 #endif
187 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
188   LWIP_UNUSED_ARG(p);
189   LWIP_UNUSED_ARG(desc);
190 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
191 }
192 
193 #if MEMP_OVERFLOW_CHECK >= 2
194 /**
195  * Do an overflow check for all elements in every pool.
196  *
197  * @see memp_overflow_check_element for a description of the check
198  */
199 static void
memp_overflow_check_all(void)200 memp_overflow_check_all(void)
201 {
202   u16_t i, j;
203   struct memp *p;
204   SYS_ARCH_DECL_PROTECT(old_level);
205   SYS_ARCH_PROTECT(old_level);
206 
207   for (i = 0; i < MEMP_MAX; ++i) {
208     p = (struct memp *)(size_t)(memp_pools[i]->base);
209     for (j = 0; j < memp_pools[i]->num; ++j) {
210       memp_overflow_check_element_overflow(p, memp_pools[i]);
211       memp_overflow_check_element_underflow(p, memp_pools[i]);
212       p = (struct memp*)(size_t)((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED);
213     }
214   }
215   SYS_ARCH_UNPROTECT(old_level);
216 }
217 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
218 #endif /* MEMP_OVERFLOW_CHECK */
219 
220 /**
221  * Initialize custom memory pool.
222  * Related functions: memp_malloc_pool, memp_free_pool
223  *
224  * @param desc pool to initialize
225  */
226 void
memp_init_pool(const struct memp_desc * desc)227 memp_init_pool(const struct memp_desc *desc)
228 {
229 #if MEMP_MEM_MALLOC
230   LWIP_UNUSED_ARG(desc);
231 #else
232   int i;
233   struct memp *memp;
234 
235   *desc->tab = NULL;
236   memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
237   /* create a linked list of memp elements */
238   for (i = 0; i < desc->num; ++i) {
239     memp->next = *desc->tab;
240     *desc->tab = memp;
241 #if MEMP_OVERFLOW_CHECK
242     memp_overflow_init_element(memp, desc);
243 #endif /* MEMP_OVERFLOW_CHECK */
244    /* cast through void* to get rid of alignment warnings */
245    memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
246 #if MEMP_OVERFLOW_CHECK
247       + MEMP_SANITY_REGION_AFTER_ALIGNED
248 #endif
249     );
250   }
251 #if MEMP_STATS
252   desc->stats->avail = desc->num;
253 #endif /* MEMP_STATS */
254 #endif /* !MEMP_MEM_MALLOC */
255 
256 #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
257   desc->stats->name  = desc->desc;
258 #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
259 }
260 
261 /**
262  * Initializes lwIP built-in pools.
263  * Related functions: memp_malloc, memp_free
264  *
265  * Carves out memp_memory into linked lists for each pool-type.
266  */
267 void
memp_init(void)268 memp_init(void)
269 {
270   u16_t i;
271 
272   /* for every pool: */
273   for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
274     memp_init_pool(memp_pools[i]);
275 
276 #if LWIP_STATS && MEMP_STATS
277     lwip_stats.memp[i] = memp_pools[i]->stats;
278 #endif
279   }
280 
281 #if MEMP_OVERFLOW_CHECK >= 2
282   /* check everything a first time to see if it worked */
283   memp_overflow_check_all();
284 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
285 }
286 
287 static void*
288 #if !MEMP_OVERFLOW_CHECK
do_memp_malloc_pool(const struct memp_desc * desc)289 do_memp_malloc_pool(const struct memp_desc *desc)
290 #else
291 do_memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
292 #endif
293 {
294   struct memp *memp;
295   SYS_ARCH_DECL_PROTECT(old_level);
296 
297 #if MEMP_MEM_MALLOC
298   memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
299   SYS_ARCH_PROTECT(old_level);
300 #else /* MEMP_MEM_MALLOC */
301   SYS_ARCH_PROTECT(old_level);
302 
303   memp = *desc->tab;
304 
305 #if MEMP_OVERFLOW_CHECK == 1
306   memp_overflow_check_element_overflow(memp, desc);
307   memp_overflow_check_element_underflow(memp, desc);
308 #endif /* MEMP_OVERFLOW_CHECK */
309 #endif /* MEMP_MEM_MALLOC */
310 
311   if (memp != NULL) {
312 #if !MEMP_MEM_MALLOC
313     *desc->tab = memp->next;
314 #if MEMP_OVERFLOW_CHECK
315     memp->next = NULL;
316 #endif /* MEMP_OVERFLOW_CHECK */
317 #endif /* !MEMP_MEM_MALLOC */
318 #if MEMP_OVERFLOW_CHECK
319     memp->file = file;
320     memp->line = line;
321 #if MEMP_MEM_MALLOC
322     memp_overflow_init_element(memp, desc);
323 #endif /* MEMP_MEM_MALLOC */
324 #endif /* MEMP_OVERFLOW_CHECK */
325     LWIP_ASSERT("memp_malloc: memp properly aligned",
326                 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
327 #if MEMP_STATS
328     desc->stats->used++;
329     if (desc->stats->used > desc->stats->max) {
330       desc->stats->max = desc->stats->used;
331     }
332 #endif
333     SYS_ARCH_UNPROTECT(old_level);
334     /* cast through u8_t* to get rid of alignment warnings */
335     return ((u8_t*)memp + MEMP_SIZE);
336   } else {
337     LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
338 #if MEMP_STATS
339     desc->stats->err++;
340 #endif
341   }
342 
343   SYS_ARCH_UNPROTECT(old_level);
344   return NULL;
345 }
346 
347 /**
348  * Get an element from a custom pool.
349  *
350  * @param desc the pool to get an element from
351  *
352  * @return a pointer to the allocated memory or a NULL pointer on error
353  */
354 void *
355 #if !MEMP_OVERFLOW_CHECK
memp_malloc_pool(const struct memp_desc * desc)356 memp_malloc_pool(const struct memp_desc *desc)
357 #else
358 memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
359 #endif
360 {
361   LWIP_ASSERT("invalid pool desc", desc != NULL);
362   if (desc == NULL) {
363     return NULL;
364   }
365 
366 #if !MEMP_OVERFLOW_CHECK
367   return do_memp_malloc_pool(desc);
368 #else
369   return do_memp_malloc_pool_fn(desc, file, line);
370 #endif
371 }
372 
373 /**
374  * Get an element from a specific pool.
375  *
376  * @param type the pool to get an element from
377  *
378  * @return a pointer to the allocated memory or a NULL pointer on error
379  */
380 void *
381 #if !MEMP_OVERFLOW_CHECK
memp_malloc(memp_t type)382 memp_malloc(memp_t type)
383 #else
384 memp_malloc_fn(memp_t type, const char* file, const int line)
385 #endif
386 {
387   void *memp;
388   LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
389 
390 #if MEMP_OVERFLOW_CHECK >= 2
391   memp_overflow_check_all();
392 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
393 
394 #if !MEMP_OVERFLOW_CHECK
395   memp = do_memp_malloc_pool(memp_pools[type]);
396 #else
397   memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
398 #endif
399 
400   return memp;
401 }
402 
403 static void
do_memp_free_pool(const struct memp_desc * desc,void * mem)404 do_memp_free_pool(const struct memp_desc* desc, void *mem)
405 {
406   struct memp *memp;
407   SYS_ARCH_DECL_PROTECT(old_level);
408 
409   LWIP_ASSERT("memp_free: mem properly aligned",
410                 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
411 
412   /* cast through void* to get rid of alignment warnings */
413   memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
414 
415   SYS_ARCH_PROTECT(old_level);
416 
417 #if MEMP_OVERFLOW_CHECK == 1
418   memp_overflow_check_element_overflow(memp, desc);
419   memp_overflow_check_element_underflow(memp, desc);
420 #endif /* MEMP_OVERFLOW_CHECK */
421 
422 #if MEMP_STATS
423   desc->stats->used--;
424 #endif
425 
426 #if MEMP_MEM_MALLOC
427   LWIP_UNUSED_ARG(desc);
428   SYS_ARCH_UNPROTECT(old_level);
429   mem_free(memp);
430 #else /* MEMP_MEM_MALLOC */
431   memp->next = *desc->tab;
432   *desc->tab = memp;
433 
434 #if MEMP_SANITY_CHECK
435   LWIP_ASSERT("memp sanity", memp_sanity(desc));
436 #endif /* MEMP_SANITY_CHECK */
437 
438   SYS_ARCH_UNPROTECT(old_level);
439 #endif /* !MEMP_MEM_MALLOC */
440 }
441 
442 /**
443  * Put a custom pool element back into its pool.
444  *
445  * @param desc the pool where to put mem
446  * @param mem the memp element to free
447  */
448 void
memp_free_pool(const struct memp_desc * desc,void * mem)449 memp_free_pool(const struct memp_desc* desc, void *mem)
450 {
451   LWIP_ASSERT("invalid pool desc", desc != NULL);
452   if ((desc == NULL) || (mem == NULL)) {
453     return;
454   }
455 
456   do_memp_free_pool(desc, mem);
457 }
458 
459 /**
460  * Put an element back into its pool.
461  *
462  * @param type the pool where to put mem
463  * @param mem the memp element to free
464  */
465 void
memp_free(memp_t type,void * mem)466 memp_free(memp_t type, void *mem)
467 {
468 #ifdef LWIP_HOOK_MEMP_AVAILABLE
469   struct memp *old_first;
470 #endif
471 
472   LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
473 
474 #if MEMP_OVERFLOW_CHECK >= 2
475   memp_overflow_check_all();
476 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
477 
478 #ifdef LWIP_HOOK_MEMP_AVAILABLE
479   old_first = *memp_pools[type]->tab;
480 #endif
481 
482   do_memp_free_pool(memp_pools[type], mem);
483 
484 #ifdef LWIP_HOOK_MEMP_AVAILABLE
485   if (old_first == NULL) {
486     LWIP_HOOK_MEMP_AVAILABLE(type);
487   }
488 #endif
489 }
490