Coverage Report

Created: 2026-03-11 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/hashtable/hashtable.c
Line
Count
Source
1
/*
2
 * Copyright 2024-2025 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 <internal/hashfunc.h>
57
#include <openssl/rand.h>
58
59
/*
60
 * gcc defines __SANITIZE_THREAD__
61
 * but clang uses the feature attributes api
62
 * map the latter to the former
63
 */
64
#if defined(__clang__) && defined(__has_feature)
65
#if __has_feature(thread_sanitizer)
66
#define __SANITIZE_THREADS__
67
#endif
68
#endif
69
70
#ifdef __SANITIZE_THREADS__
71
#include <sanitizer/tsan_interface.h>
72
#endif
73
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
127k
#define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries)
83
225k
#define PREFETCH(x) __builtin_prefetch(x)
84
#define ALIGN __attribute__((aligned(8)))
85
#else
86
#define PREFETCH_NEIGHBORHOOD(x)
87
#define PREFETCH(x)
88
#define ALIGN
89
#endif
90
91
/*
92
 * Define our neighborhood list length
93
 * Note: It should always be a power of 2
94
 */
95
0
#define DEFAULT_NEIGH_LEN_LOG 4
96
0
#define DEFAULT_NEIGH_LEN (1 << DEFAULT_NEIGH_LEN_LOG)
97
98
/*
99
 * For now assume cache line size is 64 bytes
100
 */
101
302k
#define CACHE_LINE_BYTES 64
102
#define CACHE_LINE_ALIGNMENT CACHE_LINE_BYTES
103
104
302k
#define NEIGHBORHOOD_LEN (CACHE_LINE_BYTES / sizeof(struct ht_neighborhood_entry_st))
105
/*
106
 * Defines our chains of values
107
 */
108
struct ht_internal_value_st {
109
    HT_VALUE value;
110
    HT *ht;
111
};
112
113
struct ht_neighborhood_entry_st {
114
    uint64_t hash;
115
    struct ht_internal_value_st *value;
116
} ALIGN;
117
118
struct ht_neighborhood_st {
119
    struct ht_neighborhood_entry_st entries[NEIGHBORHOOD_LEN];
120
};
121
122
/*
123
 * Updates to data in this struct
124
 * require an rcu sync after modification
125
 * prior to free
126
 */
127
struct ht_mutable_data_st {
128
    struct ht_neighborhood_st *neighborhoods;
129
    void *neighborhood_ptr_to_free;
130
    uint64_t neighborhood_mask;
131
};
132
133
/*
134
 * Private data may be updated on the write
135
 * side only, and so do not require rcu sync
136
 */
137
struct ht_write_private_data_st {
138
    size_t neighborhood_len;
139
    size_t value_count;
140
    int need_sync;
141
};
142
143
struct ht_internal_st {
144
    HT_CONFIG config;
145
    CRYPTO_RCU_LOCK *lock;
146
    CRYPTO_RWLOCK *atomic_lock;
147
    struct ht_mutable_data_st *md;
148
    struct ht_write_private_data_st wpd;
149
};
150
151
static void free_value(struct ht_internal_value_st *v);
152
153
static struct ht_neighborhood_st *alloc_new_neighborhood_list(size_t len,
154
    void **freeptr)
