• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 #include "src/core/lib/iomgr/port.h"
19 
20 /* This test only relevant on linux systems where epoll() is available */
21 #if defined(GRPC_LINUX_EPOLL_CREATE1) && defined(GRPC_LINUX_EVENTFD)
22 #include "src/core/lib/iomgr/ev_epollex_linux.h"
23 
24 #include <grpc/grpc.h>
25 #include <string.h>
26 #include <sys/eventfd.h>
27 
28 #include "test/core/util/test_config.h"
29 
pollset_destroy(void * ps,grpc_error * error)30 static void pollset_destroy(void* ps, grpc_error* error) {
31   grpc_pollset_destroy(static_cast<grpc_pollset*>(ps));
32   gpr_free(ps);
33 }
34 
35 // This test is added to cover the case found in bug:
36 // https://github.com/grpc/grpc/issues/15760
test_pollable_owner_fd()37 static void test_pollable_owner_fd() {
38   grpc_core::ExecCtx exec_ctx;
39   int ev_fd1;
40   int ev_fd2;
41   grpc_fd* grpc_fd1;
42   grpc_fd* grpc_fd2;
43   grpc_pollset* ps;
44   gpr_mu* mu;
45 
46   // == Create two grpc_fds ==
47   // All we need is two file descriptors. Doesn't matter what type. We use
48   // eventfd type here for the purpose of this test
49   ev_fd1 = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
50   ev_fd2 = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
51   if (ev_fd1 < 0 || ev_fd2 < 0) {
52     gpr_log(GPR_ERROR, "Error in creating event fds for the test");
53     return;
54   }
55   grpc_fd1 = grpc_fd_create(ev_fd1, "epollex-test-fd1", false);
56   grpc_fd2 = grpc_fd_create(ev_fd2, "epollex-test-fd2", false);
57   grpc_core::ExecCtx::Get()->Flush();
58 
59   // == Create a pollset ==
60   ps = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
61   grpc_pollset_init(ps, &mu);
62   grpc_core::ExecCtx::Get()->Flush();
63 
64   // == Add fd1 to pollset ==
65   grpc_pollset_add_fd(ps, grpc_fd1);
66   grpc_core::ExecCtx::Get()->Flush();
67 
68   // == Destroy fd1 ==
69   grpc_fd_orphan(grpc_fd1, nullptr, nullptr, "test fd1 orphan");
70   grpc_core::ExecCtx::Get()->Flush();
71 
72   // = Add fd2 to pollset ==
73   //
74   // Before https://github.com/grpc/grpc/issues/15760, the following line caused
75   // unexpected behavior (The previous grpc_pollset_add_fd(ps, grpc_fd1) created
76   // an underlying structure in epollex that held a reference to grpc_fd1 which
77   // was being accessed here even after grpc_fd_orphan(grpc_fd1) was called
78   grpc_pollset_add_fd(ps, grpc_fd2);
79   grpc_core::ExecCtx::Get()->Flush();
80 
81   // == Destroy fd2 ==
82   grpc_fd_orphan(grpc_fd2, nullptr, nullptr, "test fd2 orphan");
83   grpc_core::ExecCtx::Get()->Flush();
84 
85   // == Destroy pollset
86   grpc_closure ps_destroy_closure;
87   GRPC_CLOSURE_INIT(&ps_destroy_closure, pollset_destroy, ps,
88                     grpc_schedule_on_exec_ctx);
89   grpc_pollset_shutdown(ps, &ps_destroy_closure);
90   grpc_core::ExecCtx::Get()->Flush();
91 }
92 
main(int argc,char ** argv)93 int main(int argc, char** argv) {
94   const char* poll_strategy = nullptr;
95   grpc_test_init(argc, argv);
96   grpc_init();
97   {
98     grpc_core::ExecCtx exec_ctx;
99     poll_strategy = grpc_get_poll_strategy_name();
100     if (poll_strategy != nullptr && strcmp(poll_strategy, "epollex") == 0) {
101       test_pollable_owner_fd();
102     } else {
103       gpr_log(GPR_INFO,
104               "Skipping the test. The test is only relevant for 'epollex' "
105               "strategy. and the current strategy is: '%s'",
106               poll_strategy);
107     }
108   }
109 
110   grpc_shutdown();
111   return 0;
112 }
113 #else /* defined(GRPC_LINUX_EPOLL_CREATE1) && defined(GRPC_LINUX_EVENTFD) */
main(int argc,char ** argv)114 int main(int argc, char** argv) { return 0; }
115 #endif
116