最近在将我们的应用程序从gcc-5.3移植到8.2时,我们发现了一个破坏我们应用程序的奇怪行为。
简而言之,似乎gcc-8.2删除了我们的“if分支,它比较了2个无符号整数”,甚至没有产生警告。
我们尝试使用相同的编译选项g ++ 5.3,g ++ 7.4和g ++ 8.2,只有g ++ 8.2有这个问题。将在下面显示一个简短的例子。
#include <iostream>
#include <cstdint>
#include <cstdlib>
#include <cstring>
using namespace std;
struct myunion {
myunion(uint32_t x) {
_data.u32 = x;
}
uint16_t hi() const { return _data.u16[1]; }
uint16_t lo() const { return _data.u16[0]; }
union {
uint16_t u16[2];
uint32_t u32;
} _data;
};
__attribute__((noinline)) void printx1x2(uint32_t x1, uint32_t x2) {
cout << "x1: " << x1 << endl;
cout << "x2: " << x2 << endl;
}
__attribute__((noinline)) int func(uint32_t a, uint32_t b) {
const uint32_t x1 = myunion(a).hi() * myunion(b).lo();
const uint32_t x2 = x1 + myunion(a).lo() * myunion(b).hi();
printx1x2(x1, x2);
int ret = 0;
if ( x2 < x1 ) {
ret = 0x10000;
}
return ret;
}
int main(int argc, char** argv) {
cout << func(4294967295, 4294917296) << endl;
return 0;
}
上面的代码编译如下:
$ g++-7 --version
g++-7 (GCC) 7.4.1 20181207
Copyright (C) 2017 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.
$ g++-7 -Wall -std=c++14 -O3 a.cxx -o 7.out
$ ./7.out
x1: 1018151760
x2: 1018020689
65536
$ g++ --version
g++ (GCC) 8.2.1 20181127
Copyright (C) 2018 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.
$ g++ -Wall -std=c++14 -O3 a.cxx -o 8.out
$ ./8.out
x1: 1018151760
x2: 1018020689
0
我期待7.out
的输出是正确的。
这实际上是UB(未定义的行为)还是g ++错误?
UPDATE
看起来删除联合访问UB仍然处理不需要的结果:
#include <iostream>
#include <cstdint>
#include <cstdlib>
#include <cstring>
using namespace std;
struct myunion2 {
myunion2(uint32_t x) {
_data = x;
}
uint16_t hi() const { return (uint16_t)((_data & 0xFFFF0000) >> 16); }
uint16_t lo() const { return (uint16_t)((_data & 0xFFFF)); }
uint32_t _data;
};
__attribute__((noinline)) void printx1x2(uint32_t x1, uint32_t x2) {
cout << "x1: " << x1 << endl;
cout << "x2: " << x2 << endl;
}
__attribute__((noinline)) int func(uint32_t a, uint32_t b) {
const uint32_t x1 = myunion2(a).hi() * myunion2(b).lo();
const uint32_t x2 = x1 + myunion2(a).lo() * myunion2(b).hi();
printx1x2(x1, x2);
int ret = 0;
if ( x2 < x1 ) {
ret = 0x10000;
}
return ret;
}
int main(int argc, char** argv) {
cout << func(4294967295, 4294917296) << endl;
return 0;
}
输出:
$ g++-7 -Wall -std=c++14 -O3 a.cxx -o 7.out
[2019-03-27 22:48:30][wliu@wliu-arch-vm1 ~/tests]
$ ./7.out
x1: 1018151760
x2: 1018020689
65536
[2019-03-27 22:48:32][wliu@wliu-arch-vm1 ~/tests]
$ g++ -Wall -std=c++14 -O3 a.cxx -o 8.out
[2019-03-27 22:49:11][wliu@wliu-arch-vm1 ~/tests]
$ ./8.out
x1: 1018151760
x2: 1018020689
0
问题(除了原始示例中的union-punning)是这个表达式:
myunion2(a).lo() * myunion2(b).hi();
操作数的值为65535 * 65535.操作数的类型为uint16_t
。
不对小于int
的类型执行算术运算。较小的类型首先被提升。由于uint16_t
小于int
,并且由uint16_t
表示的值的范围可以用int
表示,那些操作数被提升为int
。但操作65535 * 65535溢出int
,这是一个签名类型。签名溢出具有未定义的行为。
解决方案:在乘法之前转换为更大的无符号(或者首先返回更大的无符号):
const uint32_t x1 = (unsigned)myunion2(a).hi() * myunion2(b).lo();
const uint32_t x2 = x1 + (unsigned)myunion2(a).lo() * myunion2(b).hi();