public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286]
@ 2022-07-23 16:38 Immad Mir
  2022-07-23 17:43 ` David Malcolm
  0 siblings, 1 reply; 4+ messages in thread
From: Immad Mir @ 2022-07-23 16:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: dmalcolm, mirimnan017, Immad Mir

This patch adds get_meaning_for_state_change vfunc to
fd_diagnostic in sm-fd.cc which could be used by SARIF output.

Lightly tested in x86_64 Linux.

gcc/analyzer/ChangeLog:
	PR analyzer/106286
	* sm-fd.cc:
	(fd_diagnostic::get_meaning_for_state_change): New.

Signed-off-by: Immad Mir <mirimmad@outlook.com>
---
 gcc/analyzer/sm-fd.cc | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
index c3dac48509e..f77b1f4d3e2 100644
--- a/gcc/analyzer/sm-fd.cc
+++ b/gcc/analyzer/sm-fd.cc
@@ -229,6 +229,22 @@ public:
     return label_text ();
   }
 
+  diagnostic_event::meaning
+  get_meaning_for_state_change (
+      const evdesc::state_change &change) const final override
+  {
+    if (change.m_old_state == m_sm.get_start_state ()
+            && (change.m_new_state == m_sm.m_unchecked_read_write
+        || change.m_new_state == m_sm.m_unchecked_read_only
+        || change.m_new_state == m_sm.m_unchecked_write_only))
+      return diagnostic_event::meaning (diagnostic_event::VERB_acquire,
+                                        diagnostic_event::NOUN_resource);
+    if (change.m_new_state == m_sm.m_closed)
+      return diagnostic_event::meaning (diagnostic_event::VERB_release,
+                                        diagnostic_event::NOUN_resource);
+    return diagnostic_event::meaning ();
+  }
+
 protected:
   const fd_state_machine &m_sm;
   tree m_arg;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286]
  2022-07-23 16:38 [PATCH] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286] Immad Mir
@ 2022-07-23 17:43 ` David Malcolm
  0 siblings, 0 replies; 4+ messages in thread
From: David Malcolm @ 2022-07-23 17:43 UTC (permalink / raw)
  To: mirimnan017, gcc-patches; +Cc: Immad Mir

On Sat, 2022-07-23 at 22:08 +0530, Immad Mir wrote:
> This patch adds get_meaning_for_state_change vfunc to
> fd_diagnostic in sm-fd.cc which could be used by SARIF output.
> 
> Lightly tested in x86_64 Linux.
> 
> gcc/analyzer/ChangeLog:
>         PR analyzer/106286
>         * sm-fd.cc:
>         (fd_diagnostic::get_meaning_for_state_change): New.
> 
> Signed-off-by: Immad Mir <mirimmad@outlook.com>
> ---
>  gcc/analyzer/sm-fd.cc | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
> index c3dac48509e..f77b1f4d3e2 100644
> --- a/gcc/analyzer/sm-fd.cc
> +++ b/gcc/analyzer/sm-fd.cc
> @@ -229,6 +229,22 @@ public:
>      return label_text ();
>    }
>  
> +  diagnostic_event::meaning
> +  get_meaning_for_state_change (
> +      const evdesc::state_change &change) const final override
> +  {
> +    if (change.m_old_state == m_sm.get_start_state ()
> +            && (change.m_new_state == m_sm.m_unchecked_read_write
> +        || change.m_new_state == m_sm.m_unchecked_read_only
> +        || change.m_new_state == m_sm.m_unchecked_write_only))

I think you can simplify this by using:

   m_sm.is_unchecked_fd_p (change.m_new_state)

for the right-hand side of the &&.


[...snip...]

Other than that, patch looks OK, but please add a test case for this
e.g. "fd-meaning.c"; see:

  gcc/testsuite/gcc.dg/analyzer/file-meaning-1.c

for an analogous one for the sm-file.cc (since otherwise it's too easy
for this kind of thing to regress).

Dave


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286]
  2022-07-26 16:37 Immad Mir
