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】。
Require Import VST.floyd.proofauto.
Require Import VST.floyd.VSU.
Require Import VST.floyd.VSU.
接下来,我们导入被导入模块以及本模块的 ASI。
由于 stack2.c 使用 malloc/free 库
(由我们的 stdlib 模块导出),
我们既导入 Spec_stdlib 也导入 Spec_stack。
接下来,照常我们导入一个 C 程序文件
(由 clightgen 解析而来)。但我们可以只导入这个模块:
Require Import VC.stack2.
CompSpecs 表示「复合类型规约」
(composite specifications)。
它是 CompCert 对本模块中定义的
结构体与联合类型的描述。
一个程序的不同模块可能有不同的
结构体与联合类型。不同模块即不同的 .c 文件,
它们将来会被链接在一起。
这里我们处理【本】模块的 compspecs。
此处的 prog 实际上是 VC.stack2.prog。
回想一下,我们的 ASI 由它们所使用的 APD 参数化;
为此我们在 Spec_stack 中使用了一个 Section。
StackASI 由 (M: MallocFreeAPD) 与
(STACK: StackAPD) 参数化。
Stack VSU 将使用同样这两个 APD。与之前一样,M 将是一个形参。
但 STACK 不会是形参:我们将在本次验证中
构造并实例化那个 APD。因此 Section Stack_VSU
只为按 M 参数化而设。
首先,实例化 APD
练习: 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.
☐
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.
☐
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).
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).
模块的【私有|Privates】部分由局部定义函数的
函数规约组成;这些函数【不】导出给客户使用。
【内部|Internals】部分就是私有部分加上其余内部函数规约。
【如果】模块不重新导出任何导入的函数,
它就等于私有部分加上【导出|Exports】部分,
如下所示:
Gprog 是证明每条 semax_body 的基础;
它总是导入部分加上内部部分
除 Main 外的每个模块,Vprog 按如下方式计算:
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.
☐
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
#[export] Existing Instance NullExtension.Espec.
上面所有 semax_body 证明都不依赖
所用的 Espec,因此可移植到任何 Espec。
如果其中某些证明做了 I/O,情况就不是这样了。
为了构造 StackVSU,我们首先定义它的【类型】,如下:
也就是说,
指定了 StackVSU 引理之后,我们现在来证明它。
mkVSU 行有两个实参:C 程序(stack2.prog),
以及【内部|Internals】(未在 VSU 的【类型】中提及)。
随后,.c 文件中定义的每个函数体对应一个子目标。
在每个子目标中,solve_SF_internal 用你已证明的
相应 semax_body 引理解决目标。
- nil,本模块的【外部函数|Externs】;
- Stack_imports,本模块的【导入|Imports】;
- prog,本模块的【程序】,但由 QPprog 策略 转换为 QP.program;
- StackASI,本模块的导出;
- emp 是全局变量说明符,因为该 stack 模块的实现 没有自己的全局变量。
Proof.
mkVSU prog Stack_internals.
+ solve_SF_internal body_surely_malloc.
+ solve_SF_internal body_newstack.
+
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。
下一章:VSU_triang
(* 2026-11-26 16:01 *)
