1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
//===- BasicValueFactory.cpp - Basic values for Path Sens analysis --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines BasicValueFactory, a class that manages the lifetime
// of APSInt objects and symbolic constraints used by ExprEngine
// and related classes.
//
//===----------------------------------------------------------------------===//
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/ImmutableList.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include <cassert>
#include <cstdint>
#include <utility>
using namespace clang;
using namespace ento;
void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
llvm::ImmutableList<SVal> L) {
T.Profile(ID);
ID.AddPointer(L.getInternalPointer());
}
void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
const StoreRef &store,
const TypedValueRegion *region) {
ID.AddPointer(store.getStore());
ID.AddPointer(region);
}
void PointerToMemberData::Profile(
llvm::FoldingSetNodeID &ID, const NamedDecl *D,
llvm::ImmutableList<const CXXBaseSpecifier *> L) {
ID.AddPointer(D);
ID.AddPointer(L.getInternalPointer());
}
using SValData = std::pair<SVal, uintptr_t>;
using SValPair = std::pair<SVal, SVal>;
namespace llvm {
template<> struct FoldingSetTrait<SValData> {
static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
X.first.Profile(ID);
ID.AddPointer( (void*) X.second);
}
};
template<> struct FoldingSetTrait<SValPair> {
static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
X.first.Profile(ID);
X.second.Profile(ID);
}
};
} // namespace llvm
using PersistentSValsTy =
llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData>>;
using PersistentSValPairsTy =
llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair>>;
BasicValueFactory::~BasicValueFactory() {
// Note that the dstor for the contents of APSIntSet will never be called,
// so we iterate over the set and invoke the dstor for each APSInt. This
// frees an aux. memory allocated to represent very large constants.
for (const auto &I : APSIntSet)
I.getValue().~APSInt();
delete (PersistentSValsTy*) PersistentSVals;
delete (PersistentSValPairsTy*) PersistentSValPairs;
}
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
llvm::FoldingSetNodeID ID;
void *InsertPos;
using FoldNodeTy = llvm::FoldingSetNodeWrapper<llvm::APSInt>;
X.Profile(ID);
FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
if (!P) {
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
new (P) FoldNodeTy(X);
APSIntSet.InsertNode(P, InsertPos);
}
return *P;
}
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
bool isUnsigned) {
llvm::APSInt V(X, isUnsigned);
return getValue(V);
}
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
bool isUnsigned) {
llvm::APSInt V(BitWidth, isUnsigned);
V = X;
return getValue(V);
}
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
return getValue(getAPSIntType(T).getValue(X));
}
const CompoundValData*
BasicValueFactory::getCompoundValData(QualType T,
llvm::ImmutableList<SVal> Vals) {
llvm::FoldingSetNodeID ID;
CompoundValData::Profile(ID, T, Vals);
void *InsertPos;
CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
if (!D) {
D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
new (D) CompoundValData(T, Vals);
CompoundValDataSet.InsertNode(D, InsertPos);
}
return D;
}
const LazyCompoundValData*
BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
const TypedValueRegion *region) {
llvm::FoldingSetNodeID ID;
LazyCompoundValData::Profile(ID, store, region);
void *InsertPos;
LazyCompoundValData *D =
LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
if (!D) {
D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
new (D) LazyCompoundValData(store, region);
LazyCompoundValDataSet.InsertNode(D, InsertPos);
}
return D;
}
const PointerToMemberData *BasicValueFactory::getPointerToMemberData(
const NamedDecl *ND, llvm::ImmutableList<const CXXBaseSpecifier *> L) {
llvm::FoldingSetNodeID ID;
PointerToMemberData::Profile(ID, ND, L);
void *InsertPos;
PointerToMemberData *D =
PointerToMemberDataSet.FindNodeOrInsertPos(ID, InsertPos);
if (!D) {
D = (PointerToMemberData *)BPAlloc.Allocate<PointerToMemberData>();
new (D) PointerToMemberData(ND, L);
PointerToMemberDataSet.InsertNode(D, InsertPos);
}
return D;
}
LLVM_ATTRIBUTE_UNUSED bool hasNoRepeatedElements(
llvm::ImmutableList<const CXXBaseSpecifier *> BaseSpecList) {
llvm::SmallPtrSet<QualType, 16> BaseSpecSeen;
for (const CXXBaseSpecifier *BaseSpec : BaseSpecList) {
QualType BaseType = BaseSpec->getType();
// Check whether inserted
if (!BaseSpecSeen.insert(BaseType).second)
return false;
}
return true;
}
const PointerToMemberData *BasicValueFactory::accumCXXBase(
llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
const nonloc::PointerToMember &PTM, const CastKind &kind) {
assert((kind == CK_DerivedToBaseMemberPointer ||
kind == CK_BaseToDerivedMemberPointer ||
kind == CK_ReinterpretMemberPointer) &&
"accumCXXBase called with wrong CastKind");
nonloc::PointerToMember::PTMDataType PTMDT = PTM.getPTMData();
const NamedDecl *ND = nullptr;
llvm::ImmutableList<const CXXBaseSpecifier *> BaseSpecList;
if (PTMDT.isNull() || PTMDT.is<const NamedDecl *>()) {
if (PTMDT.is<const NamedDecl *>())
ND = PTMDT.get<const NamedDecl *>();
BaseSpecList = CXXBaseListFactory.getEmptyList();
} else {
const PointerToMemberData *PTMD = PTMDT.get<const PointerToMemberData *>();
ND = PTMD->getDeclaratorDecl();
BaseSpecList = PTMD->getCXXBaseList();
}
assert(hasNoRepeatedElements(BaseSpecList) &&
"CXXBaseSpecifier list of PointerToMemberData must not have repeated "
"elements");
if (kind == CK_DerivedToBaseMemberPointer) {
// Here we pop off matching CXXBaseSpecifiers from BaseSpecList.
// Because, CK_DerivedToBaseMemberPointer comes from a static_cast and
// serves to remove a matching implicit cast. Note that static_cast's that
// are no-ops do not count since they produce an empty PathRange, a nice
// thing about Clang AST.
// Now we know that there are no repetitions in BaseSpecList.
// So, popping the first element from it corresponding to each element in
// PathRange is equivalent to only including elements that are in
// BaseSpecList but not it PathRange
auto ReducedBaseSpecList = CXXBaseListFactory.getEmptyList();
for (const CXXBaseSpecifier *BaseSpec : BaseSpecList) {
auto IsSameAsBaseSpec = [&BaseSpec](const CXXBaseSpecifier *I) -> bool {
return BaseSpec->getType() == I->getType();
};
if (llvm::none_of(PathRange, IsSameAsBaseSpec))
ReducedBaseSpecList =
CXXBaseListFactory.add(BaseSpec, ReducedBaseSpecList);
}
return getPointerToMemberData(ND, ReducedBaseSpecList);
}
// FIXME: Reinterpret casts on member-pointers are not handled properly by
// this code
for (const CXXBaseSpecifier *I : llvm::reverse(PathRange))
BaseSpecList = prependCXXBase(I, BaseSpecList);
return getPointerToMemberData(ND, BaseSpecList);
}
const llvm::APSInt*
BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
const llvm::APSInt& V1, const llvm::APSInt& V2) {
switch (Op) {
default:
llvm_unreachable("Invalid Opcode.");
case BO_Mul:
return &getValue( V1 * V2 );
case BO_Div:
if (V2 == 0) // Avoid division by zero
return nullptr;
return &getValue( V1 / V2 );
case BO_Rem:
if (V2 == 0) // Avoid division by zero
return nullptr;
return &getValue( V1 % V2 );
case BO_Add:
return &getValue( V1 + V2 );
case BO_Sub:
return &getValue( V1 - V2 );
case BO_Shl: {
// FIXME: This logic should probably go higher up, where we can
// test these conditions symbolically.
if (V2.isSigned() && V2.isNegative())
return nullptr;
uint64_t Amt = V2.getZExtValue();
if (Amt >= V1.getBitWidth())
return nullptr;
if (!Ctx.getLangOpts().CPlusPlus20) {
if (V1.isSigned() && V1.isNegative())
return nullptr;
if (V1.isSigned() && Amt > V1.countLeadingZeros())
return nullptr;
}
return &getValue( V1.operator<<( (unsigned) Amt ));
}
case BO_Shr: {
// FIXME: This logic should probably go higher up, where we can
// test these conditions symbolically.
if (V2.isSigned() && V2.isNegative())
return nullptr;
uint64_t Amt = V2.getZExtValue();
if (Amt >= V1.getBitWidth())
return nullptr;
return &getValue( V1.operator>>( (unsigned) Amt ));
}
case BO_LT:
return &getTruthValue( V1 < V2 );
case BO_GT:
return &getTruthValue( V1 > V2 );
case BO_LE:
return &getTruthValue( V1 <= V2 );
case BO_GE:
return &getTruthValue( V1 >= V2 );
case BO_EQ:
return &getTruthValue( V1 == V2 );
case BO_NE:
return &getTruthValue( V1 != V2 );
// Note: LAnd, LOr, Comma are handled specially by higher-level logic.
case BO_And:
return &getValue( V1 & V2 );
case BO_Or:
return &getValue( V1 | V2 );
case BO_Xor:
return &getValue( V1 ^ V2 );
}
}
const std::pair<SVal, uintptr_t>&
BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
// Lazily create the folding set.
if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
llvm::FoldingSetNodeID ID;
void *InsertPos;
V.Profile(ID);
ID.AddPointer((void*) Data);
PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValData>;
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
if (!P) {
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
new (P) FoldNodeTy(std::make_pair(V, Data));
Map.InsertNode(P, InsertPos);
}
return P->getValue();
}
const std::pair<SVal, SVal>&
BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
// Lazily create the folding set.
if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
llvm::FoldingSetNodeID ID;
void *InsertPos;
V1.Profile(ID);
V2.Profile(ID);
PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValPair>;
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
if (!P) {
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
new (P) FoldNodeTy(std::make_pair(V1, V2));
Map.InsertNode(P, InsertPos);
}
return P->getValue();
}
const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
return &getPersistentSValWithData(X, 0).first;
}
|