public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "priour.be at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/109365] New: Double delete yields -Wanalyzer-use-after-free instead of -Wanalyzer-double-free
Date: Fri, 31 Mar 2023 20:31:53 +0000	[thread overview]
Message-ID: <bug-109365-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109365
           Summary: Double delete yields -Wanalyzer-use-after-free instead
                    of -Wanalyzer-double-free
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: priour.be at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu
             Build: 13.0.1 20230328 (experimental)

Double delete does not result in a -Wanalyzer-double-free warning as expected,
but rather into -Wanalyzer-use-after-free warning. 

Using the following reproducer:
// file ../../double_delete_test.cpp
struct A {int a; int b;};
int main () {
    A* a = new A();
    delete a;
    delete a;
    return 0;
}

Then compiling with 
./xg++ -B. -S -fanalyzer -Wanalyzer-double-free ../../double_delete_test.cpp
../../double_delete_test.cpp: In function ‘int main()’:
../../double_delete_test.cpp:9:1: warning: use after ‘delete’ of ‘a’ [CWE-416]
[-Wanalyzer-use-after-free]
    9 | }
      | ^
  ‘int main()’: events 1-7
    |
    |    5 |     A* a = new A();
    |      |                  ^
    |      |                  |
    |      |                  (1) state of ‘&HEAP_ALLOCATED_REGION(10)’:
‘start’ -> ‘nonnull’ (NULL origin)
    |    6 |     delete a;
    |      |     ~~~~~~~~      
    |      |     |      |
    |      |     |      (3) ...to here
    |      |     |      (4) deleted here
    |      |     (2) following ‘true’ branch...
    |    7 |     delete a;
    |      |     ~~~~~~~~      
    |      |     |      |
    |      |     |      (6) ...to here
    |      |     (5) following ‘true’ branch...
    |    8 |     return 0;
    |    9 | }
    |      | ~                 
    |      | |
    |      | (7) use after ‘delete’ of ‘a’; deleted at (4)
    |

I also attempted with -fno-exception, but no impact was observer on the graphs
nor the output.
With the addition of -fanalyzer-fine-grained, I observed than each delete
statement is actually split into two:
delete a;
becomes in the ipa form
<bb 3> :
  *a.0_9 ={v} {CLOBBER};
  operator delete (a.0_9, 8);

The exploded-graph shows that the second '*a.1_12 ={v} {CLOBBER};' dereference
is responsible for the -Wanalyzer-use-after-free, and changes the state of the
allocated region from 'freed' to 'stop', which causes the actual following
'operator delete' to not be detected as a double free.

I am still familiarizing myself with the gimplification and ssa passes, so I'm
yet unsure as to how to tackle this.
I'm still looking into this though, and would gladly receive your pointers. 

(Note: sorry David, I've binged through bugzilla doc and gcc bugs page yet I
cannot seem to find the way to add this to the 'analyzer-c++' block, nor do I
see the option in the advanced fields.)

             reply	other threads:[~2023-03-31 20:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-31 20:31 priour.be at gmail dot com [this message]
2023-03-31 20:34 ` [Bug analyzer/109365] " dmalcolm at gcc dot gnu.org
2023-03-31 20:37 ` priour.be at gmail dot com
2023-07-05 11:31 ` vultkayn at gcc dot gnu.org
2023-07-07 11:28 ` vultkayn at gcc dot gnu.org
2023-07-21 22:22 ` dmalcolm at gcc dot gnu.org
2023-07-26 17:10 ` vultkayn at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109365-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).