Coverage Report

Created: 2026-06-15 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/mempool-alloconly.c
Line
Count
Source
1
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
2
3
/* @UNSAFE: whole file */
4
#include "lib.h"
5
#include "safe-memset.h"
6
#include "mempool.h"
7
8
/*
9
 * As the name implies, alloconly pools support only allocating memory.
10
 * Memory freeing is not supported, except as a special case - the pool's
11
 * last allocation can be freed.  Additionally, p_realloc() also tries to
12
 * grow an existing allocation if and only if it is the last allocation,
13
 * otherwise it just allocates a new memory area and copies the data there.
14
 *
15
 * Alloconly pools are commonly used for an object that builds its state
16
 * from many memory allocations, but doesn't change (much of) its state.
17
 * It is simpler to free such an object by destroying the entire memory
18
 * pool.
19
 *
20
 * Implementation
21
 * ==============
22
 *
23
 * Each alloconly pool contains a pool structure (struct alloconly_pool) to
24
 * keep track of alloconly-specific pool information and one or more blocks
25
 * (struct pool_block) that keep track of ranges of memory used to back the
26
 * allocations.  The blocks are kept in a linked list implementing a stack.
27
 * The block size decreases the further down the stack one goes.
28
 *
29
 * +-----------+
30
 * | alloconly |
31
 * |    pool   |
32
 * +-----+-----+
33
 *       |
34
 *       | block  +------------+ next  +------------+ next
35
 *       \------->| pool block |------>| pool block |------>...
36
 *                +------------+       +------------+
37
 *                |   <data>   |       |   <data>   |
38
 *                      .                    .
39
 *                      .                    .
40
 *                      .              |   <data>   |
41
 *                      .              +------------+
42
 *                |   <data>   |
43
 *                +------------+
44
 *
45
 * Creation
46
 * --------
47
 *
48
 * When an alloconly pool is created, one block is allocated.  This block is
49
 * large enough to hold the necessary internal structures (struct
50
 * alloconly_pool and struct pool_block) and still have enough space to
51
 * satisfy allocations for at least the amount of space requested by the
52
 * consumer via the size argument to pool_alloconly_create().
53
 *
54
 * Allocation
55
 * ----------
56
 *
57
 * Each allocation (via p_malloc()) checks the top-most block to see whether
58
 * or not it has enough space to satisfy the allocation.  If there is not
59
 * enough space, it allocates a new block (via block_alloc()) to serve as
60
 * the new top-most block.  This newly-allocated block is guaranteed to have
61
 * enough space for the allocation.  Then, regardless of whether or not a
62
 * new block was allocated, the allocation code reserves enough space in the
63
 * top-most block for the allocation and returns a pointer to it to the
64
 * caller.
65
 *
66
 * The free space tracking within each block is very simple.  In addition to
67
 * keeping track of the size of the block, the block header contains a
68
 * "pointer" to the beginning of free space.  A new allocation simply moves
69
 * this pointer by the number of bytes allocated.
70
 *
71
 * Reallocation
72
 * ------------
73
 *
74
 * If the passed in allocation is the last allocation in a block and there
75
 * is enough space after it, the allocation is resized.  Otherwise, a new
76
 * buffer is allocated (see Allocation above) and the contents are copied
77
 * over.
78
 *
79
 * Freeing
80
 * -------
81
 *
82
 * Freeing of the last allocation moves the "pointer" to free space back by
83
 * the size of the last allocation.
84
 *
85
 * Freeing of any other allocation is a no-op.
86
 *
87
 * Clearing
88
 * --------
89
 *
90
 * Clearing the pool is supposed to return the pool to the same state it was
91
 * in when it was first created.  To that end, the alloconly pool frees all
92
 * the blocks allocated since the pool's creation.  The remaining block
93
 * (allocated during creation) is reset to consider all the space for
94
 * allocations as available.
95
 *
96
 * In other words, the per-block free space tracking variables are set to
97
 * indicate that the full block is available and that there have been no
98
 * allocations.
99
 *
100
 * Finally, if the pool was created via pool_alloconly_create_clean(), all
101
 * blocks are safe_memset()/memset() to zero before being free()d.
102
 *
103
 * Destruction
104
 * -----------
105
 *
106
 * Destroying a pool first clears it (see above).  The clearing leaves the
107
 * pool in a minimal state with only one block allocated.  This remaining
108
 * block may be safe_memset() to zero if the pool was created with
109
 * pool_alloconly_create_clean().
110
 *
111
 * Since the pool structure itself is allocated from the first block, this
112
 * final call to free() will release the memory allocated for struct
113
 * alloconly_pool and struct pool.
114
 */
