Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/include/internal/hashtable.h
Line
Count
Source
1
/*
2
 * Copyright 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
#ifndef OPENSSL_HASHTABLE_H
11
#define OPENSSL_HASHTABLE_H
12
#pragma once
13
14
#include <stddef.h>
15
#include <stdint.h>
16
#include <openssl/e_os2.h>
17
#include <internal/rcu.h>
18
#include "crypto/context.h"
19
20
typedef struct ht_internal_st HT;
21
22
/*
23
 * Represents a key to a hashtable
24
 */
25
typedef struct ht_key_header_st {
26
    size_t keysize;
27
    uint8_t *keybuf;
28
} HT_KEY;
29
30
/*
31
 * Represents a value in the hash table
32
 */
33
typedef struct ht_value_st {
34
    void *value;
35
    uintptr_t *type_id;
36
    HT_KEY key;
37
} HT_VALUE;
38
39
/*
40
 * Represents a list of values filtered from a hash table
41
 */
42
typedef struct ht_value_list_st {
43
    size_t list_len;
44
    HT_VALUE **list;
45
} HT_VALUE_LIST;
46
47
/*
48
 * Hashtable configuration
49
 */
50
typedef struct ht_config_st {
51
    OSSL_LIB_CTX *ctx;
52
    void (*ht_free_fn)(HT_VALUE *obj);
53
    uint64_t (*ht_hash_fn)(uint8_t *key, size_t keylen);
54
    size_t init_neighborhoods;
55
    uint32_t collision_check;
56
    uint32_t lockless_reads;
57
} HT_CONFIG;
58
59
/*
60
 * Hashtable key rules
61
 * Any struct can be used to formulate a hash table key, as long as the
62
 * following rules
63
 * 1) The first element of the struct defining the key must be an HT_KEY
64
 * 2) All struct elements must have a compile time defined length
65
 * 3) Pointers can be used, but the value of the pointer, rather than
66
 *    the contents of the address it points to will be used to compute
67
 *    the hash
68
 * The key definition macros will assist with enforcing these rules
69
 */
70
71
/*
72
 * Starts the definition of a hash table key
73
 */
74
#define HT_START_KEY_DEFN(keyname) \
75
    typedef struct keyname##_st {  \
76
        HT_KEY key_header;         \
77
        struct {
78
79
/*
80
 * Ends a hash table key definitions
81
 */
82
#define HT_END_KEY_DEFN(keyname) \
83
    }                            \
84
    keyfields;                   \
85
    }                            \
86
    keyname;
87
88
/*
89
 * Defines a field in a hash table key
90
 */
91
#define HT_DEF_KEY_FIELD(name, type) type name;
92
93
/*
94
 * convenience macro to define a static char
95
 * array field in a hash table key
96
 */
97
#define HT_DEF_KEY_FIELD_CHAR_ARRAY(name, size) \
98
    HT_DEF_KEY_FIELD(name[size], char)
99
100
/*
101
 * Defines a uint8_t (blob) field in a hash table key
102
 */
103
#define HT_DEF_KEY_FIELD_UINT8T_ARRAY(name, size) \
104
    HT_DEF_KEY_FIELD(name[size], uint8_t)
105
106
/*
107
 * Initializes a key
108
 */
109
#define HT_INIT_KEY(key)                                                \
110
32.6M
    do {                                                                \
111
32.6M
        memset((key), 0, sizeof(*(key)));                               \
112
32.6M
        (key)->key_header.keysize = (sizeof(*(key)) - sizeof(HT_KEY));  \
113
32.6M
        (key)->key_header.keybuf = (((uint8_t *)key) + sizeof(HT_KEY)); \
114
32.6M
    } while (0)
115
116
/*
117
 * Resets a hash table key to a known state
118
 */
119
101
#define HT_KEY_RESET(key) memset((key)->key_header.keybuf, 0, (key)->key_header.keysize)
120
121
/*
122
 * Sets a scalar field in a hash table key
123
 */
124
101
#define HT_SET_KEY_FIELD(key, member, value) (key)->keyfields.member = value;
125
126
/*
127
 * Sets a string field in a hash table key, preserving
128
 * null terminator
129
 */
130
#define HT_SET_KEY_STRING(key, member, value)                                             \
131
    do {                                                                                  \
132
        if ((value) != NULL)                                                              \
133
            strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \
134
    } while (0)
