首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >分段故障,但不在val差内或gdb中

分段故障,但不在val差内或gdb中
EN

Stack Overflow用户
提问于 2017-04-08 16:51:30
回答 1查看 3K关注 0票数 3

在我的项目中,有一个库,它具有使用Autodesk的fbx 2017.1加载FBX的代码。

在调试和发布中加载fbx崩溃。坠机以两种不同的方式发生,似乎是随机的:

  • 崩溃不是简单的“分割故障”(大多数情况下)
  • 崩溃是可能涉及到崩溃的所有库的转储,以及与realloc()调用有关的问题的暗示。(每隔一段时间)从消息的上下文中,我无法判断出可能是哪个realloc (消息后面是链接的所有库的转储)。

代码确实包含realloc()调用,特别是在FbxStream的自定义实现中使用的缓冲区分配中。

大多数代码路径对于windows是完全相同的,只有一些特定于平台的部分已经被重新实现。在windows上,它按预期运行。

令我印象深刻的是,如果我在gdb或val差内运行程序,崩溃就会消失!因此,我开始寻找未初始化的成员/值,但到目前为止,我没有发现任何可疑的东西。我使用了CppDepend/CppCheck和VS2012代码分析,但在未初始化的变量/成员上都是空的。

给出一些关于fbx加载的背景;FBX有许多处理不同类型资源的方法(obj、3ds、FBX、..)。它们可以从文件或流加载。要支持大文件,流选项是更相关的选项。下面的代码远非十全十美,但我目前最感兴趣的是val差业/gdb不会崩溃的原因。我将SDK文档放在ReadString之上,因为它是最复杂的文档。

