【发布时间】:2016-02-27 14:48:23
【问题描述】:
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-once/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; }
这就是我运行它的方式:
g++-5 a.cpp -Wall -Wextra -pedantic -std=c++98 -g -O3 -fno-inline
valgrind --leak-check=full --track-origins=yes -v ./a.out
g++-5 a.cpp -Wall -Wextra -pedantic -std=c++98 -g -O3
valgrind --leak-check=full --track-origins=yes -v ./a.out
第二次运行导致valgrind错误。
删除 FunctionData 的任何成员都会停止重现问题。 将Vector从图片中剪掉也不会出错。
我已经浪费了几个多小时来最小化这个问题,所以我已经完成了对复制代码的最小化。
那么谁错了——g++5 还是 valgrind?或者我?接下来我该怎么办?为什么会发生这种情况?
编辑:
哈哈!刚刚注意到错误中的(a.cpp:17),所以有问题的行是m_str = static_cast<char*>(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:
将 __attribute__((noinline)) 添加到 String 类的构造函数(任何 - 正常和复制 - 两者都适用)解决了这个问题。那么这是一个错误吗?下一步该做什么?
编辑 4:
我玩了一点,将代码更改为this(删除了 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)
【问题讨论】:
-
输出表明第17行有错误,但您发布的第17行与
regTest无关。请确保您正在运行的代码与您发布的代码完全相同,一个字符一个字符,并且 valgrind 输出也与它发生时完全相同 -
@M.M 但一切都内联在 regTest 中!这就是它在那里报告错误的原因。 regTest() 调用一些 String 复制构造函数...只需查看存储库和 travis 中的相应构建。当我禁用内联以查看错误发生的确切位置时 - 它消失了!我有 >>> quatriple
-
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 编译器错误消息总是反映它们所在的源代码行,无论是内联还是什么
标签: c++ g++ valgrind compiler-optimization compiler-bug