1

Is there any "almost-usable" static analysis tool for C (or C-like) programs that can automatically infer loop termination, at least for very simple programs?

I looked around a bit and found several research articles, a few prototypes, and even some tools (such as Frama-C) that try to infer some termination properties from an annotated source code, but I was expecting to find at least one simple tool that you could just give it a C program and it would output: loop #N terminates/does not terminate/unknown.

(I know this is undecidable in the general case, but for some classes of loops semi-algorithms are possible).

I'd also be interested in tools that work for imperative languages other than C, such as Java.

Edit: just an update to my question, I found LoopFrog, built on top of goto-cc, that seems to be in the direction of what I was looking for, however I still didn't have time to actually understand what its output means precisely. Should it be the answer to my question, I'll post an update here.

2
  • 1
    LLVM loop analysis passes can infer loop bounds, and in some trivial cases can prove the loop does not terminate.
    – SK-logic
    Nov 29, 2012 at 12:09
  • Thanks, I'll take a look at that. It doesn't seem as "ready-made" as I expected, but it might be useful anyway.
    – anol
    Nov 29, 2012 at 12:29

1 Answer 1

1

I don't know if you have read these two blog posts (1, 2), but one of the “simple” tools you are looking for could be a script that, in parallel, launches Frama-C's value analysis as an ordinary sound abstract interpreter (able to infer that the end of a program is unreachable) and with its option -obviously-terminates (in which case it can infer that all executions of a program terminate). In both cases you might want to use a timeout. For the analysis with option -obviously-terminates, the timeout is mandatory, because the analysis fails to terminate if the analysed program does not itself terminate.

According to these blog posts I wrote, you should be able to diagnose the following examples, not all of which are entirely trivial:

char x, y;

main()
{
  x = input();
  y = input();
  while (x>0 && y>0)
    {
      if (input() == 1)
    {
      x = x - 1;
      y = input();
    }
      else
    y = y - 1;
    }
}

Terminates


char x, y;

main()
{
  x = input();
  y = input();

  while (x>0 && y>0)
    {
      // Frama_C_dump_each();
      if (input())
        {
          x = x - 1;
          y = x;
        }
      else
        {
          x = y - 2;
          y = x + 1;
        }
    }
}

Terminates


char x;

main(){
  x = input();
  while (x > 0)
    {
      Frama_C_dump_each();
      if (x > 11)
        x = x - 12;
      else
        x = x + 1;
    }
}

Terminates


unsigned char u;

int main(){
  while (u * u != 17)
    {
      u = u + 1;
    }
  return u;
}

Does not terminate.


However, the option -obviously-terminates involved in these examples was not originally designed for this use (it was more of an optimisation for the analysis of a certain kind of program). I did not realise that in some rare cases, when this option was set, the analysis could terminate without the analysed program itself terminating. If you are willing to recompile from sources, this issue would be fixed by setting variable obviously_terminates to true (instead of false) in file state_set.ml. If you have reasons to think that this script is not the solution you are looking for, then don't bother: the issue seems rare, as I said. I only noticed it while trying to determinate whether programs terminated in a more competitive setting.

1
  • Thanks, I had probably overlooked that. I'll try it when I get the time and I believe this will work as I intended.
    – anol
    Dec 20, 2012 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.