GeezerVison is showing github.com — Parsing concurrently loaded libraries on Linux is error-prone · Issue #1125 · async-profiler/async-profiler
Parsing concurrently loaded libraries on Linux is error-prone #1125DescriptionDescribe the bug
The bug was originally noticed when testing nativemem with GraalVM.
Sometimes, when two shared libraries are opened concurrently in different threads, async-profiler sees unadjusted offset (instead of a relocated address) when parsing relocation entries in parseDynamicSection().
Expected vs. actual behavior
JVM may crash when profiler is started with nativemem option.
Reproduction Steps
while true; do
java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCIProduct -Djava.library.path=build/test/lib -agentpath:build/lib/libasyncProfiler.so=start,nativemem=1,total,file=out.jfr -cp build/test test.nativemem.CallsAllNoLeak once
done
Additional Information/Context
No response
Async-profiler version
master
Environment details
- Linux aarch64, glibc
- OpenJDK Runtime Environment GraalVM CE 21.0.5-dev+14.1 (build 21.0.5+14-LTS)
Activitykrk commented on Feb 14, 2025 After running over 40,000 iterations on both x86_64 and aarch64 with GraalVM 21.0.5+9.1 and 21.0.6+8.1, no crashes were observed. So far, there is no reproducible scenario for this issue. apangin commented on Feb 25, 2025 CollaboratorAuthor More actions A scenario leading to a crash can be the following:
- Thread 1 starts loading library
A.
Profiler::dlopen_hook is called.
dlopen(A) completes successfully.
- Thread 2 starts loading library
B.
B is mapped into memory, but not yet relocated. dlopen(B) still runs.
- Thread 1 calls
Profiler::updateSymbols.
- It finds library
B in /proc/self/maps and starts parsing it.
- Thread 1 calls
MallocTracer::installHooks and updates malloc entry in B's GOT.
- Thread 2 completes linking of
B and updates B's GOT once again, resulting in an invalid target for malloc.
- The next time
B calls malloc, JVM crashes.
Why can (5) and (7) run concurrently? The problem is that dlopen acquires dl_load_lock, while dl_iterate_phdr acquires dl_load_write_lock. These are different locks. dl_iterate_phdr trick helps to protect from concurrent unloading, but not from concurrent loading.
To fix this, we should parse only libraries that are known to be completely loaded. We can again appeal to dl_iterate_phdr to get a list of such libraries. krk commented on Feb 28, 2025 Thanks for the scenario.
I can repro this with Graal CE on aarch64, after bisecting I found these commits:
Furthermore, crash will happen on the good commit with this change at oracle/graal@9ea43a6#diff-fb2c0399284a68730faf93ed01df52083b8653b9c615c46ec8f2889f7ccb44afR130:
-Files.write(exportedSymbolsPath, accessImpl.getImageSymbols(false)) // crash
+Files.write(exportedSymbolsPath, accessImpl.getImageSymbols(true)) // no crash
PosixLibC::malloc function difference
There are more symbols in the bad version in libjvmcicompiler.so.
Inspecting ghidra decompilation of libjvmcicompiler.so, focusing on the malloc import, I noticed a difference in thunks:
Bad version has two malloc symbols defined (malloc and com.oracle.svm.core.posix.headers.PosixLibC::malloc), one is the thunk that resolves to the PLT symbol, other using __svm_cglobaldata_base to call malloc indirectly:
// BAD
/* org.graalvm.word.PointerBase
com.oracle.svm.core.posix.headers.PosixLibC::malloc(org.graalvm.word.UnsignedWord) */
void com.oracle.svm.core.posix.headers.PosixLibC::malloc(void)
{
(*(code *)PTR_malloc_02630c60)();
return;
}
// GOOD
void FUN_003a2770(void)
{
((code *)PTR_malloc_02630f20)();
return;
}
Regardless of what linker/loader/profiler race may be causing this, we can still "parse only libraries that are known to be completely loaded", will be testing this method in parallel to the investigation. apangin commented on Feb 28, 2025 CollaboratorAuthor More actions Difference is getImageSymbols() is likely a red herring. It affects the number of symbols in the result binary. Parsing more symbols in one thread just allows another thread a bit more time to complete loading of a shared library. added 2 commits that reference this issue on Apr 6, 2025#1125: Use dlopen instead of dl_iterate_phdr for parsing libraries #1125: Use dlopen instead of dl_iterate_phdr for parsing libraries (#… apangin commented on Apr 8, 2025 CollaboratorAuthor More actions
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|