Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/sparse_array.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <openssl/crypto.h>
12
#include <openssl/bn.h>
13
#include "crypto/sparse_array.h"
14
15
/*
16
 * How many bits are used to index each level in the tree structure?
17
 * This setting determines the number of pointers stored in each node of the
18
 * tree used to represent the sparse array.  Having more pointers reduces the
19
 * depth of the tree but potentially wastes more memory.  That is, this is a
20
 * direct space versus time tradeoff.
21
 *
22
 * The default is to use four bits which means that the are 16
23
 * pointers in each tree node.
24
 *
25
 * The library builder is also permitted to define other sizes in the closed
26
 * interval [2, sizeof(ossl_uintmax_t) * 8].  Space use generally scales
27
 * exponentially with the block size, although the implementation only
28
 * creates enough blocks to support the largest used index.  The depth is:
29
 *      ceil(log_2(largest index) / 2^{block size})
30
 * E.g. with a block size of 4, and a largest index of 1000, the depth
31
 * will be three.
32
 */
33
#ifndef OPENSSL_SA_BLOCK_BITS
34
1.89G
# define OPENSSL_SA_BLOCK_BITS           4
35
#elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > (BN_BITS2 - 1)
36
# error OPENSSL_SA_BLOCK_BITS is out of range
37
#endif
38
39
/*
40
 * From the number of bits, work out:
41
 *    the number of pointers in a tree node;
42
 *    a bit mask to quickly extract an index and
43
 *    the maximum depth of the tree structure.
44
  */
45
1.68G
#define SA_BLOCK_MAX            (1 << OPENSSL_SA_BLOCK_BITS)
46
717M
#define SA_BLOCK_MASK           (SA_BLOCK_MAX - 1)
47
206k
#define SA_BLOCK_MAX_LEVELS     (((int)sizeof(ossl_uintmax_t) * 8 \
48
206k
                                  + OPENSSL_SA_BLOCK_BITS - 1) \
49
206k
                                 / OPENSSL_SA_BLOCK_BITS)
50
51
struct sparse_array_st {
52
    int levels;
53
    ossl_uintmax_t top;
54
    size_t nelem;
55
    void **nodes;
56
};
57
58
OPENSSL_SA *ossl_sa_new(void)
59
163k
{
60
163k
    OPENSSL_SA *res = OPENSSL_zalloc(sizeof(*res));
61
62
163k
    return res;
63
163k
}
64
65
static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
66
                     void (*leaf)(ossl_uintmax_t, void *, void *), void *arg)
67
1.31M
{
68
1.31M
    int i[SA_BLOCK_MAX_LEVELS];
69
1.31M
    void *nodes[SA_BLOCK_MAX_LEVELS];
70
1.31M
    ossl_uintmax_t idx = 0;
71
1.31M
    int l = 0;
72
73
1.31M
    i[0] = 0;
74
1.31M
    nodes[0] = sa->nodes;
75
970M
    while (l >= 0) {
76
969M
        const int n = i[l];
77
969M
        void ** const p = nodes[l];
78
79
969M
        if (n >= SA_BLOCK_MAX) {
80
57.0M
            if (p != NULL && node != NULL)
81
143k
                (*node)(p);
82
57.0M
            l--;
83
57.0M
            idx >>= OPENSSL_SA_BLOCK_BITS;
84
912M
        } else {
85
912M
            i[l] = n + 1;
86
912M
            if (p != NULL && p[n] != NULL) {
87
92.6M
                idx = (idx & ~SA_BLOCK_MASK) | n;
88
92.6M
                if (l < sa->levels - 1) {
89
55.7M
                    i[++l] = 0;
90
55.7M
                    nodes[l] = p[n];
91
55.7M
                    idx <<= OPENSSL_SA_BLOCK_BITS;
92
55.7M
                } else if (leaf != NULL) {
93
36.9M
                    (*leaf)(idx, p[n], arg);
94
36.9M
                }
95
92.6M
            }
96
912M
        }
97
969M
    }
98
1.31M
}
99
100
static void sa_free_node(void **p)
101
143k
{
102
143k
    OPENSSL_free(p);
103
143k
}
104
105
static void sa_free_leaf(ossl_uintmax_t n, void *p, void *arg)
106
0
{
107
0
    OPENSSL_free(p);
108
0
}
109
110
void ossl_sa_free(OPENSSL_SA *sa)
111
162k
{
112
162k
    if (sa != NULL) {
113
162k
        sa_doall(sa, &sa_free_node, NULL, NULL);
114
162k
        OPENSSL_free(sa);
115
162k
    }
116
162k
}
117
118
void ossl_sa_free_leaves(OPENSSL_SA *sa)
119
0
{
120
0
    sa_doall(sa, &sa_free_node, &sa_free_leaf, NULL);
121
0
    OPENSSL_free(sa);
122
0
}
123
124
/* Wrap this in a structure to avoid compiler warnings */
125
struct trampoline_st {
126
    void (*func)(ossl_uintmax_t, void *);
127
};
128
129
static void trampoline(ossl_uintmax_t n, void *l, void *arg)
130
0
{
131
0
    ((const struct trampoline_st *)arg)->func(n, l);
132
0
}
133
134
void ossl_sa_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t, void *))
135
600
{
136
600
    struct trampoline_st tramp;
137
138
600
    tramp.func = leaf;
139
600
    if (sa != NULL)
140
600
        sa_doall(sa, NULL, &trampoline, &tramp);
141
600
}
142
143
void ossl_sa_doall_arg(const OPENSSL_SA *sa,
144
                          void (*leaf)(ossl_uintmax_t, void *, void *),
145
                          void *arg)