115
116
#ifndef DEBUG
117
#  define POOL_ALLOCONLY_MAX_EXTRA MEM_ALIGN(1)
118
#else
119
#  define POOL_ALLOCONLY_MAX_EXTRA \
120
  (MEM_ALIGN(sizeof(size_t)) + MEM_ALIGN(1) + MEM_ALIGN(SENTRY_COUNT))
121
#endif
122
123
struct alloconly_pool {
124
  struct pool pool;
125
  int refcount;
126
127
  struct pool_block *block;
128
#ifdef DEBUG
129
  const char *name;
130
  size_t base_size;
131
  bool disable_warning;
132
#endif
133
  bool clean_frees;
134
};
135
136
struct pool_block {
137
  struct pool_block *prev;
138
139
  size_t size;
140
  size_t left;
141
  size_t last_alloc_size;
142
143
  /* unsigned char data[]; */
144
};
145
2.68k
#define SIZEOF_POOLBLOCK (MEM_ALIGN(sizeof(struct pool_block)))
146
147
#define POOL_BLOCK_DATA(block) \
148
1.97k
  ((unsigned char *) (block) + SIZEOF_POOLBLOCK)
149
150
0
#define DEFAULT_BASE_SIZE MEM_ALIGN(sizeof(struct alloconly_pool))
151
152
#ifdef DEBUG
153
#  define CLEAR_CHR 0xde
154
#  define SENTRY_COUNT 8
155
#else
156
#  define SENTRY_COUNT 0
157
0
#  define CLEAR_CHR 0
158
#endif
159
160
static const char *pool_alloconly_get_name(pool_t pool);
161
static void pool_alloconly_ref(pool_t pool);
162
static void pool_alloconly_unref(pool_t *pool);
163
static void *pool_alloconly_malloc(pool_t pool, size_t size);
164
static void pool_alloconly_free(pool_t pool, void *mem);
165
static void *pool_alloconly_realloc(pool_t pool, void *mem,
166
            size_t old_size, size_t new_size);