155
16
{
156
16
    struct ht_neighborhood_st *ret;
157
158
16
#if !defined(OPENSSL_SMALL_FOOTPRINT)
159
16
    ret = OPENSSL_aligned_alloc_array(len, sizeof(struct ht_neighborhood_st),
160
16
        CACHE_LINE_BYTES, freeptr);
161
162
    /* fall back to regular malloc */
163
16
    if (ret == NULL)
164
0
#endif
165
0
    {
166
0
        ret = *freeptr = OPENSSL_malloc_array(len, sizeof(struct ht_neighborhood_st));
167
0
        if (ret == NULL)
168
0
            return NULL;
169
0
    }
170
16
    memset(ret, 0, sizeof(struct ht_neighborhood_st) * len);
171
16
    return ret;
172
16
}
173
174
static void internal_free_nop(HT_VALUE *v)
175
0
{
176
0
    return;
177
0
}
178
179
static uint64_t internal_ht_hash_fn(HT_KEY *key)
180
127k
{
181
127k
    return ossl_fnv1a_hash(key->keybuf, key->keysize);
182
127k
}
183
184
HT *ossl_ht_new(const HT_CONFIG *conf)
185
16
{
186
16
    HT *new = OPENSSL_zalloc(sizeof(*new));
187
188
16
    if (new == NULL)
189
0
        return NULL;
190
191
16
    if (conf->lockless_reads && conf->no_rcu)
192
0
        goto err;
193
194
16
    if (!conf->no_rcu) {
195
16
        new->atomic_lock = CRYPTO_THREAD_lock_new();
196
16
        if (new->atomic_lock == NULL)
197
0
            goto err;
198
16
    }
199
16
    memcpy(&new->config, conf, sizeof(*conf));
200
201
16
    if (new->config.init_neighborhoods != 0) {
202
16
        new->wpd.neighborhood_len = new->config.init_neighborhoods;
203
        /* round up to the next power of 2 */
204
16
        new->wpd.neighborhood_len--;
205
16
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 1;
206
16
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 2;
207
16
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 4;
208
16
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 8;
209
16
        new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 16;
210
16
        new->wpd.neighborhood_len++;
211
16
    } else {
212
0
        new->wpd.neighborhood_len = DEFAULT_NEIGH_LEN;
213
0
    }
214
215
16
    if (new->config.ht_free_fn == NULL)
216
16
        new->config.ht_free_fn = internal_free_nop;
217
218
16
    new->md = OPENSSL_zalloc(sizeof(*new->md));
219
16
    if (new->md == NULL)
220
0
        goto err;
221
222
16
    new->md->neighborhoods = alloc_new_neighborhood_list(new->wpd.neighborhood_len,
223
16
        &new->md->neighborhood_ptr_to_free);
224
16
    if (new->md->neighborhoods == NULL)
225
0
        goto err;
226
16
    new->md->neighborhood_mask = new->wpd.neighborhood_len - 1;
227
228
16
    if (!conf->no_rcu) {
229
16
        new->lock = ossl_rcu_lock_new(1, conf->ctx);
230
16
        if (new->lock == NULL)
231
0
            goto err;
232
16
    }
233
16
    if (new->config.ht_hash_fn == NULL)
234
16
        new->config.ht_hash_fn = internal_ht_hash_fn;
235
236
16
    return new;
237
238
0
err:
239
0
    if (!conf->no_rcu) {
240
0
        CRYPTO_THREAD_lock_free(new->atomic_lock);
241
0
        ossl_rcu_lock_free(new->lock);
242
0
    }
243
0
    if (new->md != NULL)
244
0
        OPENSSL_free(new->md->neighborhood_ptr_to_free);
245
0
    OPENSSL_free(new->md);
246
0
    OPENSSL_free(new);
247
0
    return NULL;
248
16
}
249
250
int ossl_ht_read_lock(HT *htable)
251
0
{
252
0
    if (htable->config.no_rcu)
253
0
        return 1;
254
255
0
    return ossl_rcu_read_lock(htable->lock);
256
0
}
257
258
void ossl_ht_read_unlock(HT *htable)
259
0
{
260
0
    if (htable->config.no_rcu)
261
0
        return;
262
263
0
    ossl_rcu_read_unlock(htable->lock);
264
0
}
265
266
void ossl_ht_write_lock(HT *htable)
267
0
{
268
0
    if (htable->config.no_rcu)
269
0
        return;
270
271
0
    ossl_rcu_write_lock(htable->lock);
272
0
    htable->wpd.need_sync = 0;
273
0
}
274
275
void ossl_ht_write_unlock(HT *htable)
276
0
{
277
0
    int need_sync = htable->wpd.need_sync;
278
279
0
    if (htable->config.no_rcu)
280
0
        return;
281
282
0
    htable->wpd.need_sync = 0;
283
0
    ossl_rcu_write_unlock(htable->lock);
284
0
    if (need_sync)
285
0
        ossl_synchronize_rcu(htable->lock);
286
0
}
287
288
static void free_oldmd(void *arg)
289
0
{
290
0
    struct ht_mutable_data_st *oldmd = arg;
291
0
    size_t i, j;
292
0
    size_t neighborhood_len = (size_t)oldmd->neighborhood_mask + 1;
293
0
    struct ht_internal_value_st *v;
294
295
0
    for (i = 0; i < neighborhood_len; i++) {
296
0
        PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[i + 1]);
297
0
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
298
0
            if (oldmd->neighborhoods[i].entries[j].value != NULL) {
299
0
                v = oldmd->neighborhoods[i].entries[j].value;
300
0
                v->ht->config.ht_free_fn((HT_VALUE *)v);
301
0
                free_value(v);
302
0
            }
303
0
        }
