summaryrefslogtreecommitdiff
path: root/tests/test-ssfmalloc.c
blob: ff1d74122d27670df3bbed7d143f1afdaf375ea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Test of simple and straight-forward malloc implementation.
   Copyright (C) 2020-2023 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2020.  */

#include <config.h>

#include <stdint.h>
#include <stdlib.h>

#if defined _WIN32 && !defined __CYGWIN__

/* Declare VirtualAlloc(), GetSystemInfo.  */
# define WIN32_LEAN_AND_MEAN
# define WIN32_EXTRA_LEAN
# include <windows.h>

#else

/* Declare getpagesize(). */
# include <unistd.h>
/* On HP-UX, getpagesize exists, but it is not declared in <unistd.h> even if
   the compiler options -D_HPUX_SOURCE -D_XOPEN_SOURCE=600 are used.  */
# ifdef __hpux
extern
#  ifdef __cplusplus
       "C"
#  endif
       int getpagesize (void);
# endif

/* Declare mmap().  */
# include <sys/types.h>
# include <sys/mman.h>

/* Some old mmap() implementations require the flag MAP_VARIABLE whenever you
   pass an addr == NULL. */
# ifndef MAP_VARIABLE
#  define MAP_VARIABLE 0
# endif

#endif

/* ================= Back end of the malloc implementation ================= */

/* The memory page size.
   Once it is initialized, a power of 2.  Typically 4096 or 8192.  */
static uintptr_t pagesize;

/* Initializes pagesize.  */
static void
init_pagesize (void)
{
#if defined _WIN32 && !defined __CYGWIN__
  /* GetSystemInfo
     <https://msdn.microsoft.com/en-us/library/ms724381.aspx>
     <https://msdn.microsoft.com/en-us/library/ms724958.aspx>  */
  SYSTEM_INFO info;
  GetSystemInfo (&info);
  pagesize = info.dwPageSize;
#else
  pagesize = getpagesize ();
#endif
}

/* Allocates a contiguous set of pages of memory.
   size > 0, must be a multiple of pagesize.
   Returns a multiple of PAGESIZE, or 0 upon failure.  */
static uintptr_t
alloc_pages (size_t size)
{
#if defined _WIN32 && !defined __CYGWIN__
  /* VirtualAlloc
     <https://msdn.microsoft.com/en-us/library/aa366887.aspx>
     <https://msdn.microsoft.com/en-us/library/aa366786.aspx>  */
  void *mem = VirtualAlloc (NULL, size, MEM_COMMIT, PAGE_READWRITE);
  if (mem == NULL)
    return 0;
  return (uintptr_t) mem;
#else
  /* Use mmap with the MAP_ANONYMOUS or MAP_ANON flag.  */
  void *mem = mmap (NULL, size, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANONYMOUS | MAP_VARIABLE, -1, 0);
  if (mem == (void *)(-1))
    return 0;
  return (uintptr_t) mem;
#endif
}

/* Frees a contiguous set of pages of memory, returned by alloc_pages.
   size > 0, must be a multiple of pagesize.  */
static void
free_pages (uintptr_t pages, size_t size)
{
#if defined _WIN32 && !defined __CYGWIN__
  /* VirtualFree
     <https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree>  */
  if (!VirtualFree ((void *) pages, 0, MEM_RELEASE))
    abort ();
#else
  if ((pages & (pagesize - 1)) != 0)
    abort ();
  if (munmap ((void *) pages, size) < 0)
    abort ();
#endif
}

/* Cygwin defines PAGESIZE in <limits.h>.  */
#undef PAGESIZE

/* ======================= Instantiate the front end ======================= */

#define PAGESIZE pagesize
/* On Cygwin and Linux/PowerPC, PAGESIZE is 65536.  On macOS 11, it is 16384.
   On all other platforms, it is either 4096 or 8192.  */
#if defined __CYGWIN__ || (defined __linux__ && defined __powerpc__)
# define PAGESIZE_MAX 65536
#else
# define PAGESIZE_MAX 16384
#endif

#define ALLOC_PAGES alloc_pages
#define FREE_PAGES free_pages
#define ALIGNMENT (sizeof (void *)) /* or 8 or 16 or 32 */
#define PAGE_RESERVED_HEADER_SIZE (3 * UINTPTR_WIDTH / 8) /* = 3 * sizeof (void *) */

#include "ssfmalloc.h"

/* ================================= Tests ================================= */

#include <limits.h>
#include <string.h>

#include "macros.h"

/* Fills a block of a given size with some contents.  */
static void
fill_block (uintptr_t block, size_t size)
{
  unsigned char code = (size % (UCHAR_MAX - 1)) + 1;
  memset ((char *) block, code, size);
}

/* Verifies that the contents of a block is still present
   (i.e. has not accidentally been overwritten by other operations).  */
static void
verify_block (uintptr_t block, size_t size)
{
  unsigned char code = (size % (UCHAR_MAX - 1)) + 1;
  char *p = (char *) block;
  for (; size > 0; p++, size--)
    if ((unsigned char) *p != code)
      abort ();
}

static size_t block_sizes[] =
  {
    /* Small blocks.  */
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    12,
    15,
    16,
    17,
    24,
    31,
    32,
    37,
    42,
    49,
    57,
    63,
    64,
    65,
    71,
    77,
    83,
    96,
    99,
    110,
    119,
    127,
    128,
    130,
    144,
    150,
    157,
    161,
    169,
    180,
    192,
    199,
    204,
    210,
    224,
    225,
    236,
    241,
    249,
    255,
    256,
    /* Medium blocks.  */
    257,
    281,
    284,
    294,
    301,
    308,
    341,
    447,
    525,
    659,
    771,
    842,
    729,
    999,
    1000,
    1020,
    1023,
    1024,
    1025,
    1280,
    1414,
    2047,
    2048,
    2049,
    2096,
    2401,
    2613,
    2843,
    3010,
    3213,
    3512,
    3678,
    3801,
    3900,
    /* Large blocks.  */
    4000,
    4060,
    4080,
    4090,
    4095,
    4096,
    4097,
    4121,
    5381,
    7814,
    8191,
    8192,
    8193,
    11238,
    16383,
    16384,
    16385,
    20184,
    51202,
    135010
  };

#define RANDOM(n) (rand () % (n))
#define RANDOM_BLOCK_SIZE() block_sizes[RANDOM (SIZEOF (block_sizes))]

int
main (int argc, char *argv[])
{
  /* Allow the user to provide a non-default random seed on the command line.  */
  if (argc > 1)
    srand (atoi (argv[1]));

  init_pagesize ();

  /* Randomly allocate and deallocate blocks.
     Also verify that there are no unexpected modifications to the contents of
     these blocks.  */
  {
    unsigned int repeat;
    char *blocks[SIZEOF (block_sizes)];

    {
      size_t i;

      for (i = 0; i < SIZEOF (block_sizes); i++)
        blocks[i] = NULL;
    }

    for (repeat = 0; repeat < 100000; repeat++)
      {
        unsigned int operation = RANDOM (2);

        switch (operation)
          {
          case 0:
            { /* Allocate a block.  */
              size_t i = RANDOM (SIZEOF (block_sizes));
              size_t size = block_sizes[i];
              if (blocks[i] == NULL)
                {
                  uintptr_t block = allocate_block (size);
                  if (block == 0)
                    abort ();
                  fill_block (block, size);
                  blocks[i] = (char *) block;
                }
            }
            break;
          case 1:
            { /* Free a block.  */
              size_t i = RANDOM (SIZEOF (block_sizes));
              size_t size = block_sizes[i];
              if (blocks[i] != NULL)
                {
                  uintptr_t block = (uintptr_t) blocks[i];
                  verify_block (block, size);
                  free_block (block);
                  blocks[i] = NULL;
                }
            }
            break;
          }
      }

    /* Free the remaining blocks.  */
    {
      size_t i;

      for (i = 0; i < SIZEOF (block_sizes); i++)
        if (blocks[i] != NULL)
          {
            uintptr_t block = (uintptr_t) blocks[i];
            size_t size = block_sizes[i];
            verify_block (block, size);
            free_block (block);
          }
    }
  }

  return 0;
}