Skip to content

Commit 8c6e08c

Browse files
committed
Add slices.Concat example
1 parent 96a7965 commit 8c6e08c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/codeql/codeql-language-guides/customizing-library-models-for-go.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,56 @@ The remaining values are used to define the ``access path``, the ``kind``, and t
195195
- The eighth value ``ReturnValue`` is the access path to the output (where data flows to), in this case ``ReturnValue``, which means that the input flows to the return value.
196196
- The ninth value ``value`` is the kind of the flow. ``value`` flow indicates an entire value is moved, ``taint`` means that taint is propagated through the call.
197197
- The tenth value ``manual`` is the provenance of the summary, which is used to identify the origin of the summary.
198+
199+
Example: Add flow through the ``Concat`` function
200+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201+
202+
This example shows how the Go query pack models flow through a function for a simple case.
203+
This pattern covers many of the cases where we need to summarize flow through a function that is stored in a library or framework outside the repository.
204+
205+
.. code-block:: go
206+
207+
import "slices"
208+
209+
func ValueFlow {
210+
a := []int{1, 2, 3}
211+
b := []int{4, 5, 6}
212+
c := slices.Concat(a, b) // There is taint flow from `a` and `b` to `c`.
213+
...
214+
}
215+
216+
We need to add a tuple to the ``summaryModel``\(package, type, subtypes, name, signature, ext, input, output, kind, provenance) extensible predicate by updating a data extension file:
217+
218+
.. code-block:: yaml
219+
220+
extensions:
221+
- addsTo:
222+
pack: codeql/go-all
223+
extensible: summaryModel
224+
data:
225+
- ["slices", "", True, "Concat", "", "", "Argument[0].ArrayElement.ArrayElement", "ReturnValue.ArrayElement", "value", "manual"]
226+
227+
Since we are adding flow through a method, we need to add tuples to the ``summaryModel`` extensible predicate.
228+
Each tuple defines flow from one argument to the return value.
229+
The first row defines flow from the arguments (``a`` and ``b`` in the example) to the return value (``c`` in the example) and the second row defines flow from the second argument (``sep`` in the example) to the return value (``t`` in the example).
230+
231+
The first five values identify the function to be modeled as a summary.
232+
These are the same for both of the rows above as we are adding two summaries for the same method.
233+
234+
- The first value ``slices`` is the package name.
235+
- The second value ``""`` is left blank, since the function is not a method of a type.
236+
- The third value ``False`` is a flag that indicates whether or not the sink also applies to subtypes. This has no effect for non-method functions.
237+
- The fourth value ``Max`` is the function name.
238+
- The fifth value ``""`` is left blank, since specifying the signature is optional and Go does not allow multiple signature overloads for the same function.
239+
240+
The sixth value should be left empty and is out of scope for this documentation.
241+
The remaining values are used to define the ``access path``, the ``kind``, and the ``provenance`` (origin) of the summary.
242+
243+
- The seventh value is the access path to the input (where data flows from). ``Argument[0]`` is the access path to the first argument.
244+
- The eighth value ``ReturnValue`` is the access path to the output (where data flows to), in this case ``ReturnValue``, which means that the input flows to the return value.
245+
- The ninth value ``value`` is the kind of the flow. ``value`` flow indicates an entire value is moved, ``taint`` means that taint is propagated through the call.
246+
- The tenth value ``manual`` is the provenance of the summary, which is used to identify the origin of the summary.
247+
198248
Example: Add flow through the ``Join`` function
199249
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200250
This example shows how the Go query pack models flow through a method for a simple case.

0 commit comments

Comments
 (0)