304
0
    }
305
306
0
    OPENSSL_free(oldmd->neighborhood_ptr_to_free);
307
0
    OPENSSL_free(oldmd);
308
0
}
309
310
static int ossl_ht_flush_internal(HT *h)
311
0
{
312
0
    struct ht_mutable_data_st *newmd = NULL;
313
0
    struct ht_mutable_data_st *oldmd = NULL;
314
315
0
    newmd = OPENSSL_zalloc(sizeof(*newmd));
316
0
    if (newmd == NULL)
317
0
        return 0;
318
319
0
    newmd->neighborhoods = alloc_new_neighborhood_list(DEFAULT_NEIGH_LEN,
320
0
        &newmd->neighborhood_ptr_to_free);
321
0
    if (newmd->neighborhoods == NULL) {
322
0
        OPENSSL_free(newmd);
323
0
        return 0;
324
0
    }
325
326
0
    newmd->neighborhood_mask = DEFAULT_NEIGH_LEN - 1;
327
328
    /* Swap the old and new mutable data sets */
329
0
    if (!h->config.no_rcu) {
330
0
        oldmd = ossl_rcu_deref(&h->md);
331
0
        ossl_rcu_assign_ptr(&h->md, &newmd);
332
0
    } else {
333
0
        oldmd = h->md;
334
0
        h->md = newmd;
335
0
    }
336
337
    /* Set the number of entries to 0 */
338
0
    h->wpd.value_count = 0;
339
0
    h->wpd.neighborhood_len = DEFAULT_NEIGH_LEN;
340
341
0
    if (!h->config.no_rcu) {
342
0
        ossl_rcu_call(h->lock, free_oldmd, oldmd);
343
0
    } else {
344
0
        free_oldmd(oldmd);
345
0
    }
346
0
    h->wpd.need_sync = 1;
347
348
0
    return 1;
349
0
}
350
351
int ossl_ht_flush(HT *h)
352
0
{
353
0
    return ossl_ht_flush_internal(h);
354
0
}
355
356
void ossl_ht_free(HT *h)
357
0
{
358
0
    if (h == NULL)
359
0
        return;
360
361
0
    ossl_ht_write_lock(h);
362
0
    ossl_ht_flush_internal(h);
363
0
    ossl_ht_write_unlock(h);
364
    /* Freeing the lock does a final sync for us */
365
0
    if (!h->config.no_rcu) {
366
0
        CRYPTO_THREAD_lock_free(h->atomic_lock);
367
0
        ossl_rcu_lock_free(h->lock);
368
0
    }
369
0
    OPENSSL_free(h->md->neighborhood_ptr_to_free);
370
0
    OPENSSL_free(h->md);
371
0
    OPENSSL_free(h);
372
0
    return;
373
0
}
374
375
size_t ossl_ht_count(HT *h)
376
0
{
377
0
    size_t count;
378
379
0
    count = h->wpd.value_count;
380
0
    return count;
381
0
}
382
383
void ossl_ht_foreach_until(HT *h, int (*cb)(HT_VALUE *obj, void *arg),
384
    void *arg)
385
0
{
386
0
    size_t i, j;
387
0
    struct ht_mutable_data_st *md;
388
389
0
    md = ossl_rcu_deref(&h->md);
390
0
    for (i = 0; i < md->neighborhood_mask + 1; i++) {
391
0
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]);
392
0
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
393
0
            if (md->neighborhoods[i].entries[j].value != NULL) {
394
0
                if (!cb((HT_VALUE *)md->neighborhoods[i].entries[j].value, arg))
395
0
                    goto out;
396
0
            }
397
0
        }
398
0
    }
399
0
out:
400
0
    return;
401
0
}
402
403
HT_VALUE_LIST *ossl_ht_filter(HT *h, size_t max_len,
404
    int (*filter)(HT_VALUE *obj, void *arg),
405
    void *arg)
