Blame SOURCES/libvirt-util-don-t-check-for-parallel-iteration-in-hash-related-functions.patch

c11cae
From fa01d1d69ceeb4da685e6f39d11e9f5d05201589 Mon Sep 17 00:00:00 2001
c11cae
Message-Id: <fa01d1d69ceeb4da685e6f39d11e9f5d05201589@dist-git>
c11cae
From: Vincent Bernat <vincent@bernat.im>
c11cae
Date: Wed, 23 May 2018 12:45:29 +0200
c11cae
Subject: [PATCH] util: don't check for parallel iteration in hash-related
c11cae
 functions
c11cae
c11cae
RHEL-7.6: https://bugzilla.redhat.com/show_bug.cgi?id=1576464
c11cae
RHEL-7.5.z: https://bugzilla.redhat.com/show_bug.cgi?id=1581364
c11cae
c11cae
This is the responsability of the caller to apply the correct lock
c11cae
before using these functions. Moreover, the use of a simple boolean
c11cae
was still racy: two threads may check the boolean and "lock" it
c11cae
simultaneously.
c11cae
c11cae
Users of functions from src/util/virhash.c have to be checked for
c11cae
correctness. Lookups and iteration should hold a RO
c11cae
lock. Modifications should hold a RW lock.
c11cae
c11cae
Most important uses seem to be covered. Callers have now a greater
c11cae
responsability, notably the ability to execute some operations while
c11cae
iterating were reliably forbidden before are now accepted.
c11cae
c11cae
Signed-off-by: Vincent Bernat <vincent@bernat.im>
c11cae
(cherry picked from commit 4d7384eb9ddef2008cb0cc165eb808f74bc83d6b)
c11cae
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c11cae
c11cae
Conflicts: src/util/virhash.c: Since
c11cae
  3e7db8d3e8538bcd5deb1b111fb1233fc18831aa isn't backported,
c11cae
  macro that is being removed has misaligned line breaks. It
c11cae
  needs to be removed regardless.
c11cae
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c11cae
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
c11cae
---
c11cae
 src/util/virhash.c  | 37 --------------------
c11cae
 tests/virhashtest.c | 83 ---------------------------------------------
c11cae
 2 files changed, 120 deletions(-)
c11cae
c11cae
diff --git a/src/util/virhash.c b/src/util/virhash.c
c11cae
index 7fa2992f18..475c2b0281 100644
c11cae
--- a/src/util/virhash.c
c11cae
+++ b/src/util/virhash.c
c11cae
@@ -41,12 +41,6 @@ VIR_LOG_INIT("util.hash");
c11cae
 
c11cae
 /* #define DEBUG_GROW */
c11cae
 
c11cae
-#define virHashIterationError(ret)                                      \
c11cae
-    do {                                                                \
c11cae
-        VIR_ERROR(_("Hash operation not allowed during iteration"));   \
c11cae
-        return ret;                                                     \
c11cae
-    } while (0)
c11cae
-
c11cae
 /*
c11cae
  * A single entry in the hash table
c11cae
  */
c11cae
@@ -66,10 +60,6 @@ struct _virHashTable {
c11cae
     uint32_t seed;
c11cae
     size_t size;
c11cae
     size_t nbElems;
c11cae
-    /* True iff we are iterating over hash entries. */
c11cae
-    bool iterating;
c11cae
-    /* Pointer to the current entry during iteration. */
c11cae
-    virHashEntryPtr current;
c11cae
     virHashDataFree dataFree;
c11cae
     virHashKeyCode keyCode;
c11cae
     virHashKeyEqual keyEqual;
c11cae
@@ -339,9 +329,6 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
c11cae
     if ((table == NULL) || (name == NULL))
c11cae
         return -1;
c11cae
 
c11cae
-    if (table->iterating)
c11cae
-        virHashIterationError(-1);
c11cae
-
c11cae
     key = virHashComputeKey(table, name);
c11cae
 
c11cae
     /* Check for duplicate entry */
c11cae
@@ -551,9 +538,6 @@ virHashRemoveEntry(virHashTablePtr table, const void *name)
c11cae
     nextptr = table->table + virHashComputeKey(table, name);
c11cae
     for (entry = *nextptr; entry; entry = entry->next) {
c11cae
         if (table->keyEqual(entry->name, name)) {
c11cae
-            if (table->iterating && table->current != entry)
c11cae
-                virHashIterationError(-1);
c11cae
-
c11cae
             if (table->dataFree)
c11cae
                 table->dataFree(entry->payload, entry->name);
c11cae
             if (table->keyFree)
c11cae
@@ -593,18 +577,11 @@ virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
c11cae
     if (table == NULL || iter == NULL)
c11cae
         return -1;
c11cae
 
c11cae
-    if (table->iterating)
c11cae
-        virHashIterationError(-1);
c11cae
-
c11cae
-    table->iterating = true;
c11cae
-    table->current = NULL;
c11cae
     for (i = 0; i < table->size; i++) {
c11cae
         virHashEntryPtr entry = table->table[i];
c11cae
         while (entry) {
c11cae
             virHashEntryPtr next = entry->next;
c11cae
-            table->current = entry;
c11cae
             ret = iter(entry->payload, entry->name, data);
c11cae
-            table->current = NULL;
c11cae
 
c11cae
             if (ret < 0)
c11cae
                 goto cleanup;
c11cae
@@ -615,7 +592,6 @@ virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
c11cae
 
c11cae
     ret = 0;
c11cae
  cleanup:
c11cae
-    table->iterating = false;
c11cae
     return ret;
c11cae
 }
c11cae
 
c11cae
@@ -643,11 +619,6 @@ virHashRemoveSet(virHashTablePtr table,
c11cae
     if (table == NULL || iter == NULL)
c11cae
         return -1;
c11cae
 
c11cae
-    if (table->iterating)
c11cae
-        virHashIterationError(-1);
c11cae
-
c11cae
-    table->iterating = true;
c11cae
-    table->current = NULL;
c11cae
     for (i = 0; i < table->size; i++) {
c11cae
         virHashEntryPtr *nextptr = table->table + i;
c11cae
 
c11cae
@@ -667,7 +638,6 @@ virHashRemoveSet(virHashTablePtr table,
c11cae
             }
c11cae
         }
c11cae
     }
c11cae
-    table->iterating = false;
c11cae
 
c11cae
     return count;
c11cae
 }
c11cae
@@ -723,23 +693,16 @@ void *virHashSearch(const virHashTable *ctable,
c11cae
     if (table == NULL || iter == NULL)
c11cae
         return NULL;
c11cae
 
c11cae
-    if (table->iterating)
c11cae
-        virHashIterationError(NULL);
c11cae
-
c11cae
-    table->iterating = true;
c11cae
-    table->current = NULL;
c11cae
     for (i = 0; i < table->size; i++) {
c11cae
         virHashEntryPtr entry;
c11cae
         for (entry = table->table[i]; entry; entry = entry->next) {
c11cae
             if (iter(entry->payload, entry->name, data)) {
c11cae
-                table->iterating = false;
c11cae
                 if (name)
c11cae
                     *name = table->keyCopy(entry->name);
c11cae
                 return entry->payload;
c11cae
             }
c11cae
         }
c11cae
     }
c11cae
-    table->iterating = false;
c11cae
 
c11cae
     return NULL;
c11cae
 }
c11cae
diff --git a/tests/virhashtest.c b/tests/virhashtest.c
c11cae
index 9407f98c4b..77d724c4c6 100644
c11cae
--- a/tests/virhashtest.c
c11cae
+++ b/tests/virhashtest.c
c11cae
@@ -221,32 +221,6 @@ testHashRemoveForEachAll(void *payload ATTRIBUTE_UNUSED,
c11cae
 }
c11cae
 
c11cae
 
c11cae
-const int testHashCountRemoveForEachForbidden = ARRAY_CARDINALITY(uuids);
c11cae
-
c11cae
-static int
c11cae
-testHashRemoveForEachForbidden(void *payload ATTRIBUTE_UNUSED,
c11cae
-                               const void *name,
c11cae
-                               void *data)
c11cae
-{
c11cae
-    virHashTablePtr hash = data;
c11cae
-    size_t i;
c11cae
-
c11cae
-    for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
c11cae
-        if (STREQ(uuids_subset[i], name)) {
c11cae
-            int next = (i + 1) % ARRAY_CARDINALITY(uuids_subset);
c11cae
-
c11cae
-            if (virHashRemoveEntry(hash, uuids_subset[next]) == 0) {
c11cae
-                VIR_TEST_VERBOSE(
c11cae
-                        "\nentry \"%s\" should not be allowed to be removed",
c11cae
-                        uuids_subset[next]);
c11cae
-            }
c11cae
-            break;
c11cae
-        }
c11cae
-    }
c11cae
-    return 0;
c11cae
-}
c11cae
-
c11cae
-
c11cae
 static int
c11cae
 testHashRemoveForEach(const void *data)
c11cae
 {
c11cae
@@ -303,61 +277,6 @@ testHashSteal(const void *data ATTRIBUTE_UNUSED)
c11cae
 }
c11cae
 
c11cae
 
c11cae
-static int
c11cae
-testHashIter(void *payload ATTRIBUTE_UNUSED,
c11cae
-             const void *name ATTRIBUTE_UNUSED,
c11cae
-             void *data ATTRIBUTE_UNUSED)
c11cae
-{
c11cae
-    return 0;
c11cae
-}
c11cae
-
c11cae
-static int
c11cae
-testHashForEachIter(void *payload ATTRIBUTE_UNUSED,
c11cae
-                    const void *name ATTRIBUTE_UNUSED,
c11cae
-                    void *data)
c11cae
-{
c11cae
-    virHashTablePtr hash = data;
c11cae
-
c11cae
-    if (virHashAddEntry(hash, uuids_new[0], NULL) == 0)
c11cae
-        VIR_TEST_VERBOSE("\nadding entries in ForEach should be forbidden");
c11cae
-
c11cae
-    if (virHashUpdateEntry(hash, uuids_new[0], NULL) == 0)
c11cae
-        VIR_TEST_VERBOSE("\nupdating entries in ForEach should be forbidden");
c11cae
-
c11cae
-    if (virHashSteal(hash, uuids_new[0]) != NULL)
c11cae
-        VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
c11cae
-
c11cae
-    if (virHashSteal(hash, uuids_new[0]) != NULL)
c11cae
-        VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
c11cae
-
c11cae
-    if (virHashForEach(hash, testHashIter, NULL) >= 0)
c11cae
-        VIR_TEST_VERBOSE("\niterating through hash in ForEach"
c11cae
-                " should be forbidden");
c11cae
-    return 0;
c11cae
-}
c11cae
-
c11cae
-static int
c11cae
-testHashForEach(const void *data ATTRIBUTE_UNUSED)
c11cae
-{
c11cae
-    virHashTablePtr hash;
c11cae
-    int ret = -1;
c11cae
-
c11cae
-    if (!(hash = testHashInit(0)))
c11cae
-        return -1;
c11cae
-
c11cae
-    if (virHashForEach(hash, testHashForEachIter, hash)) {
c11cae
-        VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries");
c11cae
-        goto cleanup;
c11cae
-    }
c11cae
-
c11cae
-    ret = 0;
c11cae
-
c11cae
- cleanup:
c11cae
-    virHashFree(hash);
c11cae
-    return ret;
c11cae
-}
c11cae
-
c11cae
-
c11cae
 static int
c11cae
 testHashRemoveSetIter(const void *payload ATTRIBUTE_UNUSED,
c11cae
                       const void *name,
c11cae
@@ -628,9 +547,7 @@ mymain(void)
c11cae
     DO_TEST("Remove", Remove);
c11cae
     DO_TEST_DATA("Remove in ForEach", RemoveForEach, Some);
c11cae
     DO_TEST_DATA("Remove in ForEach", RemoveForEach, All);
c11cae
-    DO_TEST_DATA("Remove in ForEach", RemoveForEach, Forbidden);
c11cae
     DO_TEST("Steal", Steal);
c11cae
-    DO_TEST("Forbidden ops in ForEach", ForEach);
c11cae
     DO_TEST("RemoveSet", RemoveSet);
c11cae
     DO_TEST("Search", Search);
c11cae
     DO_TEST("GetItems", GetItems);
c11cae
-- 
c11cae
2.17.1
c11cae