Coverage Report

Created: 2026-01-07 06:10

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