1010import semmle.code.cpp.Function
1111import semmle.code.cpp.models.Models
1212
13- module AliasModel {
14- private newtype TParameterEscape =
15- TDoesNotEscape ( ) or
16- TEscapesOnlyViaReturn ( ) or
17- TEscapes ( )
18-
19- class ParameterEscape extends TParameterEscape {
20- string toString ( ) {
21- result = "Unknown"
22- }
23- }
24-
25- class DoesNotEscape extends ParameterEscape , TDoesNotEscape {
26- override string toString ( ) {
27- result = "DoesNotEscape"
28- }
29- }
30-
31- class EscapesOnlyViaReturn extends ParameterEscape , TEscapesOnlyViaReturn {
32- override string toString ( ) {
33- result = "EscapesOnlyViaReturn"
34- }
35- }
36-
37- class Escapes extends ParameterEscape , TEscapes {
38- override string toString ( ) {
39- result = "Escapes"
40- }
41- }
42-
13+ /**
14+ * Models the aliasing behavior of a library function.
15+ */
16+ abstract class AliasFunction extends Function {
4317 /**
44- * Models the aliasing behavior of a library function.
45- */
46- abstract class AliasFunction extends Function {
47- /**
48- * Specifies whether the address passed to the parameter at the specified index is retained after
49- * the function returns. The result is given as a `ParameterEscape` object. See the comments for
50- * that class and its subclasses for a description of each possible result.
51- *
52- * Example:
53- * ```
54- * int* g;
55- * int* func(int* p, int* q, int* r, int* s, int n) {
56- * *s = 1; // `s` does not escape.
57- * g = p; // Stored in global. `p` escapes.
58- * if (rand()) {
59- * return q; // `q` escapes via the return value.
60- * }
61- * else {
62- * return r + n; // `r` escapes via the return value, even though an offset has been added.
63- * }
64- * }
65- * ```
66- *
67- * For the above function, the following terms hold:
68- * - `getParameterEscapeBehavior(0) instanceof Escapes`
69- * - `getParameterEscapeBehavior(1) instanceof EscapesOnlyViaReturn`
70- * - `getParameterEscapeBehavior(2) instanceof EscapesOnlyViaReturn`
71- * - `getParameterEscapeBehavior(3) instanceof DoesNotEscape`
72- */
73- abstract ParameterEscape getParameterEscapeBehavior ( int index ) ;
74-
75- /**
76- * Holds if the function always returns the value of the parameter at the specified index.
77- */
78- abstract predicate parameterIsAlwaysReturned ( int index ) ;
79- }
18+ * Holds if the address passed to the parameter at the specified index is never retained after
19+ * the function returns.
20+ *
21+ * Example:
22+ * ```
23+ * int* g;
24+ * int* func(int* p, int* q, int* r, int* s, int n) {
25+ * *s = 1; // `s` does not escape.
26+ * g = p; // Stored in global. `p` escapes.
27+ * if (rand()) {
28+ * return q; // `q` escapes via the return value.
29+ * }
30+ * else {
31+ * return r + n; // `r` escapes via the return value, even though an offset has been added.
32+ * }
33+ * }
34+ * ```
35+ *
36+ * For the above function, the following terms hold:
37+ * - `parameterEscapesOnlyViaReturn(1)`
38+ * - `parameterEscapesOnlyViaReturn(2)`
39+ * - `parameterNeverEscapes(3)`
40+ */
41+ abstract predicate parameterNeverEscapes ( int index ) ;
8042
8143 /**
82- * Specifies whether the address passed to the parameter at the specified index is retained after
83- * the function returns. The result is given as a `ParameterEscape` object. See the comments for
84- * that class and its subclasses for a description of each possible result.
85- */
86- ParameterEscape getParameterEscapeBehavior ( Function f , int index ) {
87- result = f .( AliasFunction ) .getParameterEscapeBehavior ( index ) or
88- (
89- not f instanceof AliasFunction and
90- exists ( f .getParameter ( index ) ) and
91- result instanceof Escapes
92- )
93- }
44+ * Holds if the address passed to the parameter at the specified index escapes via the return
45+ * value of the function, but does not otherwise escape. See the comment for
46+ * `parameterNeverEscapes` for an example.
47+ */
48+ abstract predicate parameterEscapesOnlyViaReturn ( int index ) ;
9449
9550 /**
9651 * Holds if the function always returns the value of the parameter at the specified index.
9752 */
98- predicate parameterIsAlwaysReturned ( Function f , int index ) {
99- f .( AliasFunction ) .parameterIsAlwaysReturned ( index )
100- }
101- }
53+ abstract predicate parameterIsAlwaysReturned ( int index ) ;
54+ }
0 commit comments