Is life-before-main / `#[ctor]` safe?
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- rust
調査の方向性
Start by reading the examples and safety assumptions in this issue, then compare the behavior of the ctor and inventory crates with Rust's configure_builtins.rs. Review the linked rust-ctor issue and unsafe-code-guidelines issue 397 for existing discussion. Done means reaching a documented consensus on whether pre-main constructors require unsafe and what safety requirements apply.
索引モデルが issue の本文から書いたものです。
説明
Rust does not provide facilities for doing work before fn main(), but we can achieve it anyhow on a lot of platforms using linker tricks and global constructors, as done in ctor, inventory and the standard library itself.
Is this safe to do, or should it require unsafe? If so, what are the safety requirements that one needs to uphold inside a global constructor?
In isolation, this feature seems to be safe (?), but one reason to define it as unsafe could be to allow libraries to rely on never being used in a constructor themselves? Consider the following library:
static mut FOO: i32 = 0;
/// Do some "heavy" work to initialize `FOO`.
#[ctor]
fn setup_foo() {
// SAFETY: `get_foo` is not running at the same time as this constructor, so an unsynchronized write is fine.
unsafe { FOO = 42 };
}
/// Get the value from `FOO`.
pub fn get_foo() -> NonZero<i32> {
// SAFETY: The constructor has run before `get_foo`
let foo = unsafe { FOO };
// SAFETY: The variable was initialized to a non-zero value.
unsafe { NonZero::new_unchecked(foo) }
}
The safety comments here rely on:
- The constructor not executing concurrently with anything else.
- The constructor executing before
get_foo.
But those assumptions are actually false, since the user may write:
#[ctor]
fn user_constructor() {
std::thread::spawn(|| {
get_foo();
});
}
And, depending on linker ordering, user_constructor may be run first (unless we can somehow guarantee that setup_foo runs before user_constructor?), in which case the unsynchronized read+write is not actually okay, and the variable may not actually be initialized to a non-zero value.
The correct way to write it then would be:
static FOO: AtomicI32 = AtomicI32::new(0);
#[ctor]
fn setup_foo() {
// Must use atomics.
FOO.store(42, Ordering::Relaxed);
}
pub fn get_foo() -> NonZero<i32> {
let foo = FOO.load(Ordering::Relaxed);
// Must check that the variable is actually initialized.
NonZero::new(foo).unwrap()
}
Though this does decrease the utility of #[ctor] by a fair margin as the performance improvements of using a global constructor decreases, and you'd probably just want to use OnceLock now instead.
One noteworthy tidbit is that when using dynamic libraries (e.g. linking libc or libX11), this is a non-issue, as constructors are executed in libraries first, so any global constructors that a dynamic library has are guaranteed to be executed before any code that depends on that dynamic library.
For context, I'm opening this because the ctor crate has made the #[ctor] attribute require unsafe, see https://github.com/mmastrac/rust-ctor/issues/159, but deciding "running code before main is unsafe for xyz reason" is a decision that affects more than just ctor (e.g. it affects inventory as well), so I thought it might be something that t-opsem / the UCG has opinions on?
See also https://github.com/rust-lang/unsafe-code-guidelines/issues/397.
- 主要言語
- 言語のデータがありません
- スター
- 760
- フォーク
- 68
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rust-lang/unsafe-code-guidelines のほかの issue
-
I-opsem-nominated
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
rust-lang/unsafe-code-guidelines#634 · コメント 5 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 15/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 12/100
rust-lang/unsafe-code-guidelines#632 · コメント 8 件 · リアクション 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 10/100
rust-lang/unsafe-code-guidelines#631 · コメント 7 件 ·
-
I-opsem-nominated
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
rust-lang/unsafe-code-guidelines#630 · リアクション 1 件 ·
rust-lang/unsafe-code-guidelines の issue をすべて見る
似ている issue
-
bug core-lang
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentenceオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
mw00/project-maya#1368 ·
メンテナーはふだん 2 日以内に返信
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
foundry-rs/foundry#18828 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信