使用 g++5 进行的内联构建时出现 valgrind 错误 - valgrind 或 g++5 中的错误?
valgrind errors on build with inlining made by g++5 - bug in valgrind or g++5?
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 切出图片也不会出错。
here is the repository and here是travis日志。
我已经浪费了几个小时来最小化它,所以我完成了对复制代码的最小化。
那么谁错了 - g++5 还是 valgrind?或者我?接下来我该做什么?为什么会这样?
编辑:
哈哈!刚刚注意到 (a.cpp:17)
错误,所以有问题的行是 m_str = static_cast<char*>(malloc(strlen(other.m_str) + 1));
- 但为什么?!?!?!即使所有内容都内联 regTest()
- 我不认为这个简单的代码有真正的错误
编辑 2:
刚刚尝试使用 Ubuntu 14.04 在本地使用 g++ (Ubuntu 5.3.0-3ubuntu1~14.04) 5.3.0 20151204
和 valgrind-3.10.1
并且情况相同 - 当使用内联编译时出现错误。
也在本地尝试使用 g++-4.8 (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
和 OMG!也像 g++-5 一样有问题!也许一个补丁进入了 g++ 4.8.5 而不是在 4.8.x 和 4.9.x 中使用的 travis
编辑 3:
将 __attribute__((noinline))
添加到 String
class 的构造函数(任何 - 普通和复制 - 都适用)解决了这个问题。那么这是一个错误吗?接下来做什么?
编辑 4:
我又玩了一会儿,将代码更改为 this(删除了 Vector class),并在使用
进行编译时设法触发了 valgrind 的错误
g++ a.cpp -O3 -fno-elide-constructors
而且编译时没有错误
g++ a.cpp -O3
(两种情况都启用了内联)
这些优化有些问题。抱歉,编辑太多,篇幅太长 post - 我现在闭嘴。
编辑 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/etc。并且知道抑制错误,但是当调用被内联时,它的检测失败并且您收到错误的错误消息。
您可能希望将 strcpy 包装在一个调用中,以提醒 valgrind 以下内存访问是安全的事实,即,如果您真的想在启用主动内联的情况下进行调试,请参阅 http://valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs。
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 切出图片也不会出错。
here is the repository and here是travis日志。
我已经浪费了几个小时来最小化它,所以我完成了对复制代码的最小化。
那么谁错了 - g++5 还是 valgrind?或者我?接下来我该做什么?为什么会这样?
编辑:
哈哈!刚刚注意到 (a.cpp:17)
错误,所以有问题的行是 m_str = static_cast<char*>(malloc(strlen(other.m_str) + 1));
- 但为什么?!?!?!即使所有内容都内联 regTest()
- 我不认为这个简单的代码有真正的错误
编辑 2:
刚刚尝试使用 Ubuntu 14.04 在本地使用 g++ (Ubuntu 5.3.0-3ubuntu1~14.04) 5.3.0 20151204
和 valgrind-3.10.1
并且情况相同 - 当使用内联编译时出现错误。
也在本地尝试使用 g++-4.8 (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
和 OMG!也像 g++-5 一样有问题!也许一个补丁进入了 g++ 4.8.5 而不是在 4.8.x 和 4.9.x 中使用的 travis
编辑 3:
将 __attribute__((noinline))
添加到 String
class 的构造函数(任何 - 普通和复制 - 都适用)解决了这个问题。那么这是一个错误吗?接下来做什么?
编辑 4:
我又玩了一会儿,将代码更改为 this(删除了 Vector class),并在使用
进行编译时设法触发了 valgrind 的错误g++ a.cpp -O3 -fno-elide-constructors
而且编译时没有错误
g++ a.cpp -O3
(两种情况都启用了内联)
这些优化有些问题。抱歉,编辑太多,篇幅太长 post - 我现在闭嘴。
编辑 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/etc。并且知道抑制错误,但是当调用被内联时,它的检测失败并且您收到错误的错误消息。
您可能希望将 strcpy 包装在一个调用中,以提醒 valgrind 以下内存访问是安全的事实,即,如果您真的想在启用主动内联的情况下进行调试,请参阅 http://valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs。