git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: general protection faults with "git grep" version 1.7.7.1

From
Thomas Rast <trast@student.ethz.ch>
Date
Oct 25, 2011, 16:00 UTC
Message-ID
<201110251800.28054.trast@student.ethz.ch>
In-Reply-To
<87y5w9ayoa.fsf@rho.meyering.net>
Jim Meyering wrote:
Show 7 quoted lines
> Thomas Rast wrote:
> > [GCC moves access to a file-static variable across pthread_mutex_lock()]
> 
> Thanks for the investigation.
> Actually, isn't gcc -O2's code-motion justified?
> While we *know* that those globals may be modified asynchronously,
> builtin/grep.c forgot to tell gcc about that.
I'm somewhat unwilling to believe that:
* "volatile" enforces three unrelated things, see e.g. [1].
* Removing "static" would do the same as it prevents the compiler from
  proving at compile-time that pthread_mutex_lock() cannot affect the
  variable in question.
  If this is correct, it also means that all code in all pthreads
  tutorials I can find works merely by the accident of not declaring
  their variables "static".
  Furthermore, a future smarter compiler with better link-time
  optimization might again prove the same and eliminate the
  "superfluous" load.
However, as a result of the discussion I now have a shorter testcase:
  #include <pthread.h>
  int y;
  static int x;
  pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  void test ()
  {
          y = x;
          pthread_mutex_lock(&m);
          x = x + 1;
          pthread_mutex_unlock(&m);
  }

GCC 4.6.1 on F16 again assumes 'x' was not modified across the lock. I also tested GCC 4.5.1 and 4.4.5, which instead issue a direct add-to-memory instruction

        addl    $1, x(%rip)
in the locked part.

In the event that you and GCC 4.6.1 are right, I still vote for removing 'static' instead of adding 'volatile' so as to allow basic optimizations.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch
Previous: Markus TrippelsdorfNext: Thomas Rast
Message 10 of 15 in “general protection faults with "git grep" version 1.7.7.1”
  1. Markus TrippelsdorfOct 24, 2011
  2. Richard W.M. JonesOct 24, 2011
  3. Markus TrippelsdorfOct 24, 2011
  4. Bernt HansenOct 25, 2011
  5. Jeff KingOct 25, 2011
  6. Bernt HansenOct 25, 2011
  7. Thomas RastOct 25, 2011
  8. Jim MeyeringOct 25, 2011
  9. Markus TrippelsdorfOct 25, 2011
  10. Thomas RastOct 25, 2011
  11. Thomas RastOct 25, 2011
  12. Jim MeyeringOct 25, 2011
  13. Thomas RastOct 25, 2011
  14. Jim MeyeringOct 25, 2011
  15. Jeff KingOct 25, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.