406
0
{
407
0
    struct ht_mutable_data_st *md;
408
0
    HT_VALUE_LIST *list = OPENSSL_zalloc(sizeof(HT_VALUE_LIST)
409
0
        + (sizeof(HT_VALUE *) * max_len));
410
0
    size_t i, j;
411
0
    struct ht_internal_value_st *v;
412
413
0
    if (list == NULL)
414
0
        return NULL;
415
416
    /*
417
     * The list array lives just beyond the end of
418
     * the struct
419
     */
420
0
    list->list = (HT_VALUE **)(list + 1);
421
422
0
    md = ossl_rcu_deref(&h->md);
423
0
    for (i = 0; i < md->neighborhood_mask + 1; i++) {
424
0
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]);
425
0
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
426
0
            v = md->neighborhoods[i].entries[j].value;
427
0
            if (v != NULL && filter((HT_VALUE *)v, arg)) {
428
0
                list->list[list->list_len++] = (HT_VALUE *)v;
429
0
                if (list->list_len == max_len)
430
0
                    goto out;
431
0
            }
432
0
        }
433
0
    }
434
0
out:
435
0
    return list;
436
0
}
437
438
void ossl_ht_value_list_free(HT_VALUE_LIST *list)
439
0
{
440
0
    OPENSSL_free(list);
441
0
}
442
443
static int compare_hash(uint64_t hash1, uint64_t hash2)
444
287k
{
445
287k
    return (hash1 == hash2);
446
287k
}
447
448
static void free_old_neigh_table(void *arg)
449
0
{
450
0
    struct ht_mutable_data_st *oldmd = arg;
451
452
0
    OPENSSL_free(oldmd->neighborhood_ptr_to_free);
453
0
    OPENSSL_free(oldmd);
454
0
}
455
456
/*
457
 * Increase hash table bucket list
458
 * must be called with write_lock held
459
 */
460
static int grow_hashtable(HT *h, size_t oldsize)
461
0
{
462
0
    struct ht_mutable_data_st *newmd;
463
0
    struct ht_mutable_data_st *oldmd = ossl_rcu_deref(&h->md);
464
0
    int rc = 0;
465
0
    uint64_t oldi, oldj, newi, newj;
466
0
    uint64_t oldhash;
467
0
    struct ht_internal_value_st *oldv;
468
0
    int rehashed;
469
0
    size_t newsize = oldsize * 2;
470
471
0
    if (h->config.lockless_reads)
472
0
        goto out;
473
474
0
    if ((newmd = OPENSSL_zalloc(sizeof(*newmd))) == NULL)
475
0
        goto out;
476
477
    /* bucket list is always a power of 2 */
478
0
    newmd->neighborhoods = alloc_new_neighborhood_list(oldsize * 2,
479
0
        &newmd->neighborhood_ptr_to_free);
480
0
    if (newmd->neighborhoods == NULL)
481
0
        goto out_free;
482
483
    /* being a power of 2 makes for easy mask computation */
484
0
    newmd->neighborhood_mask = (newsize - 1);
485
486
    /*
487
     * Now we need to start rehashing entries
488
     * Note we don't need to use atomics here as the new
489
     * mutable data hasn't been published
490
     */
491
0
    for (oldi = 0; oldi < h->wpd.neighborhood_len; oldi++) {
492
0
        PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[oldi + 1]);
493
0
        for (oldj = 0; oldj < NEIGHBORHOOD_LEN; oldj++) {
494
0
            oldv = oldmd->neighborhoods[oldi].entries[oldj].value;
495
0
            if (oldv == NULL)
496
0
                continue;
497
0
            oldhash = oldmd->neighborhoods[oldi].entries[oldj].hash;
498
0
            newi = oldhash & newmd->neighborhood_mask;
499
0
            rehashed = 0;
500
0
            for (newj = 0; newj < NEIGHBORHOOD_LEN; newj++) {
501
0
                if (newmd->neighborhoods[newi].entries[newj].value == NULL) {
502
0
                    newmd->neighborhoods[newi].entries[newj].value = oldv;
503
0
                    newmd->neighborhoods[newi].entries[newj].hash = oldhash;
504
0
                    rehashed = 1;
505
0
                    break;
506
0
                }
507
0
            }
508
0
            if (rehashed == 0) {
509
                /* we ran out of space in a neighborhood, grow again */
510
0
                OPENSSL_free(newmd->neighborhoods);
511
0
                OPENSSL_free(newmd);
512
0
                return grow_hashtable(h, newsize);
513
0
            }
514
0
        }
515
0
    }
