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      @SuppressJava6Requirement(reason = "BlockHound is Java 8+, but this class is only loaded by it's SPI")
39      public static final class NettyBlockHoundIntegration implements BlockHoundIntegration {
40  
41          @Override
42          public void applyTo(BlockHound.Builder builder) {
43              builder.allowBlockingCallsInside(
44                      "io.netty.channel.nio.NioEventLoop",
45                      "handleLoopException"
46              );
47  
48              builder.allowBlockingCallsInside(
49                      "io.netty.channel.kqueue.KQueueEventLoop",
50                      "handleLoopException"
51              );
52  
53              builder.allowBlockingCallsInside(
54                      "io.netty.channel.epoll.EpollEventLoop",
55                      "handleLoopException"
56              );
57  
58              builder.allowBlockingCallsInside(
59                      "io.netty.util.HashedWheelTimer",
60                      "start"
61              );
62  
63              builder.allowBlockingCallsInside(
64                      "io.netty.util.HashedWheelTimer",
65                      "stop"
66              );
67  
68              builder.allowBlockingCallsInside(
69                      "io.netty.util.HashedWheelTimer$Worker",
70                      "waitForNextTick"
71              );
72  
73              builder.allowBlockingCallsInside(
74                      "io.netty.util.concurrent.SingleThreadEventExecutor",
75                      "confirmShutdown"
76              );
77  
78              builder.allowBlockingCallsInside(
79                      "io.netty.handler.ssl.SslHandler",
80                      "handshake"
81              );
82  
83              builder.allowBlockingCallsInside(
84                      "io.netty.handler.ssl.SslHandler",
85                      "runAllDelegatedTasks"
86              );
87              builder.allowBlockingCallsInside(
88                      "io.netty.handler.ssl.SslHandler",
89                      "runDelegatedTasks"
90              );
91              builder.allowBlockingCallsInside(
92                      "io.netty.util.concurrent.GlobalEventExecutor",
93                      "takeTask");
94  
95              builder.allowBlockingCallsInside(
96                      "io.netty.util.concurrent.GlobalEventExecutor",
97                      "addTask");
98  
99              builder.allowBlockingCallsInside(
100                     "io.netty.util.concurrent.SingleThreadEventExecutor",
101                     "takeTask");
102 
103             builder.allowBlockingCallsInside(
104                     "io.netty.util.concurrent.SingleThreadEventExecutor",
105                     "addTask");
106 
107             builder.allowBlockingCallsInside(
108                     "io.netty.handler.ssl.ReferenceCountedOpenSslClientContext$ExtendedTrustManagerVerifyCallback",
109                     "verify");
110 
111             builder.allowBlockingCallsInside(
112                     "io.netty.handler.ssl.JdkSslContext$Defaults",
113                     "init");
114 
115             // Let's whitelist SSLEngineImpl.unwrap(...) for now as it may fail otherwise for TLS 1.3.
116             // See https://mail.openjdk.java.net/pipermail/security-dev/2020-August/022271.html
117             builder.allowBlockingCallsInside(
118                     "sun.security.ssl.SSLEngineImpl",
119                     "unwrap");
120 
121             builder.allowBlockingCallsInside(
122                     "sun.security.ssl.SSLEngineImpl",
123                     "wrap");
124 
125             builder.allowBlockingCallsInside(
126                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
127                     "parse");
128 
129             builder.allowBlockingCallsInside(
130                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
131                     "parseEtcResolverSearchDomains");
132 
133             builder.allowBlockingCallsInside(
134                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
135                     "parseEtcResolverOptions");
136 
137             builder.allowBlockingCallsInside(
138                     "io.netty.resolver.HostsFileEntriesProvider$ParserImpl",
139                     "parse");
140 
141             builder.allowBlockingCallsInside(
142                     "io.netty.util.NetUtil$SoMaxConnAction",
143                     "run");
144 
145             builder.nonBlockingThreadPredicate(new Function<Predicate<Thread>, Predicate<Thread>>() {
146                 @Override
147                 public Predicate<Thread> apply(final Predicate<Thread> p) {
148                     return new Predicate<Thread>() {
149                         @Override
150                         @SuppressJava6Requirement(reason = "Predicate#test")
151                         public boolean test(Thread thread) {
152                             return p.test(thread) || thread instanceof FastThreadLocalThread;
153                         }
154                     };
155                 }
156             });
157         }
158 
159         @Override
160         public int compareTo(BlockHoundIntegration o) {
161             return 0;
162         }
163     }
164 }