1*fbb2e0a3Schristos /* Copyright Bert Belder, and other libuv contributors. All rights reserved.
2*fbb2e0a3Schristos *
3*fbb2e0a3Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
4*fbb2e0a3Schristos * of this software and associated documentation files (the "Software"), to
5*fbb2e0a3Schristos * deal in the Software without restriction, including without limitation the
6*fbb2e0a3Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*fbb2e0a3Schristos * sell copies of the Software, and to permit persons to whom the Software is
8*fbb2e0a3Schristos * furnished to do so, subject to the following conditions:
9*fbb2e0a3Schristos *
10*fbb2e0a3Schristos * The above copyright notice and this permission notice shall be included in
11*fbb2e0a3Schristos * all copies or substantial portions of the Software.
12*fbb2e0a3Schristos *
13*fbb2e0a3Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*fbb2e0a3Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*fbb2e0a3Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*fbb2e0a3Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*fbb2e0a3Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*fbb2e0a3Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*fbb2e0a3Schristos * IN THE SOFTWARE.
20*fbb2e0a3Schristos */
21*fbb2e0a3Schristos
22*fbb2e0a3Schristos #include <errno.h>
23*fbb2e0a3Schristos #include <stdio.h>
24*fbb2e0a3Schristos
25*fbb2e0a3Schristos #include "uv.h"
26*fbb2e0a3Schristos #include "task.h"
27*fbb2e0a3Schristos
28*fbb2e0a3Schristos #ifdef _MSC_VER /* msvc */
29*fbb2e0a3Schristos # define NO_INLINE __declspec(noinline)
30*fbb2e0a3Schristos #else /* gcc */
31*fbb2e0a3Schristos # define NO_INLINE __attribute__ ((noinline))
32*fbb2e0a3Schristos #endif
33*fbb2e0a3Schristos
34*fbb2e0a3Schristos #ifdef _WIN32
35*fbb2e0a3Schristos static uv_os_sock_t sock;
36*fbb2e0a3Schristos static uv_poll_t handle;
37*fbb2e0a3Schristos static int close_cb_called = 0;
38*fbb2e0a3Schristos
39*fbb2e0a3Schristos
close_cb(uv_handle_t * h)40*fbb2e0a3Schristos static void close_cb(uv_handle_t* h) {
41*fbb2e0a3Schristos close_cb_called++;
42*fbb2e0a3Schristos }
43*fbb2e0a3Schristos
44*fbb2e0a3Schristos
poll_cb(uv_poll_t * h,int status,int events)45*fbb2e0a3Schristos static void poll_cb(uv_poll_t* h, int status, int events) {
46*fbb2e0a3Schristos ASSERT(0 && "should never get here");
47*fbb2e0a3Schristos }
48*fbb2e0a3Schristos
49*fbb2e0a3Schristos
close_socket_and_verify_stack(void)50*fbb2e0a3Schristos static void NO_INLINE close_socket_and_verify_stack(void) {
51*fbb2e0a3Schristos const uint32_t MARKER = 0xDEADBEEF;
52*fbb2e0a3Schristos const int VERIFY_AFTER = 10; /* ms */
53*fbb2e0a3Schristos int r;
54*fbb2e0a3Schristos
55*fbb2e0a3Schristos volatile uint32_t data[65536];
56*fbb2e0a3Schristos size_t i;
57*fbb2e0a3Schristos
58*fbb2e0a3Schristos for (i = 0; i < ARRAY_SIZE(data); i++)
59*fbb2e0a3Schristos data[i] = MARKER;
60*fbb2e0a3Schristos
61*fbb2e0a3Schristos r = closesocket(sock);
62*fbb2e0a3Schristos ASSERT(r == 0);
63*fbb2e0a3Schristos
64*fbb2e0a3Schristos uv_sleep(VERIFY_AFTER);
65*fbb2e0a3Schristos
66*fbb2e0a3Schristos for (i = 0; i < ARRAY_SIZE(data); i++)
67*fbb2e0a3Schristos ASSERT(data[i] == MARKER);
68*fbb2e0a3Schristos }
69*fbb2e0a3Schristos #endif
70*fbb2e0a3Schristos
71*fbb2e0a3Schristos
TEST_IMPL(poll_close_doesnt_corrupt_stack)72*fbb2e0a3Schristos TEST_IMPL(poll_close_doesnt_corrupt_stack) {
73*fbb2e0a3Schristos #ifndef _WIN32
74*fbb2e0a3Schristos RETURN_SKIP("Test only relevant on Windows");
75*fbb2e0a3Schristos #else
76*fbb2e0a3Schristos struct WSAData wsa_data;
77*fbb2e0a3Schristos int r;
78*fbb2e0a3Schristos unsigned long on;
79*fbb2e0a3Schristos struct sockaddr_in addr;
80*fbb2e0a3Schristos
81*fbb2e0a3Schristos r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
82*fbb2e0a3Schristos ASSERT(r == 0);
83*fbb2e0a3Schristos
84*fbb2e0a3Schristos sock = socket(AF_INET, SOCK_STREAM, 0);
85*fbb2e0a3Schristos ASSERT(sock != INVALID_SOCKET);
86*fbb2e0a3Schristos on = 1;
87*fbb2e0a3Schristos r = ioctlsocket(sock, FIONBIO, &on);
88*fbb2e0a3Schristos ASSERT(r == 0);
89*fbb2e0a3Schristos
90*fbb2e0a3Schristos r = uv_ip4_addr("127.0.0.1", TEST_PORT, &addr);
91*fbb2e0a3Schristos ASSERT(r == 0);
92*fbb2e0a3Schristos
93*fbb2e0a3Schristos r = connect(sock, (const struct sockaddr*) &addr, sizeof addr);
94*fbb2e0a3Schristos ASSERT(r != 0);
95*fbb2e0a3Schristos ASSERT(WSAGetLastError() == WSAEWOULDBLOCK);
96*fbb2e0a3Schristos
97*fbb2e0a3Schristos r = uv_poll_init_socket(uv_default_loop(), &handle, sock);
98*fbb2e0a3Schristos ASSERT(r == 0);
99*fbb2e0a3Schristos r = uv_poll_start(&handle, UV_READABLE | UV_WRITABLE, poll_cb);
100*fbb2e0a3Schristos ASSERT(r == 0);
101*fbb2e0a3Schristos
102*fbb2e0a3Schristos uv_close((uv_handle_t*) &handle, close_cb);
103*fbb2e0a3Schristos
104*fbb2e0a3Schristos close_socket_and_verify_stack();
105*fbb2e0a3Schristos
106*fbb2e0a3Schristos r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
107*fbb2e0a3Schristos ASSERT(r == 0);
108*fbb2e0a3Schristos
109*fbb2e0a3Schristos ASSERT(close_cb_called == 1);
110*fbb2e0a3Schristos
111*fbb2e0a3Schristos MAKE_VALGRIND_HAPPY();
112*fbb2e0a3Schristos return 0;
113*fbb2e0a3Schristos #endif
114*fbb2e0a3Schristos }
115