test-poll-close-doesnt-corrupt-stack.c
Go to the documentation of this file.
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 #ifdef _WIN32
35 static uv_os_sock_t sock;
36 static uv_poll_t handle;
37 static int close_cb_called = 0;
38 
39 
40 static void close_cb(uv_handle_t* h) {
42 }
43 
44 
45 static void poll_cb(uv_poll_t* h, int status, int events) {
46  ASSERT(0 && "should never get here");
47 }
48 
49 
50 static void NO_INLINE close_socket_and_verify_stack(void) {
51  const uint32_t MARKER = 0xDEADBEEF;
52  const int VERIFY_AFTER = 10; /* ms */
53  int r;
54 
55  volatile uint32_t data[65536];
56  size_t i;
57 
58  for (i = 0; i < ARRAY_SIZE(data); i++)
59  data[i] = MARKER;
60 
61  r = closesocket(sock);
62  ASSERT(r == 0);
63 
64  uv_sleep(VERIFY_AFTER);
65 
66  for (i = 0; i < ARRAY_SIZE(data); i++)
67  ASSERT(data[i] == MARKER);
68 }
69 #endif
70 
71 
72 TEST_IMPL(poll_close_doesnt_corrupt_stack) {
73 #ifndef _WIN32
74  RETURN_SKIP("Test only relevant on Windows");
75 #else
76  struct WSAData wsa_data;
77  int r;
78  unsigned long on;
79  struct sockaddr_in addr;
80 
81  r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
82  ASSERT(r == 0);
83 
84  sock = socket(AF_INET, SOCK_STREAM, 0);
85  ASSERT(sock != INVALID_SOCKET);
86  on = 1;
87  r = ioctlsocket(sock, FIONBIO, &on);
88  ASSERT(r == 0);
89 
90  r = uv_ip4_addr("127.0.0.1", TEST_PORT, &addr);
91  ASSERT(r == 0);
92 
93  r = connect(sock, (const struct sockaddr*) &addr, sizeof addr);
94  ASSERT(r != 0);
95  ASSERT(WSAGetLastError() == WSAEWOULDBLOCK);
96 
98  ASSERT(r == 0);
100  ASSERT(r == 0);
101 
103 
104  close_socket_and_verify_stack();
105 
107  ASSERT(r == 0);
108 
109  ASSERT(close_cb_called == 1);
110 
112  return 0;
113 #endif
114 }
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
UV_WRITABLE
@ UV_WRITABLE
Definition: uv.h:791
uv_poll_init_socket
UV_EXTERN int uv_poll_init_socket(uv_loop_t *loop, uv_poll_t *handle, uv_os_sock_t socket)
Definition: unix/poll.c:96
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_poll_start
UV_EXTERN int uv_poll_start(uv_poll_t *handle, int events, uv_poll_cb cb)
Definition: unix/poll.c:118
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_os_sock_t
int uv_os_sock_t
Definition: unix.h:127
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
close_cb_called
static int close_cb_called
Definition: benchmark-million-timers.c:28
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
UV_READABLE
@ UV_READABLE
Definition: uv.h:790
uv_sleep
UV_EXTERN void uv_sleep(unsigned int msec)
Definition: unix/core.c:1521
close_cb
static void close_cb(uv_handle_t *handle)
Definition: benchmark-million-timers.c:36
poll_cb
static void poll_cb(uv_fs_t *req)
Definition: fs-poll.c:185
NO_INLINE
#define NO_INLINE
Definition: test-poll-close-doesnt-corrupt-stack.c:31
uv.h
TEST_IMPL
TEST_IMPL(poll_close_doesnt_corrupt_stack)
Definition: test-poll-close-doesnt-corrupt-stack.c:72
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_poll_s
Definition: uv.h:783
fix_build_deps.r
r
Definition: fix_build_deps.py:491
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
test_server.socket
socket
Definition: test_server.py:65
closesocket
static int closesocket(int sock)
Definition: bio_test.cc:46
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
errno.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26