VSU_stackStack 模块的 VSU 验证

stack2.c

模块 stack2.c 相对 stack.c 作了少许调整。 它使用内部函数 surely_malloc,只为说明 如何处理这样的局部函数: 它未在【抽象规约接口|Abstract Specification Interface】 (ASI)模块接口中导出。
    #include <stddef.h>
    #include "stdlib.h"
    #include "stack2.h"

    struct cons { int value; struct cons *next; };

    struct stack { struct cons *top; };

    void *surely_malloc (size_t n) {
      void *p = malloc(n);
      if (!p) exit(1);
      return p;
    }

    struct stack *newstack(void) {
      struct stack *p;
      p = (struct stack * ) surely_malloc(sizeof (struct stack));
      p->top = NULL;
      return p;
    }

    void push (struct stack *p, int i) {
      struct cons *q;
      q = (struct cons * ) surely_malloc(sizeof (struct cons));
      q->value = i;
      q->next = p->top;
      p->top = q;
    }

    int pop (struct stack *p) {
      struct cons *q;
      int i;
      q = p->top;
      p->top = q->next;
      i = q->value;
      free(q);
      return i;
    }

构造 VSU

现在我们可以验证各个模块的实现。 对每个模块,我们产出一个 VSU, 即一个【经验证软件单元|Verified Software Unit】。
这些验证各自只导入 ASI(抽象规约接口), 而非其他 VSU。一个模块的验证 只依赖接口,不依赖其他模块的实现(或验证)。
我们照常先导入 floyd.proofauto, 同时也导入 floyd.VSU 支持。
Require Import VST.floyd.proofauto.
Require Import VST.floyd.VSU.
接下来,我们导入被导入模块以及本模块的 ASI。 由于 stack2.c 使用 malloc/free 库 (由我们的 stdlib 模块导出), 我们既导入 Spec_stdlib 也导入 Spec_stack。
Require Import VC.Spec_stdlib.
Require Import VC.Spec_stack.
接下来,照常我们导入一个 C 程序文件 (由 clightgen 解析而来)。但我们可以只导入这个模块:
Require Import VC.stack2.
CompSpecs 表示「复合类型规约」 (composite specifications)。 它是 CompCert 对本模块中定义的 结构体与联合类型的描述。 一个程序的不同模块可能有不同的 结构体与联合类型。不同模块即不同的 .c 文件, 它们将来会被链接在一起。 这里我们处理【本】模块的 compspecs。 此处的 prog 实际上是 VC.stack2.prog。
#[export] Instance CompSpecs : compspecs. make_compspecs prog. Defined.
回想一下,我们的 ASI 由它们所使用的 APD 参数化; 为此我们在 Spec_stack 中使用了一个 Section。 StackASI 由 (M: MallocFreeAPD) 与 (STACK: StackAPD) 参数化。
Stack VSU 将使用同样这两个 APD。与之前一样,M 将是一个形参。 但 STACK 不会是形参:我们将在本次验证中 构造并实例化那个 APD。因此 Section Stack_VSU 只为按 M 参数化而设。
Section Stack_VSU.
Variable M: MallocFreeAPD.

首先,实例化 APD

现在来构造 StackAPD 的一个实例。为此, 我们需要构造与 Verif_stack 中相同的 stack mpred。 在本练习中,你将粘贴自己在 Verif_stack 中相应练习的解答,最终得到 StackAPD 的定义。

练习: 2 stars,标准 (STACK)

Fixpoint listrep (il: list Z) (p: val) : mpred :=
 match il with
 | i::il' ⇒ EX y: val,
        malloc_token M Ews (Tstruct _cons noattr) p ×
        data_at Ews (Tstruct _cons noattr) (Vint (Int.repr i),y) p ×
        listrep il' y
 | nil ⇒ !! (p = nullval) && emp
 end.

Lemma listrep_local_prop: ∀ il p, listrep il p |--
        !! (is_pointer_or_null p ∧ (p=nullval ↔ il=nil)).
(* 在这里填写 *) Admitted.
Local Hint Resolve listrep_local_prop : saturate_local.

Lemma listrep_valid_pointer:
  ∀ il p,
   listrep il p |-- valid_pointer p.
(* 在这里填写 *) Admitted.
Local Hint Resolve listrep_valid_pointer : valid_pointer.

Definition stack (il: list Z) (p: val) :=
 EX q: val,
  malloc_token M Ews (Tstruct _stack noattr) p ×
  data_at Ews (Tstruct _stack noattr) q p ×
  listrep il q.

Arguments stack il p : simpl never.

Lemma stack_local_prop: ∀ il p, stack il p |-- !! (isptr p).
(* 在这里填写 *) Admitted.
Local Hint Resolve stack_local_prop : saturate_local.

Lemma stack_valid_pointer:
  ∀ il p,
   stack il p |-- valid_pointer p.
