Re: Safer vararg calls

2022-06-25 Thread Yair Lenga via Gcc
Hi Jonathan, thanks for taking the time to review.

I agree with your comment about the attribute name (va_vector, va_type). My
best improvement is "va_sametype". Is it better ? ? May be "va_matchtype" ?
any other suggestions ?

For the case of the sentinel/va_sametype, I hope that the implementation
will recognize the combination of the two, and will allow NULL to be used
as-is, without having to cast it. Altough I believe that NULL pointers are
considered compatible with with any pointer. Not 100% sure about this.

I'm not sure I understand the question about mixing char * and const char
*. Probably I cause confusion with my rushed example, which should be:

  // join all parameters, return newly allocate string.
__attribute__ ((malloc(free), va_matchtype, sentinel)) char
*delimitedstr(char delim, const char *p1, ...);


On Tue, Jun 21, 2022 at 6:44 AM Jonathan Wakely 
wrote:

> On Tue, 21 Jun 2022 at 11:17, Yair Lenga via Gcc  wrote:
> >
> > Hi,
> >
> > Looking for feedback on the adding new attribute to function calls that
> will help create safer vararg functions.
> >
> > Consider the case where a vararg function takes list of arguments of the
> same type. In my case, there are terminated with a sentinel of null.
> >
> > Char *result = delimitedstr(‘:’ “foo”, “bar”, “zoo”, NULL) ;
> >
> > The standard prototype
> > is char * delimitedstr(char delim, char *p1…) ;
> >
> > Which will currently allow many incorrect calls:
> >  delimitedstr(‘:’, “foo”, 5, 7.3, ‘a’) ;// bad types + missing
> sentinel.
> >
> > The __attribute__((sentinel)) can force the last arg to be null.
> >
> > My proposal is to add new attribute ((va_vector)) that will add a check
> that all parameters in a vararg list match the typeof the last parameter.
> So that:
>
> "va_vector" is a bad name IMHO. It tells me nothing about what it
> means. Does it have something to do with SIMD vectors?
>
> >
> > __attribute__ ((va_typed)) delimitedstr(char delim, char *p1…) ;
>
> "va_typed" at least suggests something to do with types, but it
> doesn't tell me they have to be the same type.
>
> >
> > Will flag a call where any of the parameter after p1, is not a string.
>
> In your example NULL does not have the same type as the earlier
> arguments. You would have to write (char*)NULL to suppress a
> diagnostic.
>
> I also wonder how a mixture of char* and const char* arguments would
> be handled in your example.
>
>
> >
> > This can result in cleaner, safer code, without making the calling
> sequence more difficult, or modifying the behavior of the call.
> >
> > For Java developers, this is basically the same type checking provided
> by the as ‘datatype …’ (without the conversion into array).
> >
> > I am Looking for feedback, Pointers on how to implement, as I do not
> have experience with extending gcc.
> >
> > Yair
>


[PATCH] analyzer PR 106003

2022-06-25 Thread Mir Immad via Gcc
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index b6dcc45a58a..04631f737ea 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1269,6 +1269,7 @@ ANALYZER_OBJS = \
  analyzer/region-model-reachability.o \
  analyzer/sm.o \
  analyzer/sm-file.o \
+ analyzer/sm-fd.o \
  analyzer/sm-malloc.o \
  analyzer/sm-pattern-test.o \
  analyzer/sm-sensitive.o \
diff --git a/gcc/analyzer/analyzer.opt b/gcc/analyzer/analyzer.opt
index 23dfc797cea..219f6180130 100644
--- a/gcc/analyzer/analyzer.opt
+++ b/gcc/analyzer/analyzer.opt
@@ -66,6 +66,26 @@ Wanalyzer-exposure-through-output-file
 Common Var(warn_analyzer_exposure_through_output_file) Init(1) Warning
 Warn about code paths in which sensitive data is written to a file.

+Wanalyzer-fd-access-mode-mismatch
+Common Var(warn_analyzer_fd_mode_mismatch) Init(1) Warning
+Warn about code paths in which read on write-only file descriptor is
attempted, or vice versa.
+
+Wanalyzer-fd-double-close
+Common Var(warn_analyzer_fd_double_close) Init(1) Warning
+Warn about code paths in which a file descriptor can be closed more than
once.
+
+Wanalyzer-fd-leak
+Common Var(warn_analyzer_fd_leak) Init(1) Warning
+Warn about code paths in which a file descriptor is not closed.
+
+Wanalyzer-fd-use-after-close
+Common Var(warn_analyzer_fd_use_after_close) Init(1) Warning
+Warn about code paths in which a read or write is performed on a closed
file descriptor.
+
+Wanalyzer-fd-use-without-check
+Common Var(warn_analyzer_fd_use_without_check) Init(1) Warning
+Warn about code paths in which a file descriptor is used without being
checked for validity.
+
 Wanalyzer-file-leak
 Common Var(warn_analyzer_file_leak) Init(1) Warning
 Warn about code paths in which a stdio FILE is not closed.
diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
new file mode 100644
index 000..83eb0df724d
--- /dev/null
+++ b/gcc/analyzer/sm-fd.cc
@@ -0,0 +1,821 @@
+/* A state machine for detecting misuses of POSIX file descriptor APIs.
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
+   Contributed by Immad Mir .
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC 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
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "function.h"
+#include "basic-block.h"
+#include "gimple.h"
+#include "options.h"
+#include "diagnostic-path.h"
+#include "diagnostic-metadata.h"
+#include "function.h"
+#include "json.h"
+#include "analyzer/analyzer.h"
+#include "diagnostic-event-id.h"
+#include "analyzer/analyzer-logging.h"
+#include "analyzer/sm.h"
+#include "analyzer/pending-diagnostic.h"
+#include "analyzer/function-set.h"
+#include "analyzer/analyzer-selftests.h"
+#include "tristate.h"
+#include "selftest.h"
+#include "analyzer/call-string.h"
+#include "analyzer/program-point.h"
+#include "analyzer/store.h"
+#include "analyzer/region-model.h"
+
+#include 
+#if ENABLE_ANALYZER
+
+namespace ana
+{
+
+namespace
+{
+
+/* An enum for distinguishing between three different access modes. */
+
+enum access_mode
+{
+  READ_WRITE,
+  READ_ONLY,
+  WRITE_ONLY
+};
+
+class fd_state_machine : public state_machine
+{
+public:
+  fd_state_machine (logger *logger);
+
+  bool
+  inherited_state_p () const final override
+  {
+return false;
+  }
+
+  state_machine::state_t
+  get_default_state (const svalue *sval) const final override
+  {
+if (tree cst = sval->maybe_get_constant ())
+  {
+int val = TREE_INT_CST_LOW (cst);
+if (val >= 0)
+  return m_constant_fd;
+else
+  return m_invalid;
+  }
+return m_start;
+  }
+
+  bool on_stmt (sm_context *sm_ctxt, const supernode *node,
+const gimple *stmt) const final override;
+
+  void on_condition (sm_context *sm_ctxt, const supernode *node,
+ const gimple *stmt, const svalue *lhs, const
tree_code op,
+ const svalue *rhs) const final override;
+
+  bool can_purge_p (state_t s) const final override;
+  pending_diagnostic *on_leak (tree var) const final override;
+
+  bool is_unchecked (state_t s) const;
+  bool is_valid (state_t s) const;
+  enum access_mode get_access_mode_from_flag (int flag) const;
+  enum access_mode get_access_mode_from_state (state_t s) const;
+  bool check_for_closed_fd (state_t s) const;
+  bool check_for_invalid_fd (state_t s) const;
+  bool check_for_constant_fd (state_t s) const;
+
+  /* State for a constant file desc

gcc-12-20220625 is now available

2022-06-25 Thread GCC Administrator via Gcc
Snapshot gcc-12-20220625 is now available on
  https://gcc.gnu.org/pub/gcc/snapshots/12-20220625/
and on various mirrors, see http://gcc.gnu.org/mirrors.html for details.

This snapshot has been generated from the GCC 12 git branch
with the following options: git://gcc.gnu.org/git/gcc.git branch 
releases/gcc-12 revision ea754f9dd27ffdea55b8fea3b868a8fb4f7bf0af

You'll find:

 gcc-12-20220625.tar.xz   Complete GCC

  SHA256=9da015691476327cd327068cc9398dcf6e3b77148b345dbfb64f04510b729440
  SHA1=a63679066efb50add66312fae22dcc106423ecbf

Diffs from 12-20220618 are available in the diffs/ subdirectory.

When a particular snapshot is ready for public consumption the LATEST-12
link is updated and a message is sent to the gcc list.  Please do not use
a snapshot before it has been announced that way.


GOMP and gomp

2022-06-25 Thread Mohamed Atef via Gcc
What is the difference between functions with GOMP prefixes and the other
with gomp?

Mohamed