516
    /*
517
     * Now that our entries are all hashed into the new bucket list
518
     * update our bucket_len and target_max_load
519
     */
520
0
    h->wpd.neighborhood_len = newsize;
521
522
    /*
523
     * Now we replace the old mutable data with the new
524
     */
525
0
    if (!h->config.no_rcu) {
526
0
        ossl_rcu_assign_ptr(&h->md, &newmd);
527
0
        ossl_rcu_call(h->lock, free_old_neigh_table, oldmd);
528
0
        h->wpd.need_sync = 1;
529
0
    } else {
530
0
        h->md = newmd;
531
0
        free_old_neigh_table(oldmd);
532
0
    }
533
    /*
534
     * And we're done
535
     */
536
0
    rc = 1;
537
538
0
out:
539
0
    return rc;
540
0
out_free:
541
0
    OPENSSL_free(newmd->neighborhoods);
542
0
    OPENSSL_free(newmd);
543
0
    goto out;
544
0
}
545
546
static void free_old_ht_value(void *arg)
547
0
{
548
0
    HT_VALUE *h = (HT_VALUE *)arg;
549
550
    /*
551
     * Note, this is only called on replacement,
552
     * the caller is responsible for freeing the
553
     * held data, we just need to free the wrapping
554
     * struct here
555
     */
556
0
    OPENSSL_free(h);
557
0
}
558
559
static ossl_inline int match_key(HT_KEY *a, HT_KEY *b)
560
112k
{
561
    /*
562
     * keys match if they are both present, the same size
563
     * and compare equal in memory
564
     */
565
112k
    PREFETCH(a->keybuf);
566
112k
    PREFETCH(b->keybuf);
567
112k
    if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize)
568
112k
        return !memcmp(a->keybuf, b->keybuf, a->keysize);
569
570
0
    return 1;
571
112k
}
572
573
static int ossl_ht_insert_locked(HT *h, uint64_t hash,
574
    struct ht_internal_value_st *newval,
575
    HT_VALUE **olddata)
576
6.41k
{
577
6.41k
    struct ht_mutable_data_st *md = h->md;
578
6.41k
    uint64_t neigh_idx_start = hash & md->neighborhood_mask;
579
6.41k
    uint64_t neigh_idx = neigh_idx_start;
580
6.41k
    size_t j;
581
6.41k
    uint64_t ihash;
582
6.41k
    HT_VALUE *ival;
583
6.41k
    size_t empty_idx = SIZE_MAX;
584
6.41k
    int lockless_reads = h->config.lockless_reads;
585
586
6.41k
    do {
587
6.41k
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]);
588
589
8.68k
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
590
8.68k
            if (!h->config.no_rcu)
591
8.68k
                ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value);
592
0
            else
593
0
                ival = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value;
594
8.68k
            if (ival == NULL) {
595
6.41k
                empty_idx = j;
596
                /* lockless_reads implies no deletion, we can break out */
597
6.41k
                if (lockless_reads)
598
6.41k
                    goto not_found;
599
0
                continue;
600
6.41k
            }
601
2.27k
            if (!h->config.no_rcu) {
602
2.27k
                if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash,
603
2.27k
                        &ihash, h->atomic_lock))
604
0
                    return 0;
605
2.27k
            } else {
606
0
                ihash = md->neighborhoods[neigh_idx].entries[j].hash;
607
0
            }
608
2.27k
            if (compare_hash(hash, ihash) && match_key(&newval->value.key, &ival->key)) {
609
0
                if (olddata == NULL) {
610
                    /* This would insert a duplicate -> fail */
611
0
                    return 0;
612
0
                }
613
                /* Do a replacement */
614
0
                if (!h->config.no_rcu) {
615
0
                    if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash,
616
0
                            hash, h->atomic_lock))
617
0
                        return 0;
618
0
                    *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value;
619
0
                    ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[j].value,
620
0
                        &newval);
621
0
                    ossl_rcu_call(h->lock, free_old_ht_value, *olddata);
622
0
                } else {
623
0
                    md->neighborhoods[neigh_idx].entries[j].hash = hash;
624
0
                    *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value;
625
0
                    md->neighborhoods[neigh_idx].entries[j].value = newval;
626
0
                }
627
0
                h->wpd.need_sync = 1;
