Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a2fb54

Browse files
committed
Python points-to. Track bitwise or-ing of small integer flags.
1 parent 96eaf81 commit 8a2fb54

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

python/ql/src/semmle/python/Flow.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,16 @@ class BinaryExprNode extends ControlFlowNode {
658658
result = this.getNode().getOp()
659659
}
660660

661+
/** Whether left and right are a pair of operands for this binary expression */
662+
predicate operands(ControlFlowNode left, Operator op, ControlFlowNode right) {
663+
exists(BinaryExpr b, Expr eleft, Expr eright |
664+
this.getNode() = b and left.getNode() = eleft and right.getNode() = eright |
665+
eleft = b.getLeft() and eright = b.getRight() and op = b.getOp()
666+
) and
667+
left.getBasicBlock().dominates(this.getBasicBlock()) and
668+
right.getBasicBlock().dominates(this.getBasicBlock())
669+
}
670+
661671
}
662672

663673
/** A control flow node corresponding to a boolean shortcut (and/or) operation */

python/ql/src/semmle/python/pointsto/PointsTo.qll

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,11 +1119,26 @@ module Expressions {
11191119
*/
11201120
pragma [noinline]
11211121
predicate binaryPointsTo(BinaryExprNode b, PointsToContext context, ObjectInternal value, ControlFlowNode origin, ControlFlowNode operand, ObjectInternal opvalue) {
1122-
// TO DO...
1123-
// Track some integer values through `|` and the types of some objects
1124-
operand = b.getAnOperand() and
1125-
PointsToInternal::pointsTo(operand, context, opvalue, _) and
1126-
value = ObjectInternal::unknown() and origin = b
1122+
origin = b and
1123+
exists(ControlFlowNode left, Operator op, ControlFlowNode right |
1124+
b.operands(left, op, right)
1125+
|
1126+
not op instanceof BitOr and
1127+
(operand = left or operand = right) and
1128+
PointsToInternal::pointsTo(operand, context, opvalue, _) and
1129+
value = ObjectInternal::unknown()
1130+
or
1131+
op instanceof BitOr and
1132+
exists(ObjectInternal lobj, ObjectInternal robj |
1133+
PointsToInternal::pointsTo(left, context, lobj, _) and
1134+
PointsToInternal::pointsTo(right, context, robj, _) and
1135+
value = TInt(lobj.intValue().bitOr(robj.intValue()))
1136+
|
1137+
left = operand and opvalue = lobj
1138+
or
1139+
right = operand and opvalue = robj
1140+
)
1141+
)
11271142
}
11281143

11291144
pragma [noinline]

0 commit comments

Comments
 (0)