Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for SSE 4.2 and PCLMUL support to native zlib, add system properties for disabling natives #3718

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions native/src/main/c/NativeCompressImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <zlib.h>
#include "shared.h"
#include "cpuid_helper.h"
#include "net_md_5_bungee_jni_zlib_NativeCompressImpl.h"

typedef unsigned char byte;
Expand All @@ -26,6 +27,10 @@ jint throwException(JNIEnv *env, const char* message, int err) {
return (*env)->Throw(env, throwable);
}

JNIEXPORT jboolean JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_checkSupported(JNIEnv* env, jobject obj) {
return (jboolean) checkCompressionNativesSupport();
}

void JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_reset(JNIEnv* env, jobject obj, jlong ctx, jboolean compress) {
z_stream* stream = (z_stream*) ctx;
int ret = (compress) ? deflateReset(stream) : inflateReset(stream);
Expand Down
22 changes: 22 additions & 0 deletions native/src/main/c/cpuid_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Header to check for SSE 4.2 support in compression natives
// GCC only!

#ifndef _INCLUDE_CPUID_HELPER_H
#define _INCLUDE_CPUID_HELPER_H

#include <stdbool.h>
#include <cpuid.h>

static inline bool checkCompressionNativesSupport() {
unsigned int eax;
unsigned int ebx;
unsigned int ecx;
unsigned int edx;
if(__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return (ecx & bit_PCLMUL) != 0 && (ecx & bit_SSE4_2) != 0;
}else {
return false;
}
}

#endif // _INCLUDE_CPUID_HELPER_H

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion native/src/main/java/net/md_5/bungee/jni/NativeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ public final class NativeCode<T>
private final String name;
private final Supplier<? extends T> javaImpl;
private final Supplier<? extends T> nativeImpl;
private final boolean enableNativeFlag;
private final boolean extendedSupportCheck;
//
private boolean loaded;

public NativeCode(String name, Supplier<? extends T> javaImpl, Supplier<? extends T> nativeImpl)
{
this( name, javaImpl, nativeImpl, false );
}

public NativeCode(String name, Supplier<? extends T> javaImpl, Supplier<? extends T> nativeImpl, boolean extendedSupportCheck)
{
this.name = name;
this.javaImpl = javaImpl;
this.nativeImpl = nativeImpl;
this.enableNativeFlag = Boolean.parseBoolean( System.getProperty( "net.md_5.bungee.jni." + name + ".enable", "true" ) );
this.extendedSupportCheck = extendedSupportCheck;
}

public T newInstance()
Expand All @@ -32,7 +41,7 @@ public T newInstance()

public boolean load()
{
if ( !loaded && isSupported() )
if ( enableNativeFlag && !loaded && isSupported() )
{
String fullName = "bungeecord-" + name;

Expand All @@ -59,13 +68,23 @@ public boolean load()
}

System.load( temp.getPath() );

if ( extendedSupportCheck )
{
// Should throw NativeCodeException if incompatible
nativeImpl.get();
}

loaded = true;
} catch ( IOException ex )
{
// Can't write to tmp?
} catch ( UnsatisfiedLinkError ex )
{
System.out.println( "Could not load native library: " + ex.getMessage() );
} catch ( NativeCodeException ex )
{
System.out.println( "Native library " + name + " is incompatible: " + ex.getMessage() );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ public NativeCodeException(String message, int reason)
{
super( message + " : " + reason );
}

public NativeCodeException(String message)
{
super( message );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class NativeCompressImpl

static native void initFields();

native boolean checkSupported();

native void end(long ctx, boolean compress);

native void reset(long ctx, boolean compress);
Expand Down
8 changes: 8 additions & 0 deletions native/src/main/java/net/md_5/bungee/jni/zlib/NativeZlib.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public class NativeZlib implements BungeeZlib
private boolean compress;
private long ctx;

public NativeZlib()
{
if ( !nativeCompress.checkSupported() )
{
throw new NativeCodeException( "This CPU does not support the required SSE 4.2 and/or PCLMUL extensions!" );
}
}

@Override
public void init(boolean compress, int level)
{
Expand Down
Binary file modified native/src/main/resources/native-compress.so
Binary file not shown.
2 changes: 1 addition & 1 deletion native/src/test/java/net/md_5/bungee/NativeZlibTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class NativeZlibTest
{

private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );
private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new, true );

@Test
public void doTest() throws DataFormatException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
public class CompressFactory
{

public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );
public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new, true );
}