rust-lang / rust-lang/rust

csky targets' atomic RMW is not lock-free and may cause data races between load/store and it

Open
#117,306 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-atomic C-bug I-unsound O-csky P-low
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

In csky, LLVM always generates libcalls (https://github.com/llvm/llvm-project/commit/ec2de7490813a7593dad59f210c7ec41f1a29002), and atomic implementations are provided by libatomic.

https://github.com/rust-lang/rust/blob/c7224e3c954ce54853364c950608c1f0ed47ea85/compiler/rustc_target/src/spec/csky_unknown_linux_gnuabiv2.rs#L15

However, as mentioned in https://github.com/rust-lang/rust/pull/115577#issuecomment-1732597305, the atomic RMW implementation provided in libatomic.a is not lock-free.

And it violates what the standard library is intended to guarantee.

https://doc.rust-lang.org/nightly/std/sync/atomic/index.html#portability

All atomic types in this module are guaranteed to be lock-free if they’re available. This means they don’t internally acquire a global mutex.

Also, mixing lock-free load/store and non-lock-free RMW can cause data races.

To fix this, we would need to do one of the following:

  • Fix libatomic to make the RMW implementation lock-free.
  • Fix LLVM to generate atomic instructions instead of libcalls.

It is not impossible to fix this on our end, but it is not very realistic as it would require writing a lot of inline assembly.

cc @Dirreke (mentioned because you are target maintainer)


@rustbot label +A-atomic +I-unsound

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 with compiler/rustc_target/src/spec/csky_unknown_linux_gnuabiv2.rs and the standard library atomic portability documentation, then review the linked LLVM commit and Rust PR #115577. Investigate whether libatomic or LLVM owns the non-lock-free RMW behavior; done means csky atomic RMW operations satisfy the lock-free guarantee without races with load/store operations.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.