13
0
geforkt von Mirrors/Velocity

Make sure we can get memory addresses before we think about natives.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-12-30 04:33:10 -05:00
Ursprung 977d29ffa0
Commit 1c50922cba
9 geänderte Dateien mit 82 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,5 @@
package com.velocitypowered.natives;
public interface Native {
boolean isNative();
}

Datei anzeigen

@ -62,4 +62,9 @@ public class JavaVelocityCompressor implements VelocityCompressor {
private void ensureNotDisposed() {
Preconditions.checkState(!disposed, "Object already disposed");
}
@Override
public boolean isNative() {
return false;
}
}

Datei anzeigen

@ -78,4 +78,9 @@ public class NativeVelocityCompressor implements VelocityCompressor {
}
disposed = true;
}
@Override
public boolean isNative() {
return true;
}
}

Datei anzeigen

@ -1,13 +1,14 @@
package com.velocitypowered.natives.compression;
import com.velocitypowered.natives.Disposable;
import com.velocitypowered.natives.Native;
import io.netty.buffer.ByteBuf;
import java.util.zip.DataFormatException;
/**
* Provides an interface to inflate and deflate {@link ByteBuf}s using zlib.
*/
public interface VelocityCompressor extends Disposable {
public interface VelocityCompressor extends Disposable, Native {
void inflate(ByteBuf source, ByteBuf destination) throws DataFormatException;
void deflate(ByteBuf source, ByteBuf destination) throws DataFormatException;

Datei anzeigen

@ -79,4 +79,9 @@ public class JavaVelocityCipher implements VelocityCipher {
private void ensureNotDisposed() {
Preconditions.checkState(!disposed, "Object already disposed");
}
@Override
public boolean isNative() {
return false;
}
}

Datei anzeigen

@ -1,5 +1,6 @@
package com.velocitypowered.natives.encryption;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.security.GeneralSecurityException;
@ -32,6 +33,7 @@ public class NativeVelocityCipher implements VelocityCipher {
@Override
public void process(ByteBuf source, ByteBuf destination) throws ShortBufferException {
ensureNotDisposed();
source.memoryAddress();
destination.memoryAddress();
@ -65,4 +67,13 @@ public class NativeVelocityCipher implements VelocityCipher {
}
disposed = true;
}
private void ensureNotDisposed() {
Preconditions.checkState(!disposed, "Object already disposed");
}
@Override
public boolean isNative() {
return true;
}
}

Datei anzeigen

@ -1,11 +1,12 @@
package com.velocitypowered.natives.encryption;
import com.velocitypowered.natives.Disposable;
import com.velocitypowered.natives.Native;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import javax.crypto.ShortBufferException;
public interface VelocityCipher extends Disposable {
public interface VelocityCipher extends Disposable, Native {
void process(ByteBuf source, ByteBuf destination) throws ShortBufferException;

Datei anzeigen

@ -0,0 +1,33 @@
package com.velocitypowered.natives.util;
import com.velocitypowered.natives.Native;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
public class MoreByteBufUtils {
private MoreByteBufUtils() {
throw new AssertionError();
}
/**
* Ensures the {@code buf} will work with the specified {@code nativeThing}. After this function
* is called, you should decrement the reference count on the {@code buf} with
* {@link ByteBuf#release()}.
*
* @param alloc the {@link ByteBufAllocator} to use
* @param nativeStuff the native we are working with
* @param buf the buffer we are working with
* @return a buffer compatible with the native
*/
public static ByteBuf ensureCompatible(ByteBufAllocator alloc, Native nativeStuff, ByteBuf buf) {
if (!nativeStuff.isNative() || buf.hasMemoryAddress()) {
// Will always work in either case.
return buf.retain();
}
// It's not, so we must make a memory copy.
ByteBuf newBuf = alloc.directBuffer();
newBuf.writeBytes(buf);
return newBuf;
}
}

Datei anzeigen

@ -1,18 +1,32 @@
package com.velocitypowered.natives.util;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.function.BooleanSupplier;
public class NativeConstraints {
private static final boolean NATIVES_ENABLED = !Boolean.getBoolean("velocity.natives-disabled");
private static final boolean CAN_GET_MEMORYADDRESS;
static {
ByteBuf test = Unpooled.directBuffer();
try {
CAN_GET_MEMORYADDRESS = test.hasMemoryAddress();
} finally {
test.release();
}
}
static final BooleanSupplier MACOS = () -> {
return NATIVES_ENABLED
&& CAN_GET_MEMORYADDRESS
&& System.getProperty("os.name", "").equalsIgnoreCase("Mac OS X")
&& System.getProperty("os.arch").equals("x86_64");
};
static final BooleanSupplier LINUX = () -> {
return NATIVES_ENABLED
&& CAN_GET_MEMORYADDRESS
&& System.getProperty("os.name", "").equalsIgnoreCase("Linux")
&& System.getProperty("os.arch").equals("amd64");
};