Skip to content

QUEUE- and Unix-backend-related improvements, bugfixes, and tests - #557

Closed
ahippo wants to merge 12 commits into
libuv:v1.xfrom
ahippo:v1.x-uv-queue
Closed

ahippo wants to merge 12 commits into
libuv:v1.xfrom
ahippo:v1.x-uv-queue

Conversation

@ahippo

@ahippo ahippo commented Oct 1, 2015

Copy link
Copy Markdown

There are 10 commits in this pull request (as of now).
They are related but somewhat different in purpose and impact.
Some of them break the ABI (by changing struct layouts and sizes), most do not.
So it may be worth splitting the pull request into several smaller ones.
But it would be good to hear some feedback first.

All these changes are compile-tested on Linux only, so they can break builds for other OSes (but shouldn't).

Impact/severity of the changes.

sha title type ABI impact misc
511fed6 QUEUE: more type-checking, move queue.h to include/ improvement no makes QUEUE public
3f90ba8 QUEUE: convert macros to inline functions where possible improvement no
7edc205 QUEUE: introduce QUEUE_FOREACH_SAFE() and QUEUE_REMOVE_SAFE() improvement no needed for subsequent patches
5392692 QUEUE: poison prev/next pointers in QUEUE_REMOVE() and QUEUE_ADD() improvement no crashes tests and libuv unless the following 2 patches are applied
1d0afd0 unix: allow calling uv_fs_event_stop() from fs_event callback bugfix no
21fb51b unix: allow calling uv_close() from uv_{prepare,idle,check,async}_t callback bugfix breaks ABI
caf20f9 test: test that it's safe to call uv_close() from callbacks test no for future regressions
2a0ec7e test: test that uv_close() doesn't corrupt internal QUEUE test no for changesets 1d0afd0 and 21fb51b, crashes/fails without these two changesets applied
cb5d8d7 unix: use QUEUE for closing_handles improvement breaks ABI
a42e641 unix: use QUEUE_FOREACH_SAFE() in process.c improvement no

Andrey Mazo added 10 commits October 1, 2015 14:01
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 a bit.

As a consequence, move it to include/ as it's included by uv.h.

This should not break the ABI as sizeof(QUEUE) is unchanged and
memory layout is the same too.
Inline functions provide more type-checking, are as fast as macros, and
are a bit easier to debug.

This should not break the ABI.
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-code a similar thing in src/win/loop-watcher.c, for example.
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).

WARNING: this makes libuv segfault as it accesses an element's "next"
after calling QUEUE_REMOVE() on it.
Before this, calling uv_fs_event_stop() led to a segfault under certain
conditions.

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().
…allback

Before this, calling uv_{prepare,idle,check}_stop() or uv_close()
led to spurios callbacks execution under certain conditions or other bad
things.

The callstack is as follows:

mainloop
   |
  \/
uv__{prepare,idle,check}_run() or uv__async_event()
   |
  \/
QUEUE_FOREACH()                         <--- [1]
   |
  \/
user's callback
   |
  \/
uv_close() (or uv_{prepare,idle,check}_stop())
   |
  \/
uv__{prepare,idle,check,async}_close()
   |
  \/
uv_{prepare,idle,check}_stop() (this is skipped for uv_async_t)
   |
  \/
QUEUE_REMOVE()                          <--- [2]

Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH()
[1], which leads the next loop iteration dereferencing 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().

WARNING: this changes the ABI as it adds more fields to uv_loop_t.
This is inspired by recent QUEUE_REMOVE()-within-QUEUE_FOREACH() bugs.
Currently, certain handle types are stored in a QUEUE internally but
they delay the actual QUEUE_REMOVE() until all user's callbacks
terminate (by maintaining handles in UV_CLOSING state).
So, these types of handles shouldn't be affected by the aforementioned bugs.

Add this testcase to make sure, we don't have a regression in the future.

The test adds all possible types of handles to a loop and closes all of them
during the loop iteration using uv_walk()+uv_close().
The test adds all types of handles, that are known to be affected by
QUEUE_REMOVE()-within-QUEUE_FOREACH() bug, to a loop.
It then calls uv_close() to trigger QUEUE_REMOVE()-within-QUEUE_FOREACH() case
and checks whether a particular QUEUE is corrupted or not.
We have a careful QUEUE implementation, so use it instead of open-coding
a similar functionality.

WARNING: This breaks the ABI by adding one more field to uv_loop_t.
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using
careful while() and QUEUE_NEXT().
@saghul

saghul commented Oct 1, 2015

Copy link
Copy Markdown
Member

Wow, a lot to look into, thanks! I'm about to leave on vacation, so hopefully @bnoordhuis has some time to look into this.

A quick note though: we promised a stable ABI throughout libuv v1.x so the ABI breaking changes would not be acceptable.

@ahippo

ahippo commented Oct 1, 2015

Copy link
Copy Markdown
Author

Wow, a lot to look into, thanks! I'm about to leave on vacation, so hopefully @bnoordhuis has some time to look into this.

Oh, ok.:)