(* 在这里填写 *) Admitted.
Local Hint Resolve stack_valid_pointer : valid_pointer.
☐
现在我们可以把 stack 谓词与关于它的两条引理 粘合起来,构造 StackAPD 的实例 STACK。

内部函数

每个内部函数都需要一个函数规约。 导出函数的规约放在 ASI 中(如 Spec_stack), 内部函数的规约放在 VSU 中。
Definition surely_malloc_spec :=
  DECLARE _surely_malloc
   WITH t:type, gv: globals
   PRE [ size_t ]
       PROP (0 ≤ sizeof t ≤ Ptrofs.max_unsigned;
                complete_legal_cosu_type t = true;
                natural_aligned natural_alignment t = true)
       PARAMS (Vptrofs (Ptrofs.repr (sizeof t))) GLOBALS (gv)
       SEP (mem_mgr M gv)
    POST [ tptr tvoid ] EX p:_,
       PROP ()
       RETURN (p)
       SEP (mem_mgr M gv; malloc_token M Ews t p × data_at_ Ews t p).

构造 Vprog 与 Gprog

VSU 的【导入|Imports】就是它所导入各模块的 全部 ASI 的连接:
Definition Stack_imports : funspecs := MallocFreeASI M.
模块的【私有|Privates】部分由局部定义函数的 函数规约组成;这些函数【不】导出给客户使用。
Definition Stack_private : funspecs := [surely_malloc_spec].
【内部|Internals】部分就是私有部分加上其余内部函数规约。 【如果】模块不重新导出任何导入的函数, 它就等于私有部分加上【导出|Exports】部分, 如下所示:
Definition Stack_internals : funspecs := Stack_private ++ (StackASI M STACK).
Gprog 是证明每条 semax_body 的基础; 它总是导入部分加上内部部分
Definition Stack_Gprog : funspecs := Stack_imports ++ Stack_internals.
除 Main 外的每个模块,Vprog 按如下方式计算:
Definition Stack_Vprog : varspecs. mk_varspecs prog. Defined.

函数体的证明

练习: 2 stars,标准 (stack_bodies)

从 Verif_stack 复制并改编这些证明。
Lemma body_pop: semax_body Stack_Vprog Stack_Gprog f_pop (pop_spec M STACK).
Proof.
start_function.
simpl stackrep.
(* 在这里填写 *) Admitted.

Lemma body_push: semax_body Stack_Vprog Stack_Gprog f_push (push_spec M STACK).
Proof.
start_function.
simpl stackrep.
forward_call (Tstruct _cons noattr, gv).
(* 在这里填写 *) Admitted.

Lemma body_newstack: semax_body Stack_Vprog Stack_Gprog f_newstack (newstack_spec M STACK).
Proof.
start_function.
(* 在这里填写 *) Admitted.

Lemma body_surely_malloc: semax_body Stack_Vprog Stack_Gprog f_surely_malloc surely_malloc_spec.
Proof.
start_function.
forward_call (malloc_spec_sub M t) gv.
(* 在这里填写 *) Admitted.
☐

构造 VSU

做输入输出的程序需要一个「外部规约」 (或 Espec)来刻画这种交互。本程序的所有 VSU 必须使用同一个 Espec。由于本程序不做任何 I/O, 它可以使用标准的平凡 Espec,如下:
#[export] Existing Instance NullExtension.Espec.
上面所有 semax_body 证明都不依赖 所用的 Espec,因此可移植到任何 Espec。 如果其中某些证明做了 I/O,情况就不是这样了。
为了构造 StackVSU,我们首先定义它的【类型】,如下:
Lemma StackVSU: VSU nil Stack_imports ltac:(QPprog stack2.prog)
                                           (StackASI M STACK) emp.
也就是说,
  • nil,本模块的【外部函数|Externs】;
  • Stack_imports,本模块的【导入|Imports】;
  • prog,本模块的【程序】,但由 QPprog 策略 转换为 QP.program;
  • StackASI,本模块的导出;
  • emp 是全局变量说明符,因为该 stack 模块的实现 没有自己的全局变量。
指定了 StackVSU 引理之后,我们现在来证明它。 mkVSU 行有两个实参:C 程序(stack2.prog), 以及【内部|Internals】(未在 VSU 的【类型】中提及)。 随后,.c 文件中定义的每个函数体对应一个子目标。 在每个子目标中,solve_SF_internal 用你已证明的 相应 semax_body 引理解决目标。
Proof.
  mkVSU prog Stack_internals.
  + solve_SF_internal body_surely_malloc.
  + solve_SF_internal body_newstack.
  +
如何知道每个子目标该提供哪条 semax_body 证明? 这很简单:子目标形如 SF Gprog name fundef funspec。 本例中 name 是 _surely_malloc, fundef 是 Internal f_surely_malloc, 所以这里要提供的是 body_surely_malloc。
      solve_SF_internal body_push.
  + solve_SF_internal body_pop.
Qed.

End Stack_VSU.

下一章:VSU_triang

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