Skip to content

Commit

Permalink
Use direct method mapping for zstd (#108172)
Browse files Browse the repository at this point in the history
JNA supports two types of mapping to native methods, proxying and direct
method mapping. Proxying is nicer for unit testing, but unfortunately
the proxied methods are lazily loaded. NativeAccess expects that methods
are linked during static init, before SecurityManager is initialized.
For any native methods called after security manager init, the proxied
method will fail.

This commit changes the zstd bindings to use direct method mapping so
that calling zstd methods does not fail when using JNA (pre Java 21).

closes #107504
closes #107770
  • Loading branch information
rjernst committed May 2, 2024
1 parent f218c7b commit 6180b08
Showing 1 changed file with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.elasticsearch.nativeaccess.jna;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

Expand All @@ -17,27 +16,25 @@

class JnaZstdLibrary implements ZstdLibrary {

private interface NativeFunctions extends Library {
long ZSTD_compressBound(int scrLen);
public static class NativeFunctions {
public static native long ZSTD_compressBound(int scrLen);

long ZSTD_compress(Pointer dst, int dstLen, Pointer src, int srcLen, int compressionLevel);
public static native long ZSTD_compress(Pointer dst, int dstLen, Pointer src, int srcLen, int compressionLevel);

boolean ZSTD_isError(long code);
public static native boolean ZSTD_isError(long code);

String ZSTD_getErrorName(long code);
public static native String ZSTD_getErrorName(long code);

long ZSTD_decompress(Pointer dst, int dstLen, Pointer src, int srcLen);
public static native long ZSTD_decompress(Pointer dst, int dstLen, Pointer src, int srcLen);
}

private final NativeFunctions functions;

JnaZstdLibrary() {
this.functions = Native.load("zstd", NativeFunctions.class);
Native.register(NativeFunctions.class, "zstd");
}

@Override
public long compressBound(int scrLen) {
return functions.ZSTD_compressBound(scrLen);
return NativeFunctions.ZSTD_compressBound(scrLen);
}

@Override
Expand All @@ -46,7 +43,7 @@ public long compress(CloseableByteBuffer dst, CloseableByteBuffer src, int compr
assert src instanceof JnaCloseableByteBuffer;
var nativeDst = (JnaCloseableByteBuffer) dst;
var nativeSrc = (JnaCloseableByteBuffer) src;
return functions.ZSTD_compress(
return NativeFunctions.ZSTD_compress(
nativeDst.memory.share(dst.buffer().position()),
dst.buffer().remaining(),
nativeSrc.memory.share(src.buffer().position()),
Expand All @@ -57,12 +54,12 @@ public long compress(CloseableByteBuffer dst, CloseableByteBuffer src, int compr

@Override
public boolean isError(long code) {
return functions.ZSTD_isError(code);
return NativeFunctions.ZSTD_isError(code);
}

@Override
public String getErrorName(long code) {
return functions.ZSTD_getErrorName(code);
return NativeFunctions.ZSTD_getErrorName(code);
}

@Override
Expand All @@ -71,7 +68,7 @@ public long decompress(CloseableByteBuffer dst, CloseableByteBuffer src) {
assert src instanceof JnaCloseableByteBuffer;
var nativeDst = (JnaCloseableByteBuffer) dst;
var nativeSrc = (JnaCloseableByteBuffer) src;
return functions.ZSTD_decompress(
return NativeFunctions.ZSTD_decompress(
nativeDst.memory.share(dst.buffer().position()),
dst.buffer().remaining(),
nativeSrc.memory.share(src.buffer().position()),
Expand Down

0 comments on commit 6180b08

Please sign in to comment.