miscompilation: std::vector gets corrupted on i686 mingw
От: niXman Ниоткуда https://github.com/niXman
Дата: 12.08.20 20:42
Оценка: 6 (2)
привет!

ссылка на оригинал: https://sourceforge.net/p/mingw-w64/bugs/849/

суть вопроса:
We have found a very weird miscompilation issue. The code looks like this:

static void foo()
{
  std::vector<std::uint8_t> const serializedDevice = {
      0x01, 0x00, 0x74, 0x72, 0x75, 0x73, 0x74, 0x63, 0x68, 0x61, 0x69,
      0x6e, 0x20, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x02, 0xc0, 0x01, 0x65, 0x70, 0x68, 0x20, 0x70, 0x75, 0x62,
      0x20, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

  auto const trustchainId = make("a");
  auto const author = make("ba");

  for (int i = 0; i < 20; ++i)
    printf("%x ", serializedDevice[i]);
}

The make function returns a 32-byte std::array containing the given string and padded with 0s.

The issue is that the printf loop will print 0s for bytes 6 to 9 in the vector, even though they were initialized with different values.

Changing the tiniest thing in the example makes it behave correctly. For example, making one of the make function non-static, removing one of the local variables trustchainId or author, or even duplicating the for loop before the instanciation of those variables fixes the issue.

Since this example makes use of a lot of the libstdc++ code, we have tried to creduce the issue to a minimal example. I have reworked it a bit, you can see that it contains a very simple vector implementation that always allocates 100 bytes and the constructor copies the given buffer into the vector by calling __builtin_memmove. This program is also fixed by making the tiniest of changes, like removing the unused argument to allocate, removing or not initializing the null1 and null2 variables (that are never read anyway), or removing that if (b) __throw_bad_alloc();.

We couldn't manage to reproduce the issue on any other platform than windows, with 32 bits, and optimizations enabled. Note that the bug is reproducible using wine, this helped us run creduce on linux to get the minimal example.

bug.cpp is the initial code
minimal.cpp is the creduce-d code
Compile them with i686-w64-mingw32-g++.exe -g -O3
We ran the tests on i686-w64-mingw32-g++.exe (i686-posix-dwarf, Built by MinGW-W64 project) 9.3.0.

https://sourceforge.net/p/mingw-w64/bugs/849/attachment/bug.cpp
https://sourceforge.net/p/mingw-w64/bugs/849/attachment/minimal.cpp
пачка бумаги А4 стОит 2000 р, в ней 500 листов. получается, лист обычной бумаги стОит дороже имперского рубля =)
Re: miscompilation: std::vector gets corrupted on i686 mingw
От: Mystic Artifact  
Дата: 12.08.20 22:39
Оценка: -5
Здравствуйте, niXman, Вы писали:

Это было бы интересно если бы кто-то удосужился описать проблему на русском.

Кроме того mingw мягко говоря не интересен. Компилируй msvc или clang. Остальное (gcc) не заслуживает внимания. Научитесь наконец писать кросс-платформенный код, а не херню изобретать, которая мимикриует под него.

PS: Я думаю что посыл ясен и из первого параграфа, дальше личные язвы — но ты сам напросился.
Отредактировано 12.08.2020 23:18 Mystic Artifact . Предыдущая версия .
Re[2]: miscompilation: std::vector gets corrupted on i686 mingw
От: niXman Ниоткуда https://github.com/niXman
Дата: 13.08.20 10:06
Оценка:
Здравствуйте, Mystic Artifact, Вы писали:

MA>Кроме того mingw мягко говоря не интересен.


зачем же отписываешься в теме?

MA>Компилируй msvc или clang.


спасибо, но нет.

MA>Остальное (gcc) не заслуживает внимания.


мягко говоря, это ложь. либо, всего лишь, твое мнение...

MA>Научитесь наконец писать кросс-платформенный код, а не херню изобретать, которая мимикриует под него.


о чем речь?
пачка бумаги А4 стОит 2000 р, в ней 500 листов. получается, лист обычной бумаги стОит дороже имперского рубля =)
Re[3]: miscompilation: std::vector gets corrupted on i686 mingw
От: Mystic Artifact  
Дата: 25.08.20 17:17
Оценка:
Здравствуйте, niXman, Вы писали:

MA>>Остальное (gcc) не заслуживает внимания.

X>мягко говоря, это ложь. либо, всего лишь, твое мнение...
Я и не претендовал на вселенскую истинную.

MA>>Научитесь наконец писать кросс-платформенный код, а не херню изобретать, которая мимикриует под него.

X>о чем речь?
Я перечитал вопрос, и понял что я идиот и понял его неверно.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.