Coverage Report

Created: 2025-08-28 07:07

/src/openssl34/crypto/hashtable/hashtable.c
Line
Count
Source (jump to first uncovered line)
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
 *
11
 * Notes On hash table design and layout
12
 * This hashtable uses a hopscotch algorithm to do indexing.  The data structure
13
 * looks as follows:
14
 *
15
 *   hash          +--------------+
16
 *   value+------->+ HT_VALUE     |
17
 *      +          +--------------+
18
 *  +-------+
19
 *  |       |
20
 *  +---------------------------------------------------------+
21
 *  |       |       |       |       |                         |
22
 *  | entry | entry | entry | entry |                         |
23
 *  |       |       |       |       |                         |
24
 *  +---------------------------------------------------------+
25
 *  |                               |                         |
26
 *  |                               |                         |
27
 *  +---------------------------------------------------------+
28
 *  |              +                             +            +
29
 *  |        neighborhood[0]               neighborhood[1]    |
30
 *  |                                                         |
31
 *  |                                                         |
32
 *  +---------------------------------------------------------+
33
 *                              |
34
 *                              +
35
 *                         neighborhoods
36
 *
37
 * On lookup/insert/delete, the items key is hashed to a 64 bit value
38
 * and the result is masked to provide an index into the neighborhoods
39
 * table.  Once a neighborhood is determined, an in-order search is done
40
 * of the elements in the neighborhood indexes entries for a matching hash
41
 * value, if found, the corresponding HT_VALUE is used for the respective
42
 * operation.  The number of entries in a neighborhood is determined at build
43
 * time based on the cacheline size of the target CPU.  The intent is for a
44
 * neighborhood to have all entries in the neighborhood fit into a single cache
45
 * line to speed up lookups.  If all entries in a neighborhood are in use at the
46
 * time of an insert, the table is expanded and rehashed.
47
 *
48
 * Lockless reads hash table is based on the same design but does not
49
 * allow growing and deletion. Thus subsequent neighborhoods are always
50
 * searched for a match until an empty entry is found.
51
 */
52
53
#include <string.h>
54
#include <internal/rcu.h>
55
#include <internal/hashtable.h>
56
#include <openssl/rand.h>
57
58
/*
59
 * gcc defines __SANITIZE_THREAD__
60
 * but clang uses the feature attributes api
61
 * map the latter to the former
62
 */
63
#if defined(__clang__) && defined(__has_feature)
64
# if __has_feature(thread_sanitizer)
65
#  define __SANITIZE_THREADS__
66
# endif
67
#endif
68
69
#ifdef __SANITIZE_THREADS__
70
# include <sanitizer/tsan_interface.h>
71
#endif
72
73
#include "internal/numbers.h"
74
/*
75
 * When we do a lookup/insert/delete, there is a high likelihood
76
 * that we will iterate over at least part of the neighborhood list
77
 * As such, because we design a neighborhood entry to fit into a single
78
 * cache line it is advantageous, when supported to fetch the entire
79
 * structure for faster lookups
80
 */
81
#if defined(__GNUC__) || defined(__CLANG__)
82
23.1M
# define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries)
83
41.7M
# define PREFETCH(x) __builtin_prefetch(x)
84
#else
85
# define PREFETCH_NEIGHBORHOOD(x)
86
# define PREFETCH(x)
87
#endif
88
89
static ossl_unused uint64_t fnv1a_hash(uint8_t *key, size_t len)
90
6.01M
{
91
6.01M
    uint64_t hash = 0xcbf29ce484222325ULL;
92
6.01M
    size_t i;
93
94
391M
    for (i = 0; i < len; i++) {
95
385M
        hash ^= key[i];
96
385M
        hash *= 0x00000100000001B3ULL;
97
385M
    }
98
6.01M
    return hash;
99
6.01M
}
100
101
/*
102
 * Define our neighborhood list length
103
 * Note: It should always be a power of 2
104
 */
105
477
#define DEFAULT_NEIGH_LEN_LOG 4
106
477
#define DEFAULT_NEIGH_LEN (1 << DEFAULT_NEIGH_LEN_LOG)
107
108
/*
109
 * For now assume cache line size is 64 bytes
110
 */