167
static void pool_alloconly_clear(pool_t pool);
168
static size_t pool_alloconly_get_max_easy_alloc_size(pool_t pool);
169
170
static void block_alloc(struct alloconly_pool *pool, size_t size);
171
172
static const struct pool_vfuncs static_alloconly_pool_vfuncs = {
173
  pool_alloconly_get_name,
174
175
  pool_alloconly_ref,
176
  pool_alloconly_unref,
177
178
  pool_alloconly_malloc,
179
  pool_alloconly_free,
180
181
  pool_alloconly_realloc,
182
183
  pool_alloconly_clear,
184
  pool_alloconly_get_max_easy_alloc_size
185
};
186
187
static const struct pool static_alloconly_pool = {
188
  .v = &static_alloconly_pool_vfuncs,
189
190
  .alloconly_pool = TRUE,
191
  .datastack_pool = FALSE
192
};
193
194
#ifdef DEBUG
195
static void check_sentries(struct pool_block *block)
196
{
197
  const unsigned char *data = POOL_BLOCK_DATA(block);
198
  size_t i, max_pos, alloc_size, used_size;
199
200
  used_size = block->size - block->left;
201
  for (i = 0; i < used_size; ) {
202
    alloc_size = *(size_t *)(data + i);
203
    if (alloc_size == 0 || used_size - i < alloc_size)
204
      i_panic("mempool-alloconly: saved alloc size broken");
205
    i += MEM_ALIGN(sizeof(alloc_size));
206
    max_pos = i + MEM_ALIGN(alloc_size + SENTRY_COUNT);
207
    i += alloc_size;
208
209
    for (; i < max_pos; i++) {
210
      if (data[i] != CLEAR_CHR)
211
        i_panic("mempool-alloconly: buffer overflow");
212
    }
213
  }
214
215
  if (i != used_size)
216
    i_panic("mempool-alloconly: used_size wrong");
217
218
  /* The unused data must be NULs */
219
  for (; i < block->size; i++) {
220
    if (data[i] != '\0')
221
      i_unreached();
222
  }
223
  if (block->prev != NULL)
224
    check_sentries(block->prev);
225
}
226
#endif
227
228
pool_t pool_alloconly_create(const char *name ATTR_UNUSED, size_t size)
229
178
{
230
178
  struct alloconly_pool apool, *new_apool;
231
178
  size_t min_alloc = SIZEOF_POOLBLOCK +
232
178
    MEM_ALIGN(sizeof(struct alloconly_pool) + SENTRY_COUNT);
233
234
178
  (void) COMPILE_ERROR_IF_TRUE(POOL_ALLOCONLY_MAX_EXTRA >
235
178
             (SSIZE_T_MAX - POOL_MAX_ALLOC_SIZE));
236
237
#ifdef DEBUG
238
  min_alloc += MEM_ALIGN(strlen(name) + 1 + SENTRY_COUNT) +
239
    sizeof(size_t)*2;
240
#endif
241
242
  /* create a fake alloconly_pool so we can call block_alloc() */
243
178
  i_zero(&apool);
244
178
  apool.pool = static_alloconly_pool;
245
178
  apool.refcount = 1;
246
247
178
  if (size < min_alloc)
248
0
    size = nearest_power(size + min_alloc);
249
178
  block_alloc(&apool, size);
250
251
  /* now allocate the actual alloconly_pool from the created block */
252
178
  new_apool = p_new(&apool.pool, struct alloconly_pool, 1);
253
178
  *new_apool = apool;
254
#ifdef DEBUG
255
  if (str_begins(name, MEMPOOL_GROWING, &name) ||
256
      getenv("DEBUG_SILENT") != NULL)
257
    new_apool->disable_warning = TRUE;
258
  new_apool->name = p_strdup(&new_apool->pool, name);
259
260
  /* set base_size so p_clear() doesn't trash alloconly_pool structure. */
261
  new_apool->base_size = new_apool->block->size - new_apool->block->left;
262
  new_apool->block->last_alloc_size = 0;
263
#endif
264
  /* the first pool allocations must be from the first block */
265
178
  i_assert(new_apool->block->prev == NULL);
266
267
178
  return &new_apool->pool;
268
178
}
269
270
pool_t pool_alloconly_create_clean(const char *name, size_t size)
271
0
{
272
0
  struct alloconly_pool *apool;
273
0
  pool_t pool;
274
275
0
  pool = pool_alloconly_create(name, size);
276
0
  apool = container_of(pool, struct alloconly_pool, pool);
277
0
  apool->clean_frees = TRUE;
278
0
  return pool;
279
0
}
280
281
static void pool_alloconly_free_block(struct alloconly_pool *apool ATTR_UNUSED,
282
              struct pool_block *block)
