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

Skip to content

Commit e3ed8c6

Browse files
committed
Python points-to: Simplify handling of booleans and comparisons.
1 parent 84c9866 commit e3ed8c6

12 files changed

Lines changed: 139 additions & 167 deletions

File tree

python/ql/src/semmle/python/objects/Callables.qll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ abstract class CallableObjectInternal extends ObjectInternal {
2626
result = true
2727
}
2828

29-
/** Holds if this object may be true or false when evaluated as a bool */
30-
override predicate maybe() { none() }
31-
3229
override ClassDecl getClassDeclaration() {
3330
none()
3431
}

python/ql/src/semmle/python/objects/Classes.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ abstract class ClassObjectInternal extends ObjectInternal {
1515
result = true
1616
}
1717

18-
override predicate maybe() { none() }
19-
2018
override boolean isClass() { result = true }
2119

2220
override int intValue() {

python/ql/src/semmle/python/objects/Constants.qll

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ abstract class BooleanObjectInternal extends ObjectInternal {
1313
this = TTrue() or this = TFalse()
1414
}
1515

16-
override predicate maybe() { none() }
17-
1816
override ClassDecl getClassDeclaration() {
1917
none()
2018
}
@@ -111,8 +109,6 @@ class NoneObjectInternal extends ObjectInternal, TNone {
111109
result = false
112110
}
113111

114-
override predicate maybe() { none() }
115-
116112
override ClassDecl getClassDeclaration() {
117113
none()
118114
}
@@ -175,8 +171,6 @@ class IntObjectInternal extends ObjectInternal, TInt {
175171
node.getNode().(IntegerLiteral).getValue() = this.intValue()
176172
}
177173

178-
override predicate maybe() { none() }
179-
180174
override ClassDecl getClassDeclaration() {
181175
none()
182176
}
@@ -233,16 +227,14 @@ class IntObjectInternal extends ObjectInternal, TInt {
233227
class StringObjectInternal extends ObjectInternal, TString {
234228

235229
override string toString() {
236-
result = "'" + this.strValue().toString() + "'"
230+
result = "'" + this.strValue() + "'"
237231
}
238232

239233
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
240234
context.appliesTo(node) and
241235
node.getNode().(StrConst).getText() = this.strValue()
242236
}
243237

244-
override predicate maybe() { none() }
245-
246238
override ClassDecl getClassDeclaration() {
247239
none()
248240
}
@@ -252,7 +244,7 @@ class StringObjectInternal extends ObjectInternal, TString {
252244
override boolean isComparable() { result = true }
253245

254246
override ObjectInternal getClass() {
255-
result = TBuiltinClassObject(Builtin::special("str"))
247+
result = TBuiltinClassObject(Builtin::special("unicode"))
256248
}
257249

258250
override Builtin getBuiltin() {

python/ql/src/semmle/python/objects/Instances.qll

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@ private import semmle.python.types.Builtins
1010
class SpecificInstanceInternal extends TSpecificInstance, ObjectInternal {
1111

1212
override string toString() {
13-
result = "instance of " + this.getClass().(ClassObjectInternal).getName()
13+
result = this.getOrigin().toString()
1414
}
1515

1616
/** The boolean value of this object, if it has one */
1717
override boolean booleanValue() {
18-
//this.getClass().instancesAlways(result)
19-
none()
20-
}
21-
22-
/** Holds if this object may be true or false when evaluated as a bool */
23-
override predicate maybe() {
24-
// this.getClass().instancesMaybe()
25-
any()
18+
//result = this.getClass().instancesBooleanValue()
19+
result = maybe()
2620
}
2721

2822
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
@@ -97,14 +91,8 @@ class UnknownInstanceInternal extends TUnknownInstance, ObjectInternal {
9791

9892
/** The boolean value of this object, if it has one */
9993
override boolean booleanValue() {
100-
//this.getClass().instancesAlways(result)
101-
none()
102-
}
103-
104-
/** Holds if this object may be true or false when evaluated as a bool */
105-
override predicate maybe() {
106-
// this.getClass().instancesMaybe()
107-
any()
94+
//result = this.getClass().instancesBooleanValue()
95+
result = maybe()
10896
}
10997

11098
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {

python/ql/src/semmle/python/objects/Modules.qll

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ abstract class ModuleObjectInternal extends ObjectInternal {
2525

2626
override boolean isComparable() { result = true }
2727

28+
override boolean booleanValue() {
29+
result = true
30+
}
31+
2832
}
2933

3034
class BuiltinModuleObjectInternal extends ModuleObjectInternal, TBuiltinModuleObject {
@@ -45,12 +49,6 @@ class BuiltinModuleObjectInternal extends ModuleObjectInternal, TBuiltinModuleOb
4549
none()
4650
}
4751

48-
override boolean booleanValue() {
49-
result = true
50-
}
51-
52-
override predicate maybe() { none() }
53-
5452
override ClassDecl getClassDeclaration() {
5553
none()
5654
}
@@ -106,12 +104,6 @@ class PackageObjectInternal extends ModuleObjectInternal, TPackageObject {
106104
none()
107105
}
108106

109-
override boolean booleanValue() {
110-
result = true
111-
}
112-
113-
override predicate maybe() { none() }
114-
115107
override ClassDecl getClassDeclaration() {
116108
none()
117109
}
@@ -139,13 +131,6 @@ class PackageObjectInternal extends ModuleObjectInternal, TPackageObject {
139131
result.getName() = this.getName() + "." + name
140132
}
141133

142-
override predicate hasLocationInfo(string fp, int bl, int bc, int el, int ec) {
143-
this.getOrigin().getLocation().hasLocationInfo(fp, bl, bc, el, ec)
144-
or
145-
this.hasNoInitModule() and fp = this.getFolder().getAbsolutePath() and
146-
bl = 0 and bc = 0 and el = 0 and ec = 0
147-
}
148-
149134
override int intValue() {
150135
none()
151136
}
@@ -212,12 +197,6 @@ class PythonModuleObjectInternal extends ModuleObjectInternal, TPythonModule {
212197
none()
213198
}
214199

215-
override boolean booleanValue() {
216-
result = true
217-
}
218-
219-
override predicate maybe() { none() }
220-
221200
override ClassDecl getClassDeclaration() {
222201
none()
223202
}

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import python
22
private import TObject
3-
private import ObjectInternal
3+
private import semmle.python.objects.ObjectInternal
44
private import semmle.python.pointsto.PointsTo2
55
private import semmle.python.pointsto.PointsToContext2
66

@@ -22,4 +22,17 @@ class Value extends TObject {
2222
result = this.(ObjectInternal).getClass()
2323
}
2424

25+
CallNode getACall() {
26+
PointsTo2::points_to(result.getFunction(), _, this, _)
27+
or
28+
exists(BoundMethodObjectInternal bm |
29+
PointsTo2::points_to(result.getFunction(), _, bm, _) and
30+
bm.getFunction() = this
31+
)
32+
}
33+
34+
Value attr(string name) {
35+
this.(ObjectInternal).attribute(name, result, _)
36+
}
37+
2538
}

python/ql/src/semmle/python/objects/ObjectInternal.qll

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ class ObjectInternal extends TObject {
1414

1515
abstract string toString();
1616

17-
/** The boolean value of this object, if it has one */
17+
/** The boolean value of this object, this may be both
18+
* true and false if the "object" represents a set of possible objects. */
1819
abstract boolean booleanValue();
1920

20-
/** Holds if this object may be true or false when evaluated as a bool */
21-
abstract predicate maybe();
22-
2321
abstract predicate introduced(ControlFlowNode node, PointsToContext2 context);
2422

2523
/** Gets the class declaration for this object, if it is a declared class. */
@@ -54,10 +52,6 @@ class ObjectInternal extends TObject {
5452
*/
5553
abstract predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin);
5654

57-
predicate hasLocationInfo(string fp, int bl, int bc, int el, int ec) {
58-
this.getOrigin().getLocation().hasLocationInfo(fp, bl, bc, el, ec)
59-
}
60-
6155
/** The integer value of things that have integer values.
6256
* That is, ints and bools.
6357
*/
@@ -92,13 +86,8 @@ class BuiltinOpaqueObjectInternal extends ObjectInternal, TBuiltinOpaqueObject {
9286
}
9387

9488
override boolean booleanValue() {
95-
// TO DO ... Depends on class. `this.getClass().instancesAlways(result)`
96-
none()
97-
}
98-
99-
override predicate maybe() {
100-
// TO DO ... Depends on class. `this.getClass().instancesMaybe()`
101-
any()
89+
// TO DO ... Depends on class. `result = this.getClass().instancesBooleanValue()`
90+
result = maybe()
10291
}
10392

10493
override ClassDecl getClassDeclaration() {
@@ -155,11 +144,9 @@ class UnknownInternal extends ObjectInternal, TUnknown {
155144
}
156145

157146
override boolean booleanValue() {
158-
none()
147+
result = maybe()
159148
}
160149

161-
override predicate maybe() { any() }
162-
163150
override ClassDecl getClassDeclaration() {
164151
none()
165152
}
@@ -219,8 +206,6 @@ class UndefinedInternal extends ObjectInternal, TUndefined {
219206
none()
220207
}
221208

222-
override predicate maybe() { none() }
223-
224209
override ClassDecl getClassDeclaration() {
225210
none()
226211
}
@@ -328,3 +313,7 @@ module ObjectInternal {
328313
}
329314
}
330315

316+
/** Helper for boolean predicates returning both `true` and `false` */
317+
boolean maybe() {
318+
result = true or result = false
319+
}

python/ql/src/semmle/python/objects/TObject.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ newtype TObject =
6464
or
6565
TString(string s) {
6666
// Any string explicitly mentioned in the source code.
67-
exists(StrConst str |
68-
s = str.getText()
69-
)
67+
s = any(StrConst str).getText()
7068
or
7169
// Any string from the library put in the DB by the extractor.
7270
s = any(Builtin b).strValue()

0 commit comments

Comments
 (0)