You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/language/learn-ql/python/control-flow.rst
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
Tutorial: Control flow analysis
2
2
===============================
3
3
4
-
In order to analyze the `Control-flow graph <http://en.wikipedia.org/wiki/Control_flow_graph>`__ of a ``Scope`` we can use the two QL classes ``ControlFlowNode`` and ``BasicBlock``. These classes allow you to ask such questions as "can you reach point A from point B?" or "Is it possible to reach point B *without* going through point A?". To report results we use the class ``AstNode``, which represents a syntactic element and corresponds to the source code - allowing the results of the query to be more easily understood.
4
+
To analyze the `Control-flow graph <http://en.wikipedia.org/wiki/Control_flow_graph>`__ of a ``Scope`` we can use the two CodeQL classes ``ControlFlowNode`` and ``BasicBlock``. These classes allow you to ask such questions as "can you reach point A from point B?" or "Is it possible to reach point B *without* going through point A?". To report results we use the class ``AstNode``, which represents a syntactic element and corresponds to the source code - allowing the results of the query to be more easily understood.
5
5
6
6
The ``ControlFlowNode`` class
7
7
-----------------------------
8
8
9
-
The ``ControlFlowNode`` class represents nodes in the control flow graph. There is a one-to-many relation between AST nodes and control flow nodes. Each syntactic element, the ``AstNode,`` maps to zero, one or many ``ControlFlowNode`` classes, but each ControlFlowNode maps to exactly one ``AstNode``.
9
+
The ``ControlFlowNode`` class represents nodes in the control flow graph. There is a one-to-many relation between AST nodes and control flow nodes. Each syntactic element, the ``AstNode,`` maps to zero, one, or many ``ControlFlowNode`` classes, but each ``ControlFlowNode`` maps to exactly one ``AstNode``.
10
10
11
11
To show why this complex relation is required consider the following Python code:
12
12
@@ -21,7 +21,7 @@ To show why this complex relation is required consider the following Python code
21
21
22
22
There are many paths through the above code. There are three different paths through the call to ``close_resource();`` one normal path, one path that breaks out of the loop, and one path where an exception is raised by ``might_raise()``. (An annotated flow graph can be seen :doc:`here <control-flow-graph>`.)
23
23
24
-
The simplest use of the ``ControlFlowNode`` and ``AstNode`` classes is to find unreachable code. There is one ``ControlFlowNode`` per path through any ``AstNode`` and any ``AstNode`` that is unreachable has no paths flowing through it; therefore any ``AstNode`` without a corresponding ``ControlFlowNode`` is unreachable.
24
+
The simplest use of the ``ControlFlowNode`` and ``AstNode`` classes is to find unreachable code. There is one ``ControlFlowNode`` per path through any ``AstNode`` and any ``AstNode`` that is unreachable has no paths flowing through it. Therefore, any ``AstNode`` without a corresponding ``ControlFlowNode`` is unreachable.
25
25
26
26
**Unreachable AST nodes**
27
27
@@ -103,5 +103,5 @@ Combining these conditions we get:
103
103
What next?
104
104
----------
105
105
106
-
- Experiment with the worked examples in the QL for Python tutorial topic::doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
106
+
- Experiment with the worked examples in the tutorial topic :doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
107
107
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/python/functions.rst
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
Tutorial: Functions
2
2
===================
3
3
4
-
This example uses the standard QL class ``Function`` (see :doc:`Introducing the Python libraries <introduce-libraries-python>`).
4
+
This example uses the standard CodeQL class ``Function`` (see :doc:`Introducing the Python libraries <introduce-libraries-python>`).
5
5
6
6
Finding all functions called "get..."
7
7
-------------------------------------
8
8
9
9
In this example we look for all the "getters" in a program. Programmers moving to Python from Java are often tempted to write lots of getter and setter methods, rather than use properties. We might want to find those methods.
10
10
11
-
Using the member predicate ``Function.getName()``, we can list all of the getter functions in a snapshot:
11
+
Using the member predicate ``Function.getName()``, we can list all of the getter functions in a database:
12
12
13
13
Tip
14
14
@@ -77,5 +77,5 @@ In a later tutorial we will see how to use the type-inference library to find ca
77
77
What next?
78
78
----------
79
79
80
-
- Experiment with the worked examples in the QL for Python tutorial topics: :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, :doc:`Points-to analysis and type inference <pointsto-type-infer>`.
80
+
- Experiment with the worked examples in the following tutorial topics: :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, and:doc:`Points-to analysis and type inference <pointsto-type-infer>`.
81
81
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/python/introduce-libraries-python.rst
+18-22Lines changed: 18 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,39 +1,35 @@
1
-
Introducing the QL libraries for Python
2
-
=======================================
1
+
Introducing the CodeQL libraries for Python
2
+
===========================================
3
3
4
-
These libraries have been created to help you analyze Python code, providing an object-oriented layer on top of the raw data in the snapshot database. They are written in standard QL.
5
-
6
-
The QL libraries all have a ``.qll`` extension, to signify that they contain QL library code but no actual queries. Each file contains a QL class or hierarchy of classes.
7
-
8
-
You can include all of the standard libraries by beginning each query with this statement:
4
+
There is an extensive library for analyzing CodeQL databases extracted from Python projects. The classes in this library present the data from a database in an object-oriented form and provide abstractions and predicates to help you with common analysis tasks. The library is implemented as a set of QL modules, that is, files with the extension ``.qll``. The module ``python.qll`` imports all the core Python library modules, so you can include the complete library by beginning your query with:
9
5
10
6
.. code-block:: ql
11
7
12
8
import python
13
9
14
-
The rest of this tutorial summarizes the contents of the standard QL libraries. We recommend that you read this and then work through the practical examples in the Python tutorials shown at the end of the page.
10
+
The rest of this tutorial summarizes the contents of the standard libraries for Python. We recommend that you read this and then work through the practical examples in the tutorials shown at the end of the page.
15
11
16
12
Overview of the library
17
13
-----------------------
18
14
19
-
The QL Python library incorporates a large number of classes, each class corresponding either to one kind of entity in Python source code or to an entity that can be derived form the source code using static analysis. These classes can be divided into four categories:
15
+
The CodeQL library for Python incorporates a large number of classes. Each class corresponds either to one kind of entity in Python source code or to an entity that can be derived form the source code using static analysis. These classes can be divided into four categories:
20
16
21
17
- **Syntactic** - classes that represent entities in the Python source code.
22
18
- **Control flow** - classes that represent entities from the control flow graphs.
23
19
- **Type inference** - classes that represent the inferred values and types of entities in the Python source code.
24
-
- **Taint tracking** - classes that represent the source, sinks and kinds of taint used to implement taint-tracking queries.
20
+
- **Taint tracking** - classes that represent the source, sinks and kinds of taint used to implement taint-tracking queries.
25
21
26
22
Syntactic classes
27
23
~~~~~~~~~~~~~~~~~
28
24
29
-
This part of the library represents the Python source code. The ``Module``, ``Class`` and ``Function`` classes correspond to Python modules, classes and functions respectively, collectively these are known as ``Scope`` classes. Each ``Scope`` contains a list of statements each of which is represented by a subclass of the class ``Stmt``. Statements themselves can contain other statements or expressions which are represented by subclasses of ``Expr``. Finally, there are a few additional classes for the parts of more complex expressions such as list comprehensions. Collectively these classes are subclasses of ``AstNode`` and form an `Abstract syntax tree <http://en.wikipedia.org/wiki/Abstract_syntax_tree>`__ (AST). The root of each AST is a ``Module``.
25
+
This part of the library represents the Python source code. The ``Module``, ``Class``, and ``Function`` classes correspond to Python modules, classes, and functions respectively, collectively these are known as ``Scope`` classes. Each ``Scope`` contains a list of statements each of which is represented by a subclass of the class ``Stmt``. Statements themselves can contain other statements or expressions which are represented by subclasses of ``Expr``. Finally, there are a few additional classes for the parts of more complex expressions such as list comprehensions. Collectively these classes are subclasses of ``AstNode`` and form an `Abstract syntax tree <http://en.wikipedia.org/wiki/Abstract_syntax_tree>`__ (AST). The root of each AST is a ``Module``.
30
26
31
27
`Symbolic information <http://en.wikipedia.org/wiki/Symbol_table>`__ is attached to the AST in the form of variables (represented by the class ``Variable``).
32
28
33
29
Scope
34
30
^^^^^
35
31
36
-
A Python program is a group of modules. Technically a module is just a list of statements, but we often think of it as composed of classes and functions. These top-level entities, the module, class and function are represented by the three classes (`Module <https://help.semmle.com/qldoc/python/semmle/python/Module.qll/type.Module$Module.html>`__, `Class <https://help.semmle.com/qldoc/python/semmle/python/Class.qll/type.Class$Class.html>`__ and `Function <https://help.semmle.com/qldoc/python/semmle/python/Function.qll/type.Function$Function.html>`__ which are all subclasses of ``Scope``.
32
+
A Python program is a group of modules. Technically a module is just a list of statements, but we often think of it as composed of classes and functions. These top-level entities, the module, class, and function are represented by the three CodeQL classes (`Module <https://help.semmle.com/qldoc/python/semmle/python/Module.qll/type.Module$Module.html>`__, `Class <https://help.semmle.com/qldoc/python/semmle/python/Class.qll/type.Class$Class.html>`__ and `Function <https://help.semmle.com/qldoc/python/semmle/python/Function.qll/type.Function$Function.html>`__ which are all subclasses of ``Scope``.
37
33
38
34
- ``Scope``
39
35
@@ -65,7 +61,7 @@ A statement is represented by the `Stmt <https://help.semmle.com/qldoc/python/se
65
61
else:
66
62
return0
67
63
68
-
The QL `For <https://help.semmle.com/qldoc/python/semmle/python/Stmts.qll/type.Stmts$For.html>`__ class representing the ``for`` statement has a number of member predicates to access its parts:
64
+
The `For <https://help.semmle.com/qldoc/python/semmle/python/Stmts.qll/type.Stmts$For.html>`__ class representing the ``for`` statement has a number of member predicates to access its parts:
69
65
70
66
- ``getTarget()`` returns the ``Expr`` representing the variable ``var``.
71
67
- ``getIter()`` returns the ``Expr`` resenting the variable ``seq``.
@@ -98,7 +94,7 @@ As an example, to find expressions of the form ``a+2`` where the left is a simpl
98
94
Variable
99
95
^^^^^^^^
100
96
101
-
Variables are represented by the `Variable <https://help.semmle.com/qldoc/python/semmle/python/Variables.qll/type.Variables$Variable.html>`__ class in the Python QL library. There are two subclasses, ``LocalVariable`` for function-level and class-level variables and ``GlobalVariable`` for module-level variables.
97
+
Variables are represented by the `Variable <https://help.semmle.com/qldoc/python/semmle/python/Variables.qll/type.Variables$Variable.html>`__ class in the CodeQL library. There are two subclasses, ``LocalVariable`` for function-level and class-level variables and ``GlobalVariable`` for module-level variables.
102
98
103
99
Other source code elements
104
100
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -108,7 +104,7 @@ Although the meaning of the program is encoded by the syntactic elements, ``Scop
108
104
Examples
109
105
^^^^^^^^
110
106
111
-
Each syntactic element in Python source is recorded in the snapshot. These can be queried via the corresponding class. Let us start with a couple of simple examples.
107
+
Each syntactic element in Python source is recorded in the CodeQL database. These can be queried via the corresponding class. Let us start with a couple of simple examples.
112
108
113
109
1. Finding all finally blocks
114
110
'''''''''''''''''''''''''''''
@@ -137,13 +133,13 @@ A block that does nothing is one that contains no statements except ``pass`` sta
137
133
138
134
not exists(Stmt s | s = ex.getAStmt() | not s instanceof Pass)
139
135
140
-
where ``ex`` is an ``ExceptStmt`` and ``Pass`` is the class representing ``pass`` statements. Instead of using the double negative, **"no**\ *statements that are*\ **not**\ *pass statements"*, this can also be expressed positively, "all statements must be pass statements." The positive form is expressed in QL using the ``forall`` quantifier:
136
+
where ``ex`` is an ``ExceptStmt`` and ``Pass`` is the class representing ``pass`` statements. Instead of using the double negative, "**no**\ *statements that are*\ **not**\ *pass statements"*, this can also be expressed positively, *"all statements must be pass statements."* The positive form is expressed using the ``forall`` quantifier:
141
137
142
138
.. code-block:: ql
143
139
144
140
forall(Stmt s | s = ex.getAStmt() | s instanceof Pass)
145
141
146
-
Both forms are equivalent. Using the positive QL expression, the whole query looks like this:
142
+
Both forms are equivalent. Using the positive expression, the whole query looks like this:
147
143
148
144
**Find pass-only ``except`` blocks**
149
145
@@ -160,9 +156,9 @@ Both forms are equivalent. Using the positive QL expression, the whole query loo
160
156
Summary
161
157
^^^^^^^
162
158
163
-
The most commonly used standard QL library classes in the syntactic part of the library are organized as follows:
159
+
The most commonly used standard classes in the syntactic part of the library are organized as follows:
164
160
165
-
``Module``, ``Class``, ``Function``, ``Stmt`` and ``Expr`` - they are all subclasses of `AstNode <https://help.semmle.com/qldoc/python/semmle/python/AST.qll/type.AST$AstNode.html>`__.
161
+
``Module``, ``Class``, ``Function``, ``Stmt``, and ``Expr`` - they are all subclasses of `AstNode <https://help.semmle.com/qldoc/python/semmle/python/AST.qll/type.AST$AstNode.html>`__.
166
162
167
163
Abstract syntax tree
168
164
''''''''''''''''''''
@@ -293,7 +289,7 @@ The classes in the control-flow part of the library are:
293
289
Type-inference classes
294
290
----------------------
295
291
296
-
The QL library for Python also supplies some classes for accessing the inferred types of values. The classes ``Value`` and ``ClassValue`` allow you to query the possible classes that an expression may have at runtime. For example, which ``ClassValue``\ s are iterable can be determined using the query:
292
+
The CodeQL library for Python also supplies some classes for accessing the inferred types of values. The classes ``Value`` and ``ClassValue`` allow you to query the possible classes that an expression may have at runtime. For example, which ``ClassValue``\ s are iterable can be determined using the query:
297
293
298
294
**Find iterable "ClassValue"s**
299
295
@@ -321,7 +317,7 @@ These classes are explained in more detail in :doc:`Tutorial: Points-to analysis
321
317
Taint-tracking classes
322
318
----------------------
323
319
324
-
The QL library for Python also supplies classes to specify taint-tracking analyses. The ``Configuration`` class can be overrridden to specify a taint-tracking analysis, by specifying source, sinks, sanitizers and additional flow steps. For those analyses that require additional types of taint to be tracked the ``TaintKind`` class can be overridden.
320
+
The CodeQL library for Python also supplies classes to specify taint-tracking analyses. The ``Configuration`` class can be overridden to specify a taint-tracking analysis, by specifying source, sinks, sanitizers and additional flow steps. For those analyses that require additional types of taint to be tracked the ``TaintKind`` class can be overridden.
325
321
326
322
327
323
Summary
@@ -336,5 +332,5 @@ These classes are explained in more detail in :doc:`Tutorial: Taint tracking and
336
332
What next?
337
333
----------
338
334
339
-
- Experiment with the worked examples in the QL for Python tutorial topics: :doc:`Functions <functions>`, :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, :doc:`Points-to analysis and type inference <pointsto-type-infer>` and :doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
335
+
- Experiment with the worked examples in the following tutorial topics: :doc:`Functions <functions>`, :doc:`Statements and expressions <statements-expressions>`, :doc:`Control flow <control-flow>`, :doc:`Points-to analysis and type inference <pointsto-type-infer>`, and :doc:`Taint tracking and data flow analysis in Python <taint-tracking>`.
340
336
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/python/pointsto-type-infer.rst
+5-9Lines changed: 5 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
Tutorial: Points-to analysis and type inference
2
2
===============================================
3
3
4
-
This topic contains worked examples of how to write queries using the standard QL library classes for Python type inference.
4
+
This topic contains worked examples of how to write queries using the standard CodeQL library classes for Python type inference.
5
5
6
6
The ``Value`` class
7
7
--------------------
8
8
9
-
The ``Value`` class and its subclasses ``FunctionValue``, ``ClassValue`` and ``ModuleValue`` represent the values an expression may hold at runtime.
9
+
The ``Value`` class and its subclasses ``FunctionValue``, ``ClassValue``, and ``ModuleValue`` represent the values an expression may hold at runtime.
10
10
11
11
Summary
12
12
~~~~~~~
@@ -201,7 +201,7 @@ There are two problems with this query:
201
201
- It assumes that any call to something named "eval" is a call to the builtin ``eval`` function, which may result in some false positive results.
202
202
- It assumes that ``eval`` cannot be referred to by any other name, which may result in some false negative results.
203
203
204
-
We can get much more accurate results using call-graph analysis. First, we can precisely identify the ``FunctionValue`` for the ``eval`` function, by using the ``Value::named`` QL predicate as follows:
204
+
We can get much more accurate results using call-graph analysis. First, we can precisely identify the ``FunctionValue`` for the ``eval`` function, by using the ``Value::named`` predicate as follows:
205
205
206
206
.. code-block:: ql
207
207
@@ -227,9 +227,5 @@ Then we can use ``Value.getACall()`` to identify calls to the ``eval`` function,
227
227
What next?
228
228
----------
229
229
230
-
For more information on writing QL, see:
231
-
232
-
- `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ - an introduction to the concepts of QL.
233
-
- :doc:`Learning QL <../../index>` - an overview of the resources for learning how to write your own QL queries.
234
-
- `Database generation <https://lgtm.com/help/lgtm/generate-database>`__ - an overview of the process that creates a database from source code.
235
-
- :doc:`What's in a CodeQL database? <../database>` - a description of the CodeQL database.
230
+
- Find out more about QL in the `QL language handbook <https://help.semmle.com/QL/ql-handbook/index.html>`__ and `QL language specification <https://help.semmle.com/QL/ql-spec/language.html>`__.
231
+
- Read a description of the CodeQL database in :doc:`What's in a CodeQL database? <../database>`
0 commit comments