Visibility attributes on extern global variables are dropped from generated bindings
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
bindgen のコード生成におけるトップレベル変数のパスから始め、cursor.visibility() をチェックする関数解析のパスと比較します。報告された bindgen 呼び出しの hidden と protected のケースを再現し、C 宣言の可視性がデフォルトでない場合に、生成された bindings が通常のデフォルト可視の extern static を出力しなくなったことを確認します。
索引モデルが issue の本文から書いたものです。
説明
Input C/C++ Header
#if defined(PHASE8_PROTECTED)
#define TEST_VISIBILITY __attribute__((visibility("protected")))
#else
#define TEST_VISIBILITY __attribute__((visibility("hidden")))
#endif
extern int phase8_visibility_global TEST_VISIBILITY;
Bindgen Invocation
$ bindgen input.h \
--output bindings.rs \
--no-layout-tests \
--no-doc-comments \
--no-rustfmt-bindings \
--rust-target 1.75 \
-- -x c -std=gnu11
I ran the protected case again with -DPHASE8_PROTECTED=1.
Actual Results
Bindgen emits the same Rust declaration for both inputs:
extern "C" {
pub static mut phase8_visibility_global: ::std::os::raw::c_int;
}
The C and Rust objects have different ELF visibility on the undefined symbol:
input C object Rust object
hidden GLOBAL HIDDEN UND GLOBAL DEFAULT UND
protected GLOBAL PROTECTED UND GLOBAL DEFAULT UND
The LLVM declarations show the same difference:
@phase8_visibility_global = external hidden global i32
@phase8_visibility_global = external protected global i32
; Rust, for either input
@phase8_visibility_global = external global i32
This changes link behavior. A C caller carrying the hidden or protected
undefined reference cannot satisfy it from a default-visible definition in a
shared library. A Rust caller using the generated binding can. In my test, the
C link failed while the Rust program linked and printed value=31337.
As a control, I compiled a local definition that includes the same header.
Both callers then linked and printed the same value, and the provider object
retained the requested visibility.
Expected Results
Bindgen should not turn a non-default ELF reference into a default reference.
Rust has no general source attribute for ELF visibility on an extern static,
so omitting the variable with a diagnostic would be a reasonable conservative
result. A target-specific way to preserve the visibility would also work.
Emitting an ordinary extern static is not equivalent to either C declaration.
Environment
bindgen current main: 9d26c6eddeff9192ddedb563192abe3128fc5aae
bindgen release: 0.72.1
clang: 15.0.7
rustc: 1.75.0
target: x86_64-unknown-linux-gnu
object inspection: GNU readelf
I repeated hidden and protected at O0 and O2 with both bindgen versions. The C
objects retained HIDDEN or PROTECTED in every run; the objects produced from
the generated Rust binding used DEFAULT in every run.
Additional notes
Functions and variables currently take different paths here. Function parsing
checks cursor.visibility() and filters non-default declarations. The
top-level variable path does not keep the Clang visibility, so code generation
has no way to distinguish these inputs.
- 主要言語
- Rust
- スター
- 5.3k
- フォーク
- 846
- 平均マージ
- 2日 6時間
- マージ済み PR(30日)
- 11
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rust-lang/rust-bindgen のほかの issue
-
--enable-cxx-namespaces panics on namespace [[deprecated]] { ("[" is not a valid Ident)対応中かも @lwz23 が 49 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
rust-lang/rust-bindgen#3485 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
rust-lang/rust-bindgen#3483 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
rust-lang/rust-bindgen#3370 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
rust-lang/rust-bindgen#3363 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
rust-lang/rust-bindgen#3540 ·
メンテナーはふだん 1 日以内に返信
rust-lang/rust-bindgen の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 94/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 2 日以内に返信
-
enhancement good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
area:ui
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
openfootmanager/openfootmanager#1365 ·
メンテナーはふだん 1 日以内に返信
-
documentation
難易度 1/5 1〜3時間 初心者へのやさしさ 90/100
JackMechem/marbble#305 ·
メンテナーはふだん 1 日以内に返信