111
29.9M
#define CACHE_LINE_BYTES 64
112
#define CACHE_LINE_ALIGNMENT CACHE_LINE_BYTES
113
114
29.9M
#define NEIGHBORHOOD_LEN (CACHE_LINE_BYTES / sizeof(struct ht_neighborhood_entry_st))
115
/*
116
 * Defines our chains of values
117
 */
118
struct ht_internal_value_st {
119
    HT_VALUE value;
120
    HT *ht;
121
};
122
123
struct ht_neighborhood_entry_st {
124
    uint64_t hash;
125
    struct ht_internal_value_st *value;
126
};
127
128
struct ht_neighborhood_st {
129
    struct ht_neighborhood_entry_st entries[NEIGHBORHOOD_LEN];
130
};
131
132
/*
133
 * Updates to data in this struct
134
 * require an rcu sync after modification
135
 * prior to free
136
 */
137
struct ht_mutable_data_st {
138
    struct ht_neighborhood_st *neighborhoods;
139
    void *neighborhood_ptr_to_free;
140
    uint64_t neighborhood_mask;
141
};
142
143
/*
144
 * Private data may be updated on the write
145
 * side only, and so do not require rcu sync
146
 */
147
struct ht_write_private_data_st {
148
    size_t neighborhood_len;
149
    size_t value_count;
150
    int need_sync;
151
};
152
153
struct ht_internal_st {
154
    HT_CONFIG config;
155
    CRYPTO_RCU_LOCK *lock;
156
    CRYPTO_RWLOCK *atomic_lock;
157
    struct ht_mutable_data_st *md;
158
    struct ht_write_private_data_st wpd;
159
};
160
161
static void free_value(struct ht_internal_value_st *v);
162
163
static struct ht_neighborhood_st *alloc_new_neighborhood_list(size_t len,
164
                                                              void **freeptr)
165
415
{
166
415
    struct ht_neighborhood_st *ret;
167
168
415
    ret = OPENSSL_aligned_alloc(sizeof(struct ht_neighborhood_st) * len,
169
415
                                CACHE_LINE_BYTES, freeptr);
170
171
    /* fall back to regular malloc */
172
415
    if (ret == NULL) {
173
0
        ret = *freeptr = OPENSSL_malloc(sizeof(struct ht_neighborhood_st) * len);
174
0
        if (ret == NULL)
175
0
            return NULL;
176
0
    }
177
415
    memset(ret, 0, sizeof(struct ht_neighborhood_st) * len);
178
415
    return ret;
179
415
}
180
181
static void internal_free_nop(HT_VALUE *v)
182
24.5k
{
183
24.5k
    return;
184
24.5k
}
185
186
HT *ossl_ht_new(const HT_CONFIG *conf)
187
242
{
188
242
    HT *new = OPENSSL_zalloc(sizeof(*new));
189
190
242
    if (new == NULL)
191
0
        return NULL;
192
193
242
    new->atomic_lock = CRYPTO_THREAD_lock_new();
194
242
    if (new->atomic_lock == NULL)
195
0
        goto err;
196
197
242
    memcpy(&new->config, conf, sizeof(*conf));
198
199
242
    if (new->config.init_neighborhoods != 0) {
200
236
        new->wpd.neighborhood_len = new->config.init_neighborhoods;
201
        /* round up to the next power of 2 */
202
236
        new->wpd.neighborhood_len--;
203
236
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 1;
204
236
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 2;
205
236
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 4;
206
236
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 8;
207
236
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 16;
208
236
        new->wpd.neighborhood_len++;
209
236
    } else {
210
6
        new->wpd.neighborhood_len = DEFAULT_NEIGH_LEN;
211
6
    }
212
213
242
    if (new->config.ht_free_fn == NULL)
214
236
        new->config.ht_free_fn = internal_free_nop;
215
216
242
    new->md = OPENSSL_zalloc(sizeof(*new->md));
217
242
    if (new->md == NULL)
218
0
        goto err;
219
220
242
    new->md->neighborhoods =
221
242
        alloc_new_neighborhood_list(new->wpd.neighborhood_len,
222
242
                                    &new->md->neighborhood_ptr_to_free);
223
242
    if (new->md->neighborhoods == NULL)
224
0
        goto err;
225
242
    new->md->neighborhood_mask = new->wpd.neighborhood_len - 1;
226
227
242
    new->lock = ossl_rcu_lock_new(1, conf->ctx);
228
242
    if (new->lock == NULL)
229
0
        goto err;
230
231
242
    if (new->config.ht_hash_fn == NULL)
232
242
        new->config.ht_hash_fn = fnv1a_hash;
233
234
242
    return new;
235
236
0
err:
237
0
    CRYPTO_THREAD_lock_free(new->atomic_lock);
238
0
    ossl_rcu_lock_free(new->lock);
239
0
    if (new->md != NULL)
240
0
        OPENSSL_free(new->md->neighborhood_ptr_to_free);
241
0
    OPENSSL_free(new->md);
242
0
    OPENSSL_free(new);
243
0
    return NULL;
244
242
}
245
246
void ossl_ht_read_lock(HT *htable)
247
39
{
248
39
    ossl_rcu_read_lock(htable->lock);
249
39
}
250
251
void ossl_ht_read_unlock(HT *htable)
252
57
{
253
57
    ossl_rcu_read_unlock(htable->lock);
254
57
}
255
256
void ossl_ht_write_lock(HT *htable)
257
278
{
258
278
    ossl_rcu_write_lock(htable->lock);
259
278
    htable->wpd.need_sync = 0;
260
278
}
261
262
void ossl_ht_write_unlock(HT *htable)
263
278
{
264
278
    int need_sync = htable->wpd.need_sync;
265
266
278
    htable->wpd.need_sync = 0;
267
278
    ossl_rcu_write_unlock(htable->lock);
268
278
    if (need_sync)
269
179
        ossl_synchronize_rcu(htable->lock);
270
278
}
271
272
static void free_oldmd(void *arg)
273
157
{
274
157
    struct ht_mutable_data_st *oldmd = arg;
275
157
    size_t i, j;
276
157
    size_t neighborhood_len = (size_t)oldmd->neighborhood_mask + 1;
277
157
    struct ht_internal_value_st *v;
278
279
79.3k
    for (i = 0; i < neighborhood_len; i++) {
280
79.2k
        PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[i + 1]);
281
396k
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
282
316k
            if (oldmd->neighborhoods[i].entries[j].value != NULL) {
283
24.5k
                v = oldmd->neighborhoods[i].entries[j].value;
284
24.5k
                v->ht->config.ht_free_fn((HT_VALUE *)v);
285
24.5k
                free_value(v);
286
24.5k
            }
287
316k
        }
