This is an archive of the discontinued LLVM Phabricator instance.

[Analyzer][StreamChecker] Track streams that were not found to be opened.
Needs ReviewPublic

Authored by balazske on Apr 16 2020, 12:56 AM.

Details

Summary

If a stream operation is encountered with unknown stream (to the checker)
the stream is recognized and added to the data structure.
(Instead of ignoring the call.)

This patch in only here for reference purposes, to not forget that this can be a useful feature. Probably at a later time we can think about this problem again and adapt this change to the StreamChecker at that time. The problem is that if we find a stream function call without a stream open call, the stream was opened presumably at another function. So it is assumable that the other function (where it was opened) handles some of the stream calls and error handling, probably together with the current function, so analysing the current function only may produce wrong results (but still it is possible to discover real errors). (And if we have luck the current function is inlined from that other function.)

Diff Detail

Event Timeline

balazske created this revision.Apr 16 2020, 12:56 AM

I am not sure if this is the best approach: In a similar case (no opening function encountered) it is possible that the stream is already "escaped", there could be references to it from outside the analyzed function (to which it was passed, or got from another non-analyzed function). Maybe it is better to ignore such cases as it is already without this change, then we do not need to handle "escape" of the stream pointer specially (only remove it from the data).

The high level idea seems great after surveying the analyzer for similar issues, but I might need to think about this a bit longer. @baloghadamsoftware, IteratorChecker needs to solve similar problems, right? Do you have any input on this?

I like this idea, except the word OpenEncountered, but that might be a matter of taste. Please try this patch on several open-source projects, such as BitCoin, CURL, OpenSSL, PostGreS, TMux and Xerces. Then compare the results whether we have more or less true and false positives.

clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
36

Maybe SawOpened or ExplicitlyOpened?

NoQ added inline comments.May 14 2020, 7:12 AM
clang/test/Analysis/stream.c
11–13

What do these tests test?

balazske marked an inline comment as done.May 14 2020, 7:57 AM

Currently priority of this change is lower than D78374.
If a similar change is done a "escaped" stream state will be needed too.

clang/test/Analysis/stream.c
11–13

There should be no warning about resource leak (file not closed). This may be not the best approach to show what the checker does. Some other detectable problem should be added to show that the functions are not ignored by the checker.
(I do not like that the buffer argument is 0 here, probably other checker should check this.)

Szelethus added inline comments.
clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
36

Shouldn't this be in KindTy?

Its a bit hard to judge this. Have you tested this on open source projects?

balazske edited the summary of this revision. (Show Details)Jul 10 2020, 7:27 AM