@@ -608,7 +608,7 @@ module LocalFlow {
608
608
*
609
609
* TODO: Remove once library code is extracted.
610
610
*/
611
- private module VariantLib {
611
+ private module VariantInLib {
612
612
private import codeql.util.Option
613
613
614
614
private class CrateOrigin extends string {
@@ -619,8 +619,8 @@ private module VariantLib {
619
619
620
620
private CrateOriginOption langCoreCrate ( ) { result .asSome ( ) = "lang:core" }
621
621
622
- private newtype TVariantLib =
623
- MkVariantLib ( CrateOriginOption crate , string path , string name ) {
622
+ private newtype TVariantInLib =
623
+ MkVariantInLib ( CrateOriginOption crate , string path , string name ) {
624
624
crate = langCoreCrate ( ) and
625
625
(
626
626
path = "crate::option::Option" and
@@ -631,43 +631,43 @@ private module VariantLib {
631
631
)
632
632
}
633
633
634
- /** A canonical path pointing to an enum variant . */
635
- class VariantLib extends MkVariantLib {
634
+ /** An enum variant from library code, represented by its canonical path . */
635
+ class VariantInLib extends MkVariantInLib {
636
636
CrateOriginOption crate ;
637
637
string path ;
638
638
string name ;
639
639
640
- VariantLib ( ) { this = MkVariantLib ( crate , path , name ) }
640
+ VariantInLib ( ) { this = MkVariantInLib ( crate , path , name ) }
641
+
642
+ int getAPosition ( ) {
643
+ this = MkVariantInLib ( langCoreCrate ( ) , "crate::option::Option" , "Some" ) and
644
+ result = 0
645
+ or
646
+ this = MkVariantInLib ( langCoreCrate ( ) , "crate::result::Result" , [ "Ok" , "Err" ] ) and
647
+ result = 0
648
+ }
641
649
642
650
string getExtendedCanonicalPath ( ) { result = path + "::" + name }
643
651
644
652
string toString ( ) { result = name }
645
653
}
646
654
647
- predicate variantLibPos ( VariantLib:: VariantLib v , int pos ) {
648
- v = MkVariantLib ( langCoreCrate ( ) , "crate::option::Option" , "Some" ) and
649
- pos = 0
650
- or
651
- v = MkVariantLib ( langCoreCrate ( ) , "crate::result::Result" , [ "Ok" , "Err" ] ) and
652
- pos = 0
653
- }
654
-
655
655
/** A tuple variant from library code. */
656
- class VariantLibTupleFieldContent extends VariantContent , TVariantLibTupleFieldContent {
657
- private VariantLib :: VariantLib v ;
656
+ class VariantInLibTupleFieldContent extends VariantContent , TVariantInLibTupleFieldContent {
657
+ private VariantInLib :: VariantInLib v ;
658
658
private int pos_ ;
659
659
660
- VariantLibTupleFieldContent ( ) { this = TVariantLibTupleFieldContent ( v , pos_ ) }
660
+ VariantInLibTupleFieldContent ( ) { this = TVariantInLibTupleFieldContent ( v , pos_ ) }
661
661
662
- VariantLib :: VariantLib getVariantLib ( int pos ) { result = v and pos = pos_ }
662
+ VariantInLib :: VariantInLib getVariantInLib ( int pos ) { result = v and pos = pos_ }
663
663
664
664
string getExtendedCanonicalPath ( ) { result = v .getExtendedCanonicalPath ( ) }
665
665
666
666
int getPosition ( ) { result = pos_ }
667
667
668
668
final override string toString ( ) {
669
669
// only print indices when the arity is > 1
670
- if exists ( TVariantLibTupleFieldContent ( v , 1 ) )
670
+ if exists ( TVariantInLibTupleFieldContent ( v , 1 ) )
671
671
then result = v .toString ( ) + "(" + pos_ + ")"
672
672
else result = v .toString ( )
673
673
}
@@ -687,39 +687,39 @@ private module VariantLib {
687
687
}
688
688
689
689
/** Holds if path `p` resolves to variant `v`. */
690
- private predicate pathResolveToVariantLib ( PathAstNode p , VariantLib v ) {
690
+ private predicate pathResolveToVariantInLib ( PathAstNode p , VariantInLib v ) {
691
691
exists ( CrateOriginOption crate , string path , string name |
692
692
resolveExtendedCanonicalPath ( p , pragma [ only_bind_into ] ( crate ) , path + "::" + name ) and
693
- v = MkVariantLib ( pragma [ only_bind_into ] ( crate ) , path , name )
693
+ v = MkVariantInLib ( pragma [ only_bind_into ] ( crate ) , path , name )
694
694
)
695
695
}
696
696
697
697
/** Holds if `p` destructs an enum variant `v`. */
698
698
pragma [ nomagic]
699
- private predicate tupleVariantCanonicalDestruction ( TupleStructPat p , VariantLib v ) {
700
- pathResolveToVariantLib ( p , v )
699
+ private predicate tupleVariantCanonicalDestruction ( TupleStructPat p , VariantInLib v ) {
700
+ pathResolveToVariantInLib ( p , v )
701
701
}
702
702
703
703
bindingset [ pos]
704
704
predicate tupleVariantCanonicalDestruction (
705
- TupleStructPat pat , VariantLibTupleFieldContent c , int pos
705
+ TupleStructPat pat , VariantInLibTupleFieldContent c , int pos
706
706
) {
707
- tupleVariantCanonicalDestruction ( pat , c .getVariantLib ( pos ) )
707
+ tupleVariantCanonicalDestruction ( pat , c .getVariantInLib ( pos ) )
708
708
}
709
709
710
710
/** Holds if `ce` constructs an enum value of type `v`. */
711
711
pragma [ nomagic]
712
- private predicate tupleVariantCanonicalConstruction ( CallExpr ce , VariantLib v ) {
713
- pathResolveToVariantLib ( ce .getFunction ( ) .( PathExpr ) , v )
712
+ private predicate tupleVariantCanonicalConstruction ( CallExpr ce , VariantInLib v ) {
713
+ pathResolveToVariantInLib ( ce .getFunction ( ) .( PathExpr ) , v )
714
714
}
715
715
716
716
bindingset [ pos]
717
- predicate tupleVariantCanonicalConstruction ( CallExpr ce , VariantLibTupleFieldContent c , int pos ) {
718
- tupleVariantCanonicalConstruction ( ce , c .getVariantLib ( pos ) )
717
+ predicate tupleVariantCanonicalConstruction ( CallExpr ce , VariantInLibTupleFieldContent c , int pos ) {
718
+ tupleVariantCanonicalConstruction ( ce , c .getVariantInLib ( pos ) )
719
719
}
720
720
}
721
721
722
- class VariantLibTupleFieldContent = VariantLib :: VariantLibTupleFieldContent ;
722
+ class VariantInLibTupleFieldContent = VariantInLib :: VariantInLibTupleFieldContent ;
723
723
724
724
/**
725
725
* A path to a value contained in an object. For example a field name of a struct.
@@ -1087,29 +1087,22 @@ module RustDataFlow implements InputSig<Location> {
1087
1087
node2 .( Node:: FlowSummaryNode ) .getSummaryNode ( ) )
1088
1088
}
1089
1089
1090
- /** Holds if path `p` resolves to struct `s`. */
1091
- private predicate pathResolveToStruct ( PathAstNode p , Struct s ) {
1092
- s = PathResolution:: resolvePath ( p .getPath ( ) )
1093
- }
1094
-
1095
- /** Holds if path `p` resolves to variant `v`. */
1096
- private predicate pathResolveToVariant ( PathAstNode p , Variant v ) {
1097
- v = PathResolution:: resolvePath ( p .getPath ( ) )
1090
+ /** Gets the item that `p` resolves to, if any. */
1091
+ private PathResolution:: ItemNode resolvePath ( PathAstNode p ) {
1092
+ result = PathResolution:: resolvePath ( p .getPath ( ) )
1098
1093
}
1099
1094
1100
1095
/** Holds if `p` destructs an enum variant `v`. */
1101
1096
pragma [ nomagic]
1102
- private predicate tupleVariantDestruction ( TupleStructPat p , Variant v ) {
1103
- pathResolveToVariant ( p , v )
1104
- }
1097
+ private predicate tupleVariantDestruction ( TupleStructPat p , Variant v ) { v = resolvePath ( p ) }
1105
1098
1106
1099
/** Holds if `p` destructs an enum variant `v`. */
1107
1100
pragma [ nomagic]
1108
- private predicate recordVariantDestruction ( RecordPat p , Variant v ) { pathResolveToVariant ( p , v ) }
1101
+ private predicate recordVariantDestruction ( RecordPat p , Variant v ) { v = resolvePath ( p ) }
1109
1102
1110
1103
/** Holds if `p` destructs a struct `s`. */
1111
1104
pragma [ nomagic]
1112
- private predicate structDestruction ( RecordPat p , Struct s ) { pathResolveToStruct ( p , s ) }
1105
+ private predicate structDestruction ( RecordPat p , Struct s ) { s = resolvePath ( p ) }
1113
1106
1114
1107
/**
1115
1108
* Holds if data can flow from `node1` to `node2` via a read of `c`. Thus,
@@ -1124,7 +1117,7 @@ module RustDataFlow implements InputSig<Location> {
1124
1117
|
1125
1118
tupleVariantDestruction ( pat .getPat ( ) , c .( VariantTupleFieldContent ) .getVariant ( pos ) )
1126
1119
or
1127
- VariantLib :: tupleVariantCanonicalDestruction ( pat .getPat ( ) , c , pos )
1120
+ VariantInLib :: tupleVariantCanonicalDestruction ( pat .getPat ( ) , c , pos )
1128
1121
)
1129
1122
or
1130
1123
exists ( TuplePatCfgNode pat , int pos |
@@ -1177,7 +1170,7 @@ module RustDataFlow implements InputSig<Location> {
1177
1170
exists ( TryExprCfgNode try |
1178
1171
node1 .asExpr ( ) = try .getExpr ( ) and
1179
1172
node2 .asExpr ( ) = try and
1180
- c .( VariantLibTupleFieldContent ) . getVariantLib ( 0 ) .getExtendedCanonicalPath ( ) =
1173
+ c .( VariantInLibTupleFieldContent ) . getVariantInLib ( 0 ) .getExtendedCanonicalPath ( ) =
1181
1174
[ "crate::option::Option::Some" , "crate::result::Result::Ok" ]
1182
1175
)
1183
1176
or
@@ -1198,18 +1191,16 @@ module RustDataFlow implements InputSig<Location> {
1198
1191
/** Holds if `ce` constructs an enum value of type `v`. */
1199
1192
pragma [ nomagic]
1200
1193
private predicate tupleVariantConstruction ( CallExpr ce , Variant v ) {
1201
- pathResolveToVariant ( ce .getFunction ( ) .( PathExpr ) , v )
1194
+ v = resolvePath ( ce .getFunction ( ) .( PathExpr ) )
1202
1195
}
1203
1196
1204
1197
/** Holds if `re` constructs an enum value of type `v`. */
1205
1198
pragma [ nomagic]
1206
- private predicate recordVariantConstruction ( RecordExpr re , Variant v ) {
1207
- pathResolveToVariant ( re , v )
1208
- }
1199
+ private predicate recordVariantConstruction ( RecordExpr re , Variant v ) { v = resolvePath ( re ) }
1209
1200
1210
1201
/** Holds if `re` constructs a struct value of type `s`. */
1211
1202
pragma [ nomagic]
1212
- private predicate structConstruction ( RecordExpr re , Struct s ) { pathResolveToStruct ( re , s ) }
1203
+ private predicate structConstruction ( RecordExpr re , Struct s ) { s = resolvePath ( re ) }
1213
1204
1214
1205
private predicate tupleAssignment ( Node node1 , Node node2 , TuplePositionContent c ) {
1215
1206
exists ( AssignmentExprCfgNode assignment , FieldExprCfgNode access |
@@ -1228,7 +1219,7 @@ module RustDataFlow implements InputSig<Location> {
1228
1219
|
1229
1220
tupleVariantConstruction ( call .getCallExpr ( ) , c .( VariantTupleFieldContent ) .getVariant ( pos ) )
1230
1221
or
1231
- VariantLib :: tupleVariantCanonicalConstruction ( call .getCallExpr ( ) , c , pos )
1222
+ VariantInLib :: tupleVariantCanonicalConstruction ( call .getCallExpr ( ) , c , pos )
1232
1223
)
1233
1224
or
1234
1225
exists ( RecordExprCfgNode re , string field |
@@ -1600,9 +1591,7 @@ private module Cached {
1600
1591
newtype TContent =
1601
1592
TVariantTupleFieldContent ( Variant v , int pos ) { exists ( getVariantTupleField ( v , pos ) ) } or
1602
1593
// TODO: Remove once library types are extracted
1603
- TVariantLibTupleFieldContent ( VariantLib:: VariantLib v , int pos ) {
1604
- VariantLib:: variantLibPos ( v , pos )
1605
- } or
1594
+ TVariantInLibTupleFieldContent ( VariantInLib:: VariantInLib v , int pos ) { pos = v .getAPosition ( ) } or
1606
1595
TVariantRecordFieldContent ( Variant v , string field ) { exists ( getVariantRecordField ( v , field ) ) } or
1607
1596
TElementContent ( ) or
1608
1597
TTuplePositionContent ( int pos ) {
0 commit comments