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.buffer.PoolArena",
80                      "lock"
81              );
82  
83              builder.allowBlockingCallsInside(
84                      "io.netty.buffer.PoolSubpage",
85                      "lock"
86              );
87  
88              builder.allowBlockingCallsInside(
89                      "io.netty.buffer.PoolChunk",
90                      "allocateRun"
91              );
92  
93              builder.allowBlockingCallsInside(
94                      "io.netty.buffer.PoolChunk",
95                      "free"
96              );
97  
98              builder.allowBlockingCallsInside(
99                      "io.netty.handler.ssl.SslHandler",
100                     "handshake"
101             );
102 
103             builder.allowBlockingCallsInside(
104                     "io.netty.handler.ssl.SslHandler",
105                     "runAllDelegatedTasks"
106             );
107             builder.allowBlockingCallsInside(
108                     "io.netty.handler.ssl.SslHandler",
109                     "runDelegatedTasks"
110             );
111             builder.allowBlockingCallsInside(
112                     "io.netty.util.concurrent.GlobalEventExecutor",
113                     "takeTask");
114 
115             builder.allowBlockingCallsInside(
116                     "io.netty.util.concurrent.GlobalEventExecutor",
117                     "addTask");
118 
119             builder.allowBlockingCallsInside(
120                     "io.netty.util.concurrent.SingleThreadEventExecutor",
121                     "takeTask");
122 
123             builder.allowBlockingCallsInside(
124                     "io.netty.util.concurrent.SingleThreadEventExecutor",
125                     "addTask");
126 
127             builder.allowBlockingCallsInside(
128                     "io.netty.handler.ssl.ReferenceCountedOpenSslClientContext$ExtendedTrustManagerVerifyCallback",
129                     "verify");
130 
131             builder.allowBlockingCallsInside(
132                     "io.netty.handler.ssl.JdkSslContext$Defaults",
133                     "init");
134 
135             // Let's whitelist SSLEngineImpl.unwrap(...) for now as it may fail otherwise for TLS 1.3.
136             // See https://mail.openjdk.java.net/pipermail/security-dev/2020-August/022271.html
137             builder.allowBlockingCallsInside(
138                     "sun.security.ssl.SSLEngineImpl",
139                     "unwrap");
140 
141             builder.allowBlockingCallsInside(
142                     "sun.security.ssl.SSLEngineImpl",
143                     "wrap");
144 
145             builder.allowBlockingCallsInside(
146                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
147                     "parse");
148 
149             builder.allowBlockingCallsInside(
150                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
151                     "parseEtcResolverSearchDomains");
152 
153             builder.allowBlockingCallsInside(
154                     "io.netty.resolver.dns.UnixResolverDnsServerAddressStreamProvider",
155                     "parseEtcResolverOptions");
156 
157             builder.allowBlockingCallsInside(
158                     "io.netty.resolver.HostsFileEntriesProvider$ParserImpl",
159                     "parse");
160 
161             builder.allowBlockingCallsInside(
162                     "io.netty.util.NetUtil$SoMaxConnAction",
163                     "run");
164 
165             builder.allowBlockingCallsInside("io.netty.util.internal.ReferenceCountUpdater",
166                     "retryRelease0");
167 
168             builder.allowBlockingCallsInside("io.netty.util.internal.PlatformDependent", "createTempFile");
169             builder.nonBlockingThreadPredicate(new Function<Predicate<Thread>, Predicate<Thread>>() {
170                 @Override
171                 public Predicate<Thread> apply(final Predicate<Thread> p) {
172                     return new Predicate<Thread>() {
173                         @Override
174                         @SuppressJava6Requirement(reason = "Predicate#test")
175                         public boolean test(Thread thread) {
176                             return p.test(thread) ||
177                                     thread instanceof FastThreadLocalThread &&
178                                             !((FastThreadLocalThread) thread).permitBlockingCalls();
179                         }
180                     };
181                 }
182             });
183         }
184 
185         @Override
186         public int compareTo(BlockHoundIntegration o) {
187             return 0;
188         }
189     }
190 }