regparm(2) functions on i386 are emitted as ordinary extern C

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

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

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

評価

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

調査の方向性

input.h の regparm 宣言に対する bindgen CLI の経路から始め、Clang の i386 LLVM シグネチャと生成された Rust 宣言を比較します。このサポートされていない ABI をどのように表現すべきかを判断します。完了条件は、binding が互換性のない通常の C 宣言を暗黙に出力しなくなり、報告された reproducer のカバレッジがあることです。

索引モデルが issue の本文から書いたものです。

説明

A-rustc
Input C/C++ header
int regparm2_third(int a, int b, int c) __attribute__((regparm(2)));
Bindgen invocation
$ bindgen input.h \
    --rust-target 1.75 \
    -- -target i686-unknown-linux-gnu -std=gnu11
Actual output
extern "C" {
    pub fn regparm2_third(
        a: ::std::os::raw::c_int,
        b: ::std::os::raw::c_int,
        c: ::std::os::raw::c_int,
    ) -> ::std::os::raw::c_int;
}

I defined the function to return its third argument and called it with
(11, 22, 33). The observed results were:

C caller:          return=33, callee saw c=33
Generated binding: return=11, callee saw c=11

This reproduces at both -O0 and -O2.

Clang lowers the optimized C definition as:

define dso_local i32 @regparm2_third(
    i32 inreg noundef %0,
    i32 inreg noundef %1,
    i32 noundef returned %2)

The Rust caller instead declares it as:

declare dso_local noundef i32 @regparm2_third(
    i32 noundef,
    i32 noundef,
    i32 noundef)
What is wrong

On 32-bit x86, regparm(2) passes the first two integer arguments in EAX and
EDX, with the third argument on the stack. The generated ordinary C declaration
passes all three arguments on the stack. The Rust caller and C callee therefore
use different locations for the same arguments, without any diagnostic.

Expected output

Rust does not provide a matching regparm ABI. Bindgen should diagnose this
declaration as unsupported and avoid emitting a misleading ordinary C binding,
or generate a C wrapper that preserves the source ABI.

Impact

Calls through the generated declaration read the wrong argument values. In
this reproducer, the third parameter is 11 instead of 33 and the function
returns the wrong value.

Environment
bindgen: 0.72.0, current main 25b23474496e78f6a1fbf1c02cb66f11e4d176d0
bindgen release: 0.72.1
clang/libclang: 15.0.7
rustc: 1.75.0
target: i686-unknown-linux-gnu
OS: Ubuntu 22.04.5 LTS, x86_64 host
主要言語
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 を短くまとめたダイジェスト。