Debian Codegraph @

gcc/testsuite/sarif-replay.dg/2.1.0-valid/malloc-vs-local-4.c.sarif

raw
1 /* Test a replay of a .sarif file generated from GCC testsuite.
2
3 The dg directives were stripped out from the generated .sarif
4 to avoid confusing DejaGnu for this test. */
5
6 {"$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json",
7 "version": "2.1.0",
8 "runs": [{"tool": {"driver": {"name": "GNU C23",
9 "fullName": "GNU C23 (GCC) version 15.0.0 20241211 (experimental) (x86_64-pc-linux-gnu)",
10 "version": "15.0.0 20241211 (experimental)",
11 "informationUri": "https://gcc.gnu.org/gcc-15/",
12 "rules": [{"id": "-Wanalyzer-possible-null-dereference",
13 "helpUri": "https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html#index-Wanalyzer-possible-null-dereference"},
14 {"id": "-Wanalyzer-double-free",
15 "helpUri": "https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html#index-Wanalyzer-double-free"}]}},
16 "taxonomies": [{"name": "CWE",
17 "version": "4.7",
18 "organization": "MITRE",
19 "shortDescription": {"text": "The MITRE Common Weakness Enumeration"},
20 "taxa": [{"id": "690",
21 "helpUri": "https://cwe.mitre.org/data/definitions/690.html"},
22 {"id": "415",
23 "helpUri": "https://cwe.mitre.org/data/definitions/415.html"}]}],
24 "invocations": [{"arguments": ["./cc1",
25 "-quiet",
26 "-iprefix",
27 "/home/david/coding/gcc-newgit-more-taint/build-with-libgdiagnostics/gcc/../lib/gcc/x86_64-pc-linux-gnu/15.0.0/",
28 "-isystem",
29 "./include",
30 "-isystem",
31 "./include-fixed",
32 "/not/a/real/path/malloc-vs-local-4.c",
33 "-quiet",
34 "-dumpbase",
35 "malloc-vs-local-4.c",
36 "-dumpbase-ext",
37 ".c",
38 "-mtune=generic",
39 "-march=x86-64",
40 "-fanalyzer",
41 "-fdiagnostics-add-output=sarif",
42 "-o",
43 "malloc-vs-local-4.s"],
44 "workingDirectory": {"uri": "/home/david/coding/gcc-newgit-more-taint/build-with-libgdiagnostics/gcc"},
45 "startTimeUtc": "2024-12-12T22:09:12Z",
46 "executionSuccessful": true,
47 "toolExecutionNotifications": [],
48 "endTimeUtc": "2024-12-12T22:09:12Z"}],
49 "originalUriBaseIds": {"PWD": {"uri": "file:///home/david/coding/gcc-newgit-more-taint/build-with-libgdiagnostics/gcc/"}},
50 "artifacts": [{"location": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
51 "uriBaseId": "PWD"},
52 "sourceLanguage": "c",
53 "contents": {"text": "#include <stdlib.h>\n\nvoid __attribute__((noinline)) callee_1 (int *ptr)\n{\n *ptr = 42; \n}\n\nint test_1 (int i, int flag)\n{\n /* Double diamond CFG; either use &i, or a malloc-ed buffer. */\n int *ptr = &i;\n if (flag)\n ptr = (int *)malloc (sizeof (int));\n callee_1 (ptr);\n if (flag)\n free (ptr);\n return i;\n}\n\nvoid __attribute__((noinline)) callee_2 (int *ptr)\n{\n *ptr = 42;\n}\n\nint test_2 (int flag)\n{\n int i;\n\n if (flag)\n callee_2 (&i);\n\n callee_2 (&i);\n\n if (!flag)\n {\n void *ptr = malloc (16);\n free (ptr);\n free (ptr);\n }\n}\n"},
54 "roles": ["analysisTarget",
55 "tracedFile"]}],
56 "results": [{"ruleId": "-Wanalyzer-possible-null-dereference",
57 "taxa": [{"id": "690",
58 "toolComponent": {"name": "cwe"}}],
59 "properties": {"gcc/analyzer/saved_diagnostic/sm": "malloc",
60 "gcc/analyzer/saved_diagnostic/enode": 78,
61 "gcc/analyzer/saved_diagnostic/snode": 22,
62 "gcc/analyzer/saved_diagnostic/sval": "&HEAP_ALLOCATED_REGION(46)",
63 "gcc/analyzer/saved_diagnostic/state": "unchecked ({free})",
64 "gcc/analyzer/saved_diagnostic/idx": 1},
65 "level": "warning",
66 "message": {"text": "dereference of possibly-NULL ‘ptr’"},
67 "locations": [{"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
68 "uriBaseId": "PWD"},
69 "region": {"startLine": 5,
70 "startColumn": 3,
71 "endColumn": 12},
72 "contextRegion": {"startLine": 5,
73 "snippet": {"text": " *ptr = 42;\n"}}},
74 "logicalLocations": [{"name": "callee_1",
75 "fullyQualifiedName": "callee_1",
76 "decoratedName": "callee_1",
77 "kind": "function"}]}],
78 "codeFlows": [{"threadFlows": [{"id": "main",
79 "locations": [{"properties": {"gcc/analyzer/checker_event/emission_id": "(1)",
80 "gcc/analyzer/checker_event/kind": "EK_FUNCTION_ENTRY"},
81 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
82 "uriBaseId": "PWD"},
83 "region": {"startLine": 8,
84 "startColumn": 5,
85 "endColumn": 11},
86 "contextRegion": {"startLine": 8,
87 "snippet": {"text": "int test_1 (int i, int flag)\n"}}},
88 "logicalLocations": [{"name": "test_1",
89 "fullyQualifiedName": "test_1",
90 "decoratedName": "test_1",
91 "kind": "function"}],
92 "message": {"text": "entry to ‘test_1’"}},
93 "kinds": ["enter",
94 "function"],
95 "nestingLevel": 1,
96 "executionOrder": 1},
97 {"properties": {"gcc/analyzer/checker_event/emission_id": "(2)",
98 "gcc/analyzer/checker_event/kind": "EK_START_CFG_EDGE",
99 "gcc/analyzer/superedge_event/superedge": {"kind": "SUPEREDGE_CFG_EDGE",
100 "src_idx": 13,
101 "dst_idx": 14,
102 "desc": "true (flags TRUE_VALUE) (has goto_locus)"}},
103 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
104 "uriBaseId": "PWD"},
105 "region": {"startLine": 12,
106 "startColumn": 6,
107 "endColumn": 7},
108 "contextRegion": {"startLine": 12,
109 "snippet": {"text": " if (flag)\n"}}},
110 "logicalLocations": [{"name": "test_1",
111 "fullyQualifiedName": "test_1",
112 "decoratedName": "test_1",
113 "kind": "function"}],
114 "message": {"text": "following ‘true’ branch (when ‘flag != 0’)..."}},
115 "kinds": ["branch",
116 "true"],
117 "nestingLevel": 1,
118 "executionOrder": 2},
119 {"properties": {"gcc/analyzer/checker_event/emission_id": "(3)",
120 "gcc/analyzer/checker_event/kind": "EK_END_CFG_EDGE",
121 "gcc/analyzer/superedge_event/superedge": {"kind": "SUPEREDGE_CFG_EDGE",
122 "src_idx": 13,
123 "dst_idx": 14,
124 "desc": "true (flags TRUE_VALUE) (has goto_locus)"}},
125 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
126 "uriBaseId": "PWD"},
127 "region": {"startLine": 13,
128 "startColumn": 18,
129 "endColumn": 39},
130 "contextRegion": {"startLine": 13,
131 "snippet": {"text": " ptr = (int *)malloc (sizeof (int));\n"}}},
132 "logicalLocations": [{"name": "test_1",
133 "fullyQualifiedName": "test_1",
134 "decoratedName": "test_1",
135 "kind": "function"}],
136 "message": {"text": "...to here"}},
137 "kinds": ["branch",
138 "true"],
139 "nestingLevel": 1,
140 "executionOrder": 3},
141 {"properties": {"gcc/analyzer/checker_event/emission_id": "(4)",
142 "gcc/analyzer/checker_event/kind": "EK_STATE_CHANGE"},
143 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
144 "uriBaseId": "PWD"},
145 "region": {"startLine": 13,
146 "startColumn": 18,
147 "endColumn": 39},
148 "contextRegion": {"startLine": 13,
149 "snippet": {"text": " ptr = (int *)malloc (sizeof (int));\n"}}},
150 "logicalLocations": [{"name": "test_1",
151 "fullyQualifiedName": "test_1",
152 "decoratedName": "test_1",
153 "kind": "function"}],
154 "message": {"text": "this call could return NULL"}},
155 "kinds": ["acquire",
156 "memory"],
157 "nestingLevel": 1,
158 "executionOrder": 4},
159 {"properties": {"gcc/analyzer/checker_event/emission_id": "(5)",
160 "gcc/analyzer/checker_event/kind": "EK_CALL_EDGE",
161 "gcc/analyzer/superedge_event/superedge": {"kind": "SUPEREDGE_CALL",
162 "src_idx": 15,
163 "dst_idx": 21,
164 "desc": "call"}},
165 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
166 "uriBaseId": "PWD"},
167 "region": {"startLine": 14,
168 "startColumn": 3,
169 "endColumn": 17},
170 "contextRegion": {"startLine": 14,
171 "snippet": {"text": " callee_1 (ptr);\n"}}},
172 "logicalLocations": [{"name": "test_1",
173 "fullyQualifiedName": "test_1",
174 "decoratedName": "test_1",
175 "kind": "function"}],
176 "message": {"text": "calling ‘callee_1’ from ‘test_1’"}},
177 "kinds": ["call",
178 "function"],
179 "nestingLevel": 1,
180 "executionOrder": 5},
181 {"properties": {"gcc/analyzer/checker_event/emission_id": "(6)",
182 "gcc/analyzer/checker_event/kind": "EK_FUNCTION_ENTRY"},
183 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
184 "uriBaseId": "PWD"},
185 "region": {"startLine": 3,
186 "startColumn": 32,
187 "endColumn": 40},
188 "contextRegion": {"startLine": 3,
189 "snippet": {"text": "void __attribute__((noinline)) callee_1 (int *ptr)\n"}}},
190 "logicalLocations": [{"name": "callee_1",
191 "fullyQualifiedName": "callee_1",
192 "decoratedName": "callee_1",
193 "kind": "function"}],
194 "message": {"text": "entry to ‘callee_1’"}},
195 "kinds": ["enter",
196 "function"],
197 "nestingLevel": 2,
198 "executionOrder": 6},
199 {"properties": {"gcc/analyzer/checker_event/emission_id": "(7)",
200 "gcc/analyzer/checker_event/kind": "EK_WARNING"},
201 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
202 "uriBaseId": "PWD"},
203 "region": {"startLine": 5,
204 "startColumn": 3,
205 "endColumn": 12},
206 "contextRegion": {"startLine": 5,
207 "snippet": {"text": " *ptr = 42;\n"}}},
208 "logicalLocations": [{"name": "callee_1",
209 "fullyQualifiedName": "callee_1",
210 "decoratedName": "callee_1",
211 "kind": "function"}],
212 "message": {"text": "‘ptr’ could be NULL: unchecked value from [(4)](sarif:/runs/0/results/0/codeFlows/0/threadFlows/0/locations/3)"}},
213 "kinds": ["danger"],
214 "nestingLevel": 2,
215 "executionOrder": 7}]}]}]},
216 {"ruleId": "-Wanalyzer-double-free",
217 "taxa": [{"id": "415",
218 "toolComponent": {"name": "cwe"}}],
219 "properties": {"gcc/analyzer/saved_diagnostic/sm": "malloc",
220 "gcc/analyzer/saved_diagnostic/enode": 39,
221 "gcc/analyzer/saved_diagnostic/snode": 6,
222 "gcc/analyzer/saved_diagnostic/sval": "&HEAP_ALLOCATED_REGION(46)",
223 "gcc/analyzer/saved_diagnostic/state": "freed",
224 "gcc/analyzer/saved_diagnostic/idx": 0},
225 "level": "warning",
226 "message": {"text": "double-‘free’ of ‘ptr’"},
227 "locations": [{"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
228 "uriBaseId": "PWD"},
229 "region": {"startLine": 38,
230 "startColumn": 7,
231 "endColumn": 17},
232 "contextRegion": {"startLine": 38,
233 "snippet": {"text": " free (ptr);\n"}}},
234 "logicalLocations": [{"name": "test_2",
235 "fullyQualifiedName": "test_2",
236 "decoratedName": "test_2",
237 "kind": "function"}]}],
238 "codeFlows": [{"threadFlows": [{"id": "main",
239 "locations": [{"properties": {"gcc/analyzer/checker_event/emission_id": "(1)",
240 "gcc/analyzer/checker_event/kind": "EK_START_CFG_EDGE",
241 "gcc/analyzer/superedge_event/superedge": {"kind": "SUPEREDGE_CFG_EDGE",
242 "src_idx": 5,
243 "dst_idx": 6,
244 "desc": "true (flags TRUE_VALUE) (has goto_locus)"}},
245 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
246 "uriBaseId": "PWD"},
247 "region": {"startLine": 34,
248 "startColumn": 6,
249 "endColumn": 7},
250 "contextRegion": {"startLine": 34,
251 "snippet": {"text": " if (!flag)\n"}}},
252 "logicalLocations": [{"name": "test_2",
253 "fullyQualifiedName": "test_2",
254 "decoratedName": "test_2",
255 "kind": "function"}],
256 "message": {"text": "following ‘true’ branch (when ‘flag == 0’)..."}},
257 "kinds": ["branch",
258 "true"],
259 "nestingLevel": 1,
260 "executionOrder": 1},
261 {"properties": {"gcc/analyzer/checker_event/emission_id": "(2)",
262 "gcc/analyzer/checker_event/kind": "EK_END_CFG_EDGE",
263 "gcc/analyzer/superedge_event/superedge": {"kind": "SUPEREDGE_CFG_EDGE",
264 "src_idx": 5,
265 "dst_idx": 6,
266 "desc": "true (flags TRUE_VALUE) (has goto_locus)"}},
267 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
268 "uriBaseId": "PWD"},
269 "region": {"startLine": 36,
270 "startColumn": 19,
271 "endColumn": 30},
272 "contextRegion": {"startLine": 36,
273 "snippet": {"text": " void *ptr = malloc (16);\n"}}},
274 "logicalLocations": [{"name": "test_2",
275 "fullyQualifiedName": "test_2",
276 "decoratedName": "test_2",
277 "kind": "function"}],
278 "message": {"text": "...to here"}},
279 "kinds": ["branch",
280 "true"],
281 "nestingLevel": 1,
282 "executionOrder": 2},
283 {"properties": {"gcc/analyzer/checker_event/emission_id": "(3)",
284 "gcc/analyzer/checker_event/kind": "EK_STATE_CHANGE"},
285 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
286 "uriBaseId": "PWD"},
287 "region": {"startLine": 36,
288 "startColumn": 19,
289 "endColumn": 30},
290 "contextRegion": {"startLine": 36,
291 "snippet": {"text": " void *ptr = malloc (16);\n"}}},
292 "logicalLocations": [{"name": "test_2",
293 "fullyQualifiedName": "test_2",
294 "decoratedName": "test_2",
295 "kind": "function"}],
296 "message": {"text": "allocated here"}},
297 "kinds": ["acquire",
298 "memory"],
299 "nestingLevel": 1,
300 "executionOrder": 3},
301 {"properties": {"gcc/analyzer/checker_event/emission_id": "(4)",
302 "gcc/analyzer/checker_event/kind": "EK_STATE_CHANGE"},
303 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
304 "uriBaseId": "PWD"},
305 "region": {"startLine": 37,
306 "startColumn": 7,
307 "endColumn": 17},
308 "contextRegion": {"startLine": 37,
309 "snippet": {"text": " free (ptr);\n"}}},
310 "logicalLocations": [{"name": "test_2",
311 "fullyQualifiedName": "test_2",
312 "decoratedName": "test_2",
313 "kind": "function"}],
314 "message": {"text": "first ‘free’ here"}},
315 "kinds": ["release",
316 "memory"],
317 "nestingLevel": 1,
318 "executionOrder": 4},
319 {"properties": {"gcc/analyzer/checker_event/emission_id": "(5)",
320 "gcc/analyzer/checker_event/kind": "EK_WARNING"},
321 "location": {"physicalLocation": {"artifactLocation": {"uri": "/not/a/real/path/malloc-vs-local-4.c",
322 "uriBaseId": "PWD"},
323 "region": {"startLine": 38,
324 "startColumn": 7,
325 "endColumn": 17},
326 "contextRegion": {"startLine": 38,
327 "snippet": {"text": " free (ptr);\n"}}},
328 "logicalLocations": [{"name": "test_2",
329 "fullyQualifiedName": "test_2",
330 "decoratedName": "test_2",
331 "kind": "function"}],
332 "message": {"text": "second ‘free’ here; first ‘free’ was at [(4)](sarif:/runs/0/results/1/codeFlows/0/threadFlows/0/locations/3)"}},
333 "kinds": ["danger"],
334 "nestingLevel": 1,
335 "executionOrder": 5}]}]}]}]}]}
336 // TODO: show the CWEs
337
338 /* { dg-begin-multiline-output "" }
339 In function 'callee_1':
340 /not/a/real/path/malloc-vs-local-4.c:5:3: warning: dereference of possibly-NULL ‘ptr’ [-Wanalyzer-possible-null-dereference]
341 5 | *ptr = 42;
342 | ^~~~~~~~~
343 'test_1': events 1-5
344 |
345 | 8 | int test_1 (int i, int flag)
346 | | ^~~~~~
347 | | |
348 | | (1) entry to ‘test_1’
349 |......
350 | 12 | if (flag)
351 | | ~
352 | | |
353 | | (2) following ‘true’ branch (when ‘flag != 0’)...
354 | 13 | ptr = (int *)malloc (sizeof (int));
355 | | ~~~~~~~~~~~~~~~~~~~~~
356 | | |
357 | | (3) ...to here
358 | | (4) this call could return NULL
359 | 14 | callee_1 (ptr);
360 | | ~~~~~~~~~~~~~~
361 | | |
362 | | (5) calling ‘callee_1’ from ‘test_1’
363 |
364 +--> 'callee_1': events 6-7
365 |
366 | 3 | void __attribute__((noinline)) callee_1 (int *ptr)
367 | | ^~~~~~~~
368 | | |
369 | | (6) entry to ‘callee_1’
370 | 4 | {
371 | 5 | *ptr = 42;
372 | | ~~~~~~~~~
373 | | |
374 | | (7) ‘ptr’ could be NULL: unchecked value from (4)
375 |
376 { dg-end-multiline-output "" } */
377 /* { dg-begin-multiline-output "" }
378 In function 'test_2':
379 /not/a/real/path/malloc-vs-local-4.c:38:7: warning: double-‘free’ of ‘ptr’ [-Wanalyzer-double-free]
380 38 | free (ptr);
381 | ^~~~~~~~~~
382 'test_2': events 1-5
383 34 | if (!flag)
384 | ^
385 | |
386 | (1) following ‘true’ branch (when ‘flag == 0’)...
387 35 | {
388 36 | void *ptr = malloc (16);
389 | ~~~~~~~~~~~
390 | |
391 | (2) ...to here
392 | (3) allocated here
393 37 | free (ptr);
394 | ~~~~~~~~~~
395 | |
396 | (4) first ‘free’ here
397 38 | free (ptr);
398 | ~~~~~~~~~~
399 | |
400 | (5) second ‘free’ here; first ‘free’ was at (4)
401 { dg-end-multiline-output "" } */