If an AOT cache is created with -XX:+AOTClassLinking, the Java program may not work with some agents that are loaded with -XX:+EnableDynamicAgentLoading.
For example, a dynamic agent may use ClassFileLoadHook that assumes classes are loaded on-demand, but with -XX:+AOTClassLinking, classes are all loaded at JVM start-up, so the agent may not get a chance to use ClassFileLoadHook to replace certain classes.
In filemap.cpp, we already disable the AOT cache when agents using ClassFileLoadHook are statically detected at VM bootstrap:
CDSConfig::set_has_aot_linked_classes(true);
if (JvmtiExport::should_post_class_file_load_hook()) {
log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
return false;
}
The proposed fix is to do the same check for EnableDynamicAgentLoading:
if (EnableDynamicAgentLoading) {
log_error(cds)("CDS archive has aot-linked classes. It cannot be used with -XX:+EnableDynamicAgentLoading");
return false;
}
Justification: we have no idea what capabilities are required by dynamically loaded agents, so it's better to be safe and disable the AOT cache. The application may not get the benefit of the AOT cache, but will behave correctness.
Work-around:
If your program requires -XX:+EnableDynamicAgentLoading, also add -XX:AOTMode=off to the command-line.
For example, a dynamic agent may use ClassFileLoadHook that assumes classes are loaded on-demand, but with -XX:+AOTClassLinking, classes are all loaded at JVM start-up, so the agent may not get a chance to use ClassFileLoadHook to replace certain classes.
In filemap.cpp, we already disable the AOT cache when agents using ClassFileLoadHook are statically detected at VM bootstrap:
CDSConfig::set_has_aot_linked_classes(true);
if (JvmtiExport::should_post_class_file_load_hook()) {
log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
return false;
}
The proposed fix is to do the same check for EnableDynamicAgentLoading:
if (EnableDynamicAgentLoading) {
log_error(cds)("CDS archive has aot-linked classes. It cannot be used with -XX:+EnableDynamicAgentLoading");
return false;
}
Justification: we have no idea what capabilities are required by dynamically loaded agents, so it's better to be safe and disable the AOT cache. The application may not get the benefit of the AOT cache, but will behave correctness.
Work-around:
If your program requires -XX:+EnableDynamicAgentLoading, also add -XX:AOTMode=off to the command-line.