[英]`pragma pack(push, 1)` crashes in GCC 4.4.7. Possible compiler bug?
我遇到了一個讓我難過的錯誤。 我已經將它縮小到GCC中的pragma pack
命令(特別是RHEL Linux,GCC v.4.4.7)的一個問題,可以在我下面顯示的小樣例中重新創建。 在這種情況下,看起來GCC正在計算錯誤的偏移量,這將在循環中表現為崩潰。 刪除pragma包也可以消除故障 - 但在實際應用程序中,這將導致許多額外的GB內存使用,這是不可取的。
在下面的示例中,您需要在啟用優化(O3)的情況下進行編譯以體驗失敗。 我還在結構中提供了一個可以刪除的示例項(cMagic),它將改變結構對齊並防止錯誤觸發。
我已經看了一下生成的程序集,並認為這可能是編譯器錯誤。 我錯過了別的什么嗎? 任何人都可以確認此錯誤或提供任何見解嗎?
Crash.cpp:
/* Platform Version Info:
* gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)
* uname: 2.6.32-504.16.2.el6.x86_64 #1 SMP Tue Mar 10 17:01:00 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
*
* Compiling:
* Must use -O3 for compiling and linking
* CXX= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
* CPP= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
*
* Notes:
* This appears to be an optimization and alignment issue.
* Getting rid of a byte in Place (cMagic) causes the program to complete successfully.
*
*/
#include <stdlib.h>
#include <iostream>
using namespace std;
#pragma pack(push,1) // Structures must be packed tightly
#define MAGICCONSTANT 17
struct Place {
int iFoo;
char cMagic; // GCC doesn't like cMagic. Disillusion it and everything is OK
int aiArray[MAGICCONSTANT];
};
#pragma pack(pop)
int main(int argc, const char *argv[])
{
Place *pPlace = new Place; // Place must be on the heap... so new, calloc, malloc, etc
for (int c = 0; (c < MAGICCONSTANT); c++) {
pPlace->aiArray[c] = 0;
}
delete pPlace;
cout << "Complete!" << endl;
return 0;
}
Makefile文件:
CXX= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
CPP= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
OBJS= Crash.o
SRCS= Crash.cpp
TARG= crash
debug:: ${TARG}
all:: ${TARG}
${TARG}: ${OBJS}
${CPP} -o ${TARG} ${OBJS} ${LDFLAGS} ${LIBS}
clean::
rm -f ${TARG} ${OBJS} ${TARG}.core core
反匯編圖(生成的ASM代碼):
看看使用__attribute__ ((packed));
而不是#pragma pack(1)
。 IIRC,這個版本的海灣合作委員會對待它有點不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.