A quick note though: we promised a stable ABI throughout libuv v1.x so the ABI breaking changes would not be acceptable.

Can we merge ABI breaking things into master then?
Also, I consider an issue, fixed in 21fb51b, as quite serious.
Maybe you have some ideas on how to address it in an ABI-compatible manner?
(I followed the way Win-backend solves the issue)

@saghul

saghul commented Oct 2, 2015

Copy link
Copy Markdown
Member

Can we merge ABI breaking things into master then?

Yep, that's ok.

Also, I consider an issue, fixed in 21fb51b, as quite serious.

Can you demonstrate it in a test case? IIRC while we're "safe" because the close callback is called always on the next loop iteration, so nobody will ever hold a pointer to freed memory, for instance.

Maybe you have some ideas on how to address it in an ABI-compatible manner?
(I followed the way Win-backend solves the issue)

We also have some extra fields for avoiding ABI breakage, so feel free to get creative there and we can discuss the approach!

@ahippo

ahippo commented Oct 2, 2015

Copy link
Copy Markdown
Author

Can we merge ABI breaking things into master then?

Yep, that's ok.

Cool, I'll split the pull request in two parts then: for master and for v1.x.
(after we figure out what to do with 21fb51b)

Also, I consider an issue, fixed in 21fb51b, as quite serious.

Can you demonstrate it in a test case? IIRC while we're "safe" because the close callback is called always on the next loop iteration, so nobody will ever hold a pointer to freed memory, for instance.

You're right that most likely we don't access already freed memory and thus are not susceptible to segfaults.
However, we still use an element not longer being in a queue for iterating over the queue, which can lead to strange consequences.
I came up with a test-case, which is implemented in 2a0ec7e "test that uv_close() ...": calling uv_close() on the current handle and on the next one from the handle's callback leads to a spurious callback invocation of an already stopped/closed next handle.
It's not that serious as accessing freed memory, of course, but is still quite undesirable.
And there could be other cases, that I can't see now.

Another point is that we can't merge queue poisoning patch (5392692), which should help in detecting libuv misuse bugs early, before fixing this QUEUE_REMOVE()-within-QUEUE_FOREACH() issue.

Maybe you have some ideas on how to address it in an ABI-compatible manner?
(I followed the way Win-backend solves the issue)

We also have some extra fields for avoiding ABI breakage, so feel free to get creative there and we can discuss the approach!

Thank you for the hint!
I don't see "reserved[4]" in uv_loop_t and I'm not creative enough, so I came up with the following.
I need one additional pointer per queue -- I can try to dynamically allocate it and store it as a first element of the queue.
It's not the best thing to do as it complicates internal queue handling a bit, but should maintain the stable ABI.

It just poisons prev/next pointers as the QUEUE is statically allocated.
@ahippo

ahippo commented Oct 2, 2015

Copy link
Copy Markdown
Author

I need one additional pointer per queue -- I can try to dynamically allocate it and store it as a first element of the queue.
It's not the best thing to do as it complicates internal queue handling a bit, but should maintain the stable ABI.

So, I did this in 612c967.
It should keep the ABI stable.
If the approach is fine, I'll go ahead and create two separate pull requests: for master and for v1.x, with some commits rearranged.
If you like the approach, should I redo 1d0afd0 using it for consistency?

…}_t callback

This is a rewrite of
21fb51b
"unix: allow calling uv_close() from uv_{prepare,idle,check,async}_t callback"
in an ABI-preserving manner.
Instead of adding extra fields to uv_loop_t (which breaks the ABI),
add a special first element to a corresponding queue and
keep required information there.
Consequently, add several special functions and macros for working with
these special QUEUEs with special first element.
Unfortunately, normal QUEUEs and these special QUEUEs have the same type,
so extra care should be taken to use proper functions/macros.

Before this changeset, calling uv_{prepare,idle,check}_stop() or uv_close()
led to spurios callbacks execution under certain conditions or other bad
things.

The callstack is as follows:

mainloop
   |
  \/
uv__{prepare,idle,check}_run() or uv__async_event()
   |
  \/
QUEUE_FOREACH()                         <--- [1]
   |
  \/
user's callback
   |
  \/
uv_close() (or uv_{prepare,idle,check}_stop())
   |
  \/
uv__{prepare,idle,check,async}_close()
   |
  \/
uv_{prepare,idle,check}_stop() (this is skipped for uv_async_t)
   |
  \/
QUEUE_REMOVE()                          <--- [2]

Eventually, QUEUE_REMOVE() [2] is called from within QUEUE_FOREACH()
[1], which leads the next loop iteration dereferencing 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().
@ahippo

ahippo commented Oct 9, 2015

Copy link
Copy Markdown
Author

A similar poisoning idea and queue_foreach issue was reported in
joyent/libuv#646

@ahippo

ahippo commented Oct 16, 2015

Copy link
Copy Markdown
Author

As this pull request conflicts with QUEUE_MOVE() changeset 442b8a5 in v1.x, I've created a follow-up pull request #577 with those conflicts resolved.
So, I'm closing this pull request as it's now obsolete.

@ahippo ahippo closed this Oct 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants