Coverage Report

Created: 2026-11-22 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/numactl/fuzz/fuzz_libnuma_api.c
Line
Count
Source
1
/*
2
 * fuzz_libnuma_api.c - libFuzzer target for libnuma's node/cpu policy, memory
3
 * placement, affinity and bitmask APIs (numactl project).
4
 *
5
 * Threat model
6
 * ============
7
 * 1. Untrusted input: applications link libnuma and feed it values that are
8
 *    ultimately user-influenced: node ids, cpu ids, allocation
9
 *    sizes, policy enums, affinity masks and migration arguments. The numactl
10
 *    CLI is the canonical example: `numactl --membind <string> -- <cmd>`
11
 *    parses an untrusted string and hands the resulting mask to numa_bind(),
12
 *    numa_set_membind(), numa_migrate_pages() and friends. System agents
13
 *    (numad, monitoring, orchestrators) do the same with externally visible
14
 *    topology data. However the input is not untrusted and is not expected
15
 *    to cross trust boundaries, because that would be unsafe anyways (changing
16
 *    CPU or node affinity can cause DoS)
17
 *
18
 * Why this target (coverage rationale)
19
 * ====================================
20
 * OSS-Fuzz Fuzz Introspector lists as top unreached high-complexity functions:
21
 * numa_bind_v1/v2, numa_run_on_node_mask_v1/v2/all, numa_get_run_node_mask_*,
22
 * numa_run_on_node, numa_node_of_cpu, numa_parse_nodestring_all - all
23
 * statically reachable from fuzz_parse_str but dynamically never executed.
24
 * The wrappers in syscall.c (mbind/set_mempolicy/get_mempolicy/migrate_pages/
25
 * move_pages/sched_setaffinity) sit at 12.5% file coverage and the numa.h
26
 * v1-compat inline helpers at 2.6%. This target drives all of them through a
27
 * stateful op stream.
28
 *
29
 * Design notes
30
 * ============
31
 * - Stateful sequence: each iteration decodes an op stream (opcode + bounded
32
 *   arguments) and drives multiple API calls, so multi-call states (policy
33
 *   set then allocate under that policy, mask built then bound) are reached.
34
 * - All allocations are bounded (masks <= 4096 bits, memory areas <= 252 KiB,
35
 *   few live objects) and freed within the iteration, so OOM/leaks cannot be
36
 *   harness artefacts.
37
 * - numa_error()/numa_warn() are overridden with the library's documented
38
 *   weak-override mechanism purely to keep expected EINVAL noise out of the
39
 *   log and out of the library's exit-on-error path; sanitizer detection is
40
 *   unchanged and nothing is silenced.
41
 * - The compatibility inline helpers (numa.h) and the SYMVER v1/v2 symbol
42
 *   pairs are called explicitly: they are part of the API surface and dark in
43
 *   the production report.
44
 */
45
#include <stdint.h>
46
#include <stdlib.h>
47
#include <string.h>
48
#include <stdio.h>
49
#include <unistd.h>
50
#include <sys/mman.h>
51
#include "numa.h"
52
#include "numaif.h"
53
#include "numaint.h"
54
55
#define MAX_MASK_BITS   4096u          /* cap for numa_bitmask_alloc() */
56
0
#define MAX_AREA_PAGES  63u            /* memory areas <= 252 KiB */
57
#define NUMA_NUM_NODES_LOCAL 128       /* nodemask_t width on x86_64 */
58
59
/* quiet overrides of the documented weak error/warning hooks: expected
60
   EINVAL/EXDEV noise from policy syscalls must not spam the log. Detection
61
   behaviour is unchanged. */
