13
0
geforkt von Mirrors/Velocity

Prettify JNI stuff.

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-01-11 14:13:13 -05:00
Ursprung 1ddeb85f60
Commit 9daa6c3a64
9 geänderte Dateien mit 87 neuen und 76 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Hom
CFLAGS="-O3 -I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin/ -fPIC -shared" CFLAGS="-O3 -I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin/ -fPIC -shared"
clang $CFLAGS -lz src/main/c/jni_util.c src/main/c/jni_zlib_deflate.c src/main/c/jni_zlib_inflate.c \ clang $CFLAGS -lz src/main/c/jni_util.c src/main/c/jni_zlib_deflate.c src/main/c/jni_zlib_inflate.c \
-o src/main/resources/macosx/velocity-compress.dylib src/main/c/jni_zlib_common.c -o src/main/resources/macosx/velocity-compress.dylib
clang $CFLAGS -I $MBEDTLS_ROOT/include -shared $MBEDTLS_ROOT/library/aes.c $MBEDTLS_ROOT/library/aesni.c \ clang $CFLAGS -I $MBEDTLS_ROOT/include -shared $MBEDTLS_ROOT/library/aes.c $MBEDTLS_ROOT/library/aesni.c \
$MBEDTLS_ROOT/library/platform.c $MBEDTLS_ROOT/library/platform_util.c src/main/c/jni_util.c src/main/c/jni_cipher.c \ $MBEDTLS_ROOT/library/platform.c $MBEDTLS_ROOT/library/platform_util.c src/main/c/jni_util.c src/main/c/jni_cipher.c \
-o src/main/resources/macosx/velocity-cipher.dylib -o src/main/resources/macosx/velocity-cipher.dylib

Datei anzeigen

@ -1,7 +1,7 @@
#include <jni.h> #include <jni.h>
#include "jni_util.h" #include "jni_util.h"
JNIEXPORT void JNICALL void JNICALL
throwException(JNIEnv *env, const char *type, const char *msg) throwException(JNIEnv *env, const char *type, const char *msg)
{ {
jclass klazz = (*env)->FindClass(env, type); jclass klazz = (*env)->FindClass(env, type);

Datei anzeigen

@ -0,0 +1,29 @@
#include <jni.h>
#include <stdbool.h>
#include <stdlib.h>
#include <zlib.h>
#include "jni_util.h"
void JNICALL
check_zlib_free(JNIEnv *env, z_stream *stream, bool deflate)
{
int ret = deflate ? deflateEnd(stream) : inflateEnd(stream);
char *msg = stream->msg;
free((void*) stream);
switch (ret) {
case Z_OK:
break;
case Z_STREAM_ERROR:
if (msg == NULL) {
msg = "stream state inconsistent";
}
// fall-through
case Z_DATA_ERROR:
if (msg == NULL) {
msg = "data was discarded";
}
throwException(env, "java/lang/IllegalArgumentException", msg);
break;
}
}

Datei anzeigen

@ -0,0 +1,6 @@
#include <jni.h>
#include <stdbool.h>
#include <zlib.h>
void JNICALL
check_zlib_free(JNIEnv *env, z_stream *stream, bool deflate);

Datei anzeigen

@ -1,8 +1,10 @@
#include <assert.h> #include <assert.h>
#include <jni.h> #include <jni.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <zlib.h> #include <zlib.h>
#include "jni_util.h" #include "jni_util.h"
#include "jni_zlib_common.h"
static jfieldID finishedID; static jfieldID finishedID;
static jfieldID consumedID; static jfieldID consumedID;
@ -27,27 +29,28 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_init(JNIEnv *env,
return 0; return 0;
} }
char *msg;
int ret = deflateInit(stream, level); int ret = deflateInit(stream, level);
if (ret == Z_OK) {
switch (ret) { return (jlong) stream;
case Z_OK: } else {
return (jlong) stream; char *zlib_msg = stream->msg;
case Z_MEM_ERROR: free(stream);
free(stream); switch (ret) {
throwException(env, "java/lang/OutOfMemoryError", "zlib init"); case Z_MEM_ERROR:
return 0; throwException(env, "java/lang/OutOfMemoryError", "zlib init");
case Z_STREAM_ERROR: break;
free(stream); case Z_STREAM_ERROR: {
char message[32]; // Thanks Ken and Ritchie!
snprintf(message, 32, "invalid level %d", level); char message[32];
throwException(env, "java/lang/IllegalArgumentException", message); snprintf(message, 32, "invalid level %d", level);
return 0; throwException(env, "java/lang/IllegalArgumentException", message);
default: break;
msg = stream->msg; }
free(stream); default:
throwException(env, "java/util/zip/DataFormatException", msg); throwException(env, "java/util/zip/DataFormatException", zlib_msg);
return 0; break;
}
return 0;
} }
} }
@ -57,24 +60,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_free(JNIEnv *env,
jlong ctx) jlong ctx)
{ {
z_stream* stream = (z_stream*) ctx; z_stream* stream = (z_stream*) ctx;
int ret = deflateEnd(stream); check_zlib_free(env, stream, true);
char *msg = stream->msg;
free((void*) ctx);
switch (ret) {
case Z_OK:
break;
case Z_STREAM_ERROR:
if (msg == NULL) {
msg = "stream state inconsistent";
}
case Z_DATA_ERROR:
if (msg == NULL) {
msg = "data was discarded";
}
throwException(env, "java/lang/IllegalArgumentException", msg);
break;
}
} }
JNIEXPORT int JNICALL JNIEXPORT int JNICALL

