Skip to content

Commit b2c38b3

Browse files
authored
Merge pull request #10296 from hvitved/ruby/call-graph-missing-singletons
Ruby: Add missing edges to the call graph for singleton methods
2 parents 66df44f + b197eff commit b2c38b3

File tree

7 files changed

+49
-3
lines changed

7 files changed

+49
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,12 @@ private DataFlow::LocalSourceNode trackModule(Module tp, TypeTracker t) {
432432
resolveConstantReadAccess(result.asExpr().getExpr()) = tp
433433
or
434434
// `self` reference to Module
435-
exists(Scope scope |
436-
scope = result.(SsaSelfDefinitionNode).getSelfScope() and
435+
exists(Scope scope | scope = result.(SsaSelfDefinitionNode).getSelfScope() |
437436
tp = scope.(ModuleBase).getModule() and
438437
not scope instanceof Toplevel // handled in `trackInstance`
438+
or
439+
scope = result.(SsaSelfDefinitionNode).getSelfScope() and
440+
tp = scope.(SingletonMethod).getEnclosingModule().getModule()
439441
)
440442
)
441443
or

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ calls.rb:
7171
# 155| B
7272
#-----| super -> S
7373

74+
# 169| Singletons
75+
#-----| super -> Object
76+
7477
hello.rb:
7578
# 1| EnglishWords
7679

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ getTarget
6161
| calls.rb:162:1:162:5 | call to new | calls.rb:99:5:99:16 | new |
6262
| calls.rb:162:1:162:14 | call to s_method | calls.rb:145:5:147:7 | s_method |
6363
| calls.rb:167:1:167:15 | call to private_on_main | calls.rb:164:1:165:3 | private_on_main |
64+
| calls.rb:171:9:171:24 | call to singleton_b | calls.rb:174:5:176:7 | singleton_b |
65+
| calls.rb:175:9:175:24 | call to singleton_c | calls.rb:178:5:179:7 | singleton_c |
66+
| calls.rb:182:9:182:24 | call to singleton_a | calls.rb:170:5:172:7 | singleton_a |
67+
| calls.rb:186:1:186:22 | call to singleton_a | calls.rb:170:5:172:7 | singleton_a |
6468
| hello.rb:12:5:12:24 | call to include | calls.rb:92:5:92:20 | include |
6569
| hello.rb:14:16:14:20 | call to hello | hello.rb:2:5:4:7 | hello |
6670
| hello.rb:20:16:20:20 | call to super | hello.rb:13:5:15:7 | message |

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,22 @@ def private_on_main
165165
end
166166

167167
private_on_main
168+
169+
class Singletons
170+
def self.singleton_a
171+
self.singleton_b
172+
end
173+
174+
def self.singleton_b
175+
self.singleton_c
176+
end
177+
178+
def self.singleton_c
179+
end
180+
181+
def self.singleton_d
182+
self.singleton_a
183+
end
184+
end
185+
186+
Singletons.singleton_a

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ lookupMethod
169169
| calls.rb:155:1:158:3 | B | puts | calls.rb:87:5:87:17 | puts |
170170
| calls.rb:155:1:158:3 | B | s_method | calls.rb:145:5:147:7 | s_method |
171171
| calls.rb:155:1:158:3 | B | to_s | calls.rb:156:5:157:7 | to_s |
172+
| calls.rb:169:1:184:3 | Singletons | call_block | calls.rb:67:1:69:3 | call_block |
173+
| calls.rb:169:1:184:3 | Singletons | foo | calls.rb:1:1:3:3 | foo |
174+
| calls.rb:169:1:184:3 | Singletons | foo | calls.rb:71:1:75:3 | foo |
175+
| calls.rb:169:1:184:3 | Singletons | funny | calls.rb:119:1:121:3 | funny |
176+
| calls.rb:169:1:184:3 | Singletons | indirect | calls.rb:137:1:139:3 | indirect |
177+
| calls.rb:169:1:184:3 | Singletons | new | calls.rb:99:5:99:16 | new |
178+
| calls.rb:169:1:184:3 | Singletons | optional_arg | calls.rb:62:1:65:3 | optional_arg |
179+
| calls.rb:169:1:184:3 | Singletons | private_on_main | calls.rb:164:1:165:3 | private_on_main |
180+
| calls.rb:169:1:184:3 | Singletons | puts | calls.rb:87:5:87:17 | puts |
181+
| calls.rb:169:1:184:3 | Singletons | to_s | calls.rb:151:5:152:7 | to_s |
172182
| file://:0:0:0:0 | Class | include | calls.rb:92:5:92:20 | include |
173183
| file://:0:0:0:0 | Class | module_eval | calls.rb:91:5:91:24 | module_eval |
174184
| file://:0:0:0:0 | Class | new | calls.rb:99:5:99:16 | new |

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ getModule
1212
| calls.rb:144:1:148:3 | S |
1313
| calls.rb:150:1:153:3 | A |
1414
| calls.rb:155:1:158:3 | B |
15+
| calls.rb:169:1:184:3 | Singletons |
1516
| file://:0:0:0:0 | BasicObject |
1617
| file://:0:0:0:0 | Class |
1718
| file://:0:0:0:0 | Complex |
@@ -68,7 +69,7 @@ getADeclaration
6869
| calls.rb:82:1:84:3 | String | calls.rb:82:1:84:3 | String |
6970
| calls.rb:86:1:88:3 | Kernel | calls.rb:86:1:88:3 | Kernel |
7071
| calls.rb:90:1:95:3 | Module | calls.rb:90:1:95:3 | Module |
71-
| calls.rb:97:1:100:3 | Object | calls.rb:1:1:167:16 | calls.rb |
72+
| calls.rb:97:1:100:3 | Object | calls.rb:1:1:186:22 | calls.rb |
7273
| calls.rb:97:1:100:3 | Object | calls.rb:97:1:100:3 | Object |
7374
| calls.rb:97:1:100:3 | Object | hello.rb:1:1:22:3 | hello.rb |
7475
| calls.rb:97:1:100:3 | Object | modules.rb:1:1:121:4 | modules.rb |
@@ -80,6 +81,7 @@ getADeclaration
8081
| calls.rb:150:1:153:3 | A | calls.rb:150:1:153:3 | A |
8182
| calls.rb:150:1:153:3 | A | modules_rec.rb:7:1:9:3 | A |
8283
| calls.rb:155:1:158:3 | B | calls.rb:155:1:158:3 | B |
84+
| calls.rb:169:1:184:3 | Singletons | calls.rb:169:1:184:3 | Singletons |
8385
| hello.rb:1:1:8:3 | EnglishWords | hello.rb:1:1:8:3 | EnglishWords |
8486
| hello.rb:11:1:16:3 | Greeting | hello.rb:11:1:16:3 | Greeting |
8587
| hello.rb:18:1:22:3 | HelloWorld | hello.rb:18:1:22:3 | HelloWorld |
@@ -133,6 +135,7 @@ getSuperClass
133135
| calls.rb:150:1:153:3 | A | calls.rb:144:1:148:3 | S |
134136
| calls.rb:150:1:153:3 | A | calls.rb:155:1:158:3 | B |
135137
| calls.rb:155:1:158:3 | B | calls.rb:144:1:148:3 | S |
138+
| calls.rb:169:1:184:3 | Singletons | calls.rb:97:1:100:3 | Object |
136139
| file://:0:0:0:0 | Class | calls.rb:90:1:95:3 | Module |
137140
| file://:0:0:0:0 | Complex | file://:0:0:0:0 | Numeric |
138141
| file://:0:0:0:0 | FalseClass | calls.rb:97:1:100:3 | Object |
@@ -184,6 +187,7 @@ resolveConstantReadAccess
184187
| calls.rb:160:1:160:1 | S | S |
185188
| calls.rb:161:1:161:1 | A | A |
186189
| calls.rb:162:1:162:1 | B | B |
190+
| calls.rb:186:1:186:10 | Singletons | Singletons |
187191
| hello.rb:12:13:12:24 | EnglishWords | EnglishWords |
188192
| hello.rb:18:20:18:27 | Greeting | Greeting |
189193
| modules.rb:48:8:48:10 | Foo | Foo |
@@ -228,6 +232,7 @@ resolveConstantWriteAccess
228232
| calls.rb:144:1:148:3 | S | S |
229233
| calls.rb:150:1:153:3 | A | A |
230234
| calls.rb:155:1:158:3 | B | B |
235+
| calls.rb:169:1:184:3 | Singletons | Singletons |
231236
| hello.rb:1:1:8:3 | EnglishWords | EnglishWords |
232237
| hello.rb:11:1:16:3 | Greeting | Greeting |
233238
| hello.rb:18:1:22:3 | HelloWorld | HelloWorld |

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ calls.rb:
6767
# 155| B
6868
#-----| -> S
6969

70+
# 169| Singletons
71+
#-----| -> Object
72+
7073
hello.rb:
7174
# 1| EnglishWords
7275

0 commit comments

Comments
 (0)