283
356
{
284
356
  int old_errno = errno;
285
286
#ifdef DEBUG
287
  safe_memset(block, CLEAR_CHR, SIZEOF_POOLBLOCK + block->size);
288
#else
289
356
  if (apool->clean_frees) {
290
0
    safe_memset(block, CLEAR_CHR,
291
0
          SIZEOF_POOLBLOCK + block->size);
292
0
  }
293
356
#endif
294
356
  free(block);
295
356
  errno = old_errno;
296
356
}
297
298
static void
299
pool_alloconly_free_blocks_until_last(struct alloconly_pool *apool)
300
178
{
301
178
  struct pool_block *block;
302
303
  /* destroy all blocks but the oldest, which contains the
304
     struct alloconly_pool allocation. */
305
356
  while (apool->block->prev != NULL) {
306
178
    block = apool->block;
307
178
    apool->block = block->prev;
308
309
178
    pool_alloconly_free_block(apool, block);
310
178
  }
311
178
}
312
313
static void pool_alloconly_destroy(struct alloconly_pool *apool)
314
178
{
315
  /* destroy all but the last block */
316
178
  pool_alloconly_free_blocks_until_last(apool);
317
318
  /* destroy the last block */
319
178
  pool_alloconly_free_block(apool, apool->block);
320
178
}
321
322
static const char *pool_alloconly_get_name(pool_t pool ATTR_UNUSED)
323
0
{
324
#ifdef DEBUG
325
  struct alloconly_pool *apool =
326
    container_of(pool, struct alloconly_pool, pool);
327
328
  return apool->name;
329
#else
330
0
  return "alloconly";
331
0
#endif
332
0
}
333
334
static void pool_alloconly_ref(pool_t pool)
335
0
{
336
0
  struct alloconly_pool *apool =
337
0
    container_of(pool, struct alloconly_pool, pool);
338
339
0
  apool->refcount++;
340
0
}
341
342
static void pool_alloconly_unref(pool_t *pool)
343
178
{
344
178
  struct alloconly_pool *apool =
345
178
    container_of(*pool, struct alloconly_pool, pool);
346
347
  /* erase the pointer before freeing anything, as the pointer may
348
     exist inside the pool's memory area */
349
178
  *pool = NULL;
350
351
178
  if (--apool->refcount > 0)
352
0
    return;
353
354
178
  pool_external_refs_unref(&apool->pool);
355
178
  pool_alloconly_destroy(apool);
356
178
}
357
358
static void block_alloc(struct alloconly_pool *apool, size_t size)
359
356
{
360
356
  struct pool_block *block;
361
362
356
  i_assert(size > SIZEOF_POOLBLOCK);
363
356
  i_assert(size <= SSIZE_T_MAX);
364
365
356
  if (apool->block != NULL) {
366
    /* each block is at least twice the size of the previous one */
367
178
    if (size <= apool->block->size)
368
178
      size += apool->block->size;
369
370
    /* avoid crashing in nearest_power() if size is too large */
371
178
    size = I_MIN(size, SSIZE_T_MAX);
372
178
    size = nearest_power(size);
373
    /* nearest_power() could have grown size to SSIZE_T_MAX+1 */
374
178
    size = I_MIN(size, SSIZE_T_MAX);
375
#ifdef DEBUG
376
    if (!apool->disable_warning) {
377
      /* i_debug() overwrites unallocated data in data
378
         stack, so make sure everything is allocated before
379
         calling it. */
380
      t_buffer_alloc_last_full();
381
      i_debug("Growing pool '%s' with: %zu",
382
          apool->name, size);
383
    }
384
#endif
385
178
  }
386
387
356
  int old_errno = errno;
388
356
  block = calloc(size, 1);
389
356
  if (unlikely(block == NULL)) {
390
0
    i_fatal_status(FATAL_OUTOFMEM, "block_alloc(%zu"
391
0
             "): Out of memory", size);
392
0
  }
393
356
  errno = old_errno;
394
356
  block->prev = apool->block;
395
356
  apool->block = block;
396
397
356
  block->size = size - SIZEOF_POOLBLOCK;
398
356
  block->left = block->size;
399
356
}
400
401
static void *pool_alloconly_malloc(pool_t pool, size_t size)
402
1.97k
{
403
1.97k
  struct alloconly_pool *apool =
404
1.97k
    container_of(pool, struct alloconly_pool, pool);
405
1.97k
  void *mem;
406
1.97k
  size_t alloc_size;
407
408
1.97k
#ifndef DEBUG
409
1.97k
  alloc_size = MEM_ALIGN(size);
410
#else
411
  alloc_size = MEM_ALIGN(sizeof(size)) + MEM_ALIGN(size + SENTRY_COUNT);
412
#endif
413
414
1.97k
  if (apool->block->left < alloc_size) {
415
    /* we need a new block */
416
178
    block_alloc(apool, alloc_size + SIZEOF_POOLBLOCK);
417
178
  }
418
419
1.97k
  mem = POOL_BLOCK_DATA(apool->block) +
420
1.97k
    (apool->block->size - apool->block->left);
421
422
1.97k
  apool->block->left -= alloc_size;
423
1.97k
  apool->block->last_alloc_size = alloc_size;
424
#ifdef DEBUG
425
  memcpy(mem, &size, sizeof(size));
426
  mem = PTR_OFFSET(mem, MEM_ALIGN(sizeof(size)));
427
  /* write CLEAR_CHRs to sentry */
428
  memset(PTR_OFFSET(mem, size), CLEAR_CHR,
429
         MEM_ALIGN(size + SENTRY_COUNT) - size);
430
#endif
431
1.97k
  return mem;
432
1.97k
}
433
434
static void pool_alloconly_free(pool_t pool, void *mem)
435
0
{
436
0
  struct alloconly_pool *apool =
437
0
    container_of(pool, struct alloconly_pool, pool);
438
439
  /* we can free only the last allocation */
440
0
  if (POOL_BLOCK_DATA(apool->block) +
441
0
      (apool->block->size - apool->block->left -
442
0
       apool->block->last_alloc_size) == mem) {
443
0
    memset(mem, 0, apool->block->last_alloc_size);
444
0
    apool->block->left += apool->block->last_alloc_size;
445
0
    apool->block->last_alloc_size = 0;
446
0
  }
447
0
}
448
449
static bool pool_alloconly_try_grow(struct alloconly_pool *apool, void *mem, size_t size)
450
0
{
451
  /* see if we want to grow the memory we allocated last */
452
0
  if (POOL_BLOCK_DATA(apool->block) +
453
0
      (apool->block->size - apool->block->left -
454
0
       apool->block->last_alloc_size) == mem) {
455
    /* yeah, see if we can grow */
456
0
    if (apool->block->left >= size-apool->block->last_alloc_size) {
457
      /* just shrink the available size */
458
0
      apool->block->left -=
459
0
        size - apool->block->last_alloc_size;
460
0
      apool->block->last_alloc_size = size;
461
0
      return TRUE;
462
0
    }
463
0
  }
464
465
0
  return FALSE;
466
0
}
467
468
static void *pool_alloconly_realloc(pool_t pool, void *mem,
469
            size_t old_size, size_t new_size)
