-
Notifications
You must be signed in to change notification settings - Fork 944
add safe iterator tracking in hashtable. prevents invalid access on hashtable delete #2807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -284,6 +284,9 @@ typedef struct hashtableBucket { | |
| /* A key property is that the bucket size is one cache line. */ | ||
| static_assert(sizeof(bucket) == HASHTABLE_BUCKET_SIZE, "Bucket size mismatch"); | ||
|
|
||
| /* Forward declaration for iter type */ | ||
| typedef struct iter iter; | ||
|
|
||
| struct hashtable { | ||
| hashtableType *type; | ||
| ssize_t rehash_idx; /* -1 = rehashing not in progress. */ | ||
|
|
@@ -293,10 +296,11 @@ struct hashtable { | |
| int16_t pause_rehash; /* Non-zero = rehashing is paused */ | ||
| int16_t pause_auto_shrink; /* Non-zero = automatic resizing disallowed. */ | ||
| size_t child_buckets[2]; /* Number of allocated child buckets. */ | ||
| iter *safe_iterators; /* Head of linked list of safe iterators */ | ||
| void *metadata[]; | ||
| }; | ||
|
|
||
| typedef struct { | ||
| struct iter { | ||
| hashtable *hashtable; | ||
| bucket *bucket; | ||
| long index; | ||
|
|
@@ -309,7 +313,8 @@ typedef struct { | |
| /* Safe iterator temporary storage for bucket chain compaction. */ | ||
| uint64_t last_seen_size; | ||
| }; | ||
| } iter; | ||
| iter *next_safe_iter; /* Next safe iterator in hashtable's list */ | ||
| }; | ||
|
|
||
| /* The opaque hashtableIterator is defined as a blob of bytes. */ | ||
| static_assert(sizeof(hashtableIterator) >= sizeof(iter), | ||
|
|
@@ -1084,6 +1089,37 @@ static inline int shouldPrefetchValues(iter *iter) { | |
| return (iter->flags & HASHTABLE_ITER_PREFETCH_VALUES); | ||
| } | ||
|
|
||
| /* Add a safe iterator to the hashtable's tracking list */ | ||
| static void trackSafeIterator(iter *it) { | ||
| assert(it->next_safe_iter == NULL); | ||
| hashtable *ht = it->hashtable; | ||
| it->next_safe_iter = ht->safe_iterators; | ||
| ht->safe_iterators = it; | ||
| } | ||
|
|
||
| /* Remove a safe iterator from the hashtable's tracking list */ | ||
| static void untrackSafeIterator(iter *it) { | ||
| hashtable *ht = it->hashtable; | ||
| if (ht->safe_iterators == it) { | ||
| ht->safe_iterators = it->next_safe_iter; | ||
| } else { | ||
| iter *current = ht->safe_iterators; | ||
| assert(current != NULL); | ||
| while (current->next_safe_iter != it) { | ||
| current = current->next_safe_iter; | ||
| assert(current != NULL); | ||
| } | ||
| current->next_safe_iter = it->next_safe_iter; | ||
| } | ||
| it->next_safe_iter = NULL; | ||
| it->hashtable = NULL; /* Mark as invalid */ | ||
| } | ||
|
|
||
| /* Invalidate all safe iterators by setting hashtable = NULL */ | ||
| static void invalidateAllSafeIterators(hashtable *ht) { | ||
| while (ht->safe_iterators) untrackSafeIterator(ht->safe_iterators); | ||
| } | ||
|
|
||
| /* --- API functions --- */ | ||
|
|
||
| /* Allocates and initializes a new hashtable specified by the given type. */ | ||
|
|
@@ -1098,6 +1134,7 @@ hashtable *hashtableCreate(hashtableType *type) { | |
| ht->rehash_idx = -1; | ||
| ht->pause_rehash = 0; | ||
| ht->pause_auto_shrink = 0; | ||
| ht->safe_iterators = NULL; | ||
| resetTable(ht, 0); | ||
| resetTable(ht, 1); | ||
| if (type->trackMemUsage) type->trackMemUsage(ht, alloc_size); | ||
|
|
@@ -1153,6 +1190,7 @@ void hashtableEmpty(hashtable *ht, void(callback)(hashtable *)) { | |
|
|
||
| /* Deletes all the entries and frees the table. */ | ||
| void hashtableRelease(hashtable *ht) { | ||
| invalidateAllSafeIterators(ht); | ||
| hashtableEmpty(ht, NULL); | ||
| /* Call trackMemUsage before zfree, so trackMemUsage can access ht. */ | ||
| if (ht->type->trackMemUsage) { | ||
|
|
@@ -1242,6 +1280,7 @@ static void hashtablePauseRehashing(hashtable *ht) { | |
| /* Resumes incremental rehashing, after pausing it. */ | ||
| static void hashtableResumeRehashing(hashtable *ht) { | ||
| ht->pause_rehash--; | ||
| assert(ht->pause_rehash >= 0); | ||
| hashtableResumeAutoShrink(ht); | ||
| } | ||
|
|
||
|
|
@@ -1962,7 +2001,9 @@ size_t hashtableScanDefrag(hashtable *ht, size_t cursor, hashtableScanFunction f | |
| * rehashing to prevent entries from moving around. It's allowed to insert and | ||
| * replace entries. Deleting entries is only allowed for the entry that was just | ||
| * returned by hashtableNext. Deleting other entries is possible, but doing so | ||
| * can cause internal fragmentation, so don't. | ||
| * can cause internal fragmentation, so don't. The hash table itself can be | ||
| * safely deleted while safe iterators exist - they will be invalidated and | ||
| * subsequent calls to hashtableNext will return false. | ||
| * | ||
| * Guarantees for safe iterators: | ||
| * | ||
|
|
@@ -1978,7 +2019,7 @@ size_t hashtableScanDefrag(hashtable *ht, size_t cursor, hashtableScanFunction f | |
| * - Entries that are inserted during the iteration may or may not be returned | ||
| * by the iterator. | ||
| * | ||
| * Call hashtableNext to fetch each entry. You must call hashtableResetIterator | ||
| * Call hashtableNext to fetch each entry. You must call hashtableCleanupIterator | ||
| * when you are done with the iterator. | ||
| */ | ||
| void hashtableInitIterator(hashtableIterator *iterator, hashtable *ht, uint8_t flags) { | ||
|
|
@@ -1988,22 +2029,31 @@ void hashtableInitIterator(hashtableIterator *iterator, hashtable *ht, uint8_t f | |
| iter->table = 0; | ||
| iter->index = -1; | ||
| iter->flags = flags; | ||
| iter->next_safe_iter = NULL; | ||
| if (isSafe(iter) && ht != NULL) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kvstore's iterator contains a hashtableIterator, and in |
||
| trackSafeIterator(iter); | ||
| } | ||
| } | ||
|
|
||
| /* Reinitializes the iterator for the provided hashtable while | ||
| * preserving the flags from its previous initialization. */ | ||
| void hashtableReinitIterator(hashtableIterator *iterator, hashtable *ht) { | ||
| /* Reinitializes the iterator to begin a new iteration of the provided hashtable | ||
| * while preserving the flags from its previous initialization. */ | ||
| void hashtableRetargetIterator(hashtableIterator *iterator, hashtable *ht) { | ||
| iter *iter = iteratorFromOpaque(iterator); | ||
| hashtableInitIterator(iterator, ht, iter->flags); | ||
| uint8_t flags = iter->flags; | ||
|
|
||
| hashtableCleanupIterator(iterator); | ||
| hashtableInitIterator(iterator, ht, flags); | ||
| } | ||
|
|
||
| /* Resets a stack-allocated iterator. */ | ||
| void hashtableResetIterator(hashtableIterator *iterator) { | ||
| /* Performs required cleanup for a stack-allocated iterator. */ | ||
| void hashtableCleanupIterator(hashtableIterator *iterator) { | ||
| iter *iter = iteratorFromOpaque(iterator); | ||
| if (iter->hashtable == NULL) return; | ||
|
|
||
| if (!(iter->index == -1 && iter->table == 0)) { | ||
| if (isSafe(iter)) { | ||
| hashtableResumeRehashing(iter->hashtable); | ||
| assert(iter->hashtable->pause_rehash >= 0); | ||
| untrackSafeIterator(iter); | ||
| } else { | ||
| assert(iter->fingerprint == hashtableFingerprint(iter->hashtable)); | ||
| } | ||
|
|
@@ -2021,7 +2071,7 @@ hashtableIterator *hashtableCreateIterator(hashtable *ht, uint8_t flags) { | |
| /* Resets and frees the memory of an allocated iterator, i.e. one created using | ||
| * hashtableCreate(Safe)Iterator. */ | ||
| void hashtableReleaseIterator(hashtableIterator *iterator) { | ||
| hashtableResetIterator(iterator); | ||
| hashtableCleanupIterator(iterator); | ||
| iter *iter = iteratorFromOpaque(iterator); | ||
| zfree(iter); | ||
| } | ||
|
|
@@ -2030,6 +2080,9 @@ void hashtableReleaseIterator(hashtableIterator *iterator) { | |
| * Returns false if there are no more entries. */ | ||
| bool hashtableNext(hashtableIterator *iterator, void **elemptr) { | ||
| iter *iter = iteratorFromOpaque(iterator); | ||
| /* Check if iterator has been invalidated */ | ||
| if (iter->hashtable == NULL) return false; | ||
|
|
||
| while (1) { | ||
| if (iter->index == -1 && iter->table == 0) { | ||
| /* It's the first call to next. */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.