FIR CFA: properly visit subgraphs in checkers

Interpretation: a graph A is a subgraph of B if information available at
nodes of A depends on the paths taken in B. For example, local classes
are subgraphs of a graph in which they are declared, and members of
those classes are subgraphs of the local class itself - because these
members can reference captured values.

Consequences:

 * if graph G is a subgraph of node N, then G is a subgraph of N's
   owner;
 * `ControlFlowAnalysisDiagnosticComponent` will only visit root graphs;
 * `graph.traverse` will ignore subgraph boundaries, as if all subgraphs
   are inlined into one huge root graph;
 * if a control flow checker needs information from a declaration to
   which a graph is attached, it must look at subgraphs explicitly.

For example, consider the `callsInPlace` checker. When a function
has a `callsInPlace` contract and a local declaration, the checker must
visit that local declaration to ensure it does not capture the allegedly
called-in-place argument - hence `graph.traverse` will look at the
nodes. However, the local declaration can also be a function with its
own `callsInPlace` contracts, so the checker should also run for it in
isolation. If that sounds quadratic, that's because unfortunately it is.
This commit is contained in:
pyos
2022-12-09 12:32:46 +01:00
committed by Dmitriy Novozhilov
parent 5180e1f4a4
commit aadea0e26f
35 changed files with 1191 additions and 1325 deletions
@@ -19,7 +19,25 @@ digraph inAnonymousObject_kt {
color=blue
13 [label="Enter class <anonymous object>" style="filled" fillcolor=red];
14 [label="Part of class initialization"];
subgraph cluster_4 {
color=blue
17 [label="Enter property" style="filled" fillcolor=red];
18 [label="Access variable R|<local>/a|"];
19 [label="Exit property" style="filled" fillcolor=red];
}
15 [label="Part of class initialization"];
subgraph cluster_5 {
color=blue
20 [label="Enter init block" style="filled" fillcolor=red];
subgraph cluster_6 {
color=blue
21 [label="Enter block"];
22 [label="Access variable R|<local>/b|"];
23 [label="Assignment: R|/<anonymous>.leaked|"];
24 [label="Exit block"];
}
25 [label="Exit init block" style="filled" fillcolor=red];
}
16 [label="Exit class <anonymous object>" style="filled" fillcolor=red];
}
5 [label="Exit anonymous object"];
@@ -32,17 +50,34 @@ digraph inAnonymousObject_kt {
}
12 [label="Exit function foo" style="filled" fillcolor=red];
}
subgraph cluster_7 {
color=blue
29 [label="Enter function run" style="filled" fillcolor=red];
subgraph cluster_8 {
color=blue
30 [label="Enter block"];
31 [label="Function call: R|<local>/c|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
32 [label="Exit block"];
}
33 [label="Exit function run" style="filled" fillcolor=red];
}
subgraph cluster_9 {
color=blue
26 [label="Enter function <init>" style="filled" fillcolor=red];
27 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
28 [label="Exit function <init>" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {4};
3 -> {37 40 43 49} [color=red];
3 -> {17 20 26 29} [color=red];
4 -> {13} [color=green];
4 -> {5} [color=red];
4 -> {13} [style=dashed];
5 -> {6};
5 -> {37 49} [color=green];
5 -> {37 49} [style=dashed];
5 -> {26 29} [color=green];
5 -> {26 29} [style=dashed];
6 -> {7};
7 -> {8};
8 -> {9};
@@ -50,66 +85,27 @@ digraph inAnonymousObject_kt {
10 -> {11};
11 -> {12};
13 -> {14} [color=green];
14 -> {40} [color=green];
14 -> {17} [color=green];
14 -> {15} [style=dotted];
14 -> {40} [style=dashed];
15 -> {43} [color=green];
14 -> {17} [style=dashed];
15 -> {20} [color=green];
15 -> {16} [style=dotted];
15 -> {43} [style=dashed];
15 -> {20} [style=dashed];
16 -> {5} [color=green];
subgraph cluster_4 {
color=red
37 [label="Enter function <init>" style="filled" fillcolor=red];
38 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
39 [label="Exit function <init>" style="filled" fillcolor=red];
}
37 -> {38};
38 -> {39};
subgraph cluster_5 {
color=red
40 [label="Enter property" style="filled" fillcolor=red];
41 [label="Access variable R|<local>/a|"];
42 [label="Exit property" style="filled" fillcolor=red];
}
40 -> {41};
41 -> {42};
42 -> {15} [color=green];
subgraph cluster_6 {
color=red
43 [label="Enter init block" style="filled" fillcolor=red];
subgraph cluster_7 {
color=blue
44 [label="Enter block"];
45 [label="Access variable R|<local>/b|"];
46 [label="Assignment: R|/<anonymous>.leaked|"];
47 [label="Exit block"];
}
48 [label="Exit init block" style="filled" fillcolor=red];
}
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47};
47 -> {48};
48 -> {16} [color=green];
subgraph cluster_8 {
color=red
49 [label="Enter function run" style="filled" fillcolor=red];
subgraph cluster_9 {
color=blue
50 [label="Enter block"];
51 [label="Function call: R|<local>/c|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
52 [label="Exit block"];
}
53 [label="Exit function run" style="filled" fillcolor=red];
}
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53};
17 -> {18};
18 -> {19};
19 -> {15} [color=green];
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {16} [color=green];
26 -> {27};
27 -> {28};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
}
@@ -26,90 +26,86 @@ digraph inLocalClass_kt {
color=blue
10 [label="Enter class LocalClass" style="filled" fillcolor=red];
11 [label="Part of class initialization"];
subgraph cluster_4 {
color=blue
29 [label="Enter function run" style="filled" fillcolor=red];
subgraph cluster_5 {
color=blue
30 [label="Enter block"];
31 [label="Function call: R|<local>/d|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
32 [label="Exit block"];
}
33 [label="Exit function run" style="filled" fillcolor=red];
}
subgraph cluster_6 {
color=blue
23 [label="Enter function <init>" style="filled" fillcolor=red];
24 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
subgraph cluster_7 {
color=blue
25 [label="Enter block"];
26 [label="Function call: R|<local>/b|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
27 [label="Exit block"];
}
28 [label="Exit function <init>" style="filled" fillcolor=red];
}
subgraph cluster_8 {
color=blue
14 [label="Enter property" style="filled" fillcolor=red];
15 [label="Access variable R|<local>/a|"];
16 [label="Exit property" style="filled" fillcolor=red];
}
12 [label="Part of class initialization"];
subgraph cluster_9 {
color=blue
17 [label="Enter init block" style="filled" fillcolor=red];
subgraph cluster_10 {
color=blue
18 [label="Enter block"];
19 [label="Access variable R|<local>/c|"];
20 [label="Assignment: R|<local>/leaked|"];
21 [label="Exit block"];
}
22 [label="Exit init block" style="filled" fillcolor=red];
}
13 [label="Exit class LocalClass" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {4};
3 -> {38 41 47 53} [color=red];
3 -> {14 17 23 29} [color=red];
4 -> {5};
4 -> {10 41 53} [color=green];
4 -> {10 41 53} [style=dashed];
4 -> {10 23 29} [color=green];
4 -> {10 23 29} [style=dashed];
5 -> {6};
6 -> {7};
7 -> {8};
8 -> {9};
10 -> {11} [color=green];
11 -> {38} [color=green];
11 -> {14} [color=green];
11 -> {12} [style=dotted];
11 -> {38} [style=dashed];
12 -> {47} [color=green];
11 -> {14} [style=dashed];
12 -> {17} [color=green];
12 -> {13} [style=dotted];
12 -> {47} [style=dashed];
subgraph cluster_4 {
color=red
38 [label="Enter property" style="filled" fillcolor=red];
39 [label="Access variable R|<local>/a|"];
40 [label="Exit property" style="filled" fillcolor=red];
}
38 -> {39};
39 -> {40};
40 -> {12} [color=green];
subgraph cluster_5 {
color=red
41 [label="Enter function <init>" style="filled" fillcolor=red];
42 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
subgraph cluster_6 {
color=blue
43 [label="Enter block"];
44 [label="Function call: R|<local>/b|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
45 [label="Exit block"];
}
46 [label="Exit function <init>" style="filled" fillcolor=red];
}
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {46};
subgraph cluster_7 {
color=red
47 [label="Enter init block" style="filled" fillcolor=red];
subgraph cluster_8 {
color=blue
48 [label="Enter block"];
49 [label="Access variable R|<local>/c|"];
50 [label="Assignment: R|<local>/leaked|"];
51 [label="Exit block"];
}
52 [label="Exit init block" style="filled" fillcolor=red];
}
47 -> {48};
48 -> {49};
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {13} [color=green];
subgraph cluster_9 {
color=red
53 [label="Enter function run" style="filled" fillcolor=red];
subgraph cluster_10 {
color=blue
54 [label="Enter block"];
55 [label="Function call: R|<local>/d|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
56 [label="Exit block"];
}
57 [label="Exit function run" style="filled" fillcolor=red];
}
53 -> {54};
54 -> {55};
55 -> {56};
56 -> {57};
12 -> {17} [style=dashed];
14 -> {15};
15 -> {16};
16 -> {12} [color=green];
17 -> {18};
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {13} [color=green];
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
}