diff options
author | David Malcolm <dmalcolm@redhat.com> | 2023-12-01 08:47:41 -0500 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2023-12-01 08:47:41 -0500 |
commit | 12b67d1e13b3cf14fb24cf2a1c008b73d377ff6d (patch) | |
tree | 4ad4d9f5e959e18e32a33726c3da47668220593c /gcc/analyzer/sm-signal.cc | |
parent | 83b210d55b28461e7604068c5df95a24b21e7081 (diff) | |
download | gcc-12b67d1e13b3cf14fb24cf2a1c008b73d377ff6d.zip gcc-12b67d1e13b3cf14fb24cf2a1c008b73d377ff6d.tar.gz gcc-12b67d1e13b3cf14fb24cf2a1c008b73d377ff6d.tar.bz2 |
diagnostics, analyzer: add optional per-diagnostic property bags to SARIF
I've found it useful in debugging the analyzer for the SARIF output to
contain extra analyzer-specific data in each diagnostic.
This patch:
* adds a way for a diagnostic_metadata to populate a property
bag within a SARIF "result" object based on a new vfunc
* reworks how diagnostics are emitted within the analyzer so
that a custom diagnostic_metadata subclass is used, which populates
the property bag with information from the saved_diagnostic, and with
a vfunc hook allowing for per-pending_diagnotic-subclass extra
properties.
Doing so makes it trivial to go from the SARIF output back to
pertinent parts of the analyzer's internals (e.g. the index of
the diagnostic within the ana::diagnostic_manager, the index of
the ana::exploded_node, etc).
It also replaces a lot of boilerplate in the "emit" implementations
in the various pending_diagnostics subclasses. In particular, doing
so fixes missing CVE metadata for -Wanalyzer-fd-phase-mismatch (where
sm-fd.cc's fd_phase_mismatch::emit was failing to use its
diagnostic_metadata instance).
gcc/analyzer/ChangeLog:
* analyzer.h (class saved_diagnostic): New forward decl.
* bounds-checking.cc: Update for changes to
pending_diagnostic::emit.
* call-details.cc: Likewise.
* diagnostic-manager.cc: Include "diagnostic-format-sarif.h".
(saved_diagnostic::maybe_add_sarif_properties): New.
(class pending_diagnostic_metadata): New.
(diagnostic_manager::emit_saved_diagnostic): Create a
pending_diagnostic_metadata and a diagnostic_emission_context.
Pass the latter to the pending_diagnostic::emit vfunc.
* diagnostic-manager.h
(saved_diagnostic::maybe_add_sarif_properties): New decl.
* engine.cc: Update for changes to pending_diagnostic::emit.
* infinite-loop.cc: Likewise.
* infinite-recursion.cc: Likewise.
* kf-analyzer.cc: Likewise.
* kf.cc: Likewise.
* pending-diagnostic.cc
(diagnostic_emission_context::get_pending_diagnostic): New.
(diagnostic_emission_context::warn): New.
(diagnostic_emission_context::inform): New.
* pending-diagnostic.h (class diagnostic_emission_context): New.
(pending_diagnostic::emit): Update params.
(pending_diagnostic::maybe_add_sarif_properties): New vfunc.
* region.cc: Don't include "diagnostic-metadata.h".
* region-model.cc: Include "diagnostic-format-sarif.h". Update
for changes to pending_diagnostic::emit.
(exposure_through_uninit_copy::maybe_add_sarif_properties): New.
* sm-fd.cc: Update for changes to pending_diagnostic::emit.
* sm-file.cc: Likewise.
* sm-malloc.cc: Likewise.
* sm-pattern-test.cc: Likewise.
* sm-sensitive.cc: Likewise.
* sm-signal.cc: Likewise.
* sm-taint.cc: Likewise.
* store.cc: Don't include "diagnostic-metadata.h".
* varargs.cc: Update for changes to pending_diagnostic::emit.
gcc/ChangeLog:
* diagnostic-core.h (emit_diagnostic_valist): New overload decl.
* diagnostic-format-sarif.cc (sarif_builder::make_result_object):
When we have metadata, call its maybe_add_sarif_properties vfunc.
* diagnostic-metadata.h (class sarif_object): Forward decl.
(diagnostic_metadata::~diagnostic_metadata): New.
(diagnostic_metadata::maybe_add_sarif_properties): New vfunc.
* diagnostic.cc (emit_diagnostic_valist): New overload.
gcc/testsuite/ChangeLog:
* gcc.dg/analyzer/fd-accept.c: Update for fix to missing CWE
metadata for -Wanalyzer-fd-phase-mismatch.
* gcc.dg/analyzer/fd-bind.c: Likewise.
* gcc.dg/analyzer/fd-socket-misuse.c: Likewise.
* gcc.dg/plugin/analyzer_cpython_plugin.c: Update for changes to
pending_diagnostic::emit.
* gcc.dg/plugin/analyzer_gil_plugin.c: Likewise.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/analyzer/sm-signal.cc')
-rw-r--r-- | gcc/analyzer/sm-signal.cc | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/gcc/analyzer/sm-signal.cc b/gcc/analyzer/sm-signal.cc index 9ebcbdb..6bca395 100644 --- a/gcc/analyzer/sm-signal.cc +++ b/gcc/analyzer/sm-signal.cc @@ -32,7 +32,6 @@ along with GCC; see the file COPYING3. If not see #include "options.h" #include "bitmap.h" #include "diagnostic-path.h" -#include "diagnostic-metadata.h" #include "analyzer/analyzer.h" #include "diagnostic-event-id.h" #include "analyzer/analyzer-logging.h" @@ -114,15 +113,13 @@ public: return OPT_Wanalyzer_unsafe_call_within_signal_handler; } - bool emit (rich_location *rich_loc, logger *) final override + bool emit (diagnostic_emission_context &ctxt) final override { auto_diagnostic_group d; - diagnostic_metadata m; /* CWE-479: Signal Handler Use of a Non-reentrant Function. */ - m.add_cwe (479); - if (warning_meta (rich_loc, m, get_controlling_option (), - "call to %qD from within signal handler", - m_unsafe_fndecl)) + ctxt.add_cwe (479); + if (ctxt.warn ("call to %qD from within signal handler", + m_unsafe_fndecl)) { /* If we know a possible alternative function, add a note suggesting the replacement. */ |