Skip to content

Commit 321d510

Browse files
committed
Python: CG trace: Autogenerate BytecodeExpr.qll
Some code I had lying around, just hadn't comitted. Not that useful since most of these have been disabled in 55404ae for now.
1 parent a7bc954 commit 321d510

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

python/tools/recorded-call-graph-metrics/ql/BytecodeExpr.qll

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import python
22

33
abstract class XMLBytecodeExpr extends XMLElement { }
44

5+
class XMLBytecodeConst extends XMLBytecodeExpr {
6+
XMLBytecodeConst() { this.hasName("BytecodeConst") }
7+
8+
string get_value_data_raw() { result = this.getAChild("value").getTextValue() }
9+
}
10+
511
class XMLBytecodeVariableName extends XMLBytecodeExpr {
612
XMLBytecodeVariableName() { this.hasName("BytecodeVariableName") }
713

@@ -16,6 +22,30 @@ class XMLBytecodeAttribute extends XMLBytecodeExpr {
1622
XMLBytecodeExpr get_object_data() { result.getParent() = this.getAChild("object") }
1723
}
1824

25+
class XMLBytecodeSubscript extends XMLBytecodeExpr {
26+
XMLBytecodeSubscript() { this.hasName("BytecodeSubscript") }
27+
28+
XMLBytecodeExpr get_key_data() { result.getParent() = this.getAChild("key") }
29+
30+
XMLBytecodeExpr get_object_data() { result.getParent() = this.getAChild("object") }
31+
}
32+
33+
class XMLBytecodeTuple extends XMLBytecodeExpr {
34+
XMLBytecodeTuple() { this.hasName("BytecodeTuple") }
35+
36+
XMLBytecodeExpr get_elements_data(int index) {
37+
result = this.getAChild("elements").getChild(index)
38+
}
39+
}
40+
41+
class XMLBytecodeList extends XMLBytecodeExpr {
42+
XMLBytecodeList() { this.hasName("BytecodeList") }
43+
44+
XMLBytecodeExpr get_elements_data(int index) {
45+
result = this.getAChild("elements").getChild(index)
46+
}
47+
}
48+
1949
class XMLBytecodeCall extends XMLBytecodeExpr {
2050
XMLBytecodeCall() { this.hasName("BytecodeCall") }
2151

@@ -24,4 +54,20 @@ class XMLBytecodeCall extends XMLBytecodeExpr {
2454

2555
class XMLBytecodeUnknown extends XMLBytecodeExpr {
2656
XMLBytecodeUnknown() { this.hasName("BytecodeUnknown") }
57+
58+
string get_opname_data() { result = this.getAChild("opname").getTextValue() }
59+
}
60+
61+
class XMLBytecodeMakeFunction extends XMLBytecodeExpr {
62+
XMLBytecodeMakeFunction() { this.hasName("BytecodeMakeFunction") }
63+
64+
XMLBytecodeExpr get_qualified_name_data() {
65+
result.getParent() = this.getAChild("qualified_name")
66+
}
67+
}
68+
69+
class XMLSomethingInvolvingScaryBytecodeJump extends XMLBytecodeExpr {
70+
XMLSomethingInvolvingScaryBytecodeJump() { this.hasName("SomethingInvolvingScaryBytecodeJump") }
71+
72+
string get_opname_data() { result = this.getAChild("opname").getTextValue() }
2773
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import dataclasses
2+
from typing import Any, List
3+
4+
from cg_trace.bytecode_reconstructor import BytecodeExpr
5+
6+
PREAMBLE = """\
7+
import python
8+
9+
abstract class XMLBytecodeExpr extends XMLElement { }
10+
"""
11+
12+
CLASS_PREAMBLE = """\
13+
class XML{class_name} extends XMLBytecodeExpr {{
14+
XML{class_name}() {{ this.hasName("{class_name}") }}
15+
"""
16+
17+
CLASS_AFTER = """\
18+
}
19+
"""
20+
21+
ATTR_TEMPLATES = {
22+
str: 'string get_{name}_data() {{ result = this.getAChild("{name}").getTextValue() }}',
23+
int: 'int get_{name}_data() {{ result = this.getAChild("{name}").getTextValue().toInt() }}',
24+
BytecodeExpr: 'XMLBytecodeExpr get_{name}_data() {{ result.getParent() = this.getAChild("{name}") }}',
25+
List[
26+
BytecodeExpr
27+
]: 'XMLBytecodeExpr get_{name}_data(int index) {{ result = this.getAChild("{name}").getChild(index) }}',
28+
Any: 'string get_{name}_data_raw() {{ result = this.getAChild("{name}").getTextValue() }}',
29+
}
30+
31+
if __name__ == "__main__":
32+
33+
print(PREAMBLE)
34+
35+
for sc in BytecodeExpr.__subclasses__():
36+
print(CLASS_PREAMBLE.format(class_name=sc.__name__))
37+
38+
for f in dataclasses.fields(sc):
39+
field_template = ATTR_TEMPLATES.get(f.type)
40+
if field_template:
41+
generated = field_template.format(name=f.name)
42+
print(f" {generated}")
43+
else:
44+
raise Exception("no template for", f.type)
45+
46+
print(CLASS_AFTER)

0 commit comments

Comments
 (0)