Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/ssl/priority_queue.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/crypto.h>
11
#include <openssl/err.h>
12
#include <assert.h>
13
#include "internal/priority_queue.h"
14
#include "internal/safe_math.h"
15
#include "internal/numbers.h"
16
17
OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
18
19
/*
20
 * Fundamental operations:
21
 *                        Binary Heap   Fibonacci Heap
22
 *  Get smallest            O(1)          O(1)
23
 *  Delete any              O(log n)      O(log n) average but worst O(n)
24
 *  Insert                  O(log n)      O(1)
25
 *
26
 * Not supported:
27
 *  Merge two structures    O(log n)      O(1)
28
 *  Decrease key            O(log n)      O(1)
29
 *  Increase key            O(log n)      ?
30
 *
31
 * The Fibonacci heap is quite a bit more complicated to implement and has
32
 * larger overhead in practice.  We favour the binary heap here.  A multi-way
33
 * (ternary or quaternary) heap might elicit a performance advantage via better
34
 * cache access patterns.
35
 */
36
37
struct pq_heap_st {
38
    void *data; /* User supplied data pointer */
39
    size_t index; /* Constant index in elements[] */
40
};
41
42
struct pq_elem_st {
43
    size_t posn; /* Current index in heap[] or link in free list */
44
#ifndef NDEBUG
45
    int used; /* Debug flag indicating that this is in use */
46
#endif
47
};
48
49
struct ossl_pqueue_st {
50
    struct pq_heap_st *heap;
51
    struct pq_elem_st *elements;
52
    int (*compare)(const void *, const void *);
53
    size_t htop; /* Highest used heap element */
54
    size_t hmax; /* Allocated heap & element space */
55
    size_t freelist; /* Index into elements[], start of free element list */
56
};
57
58
/*
59
 * The initial and maximum number of elements in the heap.
60
 */
61
static const size_t min_nodes = 8;
62
static const size_t max_nodes = SIZE_MAX / (sizeof(struct pq_heap_st) > sizeof(struct pq_elem_st) ? sizeof(struct pq_heap_st) : sizeof(struct pq_elem_st));
63
64
#ifndef NDEBUG
65
/* Some basic sanity checking of the data structure */
66
#define ASSERT_USED(pq, idx)                        \
67
178M
    assert(pq->elements[pq->heap[idx].index].used); \
68
174M
    assert(pq->elements[pq->heap[idx].index].posn == idx)
69
#define ASSERT_ELEM_USED(pq, elem) \
70
3.61M
    assert(pq->elements[elem].used)
71
#else
72
#define ASSERT_USED(pq, idx)
73
#define ASSERT_ELEM_USED(pq, elem)
74
#endif
75
76
/*
77
 * Calculate the array growth based on the target size.
78
 *
79
 * The growth factor is a rational number and is defined by a numerator
80
 * and a denominator.  According to Andrew Koenig in his paper "Why Are
81
 * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
82
 * than the golden ratio (1.618...).
83
 *
84
 * We use an expansion factor of 8 / 5 = 1.6
85
 */
86
static ossl_inline size_t compute_pqueue_growth(size_t target, size_t current)
87
89.5k
{
88
89.5k
    int err = 0;
89
90
179k
    while (current < target) {
91
89.5k
        if (current >= max_nodes)
92
0
            return 0;
93
94
89.5k
        current = safe_muldiv_size_t(current, 8, 5, &err);
95
89.5k
        if (err)
96
0
            return 0;
97
89.5k
        if (current >= max_nodes)
98
0
            current = max_nodes;
99
89.5k
    }
100
89.5k
    return current;
101
89.5k
}
102
103
static ossl_inline void pqueue_swap_elem(OSSL_PQUEUE *pq, size_t i, size_t j)
104
33.5M
{
105
33.5M
    struct pq_heap_st *h = pq->heap, t_h;
106
33.5M
    struct pq_elem_st *e = pq->elements;
107
108
33.5M
    ASSERT_USED(pq, i);
109
67.0M
    ASSERT_USED(pq, j);
110
111
67.0M
    t_h = h[i];
112
33.5M
    h[i] = h[j];
113
33.5M
    h[j] = t_h;
114
115
33.5M
    e[h[i].index].posn = i;
116
33.5M
    e[h[j].index].posn = j;
117
33.5M
}
118
119
static ossl_inline void pqueue_move_elem(OSSL_PQUEUE *pq, size_t from, size_t to)
120
5.46M
{
121
5.46M
    struct pq_heap_st *h = pq->heap;
122
5.46M
    struct pq_elem_st *e = pq->elements;
123
124
5.46M
    ASSERT_USED(pq, from);
125
126
5.46M
    h[to] = h[from];
127
5.46M
    e[h[to].index].posn = to;
128
5.46M
}
129
130
/*
131
 * Force the specified element to the front of the heap.  This breaks
132
 * the heap partial ordering pre-condition.
133
 */
134
static ossl_inline void pqueue_force_bottom(OSSL_PQUEUE *pq, size_t n)
135
0
{
136
0
    ASSERT_USED(pq, n);
137
0
    while (n > 0) {
138
0
        const size_t p = (n - 1) / 2;
139
140
0
        ASSERT_USED(pq, p);
141
0
        pqueue_swap_elem(pq, n, p);
142
0
        n = p;
143
0
    }
144
0
}
145
146
/*
147
 * Move an element down to its correct position to restore the partial
148
 * order pre-condition.
149
 */
150
static ossl_inline void pqueue_move_down(OSSL_PQUEUE *pq, size_t n)
151
6.07M
{
152
6.07M
    struct pq_heap_st *h = pq->heap;
153
154
6.07M
    ASSERT_USED(pq, n);
155
8.93M
    while (n > 0) {
156
8.23M
        const size_t p = (n - 1) / 2;
157
158
8.23M
        ASSERT_USED(pq, p);
159
8.23M
        if (pq->compare(h[n].data, h[p].data) >= 0)
160
5.37M
            break;
161
2.85M
        pqueue_swap_elem(pq, n, p);
162
2.85M
        n = p;
163
2.85M
    }
164
6.07M
}
165
166
/*
167
 * Move an element up to its correct position to restore the partial
168
 * order pre-condition.
169
 */
