C++ g++;5-valgrind或g+中的bug+;5.

C++ g++;5-valgrind或g+中的bug+;5.,c++,g++,valgrind,compiler-optimization,compiler-bug,C++,G++,Valgrind,Compiler Optimization,Compiler Bug,g++和valgrind的确切版本: g++-5 (Ubuntu 5.2.1-23ubuntu1~12.04) 5.2.1 20151031 valgrind-3.7.0 我没有深入研究到底哪个标志会这样做(finline small functions/findirect inlining/finline functions/finline functions called one/fearly inlining),因为我正在travis上远程测试这个功能,我已经等得很烦了,所以我只使用了-

g++和valgrind的确切版本:

g++-5 (Ubuntu 5.2.1-23ubuntu1~12.04) 5.2.1 20151031
valgrind-3.7.0
我没有深入研究到底哪个标志会这样做(finline small functions/findirect inlining/finline functions/finline functions called one/fearly inlining),因为我正在travis上远程测试这个功能,我已经等得很烦了,所以我只使用了
-fno inline
(我的机器上没有运行的linux)

实际上,我不知道这是由于内联造成的,我希望valgrind报告导致错误的实际函数,因此禁用了内联。。。瞧

请注意,这只发生在g++5上——我已经测试了g++4.4/4.5/4.7/4.8/4.9(4.6未测试)和clang++3.4/3.5/3.6/3.7/3.8(以及OSX下的所有这些编译器)

以下是错误:

==3063== 1 errors in context 1 of 1:
==3063== Invalid read of size 4
==3063==    at 0x40092E: regTest(char const*, char const*) (a.cpp:17)
这是我的代码:

// required includes
#include <cstdio>  // printf and friends
#include <cstdlib> // malloc, free, qsort
#include <cstring> // strlen, strcpy, strtok
#include <new>     // placement new

struct String
{
    char* m_str;

    void copy(const String& other) {
        if(m_str)
            free(m_str);
        m_str = 0;

        if(other.m_str) {
            m_str = static_cast<char*>(malloc(strlen(other.m_str) + 1));
            strcpy(m_str, other.m_str);
        }
    }

    String(const char* in = 0)
            : m_str(0) {
        if(in == 0)
            return;

        m_str = static_cast<char*>(malloc(strlen(in) + 1));
        strcpy(m_str, in);
    }

    String(const String& other)
            : m_str(0) {
        copy(other);
    }

    ~String() {
        if(m_str)
            free(m_str);
    }

    String& operator=(const String& other) {
        if(this != &other)
            copy(other);
        return *this;
    }
};

template <class T>
class Vector
{
    unsigned m_size;
    unsigned m_capacity;
    T*       m_buffer;

public:
    Vector()
            : m_size(0)
            , m_capacity(0)
            , m_buffer(0) {}

    Vector(const Vector& other)
            : m_size(other.m_size)
            , m_capacity(other.m_capacity)
            , m_buffer(static_cast<T*>(malloc(sizeof(T) * m_capacity))) {
        for(unsigned i = 0; i < m_size; ++i)
            new(m_buffer + i) T(other.m_buffer[i]);
    }

    ~Vector() {
        for(unsigned i = 0; i < m_size; ++i)
            (*(m_buffer + i)).~T();
        free(m_buffer);
    }

    Vector& operator=(const Vector& other) {
        if(this != &other) {
            for(size_t i = 0; i < m_size; ++i)
                (*(m_buffer + i)).~T();
            free(m_buffer);

            m_size     = other.m_size;
            m_capacity = other.m_capacity;

            m_buffer = static_cast<T*>(malloc(sizeof(T) * m_capacity));
            for(unsigned i = 0; i < m_size; ++i)
                new(m_buffer + i) T(other.m_buffer[i]);
        }
        return *this;
    }

    unsigned size() const { return m_size; }

    void push_back(const T& item) {
        if(m_size < m_capacity) {
            new(m_buffer + m_size++) T(item);
        } else {
            if(m_capacity == 0)
                m_capacity = 5; // initial capacity
            else
                m_capacity *= 2; // capacity growth factor
            T* temp = static_cast<T*>(malloc(sizeof(T) * m_capacity));
            for(unsigned i = 0; i < m_size; ++i) {
                new(temp + i) T(m_buffer[i]);
                (*(m_buffer + i)).~T();
            }
            new(temp + m_size++) T(item);
            free(m_buffer);
            m_buffer = temp;
        }
    }
};

struct FunctionData
{
    String m_suite;
    String m_name;

    const char* m_file;

    FunctionData(const char* suite, const char* name, const char* file)
            : m_suite(suite)
            , m_name(name)
            , m_file(file) {}

    FunctionData(const FunctionData& other)
            : m_suite(other.m_suite)
            , m_name(other.m_name)
            , m_file(other.m_file) {}
};

const char*& getCurrentTestSuite() {
    static const char* data = 0;
    return data;
}

int setTestSuiteName(const char* name) {
    getCurrentTestSuite() = name;
    return 0;
}

int regTest(const char* file, const char* name) {
    Vector<FunctionData> temp;

    temp.push_back(FunctionData(getCurrentTestSuite(), name, file));

    // main() is empty and we dont want this optimized away
    printf("hello! %d\n", temp.size());

    return 0;
}

__attribute__((unused)) static int a1 = setTestSuiteName("current testsuite");
__attribute__((unused)) static int a2 = regTest("a.cpp", "zzz");

int main(int, char**) { return 0; }
第二次运行导致valgrind错误

