Init.ShareCommon unchecked StateFactoryBuilder breaks runtime/spec consistency

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

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
領域
compilers

調査の方向性

Start with src/Init/ShareCommon.lean lines 47–86 and the minimal reproduction in the issue, then trace how StateFactoryBuilder callbacks are used by StateFactory.mk and State.shareCommon at runtime. Determine how to preserve the logical result when callbacks are non-lawful and verify that the reproduction no longer makes native_decide derive a contradiction.

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

説明

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    Paranoid catches it
Description

ShareCommon.StateFactoryBuilder is a public structure with no lawfulness constraints on mapFind? / setFind?, and ShareCommon.StateFactory.mk is a public opaque function implemented via unsafeCast (@[implemented_by Internal.StateFactory.mkImpl]). Meanwhile, ShareCommon.State.shareCommon has the pure logical definition (a, s) while executing @[extern "lean_state_sharecommon"] at runtime, returning whatever setFind? / mapFind? returns.

Providing a non-lawful setFind? (e.g., returning the previously inserted element regardless of the lookup key) makes (s.shareCommon a).1 disagree with its logical definition a—and across distinct types α ≠ β, returns an α value at type β in safe Lean (no unsafe, @[extern], or @[implemented_by]). This breaks runtime type safety and lets native_decide prove 0 = 1.

Scope: Does not affect pure-kernel proofs (the proof of 0 = 1 depends on .native_decide.ax_ and is rejected by standard axiom checks). Still present on master after #16715.

Where issue is

src/Init/ShareCommon.lean lines 47–86 (e6e9431b15):
public structure StateFactoryBuilder (lines 47–55)
@[implemented_by Internal.StateFactory.mkImpl] public opaque StateFactory.mk (lines 71–72)
@[extern "lean_state_sharecommon"] public def State.shareCommon (lines 85–86)

Minimum Reproducible Issue

import Init.ShareCommon

def evilFactory : ShareCommon.StateFactory :=
  ShareCommon.StateFactory.mk {
    Map := fun _ β _ _ => Option β
    mkMap := fun _ => none
    mapFind? := fun m _ => m
    mapInsert := fun _ _ v => some v
    Set := fun α _ _ => Option α
    mkSet := fun _ => none
    setFind? := fun s _ => s
    setInsert := fun _ x => some x
  }

def evilVal : List Nat :=
  let s0 : ShareCommon.State.{0} evilFactory := ShareCommon.State.mk evilFactory
  let (_, s1) := s0.shareCommon [0]
  (s1.shareCommon [1]).1

theorem evil_spec : evilVal = [1] := rfl
theorem evil_native : evilVal = [0] := by native_decide

theorem zero_eq_one : 0 = 1 := by
  have h : ([0] : List Nat) = [1] := evil_native.symm.trans evil_spec
  injection h

#print axioms zero_eq_one

Output

'zero_eq_one' depends on axioms: [Classical.choice, evil_native._native.native_decide.ax_1_1]

Versions

v4.36.0 , v4.37.0-rc1

主要言語
Lean
スター
10.3k
フォーク
1.1k
平均マージ
16時間 45分
マージ済み PR(30日)
361

環境構築

はじめの一歩

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

leanprover/lean4 のほかの issue

leanprover/lean4 の issue をすべて見る

似ている issue

Compilers の issue をもっと見る

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

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