Skip to content

Commit

Permalink
[analyzer] Fix crash in GenericTaintChecker when propagatig taint to …
Browse files Browse the repository at this point in the history
…AllocaRegion

The `GenericTaintChecker` checker was crashing, when the taint
was propagated to `AllocaRegion` region in following code:
```
  int x;
  void* p = alloca(10);
  mempcy(p, &x, sizeof(x));
```
This crash was caused by the fact that determining type of
`AllocaRegion` returns a null `QualType`.

This patch makes `AllocaRegion` expose its type as `void`,
making them consistent with results of `malloc` or `new`
that produce `SymRegion` with `void*` symbol.

Reviewed By: steakhal, xazax.hun

Differential Revision: https://reviews.llvm.org/D155847
  • Loading branch information
tomasz-kaminski-sonarsource authored and Tomasz Kamiński committed Jul 24, 2023
1 parent 595d5f3 commit 438fc2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clang/lib/StaticAnalyzer/Core/SVals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ class TypeRetrievingVisitor
QualType VisitSymbolicRegion(const SymbolicRegion *SR) {
return Visit(SR->getSymbol());
}
QualType VisitAllocaRegion(const AllocaRegion *) {
return QualType{Context.VoidPtrTy};
}
QualType VisitTypedRegion(const TypedRegion *TR) {
return TR->getLocationType();
}
Expand Down
19 changes: 19 additions & 0 deletions clang/test/Analysis/taint-generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,25 @@ void testTaintedVLASize(void) {
int vla[x]; // expected-warning{{Declared variable-length array (VLA) has tainted size}}
}

int testTaintedAllocaMem() {
char x;
void * p;
scanf("%c", &x);
p = __builtin_alloca(1);
__builtin_memcpy(p, &x, 1);
return 5 / *(char*)p; // expected-warning {{Division by a tainted value, possibly zero}}
}

int testTaintedMallocMem() {
char x;
void * p;
scanf("%c", &x);
p = malloc(1);
__builtin_memcpy(p, &x, 1);
return 5 / *(char*)p; // expected-warning {{Division by a tainted value, possibly zero}}
}


// This computation used to take a very long time.
#define longcmp(a,b,c) { \
a -= c; a ^= c; c += b; b -= a; b ^= (a<<6) | (a >> (32-b)); a += c; c -= b; c ^= b; b += a; \
Expand Down

0 comments on commit 438fc2c

Please sign in to comment.