135
136
/*
137
 * This is the same as HT_SET_KEY_STRING, except that it uses
138
 * ossl_ht_strcase to make the value being passed case insensitive
139
 * This is useful for instances in which we want upper and lower case
140
 * key value to hash to the same entry
141
 */
142
#define HT_SET_KEY_STRING_CASE(key, member, value)                                            \
143
32.6M
    do {                                                                                      \
144
32.6M
        ossl_ht_strcase((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \
145
32.6M
    } while (0)
146
147
/*
148
 * Sets a uint8_t (blob) field in a hash table key
149
 */
150
#define HT_SET_KEY_BLOB(key, member, value, len)         \
151
    do {                                                 \
152
        if (value != NULL)                               \
153
            memcpy((key)->keyfields.member, value, len); \
154
    } while (0)
155
156
/*
157
 * Converts a defined key type to an HT_KEY
158
 */
159
32.6M
#define TO_HT_KEY(key) &(key)->key_header
160
161
/*
162
 * Converts an HT_KEY back to its defined
163
 * type
164
 */
165
#define FROM_HT_KEY(key, type) (type)(key)
166
167
/*
168
 * Implements the following type safe operations for a hash table
169
 * ossl_ht_NAME_TYPE_insert - insert a value to a hash table of type TYPE
170
 * ossl_ht_NAME_TYPE_get - gets a value of a specific type from the hash table
171
 * ossl_ht_NAME_TYPE_from_value - converts an HT_VALUE to its type
172
 * ossl_ht_NAME_TYPE_to_value - converts a TYPE to an HT_VALUE
173
 * ossl_ht_NAME_TYPE_type - boolean to detect if a value is of TYPE
174
 */
175
#define IMPLEMENT_HT_VALUE_TYPE_FNS(vtype, name, pfx)                          \
176
    static uintptr_t name##_##vtype##_id = 0;                                  \
177
    pfx ossl_unused int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key,  \
178
        vtype *data,                                                           \
179
        vtype **olddata)                                                       \
180
103
    {                                                                          \
181
103
        HT_VALUE inval;                                                        \
182
103
        HT_VALUE *oval = NULL;                                                 \
183
103
        int rc;                                                                \
184
103
                                                                               \
185
103
        inval.value = data;                                                    \
186
103
        inval.type_id = &name##_##vtype##_id;                                  \
187
103
        rc = ossl_ht_insert(h, key, &inval, olddata == NULL ? NULL : &oval);   \
188
103
        if (oval != NULL)                                                      \
189
103
            *olddata = (vtype *)oval->value;                                   \
190
103
        return rc;                                                             \
191
103
    }                                                                          \
192
                                                                               \
193
    pfx ossl_unused vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v)  \
194
767
    {                                                                          \
195
767
        uintptr_t *expect_type = &name##_##vtype##_id;                         \
196
767
        if (v == NULL)                                                         \
197
767
            return NULL;                                                       \
198
767
        if (v->type_id != expect_type)                                         \
199
767
            return NULL;                                                       \
200
767
        return (vtype *)v->value;                                              \
201
767
    }                                                                          \
202
                                                                               \
203
    pfx ossl_unused vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h,   \
204
        HT_KEY *key,                                                           \
205
        HT_VALUE **v)                                                          \
206
64
    {                                                                          \
207
64
        HT_VALUE *vv;                                                          \
208
64
        vv = ossl_ht_get(h, key);                                              \
209
64
        if (vv == NULL)                                                        \
210
64
            return NULL;                                                       \
211
64
        *v = ossl_rcu_deref(&vv);                                              \
212
6
        return ossl_ht_##name##_##vtype##_from_value(*v);                      \
213
64
    }                                                                          \
214
                                                                               \
215
    pfx ossl_unused HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, \
216
        HT_VALUE *v)                                                           \
217
6
    {                                                                          \
218
6
        v->type_id = &name##_##vtype##_id;                                     \
219
6
        v->value = data;                                                       \
220
6
        return v;                                                              \
221
6
    }                                                                          \
222
                                                                               \
223
    pfx ossl_unused int ossl_ht_##name##_##vtype##_type(HT_VALUE *h)           \
224
6
    {                                                                          \
225
6
        return h->type_id == &name##_##vtype##_id;                             \
226
6
    }
227
228
#define DECLARE_HT_VALUE_TYPE_FNS(vtype, name)                               \
229
    int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, vtype *data,   \
230
        vtype **olddata);                                                    \
