Repository navigation
Conversation
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().
|
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. |
Oh, ok.:)
Can we merge ABI breaking things into master then? |
Yep, that's ok.
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.
We also have some extra fields for avoiding ABI breakage, so feel free to get creative there and we can discuss the approach! |
Cool, I'll split the pull request in two parts then: for master and for v1.x.
You're right that most likely we don't access already freed memory and thus are not susceptible to segfaults. 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.
Thank you for the hint! |
It just poisons prev/next pointers as the QUEUE is statically allocated.
So, I did this in 612c967. |
…}_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().
|
A similar poisoning idea and queue_foreach issue was reported in |
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.