170
static ossl_inline void pqueue_move_up(OSSL_PQUEUE *pq, size_t n)
171
5.46M
{
172
5.46M
    struct pq_heap_st *h = pq->heap;
173
5.46M
    size_t p = n * 2 + 1;
174
175
5.46M
    ASSERT_USED(pq, n);
176
5.46M
    if (pq->htop > p + 1) {
177
5.23M
        ASSERT_USED(pq, p);
178
10.4M
        ASSERT_USED(pq, p + 1);
179
10.4M
        if (pq->compare(h[p].data, h[p + 1].data) > 0)
180
1.22M
            p++;
181
5.23M
    }
182
36.1M
    while (pq->htop > p && pq->compare(h[p].data, h[n].data) < 0) {
183
30.6M
        ASSERT_USED(pq, p);
184
30.6M
        pqueue_swap_elem(pq, n, p);
185
30.6M
        n = p;
186
30.6M
        p = n * 2 + 1;
187
30.6M
        if (pq->htop > p + 1) {
188
28.4M
            ASSERT_USED(pq, p + 1);
189
28.4M
            if (pq->compare(h[p].data, h[p + 1].data) > 0)
190
13.3M
                p++;
191
28.4M
        }
192
30.6M
    }
193
5.46M
}
194
195
int ossl_pqueue_push(OSSL_PQUEUE *pq, void *data, size_t *elem)
196
6.07M
{
197
6.07M
    size_t n, m;
198
199
6.07M
    if (!ossl_pqueue_reserve(pq, 1))
200
0
        return 0;
201
202
6.07M
    n = pq->htop++;
203
6.07M
    m = pq->freelist;
204
6.07M
    pq->freelist = pq->elements[m].posn;
205
206
6.07M
    pq->heap[n].data = data;
207
6.07M
    pq->heap[n].index = m;
208
209
6.07M
    pq->elements[m].posn = n;
210
6.07M
#ifndef NDEBUG
211
6.07M
    pq->elements[m].used = 1;
212
6.07M
#endif
213
6.07M
    pqueue_move_down(pq, n);
214
6.07M
    if (elem != NULL)
215
6.07M
        *elem = m;
216
6.07M
    return 1;
217
6.07M
}
218
219
void *ossl_pqueue_peek(const OSSL_PQUEUE *pq)
220
12.6M
{
221
12.6M
    if (pq->htop > 0) {
222
3.84M
        ASSERT_USED(pq, 0);
223
3.84M
        return pq->heap->data;
224
3.84M
    }
225
8.85M
    return NULL;
226
12.6M
}
227
228
void *ossl_pqueue_pop(OSSL_PQUEUE *pq)
229
9.50M
{
230
9.50M
    void *res;
231
9.50M
    size_t elem;
232
233
9.50M
    if (pq == NULL || pq->htop == 0)
234
3.98M
        return NULL;
235
236
11.0M
    ASSERT_USED(pq, 0);
237
11.0M
    res = pq->heap->data;
238
5.51M
    elem = pq->heap->index;
239
240
5.51M
    if (--pq->htop != 0) {
241
5.46M
        pqueue_move_elem(pq, pq->htop, 0);
242
5.46M
        pqueue_move_up(pq, 0);
243
5.46M
    }
244
245
5.51M
    pq->elements[elem].posn = pq->freelist;
246
5.51M
    pq->freelist = elem;
247
5.51M
#ifndef NDEBUG
248
5.51M
    pq->elements[elem].used = 0;
249
5.51M
#endif
250
5.51M
    return res;
251
11.0M
}
252
253
void *ossl_pqueue_remove(OSSL_PQUEUE *pq, size_t elem)
254
3.61M
{
255
3.61M
    size_t n;
256
257
3.61M
    if (pq == NULL || elem >= pq->hmax || pq->htop == 0)
258
0
        return 0;
259
260
3.61M
    ASSERT_ELEM_USED(pq, elem);
261
3.61M
    n = pq->elements[elem].posn;
262
263
3.61M
    ASSERT_USED(pq, n);
264
265
3.61M
    if (n == pq->htop - 1) {
266
556k
        pq->elements[elem].posn = pq->freelist;
267
556k
        pq->freelist = elem;
268
556k
#ifndef NDEBUG
269
556k
        pq->elements[elem].used = 0;
270
556k
#endif
271
556k
        return pq->heap[--pq->htop].data;
272
556k
    }
273
3.05M
    if (n > 0)
274
0
        pqueue_force_bottom(pq, n);
275
3.05M
    return ossl_pqueue_pop(pq);
276
3.61M
}
277
278
static void pqueue_add_freelist(OSSL_PQUEUE *pq, size_t from)
279
4.07M
{
280
4.07M
    struct pq_elem_st *e = pq->elements;
281
4.07M
    size_t i;
282
283
4.07M
#ifndef NDEBUG
284
41.9M
    for (i = from; i < pq->hmax; i++)
285
37.8M
        e[i].used = 0;
286
4.07M
#endif
287
4.07M
    e[from].posn = pq->freelist;
288
37.8M
    for (i = from + 1; i < pq->hmax; i++)
289
33.7M
        e[i].posn = i - 1;
290
4.07M
    pq->freelist = pq->hmax - 1;
291
4.07M
}
292
293
int ossl_pqueue_reserve(OSSL_PQUEUE *pq, size_t n)
294
6.07M
{
295
6.07M
    size_t new_max, cur_max;
296
6.07M
    struct pq_heap_st *h;
297
6.07M
    struct pq_elem_st *e;
298
299
6.07M
    if (pq == NULL)
300
0
        return 0;
301
6.07M
    cur_max = pq->hmax;
302
6.07M
    if (pq->htop + n < cur_max)
303
5.98M
        return 1;
304
305
89.5k
    new_max = compute_pqueue_growth(n + cur_max, cur_max);
306
89.5k
    if (new_max == 0) {
307
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
308
0
        return 0;
309
0
    }
310
311
89.5k
    h = OPENSSL_realloc(pq->heap, new_max * sizeof(*pq->heap));
312
89.5k
    if (h == NULL)
313
0
        return 0;
314
89.5k
    pq->heap = h;
315
316
89.5k
    e = OPENSSL_realloc(pq->elements, new_max * sizeof(*pq->elements));
317
89.5k
    if (e == NULL)
318
0
        return 0;
319
89.5k
    pq->elements = e;
320
321
89.5k
    pq->hmax = new_max;
322
89.5k
    pqueue_add_freelist(pq, cur_max);
323
89.5k
    return 1;
324
89.5k
}
325
326
OSSL_PQUEUE *ossl_pqueue_new(int (*compare)(const void *, const void *))
327
3.98M
{
328
3.98M
    OSSL_PQUEUE *pq;
329
330
3.98M
    if (compare == NULL)
331
0
        return NULL;
332
333
3.98M
    pq = OPENSSL_malloc(sizeof(*pq));
334
3.98M
    if (pq == NULL)
335
0
        return NULL;
336
3.98M
    pq->compare = compare;
337
3.98M
    pq->hmax = min_nodes;
338
3.98M
    pq->htop = 0;
339
3.98M
    pq->freelist = 0;
340
3.98M
    pq->heap = OPENSSL_malloc(sizeof(*pq->heap) * min_nodes);
341
3.98M
    pq->elements = OPENSSL_malloc(sizeof(*pq->elements) * min_nodes);
342
3.98M
    if (pq->heap == NULL || pq->elements == NULL) {
343
0
        ossl_pqueue_free(pq);
344
0
        return NULL;
345
0
    }
346
3.98M
    pqueue_add_freelist(pq, 0);
347
3.98M
    return pq;
348
3.98M
}
349
350
void ossl_pqueue_free(OSSL_PQUEUE *pq)
351
3.98M
{
352
3.98M
    if (pq != NULL) {
353
3.98M
        OPENSSL_free(pq->heap);
354
3.98M
        OPENSSL_free(pq->elements);
355
3.98M
        OPENSSL_free(pq);
356
3.98M
    }
357
3.98M
}
358
359
void ossl_pqueue_pop_free(OSSL_PQUEUE *pq, void (*freefunc)(void *))
360
0
{
361
0
    size_t i;
362
363
0
    if (pq != NULL) {
364
0
        for (i = 0; i < pq->htop; i++)
365
0
            (*freefunc)(pq->heap[i].data);
366
0
        ossl_pqueue_free(pq);
367
0
    }
368
0
}
369
370
size_t ossl_pqueue_num(const OSSL_PQUEUE *pq)
371
7.46M
{
372
7.46M
    return pq != NULL ? pq->htop : 0;
373
7.46M
}