Datei anzeigen

@ -1,8 +1,10 @@
#include <assert.h> #include <assert.h>
#include <jni.h> #include <jni.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <zlib.h> #include <zlib.h>
#include "jni_util.h" #include "jni_util.h"
#include "jni_zlib_common.h"
static jfieldID finishedID; static jfieldID finishedID;
static jfieldID consumedID; static jfieldID consumedID;
@ -26,25 +28,23 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_init(JNIEnv *env,
return 0; return 0;
} }
char *msg;
int ret = inflateInit(stream); int ret = inflateInit(stream);
if (ret == Z_OK) {
switch (ret) { return (jlong) stream;
case Z_OK: } else {
return (jlong) stream; char *zlib_msg = stream->msg;
case Z_MEM_ERROR: free(stream);
free(stream); switch (ret) {
throwException(env, "java/lang/OutOfMemoryError", "zlib init"); case Z_MEM_ERROR:
return 0; throwException(env, "java/lang/OutOfMemoryError", "zlib init");
case Z_STREAM_ERROR: return 0;
free(stream); case Z_STREAM_ERROR:
throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?"); throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?");
return 0; return 0;
default: default:
msg = stream->msg; throwException(env, "java/util/zip/DataFormatException", zlib_msg);
free(stream); return 0;
throwException(env, "java/util/zip/DataFormatException", msg); }
return 0;
} }
} }
@ -54,24 +54,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_free(JNIEnv *env,
jlong ctx) jlong ctx)
{ {
z_stream* stream = (z_stream*) ctx; z_stream* stream = (z_stream*) ctx;
int ret = inflateEnd(stream); check_zlib_free(env, stream, false);
char *msg = stream->msg;
free((void*) ctx);
switch (ret) {
case Z_OK:
break;
case Z_STREAM_ERROR:
if (msg == NULL) {
msg = "stream state inconsistent";
}
case Z_DATA_ERROR:
if (msg == NULL) {
msg = "data was discarded";
}
throwException(env, "java/lang/IllegalArgumentException", msg);
break;
}
} }
JNIEXPORT int JNICALL JNIEXPORT int JNICALL

Datei anzeigen

@ -1,5 +1,6 @@
package com.velocitypowered.natives.compression; package com.velocitypowered.natives.compression;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.condition.OS.LINUX; import static org.junit.jupiter.api.condition.OS.LINUX;
@ -28,6 +29,12 @@ class VelocityCompressorTest {
new Random(1).nextBytes(TEST_DATA); new Random(1).nextBytes(TEST_DATA);
} }
@Test
@EnabledOnOs({MAC, LINUX})
void sanityCheckNative() {
assertThrows(IllegalArgumentException.class, () -> Natives.compress.get().create(-42));
}
@Test @Test
@EnabledOnOs({MAC, LINUX}) @EnabledOnOs({MAC, LINUX})
void nativeIntegrityCheck() throws DataFormatException { void nativeIntegrityCheck() throws DataFormatException {