Debian Codegraph @

docs/data-model.rst

raw
1 .. Copyright 2017 David Malcolm <[email protected]>
2 Copyright 2017 Red Hat, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 USA
18
19 Data model
20 ==========
21
22 .. This could use an XML domain for Sphinx.
23
24 .. py:module:: firehose.model
25
26 .. py:class:: Analysis
27
28 The :py:class:`Analysis` class represents one invocation of a
29 code analysis tool.
30
31 It corresponds to the ``<analysis>`` XML element, the top-level element
32 of a Firehose XML document.
33
34 .. py:attribute:: metadata
35
36 :py:class:`Metadata`
37
38 .. py:attribute:: results
39
40 A list of :py:class:`Result` objects, representing the various
41 issues, failures, and other information found during the analysis.
42
43 .. py:attribute:: customfields
44
45 :py:class:`CustomFields` or ``None``
46
47 Here is the pertinent part of the XML schema:
48
49 .. literalinclude:: ../firehose.rng
50 :start-after: datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
51 :end-before: <define name="metadata-element">
52 :language: xml
53
54 .. py:method:: __init__(self, metadata, results, customfields=None):
55
56 :param metadata:
57 :type metadata: :py:class:`Metadata`
58 :param results:
59 :type results: list(:py:class:`Result`)
60 :param customfields:
61 :type customfields: :py:class:`CustomFields` or None
62
63 ..
64 TODO: talk about to_xml, from_xml, to_json, from_json
65
66 .. py:classmethod:: from_xml(cls, fileobj)
67
68 Parse XML from fileobj, and return an :py:class:`Analysis` instance
69 representing the data seen there.
70
71 .. py:method:: to_xml(self)
72
73 Generate an :py:class:`ET.ElementTree()` representing the data
74 within self.
75
76 .. py:method:: to_xml_bytes(self)
77
78 Generate a ``bytes`` instance containing an XML serialization of the
79 data within self.
80
81 ..
82 def accept(self, visitor):
83
84 def fixup_files(self, relativedir=None, hashalg=None):
85 Record the absolute path of each file, and record the digest of the
86 file content
87
88 def set_custom_field(self, name, value):
89
90
91 Results
92 *******
93
94 .. py:class:: Result
95
96 Result is a base class
97
98 There are three subclasses:
99
100 * an :py:class:`Issue` represents a report from the analyzer about a
101 possible problem with the software under test.
102
103 * an :py:class:`Info` represents additional kinds of information
104 generated by an analyzer that isn't a problem per-se e.g.
105 code metrics, licensing info, etc.
106
107 * a :py:class:`Failure` represents a report about a failure of the
108 analyzer itself (e.g. if the analyzer crashed).
109
110
111 .. py:class:: Issue(Result)
112
113 An :py:class:`Issue` represents a report from the analyzer about a
114 possible problem with the software under test.
115
116 It corresponds to the ``<issue>`` XML element within a
117 Firehose XML document.
118
119 .. py:attribute:: cwe
120
121 (``int`` or ``None``): The Common Weakness Enumeration ID
122 (see http://cwe.mitre.org/index.html )
123 e.g. "131" representing CWE-131
124 aka "Incorrect Calculation of Buffer Size"
125 http://cwe.mitre.org/data/definitions/131.html
126
127 .. py:attribute:: testid
128
129 (``str`` or ``None``): Each static analysis tool potentially has
130 multiple tests, with its own IDs for its own tests.
131 These can be captured here, as free-form strings.
132
133 .. py:attribute:: location
134
135 (:py:class:`Location`): Where is the problem?
136
137 .. py:attribute:: message
138
139 (:py:class:`Message`): A message summarizing the problem.
140
141 .. py:attribute:: notes
142
143 (:py:class:`Notes` or ``None``): Additional descriptive details.
144
145 .. py:attribute:: trace
146
147 (:py:class:`Trace` or ``None``): An optional list of events that
148 describe the circumstances leading up to a problem.
149
150 .. py:attribute:: severity
151
152 (``str`` or ``None``): Each static analysis tool potentially can
153 report a "severity", which may be of use for filtering.
154
155 The precise strings are likely to vary from tool to tool.
156 To avoid data-transfer issues, support storing it as an optional
157 freeform string here.
158
159 See:
160 http://lists.fedoraproject.org/pipermail/firehose-devel/2013-February/000001.html
161
162 .. py:attribute:: customfields
163
164 (:py:class:`CustomFields` or ``None``): A given tool/testid may have
165 additional key/value pairs that it may be useful to capture.
166
167
168 ..
169 def __init__(self,
170 cwe,
171 testid,
172 location,
173 message,
174 notes,
175 trace,
176 severity=None,
177 customfields=None):
178 if cwe is not None:
179 assert isinstance(cwe, int)
180 if testid is not None:
181 assert isinstance(testid, _string_type)
182 assert isinstance(location, Location)
183 assert isinstance(message, Message)
184 if notes:
185 assert isinstance(notes, Notes)
186 if trace:
187 assert isinstance(trace, Trace)
188 if severity is not None:
189 assert isinstance(severity, _string_type)
190 if customfields is not None:
191 assert isinstance(customfields, CustomFields)
192 self.cwe = cwe
193 self.testid = testid
194 self.location = location
195 self.message = message
196 self.notes = notes
197 self.trace = trace
198 self.severity = severity
199 self.customfields = customfields
200
201 .. py:method:: write_as_gcc_output(self, out)
202
203 Write the issue in the style of a GCC warning to the given
204 file-like object.
205
206 >>> issue.write_as_gcc_output(sys.stderr)
207 examples/python-src-example.c:40:4: warning: ob_refcnt of '*item' is 1 too high [CWE-401]
208 was expecting final item->ob_refcnt to be N + 1 (for some unknown N)
209 due to object being referenced by: PyListObject.ob_item[0]
210 but final item->ob_refcnt is N + 2
211 examples/python-src-example.c:36:14: note: PyLongObject allocated at: item = PyLong_FromLong(random());
212 examples/python-src-example.c:37:8: note: when PyList_Append() succeeds
213
214 .. py:method:: get_cwe_str(self)
215
216 Get a string giving the CWE title, or None::
217
218 >>> issue.get_cwe_str()
219 'CWE-131'
220
221 .. py:method:: get_cwe_url(self)
222
223 Get a string containing the URL of the CWE id, or None::
224
225 >>> issue.get_cwe_url()
226 'http://cwe.mitre.org/data/definitions/131.html'
227
228
229 .. py:class:: Info(Result)
230
231 An :py:class:`Info` represents additional kinds of information
232 generated by an analyzer that isn't a problem per-se e.g.
233 code metrics, licensing info, cross-referencing information, etc.
234
235 It corresponds to the ``<info>`` XML element within a
236 Firehose XML document.
237
238 .. py:attribute:: infoid
239
240 (``str`` or ``None``): an optional free-form string
241 identifying the kind of information being reported.
242
243 .. py:attribute:: location
244
245 :py:class:`Location` or ``None``
246
247 .. py:attribute:: message
248
249 :py:class:`Message` or ``None``
250
251 .. py:attribute:: customfields
252
253 :py:class:`CustomFields` or ``None``
254
255 ..
256 def __init__(self, infoid, location, message, customfields):
257 if infoid is not None:
258 assert isinstance(infoid, _string_type)
259 if location is not None:
260 assert isinstance(location, Location)
261 if message is not None:
262 assert isinstance(message, Message)
263 if customfields is not None:
264 assert isinstance(customfields, CustomFields)
265 self.infoid = infoid
266 self.location = location
267 self.message = message
268 self.customfields = customfields
269
270
271 .. py:class:: Failure(Result)
272
273 A :py:class:`Failure` represents a report about a failure of the
274 analyzer itself (e.g. if the analyzer crashed).
275
276 If any of these are present then we don't have full coverage.
277
278 For some analyzers this is an all-or-nothing affair: we either
279 get issues reported, or a failure happens (e.g. a segfault of the
280 analysis tool).
281
282 Other analyzers may be more fine-grained: able to report some issues,
283 but choke on some subset of the code under analysis. For example
284 cpychecker runs once per function, and any unhandled Python exceptions
285 only affect one function.
286
287 It corresponds to the ``<failure>`` XML element within a
288 Firehose XML document.
289
290 .. py:attribute:: failureid
291
292 (``str`` or ``None``): Each static analysis tool potentially can
293 identify types of way that it can fail.
294
295 Capture those that do here, as (optional) free-form strings.
296
297 .. py:attribute:: location
298
299 :py:class:`Location`: Some analysis tools may be able to annotate
300 a failure report by providing the location *within the
301 software-under-test* that broke them.
302
303 For example, gcc-python-plugin has a ``gcc.set_location()`` method
304 which can be used by a code analysis script to record what
305 location is being analyzed, so that if unhandled Python exception
306 happens, it is reported at that location. This is invaluable
307 when debugging analysis failures.
308
309 .. py:attribute:: message
310
311 :py:class:`Message`: A summary of the failure.
312
313 .. py:attribute:: customfields
314
315 :py:class:`CustomFields` or ``None``:
316 Every type of failure seems to have its own kinds of data that
317 are worth capturing:
318
319 * stdout/stderr/returncode for a failed subprocess
320
321 * traceback for an unhandled Python exception
322
323 * verbose extra information about a cppcheck failure
324
325 etc.
326 Hence we allow a ``<failure>`` to optionally contain extra key/value
327 pairs, based on the ``failureid``.
328
329 ..
330 def __init__(self, failureid, location, message, customfields):
331 if failureid is not None:
332 assert isinstance(failureid, _string_type)
333 if location is not None:
334 assert isinstance(location, Location)
335 if message is not None:
336 assert isinstance(message, Message)
337 if customfields is not None:
338 assert isinstance(customfields, CustomFields)
339 self.failureid = failureid
340 self.location = location
341 self.message = message
342 self.customfields = customfields
343
344
345 Metadata
346 ********
347
348 .. py:class:: Metadata
349
350 Holder for metadata about an analyzer invocation.
351
352 It corresponds to the ``<metadata>`` XML element within a
353 Firehose XML document.
354
355 .. py:attribute:: generator
356
357 :py:class:`Generator`
358
359 .. py:attribute:: sut
360
361 :py:class:`Sut` or ``None``
362
363 .. py:attribute:: file_
364
365 :py:class:`File` or ``None``
366
367 .. py:attribute:: stats
368
369 :py:class:`Stats` or ``None``
370
371 ..
372 def __init__(self, generator, sut, file_, stats):
373 assert isinstance(generator, Generator)
374 if sut is not None:
375 assert isinstance(sut, Sut)
376 if file_ is not None:
377 assert isinstance(file_, File)
378 if stats is not None:
379 assert isinstance(stats, Stats)
380 self.generator = generator
381 self.sut = sut
382 self.file_ = file_
383 self.stats = stats
384
385 .. py:class:: Generator
386
387 .. py:attribute:: name
388
389 ``str``
390
391 .. py:attribute:: version
392
393 ``str`` or ``None``
394
395 .. py:class:: Stats
396
397 :py:class:`Stats` is an optional field of :py:class:`Metadata` for
398 capturing stats about an analysis run.
399
400 .. py:attribute:: wallclocktime
401
402 ``float``: how long (in seconds) the analyzer took to run
403
404
405 Describing the software under test
406 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
407
408 .. warning:: this part of the schema may need more thought/work
409
410 .. py:class:: Sut
411
412 Base class for describing the software-under-test.
413
414 .. py:class:: SourceRpm(Sut)
415
416 It corresponds to the ``<source-rpm>`` XML element within
417 a Firehose XML document.
418
419 .. py:attribute:: name
420
421 ``str``
422
423 .. py:attribute:: version
424
425 ``str``
426
427 .. py:attribute:: release
428
429 ``str``
430
431 .. py:attribute:: buildarch
432
433 ``str``
434
435 .. py:class:: DebianBinary(Sut)
436
437 Internal Firehose representation of a Debian binary package. This Object
438 is extremely similar to a SourceRpm.
439
440 It corresponds to the ``<debian-binary>`` XML element within
441 a Firehose XML document.
442
443 .. py:attribute:: name
444
445 ``str``: the *binary* package name.
446
447 .. py:attribute:: version
448
449 ``str``: should match Upstream's version number
450
451 .. py:attribute:: release
452
453 ``str`` or ``None``: should be the Debian package local version.
454 This should only be omited if the package is a Debian Native package.
455
456 .. py:attribute:: buildarch
457
458 ``str``: valid entries include ``amd64`', ``kfreebsd-amd64``,
459 ``armhf``, ``hurd-i386``, among others for Debian.
460
461 .. py:class:: DebianSource(Sut)
462
463 Internal Firehose representation of a Debian source package. This Object
464 is extremely similar to a SourceRpm, but does not include the `buildarch`
465 attribute.
466
467 It corresponds to the ``<debian-source>`` XML element within
468 a Firehose XML document.
469
470 .. py:attribute:: name
471
472 ``str``: should be the *source* package name
473
474 .. py:attribute:: version
475
476 ``str``: should match Upstream's version number
477
478 .. py:attribute:: release
479
480 ``str`` or ``None``: if given, should be the Debian package local
481 version. This should only be omited if the package is a
482 Debian Native package.
483
484
485 .. py:class:: Message
486
487 Summary text aimed at a developer. This is required for an
488 :py:class:`Issue`, but is also can (optionally) be provided by
489 a :py:class:`Failure` or :py:class:`Info`.
490
491 It corresponds to the ``<message>`` XML element within
492 a Firehose XML document.
493
494 .. py:attribute:: text
495
496 ``str``
497
498
499 .. py:class:: Notes
500
501 Additional optional descriptive details for a :py:class:`Result` or
502 for a :py:class:`State`.
503
504 It corresponds to the ``<notes>`` XML element within
505 a Firehose XML document.
506
507 .. py:attribute:: text
508
509 ``str``
510
511 .. This might support some simple markup at some point
512 (as might :py:class:`Message`).
513
514 Describing source code
515 **********************
516
517 .. py:class:: Location
518
519 A particular source code location.
520
521 It corresponds to the ``<location>`` XML element within
522 a Firehose XML document.
523
524 .. py:attribute:: file
525
526 :py:class:`File`
527
528 .. py:attribute:: function
529
530 :py:class:`Function` or ``None``. The function (or method)
531 containing the problem.
532
533 This is optional. Some problems occur in global scope, and
534 unfortunately, some analyzers don't always report which function each
535 problem was discovered in. Given that function names are less
536 likely to change than line numbers, this is something that we
537 should patch in each upstream analyzer as we go.
538
539 We can refer to either a location, or a range of locations
540 within the file:
541
542 .. py:attribute:: point
543
544 :py:class:`Point` or ``None``
545
546 .. py:attribute:: range_
547
548 :py:class:`Range` or ``None``
549
550 ..
551 def __init__(self, file, function, point=None, range_=None):
552 assert isinstance(file, File)
553 if function is not None:
554 assert isinstance(function, Function)
555 if point is not None:
556 assert isinstance(point, Point)
557 if range_ is not None:
558 assert isinstance(range_, Range)
559 self.file = file
560 self.function = function
561 self.point = point
562 self.range_ = range_
563 @property
564 def line(self):
565 if self.point is not None:
566 return self.point.line
567 if self.range_ is not None:
568 return self.range_.start.line
569 @property
570 def column(self):
571 if self.point is not None:
572 return self.point.column
573 if self.range_ is not None:
574 return self.range_.start.column
575
576 .. py:class:: File
577
578 A description of a particular source file.
579
580 It corresponds to the ``<file>`` XML element within
581 a Firehose XML document.
582
583 .. py:attribute:: givenpath
584
585 ``str``: the filename given by the analyzer.
586
587 This is typically the one supplied to it on the command line,
588 which might be absolute or relative.
589
590 Examples:
591
592 * "foo.c"
593
594 * "./src/foo.c"
595
596 * "/home/david/libfoo-1.0/src/foo.c"
597
598 .. py:attribute:: abspath
599
600 (``str`` or ``None``): Optionally, a record of the absolute path
601 of the file, to help deal with collating results from a build
602 that changes working directory (e.g. recursive make).
603
604 .. py:attribute:: hash_
605
606 (:py:class:`Hash` or ``None``)
607
608 ..
609 def __init__(self, givenpath, abspath, hash_=None):
610 assert isinstance(givenpath, ``str``)
611 if abspath is not None:
612 assert isinstance(abspath, ``str``)
613 if hash_ is not None:
614 assert isinstance(hash_, Hash)
615 self.givenpath = givenpath
616 self.abspath = abspath
617 self.hash_ = hash_
618
619 .. py:class:: Hash
620
621 An optional value within :py:class:`File`, allowing the report to
622 specify a hash value for a particular file.
623
624 This can be used for tracking different versions of files when
625 collating different reports and e.g. for caching file content
626 in a UI.
627
628 It corresponds to the ``<hash>`` XML element within
629 a Firehose XML document.
630
631 .. py:attribute:: alg
632
633 ``str``: the name of the hash algorithm.
634
635 TODO: what naming convention?
636
637 .. py:attribute:: hexdigest
638
639 ``str``: the hexadecimal value of the digest (lower-case
640 hexdigits, without any leading `0x`).
641
642 ..
643 def __init__(self, alg, hexdigest):
644 assert isinstance(alg, ``str``)
645 assert isinstance(hexdigest, ``str``)
646 self.alg = alg
647 self.hexdigest = hexdigest
648
649 .. py:class:: Function
650
651 Identification of a particular function within source code.
652
653 It corresponds to the ``<function>`` XML element within
654 a Firehose XML document.
655
656 .. py:attribute:: name
657
658 ``str``: the name of the function or method.
659
660 ..
661 def __init__(self, name):
662 self.name = name
663
664 .. py:class:: Point
665
666 Identification of a particular line/column within a source file.
667
668 It corresponds to the ``<point>`` XML element within
669 a Firehose XML document.
670
671 .. py:attribute:: line
672
673 ``int``: the 1-based number of the line containing the point
674
675 .. py:attribute:: column
676
677 ``int``: 1-based number of the column
678
679 .. note::
680
681 GCC uses a 1-based convention for source columns,
682 whereas Emacs's ``M-x column-number-mode`` uses a 0-based convention.
683
684 For example, an error in the initial, left-hand
685 column of source line 3 is reported by GCC as::
686
687 some-file.c:3:1: error: ...etc...
688
689 On navigating to the location of that error in Emacs
690 (e.g. via ``next-error``), the locus is reported in the Mode Line
691 (assuming ``M-x column-number-mode``) as::
692
693 some-file.c 10% (3, 0)
694
695 i.e. ``3:1:`` in GCC corresponds to ``(3, 0)`` in Emacs.
696
697 .. py:class:: Range
698
699 Identification of a range of text within a source file.
700
701 It corresponds to the ``<range>`` XML element within
702 a Firehose XML document.
703
704 .. py:attribute:: start
705
706 (:py:class:`Point`)
707
708 .. py:attribute:: end
709
710 (:py:class:`Point`)
711
712 Capturing the circumstances leading up to a problem
713 ***************************************************
714
715 .. py:class:: Trace
716
717 An optional list of events within an :py:class:`Issue` that describe
718 the circumstances leading up to a problem.
719
720 It corresponds to the ``<trace>`` XML element within a
721 Firehose XML document.
722
723 See :ref:`example of a trace <trace_example>`.
724
725 .. py:attribute:: states
726
727 list of :py:class:`State`
728
729 .. py:class:: State
730
731 A state within a :py:class:`Trace`.
732
733 .. py:attribute:: location
734
735 :py:class:`Location`
736
737 .. py:attribute:: notes
738
739 :py:class:`Notes` or ``None``
740
741 ..
742 def __init__(self, location, notes):
743 assert isinstance(location, Location)
744 if notes is not None:
745 assert isinstance(notes, Notes)
746 self.location = location
747 self.notes = notes
748
749 Other data
750 **********
751
752 .. py:class:: CustomFields(OrderedDict)
753
754 A big escape-hatch in the data model: support for arbitrary, ordered
755 key/value pairs for roundtripping data specific to a particular
756 situation. e.g. debugging attributes for a particular failure
757
758 It corresponds to the ``<custom-fields>`` XML element within
759 a Firehose XML document.
760
761
762 .. TODO:
763
764 * .. py:class:: Visitor
765 * main