Skip to content

QUEUE- and Unix-backend-related improvements, bugfixes, and tests. Version 4 - #620

Closed
ahippo wants to merge 9 commits into
libuv:masterfrom
ahippo:v1.x-uv-queue-v4
Closed

ahippo wants to merge 9 commits into
libuv:masterfrom
ahippo:v1.x-uv-queue-v4

Conversation

@ahippo

@ahippo ahippo commented Nov 17, 2015

Copy link
Copy Markdown

This is an improved version of #577, which instead of exposing the whole queue.h to a user and making it a part of the public API, exposes only the absolutely necessary part of queue.h (struct QUEUE declaration).
This pull request also incorporates comments raised as part of review request #577.
Everything else is identical to #577.

I didn't force-push new changesets to #577 as this would wipe many comments there because they are tied to particular changesets.

@ahippo

ahippo commented Sep 8, 2016

Copy link
Copy Markdown
Author

@saghul, since this PR is marked as v2, should I rebase it on top of master instead of v1.x?
Then I can also include commit cb5d8d7 (which breaks ABI) into the PR.

@txdv

txdv commented Sep 9, 2016

Copy link
Copy Markdown
Contributor

O yeah, I remember this PR. It was really awesome, the inline instead of the do while seems a modern approach to do while preprocessor magic. Hopefully @saghul has more time to review it this time!

@saghul

saghul commented Sep 9, 2016

Copy link
Copy Markdown
Member

@ahippo doh, this fell through the cracks, I'm afraid! Yeah, please rebase on top of master since we're now getting started with v2 stuff.

@saghul saghul added this to the v2.0.0 milestone Sep 9, 2016
@ahippo

ahippo commented Sep 9, 2016

Copy link
Copy Markdown
Author

O yeah, I remember this PR. It was really awesome, the inline instead of the do while seems a modern approach to do while preprocessor magic. Hopefully @saghul has more time to review it this time!

Thank you!
At least, it seemed more debug-able to me in case of a queue corruption.

@ahippo doh, this fell through the cracks, I'm afraid! Yeah, please rebase on top of master since we're now getting started with v2 stuff.

No worries, it was quite intrusive for a stable branch.
Ok, I'll rebase on top of master and update the pull request.

ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
Make QUEUE a struct instead of an array of pointers.
This adds some minimal type-checking to QUEUE_*() macros.
Also, it makes the purpose of fields a bit clearer
(it's not that obvious that "void* active_reqs[2]" is not an arbitrary array
of void* pointers but is, in fact, a QUEUE).
Also, this aids in debugging queue-related issues.

As a consequence, factor out public uv-queue.h header with QUEUE
definition from queue.h.
queue.h still contains the actual implementation.

Drop now unneeded QUEUE_{NEXT,PREV}() macros and friends as
q->next or q->prev is shorter and clearer (especially, when used as lvalue).

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
Inline functions provide more type-checking, are as fast as macros, and
are easier to debug.

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
These macros/functions should be used to remove elements from a queue
while iterating over it.
This turns out to be a common use-case for QUEUE.
For example, calling uv_close() on a handle from the handle's callback
ends up calling QUEUE_REMOVE() from within QUEUE_FOREACH(),
which is unsafe if special care is not taken.

Win backend open-codes a similar thing in src/win/loop-watcher.c, for example.

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
It's bad to leave dangling pointers in QUEUE operations.
Most QUEUE operations update all involved queue elements' prev/next pointers to be
valid, except for QUEUE_ADD() and QUEUE_REMOVE().
These operations leave dangling pointers behind.
So, explicitly poison pointers that shouldn't be used after QUEUE_ADD()
and QUEUE_REMOVE() calls.
This greatly aids in debugging as it causes an immediate segfault
upon dereference of these pointers instead of making havoc down the road
(like infinite loops over the queue, at best).

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
Introduce QUEUE_DESTROY() as a complementary operation to QUEUE_INIT().
For now, it just poisons prev/next pointers as the QUEUE is statically allocated.

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
The following commits added a stripped-down version of this test to avoid
test failures on platforms other than Linux:
6060841 test: test that uv_close() doesn't corrupt QUEUE
4634376 unix: fix uv_fs_event_stop() from fs_event_cb

Finally, add checking of asyncs that was omitted.

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
…event callback

Revert
4634376 unix: fix uv_fs_event_stop() from fs_event_cb

Instead of using an ad-hoc trick with additional variable to indicate if
a queue is being iterated over, and actually implementing QUEUE_REMOVE_SAFE() macro
for this particular queue,
use generic QUEUE_FOREACH_SAFE() and QUEUE_REMOVE_SAFE() pair of macros
to handle queue modifications from within user callbacks while
iterating over the queue.

The callstack is as follows:

uv__inotify_read()
   |
  \/
QUEUE_FOREACH()                         <--- [1]
   |
  \/
user's callback
   |
  \/
uv_close() (or uv_fs_event_stop())
   |
  \/
uv__fs_event_close()
   |
  \/
uv_fs_event_stop()
   |
  \/
QUEUE_REMOVE()                          <--- [2]

Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH()
[1], which leads the next loop iteration to dereference a dangling
pointer.

Use "safe" versions of appropriate macros with a shared iterator, so
that QUEUE_REMOVE_SAFE() can update it in case someone is removing an
element from under out feet.

The use of an elements "next" pointer after the element is removed from
a queue is found using "next" pointer poisoning in QUEUE_REMOVE().

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using
careful while() and q->next.

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
We have a careful QUEUE implementation, so use it instead of open-coding
a similar functionality.

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
…llback

Revert
4634376 unix: fix uv_fs_event_stop() from fs_event_cb

Instead of using an ad-hoc trick with additional variable to indicate if
a queue is being iterated over, and actually implementing QUEUE_REMOVE_SAFE() macro
for this particular queue,
use generic QUEUE_FOREACH_SAFE() and QUEUE_REMOVE_SAFE() pair of macros
to handle queue modifications from within user callbacks while
iterating over the queue.

The callstack is as follows:

uv__inotify_read()
   |
  \/
QUEUE_FOREACH()                         <--- [1]
   |
  \/
user's callback
   |
  \/
uv_close() (or uv_fs_event_stop())
   |
  \/
uv__fs_event_close()
   |
  \/
uv_fs_event_stop()
   |
  \/
QUEUE_REMOVE()                          <--- [2]

Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH()
[1], which leads the next loop iteration to dereference a dangling
pointer.

Use "safe" versions of appropriate macros with a shared iterator, so
that QUEUE_REMOVE_SAFE() can update it in case someone is removing an
element from under out feet.

The use of an elements "next" pointer after the element is removed from
a queue is found using "next" pointer poisoning in QUEUE_REMOVE().

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using
careful while() and q->next.

PR-URL: libuv#620
ahippo pushed a commit to ahippo/libuv that referenced this pull request Sep 9, 2016
We have a careful QUEUE implementation, so use it instead of open-coding
a similar functionality.

PR-URL: libuv#620
@ahippo
ahippo changed the base branch from v1.x to master September 9, 2016 17:19
@ahippo

ahippo commented Sep 9, 2016 •

Copy link
Copy Markdown
Author

@saghul, I've just rebased my changes on top of master branch.
My branch is now confusingly called v1.x-uv-queue-v4 although changes are meant for master not v1.x.
(I kept the old branch name in order to reuse this pull request)

Comments are welcome!
Thank you in advance!
Please, let me know if you have any questions.

@cjihrig

cjihrig commented Mar 3, 2018

Copy link
Copy Markdown
Contributor

@ahippo are you interested in picking this back up? It has a number of conflicts at the moment.

@ahippo

ahippo commented Mar 5, 2018

Copy link
Copy Markdown
Author

@ahippo are you interested in picking this back up? It has a number of conflicts at the moment.

@cjihrig, yes, I'm still interested.
Should I rebase it on top of the current master?

@cjihrig

cjihrig commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

Yes, I think that's a good place to start.

@ahippo

ahippo commented Mar 7, 2018

Copy link
Copy Markdown
Author

Yes, I think that's a good place to start.

OK, I'll work on rebasing the PR on top of current master, and will update the PR.

Andrey Mazo added 2 commits March 23, 2018 04:55
Make QUEUE a struct instead of an array of pointers.
This adds some minimal type-checking to QUEUE_*() macros.
Also, it makes the purpose of fields a bit clearer
(it's not that obvious that "void* active_reqs[2]" is not an arbitrary array
of void* pointers but is, in fact, a QUEUE).
Also, this aids in debugging queue-related issues.

As a consequence, factor out public uv-queue.h header with QUEUE
definition from queue.h.
queue.h still contains the actual implementation.

Drop now unneeded QUEUE_{NEXT,PREV}() macros and friends as
q->next or q->prev is shorter and clearer (especially, when used as lvalue).

PR-URL: libuv#620
Inline functions provide more type-checking, are as fast as macros, and
are easier to debug.

PR-URL: libuv#620
Andrey Mazo added 7 commits March 23, 2018 04:55
These macros/functions should be used to remove elements from a queue
while iterating over it.
This turns out to be a common use-case for QUEUE.
For example, calling uv_close() on a handle from the handle's callback
ends up calling QUEUE_REMOVE() from within QUEUE_FOREACH(),
which is unsafe if special care is not taken.

Win backend open-codes a similar thing in src/win/loop-watcher.c, for example.

PR-URL: libuv#620
It's bad to leave dangling pointers in QUEUE operations.
Most QUEUE operations update all involved queue elements' prev/next pointers to be
valid, except for QUEUE_ADD() and QUEUE_REMOVE().
These operations leave dangling pointers behind.
So, explicitly poison pointers that shouldn't be used after QUEUE_ADD()
and QUEUE_REMOVE() calls.
This greatly aids in debugging as it causes an immediate segfault
upon dereference of these pointers instead of making havoc down the road
(like infinite loops over the queue, at best).

PR-URL: libuv#620
Introduce QUEUE_DESTROY() as a complementary operation to QUEUE_INIT().
For now, it just poisons prev/next pointers as the QUEUE is statically allocated.

PR-URL: libuv#620
The following commits added a stripped-down version of this test to avoid
test failures on platforms other than Linux:
6060841 test: test that uv_close() doesn't corrupt QUEUE
4634376 unix: fix uv_fs_event_stop() from fs_event_cb

Finally, add checking of asyncs that was omitted.

PR-URL: libuv#620
…llback

Revert
4634376 unix: fix uv_fs_event_stop() from fs_event_cb

Instead of using an ad-hoc trick with additional variable to indicate if
a queue is being iterated over, and actually implementing QUEUE_REMOVE_SAFE() macro
for this particular queue,
use generic QUEUE_FOREACH_SAFE() and QUEUE_REMOVE_SAFE() pair of macros
to handle queue modifications from within user callbacks while
iterating over the queue.

The callstack is as follows:

uv__inotify_read()
   |
  \/
QUEUE_FOREACH()                         <--- [1]
   |
  \/
user's callback
   |
  \/
uv_close() (or uv_fs_event_stop())
   |
  \/
uv__fs_event_close()
   |
  \/
uv_fs_event_stop()
   |
  \/
QUEUE_REMOVE()                          <--- [2]

Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH()
[1], which leads the next loop iteration to dereference a dangling
pointer.

Use "safe" versions of appropriate macros with a shared iterator, so
that QUEUE_REMOVE_SAFE() can update it in case someone is removing an
element from under out feet.

Similarly, use QUEUE_FOREACH_SAFE() in uv__inotify_fork().

The use of an elements "next" pointer after the element is removed from
a queue was found using "next" pointer poisoning in QUEUE_REMOVE().

PR-URL: libuv#620
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using
careful while() and q->next.

PR-URL: libuv#620
We have a careful QUEUE implementation, so use it instead of open-coding
a similar functionality.

PR-URL: libuv#620
@ahippo
ahippo force-pushed the v1.x-uv-queue-v4 branch from d6ec3e6 to 320d673 Compare March 23, 2018 04:05
@ahippo

ahippo commented Mar 23, 2018

Copy link
Copy Markdown
Author

@cjihrig , I've rebased my changes on top of the current master.
I ran tests on my Linux box, and it seems, I didn't break anything.
Please, let me know if you have any questions/comments/suggestions.

@ahippo

ahippo commented Apr 6, 2018

Copy link
Copy Markdown
Author

@cjihrig , are there any comments/questions about the changes?

@cjihrig cjihrig left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple comments. To be honest, I'm still not entirely sure what the end goal of this PR is. Would it make more sense to split it up into smaller, more easily digestible PRs?

I'd also like @bnoordhuis and @santigimeno to weigh in.

Comment thread include/uv/queue.h
#ifndef UV_QUEUE_H_
#define UV_QUEUE_H_

#ifdef __cplusplus

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these blocks necessary?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really most likely.
uv/queue.h can be included from C++ code via uv.h only, which has these extern "C" guards.
Also, uv/queue.h only defines a POD struct now, so there should be no difference C vs C++.
I just put extern "C" to be on a safer side as they don't hurt.
I'll remove them.

Comment thread src/queue.h
(q) != (h); \
(q) = (n), (n) = (n)->next)

static inline int QUEUE_EMPTY(const QUEUE *q) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you align the pointer stars to the left throughout.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing.
I'll post an updated version after gathering some more comments.

@ahippo

ahippo commented Apr 7, 2018 •

Copy link
Copy Markdown
Author

Left a couple comments. To be honest, I'm still not entirely sure what the end goal of this PR is. Would it make more sense to split it up into smaller, more easily digestible PRs?

GitHub shows commits in a wrong order, which makes it even harder to understand what's going on.
I can definitely split the PR into multiple PRs if that would help with the review.
Is this what you want me to do or first wait for others to review the PR as is?

There are four goals overall:

  1. Convert queue implementation to a struct and inline functions to make debugging and maintenance easier. This also makes the related code a bit more self-documented.
  2. Increase coverage of the queue test.
  3. Use pretty standard QUEUE_FOREACH_SAFE() for iterating over a queue while possibly removing elements from it.
  4. Use QUEUE and related machinery in several places replacing an open-coded implementation.

@bnoordhuis

Copy link
Copy Markdown
Member

Closing because this PR is unfortunately very stale.

@bnoordhuis bnoordhuis closed this Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants