GeezerVison is showing github.com — JDK8 "Instance field not found" when using "-Xcheck:jni" · Issue #1203 · async-profiler/async-profiler
JDK8 "Instance field not found" when using "-Xcheck:jni" #1203DescriptionDescribe the bug
While working on the #1174 issue it was noticed that when starting the JVM with the -Xcheck:jni option that the async profiler will crash when using JDK 8 with the FATAL ERROR in native method: Instance field not found in JNI get/set field operations message
Expected vs. actual behavior
-Xcheck:jni should work normally with JDK 8
Reproduction Steps
You can use the tests created here #1192
by enabling back the -Xcheck:jni option & running the test using JDK 8
Additional Information/Context
No response
Async-profiler version
latest
Environment details
No response Activityapangin commented on Mar 31, 2025 I could not reproduce the error when running existing async-profiler tests or regular apps with JDK 8 and -Xcheck:jni.
I also don't see from async-profiler code how the error may happen, since when _klass jfieldID is constructed, it does not have checked bit and therefore is not subject to JNI checks.
So, the error seems specific to your new code added in #1192. Baraa-Hasheesh commented on Apr 1, 2025 ContributorAuthor More actions I will try to create a reproducer using the latest version with no changes to async-profiler Baraa-Hasheesh commented on Apr 1, 2025 ContributorAuthor More actions Hello @apangin
I've managed to create reproduction steps for the crash in JDK 8, I will share the steps here
I will be using some place holder values using the ${VALUE} syntax -> Please make sure to update all needed values
- Define the following CPP file (main.cpp)
/*
* Copyright The async-profiler authors
* SPDX-License-Identifier: Apache-2.0
*/
#include <dlfcn.h>
#include <jni.h>
#include <unistd.h>
#include <iostream>
#include <limits.h>
#include "asprof.h"
// detect arch for java 8
#if defined(__x86_64__) || defined(_M_X64)
#define JAVA8_ARCH_PATH "amd64/"
#elif defined(__i386) || defined(_M_IX86)
#define JAVA8_ARCH_PATH "i386/"
#elif defined(__aarch64__) || defined(_M_ARM64)
#define JAVA8_ARCH_PATH "aarch64/"
#elif defined(__arm__) || defined(_M_ARM)
#define JAVA8_ARCH_PATH "arm/"
#else
#define JAVA8_ARCH_PATH "/"
#endif
const char profiler_lib_path[] = "${PATH_TO_ASYNC_PROFILER_ROOT}/build/lib/libasyncProfiler.so";
const char jvm_lib_path[] = "lib/server/libjvm.so";
const char jvm8_lib_path[] = "lib/" JAVA8_ARCH_PATH "server/libjvm.so";
typedef jint (*CreateJvm)(JavaVM **, void **, void *);
void outputCallback(const char *buffer, size_t size) {
fwrite(buffer, sizeof(char), size, stderr);
}
int main(int argc, char **argv) {
asprof_init_t asprof_init;
JavaVM *jvm;
JNIEnv *env;
void *jvm_lib;
jint java_version = 0;
void *lib = dlopen(profiler_lib_path, RTLD_NOW | RTLD_GLOBAL);
if (lib == NULL) {
std::cerr << dlerror() << std::endl;
exit(1);
}
asprof_init = (asprof_init_t)dlsym(lib, "asprof_init");
asprof_init();
// Get Java home
char *java_home = getenv("TEST_JAVA_HOME");
if (java_home == NULL) {
std::cerr << "TEST_JAVA_HOME is not set" << std::endl;
exit(1);
}
// Get Java version
char *version = getenv("TEST_JAVA_VERSION");
if (version == NULL) {
std::cerr << "TEST_JAVA_VERSION is not set" << std::endl;
exit(1);
}
// Java 8 or higher
java_version = std::stoi(version);
if (java_version < 8) {
std::cerr << "Unsupported Java version: " << version << std::endl;
exit(1);
}
char lib_path[PATH_MAX];
snprintf(lib_path, sizeof(lib_path), "%s/%s", java_home, java_version == 8 ? jvm8_lib_path : jvm_lib_path);
jvm_lib = dlopen(lib_path, RTLD_LOCAL | RTLD_NOW);
if (jvm_lib == NULL) {
std::cerr << "Unable to find: " << lib_path << ", Error: " << dlerror() << std::endl;
exit(1);
}
// Start JVM
JavaVMInitArgs vm_args;
JavaVMOption options[2];
options[0].optionString = const_cast<char *>("-Djava.class.path=./"
":${PATH_TO_ASYNC_PROFILER_ROOT}/build/jar/async-profiler.jar");
options[1].optionString = const_cast<char *>("-Xcheck:jni");
// Configure JVM
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 2;
vm_args.options = options;
vm_args.ignoreUnrecognized = true;
CreateJvm createJvm = (CreateJvm)dlsym(jvm_lib, "JNI_CreateJavaVM");
if (createJvm == NULL) {
std::cerr << "Unable to find: JNI_CreateJavaVM" << std::endl;
exit(1);
}
// Create the JVM
jint rc = createJvm(&jvm, (void **)&env, &vm_args);
if (rc != JNI_OK) {
std::cerr << "Failed to create JVM" << std::endl;
exit(1);
}
jclass customClass = env->FindClass("JavaClass");
if (customClass == nullptr) {
std::cerr << "Can't find JavaClass" << std::endl;
exit(1);
}
jmethodID cpuHeavyTask = env->GetStaticMethodID(customClass, "cpuHeavyTask", "()D");
if (cpuHeavyTask == nullptr) {
std::cerr << "Can't find cpuHeavyTask" << std::endl;
exit(1);
}
jdouble result = env->CallStaticDoubleMethod(customClass, cpuHeavyTask);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
std::cerr << "Exception in cpuHeavyTask" << std::endl;
exit(1);
}
std::cout << "Result: " << result << std::endl;
env->DeleteLocalRef(customClass);
rc = jvm->DestroyJavaVM();
if (rc != JNI_OK) {
std::cerr << "Failed to destroy JVM" << std::endl;
exit(1);
}
return 0;
}
- Create the following Java Class (JavaClass.java)
import one.profiler.AsyncProfiler;
import one.profiler.Counter;
public class JavaClass {
private static AsyncProfiler profiler;
public static void main(String[] args) {
cpuHeavyTask();
}
public static double cpuHeavyTask() {
System.out.println("Hello World!");
// Initialize the profiler
profiler = AsyncProfiler.getInstance();
try {
// Start profiling CPU samples
profiler.start("cpu", 1000_000);
double sum = 0;
for (int i = 0; i < 1; i++) {
sum += Math.sqrt(Math.random());
sum += Math.pow(Math.random(), Math.random());
}
System.out.println(sum);
String profile = AsyncProfiler.getInstance().dumpCollapsed(Counter.SAMPLES);
System.out.println(profile);
} finally {
// Clean up
if (profiler != null) {
profiler.stop();
}
}
return 0.0;
}
}
-
Now using a terminal that has JDK 11 setup please do the following
3.1. compile async profiler found @ ${PATH_TO_ASYNC_PROFILER_ROOT} using the make command
3.2. Set the following env variable => export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${PATH_TO_ASYNC_PROFILER_ROOT}/build/lib
3.3. Compile the JavaClass.java file as such => javac -cp ".:${PATH_TO_ASYNC_PROFILER_ROOT}/build/jar/async-profiler.jar" JavaClass.java --release 8
3.4. compile the main.cpp file using the following => g++ -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -I${PATH_TO_ASYNC_PROFILER_ROOT}/src -L${PATH_TO_ASYNC_PROFILER_ROOT}/build/lib main.cpp -o main -ldl
-
To run on the JDK 11 terminal do the following env set as well
export TEST_JAVA_VERSION=11
export TEST_JAVA_HOME=$JAVA_HOME
- For JDK 8, you will need to create a new terminal configure with JDK 8 & set the following env variables
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${PATH_TO_ASYNC_PROFILER_ROOT}/build/lib
export TEST_JAVA_VERSION=8
export TEST_JAVA_HOME=$JAVA_HOME/jre
Please note the following:
- The above will only work on linux
- You must compile the using JDK 11
- Running the compiled c file with JDK 8 will result in the following crash
FATAL ERROR in native method: Instance field not found in JNI get/set field operations
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1934)
- locked <0x0000000580106d40> (a java.util.Vector)
- locked <0x00000005801518d8> (a java.util.Vector)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1850)
at java.lang.Runtime.loadLibrary0(Runtime.java:843)
- locked <0x000000058011adb8> (a java.lang.Runtime)
at java.lang.System.loadLibrary(System.java:1136)
at one.profiler.AsyncProfiler.getInstance(AsyncProfiler.java:50)
- locked <0x000000058018bf48> (a java.lang.Class for one.profiler.AsyncProfiler)
at one.profiler.AsyncProfiler.getInstance(AsyncProfiler.java:26)
at JavaClass.cpuHeavyTask(JavaClass.java:15)
- Running in JDK 11 will cause a warning but not crash
WARNING: JNI local refs: 675, exceeds capacity: 32
at java.lang.ClassLoader$NativeLibrary.load0([email protected]/Native Method)
at java.lang.ClassLoader$NativeLibrary.load([email protected]/ClassLoader.java:2450)
at java.lang.ClassLoader$NativeLibrary.loadLibrary([email protected]/ClassLoader.java:2506)
- locked <0x00000000fd0076c0> (a java.util.HashSet)
at java.lang.ClassLoader.loadLibrary0([email protected]/ClassLoader.java:2705)
at java.lang.ClassLoader.loadLibrary([email protected]/ClassLoader.java:2667)
at java.lang.Runtime.loadLibrary0([email protected]/Runtime.java:830)
at java.lang.System.loadLibrary([email protected]/System.java:1890)
at one.profiler.AsyncProfiler.getInstance(AsyncProfiler.java:50)
- locked <0x00000000fd0b4408> (a java.lang.Class for one.profiler.AsyncProfiler)
at one.profiler.AsyncProfiler.getInstance(AsyncProfiler.java:26)
at JavaClass.cpuHeavyTask(JavaClass.java:15)
added a commit that references this issue on Apr 15, 2026#1203: Fix "Instance field not found" when using `-Xcheck:jni` on JDK 8 apangin commented on Apr 15, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|