View Javadoc
1   /*
2    * Copyright 2019 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.util.internal;
18  
19  import io.netty.util.concurrent.FastThreadLocalThread;
20  import reactor.blockhound.BlockHound;
21  import reactor.blockhound.integration.BlockHoundIntegration;
22  
23  import java.util.function.Function;
24  import java.util.function.Predicate;
25  
26  /**
27   * Contains classes that must have public visibility but are not public API.
28   */
29  class Hidden {
30  
31      /**
32       * This class integrates Netty with BlockHound.
33       * <p>
34       * It is public but only because of the ServiceLoader's limitations
35       * and SHOULD NOT be considered a public API.
36       */
37      @UnstableApi
38      public static final class NettyBlockHoundIntegration implements BlockHoundIntegration {
39  
40          @Override
41          public void applyTo(BlockHound.Builder builder) {
42              builder.allowBlockingCallsInside(
43                      "io.netty.channel.nio.NioEventLoop",
44                      "handleLoopException"
45              );
46  
47              builder.allowBlockingCallsInside(
48                      "io.netty.channel.kqueue.KQueueEventLoop",
49                      "handleLoopException"
50              );
51  
52              builder.allowBlockingCallsInside(
53                      "io.netty.channel.epoll.EpollEventLoop",
54                      "handleLoopException"
55              );
56  
57              builder.allowBlockingCallsInside(
58                      "io.netty.util.HashedWheelTimer",
59                      "start"
60              );
61  
62              builder.allowBlockingCallsInside(
63                      "io.netty.util.HashedWheelTimer",
64                      "stop"
65              );
66  
67              builder.allowBlockingCallsInside(
68                      "io.netty.util.HashedWheelTimer$Worker",
69                      "waitForNextTick"
70              );
71  
72              builder.allowBlockingCallsInside(
73                      "io.netty.util.concurrent.SingleThreadEventExecutor",
74                      "confirmShutdown"
75              );
76  
77              builder.allowBlockingCallsInside(
78                      "io.netty.buffer.PoolArena",
79                      "lock"
80              );
81  
82              builder.allowBlockingCallsInside(
83                      "io.netty.buffer.PoolSubpage",
84                      "lock"
85              );
86  
87              builder.allowBlockingCallsInside(
88                      "io.netty.buffer.PoolChunk",
89                      "allocateRun"
90              );
91  
92              builder.allowBlockingCallsInside(
93                      "io.netty.buffer.PoolChunk",
94                      "free"
95              );
96  
97              builder.allowBlockingCallsInside(
98                      "io.netty.buffer.AdaptivePoolingAllocator$1",
99                      "initialValue"
100             );
101 
102             builder.allowBlockingCallsInside(
103                     "io.netty.buffer.AdaptivePoolingAllocator$1",
104                     "onRemoval"
105             );
106 
107             builder.allowBlockingCallsInside(
108                     "io.netty.buffer.AdaptivePoolingAllocator$Magazine",
109                     "free"
110             );
111 
112             builder.allowBlockingCallsInside(
113                     "io.netty.handler.ssl.SslHandler",
114                     "handshake"
115             );
116 
117             builder.allowBlockingCallsInside(
118                     "io.netty.handler.ssl.SslHandler",
119                     "runAllDelegatedTasks"
120             );
121             builder.allowBlockingCallsInside(
122                     "io.netty.handler.ssl.SslHandler",
123                     "runDelegatedTasks"
124             );
125             builder.allowBlockingCallsInside(
126                     "io.netty.util.concurrent.GlobalEventExecutor",
127                     "takeTask");
128 
129             builder.allowBlockingCallsInside(
130                     "io.netty.util.concurrent.GlobalEventExecutor",
131                     "addTask");
132 
133             builder.allowBlockingCallsInside(
134                     "io.netty.util.concurrent.SingleThreadEventExecutor",
135                     "pollTaskFrom");
136 
137             builder.allowBlockingCallsInside(
138                     "io.netty.util.concurrent.SingleThreadEventExecutor",
139                     "takeTask");
140 
141             builder.allowBlockingCallsInside(
142                     "io.netty.util.concurrent.SingleThreadEventExecutor",
143                     "addTask");
144 
145             builder.allowBlockingCallsInside(
146                     "io.netty.handler.ssl.ReferenceCountedOpenSslClientContext$ExtendedTrustManagerVerifyCallback",
147                     "verify");
148 
149             builder.allowBlockingCallsInside(
150                     "io.netty.handler.ssl.JdkSslContext$Defaults",
151                     "init");
152 
153             // Let's whitelist SSLEngineImpl.unwrap(...) for now as it may fail otherwise for TLS 1.3.
154             // See https://mail.openjdk.java.net/pipermail/security-dev/2020-August/022271.html
155             builder.allowBlockingCallsInside(
156                     "sun.security.ssl.SSLEngineImpl",
157                     "unwrap");
158 
159             builder.allowBlockingCallsInside(
160                     "sun.security.ssl.SSLEngineImpl",
161                     "wrap");
162 
163             builder.allowBlockingCallsInside(
164                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
165                     "parse");
166 
167             builder.allowBlockingCallsInside(
168                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
169                     "parseEtcResolverSearchDomains");
170 
171             builder.allowBlockingCallsInside(
172                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
173                     "parseEtcResolverOptions");
174 
175             builder.allowBlockingCallsInside(
176                     "io.netty.resolver.dns.DnsQueryIdSpace",
177                     "nextId");
178 
179             builder.allowBlockingCallsInside(
180                     "io.netty.resolver.dns.DnsQueryIdSpace$DnsQueryIdRange",
181                     "pushId");
182 
183             builder.allowBlockingCallsInside(
184                     "io.netty.resolver.HostsFileEntriesProvider$ParserImpl",
185                     "parse");
186 
187             builder.allowBlockingCallsInside(
188                     "io.netty.util.NetUtil$SoMaxConnAction",
189                     "run");
190 
191             builder.allowBlockingCallsInside("io.netty.util.internal.ReferenceCountUpdater",
192                     "retryRelease0");
193 
194             builder.allowBlockingCallsInside("io.netty.util.internal.PlatformDependent", "createTempFile");
195             builder.nonBlockingThreadPredicate(new Function<Predicate<Thread>, Predicate<Thread>>() {
196                 @Override
197                 public Predicate<Thread> apply(final Predicate<Thread> p) {
198                     return new Predicate<Thread>() {
199                         @Override
200                         public boolean test(Thread thread) {
201                             return p.test(thread) ||
202                                     thread instanceof FastThreadLocalThread &&
203                                             !((FastThreadLocalThread) thread).permitBlockingCalls();
204                         }
205                     };
206                 }
207             });
208         }
209 
210         @Override
211         public int compareTo(BlockHoundIntegration o) {
212             return 0;
213         }
214     }
215 }