代码语言:javascript
运行
复制
class MyFbxStream : public FbxStream{
    uint32 m_FormatID;
    uint32 m_Error;
    EState m_State;
    size_t m_Pos;
    size_t m_Size;
    const Engine::Buffer* const m_Buffer;
    MyFbxStream& operator = (const MyFbxStream& other) const;
public:
    MyFbxStream(const Engine::Buffer* const buffer) 
    : m_FormatID(0)
    , m_Error(0)
    , m_State(eClosed)
    , m_Pos(0)
    , m_Size(0)
    , m_Buffer(buffer) {};
    virtual ~MyFbxStream() {};
    virtual bool Open(void* pStreamData) {
        m_FormatID = *(uint32*)pStreamData;
        m_Pos = 0;
        m_State = eOpen;
        m_Size = m_Buffer->GetSize();
        return true;
    }
    virtual bool Close() {
        m_Pos = m_Size = 0;
        m_State = eClosed;
        return true;
    }
    virtual int Read(void* pData, int pSize) const  {
        const unsigned char* data = (m_Buffer->GetBase(m_Pos));
        const size_t bytesRead = m_Pos + pSize > m_Buffer->GetSize() ? (m_Buffer->GetSize() - m_Pos) : pSize;
        const_cast<MyFbxStream*>(this)->m_Pos += bytesRead;
        memcpy(pData, data, bytesRead);
        return (int)bytesRead;
    }
    /** Read a string from the stream.
    * The default implementation is written in terms of Read() but does not cope with DOS line endings.
    * Subclasses may need to override this if DOS line endings are to be supported.
    * \param pBuffer Pointer to the memory block where the read bytes are stored.
    * \param pMaxSize Maximum number of bytes to be read from the stream.
    * \param pStopAtFirstWhiteSpace Stop reading when any whitespace is encountered. Otherwise read to end of line (like fgets()).
    * \return pBuffer, if successful, else NULL.
    * \remark The default implementation terminates the \e pBuffer with a null character and assumes there is enough room for it.
    * For example, a call with \e pMaxSize = 1 will fill \e pBuffer with the null character only. */
    virtual char* ReadString(char* pBuffer, int pMaxSize, bool pStopAtFirstWhiteSpace = false) {
        assert(!pStopAtFirstWhiteSpace); // "Not supported"
        const size_t pSize = pMaxSize - 1;
        if (pSize) {
            const char* const base = (const char* const)m_Buffer->GetBase();
            char* cBuffer = pBuffer;
            const size_t totalSize = std::min(m_Buffer->GetSize(), (m_Pos + pSize));
            const char* const maxSize = base + totalSize;
            const char* sum = base + m_Pos;
            bool done = false;
            // first align the copy on alignment boundary (4byte)
            while ((((size_t)sum & 0x3) != 0) && (sum < maxSize)) {
                const unsigned char c = *sum++;
                *cBuffer++ = c;
                if ((c == '\n') || (c == '\r')) {
                    done = true;
                    break;
            }   }
            // copy from alignment boundary to boundary (4byte)
            if (!done) {
                int64 newBytesRead = 0;
                uint32* dBuffer = (uint32*)cBuffer;
                const uint32* dBase = (uint32*)sum;
                const uint32* const dmaxSize = ((uint32*)maxSize) - 1;
                while (dBase < dmaxSize) {
                    const uint32 data = *(const uint32*const)dBase++;
                    *dBuffer++ = data;
                    if (((data & 0xff) == 0x0a) || ((data & 0xff) == 0x0d)) { // third bytes, 4 bytes read..
                        newBytesRead -= 3;
                        done = true;
                        break;
                    } else {
                        const uint32 shiftedData8 = data & 0xff00;
                        if ((shiftedData8 == 0x0a00) || (shiftedData8 == 0x0d00)) { // third bytes, 3 bytes read..
                            newBytesRead -= 2;
                            done = true;
                            break;
                        } else {
                            const uint32 shiftedData16 = data & 0xff0000;
                            if ((shiftedData16 == 0x0a0000) || (shiftedData16 == 0x0d0000)) { // second byte, 2 bytes read..
                                newBytesRead -= 1;
                                done = true;
                                break;
                            } else {
                                const uint32 shiftedData24 = data & 0xff000000;
                                if ((shiftedData24 == 0x0a000000) || (shiftedData24 == 0x0d000000)) { // first byte, 1 bytes read..
                                    done = true;
                                    break;
                }   }   }   }   }
                newBytesRead += (int64)dBuffer - (int64)cBuffer;
                if (newBytesRead) {
                    sum += newBytesRead;
                    cBuffer += newBytesRead;
            }   }
            // copy anything beyond the last alignment boundary (4byte)
            if (!done) {
                while (sum < maxSize) {                 
                    const unsigned char c = *sum++;
                    *cBuffer++ = c;
                    if ((c == '\n') || (c == '\r')) {
                        done = true;
                        break;
            }   }   }
            const size_t bytesRead = cBuffer - pBuffer;
            if (bytesRead) {
                const_cast<MyFbxStream*>(this)->m_Pos += bytesRead;
                pBuffer[bytesRead] = 0;
                return pBuffer;
        }   }       
        pBuffer = NULL;
        return NULL;
    }
    virtual void Seek(const FbxInt64& pOffset, const FbxFile::ESeekPos& pSeekPos) {
        switch (pSeekPos) {
            case FbxFile::ESeekPos::eBegin:     m_Pos = pOffset; break;
            case FbxFile::ESeekPos::eCurrent:   m_Pos += pOffset; break;
            case FbxFile::ESeekPos::eEnd:       m_Pos = m_Size - pOffset; break;
        }
    }
    virtual long GetPosition() const        {   return (long)m_Pos; }
    virtual void SetPosition(long position) {   m_Pos = position;   }
    virtual void ClearError()               {   m_Error = 0;    }
    virtual int GetError() const            {   return m_Error; }
    virtual EState GetState()               {   return m_State; }
    virtual int GetReaderID() const         {   return m_FormatID;  }
    virtual int GetWriterID() const         {   return -1;  }                       // readonly stream
    virtual bool Flush()                    {   return true;    }                   // readonly stream
    virtual int Write(const void* /*d*/, int /*s*/) {   assert(false);  return 0; } // readonly stream
};

我假设可能有一些与malloc/free/realloc操作相关的未定义行为,但在gdb中却不存在。但是如果是这样的话,我也希望Windows二进制文件会出现问题。

另外,我不知道这是否相关,但是当我跟踪打开()函数并打印"m_Buffer“指针的值(或" this ")时,我会得到一个以0 0xfffffff开头的指针值。对于Windows程序员来说,这似乎是个问题。但是,我能否在linux中得出同样的结论,因为我也在静态函数调用中看到了这种情况。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-08 17:07:19

如果我在gdb或val差内运行程序,崩溃就会消失!

有两种可能的解释:

  1. 有多个线程,代码显示一个数据竞争,GDB和Val差伦都会显著影响执行时间。
  2. GDB禁用地址随机化;Valgrind对程序布局有显著影响,崩溃对精确布局非常敏感。

我将采取的步骤:

  1. 设置ulimit -c unlimited,运行程序并将其转储到core,然后在GDB中使用死后分析。
  2. 在GDB下运行程序,使用set disable-randomization off,看看是否可以通过这种方式到达崩溃点。
  3. 运行程序与黑尔兰DRD,瓦兰的线程错误检测器。
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43297814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档