我们使用VC6.0、Purify、PC-Lint开发了一个控制台应用程序,并在Windows上进行了量化.VC6将不能在Windows7和8上工作。如果我们要升级到Windows8,我已经考虑了开发环境的选择。我们的应用程序是标准的C++控制台应用程序。实际上,我们所有的用户都在Linux上。有没有人有过VC++ Pro 2013或2012年跨平台c++开发的经验?具体来说,它是否可以进行内存边界检查、内存泄漏检查和代码性能分析(每个函数需要多长时间)?
发布于 2014-01-18 15:51:53
2013年视觉c++可以做什么净化和量化做什么?
好吧,这不是“一个人能做另一个人做的每件事”。它更像是一个交叉口,并利用它们来获得最大的覆盖率。
Purify是一个运行时检查器,因此它通常会优于Visual内置的内存检查工具。但是Purify不进行静态分析,因此您需要使用Visual。这是一个大的合作伙伴关系。
我已经考虑过我们开发环境的选择..。我们的应用程序是跨平台的标准C++控制台应用程序.
自行车脱落开始..。这是一个很好的机会,因为您正在编写可移植代码。这么多人编写运行在一个平台上的代码,他们从其他平台上的其他工具中分离出诊断信息。
下面的所有内容都是免费的(下的企业代码分析除外),如果您能够干净利落地完成这个过程,那么您将拥有相当可靠的代码。
Windows
使用Visual (任何版本)并显示警告。他们包括/WAll
和/W4
。如果您有,请确保添加企业代码分析或添加/analyze
开关。
您可以在Visual中进行基本内存检查。我不知道最新的“纯净”是如何运作的。(我不使用它,因为我编写了跨平台代码,并使用Linux进行繁重的内存检查)。
对于Windows平台,您还应该做其他的事情。您可以在OWASP的C基刀具链淬火上找到关于开发工具链的讨论。
Linux
一定要支持GCC、Clang和ICC。在使用它们时,请确保启动警告,包括-Wall
、-Wextra
和-Wconversion
。GCC是中流砥柱,您的代码可能运行良好。ICC是Intel的编译器,它无情地删除未定义的行为。如果您的代码在ICC下中断,这可能是因为编译器/优化器删除了一些未定义的行为(请参阅Clang下面关于如何定位违规代码的未定义清除程序)。
Clang3.3真的用它的杀菌剂发光(Clang3.2及以下没有)。确保使用-fsanitize=address
和-fsanitize=undefined
运行。清除器添加运行时检查程序,并在执行过程中查找违规行为。自我测试越多越好。一个完整的杀菌剂清单是可用的Clang控制码生成。
Clang 3.3食谱如下。它们包括如何获取Clang,如何构建Clang,以及如何使用santizer执行测试。
在您完成与GCC,Clang和ICC的编译之后,运行该程序。瓦兰是另一个动态内存检查器。
对于Linux平台,您还应该做其他的事情。您可以在OWASP的C基刀具链淬火上找到关于开发工具链的讨论。
下载和构建Clang 3.3的最新内容:
wget http://llvm.org/releases/3.3/llvm-3.3.src.tar.gz
wget http://llvm.org/releases/3.3/cfe-3.3.src.tar.gz
wget http://llvm.org/releases/3.3/compiler-rt-3.3.src.tar.gz
# wget http://llvm.org/releases/3.3/lldb-3.3.src.tar.gz
tar xvf llvm-3.3.src.tar.gz
cd llvm-3.3.src/tools
tar xvf ../../cfe-3.3.src.tar.gz
mv cfe-3.3.src clang
# tar xvf ../../lldb-3.3.src.tar.gz
# mv lldb-3.3.src/ lldb
cd ..
cd projects
tar xvf ../../compiler-rt-3.3.src.tar.gz
mv compiler-rt-3.3.src/ compiler-rt
cd ..
./configure --enable-optimized --prefix=/usr/local
make -j4
# Pause to wait for the password prompt
read -p "Press [Enter] key to install..."
# Begin install
sudo make install
# Install does not copy asan_symbolize.py
sudo cp projects/compiler-rt/lib/asan/scripts/asan_symbolize.py /usr/local/bin
# Install does not install scan-build and scan-view
# Perform the copy, and/or put them on-path
sudo mkdir /usr/local/bin/scan-build
sudo cp -r tools/clang/tools/scan-build /usr/local/bin
sudo mkdir /usr/local/bin/scan-view
sudo cp -r tools/clang/tools/scan-view /usr/local/bin
使用Clang:
export CC=/usr/local/bin/clang
export CXX=/usr/local/bin/clang++
export CFLAGS="-g3 -fsanitize=undefined"
export CXXFLAGS="-g3 -fsanitize=undefined -fno-sanitize=vptr"
./configure
make
make check | /usr/local/bin/asan_symbolize.py
如果遇到任何内存问题,它将类似于以下内容(摘自Squid 3.3.9 MacOSX10.8上的自测试失败):
==76794==ERROR: AddressSanitizer: global-buffer-overflow on address
0x000105ad50d2 at pc 0x105a364ab bp 0x7fff5a23f720 sp 0x7fff5a23f718
READ of size 19 at 0x000105ad50d2 thread T0
#0 0x105a364aa in MemBuf::append MemBuf.cc:248
#1 0x105a4ef57 in testHttpReply::testSanityCheckFirstLine
testHttpReply.cc:197
#2 0x1068d71d1 in CppUnit::TestCaseMethodFunctor::operator()() const (in
libcppunit-1.12.1.dylib) + 33
#3 0x1068cd9a3 in CppUnit::DefaultProtector::protect(CppUnit::Functor
const&, CppUnit::ProtectorContext const&) (in libcppunit-1.12.1.dylib) + 35
#4 0x1068d4d88 in CppUnit::ProtectorChain::ProtectFunctor::operator()()
const (in libcppunit-1.12.1.dylib) + 24
#5 0x1068d45e8 in CppUnit::ProtectorChain::protect(CppUnit::Functor const&,
CppUnit::ProtectorContext const&) (in libcppunit-1.12.1.dylib) + 456
#6 0x1068dcf78 in CppUnit::TestResult::protect(CppUnit::Functor const&,
CppUnit::Test*, std::string const&) (in libcppunit-1.12.1.dylib) + 56
#7 0x1068d6d1d in CppUnit::TestCase::run(CppUnit::TestResult*) (in
libcppunit-1.12.1.dylib) + 285
#8 0x1068d77b6 in
CppUnit::TestComposite::doRunChildTests(CppUnit::TestResult*) (in
libcppunit-1.12.1.dylib) + 54
#9 0x1068d76be in CppUnit::TestComposite::run(CppUnit::TestResult*) (in
libcppunit-1.12.1.dylib) + 30
#10 0x1068d77b6 in
CppUnit::TestComposite::doRunChildTests(CppUnit::TestResult*) (in
libcppunit-1.12.1.dylib) + 54
#11 0x1068d76be in CppUnit::TestComposite::run(CppUnit::TestResult*) (in
libcppunit-1.12.1.dylib) + 30
#12 0x1068dcde1 in CppUnit::TestResult::runTest(CppUnit::Test*) (in
libcppunit-1.12.1.dylib) + 33
#13 0x1068de9a5 in CppUnit::TestRunner::run(CppUnit::TestResult&,
std::string const&) (in libcppunit-1.12.1.dylib) + 53
#14 0x105a55a97 in main testMain.cc:31
#15 0x7fff90af07e0 in start (in libdyld.dylib) + 0
#16 0x0
0x000105ad50d2 is located 46 bytes to the left of global variable '.str28' from
'tests/testHttpReply.cc' (0x105ad5100) of size 16
'.str28' is ascii string 'HTTP/1.1 -000
0x000105ad50d2 is located 0 bytes to the right of global variable '.str27' from
'tests/testHttpReply.cc' (0x105ad50c0) of size 18
'.str27' is ascii string 'HTTP/1.10 Okay
以下是未定义的行为和非法转移(摘自Postgres的Clang 3.3调查结果和非法转移) )
make check
...
vacuuming database template1 ... localtime.c:127:20: runtime error:
left shift of negative value -1
pg_lzcompress.c:601:5: runtime error: left shift of negative value -68
pg_lzcompress.c:601:5: runtime error: left shift of negative value -68
pg_lzcompress.c:385:16: runtime error: left shift of negative value -68
pg_lzcompress.c:615:4: runtime error: left shift of negative value -68
如果您在英特尔的ICC下失败了,那么一定要在Clang和-fsanitize=undefined
下运行它,因为ICC会悄悄地删除任何违规代码。这是我找到的找到违规代码的最简单的方法。
https://stackoverflow.com/questions/21205734
复制相似问题