Skip to content

Commit b46983a

Browse files
authored
Merge pull request github#13068 from hvitved/ruby/type-tracking-flow-through
Ruby: Include `self` parameters in type tracking flow-through logic
2 parents 867bdcf + 826b621 commit b46983a

File tree

8 files changed

+1809
-1698
lines changed

8 files changed

+1809
-1698
lines changed

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPrivate.qll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,12 @@ private class NewCall extends DataFlowCall {
940940
abstract class ReturningNode extends Node {
941941
/** Gets the kind of this return node. */
942942
abstract ReturnKind getKind();
943+
944+
pragma[nomagic]
945+
predicate hasKind(ReturnKind kind, CfgScope scope) {
946+
kind = this.getKind() and
947+
scope = this.(NodeImpl).getCfgScope()
948+
}
943949
}
944950

945951
/** A data-flow node that represents a value returned by a callable. */
@@ -1060,10 +1066,8 @@ private module ReturnNodes {
10601066
SynthReturnNode() { this = TSynthReturnNode(scope, kind) }
10611067

10621068
/** Gets a syntactic return node that flows into this synthetic node. */
1063-
ReturningNode getAnInput() {
1064-
result.(NodeImpl).getCfgScope() = scope and
1065-
result.getKind() = kind
1066-
}
1069+
pragma[nomagic]
1070+
ReturningNode getAnInput() { result.hasKind(kind, scope) }
10671071

10681072
override ReturnKind getKind() { result = kind }
10691073

ruby/ql/lib/codeql/ruby/typetracking/TypeTrackerSpecific.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ predicate jumpStep = DataFlowPrivate::jumpStep/2;
8080
pragma[nomagic]
8181
private predicate flowThrough(DataFlowPublic::ParameterNode param) {
8282
exists(DataFlowPrivate::ReturningNode returnNode, DataFlowDispatch::ReturnKind rk |
83-
DataFlowPrivate::LocalFlow::getParameterDefNode(param.getParameter())
84-
.(TypeTrackingNode)
85-
.flowsTo(returnNode) and
86-
rk = returnNode.getKind()
83+
param.flowsTo(returnNode) and
84+
returnNode.hasKind(rk, param.(DataFlowPrivate::NodeImpl).getCfgScope())
8785
|
8886
rk instanceof DataFlowDispatch::NormalReturnKind
8987
or

ruby/ql/test/library-tests/modules/ancestors.expected

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,64 +94,64 @@ calls.rb:
9494
# 325| C1
9595
#-----| super -> Object
9696

97-
# 331| C2
97+
# 335| C2
9898
#-----| super -> C1
9999

100-
# 337| C3
100+
# 341| C3
101101
#-----| super -> C2
102102

103-
# 377| SingletonOverride1
103+
# 385| SingletonOverride1
104104
#-----| super -> Object
105105

106-
# 412| SingletonOverride2
106+
# 420| SingletonOverride2
107107
#-----| super -> SingletonOverride1
108108

109-
# 433| ConditionalInstanceMethods
109+
# 441| ConditionalInstanceMethods
110110
#-----| super -> Object
111111

112-
# 496| ExtendSingletonMethod
112+
# 504| ExtendSingletonMethod
113113

114-
# 506| ExtendSingletonMethod2
114+
# 514| ExtendSingletonMethod2
115115

116-
# 512| ExtendSingletonMethod3
116+
# 520| ExtendSingletonMethod3
117117

118-
# 525| ProtectedMethodInModule
118+
# 533| ProtectedMethodInModule
119119

120-
# 531| ProtectedMethods
120+
# 539| ProtectedMethods
121121
#-----| super -> Object
122122
#-----| include -> ProtectedMethodInModule
123123

124-
# 550| ProtectedMethodsSub
124+
# 558| ProtectedMethodsSub
125125
#-----| super -> ProtectedMethods
126126

127-
# 564| SingletonUpCall_Base
127+
# 572| SingletonUpCall_Base
128128
#-----| super -> Object
129129

130-
# 568| SingletonUpCall_Sub
130+
# 576| SingletonUpCall_Sub
131131
#-----| super -> SingletonUpCall_Base
132132

133-
# 576| SingletonUpCall_SubSub
133+
# 584| SingletonUpCall_SubSub
134134
#-----| super -> SingletonUpCall_Sub
135135

136-
# 583| SingletonA
136+
# 591| SingletonA
137137
#-----| super -> Object
138138

139-
# 596| SingletonB
139+
# 604| SingletonB
140140
#-----| super -> SingletonA
141141

142-
# 605| SingletonC
142+
# 613| SingletonC
143143
#-----| super -> SingletonA
144144

145-
# 618| Included
145+
# 626| Included
146146

147-
# 626| IncludesIncluded
147+
# 634| IncludesIncluded
148148
#-----| super -> Object
149149
#-----| include -> Included
150150

151-
# 633| CustomNew1
151+
# 641| CustomNew1
152152
#-----| super -> Object
153153

154-
# 641| CustomNew2
154+
# 649| CustomNew2
155155
#-----| super -> Object
156156

157157
hello.rb:

ruby/ql/test/library-tests/modules/callgraph.expected

Lines changed: 224 additions & 207 deletions
Large diffs are not rendered by default.

ruby/ql/test/library-tests/modules/calls.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ class C1
326326
def instance
327327
puts "C1#instance"
328328
end
329+
330+
def return_self
331+
self
332+
end
329333
end
330334

331335
class C2 < C1
@@ -360,10 +364,13 @@ def pattern_dispatch x
360364

361365
c1 = C1.new
362366
c1.instance
367+
363368
pattern_dispatch (C1.new)
364369
pattern_dispatch (C2.new)
365370
pattern_dispatch (C3.new)
366371

372+
C3.new.return_self.instance
373+
367374
def add_singleton x
368375
def x.instance
369376
puts "instance_on x"
@@ -373,6 +380,7 @@ def x.instance
373380
c3 = C1.new
374381
add_singleton c3
375382
c3.instance
383+
c3.return_self.instance
376384

377385
class SingletonOverride1
378386
class << self
@@ -649,3 +657,11 @@ def instance
649657
end
650658

651659
CustomNew2.new.instance
660+
661+
def capture_parameter x
662+
[0,1,2].each do
663+
x
664+
end
665+
end
666+
667+
(capture_parameter C1.new).instance # NoMethodError

ruby/ql/test/library-tests/modules/methods.expected

Lines changed: 510 additions & 464 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)