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