[英]Frama-C Prove While Loop with "/*@ ensures"
我是 Frama-C 的新手,我正在嘗試驗證 C 代碼。 該代碼非常基本,但不知何故我無法驗證它。
總之,我試圖證明該函數或循環是否曾經運行過。 為此,我在開始時給變量一個值 (4)。 在函數中,我將值更改為“5”,並嘗試確保變量 5 在末尾。
編碼:
#include <stdio.h>
int x=4;
/*@
ensures x ==5;
*/
int abs(int val){
int accept=0;
int count=3;
/*@
loop invariant 0 <= count <= \at(count, Pre);
loop assigns accept,x,count;
loop variant count;
*/
while (count !=0){
x=5;
accept =1;
count--;
}
return x;
}
我將命令作為“frama-c-gui -wp -rte testp4.c”給 CLI 以啟動 frama-c。
結果: Frama-1
但是 "*@ 確保 x ≡ 5; */" 仍然是未知的。
有沒有人可以幫助我解決這個問題? 如果我取"x=5;"
到 while 循環之外(返回之前)它驗證/*@ ensures x ==5; */
/*@ ensures x ==5; */
提前感謝所有做出貢獻的人!
這里的大多數問題都與內置標簽的使用有關。 這里是我們需要的標簽:
第一件事。 我不知道您使用的是哪個版本的 Frama-C,但我建議更新 ;) 。 您將收到以下錯誤消息以防止出現錯誤:
[kernel:annot-error] file.c:11: Warning:
unbound logic variable count. Ignoring loop annotation
為什么? 因為您在談論不存在的前提條件下count
。 請注意,在舊版本的 Frama-C 中,WP 可能對此沒問題,因為在某些情況下合並了局部變量的預狀態和初始化。
我們想要的可能是“ count
的當前值介於 0 和我們開始循環時的值之間”,因此:
loop invariant 0 <= count <= \at(count, LoopEntry);
which is:
loop invariant 0 <= \at(count, Here) <= \at(count, LoopEntry);
現在我們有了這個(經過驗證的)不變量,我們想將循環的行為與后置條件聯系起來(當前,我們的循環不變量沒有說明x
並且 WP 沒有使用循環體。這意味着我們需要另一個不變量,即“當開始循環時count
不等於其值時, x
為 5`”。
因此:
loop invariant count != \at(count, LoopEntry) ==> x == 5 ;
這允許完全證明該程序:)。 最終注釋程序:
#include <stdio.h>
int x=4;
/*@
ensures x ==5;
*/
int abs(int val){
int accept=0;
int count=3;
/*@
loop invariant 0 <= count <= \at(count, LoopEntry);
loop invariant count != \at(count, LoopEntry) ==> x == 5 ;
loop assigns accept,x,count;
loop variant count;
*/
while (count !=0){
x=5;
accept =1;
count--;
}
return x;
}
關於為什么\at(count, Pre) != 0 ==> \at(x, Post) == 5
(在評論中)不起作用的一些話:
\at(count, Pre)
是函數前置條件下的count
值, \at(count, Post)
是函數后置條件下的count
值。 因此,這里不是我們需要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.