GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #1125

New issue
Copy link
New issue
Copy link
Closed
Closed
Parsing concurrently loaded libraries on Linux is error-prone#1125
Copy link
Labels
bug

Description

@apangin
apangin
opened on Feb 5, 2025
Issue body actions

Describe 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)

Activity

  1. apangin
    added
    bug
    on Feb 5, 2025
  2. krk commented on Feb 14, 2025

    @krk
    krk
    on Feb 14, 2025
    Contributor
    More actions

    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.

  3. apangin commented on Feb 25, 2025

    @apangin
    apangin
    on Feb 25, 2025
    CollaboratorAuthor
    More actions

    A scenario leading to a crash can be the following:

    1. Thread 1 starts loading library A.
    2. Profiler::dlopen_hook is called.
    3. dlopen(A) completes successfully.
    4. Thread 2 starts loading library B.
    5. B is mapped into memory, but not yet relocated. dlopen(B) still runs.
    6. Thread 1 calls Profiler::updateSymbols.
    7. It finds library B in /proc/self/maps and starts parsing it.
    8. Thread 1 calls MallocTracer::installHooks and updates malloc entry in B's GOT.
    9. Thread 2 completes linking of B and updates B's GOT once again, resulting in an invalid target for malloc.
    10. 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.

  4. krk commented on Feb 28, 2025

    @krk
    krk
    on Feb 28, 2025
    Contributor
    More actions

    Thanks for the scenario.

    I can repro this with Graal CE on aarch64, after bisecting I found these commits:

    • No crash: oracle/graal@9ea43a6
    • Crash: oracle/graal@b7bcc58

    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.

  5. apangin commented on Feb 28, 2025

    @apangin
    apangin
    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.

    Reacted by Kerem Kat
  6. apangin
    added 2 commits that reference this issue on Apr 6, 2025

    #1125: Use dlopen instead of dl_iterate_phdr for parsing libraries

    3bafe30

    #1125: Use dlopen instead of dl_iterate_phdr for parsing libraries (#…

    ebd8894
  7. apangin commented on Apr 8, 2025

    @apangin
    apangin
    on Apr 8, 2025
    CollaboratorAuthor
    More actions

    Fixed in #1220

  8. apangin
    closed this as completedon Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.