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