clang 19.0.0git
DataflowEnvironment.h
Go to the documentation of this file.
1//===-- DataflowEnvironment.h -----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an Environment class that is used by dataflow analyses
10// that run over Control-Flow Graphs (CFGs) to keep track of the state of the
11// program at given program points.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_DATAFLOWENVIRONMENT_H
16#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_DATAFLOWENVIRONMENT_H
17
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclBase.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/Type.h"
28#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/DenseSet.h"
30#include "llvm/ADT/MapVector.h"
31#include "llvm/Support/Compiler.h"
32#include "llvm/Support/ErrorHandling.h"
33#include <memory>
34#include <type_traits>
35#include <utility>
36
37namespace clang {
38namespace dataflow {
39
40/// Indicates the result of a tentative comparison.
41enum class ComparisonResult {
42 Same,
44 Unknown,
45};
46
47/// The result of a `widen` operation.
49 /// Non-null pointer to a potentially widened version of the input value.
51 /// Whether `V` represents a "change" (that is, a different value) with
52 /// respect to the previous value in the sequence.
54};
55
56/// Holds the state of the program (store and heap) at a given program point.
57///
58/// WARNING: Symbolic values that are created by the environment for static
59/// local and global variables are not currently invalidated on function calls.
60/// This is unsound and should be taken into account when designing dataflow
61/// analyses.
63public:
64 /// Supplements `Environment` with non-standard comparison and join
65 /// operations.
66 class ValueModel {
67 public:
68 virtual ~ValueModel() = default;
69
70 /// Returns:
71 /// `Same`: `Val1` is equivalent to `Val2`, according to the model.
72 /// `Different`: `Val1` is distinct from `Val2`, according to the model.
73 /// `Unknown`: The model can't determine a relationship between `Val1` and
74 /// `Val2`.
75 ///
76 /// Requirements:
77 ///
78 /// `Val1` and `Val2` must be distinct.
79 ///
80 /// `Val1` and `Val2` must model values of type `Type`.
81 ///
82 /// `Val1` and `Val2` must be assigned to the same storage location in
83 /// `Env1` and `Env2` respectively.
85 const Environment &Env1, const Value &Val2,
86 const Environment &Env2) {
87 // FIXME: Consider adding QualType to RecordValue and removing the Type
88 // argument here.
90 }
91
92 /// Modifies `JoinedVal` to approximate both `Val1` and `Val2`. This should
93 /// obey the properties of a lattice join.
94 ///
95 /// `Env1` and `Env2` can be used to query child values and path condition
96 /// implications of `Val1` and `Val2` respectively.
97 ///
98 /// Requirements:
99 ///
100 /// `Val1` and `Val2` must be distinct.
101 ///
102 /// `Val1`, `Val2`, and `JoinedVal` must model values of type `Type`.
103 ///
104 /// `Val1` and `Val2` must be assigned to the same storage location in
105 /// `Env1` and `Env2` respectively.
106 virtual void join(QualType Type, const Value &Val1, const Environment &Env1,
107 const Value &Val2, const Environment &Env2,
108 Value &JoinedVal, Environment &JoinedEnv) {}
109
110 /// This function may widen the current value -- replace it with an
111 /// approximation that can reach a fixed point more quickly than iterated
112 /// application of the transfer function alone. The previous value is
113 /// provided to inform the choice of widened value. The function must also
114 /// serve as a comparison operation, by indicating whether the widened value
115 /// is equivalent to the previous value.
116 ///
117 /// Returns one of the folowing:
118 /// * `std::nullopt`, if this value is not of interest to the
119 /// model.
120 /// * A `WidenResult` with:
121 /// * A non-null `Value *` that points either to `Current` or a widened
122 /// version of `Current`. This value must be consistent with
123 /// the flow condition of `CurrentEnv`. We particularly caution
124 /// against using `Prev`, which is rarely consistent.
125 /// * A `LatticeEffect` indicating whether the value should be
126 /// considered a new value (`Changed`) or one *equivalent* (if not
127 /// necessarily equal) to `Prev` (`Unchanged`).
128 ///
129 /// `PrevEnv` and `CurrentEnv` can be used to query child values and path
130 /// condition implications of `Prev` and `Current`, respectively.
131 ///
132 /// Requirements:
133 ///
134 /// `Prev` and `Current` must model values of type `Type`.
135 ///
136 /// `Prev` and `Current` must be assigned to the same storage location in
137 /// `PrevEnv` and `CurrentEnv`, respectively.
138 virtual std::optional<WidenResult> widen(QualType Type, Value &Prev,
139 const Environment &PrevEnv,
140 Value &Current,
141 Environment &CurrentEnv) {
142 // The default implementation reduces to just comparison, since comparison
143 // is required by the API, even if no widening is performed.
144 switch (compare(Type, Prev, PrevEnv, Current, CurrentEnv)) {
146 return std::nullopt;
148 return WidenResult{&Current, LatticeEffect::Unchanged};
150 return WidenResult{&Current, LatticeEffect::Changed};
151 }
152 llvm_unreachable("all cases in switch covered");
153 }
154 };
155
156 /// Creates an environment that uses `DACtx` to store objects that encompass
157 /// the state of a program.
158 explicit Environment(DataflowAnalysisContext &DACtx);
159
160 // Copy-constructor is private, Environments should not be copied. See fork().
162
165
166 /// Creates an environment that uses `DACtx` to store objects that encompass
167 /// the state of a program.
168 ///
169 /// If `DeclCtx` is a function, initializes the environment with symbolic
170 /// representations of the function parameters.
171 ///
172 /// If `DeclCtx` is a non-static member function, initializes the environment
173 /// with a symbolic representation of the `this` pointee.
174 Environment(DataflowAnalysisContext &DACtx, const DeclContext &DeclCtx);
175
176 /// Assigns storage locations and values to all parameters, captures, global
177 /// variables, fields and functions referenced in the function currently being
178 /// analyzed.
179 ///
180 /// Requirements:
181 ///
182 /// The function must have a body, i.e.
183 /// `FunctionDecl::doesThisDecalarationHaveABody()` must be true.
184 void initialize();
185
186 /// Returns a new environment that is a copy of this one.
187 ///
188 /// The state of the program is initially the same, but can be mutated without
189 /// affecting the original.
190 ///
191 /// However the original should not be further mutated, as this may interfere
192 /// with the fork. (In practice, values are stored independently, but the
193 /// forked flow condition references the original).
194 Environment fork() const;
195
196 /// Creates and returns an environment to use for an inline analysis of the
197 /// callee. Uses the storage location from each argument in the `Call` as the
198 /// storage location for the corresponding parameter in the callee.
199 ///
200 /// Requirements:
201 ///
202 /// The callee of `Call` must be a `FunctionDecl`.
203 ///
204 /// The body of the callee must not reference globals.
205 ///
206 /// The arguments of `Call` must map 1:1 to the callee's parameters.
207 Environment pushCall(const CallExpr *Call) const;
209
210 /// Moves gathered information back into `this` from a `CalleeEnv` created via
211 /// `pushCall`.
212 void popCall(const CallExpr *Call, const Environment &CalleeEnv);
213 void popCall(const CXXConstructExpr *Call, const Environment &CalleeEnv);
214
215 /// Returns true if and only if the environment is equivalent to `Other`, i.e
216 /// the two environments:
217 /// - have the same mappings from declarations to storage locations,
218 /// - have the same mappings from expressions to storage locations,
219 /// - have the same or equivalent (according to `Model`) values assigned to
220 /// the same storage locations.
221 ///
222 /// Requirements:
223 ///
224 /// `Other` and `this` must use the same `DataflowAnalysisContext`.
225 bool equivalentTo(const Environment &Other,
226 Environment::ValueModel &Model) const;
227
228 /// How to treat expression state (`ExprToLoc` and `ExprToVal`) in a join.
229 /// If the join happens within a full expression, expression state should be
230 /// kept; otherwise, we can discard it.
234 };
235
236 /// Joins two environments by taking the intersection of storage locations and
237 /// values that are stored in them. Distinct values that are assigned to the
238 /// same storage locations in `EnvA` and `EnvB` are merged using `Model`.
239 ///
240 /// Requirements:
241 ///
242 /// `EnvA` and `EnvB` must use the same `DataflowAnalysisContext`.
243 static Environment join(const Environment &EnvA, const Environment &EnvB,
245 ExprJoinBehavior ExprBehavior);
246
247 /// Widens the environment point-wise, using `PrevEnv` as needed to inform the
248 /// approximation.
249 ///
250 /// Requirements:
251 ///
252 /// `PrevEnv` must be the immediate previous version of the environment.
253 /// `PrevEnv` and `this` must use the same `DataflowAnalysisContext`.
254 LatticeEffect widen(const Environment &PrevEnv,
256
257 // FIXME: Rename `createOrGetStorageLocation` to `getOrCreateStorageLocation`,
258 // `getStableStorageLocation`, or something more appropriate.
259
260 /// Creates a storage location appropriate for `Type`. Does not assign a value
261 /// to the returned storage location in the environment.
262 ///
263 /// Requirements:
264 ///
265 /// `Type` must not be null.
267
268 /// Creates a storage location for `D`. Does not assign the returned storage
269 /// location to `D` in the environment. Does not assign a value to the
270 /// returned storage location in the environment.
272
273 /// Creates a storage location for `E`. Does not assign the returned storage
274 /// location to `E` in the environment. Does not assign a value to the
275 /// returned storage location in the environment.
277
278 /// Assigns `Loc` as the storage location of `D` in the environment.
279 ///
280 /// Requirements:
281 ///
282 /// `D` must not already have a storage location in the environment.
283 void setStorageLocation(const ValueDecl &D, StorageLocation &Loc);
284
285 /// Returns the storage location assigned to `D` in the environment, or null
286 /// if `D` isn't assigned a storage location in the environment.
288
289 /// Removes the location assigned to `D` in the environment (if any).
290 void removeDecl(const ValueDecl &D);
291
292 /// Assigns `Loc` as the storage location of the glvalue `E` in the
293 /// environment.
294 ///
295 /// Requirements:
296 ///
297 /// `E` must not be assigned a storage location in the environment.
298 /// `E` must be a glvalue or a `BuiltinType::BuiltinFn`
299 void setStorageLocation(const Expr &E, StorageLocation &Loc);
300
301 /// Returns the storage location assigned to the glvalue `E` in the
302 /// environment, or null if `E` isn't assigned a storage location in the
303 /// environment.
304 ///
305 /// Requirements:
306 /// `E` must be a glvalue or a `BuiltinType::BuiltinFn`
307 StorageLocation *getStorageLocation(const Expr &E) const;
308
309 /// Returns the result of casting `getStorageLocation(...)` to a subclass of
310 /// `StorageLocation` (using `cast_or_null<T>`).
311 /// This assert-fails if the result of `getStorageLocation(...)` is not of
312 /// type `T *`; if the storage location is not guaranteed to have type `T *`,
313 /// consider using `dyn_cast_or_null<T>(getStorageLocation(...))` instead.
314 template <typename T>
315 std::enable_if_t<std::is_base_of_v<StorageLocation, T>, T *>
316 get(const ValueDecl &D) const {
317 return cast_or_null<T>(getStorageLocation(D));
318 }
319 template <typename T>
320 std::enable_if_t<std::is_base_of_v<StorageLocation, T>, T *>
321 get(const Expr &E) const {
322 return cast_or_null<T>(getStorageLocation(E));
323 }
324
325 /// Returns the storage location assigned to the `this` pointee in the
326 /// environment or null if the `this` pointee has no assigned storage location
327 /// in the environment.
329 return ThisPointeeLoc;
330 }
331
332 /// Sets the storage location assigned to the `this` pointee in the
333 /// environment.
335 ThisPointeeLoc = &Loc;
336 }
337
338 /// Returns the location of the result object for a record-type prvalue.
339 ///
340 /// In C++, prvalues of record type serve only a limited purpose: They can
341 /// only be used to initialize a result object (e.g. a variable or a
342 /// temporary). This function returns the location of that result object.
343 ///
344 /// When creating a prvalue of record type, we already need the storage
345 /// location of the result object to pass in `this`, even though prvalues are
346 /// otherwise not associated with storage locations.
347 ///
348 /// Requirements:
349 /// `E` must be a prvalue of record type.
351 getResultObjectLocation(const Expr &RecordPRValue) const;
352
353 /// Returns the return value of the current function. This can be null if:
354 /// - The function has a void return type
355 /// - No return value could be determined for the function, for example
356 /// because it calls a function without a body.
357 ///
358 /// Requirements:
359 /// The current function must have a non-reference return type.
361 assert(getCurrentFunc() != nullptr &&
362 !getCurrentFunc()->getReturnType()->isReferenceType());
363 return ReturnVal;
364 }
365
366 /// Returns the storage location for the reference returned by the current
367 /// function. This can be null if function doesn't return a single consistent
368 /// reference.
369 ///
370 /// Requirements:
371 /// The current function must have a reference return type.
373 assert(getCurrentFunc() != nullptr &&
374 getCurrentFunc()->getReturnType()->isReferenceType());
375 return ReturnLoc;
376 }
377
378 /// Sets the return value of the current function.
379 ///
380 /// Requirements:
381 /// The current function must have a non-reference return type.
383 assert(getCurrentFunc() != nullptr &&
384 !getCurrentFunc()->getReturnType()->isReferenceType());
385 ReturnVal = Val;
386 }
387
388 /// Sets the storage location for the reference returned by the current
389 /// function.
390 ///
391 /// Requirements:
392 /// The current function must have a reference return type.
394 assert(getCurrentFunc() != nullptr &&
395 getCurrentFunc()->getReturnType()->isReferenceType());
396 ReturnLoc = Loc;
397 }
398
399 /// Returns a pointer value that represents a null pointer. Calls with
400 /// `PointeeType` that are canonically equivalent will return the same result.
402
403 /// Creates a value appropriate for `Type`, if `Type` is supported, otherwise
404 /// returns null.
405 ///
406 /// If `Type` is a pointer or reference type, creates all the necessary
407 /// storage locations and values for indirections until it finds a
408 /// non-pointer/non-reference type.
409 ///
410 /// If `Type` is a class, struct, or union type, creates values for all
411 /// modeled fields (including synthetic fields) and calls `setValue()` to
412 /// associate the `RecordValue` with its storage location
413 /// (`RecordValue::getLoc()`).
414 ///
415 /// If `Type` is one of the following types, this function will always return
416 /// a non-null pointer:
417 /// - `bool`
418 /// - Any integer type
419 /// - Any class, struct, or union type
420 ///
421 /// Requirements:
422 ///
423 /// `Type` must not be null.
425
426 /// Creates an object (i.e. a storage location with an associated value) of
427 /// type `Ty`. If `InitExpr` is non-null and has a value associated with it,
428 /// initializes the object with this value. Otherwise, initializes the object
429 /// with a value created using `createValue()`.
430 StorageLocation &createObject(QualType Ty, const Expr *InitExpr = nullptr) {
431 return createObjectInternal(nullptr, Ty, InitExpr);
432 }
433
434 /// Creates an object for the variable declaration `D`. If `D` has an
435 /// initializer and this initializer is associated with a value, initializes
436 /// the object with this value. Otherwise, initializes the object with a
437 /// value created using `createValue()`. Uses the storage location returned by
438 /// `DataflowAnalysisContext::getStableStorageLocation(D)`.
440 return createObjectInternal(&D, D.getType(), D.getInit());
441 }
442
443 /// Creates an object for the variable declaration `D`. If `InitExpr` is
444 /// non-null and has a value associated with it, initializes the object with
445 /// this value. Otherwise, initializes the object with a value created using
446 /// `createValue()`. Uses the storage location returned by
447 /// `DataflowAnalysisContext::getStableStorageLocation(D)`.
448 StorageLocation &createObject(const ValueDecl &D, const Expr *InitExpr) {
449 return createObjectInternal(&D, D.getType(), InitExpr);
450 }
451
452 /// Initializes the fields (including synthetic fields) of `Loc` with values,
453 /// unless values of the field type are not supported or we hit one of the
454 /// limits at which we stop producing values.
455 /// If `Type` is provided, initializes only those fields that are modeled for
456 /// `Type`; this is intended for use in cases where `Loc` is a derived type
457 /// and we only want to initialize the fields of a base type.
461 }
462
463 /// Assigns `Val` as the value of `Loc` in the environment.
464 void setValue(const StorageLocation &Loc, Value &Val);
465
466 /// Clears any association between `Loc` and a value in the environment.
467 void clearValue(const StorageLocation &Loc) { LocToVal.erase(&Loc); }
468
469 /// Assigns `Val` as the value of the prvalue `E` in the environment.
470 ///
471 /// Requirements:
472 ///
473 /// - `E` must be a prvalue
474 /// - If `Val` is a `RecordValue`, its `RecordStorageLocation` must be
475 /// `getResultObjectLocation(E)`. An exception to this is if `E` is an
476 /// expression that originally creates a `RecordValue` (such as a
477 /// `CXXConstructExpr` or `CallExpr`), as these establish the location of
478 /// the result object in the first place.
479 void setValue(const Expr &E, Value &Val);
480
481 /// Returns the value assigned to `Loc` in the environment or null if `Loc`
482 /// isn't assigned a value in the environment.
483 Value *getValue(const StorageLocation &Loc) const;
484
485 /// Equivalent to `getValue(getStorageLocation(D))` if `D` is assigned a
486 /// storage location in the environment, otherwise returns null.
487 Value *getValue(const ValueDecl &D) const;
488
489 /// Equivalent to `getValue(getStorageLocation(E, SP))` if `E` is assigned a
490 /// storage location in the environment, otherwise returns null.
491 Value *getValue(const Expr &E) const;
492
493 /// Returns the result of casting `getValue(...)` to a subclass of `Value`
494 /// (using `cast_or_null<T>`).
495 /// This assert-fails if the result of `getValue(...)` is not of type `T *`;
496 /// if the value is not guaranteed to have type `T *`, consider using
497 /// `dyn_cast_or_null<T>(getValue(...))` instead.
498 template <typename T>
499 std::enable_if_t<std::is_base_of_v<Value, T>, T *>
500 get(const StorageLocation &Loc) const {
501 return cast_or_null<T>(getValue(Loc));
502 }
503 template <typename T>
504 std::enable_if_t<std::is_base_of_v<Value, T>, T *>
505 get(const ValueDecl &D) const {
506 return cast_or_null<T>(getValue(D));
507 }
508 template <typename T>
509 std::enable_if_t<std::is_base_of_v<Value, T>, T *> get(const Expr &E) const {
510 return cast_or_null<T>(getValue(E));
511 }
512
513 // FIXME: should we deprecate the following & call arena().create() directly?
514
515 /// Creates a `T` (some subclass of `Value`), forwarding `args` to the
516 /// constructor, and returns a reference to it.
517 ///
518 /// The analysis context takes ownership of the created object. The object
519 /// will be destroyed when the analysis context is destroyed.
520 template <typename T, typename... Args>
521 std::enable_if_t<std::is_base_of<Value, T>::value, T &>
522 create(Args &&...args) {
523 return arena().create<T>(std::forward<Args>(args)...);
524 }
525
526 /// Returns a symbolic integer value that models an integer literal equal to
527 /// `Value`
529 return arena().makeIntLiteral(Value);
530 }
531
532 /// Returns a symbolic boolean value that models a boolean literal equal to
533 /// `Value`
535 return arena().makeBoolValue(arena().makeLiteral(Value));
536 }
537
538 /// Returns an atomic boolean value.
540 return arena().makeAtomValue();
541 }
542
543 /// Returns a unique instance of boolean Top.
545 return arena().makeTopValue();
546 }
547
548 /// Returns a boolean value that represents the conjunction of `LHS` and
549 /// `RHS`. Subsequent calls with the same arguments, regardless of their
550 /// order, will return the same result. If the given boolean values represent
551 /// the same value, the result will be the value itself.
553 return arena().makeBoolValue(
554 arena().makeAnd(LHS.formula(), RHS.formula()));
555 }
556
557 /// Returns a boolean value that represents the disjunction of `LHS` and
558 /// `RHS`. Subsequent calls with the same arguments, regardless of their
559 /// order, will return the same result. If the given boolean values represent
560 /// the same value, the result will be the value itself.
561 BoolValue &makeOr(BoolValue &LHS, BoolValue &RHS) const {
562 return arena().makeBoolValue(
563 arena().makeOr(LHS.formula(), RHS.formula()));
564 }
565
566 /// Returns a boolean value that represents the negation of `Val`. Subsequent
567 /// calls with the same argument will return the same result.
569 return arena().makeBoolValue(arena().makeNot(Val.formula()));
570 }
571
572 /// Returns a boolean value represents `LHS` => `RHS`. Subsequent calls with
573 /// the same arguments, will return the same result. If the given boolean
574 /// values represent the same value, the result will be a value that
575 /// represents the true boolean literal.
577 return arena().makeBoolValue(
578 arena().makeImplies(LHS.formula(), RHS.formula()));
579 }
580
581 /// Returns a boolean value represents `LHS` <=> `RHS`. Subsequent calls with
582 /// the same arguments, regardless of their order, will return the same
583 /// result. If the given boolean values represent the same value, the result
584 /// will be a value that represents the true boolean literal.
586 return arena().makeBoolValue(
587 arena().makeEquals(LHS.formula(), RHS.formula()));
588 }
589
590 /// Returns a boolean variable that identifies the flow condition (FC).
591 ///
592 /// The flow condition is a set of facts that are necessarily true when the
593 /// program reaches the current point, expressed as boolean formulas.
594 /// The flow condition token is equivalent to the AND of these facts.
595 ///
596 /// These may e.g. constrain the value of certain variables. A pointer
597 /// variable may have a consistent modeled PointerValue throughout, but at a
598 /// given point the Environment may tell us that the value must be non-null.
599 ///
600 /// The FC is necessary but not sufficient for this point to be reachable.
601 /// In particular, where the FC token appears in flow conditions of successor
602 /// environments, it means "point X may have been reached", not
603 /// "point X was reached".
604 Atom getFlowConditionToken() const { return FlowConditionToken; }
605
606 /// Record a fact that must be true if this point in the program is reached.
607 void assume(const Formula &);
608
609 /// Returns true if the formula is always true when this point is reached.
610 /// Returns false if the formula may be false (or the flow condition isn't
611 /// sufficiently precise to prove that it is true) or if the solver times out.
612 ///
613 /// Note that there is an asymmetry between this function and `allows()` in
614 /// that they both return false if the solver times out. The assumption is
615 /// that if `proves()` or `allows()` returns true, this will result in a
616 /// diagnostic, and we want to bias towards false negatives in the case where
617 /// the solver times out.
618 bool proves(const Formula &) const;
619
620 /// Returns true if the formula may be true when this point is reached.
621 /// Returns false if the formula is always false when this point is reached
622 /// (or the flow condition is overly constraining) or if the solver times out.
623 bool allows(const Formula &) const;
624
625 /// Returns the `DeclContext` of the block being analysed, if any. Otherwise,
626 /// returns null.
627 const DeclContext *getDeclCtx() const { return CallStack.back(); }
628
629 /// Returns the function currently being analyzed, or null if the code being
630 /// analyzed isn't part of a function.
632 return dyn_cast<FunctionDecl>(getDeclCtx());
633 }
634
635 /// Returns the size of the call stack.
636 size_t callStackSize() const { return CallStack.size(); }
637
638 /// Returns whether this `Environment` can be extended to analyze the given
639 /// `Callee` (i.e. if `pushCall` can be used), with recursion disallowed and a
640 /// given `MaxDepth`.
641 bool canDescend(unsigned MaxDepth, const DeclContext *Callee) const;
642
643 /// Returns the `DataflowAnalysisContext` used by the environment.
645
646 Arena &arena() const { return DACtx->arena(); }
647
648 LLVM_DUMP_METHOD void dump() const;
649 LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
650
651private:
652 using PrValueToResultObject =
653 llvm::DenseMap<const Expr *, RecordStorageLocation *>;
654
655 // The copy-constructor is for use in fork() only.
656 Environment(const Environment &) = default;
657
658 /// Creates a value appropriate for `Type`, if `Type` is supported, otherwise
659 /// return null.
660 ///
661 /// Recursively initializes storage locations and values until it sees a
662 /// self-referential pointer or reference type. `Visited` is used to track
663 /// which types appeared in the reference/pointer chain in order to avoid
664 /// creating a cyclic dependency with self-referential pointers/references.
665 ///
666 /// Requirements:
667 ///
668 /// `Type` must not be null.
669 Value *createValueUnlessSelfReferential(QualType Type,
671 int Depth, int &CreatedValuesCount);
672
673 /// Creates a storage location for `Ty`. Also creates and associates a value
674 /// with the storage location, unless values of this type are not supported or
675 /// we hit one of the limits at which we stop producing values (controlled by
676 /// `Visited`, `Depth`, and `CreatedValuesCount`).
677 StorageLocation &createLocAndMaybeValue(QualType Ty,
679 int Depth, int &CreatedValuesCount);
680
681 /// Initializes the fields (including synthetic fields) of `Loc` with values,
682 /// unless values of the field type are not supported or we hit one of the
683 /// limits at which we stop producing values (controlled by `Visited`,
684 /// `Depth`, and `CreatedValuesCount`). If `Type` is different from
685 /// `Loc.getType()`, initializes only those fields that are modeled for
686 /// `Type`.
689 int &CreatedValuesCount);
690
691 /// Shared implementation of `createObject()` overloads.
692 /// `D` and `InitExpr` may be null.
693 StorageLocation &createObjectInternal(const ValueDecl *D, QualType Ty,
694 const Expr *InitExpr);
695
696 /// Shared implementation of `pushCall` overloads. Note that unlike
697 /// `pushCall`, this member is invoked on the environment of the callee, not
698 /// of the caller.
699 void pushCallInternal(const FunctionDecl *FuncDecl,
701
702 /// Assigns storage locations and values to all global variables, fields
703 /// and functions referenced in `FuncDecl`. `FuncDecl` must have a body.
704 void initFieldsGlobalsAndFuncs(const FunctionDecl *FuncDecl);
705
706 static PrValueToResultObject
707 buildResultObjectMap(DataflowAnalysisContext *DACtx,
708 const FunctionDecl *FuncDecl,
709 RecordStorageLocation *ThisPointeeLoc,
710 RecordStorageLocation *LocForRecordReturnVal);
711
712 // `DACtx` is not null and not owned by this object.
714
715 // FIXME: move the fields `CallStack`, `ResultObjectMap`, `ReturnVal`,
716 // `ReturnLoc` and `ThisPointeeLoc` into a separate call-context object,
717 // shared between environments in the same call.
718 // https://github.com/llvm/llvm-project/issues/59005
719
720 // `DeclContext` of the block being analysed if provided.
721 std::vector<const DeclContext *> CallStack;
722
723 // Maps from prvalues of record type to their result objects. Shared between
724 // all environments for the same function.
725 // FIXME: It's somewhat unsatisfactory that we have to use a `shared_ptr`
726 // here, though the cost is acceptable: The overhead of a `shared_ptr` is
727 // incurred when it is copied, and this happens only relatively rarely (when
728 // we fork the environment). The need for a `shared_ptr` will go away once we
729 // introduce a shared call-context object (see above).
730 std::shared_ptr<PrValueToResultObject> ResultObjectMap;
731
732 // The following three member variables handle various different types of
733 // return values.
734 // - If the return type is not a reference and not a record: Value returned
735 // by the function.
736 Value *ReturnVal = nullptr;
737 // - If the return type is a reference: Storage location of the reference
738 // returned by the function.
739 StorageLocation *ReturnLoc = nullptr;
740 // - If the return type is a record or the function being analyzed is a
741 // constructor: Storage location into which the return value should be
742 // constructed.
743 RecordStorageLocation *LocForRecordReturnVal = nullptr;
744
745 // The storage location of the `this` pointee. Should only be null if the
746 // function being analyzed is only a function and not a method.
747 RecordStorageLocation *ThisPointeeLoc = nullptr;
748
749 // Maps from declarations and glvalue expression to storage locations that are
750 // assigned to them. Unlike the maps in `DataflowAnalysisContext`, these
751 // include only storage locations that are in scope for a particular basic
752 // block.
753 llvm::DenseMap<const ValueDecl *, StorageLocation *> DeclToLoc;
754 llvm::DenseMap<const Expr *, StorageLocation *> ExprToLoc;
755 // Maps from prvalue expressions and storage locations to the values that
756 // are assigned to them.
757 // We preserve insertion order so that join/widen process values in
758 // deterministic sequence. This in turn produces deterministic SAT formulas.
759 llvm::MapVector<const Expr *, Value *> ExprToVal;
760 llvm::MapVector<const StorageLocation *, Value *> LocToVal;
761
762 Atom FlowConditionToken;
763};
764
765/// Returns the storage location for the implicit object of a
766/// `CXXMemberCallExpr`, or null if none is defined in the environment.
767/// Dereferences the pointer if the member call expression was written using
768/// `->`.
769RecordStorageLocation *getImplicitObjectLocation(const CXXMemberCallExpr &MCE,
770 const Environment &Env);
771
772/// Returns the storage location for the base object of a `MemberExpr`, or null
773/// if none is defined in the environment. Dereferences the pointer if the
774/// member expression was written using `->`.
775RecordStorageLocation *getBaseObjectLocation(const MemberExpr &ME,
776 const Environment &Env);
777
778/// Associates a new `RecordValue` with `Loc` and returns the new value.
779RecordValue &refreshRecordValue(RecordStorageLocation &Loc, Environment &Env);
780
781/// Associates a new `RecordValue` with `Expr` and returns the new value.
782RecordValue &refreshRecordValue(const Expr &Expr, Environment &Env);
783
784} // namespace dataflow
785} // namespace clang
786
787#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_DATAFLOWENVIRONMENT_H
const Environment & Env
Definition: HTMLLogger.cpp:148
llvm::DenseSet< const void * > Visited
Definition: HTMLLogger.cpp:146
C Language Family Type Representation.
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1540
Represents a call to a member function that may be written either with member call syntax (e....
Definition: ExprCXX.h:176
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2820
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1971
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3172
A (possibly-)qualified type.
Definition: Type.h:738
The base class of the type hierarchy.
Definition: Type.h:1607
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:706
QualType getType() const
Definition: Decl.h:717
Represents a variable declaration or definition.
Definition: Decl.h:918
const Expr * getInit() const
Definition: Decl.h:1355
The Arena owns the objects that model data within an analysis.
Definition: Arena.h:21
IntegerValue & makeIntLiteral(llvm::APInt Value)
Returns a symbolic integer value that models an integer literal equal to Value.
Definition: Arena.cpp:104
TopBoolValue & makeTopValue()
Creates a fresh Top boolean value.
Definition: Arena.h:76
BoolValue & makeBoolValue(const Formula &)
Creates a BoolValue wrapping a particular formula.
Definition: Arena.cpp:112
AtomicBoolValue & makeAtomValue()
Creates a fresh atom and wraps in in an AtomicBoolValue.
Definition: Arena.h:71
std::enable_if_t< std::is_base_of< StorageLocation, T >::value, T & > create(Args &&...args)
Creates a T (some subclass of StorageLocation), forwarding args to the constructor,...
Definition: Arena.h:36
Models a boolean.
Definition: Value.h:96
const Formula & formula() const
Definition: Value.h:109
Owns objects that encompass the state of a program and stores context that is used during dataflow an...
Supplements Environment with non-standard comparison and join operations.
virtual std::optional< WidenResult > widen(QualType Type, Value &Prev, const Environment &PrevEnv, Value &Current, Environment &CurrentEnv)
This function may widen the current value – replace it with an approximation that can reach a fixed p...
virtual void join(QualType Type, const Value &Val1, const Environment &Env1, const Value &Val2, const Environment &Env2, Value &JoinedVal, Environment &JoinedEnv)
Modifies JoinedVal to approximate both Val1 and Val2.
virtual ComparisonResult compare(QualType Type, const Value &Val1, const Environment &Env1, const Value &Val2, const Environment &Env2)
Returns: Same: Val1 is equivalent to Val2, according to the model.
Holds the state of the program (store and heap) at a given program point.
bool allows(const Formula &) const
Returns true if the formula may be true when this point is reached.
void initializeFieldsWithValues(RecordStorageLocation &Loc)
LatticeEffect widen(const Environment &PrevEnv, Environment::ValueModel &Model)
Widens the environment point-wise, using PrevEnv as needed to inform the approximation.
PointerValue & getOrCreateNullPointerValue(QualType PointeeType)
Returns a pointer value that represents a null pointer.
BoolValue & makeAnd(BoolValue &LHS, BoolValue &RHS) const
Returns a boolean value that represents the conjunction of LHS and RHS.
std::enable_if_t< std::is_base_of_v< Value, T >, T * > get(const StorageLocation &Loc) const
Returns the result of casting getValue(...) to a subclass of Value (using cast_or_null<T>).
RecordStorageLocation * getThisPointeeStorageLocation() const
Returns the storage location assigned to the this pointee in the environment or null if the this poin...
BoolValue & makeIff(BoolValue &LHS, BoolValue &RHS) const
Returns a boolean value represents LHS <=> RHS.
Environment pushCall(const CallExpr *Call) const
Creates and returns an environment to use for an inline analysis of the callee.
void clearValue(const StorageLocation &Loc)
Clears any association between Loc and a value in the environment.
StorageLocation * getStorageLocation(const ValueDecl &D) const
Returns the storage location assigned to D in the environment, or null if D isn't assigned a storage ...
LLVM_DUMP_METHOD void dump() const
void setReturnValue(Value *Val)
Sets the return value of the current function.
Environment(Environment &&Other)=default
BoolValue & makeTopBoolValue() const
Returns a unique instance of boolean Top.
StorageLocation & createObject(const VarDecl &D)
Creates an object for the variable declaration D.
void initializeFieldsWithValues(RecordStorageLocation &Loc, QualType Type)
Initializes the fields (including synthetic fields) of Loc with values, unless values of the field ty...
StorageLocation & createStorageLocation(QualType Type)
Creates a storage location appropriate for Type.
Value * getReturnValue() const
Returns the return value of the current function.
Environment fork() const
Returns a new environment that is a copy of this one.
void popCall(const CallExpr *Call, const Environment &CalleeEnv)
Moves gathered information back into this from a CalleeEnv created via pushCall.
bool equivalentTo(const Environment &Other, Environment::ValueModel &Model) const
Returns true if and only if the environment is equivalent to Other, i.e the two environments:
BoolValue & makeAtomicBoolValue() const
Returns an atomic boolean value.
std::enable_if_t< std::is_base_of_v< Value, T >, T * > get(const ValueDecl &D) const
bool proves(const Formula &) const
Returns true if the formula is always true when this point is reached.
Value * getValue(const StorageLocation &Loc) const
Returns the value assigned to Loc in the environment or null if Loc isn't assigned a value in the env...
bool canDescend(unsigned MaxDepth, const DeclContext *Callee) const
Returns whether this Environment can be extended to analyze the given Callee (i.e.
Environment & operator=(const Environment &Other)=delete
const FunctionDecl * getCurrentFunc() const
Returns the function currently being analyzed, or null if the code being analyzed isn't part of a fun...
BoolValue & getBoolLiteralValue(bool Value) const
Returns a symbolic boolean value that models a boolean literal equal to Value
StorageLocation & createObject(QualType Ty, const Expr *InitExpr=nullptr)
Creates an object (i.e.
void assume(const Formula &)
Record a fact that must be true if this point in the program is reached.
DataflowAnalysisContext & getDataflowAnalysisContext() const
Returns the DataflowAnalysisContext used by the environment.
void setStorageLocation(const ValueDecl &D, StorageLocation &Loc)
Assigns Loc as the storage location of D in the environment.
void removeDecl(const ValueDecl &D)
Removes the location assigned to D in the environment (if any).
RecordStorageLocation & getResultObjectLocation(const Expr &RecordPRValue) const
Returns the location of the result object for a record-type prvalue.
std::enable_if_t< std::is_base_of_v< StorageLocation, T >, T * > get(const Expr &E) const
ExprJoinBehavior
How to treat expression state (ExprToLoc and ExprToVal) in a join.
static Environment join(const Environment &EnvA, const Environment &EnvB, Environment::ValueModel &Model, ExprJoinBehavior ExprBehavior)
Joins two environments by taking the intersection of storage locations and values that are stored in ...
Value * createValue(QualType Type)
Creates a value appropriate for Type, if Type is supported, otherwise returns null.
void setValue(const StorageLocation &Loc, Value &Val)
Assigns Val as the value of Loc in the environment.
IntegerValue & getIntLiteralValue(llvm::APInt Value) const
Returns a symbolic integer value that models an integer literal equal to Value
Environment & operator=(Environment &&Other)=default
void setThisPointeeStorageLocation(RecordStorageLocation &Loc)
Sets the storage location assigned to the this pointee in the environment.
Atom getFlowConditionToken() const
Returns a boolean variable that identifies the flow condition (FC).
StorageLocation & createObject(const ValueDecl &D, const Expr *InitExpr)
Creates an object for the variable declaration D.
BoolValue & makeNot(BoolValue &Val) const
Returns a boolean value that represents the negation of Val.
size_t callStackSize() const
Returns the size of the call stack.
const DeclContext * getDeclCtx() const
Returns the DeclContext of the block being analysed, if any.
std::enable_if_t< std::is_base_of_v< StorageLocation, T >, T * > get(const ValueDecl &D) const
Returns the result of casting getStorageLocation(...) to a subclass of StorageLocation (using cast_or...
void initialize()
Assigns storage locations and values to all parameters, captures, global variables,...
BoolValue & makeOr(BoolValue &LHS, BoolValue &RHS) const
Returns a boolean value that represents the disjunction of LHS and RHS.
std::enable_if_t< std::is_base_of< Value, T >::value, T & > create(Args &&...args)
Creates a T (some subclass of Value), forwarding args to the constructor, and returns a reference to ...
void setReturnStorageLocation(StorageLocation *Loc)
Sets the storage location for the reference returned by the current function.
StorageLocation * getReturnStorageLocation() const
Returns the storage location for the reference returned by the current function.
std::enable_if_t< std::is_base_of_v< Value, T >, T * > get(const Expr &E) const
BoolValue & makeImplication(BoolValue &LHS, BoolValue &RHS) const
Returns a boolean value represents LHS => RHS.
Models an integer.
Definition: Value.h:162
Models a symbolic pointer. Specifically, any value of type T*.
Definition: Value.h:172
A storage location for a record (struct, class, or union).
Base class for elements of the local variable store and of the heap.
Base class for all values computed by abstract interpretation.
Definition: Value.h:33
Atom
Identifies an atomic boolean variable such as "V1".
Definition: Formula.h:34
ComparisonResult
Indicates the result of a tentative comparison.
RecordStorageLocation * getImplicitObjectLocation(const CXXMemberCallExpr &MCE, const Environment &Env)
Returns the storage location for the implicit object of a CXXMemberCallExpr, or null if none is defin...
RecordStorageLocation * getBaseObjectLocation(const MemberExpr &ME, const Environment &Env)
Returns the storage location for the base object of a MemberExpr, or null if none is defined in the e...
LatticeEffect
Effect indicating whether a lattice operation resulted in a new value.
RecordValue & refreshRecordValue(RecordStorageLocation &Loc, Environment &Env)
Associates a new RecordValue with Loc and returns the new value.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
@ Other
Other implicit parameter.
The result of a widen operation.
LatticeEffect Effect
Whether V represents a "change" (that is, a different value) with respect to the previous value in th...
Value * V
Non-null pointer to a potentially widened version of the input value.