Bug 107086 - gcc trunk at -O1 failed to detect a stack-use-after-return
Summary: gcc trunk at -O1 failed to detect a stack-use-after-return
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: sanitizer (show other bugs)
Version: 13.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-09-29 12:14 UTC by Shaohua Li
Modified: 2022-09-29 14:44 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-09-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Shaohua Li 2022-09-29 12:14:43 UTC
%gcc-tk -v
Using built-in specs.
COLLECT_GCC=gcc-tk
COLLECT_LTO_WRAPPER=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-519196a27cf534e94f158733b25a4f9d10737031/libexec/gcc/x86_64-pc-linux-gnu/13.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --disable-bootstrap --enable-languages=c,c++ --prefix=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-519196a27cf534e94f158733b25a4f9d10737031
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.0.0 20220921 (experimental) (GCC)
%
%cat a.c
int a, b;
int *c;
int d() { return 0; }
static int e(int f) {
  int h[144];
  for (; b;)
    h;
  for (; a >= 0; a = -2)
    c = &f;
  return f;
}
int main() {
  e(0);
  *c = 0;
  if (d()) {
    int g[28];
    g;
  }
}
%
%
%gcc-tk -O1 -w -fsanitize=address a.c && ./a.out
%
%gcc-tk -O2 -w -fsanitize=address a.c && ./a.out
=================================================================
==491884==ERROR: AddressSanitizer: stack-use-after-return on address 0x7f9cd1000020 at pc 0x0000004010e1 bp 0x7ffe4ecb6150 sp 0x7ffe4ecb6148
WRITE of size 4 at 0x7f9cd1000020 thread T0
    #0 0x4010e0 in main /zdata/shaoli/SanitizerFuzz/csmith-only/asan/dangling-ptr-deref-prob-10/bugs/gcc_work102_8/a.c:14
    #1 0x7f9cd3a71082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 1878e6b475720c7c51969e69ab2d276fae6d1dee)
    #2 0x40114d in _start (/zdata/shaoli/SanitizerFuzz/csmith-only/asan/dangling-ptr-deref-prob-10/bugs/gcc_work102_8/a.out+0x40114d)

Address 0x7f9cd1000020 is located in stack of thread T0 at offset 32 in frame
    #0 0x40121f in e /zdata/shaoli/SanitizerFuzz/csmith-only/asan/dangling-ptr-deref-prob-10/bugs/gcc_work102_8/a.c:4

  This frame has 1 object(s):
    [32, 36) 'f' (line 4) <== Memory access at offset 32 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-use-after-return /zdata/shaoli/SanitizerFuzz/csmith-only/asan/dangling-ptr-deref-prob-10/bugs/gcc_work102_8/a.c:14 in main
Shadow bytes around the buggy address:
  0x0ff41a1f7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0ff41a1f8000: f5 f5 f5 f5[f5]f5 f5 f5 00 00 00 00 00 00 00 00
  0x0ff41a1f8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f8030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f8040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff41a1f8050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==491884==ABORTING
%

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

From the assembly code, we can see that the reason probably is the function `e()` got inlined so that the stack variable didn't get instrumented.
Comment 1 Martin Liška 2022-09-29 12:34:08 UTC
I can't reproduce it locally for some reason.
Anyway, it's a use-after-scope where an address of a function argument is taken. I'm not sure, but it's something we don't currently support.

Note clang catches that only with -O1+.