test-poll-close-doesnt-corrupt-stack.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <errno.h>
  22. #include <stdio.h>
  23. #include "uv.h"
  24. #include "task.h"
  25. #ifdef _MSC_VER /* msvc */
  26. # define NO_INLINE __declspec(noinline)
  27. #else /* gcc */
  28. # define NO_INLINE __attribute__ ((noinline))
  29. #endif
  30. #ifdef _WIN32
  31. static uv_os_sock_t sock;
  32. static uv_poll_t handle;
  33. static int close_cb_called = 0;
  34. static void close_cb(uv_handle_t* h) {
  35. close_cb_called++;
  36. }
  37. static void poll_cb(uv_poll_t* h, int status, int events) {
  38. ASSERT(0 && "should never get here");
  39. }
  40. static void NO_INLINE close_socket_and_verify_stack(void) {
  41. const uint32_t MARKER = 0xDEADBEEF;
  42. const int VERIFY_AFTER = 10; /* ms */
  43. int r;
  44. volatile uint32_t data[65536];
  45. size_t i;
  46. for (i = 0; i < ARRAY_SIZE(data); i++)
  47. data[i] = MARKER;
  48. r = closesocket(sock);
  49. ASSERT(r == 0);
  50. uv_sleep(VERIFY_AFTER);
  51. for (i = 0; i < ARRAY_SIZE(data); i++)
  52. ASSERT(data[i] == MARKER);
  53. }
  54. #endif
  55. TEST_IMPL(poll_close_doesnt_corrupt_stack) {
  56. #ifndef _WIN32
  57. RETURN_SKIP("Test only relevant on Windows");
  58. #else
  59. struct WSAData wsa_data;
  60. int r;
  61. unsigned long on;
  62. struct sockaddr_in addr;
  63. r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  64. ASSERT(r == 0);
  65. sock = socket(AF_INET, SOCK_STREAM, 0);
  66. ASSERT(sock != INVALID_SOCKET);
  67. on = 1;
  68. r = ioctlsocket(sock, FIONBIO, &on);
  69. ASSERT(r == 0);
  70. r = uv_ip4_addr("127.0.0.1", TEST_PORT, &addr);
  71. ASSERT(r == 0);
  72. r = connect(sock, (const struct sockaddr*) &addr, sizeof addr);
  73. ASSERT(r != 0);
  74. ASSERT(WSAGetLastError() == WSAEWOULDBLOCK);
  75. r = uv_poll_init_socket(uv_default_loop(), &handle, sock);
  76. ASSERT(r == 0);
  77. r = uv_poll_start(&handle, UV_READABLE | UV_WRITABLE, poll_cb);
  78. ASSERT(r == 0);
  79. uv_close((uv_handle_t*) &handle, close_cb);
  80. close_socket_and_verify_stack();
  81. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  82. ASSERT(r == 0);
  83. ASSERT(close_cb_called == 1);
  84. MAKE_VALGRIND_HAPPY();
  85. return 0;
  86. #endif
  87. }