Skip to content

Commit 4d7384e

Browse files
vincentbernatzippy2
authored andcommitted
util: don't check for parallel iteration in hash-related functions
This is the responsability of the caller to apply the correct lock before using these functions. Moreover, the use of a simple boolean was still racy: two threads may check the boolean and "lock" it simultaneously. Users of functions from src/util/virhash.c have to be checked for correctness. Lookups and iteration should hold a RO lock. Modifications should hold a RW lock. Most important uses seem to be covered. Callers have now a greater responsability, notably the ability to execute some operations while iterating were reliably forbidden before are now accepted. Signed-off-by: Vincent Bernat <[email protected]>
1 parent a3b1ee0 commit 4d7384e

2 files changed

Lines changed: 0 additions & 120 deletions

File tree

‎src/util/virhash.c‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ VIR_LOG_INIT("util.hash");
4141

4242
/* #define DEBUG_GROW */
4343

44-
#define virHashIterationError(ret) \
45-
do { \
46-
VIR_ERROR(_("Hash operation not allowed during iteration")); \
47-
return ret; \
48-
} while (0)
49-
5044
/*
5145
* A single entry in the hash table
5246
*/
@@ -66,10 +60,6 @@ struct _virHashTable {
6660
uint32_t seed;
6761
size_t size;
6862
size_t nbElems;
69-
/* True iff we are iterating over hash entries. */
70-
bool iterating;
71-
/* Pointer to the current entry during iteration. */
72-
virHashEntryPtr current;
7363
virHashDataFree dataFree;
7464
virHashKeyCode keyCode;
7565
virHashKeyEqual keyEqual;
@@ -339,9 +329,6 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
339329
if ((table == NULL) || (name == NULL))
340330
return -1;
341331

342-
if (table->iterating)
343-
virHashIterationError(-1);
344-
345332
key = virHashComputeKey(table, name);
346333

347334
/* Check for duplicate entry */
@@ -551,9 +538,6 @@ virHashRemoveEntry(virHashTablePtr table, const void *name)
551538
nextptr = table->table + virHashComputeKey(table, name);
552539
for (entry = *nextptr; entry; entry = entry->next) {
553540
if (table->keyEqual(entry->name, name)) {
554-
if (table->iterating && table->current != entry)
555-
virHashIterationError(-1);
556-
557541
if (table->dataFree)
558542
table->dataFree(entry->payload, entry->name);
559543
if (table->keyFree)
@@ -593,18 +577,11 @@ virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
593577
if (table == NULL || iter == NULL)
594578
return -1;
595579

596-
if (table->iterating)
597-
virHashIterationError(-1);
598-
599-
table->iterating = true;
600-
table->current = NULL;
601580
for (i = 0; i < table->size; i++) {
602581
virHashEntryPtr entry = table->table[i];
603582
while (entry) {
604583
virHashEntryPtr next = entry->next;
605-
table->current = entry;
606584
ret = iter(entry->payload, entry->name, data);
607-
table->current = NULL;
608585

609586
if (ret < 0)
610587
goto cleanup;
@@ -615,7 +592,6 @@ virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
615592

616593
ret = 0;
617594
cleanup:
618-
table->iterating = false;
619595
return ret;
620596
}
621597

@@ -643,11 +619,6 @@ virHashRemoveSet(virHashTablePtr table,
643619
if (table == NULL || iter == NULL)
644620
return -1;
645621

646-
if (table->iterating)
647-
virHashIterationError(-1);
648-
649-
table->iterating = true;
650-
table->current = NULL;
651622
for (i = 0; i < table->size; i++) {
652623
virHashEntryPtr *nextptr = table->table + i;
653624

@@ -667,7 +638,6 @@ virHashRemoveSet(virHashTablePtr table,
667638
}
668639
}
669640
}
670-
table->iterating = false;
671641

672642
return count;
673643
}
@@ -723,23 +693,16 @@ void *virHashSearch(const virHashTable *ctable,
723693
if (table == NULL || iter == NULL)
724694
return NULL;
725695

726-
if (table->iterating)
727-
virHashIterationError(NULL);
728-
729-
table->iterating = true;
730-
table->current = NULL;
731696
for (i = 0; i < table->size; i++) {
732697
virHashEntryPtr entry;
733698
for (entry = table->table[i]; entry; entry = entry->next) {
734699
if (iter(entry->payload, entry->name, data)) {
735-
table->iterating = false;
736700
if (name)
737701
*name = table->keyCopy(entry->name);
738702
return entry->payload;
739703
}
740704
}
741705
}
742-
table->iterating = false;
743706

744707
return NULL;
745708
}

‎tests/virhashtest.c‎

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -221,32 +221,6 @@ testHashRemoveForEachAll(void *payload ATTRIBUTE_UNUSED,
221221
}
222222

223223

224-
const int testHashCountRemoveForEachForbidden = ARRAY_CARDINALITY(uuids);
225-
226-
static int
227-
testHashRemoveForEachForbidden(void *payload ATTRIBUTE_UNUSED,
228-
const void *name,
229-
void *data)
230-
{
231-
virHashTablePtr hash = data;
232-
size_t i;
233-
234-
for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
235-
if (STREQ(uuids_subset[i], name)) {
236-
int next = (i + 1) % ARRAY_CARDINALITY(uuids_subset);
237-
238-
if (virHashRemoveEntry(hash, uuids_subset[next]) == 0) {
239-
VIR_TEST_VERBOSE(
240-
"\nentry \"%s\" should not be allowed to be removed",
241-
uuids_subset[next]);
242-
}
243-
break;
244-
}
245-
}
246-
return 0;
247-
}
248-
249-
250224
static int
251225
testHashRemoveForEach(const void *data)
252226
{
@@ -303,61 +277,6 @@ testHashSteal(const void *data ATTRIBUTE_UNUSED)
303277
}
304278

305279

306-
static int
307-
testHashIter(void *payload ATTRIBUTE_UNUSED,
308-
const void *name ATTRIBUTE_UNUSED,
309-
void *data ATTRIBUTE_UNUSED)
310-
{
311-
return 0;
312-
}
313-
314-
static int
315-
testHashForEachIter(void *payload ATTRIBUTE_UNUSED,
316-
const void *name ATTRIBUTE_UNUSED,
317-
void *data)
318-
{
319-
virHashTablePtr hash = data;
320-
321-
if (virHashAddEntry(hash, uuids_new[0], NULL) == 0)
322-
VIR_TEST_VERBOSE("\nadding entries in ForEach should be forbidden");
323-
324-
if (virHashUpdateEntry(hash, uuids_new[0], NULL) == 0)
325-
VIR_TEST_VERBOSE("\nupdating entries in ForEach should be forbidden");
326-
327-
if (virHashSteal(hash, uuids_new[0]) != NULL)
328-
VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
329-
330-
if (virHashSteal(hash, uuids_new[0]) != NULL)
331-
VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
332-
333-
if (virHashForEach(hash, testHashIter, NULL) >= 0)
334-
VIR_TEST_VERBOSE("\niterating through hash in ForEach"
335-
" should be forbidden");
336-
return 0;
337-
}
338-
339-
static int
340-
testHashForEach(const void *data ATTRIBUTE_UNUSED)
341-
{
342-
virHashTablePtr hash;
343-
int ret = -1;
344-
345-
if (!(hash = testHashInit(0)))
346-
return -1;
347-
348-
if (virHashForEach(hash, testHashForEachIter, hash)) {
349-
VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries");
350-
goto cleanup;
351-
}
352-
353-
ret = 0;
354-
355-
cleanup:
356-
virHashFree(hash);
357-
return ret;
358-
}
359-
360-
361280
static int
362281
testHashRemoveSetIter(const void *payload ATTRIBUTE_UNUSED,
363282
const void *name,
@@ -628,9 +547,7 @@ mymain(void)
628547
DO_TEST("Remove", Remove);
629548
DO_TEST_DATA("Remove in ForEach", RemoveForEach, Some);
630549
DO_TEST_DATA("Remove in ForEach", RemoveForEach, All);
631-
DO_TEST_DATA("Remove in ForEach", RemoveForEach, Forbidden);
632550
DO_TEST("Steal", Steal);
633-
DO_TEST("Forbidden ops in ForEach", ForEach);
634551
DO_TEST("RemoveSet", RemoveSet);
635552
DO_TEST("Search", Search);
636553
DO_TEST("GetItems", GetItems);

0 commit comments

Comments
 (0)