public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108968] New: fanalyzer false positive with the uninitalised-ness of the stack pointer
@ 2023-02-28 12:41 andrew.cooper3 at citrix dot com
  2023-02-28 13:12 ` [Bug c/108968] " schwab@linux-m68k.org
                   ` (18 more replies)
  0 siblings, 19 replies; 20+ messages in thread
From: andrew.cooper3 at citrix dot com @ 2023-02-28 12:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108968

            Bug ID: 108968
           Summary: fanalyzer false positive with the uninitalised-ness of
                    the stack pointer
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrew.cooper3 at citrix dot com
  Target Milestone: ---

I experimented with -fanalyzer on Xen, given all the recent work on Linux. 
We're quite similar, but one area where we are very different is accessing
per-cpu variables.

For architectural reasons (i.e. because we were virtualising Linux, and Linux
uses %gs for its per-cpu variables), Xen doesn't.  In Xen, we have a block of
metadata at the base of the stack, and the stack suitably aligned such that we
can do something like this:

  static inline struct cpu_info *get_cpu_info(void)
  {
    register unsigned long sp asm("rsp");

    return (struct cpu_info *)((sp | (STACK_SIZE - 1)) + 1) - 1;
  }

Which turns into roughly:

  ptr = ((rsp | 0x7fff) + 1) - sizeof(struct cpu_info)

which is correct and work suitably due to the alignment of the stack in the
first place.

Unfortunately, it triggers:

  ./arch/x86/include/asm/current.h:95:5: error: use of uninitialized value 'sp'
[CWE-457] [-Werror=analyzer-use-of-uninitialized-value]

reliably, every time macros such as `current` get expanded, which is
everywhere.


The reality is that the stack pointer is never uninitialised.  It is
unpredictable in the general case, but implementations can account for and
remove that unpredictability.

The normal trick to hide a variable from uninitialised handling (e.g. to asm(""
: "+g"(var)); ) doesn't work, as it suffers from the same error.

Is there any way to tell fanalyzer that this value really isn't uninitialised? 
I can't see anything obvious.


I can work around the warning by doing:

  unsigned long sp;
  asm ( "mov %%rsp, %0" : "=r" (sp) );

but this impacts code generation quite substantially.  This primitive is used
all over the place, and the regular C form undergoes far better CSE than the
explicit mov to retrieve the stack pointer.

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

end of thread, other threads:[~2023-03-29 18:19 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28 12:41 [Bug c/108968] New: fanalyzer false positive with the uninitalised-ness of the stack pointer andrew.cooper3 at citrix dot com
2023-02-28 13:12 ` [Bug c/108968] " schwab@linux-m68k.org
2023-02-28 13:44 ` andrew.cooper3 at citrix dot com
2023-02-28 13:59 ` schwab@linux-m68k.org
2023-02-28 15:41 ` andrew.cooper3 at citrix dot com
2023-02-28 18:56 ` [Bug analyzer/108968] " dmalcolm at gcc dot gnu.org
2023-02-28 19:04 ` andrew.cooper3 at citrix dot com
2023-03-02 19:02 ` cvs-commit at gcc dot gnu.org
2023-03-02 19:25 ` dmalcolm at gcc dot gnu.org
2023-03-02 21:15 ` andrew.cooper3 at citrix dot com
2023-03-02 21:20 ` andrew.cooper3 at citrix dot com
2023-03-02 21:25 ` dmalcolm at gcc dot gnu.org
2023-03-02 21:29 ` dmalcolm at gcc dot gnu.org
2023-03-02 21:34 ` andrew.cooper3 at citrix dot com
2023-03-02 21:35 ` andrew.cooper3 at citrix dot com
2023-03-02 21:41 ` andrew.cooper3 at citrix dot com
2023-03-02 21:46 ` dmalcolm at gcc dot gnu.org
2023-03-02 21:48 ` dmalcolm at gcc dot gnu.org
2023-03-02 21:52 ` dmalcolm at gcc dot gnu.org
2023-03-29 18:19 ` cvs-commit at gcc dot gnu.org

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