628
0
                return 1;
629
0
            }
630
2.27k
        }
631
0
        if (!lockless_reads)
632
0
            break;
633
        /* Continue search in subsequent neighborhoods */
634
0
        neigh_idx = (neigh_idx + 1) & md->neighborhood_mask;
635
0
    } while (neigh_idx != neigh_idx_start);
636
637
6.41k
not_found:
638
    /* If we get to here, its just an insert */
639
6.41k
    if (empty_idx == SIZE_MAX)
640
0
        return -1; /* out of space */
641
6.41k
    if (!h->config.no_rcu) {
642
6.41k
        if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[empty_idx].hash,
643
6.41k
                hash, h->atomic_lock))
644
0
            return 0;
645
6.41k
        ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[empty_idx].value,
646
6.41k
            &newval);
647
6.41k
    } else {
648
0
        md->neighborhoods[neigh_idx].entries[empty_idx].hash = hash;
649
0
        md->neighborhoods[neigh_idx].entries[empty_idx].value = newval;
650
0
    }
651
6.41k
    h->wpd.value_count++;
652
6.41k
    return 1;
653
6.41k
}
654
655
static struct ht_internal_value_st *alloc_new_value(HT *h, HT_KEY *key,
656
    void *data,
657
    uintptr_t *type)
658
6.41k
{
659
6.41k
    struct ht_internal_value_st *tmp;
660
6.41k
    size_t nvsize = sizeof(*tmp);
661
662
6.41k
    if (h->config.collision_check == 1)
663
6.41k
        nvsize += key->keysize;
664
665
6.41k
    tmp = OPENSSL_malloc(nvsize);
666
667
6.41k
    if (tmp == NULL)
668
0
        return NULL;
669
670
6.41k
    tmp->ht = h;
671
6.41k
    tmp->value.value = data;
672
6.41k
    tmp->value.type_id = type;
673
6.41k
    tmp->value.key.keybuf = NULL;
674
6.41k
    if (h->config.collision_check) {
675
6.41k
        tmp->value.key.keybuf = (uint8_t *)(tmp + 1);
676
6.41k
        tmp->value.key.keysize = key->keysize;
677
6.41k
        memcpy(tmp->value.key.keybuf, key->keybuf, key->keysize);
678
6.41k
    }
679
680
6.41k
    return tmp;
681
6.41k
}
682
683
static void free_value(struct ht_internal_value_st *v)
684
0
{
685
0
    OPENSSL_free(v);
686
0
}
687
688
int ossl_ht_insert(HT *h, HT_KEY *key, HT_VALUE *data, HT_VALUE **olddata)
689
6.41k
{
690
6.41k
    struct ht_internal_value_st *newval = NULL;
691
6.41k
    uint64_t hash;
692
6.41k
    int rc = 0;
693
6.41k
    int i;
694
695
6.41k
    if (data->value == NULL)
696
0
        goto out;
697
698
6.41k
    rc = -1;
699
6.41k
    newval = alloc_new_value(h, key, data->value, data->type_id);
700
6.41k
    if (newval == NULL)
701
0
        goto out;
702
703
    /*
704
     * we have to take our lock here to prevent other changes
705
     * to the bucket list
706
     */
707
6.41k
    hash = h->config.ht_hash_fn(key);
708
709
6.41k
    for (i = 0;
710
6.41k
        (rc = ossl_ht_insert_locked(h, hash, newval, olddata)) == -1
711
0
        && i < 4;
712
6.41k
        ++i)
713
0
        if (!grow_hashtable(h, h->wpd.neighborhood_len)) {
714
0
            rc = -1;
715
0
            break;
716
0
        }
717
718
6.41k
    if (rc <= 0)
719
0
        free_value(newval);
720
721
6.41k
out:
722
6.41k
    return rc;
723
6.41k
}
724
725
HT_VALUE *ossl_ht_get(HT *h, HT_KEY *key)
726
121k
{
727
121k
    struct ht_mutable_data_st *md;
728
121k
    uint64_t hash;
729
121k
    uint64_t neigh_idx_start;
730
121k
    uint64_t neigh_idx;
731
121k
    struct ht_internal_value_st *ival = NULL;
732
121k
    size_t j;
733
121k
    uint64_t ehash;
734
121k
    int lockless_reads = h->config.lockless_reads;
735
736
121k
    hash = h->config.ht_hash_fn(key);
737
738
121k
    if (!h->config.no_rcu)
739
121k
        md = ossl_rcu_deref(&h->md);
740
0
    else
741
0
        md = h->md;
742
121k
    neigh_idx = neigh_idx_start = hash & md->neighborhood_mask;
743
121k
    do {
744
121k
        PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]);