231
    vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v);               \
232
    vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h,                 \
233
        HT_KEY *key,                                                         \
234
        HT_VALUE **v);                                                       \
235
    HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, HT_VALUE *v); \
236
    int ossl_ht_##name##_##vtype##_type(HT_VALUE *h);
237
238
/*
239
 * Helper function to construct case insensitive keys
240
 */
241
static void ossl_unused ossl_ht_strcase(char *tgt, const char *src, int len)
242
33.6M
{
243
33.6M
    int i;
244
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
245
    const long int case_adjust = ~0x40;
246
#else
247
33.6M
    const long int case_adjust = ~0x20;
248
33.6M
#endif
249
250
33.6M
    if (src == NULL)
251
0
        return;
252
253
266M
    for (i = 0; src[i] != '\0' && i < len; i++)
254
233M
        tgt[i] = case_adjust & src[i];
255
33.6M
}
core_namemap.c:ossl_ht_strcase
Line
Count
Source
242
33.6M
{
243
33.6M
    int i;
244
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
245
    const long int case_adjust = ~0x40;
246
#else
247
33.6M
    const long int case_adjust = ~0x20;
248
33.6M
#endif
249
250
33.6M
    if (src == NULL)
251
0
        return;
252
253
266M
    for (i = 0; src[i] != '\0' && i < len; i++)
254
233M
        tgt[i] = case_adjust & src[i];
255
33.6M
}
Unexecuted instantiation: hashtable.c:ossl_ht_strcase
256
257
/*
258
 * Create a new hashtable
259
 */
260
HT *ossl_ht_new(const HT_CONFIG *conf);
261
262
/*
263
 * Frees a hash table, potentially freeing all elements
264
 */
265
void ossl_ht_free(HT *htable);
266
267
/*
268
 * Lock the table for reading
269
 */
270
void ossl_ht_read_lock(HT *htable);
271
272
/*
273
 * Lock the table for writing
274
 */
275
void ossl_ht_write_lock(HT *htable);
276
277
/*
278
 * Read unlock
279
 */
280
void ossl_ht_read_unlock(HT *htable);
281
282
/*
283
 * Write unlock
284
 */
285
void ossl_ht_write_unlock(HT *htable);
286
287
/*
288
 * Empties a hash table, potentially freeing all elements
289
 */
290
int ossl_ht_flush(HT *htable);
291
292
/*
293
 * Inserts an element to a hash table, optionally returning
294
 * replaced data to caller
295
 * Returns 1 if the insert was successful, 0 on error
296
 */
297
int ossl_ht_insert(HT *htable, HT_KEY *key, HT_VALUE *data,
298
    HT_VALUE **olddata);
299
300
/*
301
 * Deletes a value from a hash table, based on key
302
 * Returns 1 if the key was removed, 0 if they key was not found
303
 */
304
int ossl_ht_delete(HT *htable, HT_KEY *key);
305
306
/*
307
 * Returns number of elements in the hash table
308
 */
309
size_t ossl_ht_count(HT *htable);
310
311
/*
312
 * Iterates over each element in the table.
313
 * aborts the loop when cb returns 0
314
 * Contents of elements in the list may be modified during
315
 * this traversal, assuming proper thread safety is observed while doing
316
 * so (holding the table write lock is sufficient).  However, elements of the
317
 * table may not be inserted or removed while iterating.
318
 */
319
void ossl_ht_foreach_until(HT *htable, int (*cb)(HT_VALUE *obj, void *arg),
320
    void *arg);
321
/*
322
 * Returns a list of elements in a hash table based on
323
 * filter function return value.  Returns NULL on error,
324
 * or an HT_VALUE_LIST object on success.  Note it is possible
325
 * That a list will be returned with 0 entries, if none were found.
326
 * The zero length list must still be freed via ossl_ht_value_list_free
327
 */
328
HT_VALUE_LIST *ossl_ht_filter(HT *htable, size_t max_len,
329
    int (*filter)(HT_VALUE *obj, void *arg),
330
    void *arg);
331
/*
332
 * Frees the list returned from ossl_ht_filter
333
 */
334
void ossl_ht_value_list_free(HT_VALUE_LIST *list);
335
336
/*
337
 * Fetches a value from the hash table, based
338
 * on key.  Returns NULL if the element was not found.
339
 */
340
HT_VALUE *ossl_ht_get(HT *htable, HT_KEY *key);
341
342
#endif