Visibility attributes on extern global variables are dropped from generated bindings

オープン
#3,405 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
c, rust
領域
devtools

調査の方向性

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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/rust-bindgen のほかの issue

rust-lang/rust-bindgen の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。