288
79.2k
    }
289
290
157
    OPENSSL_free(oldmd->neighborhood_ptr_to_free);
291
157
    OPENSSL_free(oldmd);
292
157
}
293
294
static int ossl_ht_flush_internal(HT *h)
295
157
{
296
157
    struct ht_mutable_data_st *newmd = NULL;
297
157
    struct ht_mutable_data_st *oldmd = NULL;
298
299
157
    newmd = OPENSSL_zalloc(sizeof(*newmd));
300
157
    if (newmd == NULL)
301
0
        return 0;
302
303
157
    newmd->neighborhoods = alloc_new_neighborhood_list(DEFAULT_NEIGH_LEN,
304
157
                                                       &newmd->neighborhood_ptr_to_free);
305
157
    if (newmd->neighborhoods == NULL) {
306
0
        OPENSSL_free(newmd);
307
0
        return 0;
308
0
    }
309
310
157
    newmd->neighborhood_mask = DEFAULT_NEIGH_LEN - 1;
311
312
    /* Swap the old and new mutable data sets */
313
157
    oldmd = ossl_rcu_deref(&h->md);
314
157
    ossl_rcu_assign_ptr(&h->md, &newmd);
315
316
    /* Set the number of entries to 0 */
317
157
    h->wpd.value_count = 0;
318
157
    h->wpd.neighborhood_len = DEFAULT_NEIGH_LEN;
319
320
157
    ossl_rcu_call(h->lock, free_oldmd, oldmd);
321
157
    h->wpd.need_sync = 1;
322
157
    return 1;
323
157
}
324
325
int ossl_ht_flush(HT *h)
326
3
{
327
3
    return ossl_ht_flush_internal(h);
328
3
}
329
330
void ossl_ht_free(HT *h)
331
154
{
332
154
    if (h == NULL)
333
0
        return;
334
335
154
    ossl_ht_write_lock(h);
336
154
    ossl_ht_flush_internal(h);
337
154
    ossl_ht_write_unlock(h);
338
    /* Freeing the lock does a final sync for us */
339
154
    CRYPTO_THREAD_lock_free(h->atomic_lock);
340
154
    ossl_rcu_lock_free(h->lock);
341
154
    OPENSSL_free(h->md->neighborhood_ptr_to_free);
342
154
    OPENSSL_free(h->md);
343
154
    OPENSSL_free(h);
344
154
    return;
345
154
}
346
347
size_t ossl_ht_count(HT *h)
348
0
{
349
0
    size_t count;
350
351
0
    count = h->wpd.value_count;
352
0
    return count;
353
0
}
354
355
void ossl_ht_foreach_until(HT *h, int (*cb)(HT_VALUE *obj, void *arg),
356
                           void *arg)
