public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug sanitizer/108094] New: gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return
@ 2022-12-14  8:54 shaohua.li at inf dot ethz.ch
  2022-12-14  9:43 ` [Bug sanitizer/108094] " marxin at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2022-12-14  8:54 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108094
           Summary: gcc trunk's ASAN at -O2 and above did not report a
                    stack-use-after-return
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shaohua.li at inf dot ethz.ch
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org
  Target Milestone: ---

ASAN at -O0/1 reported the stack-use-after-return bug, however -O2 and above
only reported SEGV. I checked the pointer addresses and noted this may due to
over-optimization of UB behavior, i.e., variable a is optimized to NULL. Not
sure if this is an issue or not.

Clang can detect it at all optimization levels.

Compiler explorer: https://godbolt.org/z/bYPxe31Mx

% cat a.c
int *a;
int d;
int *e() {
  int b[1]={0};
  __builtin_printf("&b[0]=%p\n", &b[0]);
  int *c = &b[0];
  __builtin_printf("c=%p\n", c);
  return c;
}
int main() {
  a = e();
  __builtin_printf("a=%p\n", a);
  d = *a;
  return d;
}
% gcc-tk -O0 -fsanitize=address a.c -g
% AN_OPTIONS=detect_stack_use_after_return=1 ./a.out 
&b[0]=0x7fe7cc000020
c=0x7fe7cc000020
a=0x7fe7cc000020
=================================================================
==1645405==ERROR: AddressSanitizer: stack-use-after-return on address
0x7fe7cc000020 at pc 0x000000401325 bp 0x7ffe8e2fe990 sp 0x7ffe8e2fe988
READ of size 4 at 0x7fe7cc000020 thread T0
    #0 0x401324 in main /a.c:13
    #1 0x7fe7cea78082 in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId:
1878e6b475720c7c51969e69ab2d276fae6d1dee)
    #2 0x4010dd in _start (/a.out+0x4010dd)

Address 0x7fe7cc000020 is located in stack of thread T0 at offset 32 in frame
    #0 0x4011a5 in e /a.c:3

  This frame has 1 object(s):
    [32, 36) 'b' (line 4) <== Memory access at offset 32 is inside this
variable
...
%
% gcc-tk -O2 -fsanitize=address a.c -g
% AN_OPTIONS=detect_stack_use_after_return=1 ./a.out 
&b[0]=0x7f3f2a100020
c=0x7f3f2a100020
a=(nil)
AddressSanitizer:DEADLYSIGNAL
=================================================================
==1885716==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc
0x000000401107 bp 0x000000000000 sp 0x7fff49942b00 T0)
==1885716==The signal is caused by a READ memory access.
==1885716==Hint: address points to the zero page.
    #0 0x401107 in main /a.c:13
    #1 0x7f3f2cb26082 in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId:
1878e6b475720c7c51969e69ab2d276fae6d1dee)
    #2 0x40117d in _start (/a.out+0x40117d)
...
%

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

* [Bug sanitizer/108094] gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return
  2022-12-14  8:54 [Bug sanitizer/108094] New: gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return shaohua.li at inf dot ethz.ch
@ 2022-12-14  9:43 ` marxin at gcc dot gnu.org
  2022-12-14 10:19 ` shaohua.li at inf dot ethz.ch
  2022-12-14 10:21 ` marxin at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-12-14  9:43 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
gcc pr108094.c -g -Wall -Werror -O2
pr108094.c: In function ‘e’:
pr108094.c:8:10: error: function returns address of local variable
[-Werror=return-local-addr]
    8 |   return c;
      |          ^
pr108094.c:4:7: note: declared here
    4 |   int b[1]={0};
      |       ^
cc1: all warnings being treated as errors

The warning is quite obvious and should not be ignored.

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

* [Bug sanitizer/108094] gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return
  2022-12-14  8:54 [Bug sanitizer/108094] New: gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return shaohua.li at inf dot ethz.ch
  2022-12-14  9:43 ` [Bug sanitizer/108094] " marxin at gcc dot gnu.org
@ 2022-12-14 10:19 ` shaohua.li at inf dot ethz.ch
  2022-12-14 10:21 ` marxin at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2022-12-14 10:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Li Shaohua <shaohua.li at inf dot ethz.ch> ---
(In reply to Martin Liška from comment #1)
> gcc pr108094.c -g -Wall -Werror -O2
> pr108094.c: In function ‘e’:
> pr108094.c:8:10: error: function returns address of local variable
> [-Werror=return-local-addr]
>     8 |   return c;
>       |          ^
> pr108094.c:4:7: note: declared here
>     4 |   int b[1]={0};
>       |       ^
> cc1: all warnings being treated as errors
> 
> The warning is quite obvious and should not be ignored.

So there is indeed a stack-use-after-return, but gcc's ASAN just does not
report it at -O2?

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

* [Bug sanitizer/108094] gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return
  2022-12-14  8:54 [Bug sanitizer/108094] New: gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return shaohua.li at inf dot ethz.ch
  2022-12-14  9:43 ` [Bug sanitizer/108094] " marxin at gcc dot gnu.org
  2022-12-14 10:19 ` shaohua.li at inf dot ethz.ch
@ 2022-12-14 10:21 ` marxin at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-12-14 10:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
The compiler optimizes it out as it's UBSAN and provides you a reasonable
warning.

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

end of thread, other threads:[~2022-12-14 10:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-14  8:54 [Bug sanitizer/108094] New: gcc trunk's ASAN at -O2 and above did not report a stack-use-after-return shaohua.li at inf dot ethz.ch
2022-12-14  9:43 ` [Bug sanitizer/108094] " marxin at gcc dot gnu.org
2022-12-14 10:19 ` shaohua.li at inf dot ethz.ch
2022-12-14 10:21 ` marxin 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).