删除FunctionData的任何成员都会停止再现问题。 从图片中删除矢量也不会导致错误

是存储库,是travis日志

我已经浪费了超过几个小时来最小化这一点,所以我已经完成了缩小复制代码

那么谁错了——g++5还是valgrind?还是我?接下来我该怎么办?为什么会发生这种情况

编辑

哈哈!刚刚注意到
(a.cpp:17)
出现了错误,因此问题行是
m_str=static_cast(malloc(strlen(other.m_str)+1))-但为什么?!?!?!即使所有内容都内联在
regTest()
中,我也不认为这个简单的代码中存在真正的错误

编辑2

刚刚在本地用
g++(Ubuntu 5.3.0-3ubuntu1~14.04)5.3.0 20151204
valgrind-3.10.1
试用了Ubuntu 14.04,情况也是一样-使用内联编译时会出现错误

也在本地试用了
g++-4.8(Ubuntu 4.8.5-2ubuntu1~14.04.1)4.8.5
和OMG!还有像g++-5这样的小车!也许g++4.8.5中有一个补丁,而travis中使用的4.8.x和4.9.x中没有

编辑3

字符串
类的构造函数(any-normal和copy-两者都可用)添加
\uu属性(noinline))
解决了这个问题。这是一个bug吗?下一步怎么办

编辑4

我玩了多一点,将代码改为(删除了Vector类),并设法在编译时从valgrind触发一个错误

g++ a.cpp -O3 -fno-elide-constructors 
在使用

g++ a.cpp -O3
(两种情况下都启用内联)

这些优化有问题。抱歉这么多的编辑和长篇大论-我现在就闭嘴

编辑5:

一位朋友告诉我在编译时添加
-ggdb
,现在valgrind对原始代码的错误是:

==2150== Invalid read of size 4
==2150==    at 0x40095E: copy (a.cpp:17)
==2150==    by 0x40095E: String (a.cpp:33)
==2150==    by 0x40095E: FunctionData (a.cpp:128)
==2150==    by 0x40095E: push_back (a.cpp:106)
==2150==    by 0x40095E: regTest(char const*, char const*) (a.cpp:144)
==2150==    by 0x400B2C: __libc_csu_init (in /home/onqtam/a.out)
==2150==    by 0x537CE54: (below main) (libc-start.c:246)
==2150==  Address 0x5a37c90 is 16 bytes inside a block of size 18 alloc'd
==2150==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2150==    by 0x4008DF: String (a.cpp:27)
==2150==    by 0x4008DF: FunctionData (a.cpp:123)
==2150==    by 0x4008DF: regTest(char const*, char const*) (a.cpp:144)
==2150==    by 0x400B2C: __libc_csu_init (in /home/onqtam/a.out)
==2150==    by 0x537CE54: (below main) (libc-start.c:246)

这是因为gcc优化strcpy以在4字节块上运行,这总是安全的,因为您不能分配不是4字节倍数的内存块(至少在x86和x64上)。因此,从gcc的角度来看,阅读是绝对安全的,但从valgrind的角度来看,您阅读的内容已经超过了您所说的要分配的内容的末尾。通常,valgrind可以检测到您正在执行memcpy/memmove/strcpy等操作,并知道如何抑制错误,但当调用内联时,其检测失败,您会收到错误消息


您可能希望在调用中包装strcpy,以提醒valgrind以下内存访问是安全的,即,查看是否确实要在启用主动内联的情况下进行调试。

输出指示第17行有错误,但您发布的内容的第17行与
regTest
无关。请确保您正在运行的代码与您发布的代码完全相同,每个字符对应一个字符,并且valgrind输出与发生的代码完全相同too@M.M但一切都在regTest中内联!这就是它报告错误的原因。regTest()调用一些字符串复制构造函数。。。只需查看travis中的存储库和相应的构建。当我禁用内联以查看错误发生的确切位置时,它就会消失!我有>>>四重奏Valgrind 3.7.0现在已经很老了。较新的版本有很多改进(a.o.他们理解函数内联)。所以,你可以试试valgrind 3.11.0。@phd刚刚在本地用
g++(Ubuntu 5.3.0-3ubuntu1~14.04)5.3.0 20151204和
valgrind-3.10.1
试用了Ubuntu 14.04,情况也是一样的@onqtam编译器错误消息总是反映它们所在的源代码行,而不管内联或什么是真正的strlen,这才是问题所在-而不是strcpy或malloc。
==2150== Invalid read of size 4
==2150==    at 0x40095E: copy (a.cpp:17)
==2150==    by 0x40095E: String (a.cpp:33)
==2150==    by 0x40095E: FunctionData (a.cpp:128)
==2150==    by 0x40095E: push_back (a.cpp:106)
==2150==    by 0x40095E: regTest(char const*, char const*) (a.cpp:144)
==2150==    by 0x400B2C: __libc_csu_init (in /home/onqtam/a.out)
==2150==    by 0x537CE54: (below main) (libc-start.c:246)
==2150==  Address 0x5a37c90 is 16 bytes inside a block of size 18 alloc'd
==2150==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2150==    by 0x4008DF: String (a.cpp:27)
==2150==    by 0x4008DF: FunctionData (a.cpp:123)
==2150==    by 0x4008DF: regTest(char const*, char const*) (a.cpp:144)
==2150==    by 0x400B2C: __libc_csu_init (in /home/onqtam/a.out)
==2150==    by 0x537CE54: (below main) (libc-start.c:246)