[#4828] OpenSslContext throws UnsupportedOperationException when Unsafe not available

Motivation:

OpenSslContext constructor fails with a UnsupportedOperationException if Unsafe is not present on the system.

Modifications:

Make OpenSslContext work also when Unsafe is not present by fallback to using JNI to get the memory address.

Result:

Using OpenSslContext also works on systems without Unsafe.
This commit is contained in:
Norman Maurer 2016-02-03 20:50:57 +01:00
parent c161e526b9
commit 701832bec3
3 changed files with 9 additions and 9 deletions

View File

@ -16,9 +16,11 @@
package io.netty.handler.ssl; package io.netty.handler.ssl;
import io.netty.buffer.ByteBuf;
import io.netty.util.internal.NativeLibraryLoader; import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.apache.tomcat.jni.Buffer;
import org.apache.tomcat.jni.Library; import org.apache.tomcat.jni.Library;
import org.apache.tomcat.jni.Pool; import org.apache.tomcat.jni.Pool;
import org.apache.tomcat.jni.SSL; import org.apache.tomcat.jni.SSL;
@ -190,5 +192,10 @@ public final class OpenSsl {
return errorCode != SSL.SSL_ERROR_NONE; return errorCode != SSL.SSL_ERROR_NONE;
} }
static long memoryAddress(ByteBuf buf) {
assert buf.isDirect();
return buf.hasMemoryAddress() ? buf.memoryAddress() : Buffer.address(buf.nioBuffer());
}
private OpenSsl() { } private OpenSsl() { }
} }

View File

@ -544,7 +544,7 @@ public abstract class OpenSslContext extends SslContext {
private static long newBIO(ByteBuf buffer) throws Exception { private static long newBIO(ByteBuf buffer) throws Exception {
long bio = SSL.newMemBIO(); long bio = SSL.newMemBIO();
int readable = buffer.readableBytes(); int readable = buffer.readableBytes();
if (SSL.writeToBIO(bio, buffer.memoryAddress(), readable) != readable) { if (SSL.writeToBIO(bio, OpenSsl.memoryAddress(buffer), readable) != readable) {
SSL.freeBIO(bio); SSL.freeBIO(bio);
throw new IllegalStateException("Could not write data to memory BIO"); throw new IllegalStateException("Could not write data to memory BIO");
} }

View File

@ -52,6 +52,7 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import static io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior; import static io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
import static io.netty.handler.ssl.OpenSsl.memoryAddress;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*; import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
import static javax.net.ssl.SSLEngineResult.Status.*; import static javax.net.ssl.SSLEngineResult.Status.*;
@ -1235,14 +1236,6 @@ public final class OpenSslEngine extends SSLEngine {
return FINISHED; return FINISHED;
} }
private static long memoryAddress(ByteBuf buf) {
if (buf.hasMemoryAddress()) {
return buf.memoryAddress();
} else {
return Buffer.address(buf.nioBuffer());
}
}
private SSLEngineResult.Status getEngineStatus() { private SSLEngineResult.Status getEngineStatus() {
return engineClosed? CLOSED : OK; return engineClosed? CLOSED : OK;
} }