public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [pushed] analyzer: add SARIF property bags to -Wanalyzer-overlapping-buffers
Date: Wed, 10 Apr 2024 16:56:07 -0400	[thread overview]
Message-ID: <20240410205607.335758-1-dmalcolm@redhat.com> (raw)

Tested lightly by hand.
Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Successful run of analyzer integration tests on x86_64-pc-linux-gnu.
Pushed to trunk as r14-9899-g7a49d5dc0ef345.

gcc/analyzer/ChangeLog:
	* call-details.cc: Include "diagnostic-format-sarif.h".
	(overlapping_buffers::overlapping_buffers): Add params for new
	fields.
	(overlapping_buffers::maybe_add_sarif_properties): New.
	(overlapping_buffers::m_byte_range_a): New field.
	(overlapping_buffers::byte_range_b): New field.
	(overlapping_buffers::m_num_bytes_read_sval): New field.
	(call_details::complain_about_overlap): Pass new params to
	overlapping_buffers ctor.
	* ranges.cc (symbolic_byte_offset::to_json): New.
	(symbolic_byte_range::to_json): New.
	* ranges.h (symbolic_byte_offset::to_json): New decl.
	(symbolic_byte_range::to_json): New decl.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 gcc/analyzer/call-details.cc | 33 ++++++++++++++++++++++++++++++---
 gcc/analyzer/ranges.cc       | 15 +++++++++++++++
 gcc/analyzer/ranges.h        |  4 ++++
 3 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/gcc/analyzer/call-details.cc b/gcc/analyzer/call-details.cc
index 5b145a2ce638..ca47953f1461 100644
--- a/gcc/analyzer/call-details.cc
+++ b/gcc/analyzer/call-details.cc
@@ -38,6 +38,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "stringpool.h"
 #include "attribs.h"
 #include "make-unique.h"
+#include "diagnostic-format-sarif.h"
 
 #if ENABLE_ANALYZER
 
@@ -425,8 +426,14 @@ class overlapping_buffers
 : public pending_diagnostic_subclass<overlapping_buffers>
 {
 public:
-  overlapping_buffers (tree fndecl)
-  : m_fndecl (fndecl)
+  overlapping_buffers (tree fndecl,
+		       const symbolic_byte_range &byte_range_a,
+		       const symbolic_byte_range &byte_range_b,
+		       const svalue *num_bytes_read_sval)
+  : m_fndecl (fndecl),
+    m_byte_range_a (byte_range_a),
+    m_byte_range_b (byte_range_b),
+    m_num_bytes_read_sval (num_bytes_read_sval)
   {
   }
 
@@ -469,8 +476,25 @@ public:
        m_fndecl);
   }
 
+  void maybe_add_sarif_properties (sarif_object &result_obj)
+    const final override
+  {
+    sarif_property_bag &props = result_obj.get_or_create_properties ();
+#define PROPERTY_PREFIX "gcc/analyzer/overlapping_buffers/"
+    props.set (PROPERTY_PREFIX "bytes_range_a",
+	       m_byte_range_a.to_json ());
+    props.set (PROPERTY_PREFIX "bytes_range_b",
+	       m_byte_range_b.to_json ());
+    props.set (PROPERTY_PREFIX "num_bytes_read_sval",
+	       m_num_bytes_read_sval->to_json ());
+#undef PROPERTY_PREFIX
+  }
+
 private:
   tree m_fndecl;
+  symbolic_byte_range m_byte_range_a;
+  symbolic_byte_range m_byte_range_b;
+  const svalue *m_num_bytes_read_sval;
 };
 
 
@@ -517,7 +541,10 @@ call_details::complain_about_overlap (unsigned arg_idx_a,
   if (!byte_range_a.intersection (byte_range_b, *model).is_true ())
     return;
 
-  ctxt->warn (make_unique<overlapping_buffers> (get_fndecl_for_call ()));
+  ctxt->warn (make_unique<overlapping_buffers> (get_fndecl_for_call (),
+						byte_range_a,
+						byte_range_b,
+						num_bytes_read_sval));
 }
 
 } // namespace ana
diff --git a/gcc/analyzer/ranges.cc b/gcc/analyzer/ranges.cc
index ffdd0d4c5722..659ada7609d6 100644
--- a/gcc/analyzer/ranges.cc
+++ b/gcc/analyzer/ranges.cc
@@ -103,6 +103,12 @@ symbolic_byte_offset::dump (bool simple) const
   pp_flush (&pp);
 }
 
+json::value *
+symbolic_byte_offset::to_json () const
+{
+  return m_num_bytes_sval->to_json ();
+}
+
 tree
 symbolic_byte_offset::maybe_get_constant () const
 {
@@ -156,6 +162,15 @@ symbolic_byte_range::dump (bool simple, region_model_manager &mgr) const
   pp_flush (&pp);
 }
 
+json::value *
+symbolic_byte_range::to_json () const
+{
+  json::object *obj = new json::object ();
+  obj->set ("start", m_start.to_json ());
+  obj->set ("size", m_size.to_json ());
+  return obj;
+}
+
 bool
 symbolic_byte_range::empty_p () const
 {
diff --git a/gcc/analyzer/ranges.h b/gcc/analyzer/ranges.h
index 92d963b7a2bc..aca4554bde69 100644
--- a/gcc/analyzer/ranges.h
+++ b/gcc/analyzer/ranges.h
@@ -39,6 +39,8 @@ public:
   void dump_to_pp (pretty_printer *pp, bool) const;
   void dump (bool) const;
 
+  json::value *to_json () const;
+
   bool operator== (const symbolic_byte_offset &other) const
   {
    return m_num_bytes_sval == other.m_num_bytes_sval;
@@ -70,6 +72,8 @@ public:
 		   region_model_manager &mgr) const;
   void dump (bool, region_model_manager &mgr) const;
 
+  json::value *to_json () const;
+
   bool empty_p () const;
 
   symbolic_byte_offset get_start_byte_offset () const
-- 
2.26.3


                 reply	other threads:[~2024-04-10 20:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240410205607.335758-1-dmalcolm@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).