Spec_stdlib外部 malloc、free、exit 函数的规约

抽象谓词声明

Require VC.Preface. (* Check for the right version of VST *)
Require Import VST.floyd.proofauto.
Require Import VC.stdlib.
在本卷前面几章(Verif_stack、 Verif_triang 等)中,我们演示的都是单文件 C 程序。 所用 malloc_token 的类型为
Require VST.floyd.library. (* 仅为下一行所需 *)
About VST.floyd.library.malloc_token.
(*  library.malloc_token : compspecs -> share -> type -> val -> mpred
    Arguments library.malloc_token {cs}     *)

malloc_token 接受一个隐式的 compspecs 参数, type 参数的解释依赖于 compspecs。 基本上,malloc_token 需要知道 malloc 出来的记录有多少字节, 因此它需要 compspecs 来解释该类型。
但现在我们在做【模块化】 C 编程: 每个 .c 文件都可以有自己的结构体与联合类型, 也就是自己的 compspecs。 带 compspecs 参数的 malloc_token 不能假定所有模块都有相同的 compspecs。 模块化编程要从一种更原始的形式开始, 即 malloc_token_sz。它只依赖对象的【大小】, 类型为 Z,不需要 compspecs。 本文件后面会展示如何在每个模块【之内】 恢复基于类型的 malloc_token。
内存管理器 APD 中有两个抽象谓词: mem_mgr 与 malloc_token。 mem_mgr 表示内存管理器空闲链表的状态。 调用 free(p) 时,可以把地址 p 处的内存块 想象成加回了空闲链表(或其他某种数据结构); 调用 malloc(n) 时,则从空闲链表取出一个块。 空闲链表的内部表示是本模块(stdlib)私有的, 因此它是一个抽象谓词。
Record MallocFreeAPD := {
   mem_mgr: globals → mpred;
   malloc_token_sz: share → Z → val → mpred;
   malloc_token_sz_valid_pointer: ∀ sh sz p,
      sz ≤ 0 → malloc_token_sz sh sz p |-- valid_pointer p;
   malloc_token_sz_local_facts: ∀ sh sz p,
      malloc_token_sz sh sz p |-- !! malloc_compatible sz p
}.

#[export] Hint Resolve malloc_token_sz_valid_pointer : valid_pointer.
#[export] Hint Resolve malloc_token_sz_local_facts : saturate_local.

抽象规约接口

与通常一样,用一个 Section 让接口带上 APD 参数:
Section MallocFreeASI.
Variable M:MallocFreeAPD.
malloc 的规约与上面 malloc_token 的 compspecs 问题相同: ASI 必须独立于任何特定模块的 结构体/联合声明。 因此我们定义 malloc_spec_sz, 它是基于大小而非基于类型的规约。
Definition malloc_spec_sz :=
 DECLARE _malloc
   WITH n:Z, gv: globals
   PRE [ size_t ]
       PROP (0 ≤ n ≤ Ptrofs.max_unsigned)
       PARAMS (Vptrofs (Ptrofs.repr n)) GLOBALS (gv)
       SEP (mem_mgr M gv)
    POST [ tptr tvoid ] EX p:_,
       PROP ()
       LOCAL (temp ret_temp p)
       SEP (mem_mgr M gv;
             if eq_dec p nullval then emp
            else (malloc_token_sz M Ews n p × memory_block Ews n p)).

Definition free_spec_sz :=
 DECLARE _free
   WITH n:Z, p:val, gv: globals
   PRE [ tptr tvoid ]
       PROP ()
       PARAMS (p) GLOBALS (gv)
       SEP (mem_mgr M gv;
              if eq_dec p nullval then emp
              else (malloc_token_sz M Ews n p × memory_block Ews n p))
    POST [ Tvoid ]
       PROP ()
       LOCAL ()
       SEP (mem_mgr M gv).
当然,exit 系统调用并不真正属于 malloc/free 库,但这里是放置其规约的方便之处。
Definition exit_spec :=
 DECLARE _exit
 WITH i: Z
 PRE [tint]
   PROP () PARAMS (Vint (Int.repr i)) GLOBALS () SEP()
 POST [ tvoid ]
   PROP(False) LOCAL() SEP().
现在可以构造 ASI 了。与通常一样, 它只是(由 APD 参数化的)函数规约的列表。

基于类型的 malloc 与 free 规约

当然,对用户来说,更方便的是这样一种 malloc 函数规约: 其后置条件中带有正确类型的 data_at_。 因此这里我们定义另一种 malloc_token 与 malloc_spec, 它们使用 compspecs 与类型。
Definition malloc_token {cs: compspecs} (M:MallocFreeAPD) sh t v :=
   !! field_compatible t [] v &&
   malloc_token_sz M sh (sizeof t) v.

Lemma malloc_token_valid_pointer: ∀ {cs: compspecs} M sh t p,
      sizeof t ≤ 0 → malloc_token M sh t p |-- valid_pointer p.
Proof. intros. unfold malloc_token.
 apply andp_left2. apply malloc_token_sz_valid_pointer; auto.
Qed.

Ltac malloc_token_data_at_valid_pointer :=
  (* 即使 t 的大小未知,仍可从
      (malloc_token sh t p * ... * data_at_ sh t p) 证明有效指针 *)

  match goal with ⊢ ?A |-- valid_pointer ?p ⇒
   match A with
   | context [malloc_token _ _ ?t p] ⇒
         try (assert (sizeof t ≤ 0)
          by (simpl sizeof in *; rep_lia); fail 1);
         try (assert (sizeof t > 0)
          by (simpl sizeof in *; rep_lia); fail 1);
         destruct (zlt 0 (sizeof t));
         auto with valid_pointer
   | context [malloc_token_sz _ _ ?n p] ⇒
         try (assert (n ≤ 0) by rep_lia; fail 1);
         try (assert (n > 0) by rep_lia; fail 1);
         destruct (zlt 0 n);
         auto with valid_pointer
   end
 end.

#[export] Hint Extern 1 (malloc_token _ _ _ _ |-- valid_pointer _) ⇒
  (simple apply malloc_token_valid_pointer; data_at_valid_aux) : valid_pointer.
#[export] Hint Extern 4 (_ |-- valid_pointer _) ⇒ malloc_token_data_at_valid_pointer : valid_pointer.

Lemma malloc_token_local_facts: ∀ {cs: compspecs} M sh t p,
      malloc_token M sh t p |-- !! (field_compatible t [] p ∧ malloc_compatible (sizeof t) p).
Proof. intros.
 unfold malloc_token.
 normalize. rewrite prop_and.
 apply andp_right. apply prop_right; auto.
 apply malloc_token_sz_local_facts.
Qed.
#[export] Hint Resolve malloc_token_local_facts : saturate_local.

Definition malloc_spec (M:MallocFreeAPD) {cs: compspecs} (t: type) :=
 DECLARE _malloc
   WITH 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 ()
       LOCAL (temp ret_temp p)
       SEP (mem_mgr M gv;
             if eq_dec p nullval then emp
            else (malloc_token M Ews t p × data_at_ Ews t p)).

Definition free_spec (M:MallocFreeAPD) {cs: compspecs} (t: type) :=
 DECLARE _free
   WITH p: val, gv: globals
   PRE [ tptr tvoid ]
       PROP ()
       PARAMS (p) GLOBALS (gv)
       SEP (mem_mgr M gv;
              if eq_dec p nullval then emp
              else (malloc_token M Ews t p × data_at_ Ews t p))
    POST [ Tvoid ]
       PROP ()
       LOCAL ()
       SEP (mem_mgr M gv).

如何使用基于类型的 malloc_spec

malloc_spec_sz 与 malloc_spec 是同一个 malloc 函数的两种规约, 任何给定程序(.c 文件) 都可以用其中之一来验证。 然而 ASI 提供的是 malloc_spec_sz。 想在客户端验证中使用 malloc_spec, 就要在 forward_call 中传入(可选的) funspec_sub 参数。
这里我们证明:对任何 compspecs, malloc_spec_sz 都蕴含 malloc_spec。
Lemma malloc_spec_sub:
 ∀ (M:MallocFreeAPD) {cs: compspecs} (t: type),
   funspec_sub (snd (malloc_spec_sz M)) (snd (malloc_spec M t)).
Proof.
  do_funspec_sub. rename w into gv. clear H.
  Exists (sizeof t, gv) emp. simpl; entailer!.
  intros tau ? ?. Exists (eval_id ret_temp tau).
  entailer!.
  if_tac; auto.
  unfold malloc_token.
  assert_PROP (field_compatible t [] (eval_id ret_temp tau)).
  { entailer!.
    apply malloc_compatible_field_compatible; auto. }
  entailer!.
  rewrite memory_block_data_at_; auto.
Qed.

Lemma free_spec_sub:
 ∀ (M:MallocFreeAPD) {cs: compspecs} (t: type),
   funspec_sub (snd (free_spec_sz M)) (snd (free_spec M t)).
Proof.
  do_funspec_sub. destruct w as [p gv]. clear H.
  Exists (sizeof t, p, gv) emp. simpl; entailer!.
  if_tac; trivial.
  sep_apply data_at__memory_block_cancel.
  unfold malloc_token; entailer!.
Qed.
使用 malloc_spec 的模式 如 VSU_stack 所示,像这样:
forward_call (malloc_spec_sub M t) gv
forward_call (free_spec_sub M (Tstruct _cons noattr)) (q, gv)
其中 t(或如 Tstruct _cons noattr) 是客户端任选的类型, 由客户端自己的 compspecs 解释。 forward_call 在导入的 ASI(即 Gprog)中 查找 malloc,找到的规约是 malloc_spec_sz。 接着它用 funspec_sub 证明, 把该规约调整为 malloc_spec M {cs} t, 其中 cs 参数按需用【客户端的】 compspecs 实例化。
free 同理。

下一章:VSU_stack

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