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