357
50
{
358
50
    size_t i, j;
359
50
    struct ht_mutable_data_st *md;
360
361
50
    md = ossl_rcu_deref(&h->md);
362
3.95k
    for (i = 0; i < md->neighborhood_mask + 1; i++) {
363
3.90k
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]);
364
19.5k
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
365
15.6k
            if (md->neighborhoods[i].entries[j].value != NULL) {
366
256
                if (!cb((HT_VALUE *)md->neighborhoods[i].entries[j].value, arg))
367
4
                    goto out;
368
256
            }
369
15.6k
        }
370
3.90k
    }
371
50
out:
372
50
    return;
373
50
}
374
375
HT_VALUE_LIST *ossl_ht_filter(HT *h, size_t max_len,
376
                                     int (*filter)(HT_VALUE *obj, void *arg),
377
                                     void *arg)
378
38
{
379
38
    struct ht_mutable_data_st *md;
380
38
    HT_VALUE_LIST *list = OPENSSL_zalloc(sizeof(HT_VALUE_LIST)
381
38
                                         + (sizeof(HT_VALUE *) * max_len));
382
38
    size_t i, j;
383
38
    struct ht_internal_value_st *v;
384
385
38
    if (list == NULL)
386
0
        return NULL;
387
388
    /*
389
     * The list array lives just beyond the end of
390
     * the struct
391
     */
392
38
    list->list = (HT_VALUE **)(list + 1);
393
394
38
    md = ossl_rcu_deref(&h->md);
395
1.53k
    for (i = 0; i < md->neighborhood_mask + 1; i++) {
396
1.50k
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[i+1]);
397
7.49k
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
398
5.99k
            v = md->neighborhoods[i].entries[j].value;
399
5.99k
            if (v != NULL && filter((HT_VALUE *)v, arg)) {
400
2
                list->list[list->list_len++] = (HT_VALUE *)v;
401
2
                if (list->list_len == max_len)
402
2
                    goto out;
403
2
            }
404
5.99k
        }
405
1.50k
    }
406
38
out:
407
38
    return list;
408
38
}
409
410
void ossl_ht_value_list_free(HT_VALUE_LIST *list)
411
38
{
412
38
    OPENSSL_free(list);
413
38
}
414
415
static int compare_hash(uint64_t hash1, uint64_t hash2)
416
27.3M
{
417
27.3M
    return (hash1 == hash2);
418
27.3M
}
419
420
static void free_old_neigh_table(void *arg)
421
16
{
422
16
    struct ht_mutable_data_st *oldmd = arg;
423
424
16
    OPENSSL_free(oldmd->neighborhood_ptr_to_free);
425
16
    OPENSSL_free(oldmd);
426
16
}
427
428
/*
429
 * Increase hash table bucket list
430
 * must be called with write_lock held
431
 */
432
static int grow_hashtable(HT *h, size_t oldsize)
433
16
{
434
16
    struct ht_mutable_data_st *newmd;
435
16
    struct ht_mutable_data_st *oldmd = ossl_rcu_deref(&h->md);
436
16
    int rc = 0;
437
16
    uint64_t oldi, oldj, newi, newj;
438
16
    uint64_t oldhash;
439
16
    struct ht_internal_value_st *oldv;
440
16
    int rehashed;
441
16
    size_t newsize = oldsize * 2;
442
443
16
    if (h->config.lockless_reads)
444
0
        goto out;
445
446
16
    if ((newmd = OPENSSL_zalloc(sizeof(*newmd))) == NULL)
447
0
        goto out;
448
449
    /* bucket list is always a power of 2 */
450
16
    newmd->neighborhoods = alloc_new_neighborhood_list(oldsize * 2,
451
16
                                                       &newmd->neighborhood_ptr_to_free);
452
16
    if (newmd->neighborhoods == NULL)
453
0
        goto out_free;
454
455
    /* being a power of 2 makes for easy mask computation */
456
16
    newmd->neighborhood_mask = (newsize - 1);
457
458
    /*
459
     * Now we need to start rehashing entries
460
     * Note we don't need to use atomics here as the new
461
     * mutable data hasn't been published
462
     */
463
896
    for (oldi = 0; oldi < h->wpd.neighborhood_len; oldi++) {
464
880
        PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[oldi + 1]);
465
4.40k
        for (oldj = 0; oldj < NEIGHBORHOOD_LEN; oldj++) {
466
3.52k
            oldv = oldmd->neighborhoods[oldi].entries[oldj].value;
467
3.52k
            if (oldv == NULL)
468
3.40k
                continue;
469
111
            oldhash = oldmd->neighborhoods[oldi].entries[oldj].hash;
470
111
            newi = oldhash & newmd->neighborhood_mask;
471
111
            rehashed = 0;
472
190
            for (newj = 0; newj < NEIGHBORHOOD_LEN; newj++) {
473
190
                if (newmd->neighborhoods[newi].entries[newj].value == NULL) {
474
111
                    newmd->neighborhoods[newi].entries[newj].value = oldv;
475
111
                    newmd->neighborhoods[newi].entries[newj].hash = oldhash;
476
111
                    rehashed = 1;
477
111
                    break;
478
111
                }
479
190
            }
480
111
            if (rehashed == 0) {
481
                /* we ran out of space in a neighborhood, grow again */
482
0
                OPENSSL_free(newmd->neighborhoods);
483
0
                OPENSSL_free(newmd);
484
0
                return grow_hashtable(h, newsize);
485
0
            }
486
111
        }
487
880
    }
488
    /*
489
     * Now that our entries are all hashed into the new bucket list
490
     * update our bucket_len and target_max_load
491
     */
492
16
    h->wpd.neighborhood_len = newsize;
493
494
    /*
495
     * Now we replace the old mutable data with the new
496
     */
497
16
    ossl_rcu_assign_ptr(&h->md, &newmd);
498
16
    ossl_rcu_call(h->lock, free_old_neigh_table, oldmd);
499
16
    h->wpd.need_sync = 1;
500
    /*
501
     * And we're done
502
     */
503
16
    rc = 1;
504
505
16
out:
506
16
    return rc;
507
0
out_free:
508
0
    OPENSSL_free(newmd->neighborhoods);
509
0
    OPENSSL_free(newmd);
510
0
    goto out;
511
16
}
512
513
static void free_old_ht_value(void *arg)
514
4
{
515
4
    HT_VALUE *h = (HT_VALUE *)arg;
516
517
    /*
518
     * Note, this is only called on replacement,
519
     * the caller is responsible for freeing the
520
     * held data, we just need to free the wrapping
521
     * struct here
522
     */
523
4
    OPENSSL_free(h);
524
4
}
525
526
static ossl_inline int match_key(HT_KEY *a, HT_KEY *b)
527
20.8M
{
528
    /*
529
     * keys match if they are both present, the same size
530
     * and compare equal in memory
531
     */
532
20.8M
    PREFETCH(a->keybuf);
533
20.8M
    PREFETCH(b->keybuf);
534
20.8M
    if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize)
535
20.8M
        return !memcmp(a->keybuf, b->keybuf, a->keysize);
536
537
0
    return 1;
538
20.8M
}
539
540
static int ossl_ht_insert_locked(HT *h, uint64_t hash,
541
                                 struct ht_internal_value_st *newval,
542
                                 HT_VALUE **olddata)
