Skip to content

Commit 8c22442

Browse files
committed
C++: Better 'getType' for global variable nodes.
1 parent cd24405 commit 8c22442

4 files changed

Lines changed: 60 additions & 17 deletions

File tree

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,22 @@ predicate jumpStep(Node n1, Node n2) {
516516
exists(Cpp::GlobalOrNamespaceVariable v |
517517
exists(Ssa::GlobalUse globalUse |
518518
v = globalUse.getVariable() and
519-
n1.(FinalGlobalValue).getGlobalUse() = globalUse and
520-
v = n2.asVariable(globalUse.getIndirectionIndex())
519+
n1.(FinalGlobalValue).getGlobalUse() = globalUse
520+
|
521+
globalUse.getIndirectionIndex() = 1 and
522+
v = n2.asVariable()
523+
or
524+
v = n2.asIndirectVariable(globalUse.getIndirectionIndex())
521525
)
522526
or
523527
exists(Ssa::GlobalDef globalDef |
524528
v = globalDef.getVariable() and
525-
v = n1.asVariable(globalDef.getIndirectionIndex()) and
526529
n2.(InitialGlobalValue).getGlobalDef() = globalDef
530+
|
531+
globalDef.getIndirectionIndex() = 1 and
532+
v = n1.asVariable()
533+
or
534+
v = n1.asIndirectVariable(globalDef.getIndirectionIndex())
527535
)
528536
)
529537
}

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,21 @@ class Node extends TIRDataFlowNode {
268268
* Gets the variable corresponding to this node, if any. This can be used for
269269
* modeling flow in and out of global variables.
270270
*/
271-
Variable asVariable() { result = this.asVariable(1) }
271+
Variable asVariable() { this = TVariableNode(result, 1) }
272272

273-
Variable asVariable(int indirectionIndex) {
274-
exists(VariableNode varNode | this = varNode |
275-
varNode.getVariable() = result and
276-
varNode.getIndirectionIndex() = indirectionIndex
277-
)
273+
/**
274+
* Gets the `indirectionIndex`'th indirection of this node's underlying variable, if any.
275+
*
276+
* This can be used for modeling flow in and out of global variables.
277+
*/
278+
Variable asIndirectVariable(int indirectionIndex) {
279+
indirectionIndex > 1 and
280+
this = TVariableNode(result, indirectionIndex)
278281
}
279282

283+
/** Gets an indirection of this node's underlying variable, if any. */
284+
Variable asIndirectVariable() { result = this.asIndirectVariable(_) }
285+
280286
/**
281287
* Gets the expression that is partially defined by this node, if any.
282288
*
@@ -510,11 +516,16 @@ class FinalGlobalValue extends Node, TFinalGlobalValue {
510516

511517
override Declaration getFunction() { result = globalUse.getIRFunction().getFunction() }
512518

513-
override DataFlowType getType() { result instanceof VoidType } // TODO
519+
override DataFlowType getType() {
520+
exists(int indirectionIndex |
521+
indirectionIndex = globalUse.getIndirectionIndex() and
522+
result = getTypeImpl(globalUse.getUnspecifiedType(), indirectionIndex - 1)
523+
)
524+
}
514525

515526
final override Location getLocationImpl() { result = globalUse.getLocation() }
516527

517-
override string toStringImpl() { result = "FinalGlobalValue" }
528+
override string toStringImpl() { result = globalUse.toString() }
518529
}
519530

520531
class InitialGlobalValue extends Node, TInitialGlobalValue {
@@ -528,11 +539,16 @@ class InitialGlobalValue extends Node, TInitialGlobalValue {
528539

529540
override Declaration getFunction() { result = globalDef.getIRFunction().getFunction() }
530541

531-
override DataFlowType getType() { result instanceof VoidType } // TODO
542+
override DataFlowType getType() {
543+
exists(int indirectionIndex |
544+
indirectionIndex = globalDef.getIndirectionIndex() and
545+
result = getTypeImpl(globalDef.getUnspecifiedType(), indirectionIndex)
546+
)
547+
}
532548

533549
final override Location getLocationImpl() { result = globalDef.getLocation() }
534550

535-
override string toStringImpl() { result = "InitialGlobalValue" }
551+
override string toStringImpl() { result = globalDef.toString() }
536552
}
537553

538554
/**
@@ -1173,7 +1189,9 @@ class VariableNode extends Node, TVariableNode {
11731189
result = v
11741190
}
11751191

1176-
override DataFlowType getType() { result = v.getType() }
1192+
override DataFlowType getType() {
1193+
result = getTypeImpl(v.getUnspecifiedType(), indirectionIndex - 1)
1194+
}
11771195

11781196
final override Location getLocationImpl() {
11791197
// Certain variables (such as parameters) can have multiple locations.
@@ -1185,7 +1203,9 @@ class VariableNode extends Node, TVariableNode {
11851203
result instanceof UnknownDefaultLocation
11861204
}
11871205

1188-
override string toStringImpl() { result = v.toString() }
1206+
override string toStringImpl() {
1207+
if indirectionIndex = 1 then result = v.toString() else result = v.toString() + " indirection"
1208+
}
11891209
}
11901210

11911211
/**

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,13 @@ class GlobalUse extends TGlobalUse {
359359

360360
final Cpp::Location getLocation() { result = f.getLocation() }
361361

362-
string toString() { result = global.toString() + " [final value from " + f.toString() + "]" }
362+
string toString() {
363+
if indirectionIndex = 1
364+
then result = global.toString()
365+
else result = global.toString() + " indirection"
366+
}
367+
368+
Type getUnspecifiedType() { result = global.getUnspecifiedType() }
363369
}
364370

365371
class GlobalDef extends TGlobalDef {
@@ -391,7 +397,13 @@ class GlobalDef extends TGlobalDef {
391397

392398
final Cpp::Location getLocation() { result = f.getLocation() }
393399

394-
string toString() { result = global.toString() + " [initial value in " + f.toString() + "]" }
400+
string toString() {
401+
if indirectionIndex = 0
402+
then result = global.toString()
403+
else result = global.toString() + " indirection"
404+
}
405+
406+
Type getUnspecifiedType() { result = global.getUnspecifiedType() }
395407
}
396408

397409
/**

cpp/ql/src/Likely Bugs/Format/NonConstantFormat.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ predicate isSanitizerNode(DataFlow::Node node) {
122122
or
123123
not exists(node.asIndirectExpr()) and
124124
not exists(node.asDefiningArgument()) and
125+
not exists(node.asIndirectVariable()) and
126+
not node instanceof DataFlow::InitialGlobalValue and
127+
not node instanceof DataFlow::FinalGlobalValue and
125128
cannotContainString(node.getType(), false)
126129
}
127130

0 commit comments

Comments
 (0)