[英]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.