https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77728
Bug ID: 77728
Summary: Miscompilation multiple vector iteration on ARM
Product: gcc
Version: 6.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: yyc1992 at gmail dot com
Target Milestone: ---
Code to reproduce is at
https://gist.github.com/yuyichao/a66edb9d05d18755fb7587b12e021a8a. The two cpp
files are
```c++
#include <stdint.h>
#include <vector>
typedef std::vector<std::pair<uint64_t, uint64_t>> DWARFAddressRangesVector;
void dumpRanges(const DWARFAddressRangesVector& Ranges) {
for (const auto &Range: Ranges) {
(void)Range;
}
}
void collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges)
{
const DWARFAddressRangesVector &DIERanges = DWARFAddressRangesVector();
Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
}
```
```c++
#include <stdint.h>
#include <vector>
typedef std::vector<std::pair<uint64_t, uint64_t>> DWARFAddressRangesVector;
void collectAddressRanges(DWARFAddressRangesVector &CURanges,
const DWARFAddressRangesVector &CUDIERanges)
{
CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
}
int main()
{
std::vector<std::pair<uint64_t, uint64_t>> CURanges;
std::vector<std::pair<uint64_t, uint64_t>> CUDIERanges{{1, 2}};
collectAddressRanges(CURanges, CUDIERanges);
return 0;
}
```
Both compiled with `g++ -O2` and linked together. When running the compiled
program, it raises an exception in the `insert`
```
terminate called after throwing an instance of 'std::length_error'
what(): vector::_M_range_insert
```
which shouldn't happen. The issue seems to be related to merging duplicated
code since it is important to put the code into two files and the present of
the second .o file is important even though none of the code in it is used. The
iterations also have to be all on the const reference of vector. Removing one
of the const also makes the issue go away.
The g++ is version 6.2.1 from the ArchLinuxARM armv7h repository. This might be
a regression in gcc 5 since other devs using gcc <=4.9 doesn't seem to have
this issue and I was able to reproduce this on archlinux on 4-5 different
systems with gcc >=5.
This causes https://github.com/JuliaLang/julia/issues/14550