rust-lang / rust-lang/rust

crash when dlopen/dlclose rust-made *.so multi-times on android/ohos aarch64

Open
#135,815 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-linkage C-discussion O-android
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

crash when dlopen/dlclose rust-made *.so multi-times on android/ohos aarch64

Senario:

  1. Compile a Rust source file into a shared object (.so) file.
  2. A C++ file uses dlopen to load the .so file and retrieve the target symbol.
  3. Call the Rust function within the loaded .so.
  4. Use dlclose to unload the .so file.

Related issues #134820

reproduction code

Here's the translation of the provided text:

Known Information:

  1. There is no issue with regular arm64, but disassembly reveals differences involving the use of TPIDR_EL0.
  2. The main difference between ohos/an and arm64 Linux options is has_thread_local=true and tls-model=emulated, but these two compilation options do not seem to take effect when compiling the shared object (suspected that the standard library needs to be recompiled with these options). The implementation of printl is part of the Rust standard library.
  3. Upon reviewing the code, it was found that the tls-model=emulated option affects the rustc compilation behavior (either using TPIDR_EL0 or emulated TLS, by affecting the LLVM backend).

This seems to be a bug in the rustc implementation. In the combination of Android and OHOS target platform options, during the println process, a thread-local variable is implicitly created, and the thread-local resources initialized through lazy_init are not properly released when dlclose is called.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked c_cpp_dlopen_rust_so reproduction on Android or OHOS aarch64, repeating dlopen, the Rust call, and dlclose. Then inspect rustc and standard-library handling of tls-model=emulated, TPIDR_EL0, and lazy_init during shared-object unloading. Done means repeated load and unload cycles no longer crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, cpp, rust
Domain
compilers, mobile-dev, operating-systems
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.