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).
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().
ecd338f to
2a7d515
Compare
|
@txdv, sorry for the late reply. So, again, the changeset 8f98ef2 is for the sake of the review request and the v1.x-uv-queue-v3 branch is for merging. Regarding assembler output, I'll post it shortly. |
|
Regarding assembly output comparison for macro to inline function conversion. I did my tests using gcc-5.2.0 with -O2. #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. --- 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.unlikelyI can do more tests if you'd like. |
|
For clang-3.7.0 with -O2 results are a bit different for src/unix/loop-watcher.c. --- 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, thank you for reviewing this! |
|
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. 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. |
Opened #621. |
|
@saghul what do you think about the inline instead of do-while-zero? |
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.