`pragma pack(push, 1)` 在 GCC 4.4.7 中崩溃。可能的编译器错误?

Sco*_*79' 5 c++ gcc pragma gcc4

我遇到了一个让我困惑的错误。我已将其范围缩小到 GCC(特别是 RHEL Linux、GCC v.4.4.7)中命令的问题pragma pack,该问题可以在下面所示的小示例案例中重新创建。在这种情况下,GCC 似乎正在计算错误的偏移量,这将表现为循环内的崩溃。删除 pragma pack 也可以消除故障 - 但在实际应用程序中,这将导致许多额外的 GB 内存使用,这是不可取的。

在下面的示例中,您需要在启用优化 (O3) 的情况下进行编译才能遇到失败。我还在结构中提供了一个可以删除的示例项目(cMagic),这将改变结构对齐并防止错误触发。

我查看了生成的程序集,认为这可能是编译器错误。我还缺少其他东西吗?任何人都可以确认这个错误或提供任何见解吗?

崩溃.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;
}
Run Code Online (Sandbox Code Playgroud)

生成文件:

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
Run Code Online (Sandbox Code Playgroud)

反汇编图(生成的ASM代码):

拆解图

小智 4

看看使用__attribute__ ((packed));而不是#pragma pack(1). IIRC,这个版本的 GCC 对待它的方式有点不同。