745
293k
        for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
746
293k
            if (!h->config.no_rcu)
747
293k
                ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value);
748
0
            else
749
0
                ival = md->neighborhoods[neigh_idx].entries[j].value;
750
293k
            if (ival == NULL) {
751
8.72k
                if (lockless_reads)
752
                    /* lockless_reads implies no deletion, we can break out */
753
8.72k
                    return NULL;
754
0
                continue;
755
8.72k
            }
756
285k
            if (!h->config.no_rcu) {
757
285k
                if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash,
758
285k
                        &ehash, h->atomic_lock))
759
0
                    return NULL;
760
285k
            } else {
761
0
                ehash = md->neighborhoods[neigh_idx].entries[j].hash;
762
0
            }
763
285k
            if (compare_hash(hash, ehash) && match_key(&ival->value.key, key))
764
112k
                return (HT_VALUE *)ival;
765
285k
        }
766
0
        if (!lockless_reads)
767
0
            break;
768
        /* Continue search in subsequent neighborhoods */
769
0
        neigh_idx = (neigh_idx + 1) & md->neighborhood_mask;
770
0
    } while (neigh_idx != neigh_idx_start);
771
772
0
    return NULL;
773
121k
}
774
775
static void free_old_entry(void *arg)
776
0
{
777
0
    struct ht_internal_value_st *v = arg;
778
779
0
    v->ht->config.ht_free_fn((HT_VALUE *)v);
780
0
    free_value(v);
781
0
}
782
783
int ossl_ht_delete(HT *h, HT_KEY *key)
784
0
{
785
0
    uint64_t hash;
786
0
    uint64_t neigh_idx;
787
0
    size_t j;
788
0
    struct ht_internal_value_st *v = NULL;
789
0
    HT_VALUE *nv = NULL;
790
0
    int rc = 0;
791
792
0
    if (h->config.lockless_reads)
793
0
        return 0;
794
795
0
    hash = h->config.ht_hash_fn(key);
796
797
0
    neigh_idx = hash & h->md->neighborhood_mask;
798
0
    PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]);
799
0
    for (j = 0; j < NEIGHBORHOOD_LEN; j++) {
800
0
        v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value;
801
0
        if (v == NULL)
802
0
            continue;
803
0
        if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash)
804
0
            && match_key(key, &v->value.key)) {
805
0
            if (!h->config.no_rcu) {
806
0
                if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash,
807
0
                        0, h->atomic_lock))
808
0
                    break;
809
0
                ossl_rcu_assign_ptr(&h->md->neighborhoods[neigh_idx].entries[j].value, &nv);
810
0
            } else {
811
0
                h->md->neighborhoods[neigh_idx].entries[j].hash = 0;
812
0
                h->md->neighborhoods[neigh_idx].entries[j].value = NULL;
813
0
            }
814
0
            h->wpd.value_count--;
815
0
            rc = 1;
816
0
            break;
817
0
        }
818
0
    }
819
0
    if (rc == 1) {
820
0
        if (!h->config.no_rcu)
821
0
            ossl_rcu_call(h->lock, free_old_entry, v);
822
0
        else
823
0
            free_old_entry(v);
824
0
        h->wpd.need_sync = 1;
825
0
    }
826
827
0
    return rc;
828
0
}
829
830
HT_VALUE *ossl_ht_deref_value(HT *h, HT_VALUE **val)
831
0
{
832
0
    HT_VALUE *v;
833
834
0
    if (!h->config.no_rcu)
835
0
        v = ossl_rcu_deref(val);
836
0
    else
837
0
        v = *val;
838
839
0
    return v;
840
0
}
841
842
void *ossl_ht_inner_value(HT *h, HT_VALUE *v)
843
0
{
844
0
    void *inner;
845
846
0
    if (!h->config.no_rcu) {
847
0
        inner = v->value;
848
0
    } else {
849
0
        inner = v->value;
850
0
        OPENSSL_free(v);
851
0
    }
852
853
0
    return inner;
854
0
}