https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125222
Bug ID: 125222
Summary: Bogus ASAN stack-use-after-scope error when destroying
and reconstructing an object
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: sanitizer
Assignee: unassigned at gcc dot gnu.org
Reporter: rs2740 at gmail dot com
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
Target Milestone: ---
#include <cassert>
#include <new>
struct O {
union {int x;};
bool flag = false;
};
template <class F>
struct G {
explicit G(F&& f) : func_(f) {}
~G() {
if (should_execute_) {
func_();
}
}
F func_;
bool should_execute_ = true;
};
template <class F>
G(F) -> G<F>;
struct P {
O* value_;
void f(int u) {
assert(!value_->flag);
value_->~O();
G restore([p = value_] { });
::new (value_) O{u, true};
restore.should_execute_ = false;
}
};
int main() {
O x;
P p{&x};
p.f(1);
}
This spuriously errors with -std=c++17 -O3 -fsanitize=address
(https://compiler-explorer.com/z/qKW99jo9e) and appears to have done so since
GCC 9 (8.5 is happy with it):
=================================================================
==1==ERROR: AddressSanitizer: stack-use-after-scope on address 0x6e59969f0040
at pc 0x000000401359 bp 0x7ffc3c347030 sp 0x7ffc3c347028
WRITE of size 8 at 0x6e59969f0040 thread T0
#0 0x000000401358 in P::f(int) /app/example.cpp:31
#1 0x000000401358 in main /app/example.cpp:40
#2 0x725998a29d8f (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId:
095c7ba148aeca81668091f718047078d57efddb)
#3 0x725998a29e3f in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId:
095c7ba148aeca81668091f718047078d57efddb)
#4 0x000000401444 in _start (/app/output.s+0x401444) (BuildId:
dfebe76cf514401e95b936411293c48971444443)
Address 0x6e59969f0040 is located in stack of thread T0 at offset 64 in frame
#0 0x00000040111f in main /app/example.cpp:37
This frame has 3 object(s):
[32, 40) 'p' (line 39)
[64, 72) 'x' (line 38) <== Memory access at offset 64 is inside this
variable
[96, 112) 'restore' (line 30)
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-scope /app/example.cpp:31 in
P::f(int)
Shadow bytes around the buggy address:
0x6e59969efd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969efe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969efe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969eff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969eff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x6e59969f0000: f1 f1 f1 f1 00 f2 f2 f2[f8]f2 f2 f2 00 00 f3 f3
0x6e59969f0080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969f0100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969f0180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969f0200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x6e59969f0280: 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
==1==ABORTING
Various seemingly unrelated changes will unprovoke the error: remove the
assert; remove the lambda capture; change the G constructor to take by value;
removing the scope guard.