Skip to content

Commit 6c2de8d

Browse files
authored
JNI: Use GetByteArrayRegion/SetByteArrayRegion (#132)
JNI: Use GetByteArrayRegion/SetByteArrayRegion We used GetByteArrayElements/ReleaseByteArrayElements which would "pin" the JVM object and give us a pointer to its memory that we could use in native calls. This would interact with the GC and cause issues that would be platform-specific and very hard to debug. We now use GetByteArrayRegion/SetByteArrayRegion which do not "pin" the JVM ovject at all. Instead we copy data to a local buffer and use it, or copy it back from a local buffer to a JVM array.
1 parent 49140da commit 6c2de8d

File tree

6 files changed

+501
-478
lines changed

6 files changed

+501
-478
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ buildscript {
2323

2424
allprojects {
2525
group = "fr.acinq.secp256k1"
26-
version = "0.20.0"
26+
version = "0.21.0-SNAPSHOT"
2727

2828
repositories {
2929
google()

jni/c/headers/darwin/jni_md.h

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,66 @@
11
/*
2-
* @(#)jni_md.h 1.19 05/11/17
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34
*
4-
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5-
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
624
*/
725

826
#ifndef _JAVASOFT_JNI_MD_H_
927
#define _JAVASOFT_JNI_MD_H_
1028

11-
#define JNIEXPORT __attribute__((visibility("default")))
12-
#define JNIIMPORT
29+
#ifndef __has_attribute
30+
#define __has_attribute(x) 0
31+
#endif
32+
33+
#ifndef JNIEXPORT
34+
#if (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4) && (__GNUC_MINOR__ > 2))) || __has_attribute(visibility)
35+
#ifdef ARM
36+
#define JNIEXPORT __attribute__((externally_visible,visibility("default")))
37+
#else
38+
#define JNIEXPORT __attribute__((visibility("default")))
39+
#endif
40+
#else
41+
#define JNIEXPORT
42+
#endif
43+
#endif
44+
45+
#if (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4) && (__GNUC_MINOR__ > 2))) || __has_attribute(visibility)
46+
#ifdef ARM
47+
#define JNIIMPORT __attribute__((externally_visible,visibility("default")))
48+
#else
49+
#define JNIIMPORT __attribute__((visibility("default")))
50+
#endif
51+
#else
52+
#define JNIIMPORT
53+
#endif
54+
1355
#define JNICALL
1456

15-
#if defined(__LP64__) && __LP64__ /* for -Wundef */
1657
typedef int jint;
58+
#ifdef _LP64
59+
typedef long jlong;
1760
#else
18-
typedef long jint;
19-
#endif
2061
typedef long long jlong;
62+
#endif
63+
2164
typedef signed char jbyte;
2265

2366
#endif /* !_JAVASOFT_JNI_MD_H_ */

jni/c/headers/jni.h

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -765,6 +765,16 @@ struct JNINativeInterface_ {
765765

766766
jobjectRefType (JNICALL *GetObjectRefType)
767767
(JNIEnv* env, jobject obj);
768+
769+
/* Module Features */
770+
771+
jobject (JNICALL *GetModule)
772+
(JNIEnv* env, jclass clazz);
773+
774+
/* Virtual threads */
775+
776+
jboolean (JNICALL *IsVirtualThread)
777+
(JNIEnv* env, jobject obj);
768778
};
769779

770780
/*
@@ -1857,9 +1867,35 @@ struct JNIEnv_ {
18571867
return functions->GetObjectRefType(this, obj);
18581868
}
18591869

1870+
/* Module Features */
1871+
1872+
jobject GetModule(jclass clazz) {
1873+
return functions->GetModule(this, clazz);
1874+
}
1875+
1876+
/* Virtual threads */
1877+
1878+
jboolean IsVirtualThread(jobject obj) {
1879+
return functions->IsVirtualThread(this, obj);
1880+
}
1881+
18601882
#endif /* __cplusplus */
18611883
};
18621884

1885+
/*
1886+
* optionString may be any option accepted by the JVM, or one of the
1887+
* following:
1888+
*
1889+
* -D<name>=<value> Set a system property.
1890+
* -verbose[:class|gc|jni] Enable verbose output, comma-separated. E.g.
1891+
* "-verbose:class" or "-verbose:gc,class"
1892+
* Standard names include: gc, class, and jni.
1893+
* All nonstandard (VM-specific) names must begin
1894+
* with "X".
1895+
* vfprintf extraInfo is a pointer to the vfprintf hook.
1896+
* exit extraInfo is a pointer to the exit hook.
1897+
* abort extraInfo is a pointer to the abort hook.
1898+
*/
18631899
typedef struct JavaVMOption {
18641900
char *optionString;
18651901
void *extraInfo;
@@ -1951,6 +1987,12 @@ JNI_OnUnload(JavaVM *vm, void *reserved);
19511987
#define JNI_VERSION_1_2 0x00010002
19521988
#define JNI_VERSION_1_4 0x00010004
19531989
#define JNI_VERSION_1_6 0x00010006
1990+
#define JNI_VERSION_1_8 0x00010008
1991+
#define JNI_VERSION_9 0x00090000
1992+
#define JNI_VERSION_10 0x000a0000
1993+
#define JNI_VERSION_19 0x00130000
1994+
#define JNI_VERSION_20 0x00140000
1995+
#define JNI_VERSION_21 0x00150000
19541996

19551997
#ifdef __cplusplus
19561998
} /* extern "C" */

jni/c/headers/linux/jni_md.h

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
11
/*
2-
* %W% %E%
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34
*
4-
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5-
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
624
*/
725

826
#ifndef _JAVASOFT_JNI_MD_H_
927
#define _JAVASOFT_JNI_MD_H_
1028

11-
#define JNIEXPORT __attribute__((__visibility__("default")))
12-
#define JNIIMPORT
29+
#ifndef __has_attribute
30+
#define __has_attribute(x) 0
31+
#endif
32+
33+
#ifndef JNIEXPORT
34+
#if (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4) && (__GNUC_MINOR__ > 2))) || __has_attribute(visibility)
35+
#ifdef ARM
36+
#define JNIEXPORT __attribute__((externally_visible,visibility("default")))
37+
#else
38+
#define JNIEXPORT __attribute__((visibility("default")))
39+
#endif
40+
#else
41+
#define JNIEXPORT
42+
#endif
43+
#endif
44+
45+
#if (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4) && (__GNUC_MINOR__ > 2))) || __has_attribute(visibility)
46+
#ifdef ARM
47+
#define JNIIMPORT __attribute__((externally_visible,visibility("default")))
48+
#else
49+
#define JNIIMPORT __attribute__((visibility("default")))
50+
#endif
51+
#else
52+
#define JNIIMPORT
53+
#endif
54+
1355
#define JNICALL
1456

1557
typedef int jint;
16-
#ifdef _LP64 /* 64-bit Solaris */
58+
#ifdef _LP64
1759
typedef long jlong;
1860
#else
1961
typedef long long jlong;

jni/c/headers/mingw/jni_md.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
/*
2-
* @(#)jni_md.h 1.14 03/12/19
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34
*
4-
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5-
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
624
*/
725

826
#ifndef _JAVASOFT_JNI_MD_H_
927
#define _JAVASOFT_JNI_MD_H_
1028

11-
#define JNIEXPORT __declspec(dllexport)
29+
#ifndef JNIEXPORT
30+
#define JNIEXPORT __declspec(dllexport)
31+
#endif
1232
#define JNIIMPORT __declspec(dllimport)
1333
#define JNICALL __stdcall
1434

15-
#include <stdint.h>
16-
35+
// 'long' is always 32 bit on windows so this matches what jdk expects
1736
typedef long jint;
18-
typedef int64_t jlong;
37+
typedef __int64 jlong;
1938
typedef signed char jbyte;
2039

2140
#endif /* !_JAVASOFT_JNI_MD_H_ */

0 commit comments

Comments
 (0)