Skip to content

Commit

Permalink
Fix JNI error - don't use JNI while holding critical lock
Browse files Browse the repository at this point in the history
  • Loading branch information
luben committed Jun 3, 2020
1 parent c8fe216 commit 3d16e51
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ javacOptions ++= Seq("-source", "1.6", "-target", "1.6")

javacOptions in doc := Seq("-source", "1.6")

// Check at runtime for JNI errors when running tests
// fork := true
// javaOptions in Test ++= Seq("-Xcheck:jni")

// sbt-jni configuration
jniLibraryName := "zstd-jni"

Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolvers += Resolver.url("joprice-sbt-plugins", url("http://dl.bintray.com/content/joprice/sbt-plugins"))(Resolver.ivyStylePatterns)
resolvers += Resolver.url("joprice-sbt-plugins", url("https://dl.bintray.com/content/joprice/sbt-plugins"))(Resolver.ivyStylePatterns)

addSbtPlugin("com.github.joprice" % "sbt-jni" % "0.2.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.9.4")
Expand Down
14 changes: 10 additions & 4 deletions src/main/native/jni_fast_zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,13 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_compressDirec
jsize src_cap = (*env)->GetDirectBufferCapacity(env, src);
if (src_offset + src_size > src_cap) return ERROR(srcSize_wrong);

ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t)(*env)->GetLongField(env, jctx, compress_ctx_nativePtr);

char *dst_buff = (char*)(*env)->GetDirectBufferAddress(env, dst);
if (dst_buff == NULL) return ERROR(memory_allocation);
char *src_buff = (char*)(*env)->GetDirectBufferAddress(env, src);
if (src_buff == NULL) return ERROR(memory_allocation);

ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t)(*env)->GetLongField(env, jctx, compress_ctx_nativePtr);
ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
return ZSTD_compress2(cctx, dst_buff + dst_offset, (size_t) dst_size, src_buff + src_offset, (size_t) src_size);
}
Expand All @@ -363,13 +364,15 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_compressByteA
if (src_offset + src_size > (*env)->GetArrayLength(env, src)) return ERROR(srcSize_wrong);
if (dst_offset + dst_size > (*env)->GetArrayLength(env, dst)) return ERROR(dstSize_tooSmall);

ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t)(*env)->GetLongField(env, jctx, compress_ctx_nativePtr);

void *dst_buff = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
if (dst_buff == NULL) goto E1;
void *src_buff = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
if (src_buff == NULL) goto E2;

ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t)(*env)->GetLongField(env, jctx, compress_ctx_nativePtr);
ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);

size = ZSTD_compress2(cctx, dst_buff + dst_offset, (size_t) dst_size, src_buff + src_offset, (size_t) src_size);
(*env)->ReleasePrimitiveArrayCritical(env, src, src_buff, JNI_ABORT);
E2: (*env)->ReleasePrimitiveArrayCritical(env, dst, dst_buff, 0);
Expand Down Expand Up @@ -467,12 +470,13 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDecompressCtx_decompressD
jsize src_cap = (*env)->GetDirectBufferCapacity(env, src);
if (src_offset + src_size > src_cap) return ERROR(srcSize_wrong);

ZSTD_DCtx* dctx = (ZSTD_DCtx*)(intptr_t)(*env)->GetLongField(env, jctx, decompress_ctx_nativePtr);

char *dst_buff = (char*)(*env)->GetDirectBufferAddress(env, dst);
if (dst_buff == NULL) return ERROR(memory_allocation);
char *src_buff = (char*)(*env)->GetDirectBufferAddress(env, src);
if (src_buff == NULL) return ERROR(memory_allocation);

ZSTD_DCtx* dctx = (ZSTD_DCtx*)(intptr_t)(*env)->GetLongField(env, jctx, decompress_ctx_nativePtr);
ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only);
return ZSTD_decompressDCtx(dctx, dst_buff + dst_offset, (size_t) dst_size, src_buff + src_offset, (size_t) src_size);
}
Expand All @@ -493,14 +497,16 @@ JNIEXPORT jlong JNICALL Java_com_github_luben_zstd_ZstdDecompressCtx_decompressB
if (src_offset + src_size > (*env)->GetArrayLength(env, src)) return ERROR(srcSize_wrong);
if (dst_offset + dst_size > (*env)->GetArrayLength(env, dst)) return ERROR(dstSize_tooSmall);

ZSTD_DCtx* dctx = (ZSTD_DCtx*)(intptr_t)(*env)->GetLongField(env, jctx, decompress_ctx_nativePtr);

void *dst_buff = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
if (dst_buff == NULL) goto E1;
void *src_buff = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
if (src_buff == NULL) goto E2;

ZSTD_DCtx* dctx = (ZSTD_DCtx*)(intptr_t)(*env)->GetLongField(env, jctx, decompress_ctx_nativePtr);
ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only);
size = ZSTD_decompressDCtx(dctx, dst_buff + dst_offset, (size_t) dst_size, src_buff + src_offset, (size_t) src_size);

(*env)->ReleasePrimitiveArrayCritical(env, src, src_buff, JNI_ABORT);
E2: (*env)->ReleasePrimitiveArrayCritical(env, dst, dst_buff, 0);
E1: return size;
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.5-2
1.4.5-3

0 comments on commit 3d16e51

Please sign in to comment.