Bug 97069 - Zero valued #line directive results in excessively large blocks of memory being allocated
Summary: Zero valued #line directive results in excessively large blocks of memory bei...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: gcov-profile (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Martin Liška
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: patch
Depends on:
Blocks:
 
Reported: 2020-09-16 12:42 UTC by Martin Ogden
Modified: 2020-10-02 10:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 10.2.1, 11.0
Known to fail: 8.4.0, 9.3.0
Last reconfirmed: 2020-09-21 00:00:00


Attachments
Associated main.i file (127 bytes, text/plain)
2020-09-16 12:42 UTC, Martin Ogden
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Ogden 2020-09-16 12:42:39 UTC
Created attachment 49225 [details]
Associated main.i file

Adding a #line directive with a line number of 0 causes gcov to attempt to allocate large (~2G) blocks of memory. To reproduce, consider main.c:

    int main()
    {
    }

    #line 0 "main.c"
    void zero_line_directive()
    {
    }

Compile and attempt to run gcov:

    $ gcc main.c -Wall -Werror --coverage
    $ ./a.out
    $ gcov main.c # gcov a-main.c for gcc-11
    terminate called after throwing an instance of 'std::bad_alloc'
    what():  std::bad_alloc

The bad allocation is made in gcc/gcc/gcov.c at line 1346:

    s->lines.resize(last_line + 1);

A line number of 0 causes `last_line` to be large (often 0x100000, sometimes larger ~10^8).


System details
--------------

Fault occurs on Ubuntu 16.04.6 with with gcc-{5,7,8,9,11} and gcov-{5,7,8,9,11} (not tested with v{<=4,6,10}).

    $ lsb_release -a
    ...
    Distributor ID: Ubuntu
    Description:    Ubuntu 16.04.6 LTS
    Release:        16.04
    Codename:       xenial

    $ gcc -v
    gcc version 11.0.0 20200902 (experimental) (GCC)

    $ gcc-9 -v
    gcc-9 (Ubuntu 9.3.0-10ubuntu2~16.04) 9.3.0

    $ gcc-8
    gcc-8 (Ubuntu 8.1.0-9ubuntu1~16.04.york1) 8.1.0

    $ gcc-7 -v
    gcc-7 (Ubuntu 7.3.0-23ubuntu2~16.04.york0) 7.3.0

    $ gcc-5 -v
    gcc-5 (Ubuntu 5.5.0-12ubuntu1~16.04) 5.5.0 20171010

It also occurs on macOS 10.15.6 with gcc-{7,8,9} (not tested with v{<=5,6,10,11}).
Comment 1 Jakub Jelinek 2020-09-16 13:44:12 UTC
Any reason why you want #line 0 ?  Files don't really have line number 0...
Comment 2 Martin Ogden 2020-09-16 14:30:15 UTC
I appreciate `#line 0 "main.c"` doesn't make much sense, but according to https://gcc.gnu.org/onlinedocs/cpp/Line-Control.html, 0 is a valid input.
Comment 3 Martin Liška 2020-09-21 08:38:43 UTC
Let me take it.
Comment 4 GCC Commits 2020-09-23 08:05:13 UTC
The master branch has been updated by Martin Liska <marxin@gcc.gnu.org>:

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

commit r11-3384-g6b4e8bf88f1172ce8561f57b12fb81063b21a78f
Author: Martin Liska <mliska@suse.cz>
Date:   Mon Sep 21 16:26:10 2020 +0200

    gcov: fix streaming corruption
    
    gcc/ChangeLog:
    
            PR gcov-profile/97069
            * profile.c (branch_prob): Line number must be at least 1.
    
    gcc/testsuite/ChangeLog:
    
            PR gcov-profile/97069
            * g++.dg/gcov/pr97069.C: New test.
Comment 5 Martin Liška 2020-09-23 08:05:54 UTC
Fixed on master so far.
Comment 6 GCC Commits 2020-10-01 19:19:50 UTC
The releases/gcc-10 branch has been updated by Martin Liska <marxin@gcc.gnu.org>:

https://gcc.gnu.org/g:bc3914f307211c42f31af7e64d2e6cc8a69837cb

commit r10-8838-gbc3914f307211c42f31af7e64d2e6cc8a69837cb
Author: Martin Liska <mliska@suse.cz>
Date:   Mon Sep 21 16:26:10 2020 +0200

    gcov: fix streaming corruption
    
    gcc/ChangeLog:
    
            PR gcov-profile/97069
            * profile.c (branch_prob): Line number must be at least 1.
    
    gcc/testsuite/ChangeLog:
    
            PR gcov-profile/97069
            * g++.dg/gcov/pr97069.C: New test.
    
    (cherry picked from commit 6b4e8bf88f1172ce8561f57b12fb81063b21a78f)
Comment 7 GCC Commits 2020-10-02 10:06:08 UTC
The releases/gcc-9 branch has been updated by Martin Liska <marxin@gcc.gnu.org>:

https://gcc.gnu.org/g:20f699a44492f2f43351d19849873d1112ffc7e0

commit r9-8967-g20f699a44492f2f43351d19849873d1112ffc7e0
Author: Martin Liska <mliska@suse.cz>
Date:   Mon Sep 21 16:26:10 2020 +0200

    gcov: fix streaming corruption
    
    gcc/ChangeLog:
    
            PR gcov-profile/97069
            * profile.c (branch_prob): Line number must be at least 1.
    
    gcc/testsuite/ChangeLog:
    
            PR gcov-profile/97069
            * g++.dg/gcov/pr97069.C: New test.
    
    (cherry picked from commit 6b4e8bf88f1172ce8561f57b12fb81063b21a78f)
Comment 8 Martin Liška 2020-10-02 10:07:48 UTC
Resolved.