1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
|
/*
* Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <stdio.h>
#include <string.h>
#include <openssl/opensslconf.h>
#include <openssl/lhash.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
#include <internal/hashtable.h>
#include "internal/nelem.h"
#include "threadstest.h"
#include "testutil.h"
/*
* The macros below generate unused functions which error out one of the clang
* builds. We disable this check here.
*/
#ifdef __clang__
#pragma clang diagnostic ignored "-Wunused-function"
#endif
DEFINE_LHASH_OF_EX(int);
static int int_tests[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
-17, 16, 17, -23, 35, 37, 173, 11 };
static const size_t n_int_tests = OSSL_NELEM(int_tests);
static short int_found[OSSL_NELEM(int_tests)];
static short int_not_found;
static unsigned long int int_hash(const int *p)
{
return 3 & *p; /* To force collisions */
}
static int int_cmp(const int *p, const int *q)
{
return *p != *q;
}
static int int_find(int n)
{
unsigned int i;
for (i = 0; i < n_int_tests; i++)
if (int_tests[i] == n)
return i;
return -1;
}
static void int_doall(int *v)
{
const int n = int_find(*v);
if (n < 0)
int_not_found++;
else
int_found[n]++;
}
static void int_doall_arg(int *p, short *f)
{
const int n = int_find(*p);
if (n < 0)
int_not_found++;
else
f[n]++;
}
IMPLEMENT_LHASH_DOALL_ARG(int, short);
static int test_int_lhash(void)
{
static struct {
int data;
int null;
} dels[] = {
{ 65537, 0 },
{ 173, 0 },
{ 999, 1 },
{ 37, 0 },
{ 1, 0 },
{ 34, 1 }
};
const unsigned int n_dels = OSSL_NELEM(dels);
LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
unsigned int i;
int testresult = 0, j, *p;
if (!TEST_ptr(h))
goto end;
/* insert */
for (i = 0; i < n_int_tests; i++)
if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {
TEST_info("int insert %d", i);
goto end;
}
/* num_items */
if (!TEST_int_eq((size_t)lh_int_num_items(h), n_int_tests))
goto end;
/* retrieve */
for (i = 0; i < n_int_tests; i++)
if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {
TEST_info("lhash int retrieve value %d", i);
goto end;
}
for (i = 0; i < n_int_tests; i++)
if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {
TEST_info("lhash int retrieve address %d", i);
goto end;
}
j = 1;
if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))
goto end;
/* replace */
j = 13;
if (!TEST_ptr(p = lh_int_insert(h, &j)))
goto end;
if (!TEST_ptr_eq(p, int_tests + 1))
goto end;
if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))
goto end;
/* do_all */
memset(int_found, 0, sizeof(int_found));
int_not_found = 0;
lh_int_doall(h, &int_doall);
if (!TEST_int_eq(int_not_found, 0)) {
TEST_info("lhash int doall encountered a not found condition");
goto end;
}
for (i = 0; i < n_int_tests; i++)
if (!TEST_int_eq(int_found[i], 1)) {
TEST_info("lhash int doall %d", i);
goto end;
}
/* do_all_arg */
memset(int_found, 0, sizeof(int_found));
int_not_found = 0;
lh_int_doall_short(h, int_doall_arg, int_found);
if (!TEST_int_eq(int_not_found, 0)) {
TEST_info("lhash int doall arg encountered a not found condition");
goto end;
}
for (i = 0; i < n_int_tests; i++)
if (!TEST_int_eq(int_found[i], 1)) {
TEST_info("lhash int doall arg %d", i);
goto end;
}
/* delete */
for (i = 0; i < n_dels; i++) {
const int b = lh_int_delete(h, &dels[i].data) == NULL;
if (!TEST_int_eq(b ^ dels[i].null, 0)) {
TEST_info("lhash int delete %d", i);
goto end;
}
}
/* error */
if (!TEST_int_eq(lh_int_error(h), 0))
goto end;
testresult = 1;
end:
lh_int_free(h);
return testresult;
}
static int int_filter_all(HT_VALUE *v, void *arg)
{
return 1;
}
HT_START_KEY_DEFN(intkey)
HT_DEF_KEY_FIELD(mykey, int)
HT_END_KEY_DEFN(INTKEY)
IMPLEMENT_HT_VALUE_TYPE_FNS(int, test, static)
static int int_foreach(HT_VALUE *v, void *arg)
{
int *vd = ossl_ht_test_int_from_value(v);
const int n = int_find(*vd);
if (n < 0)
int_not_found++;
else
int_found[n]++;
return 1;
}
static uint64_t hashtable_hash(uint8_t *key, size_t keylen)
{
return (uint64_t)(*(uint32_t *)key);
}
static int test_int_hashtable(void)
{
static struct {
int data;
int should_del;
} dels[] = {
{ 65537 , 1},
{ 173 , 1},
{ 999 , 0 },
{ 37 , 1 },
{ 1 , 1 },
{ 34 , 0 }
};
const size_t n_dels = OSSL_NELEM(dels);
HT_CONFIG hash_conf = {
NULL,
NULL,
NULL,
0,
1,
};
INTKEY key;
int rc = 0;
size_t i;
HT *ht = NULL;
int todel;
HT_VALUE_LIST *list = NULL;
ht = ossl_ht_new(&hash_conf);
if (ht == NULL)
return 0;
/* insert */
HT_INIT_KEY(&key);
for (i = 0; i < n_int_tests; i++) {
HT_SET_KEY_FIELD(&key, mykey, int_tests[i]);
if (!TEST_int_eq(ossl_ht_test_int_insert(ht, TO_HT_KEY(&key),
&int_tests[i], NULL), 1)) {
TEST_info("int insert %zu", i);
goto end;
}
}
/* num_items */
if (!TEST_int_eq((size_t)ossl_ht_count(ht), n_int_tests))
goto end;
/* foreach, no arg */
memset(int_found, 0, sizeof(int_found));
int_not_found = 0;
ossl_ht_foreach_until(ht, int_foreach, NULL);
if (!TEST_int_eq(int_not_found, 0)) {
TEST_info("hashtable int foreach encountered a not found condition");
goto end;
}
for (i = 0; i < n_int_tests; i++)
if (!TEST_int_eq(int_found[i], 1)) {
TEST_info("hashtable int foreach %zu", i);
goto end;
}
/* filter */
list = ossl_ht_filter(ht, 64, int_filter_all, NULL);
if (!TEST_int_eq((size_t)list->list_len, n_int_tests))
goto end;
ossl_ht_value_list_free(list);
/* delete */
for (i = 0; i < n_dels; i++) {
HT_SET_KEY_FIELD(&key, mykey, dels[i].data);
todel = ossl_ht_delete(ht, TO_HT_KEY(&key));
if (dels[i].should_del) {
if (!TEST_int_eq(todel, 1)) {
TEST_info("hashtable couldn't find entry %d to delete\n",
dels[i].data);
goto end;
}
} else {
if (!TEST_int_eq(todel, 0)) {
TEST_info("%d found an entry that shouldn't be there\n", dels[i].data);
goto end;
}
}
}
rc = 1;
end:
ossl_ht_free(ht);
return rc;
}
static unsigned long int stress_hash(const int *p)
{
return *p;
}
#ifdef MEASURE_HASH_PERFORMANCE
static int
timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
{
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/*
* Compute the time remaining to wait.
* tv_usec is certainly positive.
*/
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
#endif
static int test_stress(void)
{
LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
const unsigned int n = 2500000;
unsigned int i;
int testresult = 0, *p;
#ifdef MEASURE_HASH_PERFORMANCE
struct timeval start, end, delta;
#endif
if (!TEST_ptr(h))
goto end;
#ifdef MEASURE_HASH_PERFORMANCE
gettimeofday(&start, NULL);
#endif
/* insert */
for (i = 0; i < n; i++) {
p = OPENSSL_malloc(sizeof(i));
if (!TEST_ptr(p)) {
TEST_info("lhash stress out of memory %d", i);
goto end;
}
*p = 3 * i + 1;
lh_int_insert(h, p);
}
/* num_items */
if (!TEST_int_eq(lh_int_num_items(h), n))
goto end;
/* delete in a different order */
for (i = 0; i < n; i++) {
const int j = (7 * i + 4) % n * 3 + 1;
if (!TEST_ptr(p = lh_int_delete(h, &j))) {
TEST_info("lhash stress delete %d\n", i);
goto end;
}
if (!TEST_int_eq(*p, j)) {
TEST_info("lhash stress bad value %d", i);
goto end;
}
OPENSSL_free(p);
}
testresult = 1;
end:
#ifdef MEASURE_HASH_PERFORMANCE
gettimeofday(&end, NULL);
timeval_subtract(&delta, &end, &start);
TEST_info("lhash stress runs in %ld.%ld seconds", delta.tv_sec, delta.tv_usec);
#endif
lh_int_free(h);
return testresult;
}
static void hashtable_intfree(HT_VALUE *v)
{
OPENSSL_free(v->value);
}
static int test_hashtable_stress(int idx)
{
const unsigned int n = 2500000;
unsigned int i;
int testresult = 0, *p;
HT_CONFIG hash_conf = {
NULL, /* use default context */
hashtable_intfree, /* our free function */
hashtable_hash, /* our hash function */
625000, /* preset hash size */
1, /* Check collisions */
0 /* Lockless reads */
};
HT *h;
INTKEY key;
HT_VALUE *v;
#ifdef MEASURE_HASH_PERFORMANCE
struct timeval start, end, delta;
#endif
hash_conf.lockless_reads = idx;
h = ossl_ht_new(&hash_conf);
if (!TEST_ptr(h))
goto end;
#ifdef MEASURE_HASH_PERFORMANCE
gettimeofday(&start, NULL);
#endif
HT_INIT_KEY(&key);
/* insert */
for (i = 0; i < n; i++) {
p = OPENSSL_malloc(sizeof(i));
if (!TEST_ptr(p)) {
TEST_info("hashtable stress out of memory %d", i);
goto end;
}
*p = 3 * i + 1;
HT_SET_KEY_FIELD(&key, mykey, *p);
if (!TEST_int_eq(ossl_ht_test_int_insert(h, TO_HT_KEY(&key),
p, NULL), 1)) {
TEST_info("hashtable unable to insert element %d\n", *p);
goto end;
}
}
/* make sure we stored everything */
if (!TEST_int_eq((size_t)ossl_ht_count(h), n))
goto end;
/* delete or get in a different order */
for (i = 0; i < n; i++) {
const int j = (7 * i + 4) % n * 3 + 1;
HT_SET_KEY_FIELD(&key, mykey, j);
switch (idx) {
case 0:
if (!TEST_int_eq((ossl_ht_delete(h, TO_HT_KEY(&key))), 1)) {
TEST_info("hashtable didn't delete key %d\n", j);
goto end;
}
break;
case 1:
if (!TEST_ptr(p = ossl_ht_test_int_get(h, TO_HT_KEY(&key), &v))
|| !TEST_int_eq(*p, j)) {
TEST_info("hashtable didn't get key %d\n", j);
goto end;
}
break;
}
}
testresult = 1;
end:
#ifdef MEASURE_HASH_PERFORMANCE
gettimeofday(&end, NULL);
timeval_subtract(&delta, &end, &start);
TEST_info("hashtable stress runs in %ld.%ld seconds", delta.tv_sec, delta.tv_usec);
#endif
ossl_ht_free(h);
return testresult;
}
typedef struct test_mt_entry {
int in_table;
int pending_delete;
} TEST_MT_ENTRY;
static HT *m_ht = NULL;
#define TEST_MT_POOL_SZ 256
#define TEST_THREAD_ITERATIONS 1000000
#define NUM_WORKERS 16
static struct test_mt_entry test_mt_entries[TEST_MT_POOL_SZ];
static char *worker_exits[NUM_WORKERS];
HT_START_KEY_DEFN(mtkey)
HT_DEF_KEY_FIELD(index, uint32_t)
HT_END_KEY_DEFN(MTKEY)
IMPLEMENT_HT_VALUE_TYPE_FNS(TEST_MT_ENTRY, mt, static)
static int worker_num = 0;
static CRYPTO_RWLOCK *worker_lock;
static CRYPTO_RWLOCK *testrand_lock;
static int free_failure = 0;
static int shutting_down = 0;
static int global_iteration = 0;
static void hashtable_mt_free(HT_VALUE *v)
{
TEST_MT_ENTRY *m = ossl_ht_mt_TEST_MT_ENTRY_from_value(v);
int pending_delete;
int ret;
CRYPTO_atomic_load_int(&m->pending_delete, &pending_delete, worker_lock);
if (shutting_down == 1)
return;
if (pending_delete == 0) {
TEST_info("Freeing element which was not scheduled for free");
free_failure = 1;
} else {
CRYPTO_atomic_add(&m->pending_delete, -1,
&ret, worker_lock);
}
}
#define DO_LOOKUP 0
#define DO_INSERT 1
#define DO_REPLACE 2
#define DO_DELETE 3
#define NUM_BEHAVIORS (DO_DELETE + 1)
static void do_mt_hash_work(void)
{
MTKEY key;
uint32_t index;
int num;
TEST_MT_ENTRY *m;
TEST_MT_ENTRY *expected_m = NULL;
HT_VALUE *v = NULL;
TEST_MT_ENTRY **r = NULL;
int expected_rc;
int ret;
char behavior;
size_t iter = 0;
int giter;
CRYPTO_atomic_add(&worker_num, 1, &num, worker_lock);
num--; /* atomic_add is an add/fetch operation */
HT_INIT_KEY(&key);
for (iter = 0; iter < TEST_THREAD_ITERATIONS; iter++) {
if (!TEST_true(CRYPTO_THREAD_write_lock(testrand_lock)))
return;
index = test_random() % TEST_MT_POOL_SZ;
behavior = (char)(test_random() % NUM_BEHAVIORS);
CRYPTO_THREAD_unlock(testrand_lock);
expected_m = &test_mt_entries[index];
HT_KEY_RESET(&key);
HT_SET_KEY_FIELD(&key, index, index);
if (!CRYPTO_atomic_add(&global_iteration, 1, &giter, worker_lock)) {
worker_exits[num] = "Unable to increment global iterator";
return;
}
switch(behavior) {
case DO_LOOKUP:
ossl_ht_read_lock(m_ht);
m = ossl_ht_mt_TEST_MT_ENTRY_get(m_ht, TO_HT_KEY(&key), &v);
if (m != NULL && m != expected_m) {
worker_exits[num] = "Read unexpected value from hashtable";
TEST_info("Iteration %d Read unexpected value %p when %p expected",
giter, (void *)m, (void *)expected_m);
}
ossl_ht_read_unlock(m_ht);
if (worker_exits[num] != NULL)
return;
break;
case DO_INSERT:
case DO_REPLACE:
ossl_ht_write_lock(m_ht);
if (behavior == DO_REPLACE) {
expected_rc = 1;
r = &m;
} else {
expected_rc = !expected_m->in_table;
r = NULL;
}
if (expected_rc != ossl_ht_mt_TEST_MT_ENTRY_insert(m_ht,
TO_HT_KEY(&key),
expected_m, r)) {
TEST_info("Iteration %d Expected rc %d on %s of element %u which is %s\n",
giter, expected_rc, behavior == DO_REPLACE ? "replace" : "insert",
(unsigned int)index,
expected_m->in_table ? "in table" : "not in table");
worker_exits[num] = "Failure on insert";
}
if (expected_rc == 1)
expected_m->in_table = 1;
ossl_ht_write_unlock(m_ht);
if (worker_exits[num] != NULL)
return;
break;
case DO_DELETE:
ossl_ht_write_lock(m_ht);
expected_rc = expected_m->in_table;
if (expected_rc == 1) {
/*
* We must set pending_delete before the actual deletion
* as another inserting or deleting thread can pick up
* the delete callback before the ossl_ht_write_unlock() call.
* This can happen only if no read locks are pending and
* only on Windows where we do not use the write mutex
* to get the callback list.
*/
expected_m->in_table = 0;
CRYPTO_atomic_add(&expected_m->pending_delete, 1, &ret, worker_lock);
}
if (expected_rc != ossl_ht_delete(m_ht, TO_HT_KEY(&key))) {
TEST_info("Iteration %d Expected rc %d on delete of element %u which is %s\n",
giter, expected_rc, (unsigned int)index,
expected_m->in_table ? "in table" : "not in table");
worker_exits[num] = "Failure on delete";
}
ossl_ht_write_unlock(m_ht);
if (worker_exits[num] != NULL)
return;
break;
default:
worker_exits[num] = "Undefined behavior specified";
return;
}
}
}
static int test_hashtable_multithread(void)
{
HT_CONFIG hash_conf = {
NULL, /* use default context */
hashtable_mt_free, /* our free function */
NULL, /* default hash function */
0, /* default hash size */
1, /* Check collisions */
};
int ret = 0;
thread_t workers[NUM_WORKERS];
int i;
#ifdef MEASURE_HASH_PERFORMANCE
struct timeval start, end, delta;
#endif
memset(worker_exits, 0, sizeof(char *) * NUM_WORKERS);
memset(test_mt_entries, 0, sizeof(TEST_MT_ENTRY) * TEST_MT_POOL_SZ);
memset(workers, 0, sizeof(thread_t) * NUM_WORKERS);
m_ht = ossl_ht_new(&hash_conf);
if (!TEST_ptr(m_ht))
goto end;
if (!TEST_ptr(worker_lock = CRYPTO_THREAD_lock_new()))
goto end_free;
if (!TEST_ptr(testrand_lock = CRYPTO_THREAD_lock_new()))
goto end_free;
#ifdef MEASURE_HASH_PERFORMANCE
gettimeofday(&start, NULL);
#endif
for (i = 0; i < NUM_WORKERS; i++) {
if (!run_thread(&workers[i], do_mt_hash_work))
goto shutdown;
}
shutdown:
for (--i; i >= 0; i--) {
wait_for_thread(workers[i]);
}
/*
* Now that the workers are done, check for any error
* conditions
*/
ret = 1;
for (i = 0; i < NUM_WORKERS; i++) {
if (worker_exits[i] != NULL) {
TEST_info("Worker %d failed: %s\n", i, worker_exits[i]);
ret = 0;
}
}
if (free_failure == 1) {
TEST_info("Encountered a free failure");
ret = 0;
}
#ifdef MEASURE_HASH_PERFORMANCE
gettimeofday(&end, NULL);
timeval_subtract(&delta, &end, &start);
TEST_info("multithread stress runs 40000 ops in %ld.%ld seconds", delta.tv_sec, delta.tv_usec);
#endif
end_free:
shutting_down = 1;
ossl_ht_free(m_ht);
CRYPTO_THREAD_lock_free(worker_lock);
CRYPTO_THREAD_lock_free(testrand_lock);
end:
return ret;
}
int setup_tests(void)
{
ADD_TEST(test_int_lhash);
ADD_TEST(test_stress);
ADD_TEST(test_int_hashtable);
ADD_ALL_TESTS(test_hashtable_stress, 2);
ADD_TEST(test_hashtable_multithread);
return 1;
}
|