543
28.2k
{
544
28.2k
    struct ht_mutable_data_st *md = h->md;
545
28.2k
    uint64_t neigh_idx_start = hash & md->neighborhood_mask;
546
28.2k
    uint64_t neigh_idx = neigh_idx_start;
547
28.2k
    size_t j;
548
28.2k
    uint64_t ihash;
549
28.2k
    HT_VALUE *ival;
550
28.2k
    size_t empty_idx = SIZE_MAX;
551
28.2k
    int lockless_reads = h->config.lockless_reads;
552
553
28.4k
    do {
554
28.4k
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]);
555
556
43.0k
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
557
42.7k
            ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value);
558
42.7k
            if (ival == NULL) {
559
28.3k
                empty_idx = j;
560
                /* lockless_reads implies no deletion, we can break out */
561
28.3k
                if (lockless_reads)
562
28.1k
                    goto not_found;
563
195
                continue;
564
28.3k
            }
565
14.4k
            if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash,
566
14.4k
                                    &ihash, h->atomic_lock))
567
0
                return 0;
568
14.4k
            if (compare_hash(hash, ihash) && match_key(&newval->value.key,
569
11
                                                       &ival->key)) {
570
11
                if (olddata == NULL) {
571
                    /* This would insert a duplicate -> fail */
572
7
                    return 0;
573
7
                }
574
                /* Do a replacement */
575
4
                if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash,
576
4
                                         hash, h->atomic_lock))
577
0
                    return 0;
578
4
                *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value;
579
4
                ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[j].value,
580
4
                                    &newval);
581
4
                ossl_rcu_call(h->lock, free_old_ht_value, *olddata);
582
4
                h->wpd.need_sync = 1;
583
4
                return 1;
584
4
            }
585
14.4k
        }
586
348
        if (!lockless_reads)
587
84
            break;
588
        /* Continue search in subsequent neighborhoods */
589
264
        neigh_idx = (neigh_idx + 1) & md->neighborhood_mask;
590
264
    } while (neigh_idx != neigh_idx_start);
591
592
28.2k
 not_found:
593
    /* If we get to here, its just an insert */
594
28.2k
    if (empty_idx == SIZE_MAX)
595
16
        return -1; /* out of space */
596
28.1k
    if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[empty_idx].hash,
597
28.1k
                             hash, h->atomic_lock))
598
0
        return 0;
599
28.1k
    h->wpd.value_count++;
600
28.1k
    ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[empty_idx].value,
601
28.1k
                        &newval);
602
28.1k
    return 1;
603
28.1k
}
604
605
static struct ht_internal_value_st *alloc_new_value(HT *h, HT_KEY *key,
606
                                                    void *data,
607
                                                    uintptr_t *type)
608
28.2k
{
609
28.2k
    struct ht_internal_value_st *tmp;
610
28.2k
    size_t nvsize = sizeof(*tmp);
611
612
28.2k
    if (h->config.collision_check == 1)
613
28.2k
        nvsize += key->keysize;
614
615
28.2k
    tmp = OPENSSL_malloc(nvsize);
616
617
28.2k
    if (tmp == NULL)
618
0
        return NULL;
619
620
28.2k
    tmp->ht = h;
621
28.2k
    tmp->value.value = data;
622
28.2k
    tmp->value.type_id = type;
623
28.2k
    tmp->value.key.keybuf = NULL;
624
28.2k
    if (h->config.collision_check) {
625
28.2k
        tmp->value.key.keybuf = (uint8_t *)(tmp + 1);
626
28.2k
        tmp->value.key.keysize = key->keysize;
627
28.2k
        memcpy(tmp->value.key.keybuf, key->keybuf, key->keysize);
628
28.2k
    }
629
630
631
28.2k
    return tmp;
632
28.2k
}
633
634
static void free_value(struct ht_internal_value_st *v)
635
24.5k
{
636
24.5k
    OPENSSL_free(v);
637
24.5k
}
638
639
int ossl_ht_insert(HT *h, HT_KEY *key, HT_VALUE *data, HT_VALUE **olddata)
640
28.2k
{
641
28.2k
    struct ht_internal_value_st *newval = NULL;
642
28.2k
    uint64_t hash;
643
28.2k
    int rc = 0;
644
28.2k
    int i;
645
646
28.2k
    if (data->value == NULL)
647
0
        goto out;
648
649
28.2k
    newval = alloc_new_value(h, key, data->value, data->type_id);
650
28.2k
    if (newval == NULL)
651
0
        goto out;
652
653
    /*
654
     * we have to take our lock here to prevent other changes
655
     * to the bucket list
656
     */
657
28.2k
    hash = h->config.ht_hash_fn(key->keybuf, key->keysize);
658
659
28.2k
    for (i = 0;
660
28.2k
         (rc = ossl_ht_insert_locked(h, hash, newval, olddata)) == -1
661
28.2k
         && i < 4;
662
28.2k
         ++i)
663
16
        if (!grow_hashtable(h, h->wpd.neighborhood_len)) {
664
0
            rc = -1;
665
0
            break;
666
0
        }
667
668
28.2k
    if (rc <= 0)
669
7
        free_value(newval);
670
671
28.2k
out:
672
28.2k
    return rc;
673
28.2k
}
674
675
HT_VALUE *ossl_ht_get(HT *h, HT_KEY *key)
676
23.0M
{
677
23.0M
    struct ht_mutable_data_st *md;
678
23.0M
    uint64_t hash;
679
23.0M
    uint64_t neigh_idx_start;
680
23.0M
    uint64_t neigh_idx;
681
23.0M
    struct ht_internal_value_st *ival = NULL;
682
23.0M
    size_t j;
683
23.0M
    uint64_t ehash;
684
23.0M
    int lockless_reads = h->config.lockless_reads;
685
686
23.0M
    hash = h->config.ht_hash_fn(key->keybuf, key->keysize);
687
688
23.0M
    md = ossl_rcu_deref(&h->md);
689
23.0M
    neigh_idx = neigh_idx_start = hash & md->neighborhood_mask;
690
23.0M
    do {
691
23.0M
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]);
692
29.4M
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
693
29.4M
            ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value);
