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

Skip to content

Commit 5a46df2

Browse files
committed
Python: Add ADTs for ints and strings. Add some global data-flow.
1 parent 051683f commit 5a46df2

12 files changed

Lines changed: 1452 additions & 71 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import python
2+
3+
4+
private import semmle.python.objects.TObject
5+
private import semmle.python.objects.ObjectInternal
6+
private import semmle.python.pointsto.PointsTo2
7+
private import semmle.python.pointsto.PointsToContext2
8+
private import semmle.python.types.Builtins
9+
10+
11+
abstract class ClassObjectInternal extends ObjectInternal {
12+
13+
override boolean booleanValue() {
14+
result = true
15+
}
16+
17+
override predicate maybe() { none() }
18+
19+
override predicate isClass() { any() }
20+
21+
override predicate notClass() { none() }
22+
23+
override predicate isComparable() {
24+
any()
25+
}
26+
27+
override predicate notComparable() {
28+
none()
29+
}
30+
31+
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
32+
// TO DO .. Result should (in most cases) be an instance
33+
none()
34+
}
35+
36+
}
37+
38+
class PythonClassObjectInternal extends ClassObjectInternal, TPythonClassObject {
39+
40+
Class getScope() {
41+
exists(ClassDef def |
42+
this = TPythonClassObject(def.getAFlowNode()) and
43+
result = def.getDefinedClass()
44+
)
45+
}
46+
47+
override string toString() {
48+
result = this.getScope().toString()
49+
}
50+
51+
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
52+
exists(DefinitionNode def |
53+
this = TPythonClassObject(def) and
54+
node = def.getValue() and
55+
context.appliesTo(node)
56+
)
57+
}
58+
59+
override ClassDecl getClassDeclaration() {
60+
this = TPythonClassObject(result)
61+
}
62+
63+
override ObjectInternal getClass() {
64+
result = TBuiltinClassObject(Builtin::special("FunctionType"))
65+
}
66+
67+
override Builtin getBuiltin() {
68+
none()
69+
}
70+
71+
override ControlFlowNode getOrigin() {
72+
this = TPythonClassObject(result)
73+
}
74+
75+
override predicate calleeAndOffset(Function scope, int paramOffset) {
76+
exists(PythonFunctionObjectInternal init |
77+
// TO DO... Lookup init...
78+
none() |
79+
init.getScope() = scope and paramOffset = 1
80+
)
81+
}
82+
83+
}
84+
85+
class BuiltinClassObjectInternal extends ClassObjectInternal, TBuiltinClassObject {
86+
87+
override Builtin getBuiltin() {
88+
this = TBuiltinClassObject(result)
89+
}
90+
91+
override string toString() {
92+
result = "builtin class " + this.getBuiltin().getName()
93+
}
94+
95+
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
96+
none()
97+
}
98+
99+
override ClassDecl getClassDeclaration() {
100+
this = TBuiltinClassObject(result)
101+
}
102+
103+
override ObjectInternal getClass() {
104+
result = TBuiltinClassObject(this.getBuiltin().getClass())
105+
}
106+
107+
override ControlFlowNode getOrigin() {
108+
none()
109+
}
110+
111+
}
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
import python
2+
3+
private import semmle.python.objects.TObject
4+
private import semmle.python.objects.ObjectInternal
5+
private import semmle.python.pointsto.PointsTo2
6+
private import semmle.python.pointsto.PointsToContext2
7+
private import semmle.python.types.Builtins
8+
9+
10+
abstract class BooleanObjectInternal extends ObjectInternal {
11+
12+
BooleanObjectInternal() {
13+
this = TTrue() or this = TFalse()
14+
}
15+
16+
override predicate maybe() { none() }
17+
18+
override ClassDecl getClassDeclaration() {
19+
none()
20+
}
21+
22+
override predicate isClass() { none() }
23+
24+
override predicate notClass() { any() }
25+
26+
override ObjectInternal getClass() {
27+
result = TBuiltinClassObject(Builtin::special("bool"))
28+
}
29+
30+
override predicate isComparable() {
31+
any()
32+
}
33+
34+
override predicate notComparable() {
35+
none()
36+
}
37+
38+
override Builtin getBuiltin() {
39+
none()
40+
}
41+
42+
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
43+
// Booleans aren't callable
44+
none()
45+
}
46+
47+
override ControlFlowNode getOrigin() {
48+
none()
49+
}
50+
51+
}
52+
53+
class TrueObjectInternal extends BooleanObjectInternal, TTrue {
54+
55+
override string toString() {
56+
result = "True"
57+
}
58+
59+
override boolean booleanValue() {
60+
result = true
61+
}
62+
63+
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
64+
node.(NameNode).getId() = "True" and context.appliesTo(node)
65+
}
66+
67+
}
68+
69+
class FalseObjectInternal extends BooleanObjectInternal, TFalse {
70+
71+
override string toString() {
72+
result = "False"
73+
}
74+
75+
override boolean booleanValue() {
76+
result = false
77+
}
78+
79+
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
80+
node.(NameNode).getId() = "False" and context.appliesTo(node)
81+
}
82+
83+
}
84+
85+
class NoneObjectInternal extends ObjectInternal, TNone {
86+
87+
override string toString() {
88+
result = "None"
89+
}
90+
91+
override boolean booleanValue() {
92+
result = false
93+
}
94+
95+
override predicate maybe() { none() }
96+
97+
override ClassDecl getClassDeclaration() {
98+
none()
99+
}
100+
101+
override predicate isClass() { none() }
102+
103+
override predicate notClass() { any() }
104+
105+
override ObjectInternal getClass() {
106+
result = TBuiltinClassObject(Builtin::special("NoneType"))
107+
}
108+
109+
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
110+
node.(NameNode).getId() = "None" and context.appliesTo(node)
111+
}
112+
113+
override predicate isComparable() {
114+
any()
115+
}
116+
117+
override predicate notComparable() {
118+
none()
119+
}
120+
121+
override Builtin getBuiltin() {
122+
none()
123+
}
124+
125+
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
126+
// None isn't callable
127+
none()
128+
}
129+
130+
override ControlFlowNode getOrigin() {
131+
none()
132+
}
133+
134+
}
135+
136+
137+
class IntObjectInternal extends ObjectInternal, TInt {
138+
139+
override string toString() {
140+
result = this.intValue().toString()
141+
}
142+
143+
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
144+
context.appliesTo(node) and
145+
node.getNode().(IntegerLiteral).getValue() = this.intValue()
146+
}
147+
148+
override predicate maybe() { none() }
149+
150+
override ClassDecl getClassDeclaration() {
151+
none()
152+
}
153+
154+
override predicate isClass() { none() }
155+
156+
override predicate notClass() { any() }
157+
158+
override ObjectInternal getClass() {
159+
result = TBuiltinClassObject(Builtin::special("int"))
160+
}
161+
162+
override predicate isComparable() {
163+
any()
164+
}
165+
166+
override predicate notComparable() {
167+
none()
168+
}
169+
170+
override Builtin getBuiltin() {
171+
none()
172+
}
173+
174+
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
175+
// ints aren't callable
176+
none()
177+
}
178+
179+
override ControlFlowNode getOrigin() {
180+
none()
181+
}
182+
183+
override int intValue() {
184+
this = TInt(result)
185+
}
186+
187+
override boolean booleanValue() {
188+
this.intValue() = 0 and result = false
189+
or
190+
this.intValue() != 0 and result = true
191+
}
192+
193+
}
194+
195+
196+
class StringObjectInternal extends ObjectInternal, TString {
197+
198+
override string toString() {
199+
result = "'" + this.strValue().toString() + "'"
200+
}
201+
202+
override predicate introduced(ControlFlowNode node, PointsToContext2 context) {
203+
context.appliesTo(node) and
204+
node.getNode().(StrConst).getText() = this.strValue()
205+
}
206+
207+
override predicate maybe() { none() }
208+
209+
override ClassDecl getClassDeclaration() {
210+
none()
211+
}
212+
213+
override predicate isClass() { none() }
214+
215+
override predicate notClass() { any() }
216+
217+
override ObjectInternal getClass() {
218+
result = TBuiltinClassObject(Builtin::special("str"))
219+
}
220+
221+
override predicate isComparable() {
222+
any()
223+
}
224+
225+
override predicate notComparable() {
226+
none()
227+
}
228+
229+
override Builtin getBuiltin() {
230+
none()
231+
}
232+
233+
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
234+
// strings aren't callable
235+
none()
236+
}
237+
238+
override ControlFlowNode getOrigin() {
239+
none()
240+
}
241+
242+
override string strValue() {
243+
this = TString(result)
244+
}
245+
246+
override boolean booleanValue() {
247+
this.strValue() = "" and result = false
248+
or
249+
this.strValue() != "" and result = true
250+
}
251+
252+
}
253+
254+
255+
256+

0 commit comments

Comments
 (0)