@ 2022-07-26 16:42 ` David Malcolm
  0 siblings, 0 replies; 4+ messages in thread
From: David Malcolm @ 2022-07-26 16:42 UTC (permalink / raw)
  To: mirimnan017, gcc-patches; +Cc: Immad Mir

On Tue, 2022-07-26 at 22:07 +0530, Immad Mir wrote:
> This patch adds get_meaning_for_state_change vfunc to
> fd_diagnostic in sm-fd.cc which could be used by SARIF output.
> 
> Lightly tested on x86_64 Linux.

Thanks - looks good for trunk.

Dave



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286]
@ 2022-07-26 16:37 Immad Mir
  2022-07-26 16:42 ` David Malcolm
  0 siblings, 1 reply; 4+ messages in thread
From: Immad Mir @ 2022-07-26 16:37 UTC (permalink / raw)
  To: gcc-patches; +Cc: dmalcolm, mirimnan017, Immad Mir

This patch adds get_meaning_for_state_change vfunc to
fd_diagnostic in sm-fd.cc which could be used by SARIF output.

Lightly tested on x86_64 Linux.

gcc/analyzer/ChangeLog:
	PR analyzer/106286
	* sm-fd.cc:
	(fd_diagnostic::get_meaning_for_state_change): New.

gcc/testsuite/ChangeLog:
	PR analyzer/106286
	* gcc.dg/analyzer/fd-meaning.c: New test.

Signed-off-by: Immad Mir <mirimmad@outlook.com>
---
 gcc/analyzer/sm-fd.cc                      | 14 ++++++++
 gcc/testsuite/gcc.dg/analyzer/fd-meaning.c | 37 ++++++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/analyzer/fd-meaning.c

diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
index 56b0063ba42..ed923ade100 100644
--- a/gcc/analyzer/sm-fd.cc
+++ b/gcc/analyzer/sm-fd.cc
@@ -229,6 +229,20 @@ public:
     return label_text ();
   }
 
+  diagnostic_event::meaning
+  get_meaning_for_state_change (
+      const evdesc::state_change &change) const final override
+  {
+    if (change.m_old_state == m_sm.get_start_state ()
+		&& (m_sm.is_unchecked_fd_p (change.m_new_state)))
+      return diagnostic_event::meaning (diagnostic_event::VERB_acquire,
+			 diagnostic_event::NOUN_resource);
+    if (change.m_new_state == m_sm.m_closed)
+      return diagnostic_event::meaning (diagnostic_event::VERB_release,
+			 diagnostic_event::NOUN_resource);
+    return diagnostic_event::meaning ();
+  }
+
 protected:
   const fd_state_machine &m_sm;
   tree m_arg;
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
new file mode 100644
index 00000000000..6a9ec921fd3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
@@ -0,0 +1,37 @@
+  /* { dg-additional-options "-fanalyzer-verbose-state-changes" } */
+int open(const char *, int mode);
+void close(int fd);
+
+#define O_RDONLY 0
+#define O_WRONLY 1
+#define O_RDWR 2
+
+void test_1 (const char* path)
+{
+    int fd = open (path, O_RDWR); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
+    if (fd != -1)
+    {
+        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
+        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
+    }
+}
+
+void test_2 (const char* path)
+{
+    int fd = open (path, O_RDONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
+    if (fd != -1)
+    {
+        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
+        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
+    }
+}
+
+void test_3 (const char* path)
+{
+    int fd = open (path, O_WRONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
+    if (fd != -1)
+    {
+        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
+        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
+    }
+}
\ No newline at end of file
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-26 16:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-23 16:38 [PATCH] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286] Immad Mir
2022-07-23 17:43 ` David Malcolm
2022-07-26 16:37 Immad Mir
2022-07-26 16:42 ` David Malcolm

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).