694
29.4M
            if (ival == NULL) {
695
2.14M
                if (lockless_reads)
696
                    /* lockless_reads implies no deletion, we can break out */
697
2.14M
                    return NULL;
698
127
                continue;
699
2.14M
            }
700
27.3M
            if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash,
701
27.3M
                                    &ehash, h->atomic_lock))
702
0
                return NULL;
703
27.3M
            if (compare_hash(hash, ehash) && match_key(&ival->value.key, key))
704
20.8M
                return (HT_VALUE *)ival;
705
27.3M
        }
706
4.73k
        if (!lockless_reads)
707
52
            break;
708
        /* Continue search in subsequent neighborhoods */
709
4.68k
        neigh_idx = (neigh_idx + 1) & md->neighborhood_mask;
710
4.68k
    } while (neigh_idx != neigh_idx_start);
711
712
52
    return NULL;
713
23.0M
}
714
715
static void free_old_entry(void *arg)
716
10
{
717
10
    struct ht_internal_value_st *v = arg;
718
719
10
    v->ht->config.ht_free_fn((HT_VALUE *)v);
720
10
    free_value(v);
721
10
}
722
723
int ossl_ht_delete(HT *h, HT_KEY *key)
724
42
{
725
42
    uint64_t hash;
726
42
    uint64_t neigh_idx;
727
42
    size_t j;
728
42
    struct ht_internal_value_st *v = NULL;
729
42
    HT_VALUE *nv = NULL;
730
42
    int rc = 0;
731
732
42
    if (h->config.lockless_reads)
733
0
        return 0;
734
735
42
    hash = h->config.ht_hash_fn(key->keybuf, key->keysize);
736
737
42
    neigh_idx = hash & h->md->neighborhood_mask;
738
42
    PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]);
739
182
    for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
740
150
        v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value;
741
150
        if (v == NULL)
742
84
            continue;
743
66
        if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash)
744
66
            && match_key(key, &v->value.key)) {
745
10
            if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash,
746
10
                                     0, h->atomic_lock))
747
0
                break;
748
10
            h->wpd.value_count--;
749
10
            ossl_rcu_assign_ptr(&h->md->neighborhoods[neigh_idx].entries[j].value,
750
10
                                &nv);
751
10
            rc = 1;
752
10
            break;
753
10
        }
754
66
    }
755
42
    if (rc == 1) {
756
10
        ossl_rcu_call(h->lock, free_old_entry, v);
757
10
        h->wpd.need_sync = 1;
758
10
    }
759
42
    return rc;
760
42
}