470
0
{
471
0
  struct alloconly_pool *apool =
472
0
    container_of(pool, struct alloconly_pool, pool);
473
0
  unsigned char *new_mem;
474
475
0
  i_assert(old_size < SIZE_MAX);
476
477
0
  if (new_size <= old_size)
478
0
    return mem;
479
480
0
  new_size = MEM_ALIGN(new_size);
481
482
  /* see if we can directly grow it */
483
0
  if (!pool_alloconly_try_grow(apool, mem, new_size)) {
484
    /* slow way - allocate + copy */
485
0
    new_mem = pool_alloconly_malloc(pool, new_size);
486
0
    if (old_size > 0)
487
0
      memcpy(new_mem, mem, old_size);
488
0
    mem = new_mem;
489
0
  }
490
491
0
  return mem;
492
0
}
493
494
static void pool_alloconly_clear(pool_t pool)
495
0
{
496
0
  struct alloconly_pool *apool =
497
0
    container_of(pool, struct alloconly_pool, pool);
498
0
  size_t base_size, avail_size;
499
500
#ifdef DEBUG
501
  check_sentries(apool->block);
502
#endif
503
504
0
  pool_alloconly_free_blocks_until_last(apool);
505
506
  /* clear the first block */
507
#ifdef DEBUG
508
  base_size = apool->base_size;
509
#else
510
0
  base_size = DEFAULT_BASE_SIZE;
511
0
#endif
512
0
  avail_size = apool->block->size - base_size;
513
0
  memset(PTR_OFFSET(POOL_BLOCK_DATA(apool->block), base_size), 0,
514
0
         avail_size - apool->block->left);
515
0
  apool->block->left = avail_size;
516
0
  apool->block->last_alloc_size = 0;
517
0
}
518
519
static size_t pool_alloconly_get_max_easy_alloc_size(pool_t pool)
520
0
{
521
0
  struct alloconly_pool *apool =
522
0
    container_of(pool, struct alloconly_pool, pool);
523
524
0
  return apool->block->left;
525
0
}
526
527
size_t pool_alloconly_get_total_used_size(pool_t pool)
528
0
{
529
0
  struct alloconly_pool *apool =
530
0
    container_of(pool, struct alloconly_pool, pool);
531
0
  struct pool_block *block;
532
0
  size_t size = 0;
533
534
0
  i_assert(pool->v == &static_alloconly_pool_vfuncs);
535
536
0
  for (block = apool->block; block != NULL; block = block->prev)
537
0
    size += block->size - block->left;
538
0
  return size;
539
0
}
540
541
size_t pool_alloconly_get_total_alloc_size(pool_t pool)
542
0
{
543
0
  struct alloconly_pool *apool =
544
0
    container_of(pool, struct alloconly_pool, pool);
545
0
  struct pool_block *block;
546
0
  size_t size = 0;
547
548
0
  i_assert(pool->v == &static_alloconly_pool_vfuncs);
549
550
0
  for (block = apool->block; block != NULL; block = block->prev)
551
0
    size += block->size + SIZEOF_POOLBLOCK;
552
0
  return size;
553
0
}