forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFlowImpl.qll
More file actions
3641 lines (3045 loc) · 127 KB
/
DataFlowImpl.qll
File metadata and controls
3641 lines (3045 loc) · 127 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* INTERNAL: Do not use.
*
* Provides an implementation of global (interprocedural) data flow.
*/
private import codeql.util.Unit
private import codeql.util.Option
private import codeql.util.Boolean
private import codeql.util.Location
private import codeql.dataflow.DataFlow
private import DataFlowImplStage1
module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
private import Lang
private import DataFlowMake<Location, Lang>
private import MakeImplStage1<Location, Lang>
private import DataFlowImplCommon::MakeImplCommon<Location, Lang>
private import DataFlowImplCommonPublic
private import Aliases
private import codeql.util.AlertFiltering
private module AlertFiltering = AlertFilteringImpl<Location>;
/**
* An input configuration for data flow using flow state. This signature equals
* `StateConfigSig`, but requires explicit implementation of all predicates.
*/
signature module FullStateConfigSig {
bindingset[this]
class FlowState;
/**
* Holds if `source` is a relevant data flow source with the given initial
* `state`.
*/
predicate isSource(Node source, FlowState state);
/**
* Holds if `sink` is a relevant data flow sink accepting `state`.
*/
predicate isSink(Node sink, FlowState state);
/**
* Holds if `sink` is a relevant data flow sink for any state.
*/
predicate isSink(Node sink);
/**
* Holds if data flow through `node` is prohibited. This completely removes
* `node` from the data flow graph.
*/
predicate isBarrier(Node node);
/**
* Holds if data flow through `node` is prohibited when the flow state is
* `state`.
*/
predicate isBarrier(Node node, FlowState state);
/** Holds if data flow into `node` is prohibited. */
predicate isBarrierIn(Node node);
/** Holds if data flow into `node` is prohibited when the target flow state is `state`. */
predicate isBarrierIn(Node node, FlowState state);
/** Holds if data flow out of `node` is prohibited. */
predicate isBarrierOut(Node node);
/** Holds if data flow out of `node` is prohibited when the originating flow state is `state`. */
predicate isBarrierOut(Node node, FlowState state);
/**
* Holds if data may flow from `node1` to `node2` in addition to the normal data-flow steps.
*/
predicate isAdditionalFlowStep(Node node1, Node node2, string model);
/**
* Holds if data may flow from `node1` to `node2` in addition to the normal data-flow steps.
* This step is only applicable in `state1` and updates the flow state to `state2`.
*/
predicate isAdditionalFlowStep(
Node node1, FlowState state1, Node node2, FlowState state2, string model
);
/**
* Holds if an arbitrary number of implicit read steps of content `c` may be
* taken at `node`.
*/
predicate allowImplicitRead(Node node, ContentSet c);
/**
* Holds if `node` should never be skipped over in the `PathGraph` and in path
* explanations.
*/
predicate neverSkip(Node node);
/**
* Gets the virtual dispatch branching limit when calculating field flow.
* This can be overridden to a smaller value to improve performance (a
* value of 0 disables field flow), or a larger value to get more results.
*/
int fieldFlowBranchLimit();
/** Gets the access path limit. */
int accessPathLimit();
/**
* Gets a data flow configuration feature to add restrictions to the set of
* valid flow paths.
*
* - `FeatureHasSourceCallContext`:
* Assume that sources have some existing call context to disallow
* conflicting return-flow directly following the source.
* - `FeatureHasSinkCallContext`:
* Assume that sinks have some existing call context to disallow
* conflicting argument-to-parameter flow directly preceding the sink.
* - `FeatureEqualSourceSinkCallContext`:
* Implies both of the above and additionally ensures that the entire flow
* path preserves the call context.
*
* These features are generally not relevant for typical end-to-end data flow
* queries, but should only be used for constructing paths that need to
* somehow be pluggable in another path context.
*/
FlowFeature getAFeature();
/**
* Holds if hidden nodes should be included in the data flow graph.
*
* This feature should only be used for debugging or when the data flow graph
* is not visualized (as it is in a `path-problem` query).
*/
predicate includeHiddenNodes();
/**
* Holds if sources and sinks should be filtered to only include those that
* may lead to a flow path with either a source or a sink in the location
* range given by `AlertFiltering`. This only has an effect when running
* in diff-informed incremental mode.
*
* This flag should only be applied to flow configurations whose results
* are used directly in a query result.
*/
predicate observeDiffInformedIncrementalMode();
Location getASelectedSourceLocation(Node source);
Location getASelectedSinkLocation(Node sink);
}
/**
* Provides default `FlowState` implementations given a `StateConfigSig`.
*/
module DefaultState<ConfigSig Config> {
class FlowState = Unit;
predicate isSource(Node source, FlowState state) { Config::isSource(source) and exists(state) }
predicate isSink(Node sink, FlowState state) { Config::isSink(sink) and exists(state) }
predicate isBarrier(Node node, FlowState state) { none() }
predicate isBarrierIn(Node node, FlowState state) { none() }
predicate isBarrierOut(Node node, FlowState state) { none() }
predicate isAdditionalFlowStep(
Node node1, FlowState state1, Node node2, FlowState state2, string model
) {
none()
}
}
/**
* This private type is used instead of `Unit` to ensure that users of this
* library don't take an accidental dependency on convertability to `Unit`,
* allowing us to change the implementation in the future.
*/
private newtype TDiffInformedBase = MkDiffInformedBase()
abstract class DiffInformedQueryImpl extends TDiffInformedBase {
DiffInformedQueryImpl() { any() }
string toString() { result = "DataFlow::DiffInformedQuery" }
Location getASelectedSourceLocation(Node source) { result = source.getLocation() }
Location getASelectedSinkLocation(Node sink) { result = sink.getLocation() }
pragma[nomagic]
predicate hasSourceInDiffRange() {
AlertFiltering::filterByLocation(this.getASelectedSourceLocation(_))
}
pragma[nomagic]
predicate hasSinkInDiffRange() {
AlertFiltering::filterByLocation(this.getASelectedSinkLocation(_))
}
}
module MakePrimaryDiffInformed<FullStateConfigSig Config> implements FullStateConfigSig {
// Workaround for name clash
predicate accessPathLimit = Config::accessPathLimit/0;
import Config
predicate observeDiffInformedIncrementalMode() {
// Add to existing configuration to support composition of config transformers
Config::observeDiffInformedIncrementalMode()
or
exists(DiffInformedQueryImpl q)
}
Location getASelectedSourceLocation(Node source) {
result = Config::getASelectedSourceLocation(source)
or
exists(DiffInformedQueryImpl q | result = q.getASelectedSourceLocation(source))
}
Location getASelectedSinkLocation(Node sink) {
result = Config::getASelectedSinkLocation(sink)
or
exists(DiffInformedQueryImpl q | result = q.getASelectedSinkLocation(sink))
}
}
module SecondaryConfigHelpers {
newtype IsSourceOrSink =
IsSource() or
IsSink()
signature module SecondaryConfig {
/**
* Gets the source/sink node from the primary configuration that is
* informed by a given source/sink node from the secondary configuration.
* Whether the secondary node is a source or a sink is determined by
* `sourceOrSink`.
*/
bindingset[sourceOrSink, secondaryNode]
Node getPrimaryOfSecondaryNode(IsSourceOrSink sourceOrSink, Node secondaryNode);
}
}
module MakeSinkFinder<FullStateConfigSig Config, SecondaryConfig SC> implements FullStateConfigSig
{
// Workaround for name clash
predicate accessPathLimit = Config::accessPathLimit/0;
import Config
predicate observeDiffInformedIncrementalMode() {
// Add to existing configuration to support composition of config transformers
Config::observeDiffInformedIncrementalMode()
or
// Because this configuration is for finding sinks to be used in a main
// data-flow configuration, this configuration should only restrict the
// sinks to be found if there are no main-configuration sources in the
// diff range. That's because if there is such a source, we need to
// report query results for it even with sinks outside the diff range.
//
// The `MakeSinkFinder` and `MakeSourceFinder` modules are separate
// because each can only call one of `hasSourceInDiffRange` or
// `hasSinkInDiffRange`. Otherwise it would look like a non-monotonic
// recursion.
exists(DiffInformedQuery q | not q.hasSourceInDiffRange())
}
Location getASelectedSourceLocation(Node source) {
result = Config::getASelectedSourceLocation(source)
or
exists(DiffInformedQueryImpl q, IsSource s |
result = q.getASelectedSourceLocation(SC::getPrimaryOfSecondaryNode(s, source))
)
}
Location getASelectedSinkLocation(Node sink) {
result = Config::getASelectedSinkLocation(sink)
or
exists(DiffInformedQueryImpl q, IsSink s |
result = q.getASelectedSinkLocation(SC::getPrimaryOfSecondaryNode(s, sink))
)
}
}
/**
* Constructs a data flow computation given a full input configuration, and
* an initial stage 1 pruning.
*/
module Impl<FullStateConfigSig Config, Stage1Output<Config::FlowState> Stage1> {
private class FlowState = Config::FlowState;
private class Nd = Stage1::Nd;
private class ArgNd = Stage1::ArgNd;
private class ParamNd = Stage1::ParamNd;
private class RetNd = Stage1::RetNd;
private class OutNd = Stage1::OutNd;
private class CastingNd = Stage1::CastingNd;
private predicate toNormalSinkNode = Stage1::toNormalSinkNode/1;
private predicate sourceNode = Stage1::sourceNode/1;
private predicate sinkNode = Stage1::sinkNode/1;
private predicate hasSourceCallCtx = Stage1::hasSourceCallCtx/0;
private predicate hasSinkCallCtx = Stage1::hasSinkCallCtx/0;
private predicate jumpStepEx = Stage1::jumpStepEx/2;
private predicate additionalJumpStep = Stage1::additionalJumpStep/3;
private predicate localStep1 = Stage1::localStep1/6;
private predicate isStateStep = Stage1::isStateStep/2;
private predicate sourceModel(Nd n, string model) {
exists(NodeEx node | sourceNode(n) and node = n.getNodeEx() |
model = getSourceModel(node)
or
not exists(getSourceModel(node)) and model = ""
)
}
private predicate sinkModel(Nd n, string model) {
exists(NodeEx node | sinkNode(n) and node = n.getNodeEx() |
model = getSinkModel(node)
or
not exists(getSinkModel(node)) and model = ""
)
}
bindingset[label1, label2]
pragma[inline_late]
private string mergeLabels(string label1, string label2) {
if label2.matches("Sink:%")
then if label1 = "" then result = label2 else result = label1 + " " + label2
else
// Big-step, hidden nodes, and summaries all may need to merge labels.
// These cases are expected to involve at most one non-empty label, so
// we'll just discard the 2nd+ label for now.
if label1 = ""
then result = label2
else result = label1
}
pragma[nomagic]
private predicate allowsFieldFlowThrough(Call call, Callable c) {
Stage1::callEdgeReturn(call, c, _, _, _, true)
}
private signature module StageSig {
class Ap;
class ApNil extends Ap;
predicate revFlow(Nd node);
predicate revFlow(Nd node, Ap ap);
predicate callMayFlowThroughRev(Call call);
predicate parameterMayFlowThrough(ParamNd p, boolean emptyAp);
predicate returnMayFlowThrough(RetNd ret, ReturnKindExt kind);
predicate storeStepCand(Nd node1, Content c, Nd node2, Type contentType, Type containerType);
predicate readStepCand(Nd n1, Content c, Nd n2);
predicate callEdgeArgParam(Call call, Callable c, ArgNd arg, ParamNd p, boolean emptyAp);
predicate callEdgeReturn(
Call call, Callable c, RetNd ret, ReturnKindExt kind, Nd out, boolean allowsFieldFlow
);
predicate relevantCallEdgeIn(Call call, Callable c);
predicate relevantCallEdgeOut(Call call, Callable c);
}
private module MkStage<StageSig PrevStage> {
class ApApprox = PrevStage::Ap;
signature module StageParam {
class Typ {
string toString();
}
class Ap {
string toString();
}
class ApNil extends Ap;
bindingset[result, ap]
ApApprox getApprox(Ap ap);
Typ getTyp(Type t);
bindingset[c, tail]
Ap apCons(Content c, Ap tail);
/**
* An approximation of `Content` that corresponds to the precision level of
* `Ap`, such that the mappings from both `Ap` and `Content` to this type
* are functional.
*/
class ApHeadContent;
ApHeadContent getHeadContent(Ap ap);
ApHeadContent projectToHeadContent(Content c);
class ApOption;
ApOption apNone();
ApOption apSome(Ap ap);
class Cc {
string toString();
}
class CcCall extends Cc;
// TODO: member predicate on CcCall
predicate matchesCall(CcCall cc, Call call);
class CcNoCall extends Cc;
Cc ccNone();
CcCall ccSomeCall();
/*
* The following `instanceof` predicates are necessary for proper
* caching, since we're able to cache predicates, but not the underlying
* types.
*/
predicate instanceofCc(Cc cc);
predicate instanceofCcCall(CcCall cc);
predicate instanceofCcNoCall(CcNoCall cc);
class LocalCc;
Callable viableImplCallContextReduced(Call call, CcCall ctx);
bindingset[call, ctx]
predicate viableImplNotCallContextReduced(Call call, Cc ctx);
bindingset[call, c]
CcCall getCallContextCall(Call call, Callable c);
Call viableImplCallContextReducedReverse(Callable c, CcNoCall ctx);
predicate viableImplNotCallContextReducedReverse(CcNoCall ctx);
bindingset[call, c]
CcNoCall getCallContextReturn(Callable c, Call call);
bindingset[cc]
LocalCc getLocalCc(Cc cc);
predicate localStep(
Nd node1, Nd node2, boolean preservesValue, Typ t, LocalCc lcc, string label
);
bindingset[node, t0, ap]
predicate filter(Nd node, Typ t0, Ap ap, Typ t);
bindingset[node, ap, isStoreStep]
predicate stepFilter(Nd node, Ap ap, boolean isStoreStep);
bindingset[t1, t2]
predicate typecheck(Typ t1, Typ t2);
default predicate enableTypeFlow() { any() }
}
module Stage<StageParam Param> implements StageSig {
import Param
private module TypOption = Option<Typ>;
private class TypOption = TypOption::Option;
private string ppStored(TypOption stored) {
exists(string ppt | ppt = stored.toString() |
if stored.isNone() or ppt = "" then result = "" else result = " : " + ppt
)
}
bindingset[ap]
private boolean isNil(Ap ap) {
if ap instanceof ApNil then result = true else result = false
}
/* Begin: Stage logic. */
pragma[nomagic]
private Typ getNodeTyp(Nd node) {
PrevStage::revFlow(node) and result = getTyp(node.getType())
}
pragma[nomagic]
private predicate flowThroughOutOfCall(Call call, RetNd ret, Nd out, boolean allowsFieldFlow) {
exists(ReturnKindExt kind |
PrevStage::callEdgeReturn(call, _, ret, kind, out, allowsFieldFlow) and
PrevStage::callMayFlowThroughRev(call) and
PrevStage::returnMayFlowThrough(ret, kind)
)
}
pragma[nomagic]
private predicate compatibleContainer0(ApHeadContent apc, Type containerType) {
exists(Type containerType0, Content c |
PrevStage::storeStepCand(_, c, _, _, containerType0) and
not topTypeContent(apc) and
compatibleTypesCached(containerType0, containerType) and
apc = projectToHeadContent(c)
)
}
pragma[nomagic]
private predicate topTypeContent(ApHeadContent apc) {
exists(Type containerType0, Content c |
PrevStage::storeStepCand(_, c, _, _, containerType0) and
isTopType(containerType0) and
apc = projectToHeadContent(c)
)
}
bindingset[apc, containerType]
pragma[inline_late]
private predicate compatibleContainer(ApHeadContent apc, Type containerType) {
compatibleContainer0(apc, containerType)
}
/**
* Holds if `node` is reachable with access path `ap` from a source.
*
* The call context `cc` records whether the node is reached through an
* argument in a call, and if so, `summaryCtx` records the
* corresponding parameter position and access path of that argument.
*/
pragma[nomagic]
additional predicate fwdFlow(
Nd node, Cc cc, SummaryCtx summaryCtx, Typ t, Ap ap, TypOption stored
) {
fwdFlow1(node, cc, summaryCtx, _, t, ap, stored)
}
private predicate fwdFlow1(
Nd node, Cc cc, SummaryCtx summaryCtx, Typ t0, Typ t, Ap ap, TypOption stored
) {
exists(ApApprox apa |
fwdFlow0(node, cc, summaryCtx, t0, ap, apa, stored) and
PrevStage::revFlow(node, apa) and
filter(node, t0, ap, t) and
(
if node instanceof CastingNd
then
ap instanceof ApNil or
compatibleContainer(getHeadContent(ap), node.getType()) or
topTypeContent(getHeadContent(ap))
else any()
)
)
}
pragma[nomagic]
private predicate fwdFlow0(
Nd node, Cc cc, SummaryCtx summaryCtx, Typ t, Ap ap, ApApprox apa, TypOption stored
) {
sourceNode(node) and
(if hasSourceCallCtx() then cc = ccSomeCall() else cc = ccNone()) and
summaryCtx = TSummaryCtxNone() and
t = getNodeTyp(node) and
ap instanceof ApNil and
apa = getApprox(ap) and
stored.isNone()
or
exists(Nd mid, Typ t0, LocalCc localCc |
fwdFlow(mid, cc, summaryCtx, t0, ap, stored) and
apa = getApprox(ap) and
localCc = getLocalCc(cc)
|
localStep(mid, node, true, _, localCc, _) and
t = t0
or
localStep(mid, node, false, t, localCc, _) and
ap instanceof ApNil
)
or
fwdFlowJump(node, t, ap, stored) and
apa = getApprox(ap) and
cc = ccNone() and
summaryCtx = TSummaryCtxNone()
or
// store
exists(Content c, Ap ap0 |
fwdFlowStore(_, _, ap0, _, c, t, stored, node, cc, summaryCtx) and
ap = apCons(c, ap0) and
apa = getApprox(ap)
)
or
// read
fwdFlowRead(_, _, _, _, _, node, t, ap, stored, cc, summaryCtx) and
apa = getApprox(ap)
or
// flow into a callable without summary context
fwdFlowInNoFlowThrough(node, cc, t, ap, stored) and
apa = getApprox(ap) and
summaryCtx = TSummaryCtxNone() and
// When the call contexts of source and sink needs to match then there's
// never any reason to enter a callable except to find a summary. See also
// the comment in `PathNodeMid::isAtSink`.
not Config::getAFeature() instanceof FeatureEqualSourceSinkCallContext
or
// flow into a callable with summary context (non-linear recursion)
fwdFlowInFlowThrough(node, cc, t, ap, stored) and
apa = getApprox(ap) and
summaryCtx = TSummaryCtxSome(node, t, ap, stored)
or
// flow out of a callable
fwdFlowOut(_, _, node, cc, summaryCtx, t, ap, stored) and
apa = getApprox(ap)
or
// flow through a callable
exists(Call call, RetNd ret, boolean allowsFieldFlow |
fwdFlowThrough(call, cc, summaryCtx, t, ap, stored, ret) and
flowThroughOutOfCall(call, ret, node, allowsFieldFlow) and
apa = getApprox(ap) and
if allowsFieldFlow = false then ap instanceof ApNil else any()
)
}
private newtype TSummaryCtx =
TSummaryCtxNone() or
TSummaryCtxSome(ParamNd p, Typ t, Ap ap, TypOption stored) {
fwdFlowInFlowThrough(p, _, t, ap, stored)
}
/**
* A context for generating flow summaries. This represents flow entry through
* a specific parameter with an access path of a specific shape.
*
* Summaries are only created for parameters that may flow through.
*/
private class SummaryCtx extends TSummaryCtx {
abstract string toString();
abstract Location getLocation();
}
/** A summary context from which no flow summary can be generated. */
private class SummaryCtxNone extends SummaryCtx, TSummaryCtxNone {
override string toString() { result = "<none>" }
override Location getLocation() { result.hasLocationInfo("", 0, 0, 0, 0) }
}
/** A summary context from which a flow summary can be generated. */
private class SummaryCtxSome extends SummaryCtx, TSummaryCtxSome {
private ParamNd p;
private Typ t;
private Ap ap;
private TypOption stored;
SummaryCtxSome() { this = TSummaryCtxSome(p, t, ap, stored) }
ParamNd getParamNode() { result = p }
private string ppTyp() { result = t.toString() and result != "" }
override string toString() {
result = p + concat(" : " + this.ppTyp()) + " " + ap + ppStored(stored)
}
override Location getLocation() { result = p.getLocation() }
}
private predicate fwdFlowJump(Nd node, Typ t, Ap ap, TypOption stored) {
exists(Nd mid |
fwdFlow(mid, _, _, t, ap, stored) and
jumpStepEx(mid, node)
)
or
exists(Nd mid |
fwdFlow(mid, _, _, _, ap, stored) and
additionalJumpStep(mid, node, _) and
t = getNodeTyp(node) and
ap instanceof ApNil
)
}
pragma[nomagic]
private predicate fwdFlowStore(
Nd node1, Typ t1, Ap ap1, TypOption stored1, Content c, Typ t2, TypOption stored2,
Nd node2, Cc cc, SummaryCtx summaryCtx
) {
exists(Type contentType, Type containerType |
fwdFlow(node1, cc, summaryCtx, t1, ap1, stored1) and
PrevStage::storeStepCand(node1, c, node2, contentType, containerType) and
t2 = getTyp(containerType) and
// We need to typecheck stores here, since reverse flow through a getter
// might have a different type here compared to inside the getter.
typecheck(t1, getTyp(contentType)) and
if ap1 instanceof ApNil then stored2.asSome() = t1 else stored2 = stored1
)
}
/**
* Holds if forward flow with access path `tail` and type `t1` reaches a
* store of `c` on a container of type `t2` resulting in access path
* `cons`.
*/
pragma[nomagic]
private predicate fwdFlowConsCand(Typ t2, Ap cons, Content c, Typ t1, Ap tail) {
fwdFlowStore(_, t1, tail, _, c, t2, _, _, _, _) and
cons = apCons(c, tail)
}
pragma[nomagic]
private predicate readStepCand(Nd node1, ApHeadContent apc, Content c, Nd node2) {
PrevStage::readStepCand(node1, c, node2) and
apc = projectToHeadContent(c)
}
bindingset[node1, apc]
pragma[inline_late]
private predicate readStepCand0(Nd node1, ApHeadContent apc, Content c, Nd node2) {
readStepCand(node1, apc, c, node2)
}
pragma[nomagic]
private predicate fwdFlowRead0(
Typ t, Ap ap, TypOption stored, Content c, Nd node1, Nd node2, Cc cc,
SummaryCtx summaryCtx
) {
exists(ApHeadContent apc |
fwdFlow(node1, cc, summaryCtx, t, ap, stored) and
apc = getHeadContent(ap) and
readStepCand0(node1, apc, c, node2)
)
}
pragma[nomagic]
private predicate fwdFlowRead(
Nd node1, Typ t1, Ap ap1, TypOption stored1, Content c, Nd node2, Typ t2, Ap ap2,
TypOption stored2, Cc cc, SummaryCtx summaryCtx
) {
exists(Typ ct1, Typ ct2 |
fwdFlowRead0(t1, ap1, stored1, c, node1, node2, cc, summaryCtx) and
fwdFlowConsCand(ct1, ap1, c, ct2, ap2) and
typecheck(t1, ct1) and
typecheck(t2, ct2) and
if ap2 instanceof ApNil
then stored2.isNone() and stored1.asSome() = t2
else (
stored2 = stored1 and t2 = getNodeTyp(node2)
)
)
}
pragma[nomagic]
private predicate fwdFlowIntoArg(
ArgNd arg, Cc outercc, SummaryCtx summaryCtx, Typ t, Ap ap, boolean emptyAp,
TypOption stored, boolean cc
) {
fwdFlow(arg, outercc, summaryCtx, t, ap, stored) and
(if instanceofCcCall(outercc) then cc = true else cc = false) and
emptyAp = isNil(ap)
}
private signature predicate flowThroughSig();
/**
* Exposes the inlined predicate `fwdFlowIn`, which is used to calculate both
* flow in and flow through.
*
* For flow in, only a subset of the columns are needed, specifically we don't
* need to record the argument that flows into the parameter.
*
* For flow through, we do need to record the argument, however, we can restrict
* this to arguments that may actually flow through, which reduces the
* argument-to-parameter fan-in significantly.
*/
private module FwdFlowIn<flowThroughSig/0 flowThrough> {
pragma[nomagic]
private predicate callEdgeArgParamRestricted(
Call call, Callable c, ArgNd arg, ParamNd p, boolean emptyAp
) {
PrevStage::callEdgeArgParam(call, c, arg, p, emptyAp) and
if
PrevStage::callMayFlowThroughRev(call) and
PrevStage::parameterMayFlowThrough(p, emptyAp)
then
emptyAp = true and
flowThrough()
or
emptyAp = false and
if allowsFieldFlowThrough(call, c) then flowThrough() else not flowThrough()
else not flowThrough()
}
pragma[nomagic]
private Callable viableImplCallContextReducedRestricted(Call call, CcCall ctx) {
result = viableImplCallContextReduced(call, ctx) and
callEdgeArgParamRestricted(call, result, _, _, _)
}
bindingset[call, ctx]
pragma[inline_late]
private Callable viableImplCallContextReducedInlineLate(Call call, CcCall ctx) {
result = viableImplCallContextReducedRestricted(call, ctx)
}
bindingset[arg, ctx]
pragma[inline_late]
private Callable viableImplCallContextReducedInlineLate(Call call, ArgNd arg, CcCall ctx) {
callEdgeArgParamRestricted(call, _, arg, _, _) and
instanceofCcCall(ctx) and
result = viableImplCallContextReducedInlineLate(call, ctx)
}
bindingset[call]
pragma[inline_late]
private predicate callEdgeArgParamRestrictedInlineLate(
Call call, Callable c, ArgNd arg, ParamNd p, boolean emptyAp
) {
callEdgeArgParamRestricted(call, c, arg, p, emptyAp)
}
bindingset[call, ctx]
pragma[inline_late]
private predicate viableImplNotCallContextReducedInlineLate(Call call, Cc ctx) {
instanceofCc(ctx) and
viableImplNotCallContextReduced(call, ctx)
}
bindingset[arg, outercc]
pragma[inline_late]
private predicate viableImplArgNotCallContextReduced(Call call, ArgNd arg, Cc outercc) {
callEdgeArgParamRestricted(call, _, arg, _, _) and
instanceofCc(outercc) and
viableImplNotCallContextReducedInlineLate(call, outercc)
}
pragma[inline]
private predicate fwdFlowInCand(
Call call, ArgNd arg, Cc outercc, Callable inner, ParamNd p, SummaryCtx summaryCtx,
Typ t, Ap ap, boolean emptyAp, TypOption stored, boolean cc
) {
fwdFlowIntoArg(arg, outercc, summaryCtx, t, ap, emptyAp, stored, cc) and
(
inner = viableImplCallContextReducedInlineLate(call, arg, outercc)
or
viableImplArgNotCallContextReduced(call, arg, outercc)
) and
callEdgeArgParamRestrictedInlineLate(call, inner, arg, p, emptyAp)
}
pragma[inline]
private predicate fwdFlowInCandTypeFlowDisabled(
Call call, ArgNd arg, Cc outercc, Callable inner, ParamNd p, SummaryCtx summaryCtx,
Typ t, Ap ap, TypOption stored, boolean cc
) {
not enableTypeFlow() and
fwdFlowInCand(call, arg, outercc, inner, p, summaryCtx, t, ap, _, stored, cc)
}
pragma[nomagic]
private predicate fwdFlowInCandTypeFlowEnabled(
Call call, ArgNd arg, Cc outercc, Callable inner, ParamNd p, boolean emptyAp, boolean cc
) {
enableTypeFlow() and
fwdFlowInCand(call, arg, outercc, inner, p, _, _, _, emptyAp, _, cc)
}
pragma[nomagic]
private predicate fwdFlowInValidEdgeTypeFlowDisabled(
Call call, Callable inner, CcCall innercc, boolean cc
) {
not enableTypeFlow() and
FwdTypeFlow::typeFlowValidEdgeIn(call, inner, cc) and
innercc = getCallContextCall(call, inner)
}
pragma[nomagic]
private predicate fwdFlowInValidEdgeTypeFlowEnabled(
Call call, ArgNd arg, Cc outercc, Callable inner, ParamNd p, CcCall innercc,
boolean emptyAp, boolean cc
) {
fwdFlowInCandTypeFlowEnabled(call, arg, outercc, inner, p, emptyAp, cc) and
FwdTypeFlow::typeFlowValidEdgeIn(call, inner, cc) and
innercc = getCallContextCall(call, inner)
}
pragma[inline]
predicate fwdFlowIn(
Call call, ArgNd arg, Callable inner, ParamNd p, Cc outercc, CcCall innercc,
SummaryCtx summaryCtx, Typ t, Ap ap, TypOption stored, boolean cc
) {
// type flow disabled: linear recursion
fwdFlowInCandTypeFlowDisabled(call, arg, outercc, inner, p, summaryCtx, t, ap, stored,
cc) and
fwdFlowInValidEdgeTypeFlowDisabled(call, inner, innercc, pragma[only_bind_into](cc))
or
// type flow enabled: non-linear recursion
exists(boolean emptyAp |
fwdFlowIntoArg(arg, outercc, summaryCtx, t, ap, emptyAp, stored, cc) and
fwdFlowInValidEdgeTypeFlowEnabled(call, arg, outercc, inner, p, innercc, emptyAp, cc)
)
}
}
private predicate bottom() { none() }
private module FwdFlowInNoThrough = FwdFlowIn<bottom/0>;
pragma[nomagic]
private predicate fwdFlowInNoFlowThrough(
ParamNd p, CcCall innercc, Typ t, Ap ap, TypOption stored
) {
FwdFlowInNoThrough::fwdFlowIn(_, _, _, p, _, innercc, _, t, ap, stored, _)
}
private predicate top() { any() }
private module FwdFlowInThrough = FwdFlowIn<top/0>;
pragma[nomagic]
private predicate fwdFlowInFlowThrough(
ParamNd p, CcCall innercc, Typ t, Ap ap, TypOption stored
) {
FwdFlowInThrough::fwdFlowIn(_, _, _, p, _, innercc, _, t, ap, stored, _)
}
pragma[nomagic]
private Call viableImplCallContextReducedReverseRestricted(Callable c, CcNoCall ctx) {
result = viableImplCallContextReducedReverse(c, ctx) and
PrevStage::callEdgeReturn(result, c, _, _, _, _)
}
bindingset[c, ctx]
pragma[inline_late]
private Call viableImplCallContextReducedReverseInlineLate(Callable c, CcNoCall ctx) {
result = viableImplCallContextReducedReverseRestricted(c, ctx)
}
bindingset[call]
pragma[inline_late]
private predicate flowOutOfCallInlineLate(
Call call, Callable c, RetNd ret, Nd out, boolean allowsFieldFlow
) {
PrevStage::callEdgeReturn(call, c, ret, _, out, allowsFieldFlow)
}
bindingset[c, ret, innercc]
pragma[inline_late]
pragma[noopt]
private predicate flowOutOfCallNotCallContextReduced(
Call call, Callable c, RetNd ret, Nd out, boolean allowsFieldFlow, CcNoCall innercc
) {
viableImplNotCallContextReducedReverse(innercc) and
PrevStage::callEdgeReturn(call, c, ret, _, out, allowsFieldFlow)
}
pragma[nomagic]
private predicate fwdFlowIntoRet(
RetNd ret, CcNoCall cc, SummaryCtx summaryCtx, Typ t, Ap ap, TypOption stored
) {
instanceofCcNoCall(cc) and
fwdFlow(ret, cc, summaryCtx, t, ap, stored)
}
pragma[nomagic]
private predicate fwdFlowOutCand(
Call call, RetNd ret, CcNoCall innercc, Callable inner, Nd out, boolean allowsFieldFlow
) {
fwdFlowIntoRet(ret, innercc, _, _, _, _) and
inner = ret.getEnclosingCallable() and
(
call = viableImplCallContextReducedReverseInlineLate(inner, innercc) and
flowOutOfCallInlineLate(call, inner, ret, out, allowsFieldFlow)
or
flowOutOfCallNotCallContextReduced(call, inner, ret, out, allowsFieldFlow, innercc)
)
}