简体   繁体   English

高循环边界的值分析

[英]Value analysis for high loop bounds

I am analysing a control program with the following structure:我正在分析具有以下结构的控制程序:

unsigned int cnt=0;
unsigned int inc=3;
...

void main(){
int i;
int lim;

for(i=0;i<100000;i++)
{
  f1();
 ....
  lim = f2();
  if(cnt < lim)
    cnt += inc;
 ....
}
}

My aim is to analyse enough loop iterations to show that the cnt variable cannot overflow.我的目标是分析足够的循环迭代以表明 cnt 变量不会溢出。 Increasing the slevel alone will not help since the state space will get too high.单独增加 slevel 不会有帮助,因为状态空间会变得太高。 I saw that the slevel can be adjusted for individual functions.我看到可以针对各个功能调整 slevel。 Is this also possible for eg a single if/else construct?这对于例如单个 if/else 构造也可能吗? Increasing the slevel for a whole function can be already too much for such loop structures.对于这样的循环结构,增加整个函数的 slevel 可能已经太多了。 Is there a way to prove the absence of an overflow without writing complex loop invariants and assertions?有没有办法在不编写复杂的循环不变量和断言的情况下证明不存在溢出?

BR, Harald BR,哈拉尔

I've taken the liberty of specifying that f2 returns something positive.我冒昧地指定f2返回正数。 Otherwise, the test if(cnt < lim) performs a negative -> unsigned conversion, which Value does not handle precisely at the moment.否则,测试if(cnt < lim)执行负 -> 无符号转换,此时 Value 无法精确处理。 And in fact, your property does not hold if f2 returns always -1 !而事实上,你的财产成立,如果f2总是返回-1

Under this hypothesis, cnt does not overflow.在这种假设下, cnt不会溢出。

unsigned int cnt=0;
unsigned int inc=3;

//@ assigns \result \from \nothing; ensures 0 <= \result;
int f2();

void main(){
  int i;
  int lim;

  for(i=0;i<100000;i++)
    {
      f1();
      lim = f2();      
      if(cnt < lim)
        cnt += inc;
    }
}

This is the result of the analysis.这是分析的结果。 cnt has not overflown, since its maximal value is 4294967295. cnt没有溢出,因为它的最大值是 4294967295。

[value] Values at end of function main:
  cnt ∈ [0..2147483649],0%3
  i ∈ {100000}
  lim ∈ [0..2147483647]

If f2 can return negative values <= -4, I'm not sure the result can be proven without using eg the WP plugin.如果f2可以返回负值 <= -4,我不确定可以在不使用例如 WP 插件的情况下证明结果。

Regarding the rest of your question, there are various alternatives to better use the amount of slevel required for an analysis, but probably nothing that would help you here.关于您的其余问题,有多种替代方法可以更好地使用分析所需的 slevel 量,但在这里可能没有任何帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM