2009-04-22 05:51:34 +08:00
|
|
|
//== Store.cpp - Interface for maps from Locations to Values ----*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defined the types Store and StoreManager.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/PathSensitive/Store.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-07-30 05:43:22 +08:00
|
|
|
StoreManager::StoreManager(GRStateManager &stateMgr)
|
|
|
|
: ValMgr(stateMgr.getValueManager()), StateMgr(stateMgr),
|
2009-04-22 05:51:34 +08:00
|
|
|
MRMgr(ValMgr.getRegionManager()) {}
|
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
|
2009-11-16 12:49:44 +08:00
|
|
|
QualType EleTy, uint64_t index) {
|
2009-08-01 14:17:29 +08:00
|
|
|
SVal idx = ValMgr.makeArrayIndex(index);
|
2009-10-14 14:55:01 +08:00
|
|
|
return MRMgr.getElementRegion(EleTy, idx, Base, ValMgr.getContext());
|
2009-07-07 06:23:45 +08:00
|
|
|
}
|
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: Merge with the implementation of the same method in MemRegion.cpp
|
2009-07-07 07:47:19 +08:00
|
|
|
static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
2009-07-07 07:47:19 +08:00
|
|
|
const RecordDecl *D = RT->getDecl();
|
|
|
|
if (!D->getDefinition(Ctx))
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-07 07:47:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-07 04:21:51 +08:00
|
|
|
ASTContext& Ctx = StateMgr.getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-07 06:39:40 +08:00
|
|
|
// Handle casts to Objective-C objects.
|
2009-08-01 14:17:29 +08:00
|
|
|
if (CastToTy->isObjCObjectPointerType())
|
2009-11-10 10:17:20 +08:00
|
|
|
return R->StripCasts();
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2009-07-18 14:27:51 +08:00
|
|
|
if (CastToTy->isBlockPointerType()) {
|
2009-08-28 12:49:15 +08:00
|
|
|
// FIXME: We may need different solutions, depending on the symbol
|
2009-07-18 14:27:51 +08:00
|
|
|
// involved. Blocks can be casted to/from 'id', as they can be treated
|
2009-08-28 12:49:15 +08:00
|
|
|
// as Objective-C objects. This could possibly be handled by enhancing
|
2009-09-09 23:08:12 +08:00
|
|
|
// our reasoning of downcasts of symbolic objects.
|
2009-08-28 12:49:15 +08:00
|
|
|
if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
|
2009-10-14 14:55:01 +08:00
|
|
|
return R;
|
2009-07-18 14:27:51 +08:00
|
|
|
|
|
|
|
// We don't know what to make of it. Return a NULL region, which
|
|
|
|
// will be interpretted as UnknownVal.
|
2009-10-14 14:55:01 +08:00
|
|
|
return NULL;
|
2009-07-18 14:27:51 +08:00
|
|
|
}
|
2009-07-07 06:23:45 +08:00
|
|
|
|
2009-07-07 04:21:51 +08:00
|
|
|
// Now assume we are casting from pointer to pointer. Other cases should
|
|
|
|
// already be handled.
|
2009-07-30 05:53:49 +08:00
|
|
|
QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
|
2009-08-02 12:12:53 +08:00
|
|
|
QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
|
|
|
|
|
|
|
|
// Handle casts to void*. We just pass the region through.
|
First part of changes to eliminate problems with cv-qualifiers and
sugared types. The basic problem is that our qualifier accessors
(getQualifiers, getCVRQualifiers, isConstQualified, etc.) only look at
the current QualType and not at any qualifiers that come from sugared
types, meaning that we won't see these qualifiers through, e.g.,
typedefs:
typedef const int CInt;
typedef CInt Self;
Self.isConstQualified() currently returns false!
Various bugs (e.g., PR5383) have cropped up all over the front end due
to such problems. I'm addressing this problem by splitting each
qualifier accessor into two versions:
- the "local" version only returns qualifiers on this particular
QualType instance
- the "normal" version that will eventually combine qualifiers from this
QualType instance with the qualifiers on the canonical type to
produce the full set of qualifiers.
This commit adds the local versions and switches a few callers from
the "normal" version (e.g., isConstQualified) over to the "local"
version (e.g., isLocalConstQualified) when that is the right thing to
do, e.g., because we're printing or serializing the qualifiers. Also,
switch a bunch of
Context.getCanonicalType(T1).getUnqualifiedType() == Context.getCanonicalType(T2).getQualifiedType()
expressions over to
Context.hasSameUnqualifiedType(T1, T2)
llvm-svn: 88969
2009-11-17 05:35:15 +08:00
|
|
|
if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
|
2009-10-14 14:55:01 +08:00
|
|
|
return R;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-02 12:12:53 +08:00
|
|
|
// Handle casts from compatible types.
|
2009-08-01 14:17:29 +08:00
|
|
|
if (R->isBoundable())
|
|
|
|
if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
|
|
|
|
QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
|
2009-08-02 12:12:53 +08:00
|
|
|
if (CanonPointeeTy == ObjTy)
|
2009-10-14 14:55:01 +08:00
|
|
|
return R;
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 04:21:51 +08:00
|
|
|
// Process region cast according to the kind of the region being cast.
|
2009-07-07 06:56:37 +08:00
|
|
|
switch (R->getKind()) {
|
|
|
|
case MemRegion::BEG_TYPED_REGIONS:
|
|
|
|
case MemRegion::MemSpaceRegionKind:
|
|
|
|
case MemRegion::BEG_DECL_REGIONS:
|
|
|
|
case MemRegion::END_DECL_REGIONS:
|
2009-07-30 05:43:22 +08:00
|
|
|
case MemRegion::END_TYPED_REGIONS: {
|
2009-07-07 06:56:37 +08:00
|
|
|
assert(0 && "Invalid region cast");
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-11-25 09:32:22 +08:00
|
|
|
|
|
|
|
case MemRegion::FunctionTextRegionKind:
|
2009-11-26 07:58:21 +08:00
|
|
|
case MemRegion::BlockTextRegionKind:
|
|
|
|
case MemRegion::BlockDataRegionKind: {
|
2009-07-18 14:27:51 +08:00
|
|
|
// CodeTextRegion should be cast to only a function or block pointer type,
|
2009-10-14 14:55:01 +08:00
|
|
|
// although they can in practice be casted to anything, e.g, void*, char*,
|
|
|
|
// etc.
|
|
|
|
// Just return the region.
|
|
|
|
return R;
|
2009-07-07 06:56:37 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-07 06:56:37 +08:00
|
|
|
case MemRegion::StringRegionKind:
|
|
|
|
case MemRegion::ObjCObjectRegionKind:
|
|
|
|
// FIXME: Need to handle arbitrary downcasts.
|
2009-08-01 14:17:29 +08:00
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
case MemRegion::AllocaRegionKind:
|
2009-07-07 06:56:37 +08:00
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2009-09-09 23:08:12 +08:00
|
|
|
case MemRegion::VarRegionKind:
|
2009-10-14 14:55:01 +08:00
|
|
|
return MakeElementRegion(R, PointeeTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
case MemRegion::ElementRegionKind: {
|
|
|
|
// If we are casting from an ElementRegion to another type, the
|
|
|
|
// algorithm is as follows:
|
|
|
|
//
|
|
|
|
// (1) Compute the "raw offset" of the ElementRegion from the
|
|
|
|
// base region. This is done by calling 'getAsRawOffset()'.
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
// (2a) If we get a 'RegionRawOffset' after calling
|
2009-08-01 14:17:29 +08:00
|
|
|
// 'getAsRawOffset()', determine if the absolute offset
|
2009-09-09 23:08:12 +08:00
|
|
|
// can be exactly divided into chunks of the size of the
|
|
|
|
// casted-pointee type. If so, create a new ElementRegion with
|
2009-08-01 14:17:29 +08:00
|
|
|
// the pointee-cast type as the new ElementType and the index
|
|
|
|
// being the offset divded by the chunk size. If not, create
|
|
|
|
// a new ElementRegion at offset 0 off the raw offset region.
|
|
|
|
//
|
|
|
|
// (2b) If we don't a get a 'RegionRawOffset' after calling
|
|
|
|
// 'getAsRawOffset()', it means that we are at offset 0.
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: Handle symbolic raw offsets.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
const ElementRegion *elementR = cast<ElementRegion>(R);
|
|
|
|
const RegionRawOffset &rawOff = elementR->getAsRawOffset();
|
|
|
|
const MemRegion *baseR = rawOff.getRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// If we cannot compute a raw offset, throw up our hands and return
|
|
|
|
// a NULL MemRegion*.
|
|
|
|
if (!baseR)
|
2009-10-14 14:55:01 +08:00
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
int64_t off = rawOff.getByteOffset();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (off == 0) {
|
|
|
|
// Edge case: we are at 0 bytes off the beginning of baseR. We
|
|
|
|
// check to see if type we are casting to is the same as the base
|
2009-09-09 23:08:12 +08:00
|
|
|
// region. If so, just return the base region.
|
2009-08-01 14:17:29 +08:00
|
|
|
if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
|
|
|
|
QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
|
|
|
|
QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
|
|
|
|
if (CanonPointeeTy == ObjTy)
|
2009-10-14 14:55:01 +08:00
|
|
|
return baseR;
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// Otherwise, create a new ElementRegion at offset 0.
|
2009-10-14 14:55:01 +08:00
|
|
|
return MakeElementRegion(baseR, PointeeTy);
|
2009-07-07 07:47:19 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// We have a non-zero offset from the base region. We want to determine
|
|
|
|
// if the offset can be evenly divided by sizeof(PointeeTy). If so,
|
|
|
|
// we create an ElementRegion whose index is that value. Otherwise, we
|
|
|
|
// create two ElementRegions, one that reflects a raw offset and the other
|
|
|
|
// that reflects the cast.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// Compute the index for the new ElementRegion.
|
|
|
|
int64_t newIndex = 0;
|
|
|
|
const MemRegion *newSuperR = 0;
|
2009-07-07 07:47:19 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// We can only compute sizeof(PointeeTy) if it is a complete type.
|
|
|
|
if (IsCompleteType(Ctx, PointeeTy)) {
|
|
|
|
// Compute the size in **bytes**.
|
|
|
|
int64_t pointeeTySize = (int64_t) (Ctx.getTypeSize(PointeeTy) / 8);
|
|
|
|
|
|
|
|
// Is the offset a multiple of the size? If so, we can layer the
|
|
|
|
// ElementRegion (with elementType == PointeeTy) directly on top of
|
|
|
|
// the base region.
|
|
|
|
if (off % pointeeTySize == 0) {
|
|
|
|
newIndex = off / pointeeTySize;
|
|
|
|
newSuperR = baseR;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (!newSuperR) {
|
|
|
|
// Create an intermediate ElementRegion to represent the raw byte.
|
|
|
|
// This will be the super region of the final ElementRegion.
|
2009-10-14 14:55:01 +08:00
|
|
|
newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off);
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
return MakeElementRegion(newSuperR, PointeeTy, newIndex);
|
2009-07-07 04:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
assert(0 && "unreachable");
|
|
|
|
return 0;
|
2009-07-07 04:21:51 +08:00
|
|
|
}
|
2009-08-26 04:51:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// CastRetrievedVal - Used by subclasses of StoreManager to implement
|
|
|
|
/// implicit casts that arise from loads from regions that are reinterpreted
|
|
|
|
/// as another region.
|
2009-11-16 12:49:44 +08:00
|
|
|
SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
|
|
|
|
QualType castTy) {
|
2009-08-26 04:51:30 +08:00
|
|
|
if (castTy.isNull())
|
2009-11-16 12:49:44 +08:00
|
|
|
return V;
|
|
|
|
|
2009-11-20 03:04:08 +08:00
|
|
|
assert(ValMgr.getContext().hasSameUnqualifiedType(castTy,
|
|
|
|
R->getValueType(ValMgr.getContext())));
|
2009-11-16 12:49:44 +08:00
|
|
|
return V;
|
2009-08-26 04:51:30 +08:00
|
|
|
}
|
|
|
|
|
2009-12-03 11:27:11 +08:00
|
|
|
const GRState *StoreManager::InvalidateRegions(const GRState *state,
|
|
|
|
const MemRegion * const *I,
|
|
|
|
const MemRegion * const *End,
|
|
|
|
const Expr *E,
|
|
|
|
unsigned Count,
|
|
|
|
InvalidatedSymbols *IS) {
|
|
|
|
for ( ; I != End ; ++I)
|
|
|
|
state = InvalidateRegion(state, *I, E, Count, IS);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|