Skip to content

Commit 0a3954e

Browse files
committed
Document the file format
1 parent 3e4029f commit 0a3954e

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

cpg-core/src/test/resources/function-dfg.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"dataFlows": [
99
{
10-
"from": "param2",
10+
"from": "param1",
1111
"to": "base",
1212
"dfgType": "full"
1313
}
@@ -21,7 +21,7 @@
2121
},
2222
"dataFlows": [
2323
{
24-
"from": "param1",
24+
"from": "param0",
2525
"to": "base",
2626
"dfgType": "full"
2727
}
@@ -34,7 +34,7 @@
3434
},
3535
"dataFlows": [
3636
{
37-
"from": "param1",
37+
"from": "param0",
3838
"to": "base",
3939
"dfgType": "full"
4040
}
@@ -47,8 +47,8 @@
4747
},
4848
"dataFlows": [
4949
{
50-
"from": "param2",
51-
"to": "param1",
50+
"from": "param1",
51+
"to": "param0",
5252
"dfgType": "full"
5353
}
5454
]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Specification: Data Flow Graph - Function Summaries
2+
3+
For functions and methods which are part of the analyzed codebase, the CPG can track data flows interprocedurally to some extent.
4+
However, for all functions and methods which cannot be analyzed, we have no information available.
5+
For this case, we provide the user a way to specify custom summaries of the data flows through the function.
6+
To do so, you need to fill a JSON file as follows:
7+
8+
* The outer element is a list/array
9+
* In this list, you add elements, each of which summarizes the flows for one function/method
10+
* The element consists of two objects: The `functionDeclaration` and the `dataFlows`
11+
* The `functionDeclaration` consists of:
12+
* `language`: The FQN of the `Language` element which this function is relevant for.
13+
* `methodName`: The FQN of the function or method. We use this one to identify the relevant function/method. Do not forget to add the class name and use the separators as specified by the `Language`.
14+
* `signature` (*optional*): This optional element allows us to differentiate between overloaded functions (i.e., two functions have the same FQN but accept different arguments). If no `signature` is specified, it matches to any function/method with the name you specified. The `signature` is a list of FQNs of the types (as strings)
15+
* The `dataFlows` element is a list of objects with the following elements:
16+
* `from`: A description of the start-node of a DFG-edge. Valid options:
17+
* `paramX`: where `X` is the offset (we start counting with 0)
18+
* `base`: the receiver of the method (i.e., the object the method is called on)
19+
* `to`: A description of the end-node of the DFG-edge. Valid options:
20+
* `paramX` where `X` is the offset (we start counting with 0)
21+
* `base` the receiver of the method (i.e., the object the method is called on)
22+
* `return` the return value of the function
23+
* `returnX` where `X` is a number and specifies the index of the return value (if multiple values are returned).
24+
* `dfgType`: Here, you can give more information. Currently, this is unused but should later allow us to add the properties to the edge.
25+
26+
An example of a file could look as follows:
27+
```json
28+
[
29+
{
30+
"functionDeclaration": {
31+
"language": "de.fraunhofer.aisec.cpg.frontends.java.JavaLanguage",
32+
"methodName": "java.util.List.addAll",
33+
"signature": ["int", "java.util.Object"]
34+
},
35+
"dataFlows": [
36+
{
37+
"from": "param1",
38+
"to": "base",
39+
"dfgType": "full"
40+
}
41+
]
42+
},
43+
{
44+
"functionDeclaration": {
45+
"language": "de.fraunhofer.aisec.cpg.frontends.java.JavaLanguage",
46+
"methodName": "java.util.List.addAll",
47+
"signature": ["java.util.Object"]
48+
},
49+
"dataFlows": [
50+
{
51+
"from": "param0",
52+
"to": "base",
53+
"dfgType": "full"
54+
}
55+
]
56+
},
57+
{
58+
"functionDeclaration": {
59+
"language": "de.fraunhofer.aisec.cpg.frontends.cxx.CLanguage",
60+
"methodName": "memcpy"
61+
},
62+
"dataFlows": [
63+
{
64+
"from": "param1",
65+
"to": "param0",
66+
"dfgType": "full"
67+
}
68+
]
69+
}
70+
]
71+
```
72+
This file configures the following edges:
73+
* For a method declaration in Java `java.util.List.addAll(int, java.util.Object)`, the parameter 1 flows to the base (i.e., the list object)
74+
* For a method declaration in Java `java.util.List.addAll(java.util.Object)`, the parameter 0 flows to the base (i.e., the list object)
75+
* For a function declaration in C `memcpy` (and thus also CXX `std::memcpy`), the parameter 1 flows to parameter 0.

docs/docs/CPG/specs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ links to the specifications of the following concepts:
1616

1717
* Explore our [Graph Model](./graph)
1818
* [Data Flow Graph (DFG)](./dfg)
19+
* [Data Flow Graph (DFG) Function Summaries](./dfg-function-summaries.md)
1920
* [Evaluation Order Graph (EOG)](./eog)

docs/docs/GettingStarted/library.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ val translationConfig = TranslationConfiguration
5959

6060
For a complete list of available methods, please check the KDoc.
6161

62+
If you want/have to specify data flow summaries for some methods or functions, you add the method `registerFunctionSummary` when building the `TranslationCOnfiguration` and add a file with the format specified [here](../CPG/specs/dfg-function-summaries.md)
63+
6264
## 3. Running the analysis
6365

6466
Now it's time to get the CPG. All you have to do is to run the analysis with the

docs/mkdocs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ nav:
161161
- CPG/specs/index.md
162162
- "Graph Schema": CPG/specs/graph.md
163163
- "Dataflow Graph (DFG)": CPG/specs/dfg.md
164+
- "Dataflow Graph (DFG) Function Summaries": CPG/specs/dfg-function-summaries.md
164165
- "Evaluation Order Graph (EOG)": CPG/specs/eog.md
165166
- "Implementation":
166167
- CPG/impl/index.md

0 commit comments

Comments
 (0)