Skip to content

Commit

Permalink
Use @SuppressJava6Requirement for animal sniffer plugin to ensure we …
Browse files Browse the repository at this point in the history
…always guard correctly (netty#9655)

Motivation:

We can use the `@SuppressJava6Requirement` annotation to be more precise about when we use Java6+ APIs. This helps us to ensure we always protect these places.

Modifications:

Make use of `@SuppressJava6Requirement` explicit

Result:

Fixes netty#2509.
  • Loading branch information
normanmaurer authored Oct 14, 2019
1 parent 833f11b commit 6c05d16
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;

import java.util.zip.Deflater;

Expand Down Expand Up @@ -70,11 +72,17 @@ private ByteBuf encode(ByteBufAllocator alloc, int len) {
}
}

@SuppressJava6Requirement(reason = "Guarded by java version check")
private boolean compressInto(ByteBuf compressed) {
byte[] out = compressed.array();
int off = compressed.arrayOffset() + compressed.writerIndex();
int toWrite = compressed.writableBytes();
int numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
final int numBytes;
if (PlatformDependent.javaVersion() >= 7) {
numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
} else {
numBytes = compressor.deflate(out, off, toWrite);
}
compressed.writerIndex(compressed.writerIndex() + numBytes);
return numBytes == toWrite;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.netty.channel.ChannelPromise;
import io.netty.channel.ChannelPromiseNotifier;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;

import java.util.concurrent.TimeUnit;
import java.util.zip.CRC32;
Expand Down Expand Up @@ -320,7 +322,11 @@ private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromi
return ctx.writeAndFlush(footer, promise);
}

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
private void deflate(ByteBuf out) {
if (PlatformDependent.javaVersion() < 7) {
deflateJdk6(out);
}
int numBytes;
do {
int writerIndex = out.writerIndex();
Expand All @@ -330,6 +336,16 @@ private void deflate(ByteBuf out) {
} while (numBytes > 0);
}

private void deflateJdk6(ByteBuf out) {
int numBytes;
do {
int writerIndex = out.writerIndex();
numBytes = deflater.deflate(
out.array(), out.arrayOffset() + writerIndex, out.writableBytes());
out.writerIndex(writerIndex + numBytes);
} while (numBytes > 0);
}

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
this.ctx = ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.concurrent.atomic.LongAdder;

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
final class LongAdderCounter extends LongAdder implements LongCounter {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ private NativeLibraryLoader() {

private static final class NoexecVolumeDetector {

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
private static boolean canExecuteExecutable(File file) throws IOException {
if (PlatformDependent.javaVersion() < 7) {
// Pre-JDK7, the Java API did not directly support POSIX permissions; instead of implementing a custom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void freeDirectBuffer(ByteBuffer buffer) {
if (javaVersion() >= 7) {
RANDOM_PROVIDER = new ThreadLocalRandomProvider() {
@Override
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
public Random current() {
return java.util.concurrent.ThreadLocalRandom.current();
}
Expand Down Expand Up @@ -964,6 +965,7 @@ public static ClassLoader getSystemClassLoader() {
/**
* Returns a new concurrent {@link Deque}.
*/
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
public static <C> Deque<C> newConcurrentDeque() {
if (javaVersion() < 7) {
return new LinkedBlockingDeque<C>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/**
* The {@link PlatformDependent} operations which requires access to {@code sun.misc.*}.
*/
@SuppressJava6Requirement(reason = "Unsafe access is guarded")
final class PlatformDependent0 {

private static final InternalLogger logger = InternalLoggerFactory.getInstance(PlatformDependent0.class);
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/io/netty/util/internal/SocketUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public Boolean run() throws IOException {
}
}

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
public static void bind(final SocketChannel socketChannel, final SocketAddress address) throws IOException {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
Expand Down Expand Up @@ -115,6 +116,7 @@ public SocketChannel run() throws IOException {
}
}

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
public static void bind(final DatagramChannel networkChannel, final SocketAddress address) throws IOException {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
Expand Down Expand Up @@ -182,6 +184,7 @@ public Enumeration<InetAddress> run() {
});
}

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
public static InetAddress loopbackAddress() {
return AccessController.doPrivileged(new PrivilegedAction<InetAddress>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Annotation to suppress the Java 6 source code requirement checks for a method.
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR })
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE })
public @interface SuppressJava6Requirement {

String reason();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.handler.ssl;

import io.netty.util.internal.SuppressJava6Requirement;

import javax.net.ssl.ExtendedSSLSession;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLPeerUnverifiedException;
Expand All @@ -29,6 +31,7 @@
* Delegates all operations to a wrapped {@link OpenSslSession} except the methods defined by {@link ExtendedSSLSession}
* itself.
*/
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
abstract class ExtendedOpenSslSession extends ExtendedSSLSession implements OpenSslSession {

// TODO: use OpenSSL API to actually fetch the real data but for now just do what Conscrypt does:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.handler.ssl;

import io.netty.util.internal.SuppressJava6Requirement;

import javax.net.ssl.SSLParameters;
import java.security.AlgorithmConstraints;

Expand All @@ -29,6 +31,7 @@ private Java7SslParametersUtils() {
* {@link AlgorithmConstraints} in the code. This helps us to not get into trouble when using it in java
* version < 7 and especially when using on android.
*/
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
static void setAlgorithmConstraints(SSLParameters sslParameters, Object algorithmConstraints) {
sslParameters.setAlgorithmConstraints((AlgorithmConstraints) algorithmConstraints);
}
Expand Down
3 changes: 3 additions & 0 deletions handler/src/main/java/io/netty/handler/ssl/Java8SslUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.handler.ssl;

import io.netty.util.internal.SuppressJava6Requirement;

import javax.net.ssl.SNIHostName;
import javax.net.ssl.SNIMatcher;
import javax.net.ssl.SNIServerName;
Expand All @@ -25,6 +27,7 @@
import java.util.Iterator;
import java.util.List;

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
final class Java8SslUtils {

private Java8SslUtils() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.handler.ssl;

import io.netty.util.internal.StringUtil;
import io.netty.util.internal.SuppressJava6Requirement;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
Expand All @@ -30,6 +31,7 @@
import static io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectionListener;
import static io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelector;

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
final class Java9SslEngine extends JdkSslEngine {
private final ProtocolSelectionListener selectionListener;
private final AlpnSelector alpnSelector;
Expand Down
2 changes: 2 additions & 0 deletions handler/src/main/java/io/netty/handler/ssl/Java9SslUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
final class Java9SslUtils {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Java9SslUtils.class);
private static final Method SET_APPLICATION_PROTOCOLS;
Expand Down
3 changes: 3 additions & 0 deletions handler/src/main/java/io/netty/handler/ssl/JdkSslEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.handler.ssl;

import io.netty.util.internal.SuppressJava6Requirement;

import java.nio.ByteBuffer;

import javax.net.ssl.SSLEngine;
Expand Down Expand Up @@ -145,6 +147,7 @@ public void setEnabledProtocols(String[] strings) {
engine.setEnabledProtocols(strings);
}

@SuppressJava6Requirement(reason = "Can only be called when running on JDK7+")
@Override
public SSLSession getHandshakeSession() {
return engine.getHandshakeSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.handler.ssl;

import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLPeerUnverifiedException;
Expand All @@ -35,6 +36,7 @@
* default {@link X509ExtendedTrustManager} implementations provided by the JDK that can not handle a protocol version
* of {@code TLSv1.3}.
*/
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
final class OpenSslTlsv13X509ExtendedTrustManager extends X509ExtendedTrustManager {

private final X509ExtendedTrustManager tm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.handler.ssl;

import io.netty.util.internal.SuppressJava6Requirement;

import javax.security.auth.x500.X500Principal;
import java.io.ByteArrayInputStream;
import java.math.BigInteger;
Expand Down Expand Up @@ -81,7 +83,7 @@ public Collection<List<?>> getIssuerAlternativeNames() throws CertificateParsing
}

// No @Override annotation as it was only introduced in Java8.
@Override
@SuppressJava6Requirement(reason = "Can only be called from Java8 as class is package-private")
public void verify(PublicKey key, Provider sigProvider)
throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
unwrap().verify(key, sigProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SuppressJava6Requirement;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

Expand All @@ -39,6 +40,7 @@
* This is really a "hack" until there is an official API as requested on the in
* <a href="https://bugs.openjdk.java.net/projects/JDK/issues/JDK-8210843">JDK-8210843</a>.
*/
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
final class OpenSslX509TrustManagerWrapper {
private static final InternalLogger LOGGER = InternalLoggerFactory
.getInstance(OpenSslX509TrustManagerWrapper.class);
Expand Down Expand Up @@ -163,6 +165,7 @@ private static final class UnsafeTrustManagerWrapper implements TrustManagerWrap
this.tmOffset = tmOffset;
}

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
@Override
public X509TrustManager wrapIfNeeded(X509TrustManager manager) {
if (!(manager instanceof X509ExtendedTrustManager)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.handler.ssl;

import io.netty.internal.tcnative.CertificateCallback;
import io.netty.util.internal.SuppressJava6Requirement;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.internal.tcnative.SSL;
Expand Down Expand Up @@ -155,13 +156,7 @@ static OpenSslSessionContext newSessionContext(ReferenceCountedOpenSslContext th
//
// See https://github.com/netty/netty/issues/5372

// Use this to prevent an error when running on java < 7
if (useExtendedTrustManager(manager)) {
SSLContext.setCertVerifyCallback(ctx,
new ExtendedTrustManagerVerifyCallback(engineMap, (X509ExtendedTrustManager) manager));
} else {
SSLContext.setCertVerifyCallback(ctx, new TrustManagerVerifyCallback(engineMap, manager));
}
setVerifyCallback(ctx, engineMap, manager);
} catch (Exception e) {
if (keyMaterialProvider != null) {
keyMaterialProvider.destroy();
Expand All @@ -178,6 +173,17 @@ static OpenSslSessionContext newSessionContext(ReferenceCountedOpenSslContext th
}
}

@SuppressJava6Requirement(reason = "Guarded by java version check")
private static void setVerifyCallback(long ctx, OpenSslEngineMap engineMap, X509TrustManager manager) {
// Use this to prevent an error when running on java < 7
if (useExtendedTrustManager(manager)) {
SSLContext.setCertVerifyCallback(ctx,
new ExtendedTrustManagerVerifyCallback(engineMap, (X509ExtendedTrustManager) manager));
} else {
SSLContext.setCertVerifyCallback(ctx, new TrustManagerVerifyCallback(engineMap, manager));
}
}

// No cache is currently supported for client side mode.
static final class OpenSslClientSessionContext extends OpenSslSessionContext {
OpenSslClientSessionContext(ReferenceCountedOpenSslContext context, OpenSslKeyMaterialProvider provider) {
Expand Down Expand Up @@ -234,6 +240,7 @@ void verify(ReferenceCountedOpenSslEngine engine, X509Certificate[] peerCerts, S
}
}

@SuppressJava6Requirement(reason = "Usage guarded by java version check")
private static final class ExtendedTrustManagerVerifyCallback extends AbstractCertificateVerifier {
private final X509ExtendedTrustManager manager;

Expand Down
Loading

0 comments on commit 6c05d16

Please sign in to comment.