Skip to content

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

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

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

Conversation

@ahippo

@ahippo ahippo commented Oct 16, 2015

Copy link
Copy Markdown

This is a reworked/rearranged pull request #557 after QUEUE_MOVE() change 442b8a5.
That changeset addresses some of the issues fixed in #557 but in a different way, so remove now unneeded changes from #557.

One changeset from #557 will be proposed for inclusion into master in a separate pull request.

Andrey Mazo added 10 commits October 16, 2015 15:25
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).
It just poisons prev/next pointers as the QUEUE is statically allocated.
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.
…lists"

This reverts src/unix/linux-inotify.c part of
442b8a5 "unix: use QUEUE_MOVE when iterating over lists"

The original change causes queue_foreach_delete test to fail:
`queue_foreach_delete` failed: exit code 6
Output from process `queue_foreach_delete`:
run-tests: src/unix/linux-inotify.c:244: uv_fs_event_stop: Assertion `w != ((void *)0)' failed.

Simplest test case for this:
1. create and start two uv_fs_event_t for the same path;
2. in the callback for the first one, call uv_close() on it;
3. assert/segfault while accessing the second uv_fs_event_t from uv__inotify_read().
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().
Use QUEUE_FOREACH_SAFE() instead of open-coding the same thing using
careful while() and QUEUE_NEXT().
@ahippo

ahippo commented Nov 11, 2015

Copy link
Copy Markdown
Author

@saghul, hope your vacation went great!
Have you had a chance to look at the pull request, which is an updated version of pull request #557?

@ahippo
ahippo force-pushed the v1.x-uv-queue-v2 branch 2 times, most recently from ecd338f to 2a7d515 Compare November 16, 2015 19:00
These changes are squashed in updated and rebased commits
c65a021
d9e81e1

PR-URL: libuv#577
@ahippo

ahippo commented Nov 16, 2015

Copy link
Copy Markdown
Author

@txdv, sorry for the late reply.
For now, I addressed your comments in a separate changeset 8f98ef2.
I created a new updated+rebased v1.x-uv-queue-v3 branch but didn't pushed it here because this would make the pull request useless as your comments are tied to particular commits.

So, again, the changeset 8f98ef2 is for the sake of the review request and the v1.x-uv-queue-v3 branch is for merging.
If you'd like, I can force-push the new branch here or create a new pull request.

Regarding assembler output, I'll post it shortly.

@ahippo

ahippo commented Nov 16, 2015

Copy link
Copy Markdown
Author

Regarding assembly output comparison for macro to inline function conversion.

I did my tests using gcc-5.2.0 with -O2.
For a function that simply wraps one of the QUEUE_*() calls, there is no difference in assembly output.
For example,

#include "uv-queue.h"

void do_queue_split(QUEUE *h, QUEUE *q, QUEUE *n) {
  QUEUE_SPLIT(h, q, n);
}

And that's not surprising: the compiler can't do much here except for inlining, which it no doubt does while optimizing.

For larger files (I picked src/unix/loop-watcher.c), there are some differences in the generated code.
But, as far as I can see, these differences are negligible: instructions are the same but slightly reordered.
This part of the diff is for uv_prepare_{init,start}() but there are similar diff hunks for uv_idle_{init,start}() and uv_check_{init,start}().

--- loop-watcher.s.gcc-5.2.0.macro      2015-11-16 14:58:30.619896866 -0500
+++ loop-watcher.s.gcc-5.2.0.inline     2015-11-16 14:55:05.877346236 -0500
@@ -7,24 +7,24 @@
        .globl  uv_prepare_init
        .type   uv_prepare_init, @function
 uv_prepare_init:
-.LFB63:
+.LFB72:
        .cfi_startproc
-       leaq    16(%rdi), %rax
-       movq    24(%rdi), %rdx
+       leaq    16(%rdi), %rdx
+       leaq    32(%rsi), %rax
        movq    %rdi, 8(%rsi)
        movl    $9, 16(%rsi)
        movl    $8192, 88(%rsi)
-       movq    %rax, 32(%rsi)
-       leaq    32(%rsi), %rax
+       movq    %rdx, 32(%rsi)
+       movq    24(%rdi), %rdx
        movq    %rdx, 40(%rsi)
        movq    %rax, (%rdx)
        movq    %rax, 24(%rdi)
-       xorl    %eax, %eax
        movq    $0, 80(%rsi)
+       xorl    %eax, %eax
        movq    $0, 96(%rsi)
        ret
        .cfi_endproc
-.LFE63:
+.LFE72:
        .size   uv_prepare_init, .-uv_prepare_init
        .section        .text.unlikely
 .LCOLDE0:
@@ -38,7 +38,7 @@
        .globl  uv_prepare_start
        .type   uv_prepare_start, @function
 uv_prepare_start:
-.LFB64:
+.LFB73:
        .cfi_startproc
        movl    88(%rdi), %eax
        testb   $64, %ah
@@ -46,17 +46,17 @@
        testq   %rsi, %rsi
        je      .L6
        movq    8(%rdi), %rdx
-       leaq    384(%rdx), %rcx
-       movq    384(%rdx), %r8
-       movq    %rcx, 112(%rdi)
        leaq    104(%rdi), %rcx
+       movq    384(%rdx), %r8
+       leaq    384(%rdx), %r9
+       movq    %r9, 112(%rdi)
        movq    %r8, 104(%rdi)
        movq    %rcx, 8(%r8)
        movq    %rcx, 384(%rdx)
        movl    %eax, %ecx
+       movq    %rsi, 96(%rdi)
        orb     $64, %ch
        testb   $32, %ah
-       movq    %rsi, 96(%rdi)
        movl    %ecx, 88(%rdi)
        je      .L10
        addl    $1, 8(%rdx)
@@ -67,7 +67,7 @@
        movl    $-22, %eax
        ret
        .cfi_endproc
-.LFE64:
+.LFE73:
        .size   uv_prepare_start, .-uv_prepare_start
        .section        .text.unlikely

I can do more tests if you'd like.

@ahippo

ahippo commented Nov 17, 2015

Copy link
Copy Markdown
Author

For clang-3.7.0 with -O2 results are a bit different for src/unix/loop-watcher.c.
uv_prepare_{init,start,stop,close}() are identical between macro and inline functions version.
Instead, uv__run_prepare() has some differences: inline version is even smaller!
(this is prepare part: idle and check parts are similar)

--- loop-watcher-prepare.s.clang-3.7.0.macro    2015-11-16 19:17:32.349403477 -0500
+++ loop-watcher-prepare.s.clang-3.7.0.inline   2015-11-16 19:06:34.550232528 -0500
@@ -40,8 +40,8 @@
 # BB#2:
        movq    8(%rdi), %rax
        leaq    384(%rax), %r8
-       movq    384(%rax), %rdx
        leaq    104(%rdi), %r9
+       movq    384(%rax), %rdx
        movq    %rdx, 104(%rdi)
        movq    %r8, 112(%rdi)
        movq    %r9, 8(%rdx)
@@ -96,7 +96,7 @@
        .globl  uv__run_prepare
        .align  16, 0x90
        .type   uv__run_prepare,@function
-uv__run_prepare:                        # @uv__run_prepare 
+uv__run_prepare:                        # @uv__run_prepare
        .cfi_startproc
 # BB#0:
        pushq   %r15
@@ -120,29 +120,22 @@
        movq    %rdi, %rbx
        leaq    384(%rbx), %r14
        movq    384(%rbx), %rax
-       cmpq    %rax, %r14
+       cmpq    %r14, %rax
        je      .LBB3_1
-# BB#2:
+# BB#2:                                 # %QUEUE_MOVE.exit.preheader
        movq    392(%rbx), %rcx
        movq    %rcx, 8(%rsp)
-       leaq    (%rsp), %rdx
-       movq    %rdx, (%rcx)
+       leaq    (%rsp), %r15
+       movq    %r15, (%rcx)
        movq    %rax, (%rsp)
        movq    8(%rax), %rcx
        movq    %rcx, 392(%rbx)
        movq    %r14, (%rcx)
-       movq    %rdx, 8(%rax)
-       movq    (%rsp), %rax
-       leaq    (%rsp), %r15
-       jmp     .LBB3_5
-.LBB3_1:
-       leaq    (%rsp), %rax
-       movq    %rax, (%rsp)
-       movq    %rax, 8(%rsp)
-       leaq    (%rsp), %r15
-       jmp     .LBB3_5
+       movq    %r15, 8(%rax)
+       jmp     .LBB3_4
        .align  16, 0x90
-.LBB3_4:                                #   in Loop: Header=BB3_5 Depth=1
+.LBB3_3:                                # %QUEUE_MOVE.exit
+                                        #   in Loop: Header=BB3_4 Depth=1
        leaq    -104(%rax), %rdi
        movq    (%rax), %rcx
        movq    8(%rax), %rdx
@@ -155,11 +148,17 @@
        movq    %rax, (%rcx)
        movq    %rax, 392(%rbx)
        callq   *-8(%rax)
+.LBB3_4:                                # %QUEUE_MOVE.exit
+                                        # =>This Inner Loop Header: Depth=1
        movq    (%rsp), %rax
-.LBB3_5:                                # =>This Inner Loop Header: Depth=1
-       cmpq    %rax, %r15
-       jne     .LBB3_4
-# BB#6:                                 # %._crit_edge
+       cmpq    %r15, %rax
+       jne     .LBB3_3
+       jmp     .LBB3_5
+.LBB3_1:                                # %QUEUE_MOVE.exit.preheader.thread
+       leaq    (%rsp), %rax
+       movq    %rax, (%rsp)
+       movq    %rax, 8(%rsp)
+.LBB3_5:                                # %QUEUE_MOVE.exit._crit_edge
        addq    $16, %rsp
        popq    %rbx
        popq    %r14

@txdv

txdv commented Nov 17, 2015

Copy link
Copy Markdown
Contributor

Looks like some stuff just got reordered and that is it, well it is 2015, so a do-while-zero should be basically the same as an inline. I'm up for this change, but we need @saghul to agree with it. @indutny is a master in assembly, so just cc'ing him so he gives us some input.

@ahippo

ahippo commented Nov 17, 2015

Copy link
Copy Markdown
Author

Looks like some stuff just got reordered and that is it, well it is 2015, so a do-while-zero should be basically the same as an inline. I'm up for this change, but we need @saghul to agree with it. @indutny is a master in assembly, so I would suggest like to hear his opinion as well.

@txdv, thank you for reviewing this!

@saghul

saghul commented Nov 17, 2015

Copy link
Copy Markdown
Member

@saghul, hope your vacation went great!
Have you had a chance to look at the pull request, which is an updated version of pull request #557?

Hi there! Sorry I haven't been able to look at this yet. Will do, probably after 1.8.0, sorry.

@ahippo

ahippo commented Nov 17, 2015

Copy link
Copy Markdown
Author

Hi there! Sorry I haven't been able to look at this yet. Will do, probably after 1.8.0, sorry.

Sure, no worries.

However, I would consider at least these two for 1.8.0: 698b189 and be26870, because they fix an assert in linux-inotify.c.

@ahippo

ahippo commented Nov 17, 2015

Copy link
Copy Markdown
Author

As this is not planned to land in 1.8.0, I took the extra time available and made one more (hopefully) improvement to the proposed changes: reduced exposed to users uv-queue.h to a bare minimum.
The updated version of this pull request is #620.
All changes suggested by @txdv also landed into #620.

Sorry for these hopping review requests, but I don't see a better way to handle major changes in review requests.

@ahippo ahippo closed this Nov 17, 2015
@saghul

saghul commented Nov 18, 2015

Copy link
Copy Markdown
Member

Sorry for these hopping review requests, but I don't see a better way to handle major changes in review requests.

You can make any changes locally and then force push to your branch, the PR will be updated automatically. (Note that GH does not send a notification in this case, so please leave a comment if you do that)

@saghul

saghul commented Nov 18, 2015

Copy link
Copy Markdown
Member

However, I would consider at least these two for 1.8.0: 698b189 and be26870, because they fix an assert in linux-inotify.c.

Those do look interesting indeed. Can you strip them out and make a bugfix PR? We'll also need a test for it. Thanks!

@ahippo

ahippo commented Nov 18, 2015

Copy link
Copy Markdown
Author

Sorry for these hopping review requests, but I don't see a better way to handle major changes in review requests.

You can make any changes locally and then force push to your branch, the PR will be updated automatically. (Note that GH does not send a notification in this case, so please leave a comment if you do that)

Right, and normally it works fine.
However, I tried to force-push this pull request, and got all txdv's comments lost as they are attached to particular commits.
That's why I decided to open a new pull request instead.

@ahippo

ahippo commented Nov 18, 2015

Copy link
Copy Markdown
Author

However, I would consider at least these two for 1.8.0: 698b189 and be26870, because they fix an assert in linux-inotify.c.

Those do look interesting indeed. Can you strip them out and make a bugfix PR? We'll also need a test for it. Thanks!

Cool!
Sure, I'll open a new PR.

@ahippo

ahippo commented Nov 18, 2015

Copy link
Copy Markdown
Author

Those do look interesting indeed. Can you strip them out and make a bugfix PR? We'll also need a test for it. Thanks!

Cool!
Sure, I'll open a new PR.

Opened #621.

@txdv

txdv commented Nov 19, 2015

Copy link
Copy Markdown
Contributor

@saghul what do you think about the inline instead of do-while-zero?

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.

3 participants