Semantics of function pointers with equal addresses?

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
rust
領域
compilers

調査の方向性

Start with the two Rust functions in the issue and reproduce their deduplication using the linked Godbolt example. Trace the implications of converting their equal addresses through usize and consider the analogous deduplicated-vtable case. Done means reaching and documenting a resolved language-semantics position for these cases.

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

説明

Consider the following code:

#[unsafe(no_mangle)]
fn foo(x: *mut i32, y: *mut i32) {
    unsafe {
        let a = &mut *x;
        let b = &mut *y;
        *a = *b;
    }
}

#[unsafe(no_mangle)]
fn bar(x: *mut i32, y: *mut i32) {
    unsafe {
        x.write(y.read());
    }
}

On rust version 1.91.0, the above code compiles to this assembly (Godbolt link):

bar:
        mov     eax, dword ptr [rsi]
        mov     dword ptr [rdi], eax
        ret

foo = bar

That is, the two functions are deduplicated, and therefore have the same address. This happened even though these two functions have different behavior in the abstract machine: the foo function has UB if x and y alias, while the bar function doesn't.

Suppose that some code casts these two functions into usize addresses (which are equal to each other), does some operation with those two usizes, and then transmutes an equal usize back into a function pointer. When calling that created function pointer, what is the behavior of that call? Does the call behave like calling foo or calling bar?

In theory, we probably also want (in the future) to potentially allow LLVM to deduplicate functions with incompatible ABIs, as long as their generated assembly are identical. This complicates the above question even further.

A similar question applies to deduplicated vtables.

主要言語
言語のデータがありません
スター
760
フォーク
68
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

rust-lang/unsafe-code-guidelines のほかの issue

rust-lang/unsafe-code-guidelines の issue をすべて見る

似ている issue

Compilers の issue をもっと見る

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

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