62
static int g_numa_errors;
63
void numa_error(char *where)
64
0
{
65
0
        (void)where;
66
0
        g_numa_errors++;
67
0
}
68
69
void numa_warn(int num, char *fmt, ...)
70
{
71
        (void)num;
72
        (void)fmt;
73
}
74
75
static int g_numa_ok;
76
77
/* live bitmask slots (freed at end of iteration) */
78
0
#define MAX_MASKS 3
79
static struct bitmask *g_masks[MAX_MASKS];
80
81
/* live memory areas from the numa_alloc family */
82
0
#define MAX_AREAS 2
83
struct area {
84
        void *ptr;
85
        size_t size;
86
};
87
static struct area g_areas[MAX_AREAS];
88
89
static struct bitmask *mask_at(unsigned slot)
90
0
{
91
0
        if (g_masks[slot] == NULL) {
92
0
                unsigned n = 1 + (slot % 4);   /* tiny default mask */
93
0
                g_masks[slot] = numa_bitmask_alloc(n);
94
0
        }
95
0
        return g_masks[slot];
96
0
}
97
98
static void free_masks(void)
99
0
{
100
0
        unsigned i;
101
0
        for (i = 0; i < MAX_MASKS; i++) {
102
0
                if (g_masks[i]) {
103
0
                        numa_bitmask_free(g_masks[i]);
104
0
                        g_masks[i] = NULL;
105
0
                }
106
0
        }
107
0
}
108
109
static void free_areas(void)
110
0
{
111
0
        unsigned i;
112
0
        for (i = 0; i < MAX_AREAS; i++) {
113
0
                if (g_areas[i].ptr) {
114
0
                        numa_free(g_areas[i].ptr, g_areas[i].size);
115
0
                        g_areas[i].ptr = NULL;
116
0
                        g_areas[i].size = 0;
117
0
                }
118
0
        }
119
0
}
120
121
/* allocate (or reuse) an area of bounded size in slot */
122
static struct area *area_at(unsigned slot, size_t size)
123
0
{
124
0
        struct area *a = &g_areas[slot % MAX_AREAS];
125
0
        if (a->ptr && a->size != size) {
126
0
                numa_free(a->ptr, a->size);
127
0
                a->ptr = NULL;
128
0
                a->size = 0;
129
0
        }
130
0
        if (!a->ptr) {
131
0
                a->ptr = numa_alloc(size);
132
0
                if (a->ptr == NULL)
133
0
                        return NULL;
134
0
                a->size = size;
135
0
        }
136
0
        return a;
137
0
}
138
139
static size_t fuzz_area_size(unsigned arg)
140
0
{
141
0
        return (size_t)(arg % MAX_AREA_PAGES + 1) *
142
0
               (size_t)sysconf(_SC_PAGESIZE);
143
0
}
144
145
int LLVMFuzzerInitialize(int *argc, char ***argv)
146
1
{
147
1
        (void)argc;
148
1
        (void)argv;
149
1
        g_numa_ok = numa_available() >= 0;
150
1
        return 0;
151
1
}
152
153
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
154
{
155
        unsigned max_node = g_numa_ok ? (unsigned)numa_max_node() : 0;
156
157
        while (size >= 4) {
158
                unsigned op = data[0];
159
                unsigned a0 = data[1];
160
                unsigned a1 = data[2] | ((unsigned)data[3] << 8);
161
                data += 4;
162
                size -= 4;
163
164
                /* option matrix: strictness flags flip frequently */
165
                if (op % 7 == 6) {
166
                        numa_set_strict(a0 & 1);
167
                        numa_set_bind_policy((a0 >> 1) & 1);
168
                        numa_fail_alloc_on_error = (a0 >> 2) & 1;
169
                }
170
171
                switch (op % 12) {
172
                case 0: { /* bitmask component API */
173
                        unsigned n = 1 + (a0 % MAX_MASK_BITS);
174
                        struct bitmask *m = numa_bitmask_alloc(n);
175
                        if (m) {
176
                                numa_bitmask_setall(m);
177
                                numa_bitmask_setbit(m, a1 % n);
178
                                numa_bitmask_clearbit(m, a1 % n);
179
                                (void)numa_bitmask_isbitset(m, a1 % n);
180
                                (void)numa_bitmask_weight(m);
181
                                (void)numa_bitmask_nbytes(m);
182
                                numa_bitmask_free(m);
183
                        }
184
                        break;
185
                }
186
                case 1: { /* bitmask slots + equality/compare */
187
                        struct bitmask *m1 = mask_at(a0 % MAX_MASKS);
188
                        struct bitmask *m2 = mask_at((a0 + 1) % MAX_MASKS);
189
                        if (m1 && m2) {
190
                                numa_bitmask_clearall(m1);
191
                                numa_bitmask_setbit(m1, a1 % m1->size);
192
                                (void)numa_bitmask_equal(m1, m2);
193
                                copy_bitmask_to_bitmask(m1, m2);
194
                        }
195
                        break;
196
                }
197
                case 2: { /* nodemask_t compat inline surface */
198
                        nodemask_t nm1, nm2;
199
                        unsigned node = a0 % NUMA_NUM_NODES_LOCAL;
200
                        nodemask_zero(&nm1);
201
                        nodemask_set_compat(&nm1, (int)node);
202
                        nodemask_clr_compat(&nm1, (int)(node ^ 1));
203
                        (void)nodemask_isset_compat(&nm1, (int)node);
204
                        copy_nodemask_to_bitmask(&nm1, mask_at(0));
205
                        copy_bitmask_to_nodemask(mask_at(0), &nm2);
206
                        (void)nodemask_equal(&nm1, &nm2);
207
                        break;
208
                }
209
                case 3: { /* set memory policy (v2 APIs) */
210
                        struct bitmask *big = numa_allocate_nodemask();
211
                        if (big) {
212
                                unsigned node = a0 % NUMA_NUM_NODES_LOCAL;
213
                                if (node < big->size)
214
                                        numa_bitmask_setbit(big, node);
215
                                switch (a1 % 5) {
216
                                case 0: numa_set_membind(mask_at(0)); break;
217
                                case 1: numa_set_interleave_mask(big); break;
218
                                case 2: numa_set_localalloc(); break;
219
                                case 3:
220
                                        if (numa_has_preferred_many())
221
                                                numa_set_preferred_many(big);
222
                                        else
223
                                                numa_set_preferred((int)(a0 % (max_node + 1)));
224
                                        break;
225
                                default:
226
                                        numa_set_weighted_interleave_mask(big);
227
                                        break;
228
                                }
229
                        }
230
                        if (big)
231
                                numa_bitmask_free(big);
232
                        break;
233
                }
234
                case 4: { /* get memory policy (v2 + v1 compat) */
235
                        struct bitmask *m;
236
                        nodemask_t nm;
237
                        switch (a0 % 6) {
238
                        case 0: m = numa_get_membind(); break;
239
                        case 1: m = numa_get_interleave_mask(); break;
240
                        case 2: m = numa_get_mems_allowed(); break;
241
                        case 3: m = numa_get_weighted_interleave_mask(); break;
242
                        case 4:
243
                                nm = numa_get_interleave_mask_compat();
244
                                copy_bitmask_to_nodemask(mask_at(0), &nm);
245
                                m = NULL;
246
                                break;
247
                        default:
248
                                nm = numa_get_membind_compat();
249
                                m = NULL;
250
                                break;
251
                        }
252
                        if (m)
253
                                numa_bitmask_free(m);
254
                        (void)numa_preferred();
255
                        (void)numa_preferred_err();
256
                        break;
257
                }
258
                case 5: { /* bind + run (the introspector top-10) */
259
                        struct bitmask *m = mask_at(0);
260
                        if (m) {
261
                                if (a1 % 2)
262
                                        numa_bind(m);
263
                                else
264
                                        numa_run_on_node_mask(m);
265
                        }
266
                        if (a1 % 3 == 0)
267
                                numa_run_on_node_mask_all(mask_at(1));
268
                        (void)numa_run_on_node((int)(a1 % (max_node + 1)));
269
                        {
270
                                struct bitmask *rm = numa_get_run_node_mask();
271
                                if (rm)
272
                                        numa_bitmask_free(rm);
273
                        }
274
                        break;
275
                }
276
                case 6: { /* sched affinity, v1 compat + v2 */
277
                        unsigned long buf[32];
278
                        struct bitmask *cm = numa_allocate_cpumask();
279
                        unsigned len = a0 % 16 + 1;
280
                        if (len <= 32 && cm) {
281
                                memset(buf, 0, sizeof(buf));
282
                                buf[0] = 1UL << (a1 % 64);
283
                                (void)numa_sched_setaffinity_compat(0, len, buf);
284
                                (void)numa_sched_getaffinity_compat(0, len, buf);
285
                                (void)numa_sched_setaffinity(0, cm);
286
                                (void)numa_sched_getaffinity(0, cm);
287
                        }
288
                        if (cm)
289
                                numa_bitmask_free(cm);
290
                        break;
291
                }
292
                case 7: { /* numa_alloc family + numa_free/numa_realloc */
293
                        size_t sz = fuzz_area_size(a0);
294
                        unsigned slot = a1 % MAX_AREAS;
295
                        struct area *a = &g_areas[slot];
296
                        free_areas();
297
                        switch (a0 % 8) {
298
                        case 1:
299
                                a->ptr = numa_alloc_onnode(sz, (int)(a1 % (max_node + 1)));
300
                                break;
301
                        case 2:
302
                                a->ptr = numa_alloc_interleaved(sz);
303
                                break;
304
                        case 3:
305
                                a->ptr = numa_alloc_interleaved_subset(sz, mask_at(0));
306
                                break;
307
                        case 5:
308
                                a->ptr = numa_alloc_local(sz);
309
                                break;
310
                        case 6:
311
                                a->ptr = numa_alloc_weighted_interleaved(sz);
312
                                break;
313
                        case 7:
314
                                a->ptr = numa_alloc_weighted_interleaved_subset(sz, mask_at(0));
315
                                break;
316
                        default:
317
                                a->ptr = numa_alloc(sz);
318
                                break;
319
                        }
320
                        if (a->ptr) {
321
                                a->size = sz;
322
                                ((volatile char *)a->ptr)[0] = 1;  /* touch first page */
323
                                if (a0 % 3 == 0) {
324
                                        size_t nsz = fuzz_area_size(a1);
325
                                        void *np = numa_realloc(a->ptr, a->size, nsz);
326
                                        if (np) {
327
                                                a->ptr = np;
328
                                                a->size = nsz;
329
                                        }
330
                                }
331
                        } else {
332
                                a->size = 0;
333
                        }
334
                        break;
335
                }
336
                case 8: { /* page placement on live areas */
337
                        struct area *a = area_at(a0 % MAX_AREAS, fuzz_area_size(a1));
338
                        if (a && a->ptr) {
339
                                switch (a1 % 5) {
340
                                case 0:
341
                                        numa_tonode_memory(a->ptr, a->size,
342
                                                           (int)(a0 % (max_node + 1)));
343
                                        break;
344
                                case 1:
345
                                        numa_tonodemask_memory(a->ptr, a->size, mask_at(0));
346
                                        break;
347
                                case 2:
348
                                        numa_interleave_memory(a->ptr, a->size, mask_at(0));
349
                                        break;
350
                                case 3:
351
                                        numa_weighted_interleave_memory(a->ptr, a->size, mask_at(0));
352
                                        break;
353
                                default:
354
                                        numa_police_memory(a->ptr, a->size);
355
                                        break;
356
                                }
357
                        }
358
                        break;
359
                }
360
                case 9: { /* migration + page motion */
361
                        struct area *a = area_at(0, fuzz_area_size(a0));
362
                        if (a && a->ptr) {
363
                                struct bitmask *from = numa_allocate_nodemask();
364
                                struct bitmask *to = numa_allocate_nodemask();
365
                                if (from && to) {
366
                                        void *pages[4];
367
                                        int nodes[4];
368
                                        int status[4];
369
                                        unsigned np = a0 % 4 + 1;
370
                                        unsigned i;
371
                                        size_t pg = (size_t)sysconf(_SC_PAGESIZE);
372
                                        for (i = 0; i < np; i++) {
373
                                                pages[i] = (char *)a->ptr + i * pg;
374
                                                nodes[i] = (int)(a1 % (max_node + 1));
375
                                                status[i] = -1;
376
                                        }
377
                                        numa_bitmask_setbit(from, 0);
378
                                        if (max_node)
379
                                                numa_bitmask_setbit(to, (int)(a1 % (max_node + 1)));
380
                                        (void)numa_migrate_pages(getpid(), from, to);
381
                                        (void)numa_move_pages(getpid(), np, pages, nodes,
382
                                                              status,
383
                                                              (a1 % 2) ? 1 /*MPOL_MF_MOVE*/ : 0);
384
                                }
385
                                if (from)
386
                                        numa_bitmask_free(from);
387
                                if (to)
388
                                        numa_bitmask_free(to);
389
                        }
390
                        break;
391
                }
392
                case 10: { /* topology queries */
393
                        struct bitmask *cm = numa_allocate_cpumask();
394
                        int node = (int)(a0 % (max_node + 1));
395
                        unsigned long buf[256];
396
                        if (cm) {
397
                                (void)numa_node_to_cpus(node, cm);
398
                                (void)numa_node_to_cpus_compat(node, buf, 256);
399
                        }
400
                        (void)numa_is_cpuless_node((int)(a1 % (max_node + 1)));
401
                        (void)numa_node_of_cpu((int)(a1 % 1024)); /* -1/errno is documented */
402
                        (void)numa_node_size64(node, NULL);
403
                        (void)numa_num_configured_nodes();
404
                        (void)numa_num_configured_cpus();
405
                        (void)numa_num_possible_nodes();
406
                        (void)numa_num_possible_cpus();
407
                        (void)numa_num_task_cpus();
408
                        (void)numa_num_task_nodes();
409
                        if (cm)
410
                                numa_bitmask_free(cm);
411
                        break;
412
                }
413
                default: { /* home node + area local placement */
414
                        struct area *a = area_at(0, fuzz_area_size(a0));
415
                        if (a && a->ptr) {
416
                                if (numa_has_home_node() && a1 % 2)
417
                                        (void)numa_set_mempolicy_home_node(a->ptr, a->size,
418
                                                                           (int)(a1 % (max_node + 1)),
419
                                                                           0);
420
                                if (a1 % 3 == 0)
421
                                        numa_setlocal_memory(a->ptr, a->size);
422
                        }
423
                        break;
424
                }
425
                }
426
        }
427
428
        if (g_numa_ok) {
429
                /* restore ambient state so iterations stay independent */
430
                numa_set_localalloc();
431
                numa_set_strict(0);
432
                numa_set_bind_policy(0);
433
                numa_fail_alloc_on_error = 0;
434
        }
435
        free_masks();
436
        free_areas();
437
        return 0;
438
}