VSU_stdlibmalloc/free/exit 的公理化

Require Import VST.floyd.proofauto.
Require Import VST.floyd.VSU.
Require Import VC.stdlib.
Require Import VC.Spec_stdlib.

#[export] Instance CompSpecs : compspecs. make_compspecs prog. Defined.
为外部公理化函数构造的 【经验证软件单元|Verified Software Unit】(VSU) 与其他 VSU 大体相同。 唯一的区别是:这里用 Axiom 而非 Definition 与 Lemma。
由于这个 VSU 不导入任何 【抽象谓词声明|Abstract Predicate Declaration】(APD), 我们不需要 Section。
通常在此时,我们会构造一个 M, 说明 mem_mgr 谓词是如何实现的。 但这里我们直接公理化:
Axiom M: MallocFreeAPD.

Axiom make_mem_mgr: ∀ gv, emp |-- mem_mgr M gv.
通常在此时,我们会为模块导出的每个函数 证明一条 semax_body 引理。但这里我们对外部函数 使用 body_lemma_of_funspec;并把它写成一条公理:
Axiom body_malloc:
 ∀ {Espec: OracleKind} {cs: compspecs} ,
   VST.floyd.library.body_lemma_of_funspec EF_malloc (snd (malloc_spec_sz M)).

Axiom body_free:
 ∀ {Espec: OracleKind} {cs: compspecs} ,
   VST.floyd.library.body_lemma_of_funspec EF_free (snd (free_spec_sz M)).

Axiom body_exit:
 ∀ {Espec: OracleKind},
  VST.floyd.library.body_lemma_of_funspec
    (EF_external "exit" (mksignature (Xint :: nil) Xvoid cc_default))
    (snd (exit_spec)).

内部函数

模块有一个内部(不导出的)函数。 因此照例我们必须为它写一个【函数规约|funspec】。 它就是 placeholder,一个无用的函数, 唯一的目的是迫使 clightgen 保留 malloc、free、exit 的外部声明。 没有人会调用 placeholder()。 我们可以给它一个平凡的函数规约: 前置条件为 False。
Notation placeholder_spec :=
  (* 这里用 Notation 而非 Definition 是为了绕过 VST bug 814 *)
    (_placeholder, vacuous_funspec (Internal f_placeholder)).

定义 VSU 的组成部分

现在照常把这些部分拼合起来:
  Definition MF_ASI: funspecs := MallocFreeASI M.
  Definition MF_imported_specs:funspecs := nil.
  Definition MF_internal_specs: funspecs := placeholder_spec::MF_ASI.
  Definition MF_globals: globals → mpred:= Spec_stdlib.mem_mgr M.
  Definition MFVprog : varspecs. mk_varspecs prog. Defined.
  Definition MFGprog: funspecs := MF_imported_specs ++ MF_internal_specs.

构造组件与 VSU

下面的 solve_SF_external (body_malloc) 需要这条引理。 它要保证兼容:外部函数后置条件给出的返回值, 必须符合该函数 C 语言返回类型规定的调用约定。 这条引理的具体形式由下面 solve_SF_external 的证明目标导出。
Lemma semax_func_cons_malloc_aux {cs: compspecs} (gv: globals)
         (gx : genviron) (ret : option val) n:
 (EX x : val,
  (PROP ( )
   RETURN (x) SEP (mem_mgr M gv;
             if eq_dec x nullval
             then emp
             else malloc_token_sz M Ews n x × memory_block Ews n x))
    (make_ext_rval gx (rettype_of_type (tptr tvoid)) ret)) &&
  !! Builtins0.val_opt_has_rettype ret
     (rettype_of_type (tptr tvoid))
 |-- !! tc_option_val (tptr tvoid) ret.
Proof.
 intros.
 Intros p.
 rewrite <- insert_local.
 rewrite lower_andp.
 apply derives_extract_prop; intro.
 red in H0; unfold_lift in H0; destruct H0.
 destruct ret; try contradiction.
 unfold eval_id in H0; simpl in H0. subst v.
 if_tac. rewrite H0; entailer!.
 renormalize. entailer!.
 simpl. auto.
Qed.

  Definition MF_E : funspecs := MF_ASI.
每个 VSU 都可以有一些私有全局变量, 构成它的「局部状态」。这条 VSU_initializer 引理 给出推理这些变量初始值的方法。 但模块 stdlib.c 没有任何全局变量, 所以这里的 initialize 引理相当平凡。 更有趣的例子见 VSU_stdlib2.v 中的 initialize 引理。
Lemma initialize: VSU_initializer prog MF_globals.
Proof.
InitGPred_tac.
apply make_mem_mgr.
Qed.
所有子组件都已就绪,现在可以把 VSU 打包起来:
Definition MallocFreeVSU: @VSU NullExtension.Espec
         MF_E MF_imported_specs ltac:(QPprog prog) MF_ASI MF_globals.
  Proof.
  mkVSU prog MF_internal_specs.
    - solve_SF_external (@body_malloc NullExtension.Espec CompSpecs).
       apply (semax_func_cons_malloc_aux gv gx ret n).
    - solve_SF_external (@body_free NullExtension.Espec CompSpecs).
    - solve_SF_external (@body_exit NullExtension.Espec).
    - apply initialize.
Qed.

下一章:VSU_main

(* 2026-11-26 16:01 *)