From 9daa6c3a64460d0443c5a36f161cd1b3cd438937 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 11 Jan 2019 14:13:13 -0500 Subject: [PATCH] Prettify JNI stuff. --- native/compile-osx.sh | 2 +- native/src/main/c/jni_util.c | 2 +- native/src/main/c/jni_zlib_common.c | 29 ++++++++ native/src/main/c/jni_zlib_common.h | 6 ++ native/src/main/c/jni_zlib_deflate.c | 62 +++++++----------- native/src/main/c/jni_zlib_inflate.c | 55 ++++++---------- .../resources/linux_x64/velocity-compress.so | Bin 13968 -> 13928 bytes .../resources/macosx/velocity-compress.dylib | Bin 10332 -> 10388 bytes .../compression/VelocityCompressorTest.java | 7 ++ 9 files changed, 87 insertions(+), 76 deletions(-) create mode 100644 native/src/main/c/jni_zlib_common.c create mode 100644 native/src/main/c/jni_zlib_common.h diff --git a/native/compile-osx.sh b/native/compile-osx.sh index b1404339e..3cdbeafd3 100755 --- a/native/compile-osx.sh +++ b/native/compile-osx.sh @@ -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" 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 \ $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 \ No newline at end of file diff --git a/native/src/main/c/jni_util.c b/native/src/main/c/jni_util.c index e5356c93f..1e2b6bd8c 100644 --- a/native/src/main/c/jni_util.c +++ b/native/src/main/c/jni_util.c @@ -1,7 +1,7 @@ #include #include "jni_util.h" -JNIEXPORT void JNICALL +void JNICALL throwException(JNIEnv *env, const char *type, const char *msg) { jclass klazz = (*env)->FindClass(env, type); diff --git a/native/src/main/c/jni_zlib_common.c b/native/src/main/c/jni_zlib_common.c new file mode 100644 index 000000000..20d320311 --- /dev/null +++ b/native/src/main/c/jni_zlib_common.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#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; + } +} \ No newline at end of file diff --git a/native/src/main/c/jni_zlib_common.h b/native/src/main/c/jni_zlib_common.h new file mode 100644 index 000000000..ff4bc9a80 --- /dev/null +++ b/native/src/main/c/jni_zlib_common.h @@ -0,0 +1,6 @@ +#include +#include +#include + +void JNICALL +check_zlib_free(JNIEnv *env, z_stream *stream, bool deflate); \ No newline at end of file diff --git a/native/src/main/c/jni_zlib_deflate.c b/native/src/main/c/jni_zlib_deflate.c index c0195ff38..e76ba3053 100644 --- a/native/src/main/c/jni_zlib_deflate.c +++ b/native/src/main/c/jni_zlib_deflate.c @@ -1,8 +1,10 @@ #include #include +#include #include #include #include "jni_util.h" +#include "jni_zlib_common.h" static jfieldID finishedID; static jfieldID consumedID; @@ -27,27 +29,28 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_init(JNIEnv *env, return 0; } - char *msg; int ret = deflateInit(stream, level); - - switch (ret) { - case Z_OK: - return (jlong) stream; - case Z_MEM_ERROR: - free(stream); - throwException(env, "java/lang/OutOfMemoryError", "zlib init"); - return 0; - case Z_STREAM_ERROR: - free(stream); - char message[32]; - snprintf(message, 32, "invalid level %d", level); - throwException(env, "java/lang/IllegalArgumentException", message); - return 0; - default: - msg = stream->msg; - free(stream); - throwException(env, "java/util/zip/DataFormatException", msg); - return 0; + if (ret == Z_OK) { + return (jlong) stream; + } else { + char *zlib_msg = stream->msg; + free(stream); + switch (ret) { + case Z_MEM_ERROR: + throwException(env, "java/lang/OutOfMemoryError", "zlib init"); + break; + case Z_STREAM_ERROR: { + // Thanks Ken and Ritchie! + char message[32]; + snprintf(message, 32, "invalid level %d", level); + throwException(env, "java/lang/IllegalArgumentException", message); + break; + } + default: + throwException(env, "java/util/zip/DataFormatException", zlib_msg); + break; + } + return 0; } } @@ -57,24 +60,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_free(JNIEnv *env, jlong ctx) { z_stream* stream = (z_stream*) ctx; - int ret = deflateEnd(stream); - 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; - } + check_zlib_free(env, stream, true); } JNIEXPORT int JNICALL diff --git a/native/src/main/c/jni_zlib_inflate.c b/native/src/main/c/jni_zlib_inflate.c index 0695e4c91..816670e43 100644 --- a/native/src/main/c/jni_zlib_inflate.c +++ b/native/src/main/c/jni_zlib_inflate.c @@ -1,8 +1,10 @@ #include #include +#include #include #include #include "jni_util.h" +#include "jni_zlib_common.h" static jfieldID finishedID; static jfieldID consumedID; @@ -26,25 +28,23 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_init(JNIEnv *env, return 0; } - char *msg; int ret = inflateInit(stream); - - switch (ret) { - case Z_OK: - return (jlong) stream; - case Z_MEM_ERROR: - free(stream); - throwException(env, "java/lang/OutOfMemoryError", "zlib init"); - return 0; - case Z_STREAM_ERROR: - free(stream); - throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?"); - return 0; - default: - msg = stream->msg; - free(stream); - throwException(env, "java/util/zip/DataFormatException", msg); - return 0; + if (ret == Z_OK) { + return (jlong) stream; + } else { + char *zlib_msg = stream->msg; + free(stream); + switch (ret) { + case Z_MEM_ERROR: + throwException(env, "java/lang/OutOfMemoryError", "zlib init"); + return 0; + case Z_STREAM_ERROR: + throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?"); + return 0; + default: + throwException(env, "java/util/zip/DataFormatException", zlib_msg); + return 0; + } } } @@ -54,24 +54,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_free(JNIEnv *env, jlong ctx) { z_stream* stream = (z_stream*) ctx; - int ret = inflateEnd(stream); - 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; - } + check_zlib_free(env, stream, false); } JNIEXPORT int JNICALL diff --git a/native/src/main/resources/linux_x64/velocity-compress.so b/native/src/main/resources/linux_x64/velocity-compress.so index e9655d774218967d8c17c250f4b01333f3f3d412..53a2d8597a8977a556696de6072c2ed8310f95e6 100755 GIT binary patch delta 4057 zcmZu!4Nz3q6~1qm#r=`}0hV2u!F~M6kIF78V4PC21oh!C4OVf+9|UW~XiNvGv`nUU zH$km$$aYqb)6gbuwVgDWPG(4HX3!>zK@&&&gQ;;$$42ZlaksTeiLD}Px98kwK@K&G#PMQErJEG;QeM(I2Gu zXKKo{gr(BYf+Oq}yEy*pE5EB>-?@4uwcT{{wFnW7c)Rc>6SMGsL1Jbp>~y@+n6mXi zip1M!uw2MCv`Qg3ZSX+Ym?Al0vy=f*gI8J$6~!JH#y2mF8$41Ge_lYQ7xIiNq?4q> zfYB@ck~7DQOG42!G>KQWCfEsd!^u1>EXxAX#GfU`(%EblaBBKu(ds}x;bY`FUT(}hV%PFqVsPLg#^xZ4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+?jZv`tuRn4m?)SJPd^Z8Tc^TE~BLIsO_Op!-3yr*pYs&;~?tA@nX# z{_pR{==wk#u+F2PTtZQ-XalxD3+OJ;yO?654S|;8lTwNj83MhH%7WF7Phm(4gOyXN z5z6{2Ba*^Lqm_8I({xX;-d25@27=R)Ecrd&HVjA{C>D7A_uJy$(rxx8uh!#e@iy9S z@p!c+Z)x1?U*L5$=XZm>&Rg0f*rxn7V_7|a`4}bqF2Q>_CZb`2DfAWi@3k%Pmfp|) zx6O6Lz12O=#op*9kGJ}3UJa3fK%DFIn;?mgkt`DDCw90V4qIsFqu#xijEi4f@!POKH4)atRAO>(;{jEotzd9?2@N1K63CgLgnFzx}R7X6ko7MCU3Iz3W8@rH5~+Hp4&IySvB$dZYdt_YQ15mLX-7q{l6D6vajz?MUP zsY^+lkY9W;`xSdrpB3$pX#XYre<1M_FEJ5R@TD#>-BLdBf;YfdO7$htzDak^2&wOL zzxh*%pLp?`i?e|r>n1OLawQaHKI9^9kts?^O%K&ohsu5^t;XNl6y-tIPJK+;YVr=) zDs4Z4T;pGZ!-lOuC(DZ`DL(XsY-$a99C6}JvkUSHwsLO}bKp(zKy^;7rw3}TQfK%7 zsm8AMk6d|l+=Xq=%pZ-p*Y!uPzSc(TBEM9lxH)=sqOacYJ%!S@F|7Hy(*G(y>^ zpN67Q_lVSvkXkqA{P)4yrw!Cx7javkmtoop%nA8zwdg3l$Qb!e7$$YroT5?5S=5n7 zi)Yn(tSc_QPxs8K4=5$($SnFUy*Ml4vC7^El5KW+v^X#OGE%M?u5o(C} t$rX#btp(do@z{K9_N}q`p8c9>=y%c4SiO6w+GOu1cl{i78O7^o{10Ede+~cu diff --git a/native/src/main/resources/macosx/velocity-compress.dylib b/native/src/main/resources/macosx/velocity-compress.dylib index b3dbcce4903c558811ac293f19339b73784e6e1b..afe4075d7951fa2c773ed8fbc466a257f5f290ac 100755 GIT binary patch delta 2246 zcmZ8ieN0nV6ufnME<)p^NxdYA zrjZJhr>+_SCb-(LAC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Kj|?yv)F6d^Fvz4J-1i? zrBa{JBIY7v9!6H&_?{Q zixBOliW~duHT{Oxaml^SyWP9fTeoA7&ktk1FKQ06*Ru)=mkP1HLdB;K zCu$%BnbIdR!S@qZl~w1=&9AM|^q+b1ka=Jt#H(NSMe2>+KQyr}>&#*=7qXQPZRos< zU2g!Zv{hV39>4Kb^#`e)jQsF#GMOTS%xNj3TbSSSlroUP+5o3A*wvgltk;rfsWN6J z6SLT5OTnTcc-9%;uOOG`ntHMOj||8?3+O+@J?LV2*{+f*?2vm6Nc(8!)(Wphw1mU| zy&f^M^O^HPHPy&;lCg7)(_$FA1_|Y3V!t#t@jPN_SJ%PS`jo5uU}JO1^zZ~;sBxlnCQO(s2iQd0~bZ$ClUBs$Ry4^GTb)Y&Dtis-D7*^O(ajqW=;Ry zyT$v8cd}ib(3xb{tJJXTG-RLHbskLGRl@_5>{^WlpV;NkS(*PQR;pNbw`2TKunq|q zbd1yeYbzs}TEwwl_IH-)02rK54){PN*ggNZC|aC)QWBca+d)54d3yzM)`Dx61AbRAxMtUJuQI%`GhvN zG}Q-MTgge<5@-zs$v2cJmF2DEI3*uY6B)`w)^Qt}F*}-}>1cXp5~*@ssEnjl{-P}* zL*{QObBB_7z>B=48}77e?f~PlBJ0-qj3U5g9BDvs!REOl9f~N)fUz^)!N1eeo fEqu;_2hVggQzMJ1lfC2e_Dw8oSNdX$RZ7W!%whSG diff --git a/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java b/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java index 506228c07..0d37f8f67 100644 --- a/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java +++ b/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java @@ -1,5 +1,6 @@ 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.fail; import static org.junit.jupiter.api.condition.OS.LINUX; @@ -28,6 +29,12 @@ class VelocityCompressorTest { new Random(1).nextBytes(TEST_DATA); } + @Test + @EnabledOnOs({MAC, LINUX}) + void sanityCheckNative() { + assertThrows(IllegalArgumentException.class, () -> Natives.compress.get().create(-42)); + } + @Test @EnabledOnOs({MAC, LINUX}) void nativeIntegrityCheck() throws DataFormatException {