Coverage Report

Created: 2026-05-16 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/mempool-system.c
Line
Count
Source
1
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
2
3
/* @UNSAFE: whole file */
4
5
#include "lib.h"
6
#include "safe-memset.h"
7
#include "mempool.h"
8
9
/*
10
 * The system pool is a thin wrapper around calloc() and free().  It exists
11
 * to allow direct heap usage via the pool API.
12
 *
13
 * Implementation
14
 * ==============
15
 *
16
 * Creation
17
 * --------
18
 *
19
 * The system pool is created statically and therefore is available at any
20
 * time.
21
 *
22
 * Allocation, Reallocation & Freeing
23
 * ----------------------------------
24
 *
25
 * The p_malloc(), p_realloc(), and p_free() calls get directed to calloc(),
26
 * realloc(), and free().  There is no additional per-allocation information
27
 * to track.
28
 *
29
 * Clearing
30
 * --------
31
 *
32
 * Not supported.  Attempting to clear the system pool will result in a
33
 * panic.
34
 *
35
 * Destruction
36
 * -----------
37
 *
38
 * It is not possible to destroy the system pool.  Any attempt to unref the
39
 * pool is a no-op.
40
 */
41
42
#ifndef HAVE_MALLOC_USABLE_SIZE
43
/* no extra includes needed */
44
#elif defined (HAVE_MALLOC_NP_H)
45
#  include <malloc_np.h> /* FreeBSD */
46
#elif defined (HAVE_MALLOC_H)
47
#  include <malloc.h> /* Linux */
48
#endif
49
50
#define CLEAR_CHR 0xde
51
52
static const char *pool_system_get_name(pool_t pool);
53
static void pool_system_ref(pool_t pool);
54
static void pool_system_unref(pool_t *pool);
55
static void *pool_system_malloc(pool_t pool, size_t size);
56
static void *pool_system_realloc(pool_t pool, void *mem,
57
         size_t old_size, size_t new_size);
58
static void pool_system_clear(pool_t pool);
59
static size_t pool_system_get_max_easy_alloc_size(pool_t pool);
60
61
static struct pool_vfuncs static_system_pool_vfuncs = {
62
  pool_system_get_name,
63
64
  pool_system_ref,
65
  pool_system_unref,
66
67
  pool_system_malloc,
68
  pool_system_free,
69
70
  pool_system_realloc,
71
72
  pool_system_clear,
73
  pool_system_get_max_easy_alloc_size
74
};
75
76
struct pool static_system_pool = {
77
  .v = &static_system_pool_vfuncs,
78
79
  .alloconly_pool = FALSE,
80
  .datastack_pool = FALSE
81
};
82
83
pool_t system_pool = &static_system_pool;
84
85
static const char *pool_system_get_name(pool_t pool ATTR_UNUSED)
86
0
{
87
0
  return "system";
88
0
}
89
90
static void pool_system_ref(pool_t pool ATTR_UNUSED)
91
16.7k
{
92
16.7k
}
93
94
static void pool_system_unref(pool_t *pool ATTR_UNUSED)
95
16.7k
{
96
16.7k
}
97
98
static void *pool_system_malloc(pool_t pool ATTR_UNUSED, size_t size)
99
381k
{
100
381k
  int old_errno = errno;
101
381k
  void *mem;
102
103
381k
  mem = calloc(size, 1);
104
381k
  if (unlikely(mem == NULL)) {
105
0
    i_fatal_status(FATAL_OUTOFMEM, "pool_system_malloc(%zu): "
106
0
             "Out of memory", size);
107
0
  }
108
381k
  errno = old_errno;
109
381k
  return mem;
110
381k
}
111
112
void pool_system_free(pool_t pool ATTR_UNUSED, void *mem)
113
381k
{
114
381k
  int old_errno = errno;
115
116
#if defined(HAVE_MALLOC_USABLE_SIZE) && defined(DEBUG)
117
  if (mem != NULL)
118
    safe_memset(mem, CLEAR_CHR, malloc_usable_size(mem));
119
#endif
120
381k
  free(mem);
121
381k
  errno = old_errno;
122
381k
}
123
124
static void *pool_system_realloc(pool_t pool ATTR_UNUSED, void *mem,
125
         size_t old_size, size_t new_size)
126
1.90k
{
127
1.90k
#if defined(HAVE_MALLOC_USABLE_SIZE)
128
1.90k
  i_assert(old_size == SIZE_MAX || mem == NULL ||
129
1.90k
     old_size <= malloc_usable_size(mem));
130
1.90k
#endif
131
132
1.90k
  int old_errno = errno;
133
1.90k
  mem = realloc(mem, new_size);
134
1.90k
  if (unlikely(mem == NULL)) {
135
0
    i_fatal_status(FATAL_OUTOFMEM, "pool_system_realloc(%zu): "
136
0
             "Out of memory", new_size);
137
0
  }
138
1.90k
  errno = old_errno;
139
140
1.90k
  if (old_size < new_size) {
141
    /* clear new data */
142
1.90k
    memset((char *) mem + old_size, 0, new_size - old_size);
143
1.90k
  }
144
145
1.90k
  return mem;
146
1.90k
}
147
148
static void ATTR_NORETURN
149
pool_system_clear(pool_t pool ATTR_UNUSED)
150
0
{
151
0
  i_panic("pool_system_clear() must not be called");
152
0
}
153
154
static size_t pool_system_get_max_easy_alloc_size(pool_t pool ATTR_UNUSED)
155
1.90k
{
156
1.90k
  return 0;
157
1.90k
}