我正在尝试学习 ACSL,但在尝试编写完整的规范时遇到了困难。我的代码
#include <stdint.h>
#include <stddef.h>
#define NUM_ELEMS (8)
/*@ requires expected != test;
@ requires \let n = NUM_ELEMS;
@ \valid_read(expected + (0.. n-1)) && \valid_read(test + (0.. n-1));
@ assigns \nothing;
@ behavior matches:
@ assumes \let n = NUM_ELEMS;
@ \forall integer i; 0 <= i < n ==> expected[i] == test[i];
@ ensures \result == 1;
@ behavior not_matches:
@ assumes \let n = NUM_ELEMS;
@ \exists integer i; 0 <= i < n && expected[i] != test[i];
@ ensures \result == 0;
@ complete behaviors;
@ disjoint behaviors;
@*/
int array_equals(const uint32_t expected[static NUM_ELEMS], const uint32_t test[static NUM_ELEMS]) {
for (size_t i = 0; i < NUM_ELEMS; i++) {
if (expected[i] != test[i]) {
return 0;
}
}
return 1;
}
我运行它
frama-c -wp -wp-rte test.c
我看到以下日志
[kernel] Parsing FRAMAC_SHARE/libc/__fc_builtin_for_normalization.i (no preprocessing)
[kernel] Parsing test.c (with preprocessing)
[rte] annotating function array_equals
test.c:22:[wp] warning: Missing assigns clause (assigns 'everything' instead)
[wp] 9 goals scheduled
[wp] [Alt-Ergo] Goal typed_array_equals_assign_part1 : Unknown (Qed:2ms) (67ms)
[wp] [Alt-Ergo] Goal typed_array_equals_assert_rte_mem_access_2 : Unknown (Qed:2ms) (128ms)
[wp] [Alt-Ergo] Goal typed_array_equals_assert_rte_mem_access : Unknown (Qed:2ms) (125ms)
[wp] [Alt-Ergo] Goal typed_array_equals_matches_post : Unknown (Qed:10ms) (175ms)
[wp] [Alt-Ergo] Goal typed_array_equals_not_matches_post : Unknown (Qed:7ms) (109ms)
[wp] Proved goals: 4 / 9
Qed: 4 (0.56ms-4ms)
Alt-Ergo: 0 (unknown: 5)
所以似乎我的两种行为和“分配\无”无法得到证明。那么我该如何从这里开始呢?
编辑:所以我想出了眼前的问题。我没有注释我的循环:
/*@ loop invariant \let n = NUM_ELEMS; 0 <= i <= n;
@ loop invariant \forall integer k; 0 <= k < i ==> expected[k] == test[k];
@ loop assigns i;
@ loop variant \let n = NUM_ELEMS; n-i;
@*/
我更大的问题仍然存在:调试问题的好方法是什么?我通过更改和删除代码并查看已证明/未证明的内容来解决这个问题。