From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5E8043912584; Tue, 28 Jun 2022 14:28:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E8043912584 From: "tom.cosgrove at arm dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/106119] New: Bogus use-after-free warning triggered by optimizer Date: Tue, 28 Jun 2022 14:28:08 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: tom.cosgrove at arm dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Jun 2022 14:28:08 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106119 Bug ID: 106119 Summary: Bogus use-after-free warning triggered by optimizer Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: tom.cosgrove at arm dot com Target Milestone: --- There are a number of bug reports related to -Wuse-after-free - it's not cl= ear to me if our particular issue has already been captured, so please do close= as duplicate if it has (it seems that the optimiser is moving code around, so could potentially be a duplicate of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104215). Assuming not a duplicate, this should probably be linked to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104075. Our self-test code checks to see if two consecutive calloc() calls return t= he same value, by storing the pointer value in a uintptr_t integer and compari= ng against this value later. When we compile with optimisation (we don't see t= he problem with -O0) and with an if-statement later in the code, we get a spur= ious use-after-free warning. We don't get this warning at -O0, or if we remove the if-statement. We have also confirmed that if we reorder the assignment to the uintptr_t a= nd the call to free() we consistently DO get the use-after-free warning (as expected) regardless of optimisation level. gcc-12 $ uname -a Linux e120653 5.17.15-1-MANJARO #1 SMP PREEMPT Wed Jun 15 07:09:31 UTC 2022 x86_64 GNU/Linux gcc-12 $ gcc --version gcc (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. gcc-12 $ cat foo.c #include #include #include void calloc_self_test( __attribute__ ((unused)) int verbose ) { void *buffer1 =3D calloc( 1, 1 ); uintptr_t old_buffer1 =3D (uintptr_t) buffer1; free( buffer1 ); buffer1 =3D calloc( 1, 1 ); int same =3D ( old_buffer1 =3D=3D (uintptr_t) buffer1 ); if( verbose ) printf( " CALLOC(1 again): passed (%s address)\n", same ? "same" : "different" ); free( buffer1 ); } No warning at -O0: gcc-12 $ gcc -O0 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o foo -c foo.c gcc-12 $ But unexpected warning at -O2: gcc-12 $ gcc -O2 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o foo -c foo.c foo.c: In function =E2=80=98calloc_self_test=E2=80=99: foo.c:8:15: warning: pointer =E2=80=98buffer1=E2=80=99 may be used after = =E2=80=98free=E2=80=99 [-Wuse-after-free] 8 | uintptr_t old_buffer1 =3D (uintptr_t) buffer1; | ^~~~~~~~~~~ foo.c:9:5: note: call to =E2=80=98free=E2=80=99 here 9 | free( buffer1 ); | ^~~~~~~~~~~~~~~ Removing the if-statement quashes the warning: gcc-12 $ perl -ni -e 'print unless /if.*verbose/' foo.c gcc-12 $ cat foo.c #include #include #include void calloc_self_test( __attribute__ ((unused)) int verbose ) { void *buffer1 =3D calloc( 1, 1 ); uintptr_t old_buffer1 =3D (uintptr_t) buffer1; free( buffer1 ); buffer1 =3D calloc( 1, 1 ); int same =3D ( old_buffer1 =3D=3D (uintptr_t) buffer1 ); printf( " CALLOC(1 again): passed (%s address)\n", same ? "same" : "different" ); free( buffer1 ); } gcc-12 $ gcc -O0 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o foo -c foo.c gcc-12 $ gcc -O2 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o foo -c foo.c gcc-12 $ Checking that we always get the error when we swap the assignment and free() statements: gcc-12 $ cat bar.c #include #include #include void calloc_self_test( __attribute__ ((unused)) int verbose ) { void *buffer1 =3D calloc( 1, 1 ); free( buffer1 ); uintptr_t old_buffer1 =3D (uintptr_t) buffer1; buffer1 =3D calloc( 1, 1 ); int same =3D ( old_buffer1 =3D=3D (uintptr_t) buffer1 ); if( verbose ) printf( " CALLOC(1 again): passed (%s address)\n", same ? "same" : "different" ); free( buffer1 ); } gcc-12 $ gcc -O0 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o bar -c bar.c bar.c: In function =E2=80=98calloc_self_test=E2=80=99: bar.c:9:15: warning: pointer =E2=80=98buffer1=E2=80=99 used after =E2=80=98= free=E2=80=99 [-Wuse-after-free] 9 | uintptr_t old_buffer1 =3D (uintptr_t) buffer1; | ^~~~~~~~~~~ bar.c:8:5: note: call to =E2=80=98free=E2=80=99 here 8 | free( buffer1 ); | ^~~~~~~~~~~~~~~ gcc-12 $ gcc -O2 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o bar -c bar.c bar.c: In function =E2=80=98calloc_self_test=E2=80=99: bar.c:9:15: warning: pointer =E2=80=98buffer1=E2=80=99 used after =E2=80=98= free=E2=80=99 [-Wuse-after-free] 9 | uintptr_t old_buffer1 =3D (uintptr_t) buffer1; | ^~~~~~~~~~~ bar.c:8:5: note: call to =E2=80=98free=E2=80=99 here 8 | free( buffer1 ); and we still get the warning (as we would hope) without the if-statement: gcc-12 $ perl -ni -e 'print unless /if.*verbose/' bar.c gcc-12 $ cat bar.c #include #include #include void calloc_self_test( __attribute__ ((unused)) int verbose ) { void *buffer1 =3D calloc( 1, 1 ); free( buffer1 ); uintptr_t old_buffer1 =3D (uintptr_t) buffer1; buffer1 =3D calloc( 1, 1 ); int same =3D ( old_buffer1 =3D=3D (uintptr_t) buffer1 ); printf( " CALLOC(1 again): passed (%s address)\n", same ? "same" : "different" ); free( buffer1 ); } gcc-12 $ gcc -O0 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o bar -c bar.c bar.c: In function =E2=80=98calloc_self_test=E2=80=99: bar.c:9:15: warning: pointer =E2=80=98buffer1=E2=80=99 used after =E2=80=98= free=E2=80=99 [-Wuse-after-free] 9 | uintptr_t old_buffer1 =3D (uintptr_t) buffer1; | ^~~~~~~~~~~ bar.c:8:5: note: call to =E2=80=98free=E2=80=99 here 8 | free( buffer1 ); | ^~~~~~~~~~~~~~~ gcc-12 $ gcc -O2 -Wall -Wextra -Wformat=3D2 -Wno-format-nonliteral -o bar -c bar.c bar.c: In function =E2=80=98calloc_self_test=E2=80=99: bar.c:9:15: warning: pointer =E2=80=98buffer1=E2=80=99 used after =E2=80=98= free=E2=80=99 [-Wuse-after-free] 9 | uintptr_t old_buffer1 =3D (uintptr_t) buffer1; | ^~~~~~~~~~~ bar.c:8:5: note: call to =E2=80=98free=E2=80=99 here 8 | free( buffer1 ); | ^~~~~~~~~~~~~~~=