Design draft: improving detection of uninitialized arguments (the CallAndMessageChecker)

Hi,

Here is a proposed design draft to implement an extension of the check that warns for uninitialized arguments.
Any objections/opinions on this solution?.

Primarily looking for comments on: interfaces I plan to use and if it’s correct to put the change in CallAndMessageChecker::checkPreCall etc.

The new feature is to find arguments that are pointers to uninitialized variables, i.e. detect this:

void doStuff(const int *p);
void f(void);

void f(void) {
int x;
doStuff(&x); // ← warning “Function call argument is a pointer to uninitialized value”
}

I propose this design:

  1. Extend the CallAndMessageChecker::checkPreCall to analyse the parameters in conjunction with arguments.
  2. If argument is declared as const Type*,
  3. but corresponding parameter is pointer to undefined value in current function call,
  4. then generate warning “Function call argument is a pointer to uninitialized value”

I am going to use mainly the following interfaces:
SVal::getAsloc::MemRegionVal()
MemRegionVal::getRegion()
CheckerContext::&getState()
SVal::isUndef()
QualType::getTypePtr()
Type::getPointeeType()
QualType::isConstQualified()
FunctionDecl::getParamDecl(unsigned i)

any objects/opinions on the design?

cheers!
/Per


Per Viberg Senior Engineer
Evidente ES East AB Warfvinges väg 34 SE-112 51 Stockholm Sweden

Phone: +46 (0)8 402 79 00
Mobile: +46 (0)70 912 42 52
E-mail: Per.Viberg@evidente.se

www.evidente.se

This e-mail, which might contain confidential information, is addressed to the above stated person/company. If you are not the correct addressee, employee or in any other way the person concerned, please notify the sender immediately. At the same time, please delete this e-mail and destroy any prints. Thank You.

This seems sensible to me. I am worried about one source of false positives: functions that take a pointer value solely for the purpose of using the pointer as an ID. However, this will never happen with stack variables, and static variables are always initialized, so it’s only heap variables where this could be a problem. We could choose to special-case that or not.

Checking “const T&” parameters as well as “const T*” makes sense to me.

Please use SVal::getAsRegion instead of getAsloc::MemRegionVal. In almost all cases this makes no difference today, but the latter is at least easier to read and declares intent a little better, even though it will unnecessarily check for LocAsInteger values as well.

And finally, since this is a new set of checks with potential new false positives, please make a new entry in Checkers.td for it, and parameterize CallAndMessageChecker the same way as CheckSecuritySyntaxOnly.

This could be a very useful checker. Thank you for working on it!
Jordan

Hi Jordan, good points.

I’ve attached our current testfile that specifies wanted behaviour (regarding stack/heap etc.)

Regarding “parameterize CallAndMessageChecker the same way as CheckSecuritySyntaxOnly.”, I assume you would like to be able to turn on/off the new “check if const pointer arguments are uninitialized” functionality from the command line.

i.e.

// only check if arguments are uninitialized (the old functionality only)
clang -cc1 -analyze -analyzer-checker=core.callandmessage uninit-const.cpp

// with extended functionality, i.e. “check if const pointer arguments are uninitialized” (also performs the old check).
clang -cc1 -analyze -analyzer-checker=alpha.core.CallAndMessageUnInitRefArgChecker uninit-const.cpp

/Per


Per Viberg Senior Engineer
Evidente ES East AB Warfvinges väg 34 SE-112 51 Stockholm Sweden

Phone: +46 (0)8 402 79 00
Mobile: +46 (0)70 912 42 52
E-mail: Per.Viberg@evidente.se

www.evidente.se

This e-mail, which might contain confidential information, is addressed to the above stated person/company. If you are not the correct addressee, employee or in any other way the person concerned, please notify the sender immediately. At the same time, please delete this e-mail and destroy any prints. Thank You.

uninit-const.cpp (1.03 KB)