curl/tests/unit/unit3212.c
Stefan Eissing 909af1a43b
multi: do transfer book keeping using mid
Change multi's book keeping of transfers to no longer use lists, but a
special table and bitsets for unsigned int values.

`multi-xfers` is the `uint_tbl` where `multi_add_handle()` inserts a new
transfer which assigns it a unique identifier `mid`. Use bitsets to keep
track of transfers that are in state "process" or "pending" or
"msgsent".

Use sparse bitsets to replace `conn->easyq` and event handlings tracking
of transfers per socket. Instead of pointers, keep the mids involved.

Provide base data structures and document them in docs/internal:
* `uint_tbl`: a table of transfers with `mid` as lookup key,
   handing out a mid for adds between 0 - capacity.
* `uint_bset`: a bitset keeping unsigned ints from 0 - capacity.
* `uint_spbset`: a sparse bitset for keeping a small number of
  unsigned int values
* `uint_hash`: for associating `mid`s with a pointer.

This makes the `mid` the recommended way to refer to transfers inside
the same multi without risk of running into a UAF.

Modifying table and bitsets is safe while iterating over them. Overall
memory requirements are lower as with the double linked list apprach.

Closes #16761
2025-04-17 17:28:38 +02:00

136 lines
5 KiB
C

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curlcheck.h"
#include "urldata.h"
#include "uint-table.h"
#include "curl_trc.h"
#define TBL_SIZE 100
static struct uint_tbl tbl;
static int dummy;
static CURLcode unit_setup(void)
{
Curl_uint_tbl_init(&tbl, NULL);
return Curl_uint_tbl_resize(&tbl, TBL_SIZE);
}
static void unit_stop(void)
{
Curl_uint_tbl_destroy(&tbl);
}
static void check3212(void)
{
unsigned int i, key, n;
void *entry;
fail_unless(Curl_uint_tbl_capacity(&tbl) == TBL_SIZE, "wrong capacity");
for(i = 0; i < TBL_SIZE; ++i) {
fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add");
fail_unless(key == i, "unexpected key assigned");
}
/* table should be full now */
fail_unless(Curl_uint_tbl_count(&tbl) == TBL_SIZE, "wrong count");
fail_unless(!Curl_uint_tbl_add(&tbl, &dummy, &key), "could add more");
/* remove every 2nd entry, from full table */
n = TBL_SIZE;
for(i = 0; i < TBL_SIZE; i += 2) {
Curl_uint_tbl_remove(&tbl, i);
--n;
fail_unless(Curl_uint_tbl_count(&tbl) == n, "wrong count after remove");
}
/* remove same again, should not change count */
for(i = 0; i < TBL_SIZE; i += 2) {
Curl_uint_tbl_remove(&tbl, i);
fail_unless(Curl_uint_tbl_count(&tbl) == n, "wrong count after remove");
}
/* still contains all odd entries */
for(i = 1; i < TBL_SIZE; i += 2) {
fail_unless(Curl_uint_tbl_contains(&tbl, i), "does not contain");
fail_unless(Curl_uint_tbl_get(&tbl, i) == &dummy,
"does not contain dummy");
}
/* get the first key */
fail_unless(Curl_uint_tbl_first(&tbl, &key, &entry), "first failed");
fail_unless(key == 1, "unexpected first key");
fail_unless(entry == &dummy, "unexpected first entry");
/* get the second key */
fail_unless(Curl_uint_tbl_next(&tbl, 1, &key, &entry), "next1 failed");
fail_unless(key == 3, "unexpected second key");
fail_unless(entry == &dummy, "unexpected second entry");
/* get the key after 42 */
fail_unless(Curl_uint_tbl_next(&tbl, 42, &key, &entry), "next42 failed");
fail_unless(key == 43, "unexpected next42 key");
fail_unless(entry == &dummy, "unexpected next42 entry");
/* double capacity */
n = Curl_uint_tbl_count(&tbl);
fail_unless(!Curl_uint_tbl_resize(&tbl, TBL_SIZE * 2),
"error doubling size");
fail_unless(Curl_uint_tbl_count(&tbl) == n, "wrong resize count");
/* resize to half of original */
fail_unless(!Curl_uint_tbl_resize(&tbl, TBL_SIZE / 2), "error halving size");
fail_unless(Curl_uint_tbl_count(&tbl) == n / 2, "wrong half size count");
for(i = 1; i < TBL_SIZE / 2; i += 2) {
fail_unless(Curl_uint_tbl_contains(&tbl, i), "does not contain");
fail_unless(Curl_uint_tbl_get(&tbl, i) == &dummy,
"does not contain dummy");
}
/* clear */
Curl_uint_tbl_clear(&tbl);
fail_unless(!Curl_uint_tbl_count(&tbl), "count not 0 after clear");
for(i = 0; i < TBL_SIZE / 2; ++i) {
fail_unless(!Curl_uint_tbl_contains(&tbl, i), "does contain, should not");
}
/* add after clear gets key 0 again */
fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add");
fail_unless(key == 0, "unexpected key assigned");
/* remove it again and add, should get key 1 */
Curl_uint_tbl_remove(&tbl, key);
fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add");
fail_unless(key == 1, "unexpected key assigned");
/* clear, fill, remove one, add, should get the removed key again */
Curl_uint_tbl_clear(&tbl);
for(i = 0; i < Curl_uint_tbl_capacity(&tbl); ++i)
fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add");
fail_unless(!Curl_uint_tbl_add(&tbl, &dummy, &key), "add on full");
Curl_uint_tbl_remove(&tbl, 17);
fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add again");
fail_unless(key == 17, "unexpected key assigned");
/* and again, triggering key search wrap around */
Curl_uint_tbl_remove(&tbl, 17);
fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add again");
fail_unless(key == 17, "unexpected key assigned");
}
UNITTEST_START
check3212();
UNITTEST_STOP