簡體   English   中英

Frama-C行為和價值分析

[英]Frama-C behaviors and value analysis

我想使用Frama-C來分析包含類似read函數的程序:給定緩沖區buf及其長度len ,該函數在buf寫入正確的len個字節(除非出現錯誤)。

我用ACSL來指定它,但價值分析給了我奇怪的結果。

這是我的規范,還有一個用於測試的main功能:

/*@
  assigns \result \from \nothing;
  assigns *(buf+(0..len-1)) \from \nothing;
  behavior ok:
    requires \valid(buf+(0..len-1));
    ensures  \result == 0;
    ensures  \initialized(buf+(0..len-1));
  behavior error:
    ensures  \result == -1;
 */
int read(char *buf, int len);

int main() {
  char buf[10];
  read(buf, 10);
  return 0;
}

運行frama-c -val test.c (我正在使用Frama-C Neon)時,我得到了這個結果:

[value] Analyzing a complete application starting at main
[value] Computing initial state
[value] Initial state computed
[value] Values of globals at initialization

[value] computing for function read <- main.
        Called from test.c:16.
[value] using specification for function read
test.c:6:[value] Function read, behavior ok: precondition got status valid.
test.c:10:[value] Function read, behavior error: this postcondition evaluates to false in this
        context. If it is valid, either a precondition was not verified for this
        call, or some assigns/from clauses are incomplete (or incorrect).
[value] Done for function read
[value] Recording results for main
[value] done for function main
[value] ====== VALUES COMPUTED ======
[value] Values at end of function main:
  NON TERMINATING FUNCTION

我確實放了assigns / from子句,並且沒有error行為的先決條件(因此,默認情況下,它們已經過驗證)。

這里發生了什么? 在這種情況下,如何使分析工作?

盡管消息,實際的問題是,有在的規范的錯誤read功能:兩種行為是活躍在同一時間 ,因為它們含有不assumes子句(隱式,兩者各有assumes \\true )。 因此,兩個都ensures子句為真,這意味着\\result == 0 && \\result == 1

該錯誤導致矛盾狀態,其中函數的結果為0和1(同時),因此不能返回結果,因此該函數被認為是非終止的。

其中這里的幾個可能的解決方案在於增加了不確定性鬼變,說_read_state ,代表功能的內部狀態,然后使用這個變量定義不相交的assumes條款對不同的行為:

//@ ghost volatile int _read_state;
/*@
  assigns \result \from \nothing;
  assigns *(buf+(0..len-1)) \from \nothing;
  behavior ok:
    assumes _read_state == 0;
    requires \valid(buf+(0..len-1));
    ensures  \result == 0;
    ensures  \initialized(buf+(0..len-1));
  behavior error:
    assumes _read_state != 0;
    ensures  \result == -1;
 */

注意== 0!= 0比較是任意的; 任何可能不相交的值都可以在這里工作。

使用此規范,我們獲得此程序的預期結果:

[value] Analyzing a complete application starting at main
[value] Computing initial state
[value] Initial state computed
[value] Values of globals at initialization
  _read_state ∈ [--..--]
[value] computing for function read <- main.
        Called from test.c:18.
[value] using specification for function read
tread.c:7:[value] Function read, behavior ok: 
    precondition got status valid. 
    (Behavior may be inactive, no reduction performed.)
[value] Done for function read
[value] Recording results for main
[value] done for function main
[value] ====== VALUES COMPUTED ======
[value] Values at end of function main:
  buf[0..9] ∈ [--..--] or UNINITIALIZED
  __retres ∈ {0}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM