mirror of https://github.com/lukechilds/node.git
Browse Source
PR: #9274 PR-URL: https://github.com/joyent/node/pull/9274 Reviewed-By: Julien Gilli <julien.gilli@joyent.com>v0.10.37-release
Saúl Ibarra Corretgé
10 years ago
committed by
Julien Gilli
26 changed files with 595 additions and 105 deletions
@ -0,0 +1,60 @@ |
|||
/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
|
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
*/ |
|||
|
|||
#ifndef UV_ATOMIC_OPS_H_ |
|||
#define UV_ATOMIC_OPS_H_ |
|||
|
|||
#include "internal.h" /* UV_UNUSED */ |
|||
|
|||
UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval)); |
|||
UV_UNUSED(static long cmpxchgl(long* ptr, long oldval, long newval)); |
|||
UV_UNUSED(static void cpu_relax(void)); |
|||
|
|||
/* Prefer hand-rolled assembly over the gcc builtins because the latter also
|
|||
* issue full memory barriers. |
|||
*/ |
|||
UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval)) { |
|||
#if defined(__i386__) || defined(__x86_64__) |
|||
int out; |
|||
__asm__ __volatile__ ("lock; cmpxchg %2, %1;" |
|||
: "=a" (out), "+m" (*(volatile int*) ptr) |
|||
: "r" (newval), "0" (oldval) |
|||
: "memory"); |
|||
return out; |
|||
#else |
|||
return __sync_val_compare_and_swap(ptr, oldval, newval); |
|||
#endif |
|||
} |
|||
|
|||
UV_UNUSED(static long cmpxchgl(long* ptr, long oldval, long newval)) { |
|||
#if defined(__i386__) || defined(__x86_64__) |
|||
long out; |
|||
__asm__ __volatile__ ("lock; cmpxchg %2, %1;" |
|||
: "=a" (out), "+m" (*(volatile long*) ptr) |
|||
: "r" (newval), "0" (oldval) |
|||
: "memory"); |
|||
return out; |
|||
#else |
|||
return __sync_val_compare_and_swap(ptr, oldval, newval); |
|||
#endif |
|||
} |
|||
|
|||
UV_UNUSED(static void cpu_relax(void)) { |
|||
#if defined(__i386__) || defined(__x86_64__) |
|||
__asm__ __volatile__ ("rep; nop"); /* a.k.a. PAUSE */ |
|||
#endif |
|||
} |
|||
|
|||
#endif /* UV_ATOMIC_OPS_H_ */ |
@ -0,0 +1,40 @@ |
|||
/* Copyright (c) 2014, Ben Noordhuis <info@bnoordhuis.nl>
|
|||
* |
|||
* Permission to use, copy, modify, and/or distribute this software for any |
|||
* purpose with or without fee is hereby granted, provided that the above |
|||
* copyright notice and this permission notice appear in all copies. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|||
*/ |
|||
|
|||
#include "uv.h" |
|||
#include "task.h" |
|||
|
|||
static void timer_cb(uv_timer_t* handle, int status) { |
|||
uv_close((uv_handle_t*) handle, NULL); |
|||
} |
|||
|
|||
|
|||
TEST_IMPL(loop_configure) { |
|||
uv_timer_t timer_handle; |
|||
uv_loop_t* loop; |
|||
|
|||
loop = uv_default_loop(); |
|||
#ifdef _WIN32 |
|||
ASSERT(UV_ENOSYS == uv_loop_configure(loop, UV_LOOP_BLOCK_SIGNAL, 0)); |
|||
#else |
|||
ASSERT(0 == uv_loop_configure(loop, UV_LOOP_BLOCK_SIGNAL, SIGPROF)); |
|||
#endif |
|||
ASSERT(0 == uv_timer_init(loop, &timer_handle)); |
|||
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 10, 0)); |
|||
ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT)); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
@ -0,0 +1,114 @@ |
|||
/* Copyright Bert Belder, and other libuv contributors. All rights reserved.
|
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to |
|||
* deal in the Software without restriction, including without limitation the |
|||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|||
* sell copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|||
* IN THE SOFTWARE. |
|||
*/ |
|||
|
|||
#ifdef _WIN32 |
|||
|
|||
#include <errno.h> |
|||
#include <stdio.h> |
|||
|
|||
#include "uv.h" |
|||
#include "task.h" |
|||
|
|||
#ifdef _MSC_VER /* msvc */ |
|||
# define NO_INLINE __declspec(noinline) |
|||
#else /* gcc */ |
|||
# define NO_INLINE __attribute__ ((noinline)) |
|||
#endif |
|||
|
|||
|
|||
uv_os_sock_t sock; |
|||
uv_poll_t handle; |
|||
|
|||
static int close_cb_called = 0; |
|||
|
|||
|
|||
static void close_cb(uv_handle_t* h) { |
|||
close_cb_called++; |
|||
} |
|||
|
|||
|
|||
static void poll_cb(uv_poll_t* h, int status, int events) { |
|||
ASSERT(0 && "should never get here"); |
|||
} |
|||
|
|||
|
|||
static void NO_INLINE close_socket_and_verify_stack() { |
|||
const uint32_t MARKER = 0xDEADBEEF; |
|||
const int VERIFY_AFTER = 10; /* ms */ |
|||
int r; |
|||
|
|||
volatile uint32_t data[65536]; |
|||
size_t i; |
|||
|
|||
for (i = 0; i < ARRAY_SIZE(data); i++) |
|||
data[i] = MARKER; |
|||
|
|||
r = closesocket(sock); |
|||
ASSERT(r == 0); |
|||
|
|||
uv_sleep(VERIFY_AFTER); |
|||
|
|||
for (i = 0; i < ARRAY_SIZE(data); i++) |
|||
ASSERT(data[i] == MARKER); |
|||
} |
|||
|
|||
|
|||
TEST_IMPL(poll_close_doesnt_corrupt_stack) { |
|||
struct WSAData wsa_data; |
|||
int r; |
|||
unsigned long on; |
|||
struct sockaddr_in addr; |
|||
|
|||
r = WSAStartup(MAKEWORD(2, 2), &wsa_data); |
|||
ASSERT(r == 0); |
|||
|
|||
sock = socket(AF_INET, SOCK_STREAM, 0); |
|||
ASSERT(sock != INVALID_SOCKET); |
|||
on = 1; |
|||
r = ioctlsocket(sock, FIONBIO, &on); |
|||
ASSERT(r == 0); |
|||
|
|||
addr = uv_ip4_addr("127.0.0.1", TEST_PORT); |
|||
ASSERT(r == 0); |
|||
|
|||
r = connect(sock, (const struct sockaddr*) &addr, sizeof addr); |
|||
ASSERT(r != 0); |
|||
ASSERT(WSAGetLastError() == WSAEWOULDBLOCK); |
|||
|
|||
r = uv_poll_init_socket(uv_default_loop(), &handle, sock); |
|||
ASSERT(r == 0); |
|||
r = uv_poll_start(&handle, UV_READABLE | UV_WRITABLE, poll_cb); |
|||
ASSERT(r == 0); |
|||
|
|||
uv_close((uv_handle_t*) &handle, close_cb); |
|||
|
|||
close_socket_and_verify_stack(); |
|||
|
|||
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
|||
ASSERT(r == 0); |
|||
|
|||
ASSERT(close_cb_called == 1); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
|||
|
|||
#endif /* _WIN32 */ |
@ -0,0 +1,137 @@ |
|||
/* Copyright Fedor Indutny. All rights reserved.
|
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to |
|||
* deal in the Software without restriction, including without limitation the |
|||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|||
* sell copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|||
* IN THE SOFTWARE. |
|||
*/ |
|||
|
|||
#if !defined(_WIN32) |
|||
|
|||
#include "uv.h" |
|||
#include "task.h" |
|||
|
|||
#include <errno.h> |
|||
#include <sys/socket.h> |
|||
#include <unistd.h> |
|||
|
|||
static uv_tcp_t server_handle; |
|||
static uv_tcp_t client_handle; |
|||
static uv_tcp_t peer_handle; |
|||
static uv_idle_t idle; |
|||
static uv_connect_t connect_req; |
|||
static int ticks; |
|||
static const int kMaxTicks = 10; |
|||
|
|||
|
|||
static void set_nonblocking(int fd, int set) { |
|||
int r; |
|||
int flags = fcntl(fd, F_GETFL, 0); |
|||
ASSERT(flags >= 0); |
|||
if (set) |
|||
flags |= O_NONBLOCK; |
|||
else |
|||
flags &= ~O_NONBLOCK; |
|||
r = fcntl(fd, F_SETFL, flags); |
|||
ASSERT(r >= 0); |
|||
} |
|||
|
|||
|
|||
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) { |
|||
static char storage[1024]; |
|||
return uv_buf_init(storage, sizeof(storage)); |
|||
} |
|||
|
|||
|
|||
static void idle_cb(uv_idle_t* idle, int status) { |
|||
ASSERT(status == 0); |
|||
if (++ticks < kMaxTicks) |
|||
return; |
|||
|
|||
uv_close((uv_handle_t*) &server_handle, NULL); |
|||
uv_close((uv_handle_t*) &client_handle, NULL); |
|||
uv_close((uv_handle_t*) &peer_handle, NULL); |
|||
uv_close((uv_handle_t*) idle, NULL); |
|||
} |
|||
|
|||
|
|||
static void read_cb(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) { |
|||
ASSERT(nread > 0); |
|||
ASSERT(0 == uv_idle_start(&idle, idle_cb)); |
|||
} |
|||
|
|||
|
|||
static void connect_cb(uv_connect_t* req, int status) { |
|||
ASSERT(req->handle == (uv_stream_t*) &client_handle); |
|||
ASSERT(0 == status); |
|||
} |
|||
|
|||
|
|||
static void connection_cb(uv_stream_t* handle, int status) { |
|||
int r; |
|||
int fd; |
|||
|
|||
ASSERT(0 == status); |
|||
ASSERT(0 == uv_accept(handle, (uv_stream_t*) &peer_handle)); |
|||
ASSERT(0 == uv_read_start((uv_stream_t*) &peer_handle, alloc_cb, read_cb)); |
|||
|
|||
/* Send some OOB data */ |
|||
fd = client_handle.io_watcher.fd; |
|||
set_nonblocking(fd, 0); |
|||
|
|||
/* The problem triggers only on a second message, it seem that xnu is not
|
|||
* triggering `kevent()` for the first one |
|||
*/ |
|||
do { |
|||
r = send(fd, "hello", 5, MSG_OOB); |
|||
} while (r < 0 && errno == EINTR); |
|||
ASSERT(5 == r); |
|||
|
|||
do { |
|||
r = send(fd, "hello", 5, MSG_OOB); |
|||
} while (r < 0 && errno == EINTR); |
|||
ASSERT(5 == r); |
|||
|
|||
set_nonblocking(fd, 1); |
|||
} |
|||
|
|||
|
|||
TEST_IMPL(tcp_oob) { |
|||
uv_loop_t* loop; |
|||
loop = uv_default_loop(); |
|||
|
|||
ASSERT(0 == uv_tcp_init(loop, &server_handle)); |
|||
ASSERT(0 == uv_tcp_init(loop, &client_handle)); |
|||
ASSERT(0 == uv_tcp_init(loop, &peer_handle)); |
|||
ASSERT(0 == uv_idle_init(loop, &idle)); |
|||
ASSERT(0 == uv_tcp_bind(&server_handle, uv_ip4_addr("127.0.0.1", TEST_PORT))); |
|||
ASSERT(0 == uv_listen((uv_stream_t*) &server_handle, 1, connection_cb)); |
|||
|
|||
/* Ensure two separate packets */ |
|||
ASSERT(0 == uv_tcp_nodelay(&client_handle, 1)); |
|||
|
|||
ASSERT(0 == uv_tcp_connect(&connect_req, |
|||
&client_handle, |
|||
uv_ip4_addr("127.0.0.1", TEST_PORT), |
|||
connect_cb)); |
|||
ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT)); |
|||
|
|||
ASSERT(ticks == kMaxTicks); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
|||
#endif |
Loading…
Reference in new issue