]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Hash test spurious fail fixed
authorMaria Matejka <mq@ucw.cz>
Mon, 25 Nov 2024 08:44:00 +0000 (09:44 +0100)
committerMaria Matejka <mq@ucw.cz>
Mon, 25 Nov 2024 08:44:00 +0000 (09:44 +0100)
There was a race condition in the test itself,
causing the test reader access data after free.

lib/hash_test.c

index dc0c067e63a249ef6f1f83b57608dc3b431d2b60..33b9ac857aa9265664e213dd7d4d9dc419fa9a25 100644 (file)
@@ -359,12 +359,14 @@ st_find_thread(void *_v)
 
   uint skip = st_skip[atomic_fetch_add_explicit(&st_skip_pos, 1, memory_order_acq_rel)];
 
-  for (u64 i = 0; !atomic_load_explicit(&st_end, memory_order_relaxed); i += skip)
+  for (u64 i = 0; !atomic_load_explicit(&st_end, memory_order_acquire); i += skip)
   {
     struct st_node *n = SPINHASH_FIND(*v, ST, i % ST_MAX);
     ASSERT_DIE(!n || (n->key == i % ST_MAX));
   }
 
+  atomic_fetch_add_explicit(&st_end, 1, memory_order_release);
+
   rcu_thread_stop();
   return NULL;
 }
@@ -398,6 +400,11 @@ st_update_thread(void *_v)
 
   atomic_store_explicit(&st_end, 1, memory_order_release);
 
+  /* Wait for readers to properly end before releasing the memory,
+   * as the hash nodes may be accessed even after removed from hash */
+  while (atomic_load_explicit(&st_end, memory_order_acquire) < ST_READERS + 1)
+    birdloop_yield();
+
   rcu_thread_stop();
   return NULL;
 }