Bug 97414

Summary: AddressSanitizer CHECK failed: detect_stack_use_after_return and detect_invalid_pointer_pairs
Product: gcc Reporter: Paweł Bylica <chfast>
Component: sanitizerAssignee: Martin Liška <marxin>
Status: RESOLVED FIXED    
Severity: normal CC: dimhen, dodji, dvyukov, jakub, kcc, mail, marxin
Priority: P3    
Version: 10.2.0   
Target Milestone: ---   
URL: https://reviews.llvm.org/D89552
Host: Target:
Build: Known to work: 10.2.1, 11.0
Known to fail: Last reconfirmed: 2020-10-15 00:00:00

Description Paweł Bylica 2020-10-14 07:51:47 UTC
==638106==AddressSanitizer CHECK failed: ../../../../src/libsanitizer/asan/asan_thread.cpp:369 "((bottom)) != (0)" (0x0, 0x0)
    #0 0x7f00888e08b8  (/lib/x86_64-linux-gnu/libasan.so.6+0xb98b8)
    #1 0x7f00889007ce  (/lib/x86_64-linux-gnu/libasan.so.6+0xd97ce)
    #2 0x7f00888e64f0  (/lib/x86_64-linux-gnu/libasan.so.6+0xbf4f0)
    #3 0x7f00888dd68b  (/lib/x86_64-linux-gnu/libasan.so.6+0xb668b)
    #4 0x7f00888e0269 in __sanitizer_ptr_sub (/lib/x86_64-linux-gnu/libasan.so.6+0xb9269)
    #5 0x55e8cd6641f2 in pointer_diff(int const*, int const*) /home/chfast/Projects/compiler_bugs/sanitizers/pointer_subtract_crash/pointer_subtract_crash.cpp:2
    #6 0x55e8cd664248 in main /home/chfast/Projects/compiler_bugs/sanitizers/pointer_subtract_crash/pointer_subtract_crash.cpp:10
    #7 0x7f008865c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #8 0x55e8cd66410d in _start (/home/chfast/Projects/compiler_bugs/sanitizers/pointer_subtract_crash/a.out+0x110d)


When running the program

[[gnu::noinline]] auto pointer_diff(const int *begin, const int *end) {
  return end - begin;
}

int main() {
  constexpr auto size = (2048 / sizeof(int)) + 1;

  auto buf = new int[size];
  auto end = buf + size;
  pointer_diff(end, buf);
  delete[] buf;

  return 0;
}


compiled with
gcc -fsanitize=address,pointer-subtract -g pointer_subtract_crash.cpp

To reproduce the crash, both runtime options must be enabled:
ASAN_OPTIONS=detect_stack_use_after_return=1:detect_invalid_pointer_pairs=1

This bug was previously reported in LLVM's AddressSanitizer project https://bugs.llvm.org/show_bug.cgi?id=47626, but pointer-subtract is not supported there.
Comment 1 Martin Liška 2020-10-15 15:24:47 UTC
Confirmed, I'll take a look at it.
Thank you for the bug report.
Comment 2 Martin Liška 2020-10-16 09:53:09 UTC
*** Bug 97416 has been marked as a duplicate of this bug. ***
Comment 3 Martin Liška 2020-10-16 13:41:17 UTC
I've just an upstream fix:
https://reviews.llvm.org/D89552
Comment 4 Martin Liška 2020-10-20 10:04:23 UTC
Waiting for upstream to apply the fix.
Comment 5 CVS Commits 2020-10-21 07:31:00 UTC
The master branch has been updated by Martin Liska <marxin@gcc.gnu.org>:

https://gcc.gnu.org/g:6c5b08a2ca935c5db68e79d33e5c5b752252115c

commit r11-4177-g6c5b08a2ca935c5db68e79d33e5c5b752252115c
Author: Martin Liska <mliska@suse.cz>
Date:   Wed Oct 21 09:28:56 2020 +0200

    libsanitizer: add test-case
    
    gcc/testsuite/ChangeLog:
    
            PR sanitizer/97414
            * g++.dg/asan/pr97414.C: New test.
Comment 6 Martin Liška 2020-10-21 07:33:39 UTC
Fixed on master so far.
Comment 7 CVS Commits 2020-10-21 09:22:41 UTC
The releases/gcc-10 branch has been updated by Martin Liska <marxin@gcc.gnu.org>:

https://gcc.gnu.org/g:98cd020ebc50a197de17aecd15afdff718cb06d3

commit r10-8932-g98cd020ebc50a197de17aecd15afdff718cb06d3
Author: Martin Liska <mliska@suse.cz>
Date:   Wed Oct 21 09:28:56 2020 +0200

    libsanitizer: add test-case
    
    gcc/testsuite/ChangeLog:
    
            PR sanitizer/97414
            * g++.dg/asan/pr97414.C: New test.
    
    (cherry picked from commit 6c5b08a2ca935c5db68e79d33e5c5b752252115c)
Comment 8 Martin Liška 2020-10-21 09:23:06 UTC
Fixed on all active branches.