146
1.14M
{
147
1.14M
    if (sa != NULL)
148
1.14M
        sa_doall(sa, NULL, leaf, arg);
149
1.14M
}
150
151
size_t ossl_sa_num(const OPENSSL_SA *sa)
152
984k
{
153
984k
    return sa == NULL ? 0 : sa->nelem;
154
984k
}
155
156
void *ossl_sa_get(const OPENSSL_SA *sa, ossl_uintmax_t n)
157
530M
{
158
530M
    int level;
159
530M
    void **p, *r = NULL;
160
161
530M
    if (sa == NULL || sa->nelem == 0)
162
5.29k
        return NULL;
163
164
530M
    if (n <= sa->top) {
165
530M
        p = sa->nodes;
166
626M
        for (level = sa->levels - 1; p != NULL && level > 0; level--)
167
95.5M
            p = (void **)p[(n >> (OPENSSL_SA_BLOCK_BITS * level))
168
95.5M
                           & SA_BLOCK_MASK];
169
530M
        r = p == NULL ? NULL : p[n & SA_BLOCK_MASK];
170
530M
    }
171
530M
    return r;
172
530M
}
173
174
static ossl_inline void **alloc_node(void)
175
148k
{
176
148k
    return OPENSSL_zalloc(SA_BLOCK_MAX * sizeof(void *));
177
148k
}
178
179
int ossl_sa_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)
180
44.1k
{
181
44.1k
    int i, level = 1;
182
44.1k
    ossl_uintmax_t n = posn;
183
44.1k
    void **p;
184
185
44.1k
    if (sa == NULL)
186
0
        return 0;
187
188
206k
    for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)
189
206k
        if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
190
44.1k
            break;
191
192
103k
    for (;sa->levels < level; sa->levels++) {
193
59.2k
        p = alloc_node();
194
59.2k
        if (p == NULL)
195
0
            return 0;
196
59.2k
        p[0] = sa->nodes;
197
59.2k
        sa->nodes = p;
198
59.2k
    }
199
44.1k
    if (sa->top < posn)
200
15.3k
        sa->top = posn;
201
202
44.1k
    p = sa->nodes;
203
213k
    for (level = sa->levels - 1; level > 0; level--) {
204
168k
        i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;
205
168k
        if (p[i] == NULL && (p[i] = alloc_node()) == NULL)
206
0
            return 0;
207
168k
        p = p[i];
208
168k
    }
209
44.1k
    p += posn & SA_BLOCK_MASK;
210
44.1k
    if (val == NULL && *p != NULL)
211
18.3k
        sa->nelem--;
212
25.7k
    else if (val != NULL && *p == NULL)
213
25.5k
        sa->nelem++;
214
44.1k
    *p = val;
215
44.1k
    return 1;
216
44.1k
}