cmCTestMemCheckHandler.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestMemCheckHandler.h"
  14. #include "cmXMLParser.h"
  15. #include "cmCTest.h"
  16. #include "cmake.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include <cmsys/Process.h>
  19. #include <cmsys/RegularExpression.hxx>
  20. #include <cmsys/Base64.h>
  21. #include "cmMakefile.h"
  22. #include <stdlib.h>
  23. #include <math.h>
  24. #include <float.h>
  25. struct CatToErrorType
  26. {
  27. const char* ErrorCategory;
  28. int ErrorCode;
  29. };
  30. static CatToErrorType cmCTestMemCheckBoundsChecker[] = {
  31. // Error tags
  32. {"Write Overrun", cmCTestMemCheckHandler::ABW},
  33. {"Read Overrun", cmCTestMemCheckHandler::ABR},
  34. {"Memory Overrun", cmCTestMemCheckHandler::ABW},
  35. {"Allocation Conflict", cmCTestMemCheckHandler::FMM},
  36. {"Bad Pointer Use", cmCTestMemCheckHandler::FMW},
  37. {"Dangling Pointer", cmCTestMemCheckHandler::FMR},
  38. {0,0}
  39. };
  40. // parse the xml file storing the installed version of Xcode on
  41. // the machine
  42. class cmBoundsCheckerParser : public cmXMLParser
  43. {
  44. public:
  45. cmBoundsCheckerParser(cmCTest* c) { this->CTest = c;}
  46. void StartElement(const char* name, const char** atts)
  47. {
  48. if(strcmp(name, "MemoryLeak") == 0)
  49. {
  50. this->Errors.push_back(cmCTestMemCheckHandler::MLK);
  51. }
  52. if(strcmp(name, "ResourceLeak") == 0)
  53. {
  54. this->Errors.push_back(cmCTestMemCheckHandler::MLK);
  55. }
  56. if(strcmp(name, "Error") == 0)
  57. {
  58. this->ParseError(atts);
  59. }
  60. if(strcmp(name, "Dangling Pointer") == 0)
  61. {
  62. this->ParseError(atts);
  63. }
  64. // Create the log
  65. cmOStringStream ostr;
  66. ostr << name << ":\n";
  67. int i = 0;
  68. for(; atts[i] != 0; i+=2)
  69. {
  70. ostr << " " << this->CTest->MakeXMLSafe(atts[i]).c_str()
  71. << " - " << this->CTest->MakeXMLSafe(atts[i+1]).c_str() << "\n";
  72. }
  73. ostr << "\n";
  74. this->Log += ostr.str();
  75. }
  76. void EndElement(const char* )
  77. {
  78. }
  79. const char* GetAttribute(const char* name, const char** atts)
  80. {
  81. int i = 0;
  82. for(; atts[i] != 0; ++i)
  83. {
  84. if(strcmp(name, atts[i]) == 0)
  85. {
  86. return atts[i+1];
  87. }
  88. }
  89. return 0;
  90. }
  91. void ParseError(const char** atts)
  92. {
  93. CatToErrorType* ptr = cmCTestMemCheckBoundsChecker;
  94. const char* cat = this->GetAttribute("ErrorCategory", atts);
  95. if(!cat)
  96. {
  97. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  98. cmCTestLog(this->CTest, ERROR_MESSAGE,
  99. "No Category found in Bounds checker XML\n" );
  100. return;
  101. }
  102. while(ptr->ErrorCategory && cat)
  103. {
  104. if(strcmp(ptr->ErrorCategory, cat) == 0)
  105. {
  106. this->Errors.push_back(ptr->ErrorCode);
  107. return; // found it we are done
  108. }
  109. ptr++;
  110. }
  111. if(ptr->ErrorCategory)
  112. {
  113. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  114. cmCTestLog(this->CTest, ERROR_MESSAGE,
  115. "Found unknown Bounds Checker error "
  116. << ptr->ErrorCategory << std::endl);
  117. }
  118. }
  119. cmCTest* CTest;
  120. std::vector<int> Errors;
  121. std::string Log;
  122. };
  123. #define BOUNDS_CHECKER_MARKER "******######*****Begin BOUNDS CHECKER XML******######******"
  124. //----------------------------------------------------------------------
  125. static const char* cmCTestMemCheckResultStrings[] = {
  126. "ABR",
  127. "ABW",
  128. "ABWL",
  129. "COR",
  130. "EXU",
  131. "FFM",
  132. "FIM",
  133. "FMM",
  134. "FMR",
  135. "FMW",
  136. "FUM",
  137. "IPR",
  138. "IPW",
  139. "MAF",
  140. "MLK",
  141. "MPK",
  142. "NPR",
  143. "ODS",
  144. "PAR",
  145. "PLK",
  146. "UMC",
  147. "UMR",
  148. 0
  149. };
  150. //----------------------------------------------------------------------
  151. static const char* cmCTestMemCheckResultLongStrings[] = {
  152. "Threading Problem",
  153. "ABW",
  154. "ABWL",
  155. "COR",
  156. "EXU",
  157. "FFM",
  158. "FIM",
  159. "Mismatched deallocation",
  160. "FMR",
  161. "FMW",
  162. "FUM",
  163. "IPR",
  164. "IPW",
  165. "MAF",
  166. "Memory Leak",
  167. "Potential Memory Leak",
  168. "NPR",
  169. "ODS",
  170. "Invalid syscall param",
  171. "PLK",
  172. "Uninitialized Memory Conditional",
  173. "Uninitialized Memory Read",
  174. 0
  175. };
  176. //----------------------------------------------------------------------
  177. cmCTestMemCheckHandler::cmCTestMemCheckHandler()
  178. {
  179. this->MemCheck = true;
  180. this->CustomMaximumPassedTestOutputSize = 0;
  181. this->CustomMaximumFailedTestOutputSize = 0;
  182. }
  183. //----------------------------------------------------------------------
  184. void cmCTestMemCheckHandler::Initialize()
  185. {
  186. this->Superclass::Initialize();
  187. this->MemoryTester = "";
  188. this->MemoryTesterOptionsParsed.clear();
  189. this->MemoryTesterOptions = "";
  190. this->MemoryTesterStyle = UNKNOWN;
  191. this->MemoryTesterOutputFile = "";
  192. int cc;
  193. for ( cc = 0; cc < NO_MEMORY_FAULT; cc ++ )
  194. {
  195. this->MemoryTesterGlobalResults[cc] = 0;
  196. }
  197. }
  198. //----------------------------------------------------------------------
  199. int cmCTestMemCheckHandler::PreProcessHandler()
  200. {
  201. if ( !this->InitializeMemoryChecking() )
  202. {
  203. return 0;
  204. }
  205. if ( !this->ExecuteCommands(this->CustomPreMemCheck) )
  206. {
  207. cmCTestLog(this->CTest, ERROR_MESSAGE,
  208. "Problem executing pre-memcheck command(s)." << std::endl);
  209. return 0;
  210. }
  211. return 1;
  212. }
  213. //----------------------------------------------------------------------
  214. int cmCTestMemCheckHandler::PostProcessHandler()
  215. {
  216. if ( !this->ExecuteCommands(this->CustomPostMemCheck) )
  217. {
  218. cmCTestLog(this->CTest, ERROR_MESSAGE,
  219. "Problem executing post-memcheck command(s)." << std::endl);
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. //----------------------------------------------------------------------
  225. void cmCTestMemCheckHandler::GenerateTestCommand(
  226. std::vector<const char*>& args)
  227. {
  228. std::vector<cmStdString>::size_type pp;
  229. args.push_back(this->MemoryTester.c_str());
  230. std::string memcheckcommand = "";
  231. memcheckcommand = this->MemoryTester;
  232. for ( pp = 0; pp < this->MemoryTesterOptionsParsed.size(); pp ++ )
  233. {
  234. args.push_back(this->MemoryTesterOptionsParsed[pp].c_str());
  235. memcheckcommand += " ";
  236. memcheckcommand += cmSystemTools::EscapeSpaces(
  237. this->MemoryTesterOptionsParsed[pp].c_str());
  238. }
  239. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Memory check command: "
  240. << memcheckcommand << std::endl);
  241. }
  242. //----------------------------------------------------------------------
  243. void cmCTestMemCheckHandler::PopulateCustomVectors(cmMakefile *mf)
  244. {
  245. this->cmCTestTestHandler::PopulateCustomVectors(mf);
  246. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_MEMCHECK",
  247. this->CustomPreMemCheck);
  248. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_MEMCHECK",
  249. this->CustomPostMemCheck);
  250. this->CTest->PopulateCustomVector(mf,
  251. "CTEST_CUSTOM_MEMCHECK_IGNORE",
  252. this->CustomTestsIgnore);
  253. }
  254. //----------------------------------------------------------------------
  255. void cmCTestMemCheckHandler::GenerateDartOutput(std::ostream& os)
  256. {
  257. if ( !this->CTest->GetProduceXML() )
  258. {
  259. return;
  260. }
  261. this->CTest->StartXML(os);
  262. os << "<DynamicAnalysis Checker=\"";
  263. switch ( this->MemoryTesterStyle )
  264. {
  265. case cmCTestMemCheckHandler::VALGRIND:
  266. os << "Valgrind";
  267. break;
  268. case cmCTestMemCheckHandler::PURIFY:
  269. os << "Purify";
  270. break;
  271. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  272. os << "BoundsChecker";
  273. break;
  274. default:
  275. os << "Unknown";
  276. }
  277. os << "\">" << std::endl;
  278. os << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
  279. << "\t<TestList>\n";
  280. cmCTestMemCheckHandler::TestResultsVector::size_type cc;
  281. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  282. {
  283. cmCTestTestResult *result = &this->TestResults[cc];
  284. std::string testPath = result->Path + "/" + result->Name;
  285. os << "\t\t<Test>" << cmCTest::MakeXMLSafe(
  286. this->CTest->GetShortPathToFile(testPath.c_str()))
  287. << "</Test>" << std::endl;
  288. }
  289. os << "\t</TestList>\n";
  290. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  291. "-- Processing memory checking output: ");
  292. size_t total = this->TestResults.size();
  293. size_t step = total / 10;
  294. size_t current = 0;
  295. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  296. {
  297. cmCTestTestResult *result = &this->TestResults[cc];
  298. std::string memcheckstr;
  299. int memcheckresults[cmCTestMemCheckHandler::NO_MEMORY_FAULT];
  300. int kk;
  301. bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
  302. memcheckresults);
  303. if ( res && result->Status == cmCTestMemCheckHandler::COMPLETED )
  304. {
  305. continue;
  306. }
  307. this->CleanTestOutput(memcheckstr,
  308. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize));
  309. os << "\t<Test Status=\"";
  310. if ( result->Status == cmCTestMemCheckHandler::COMPLETED )
  311. {
  312. os << "passed";
  313. }
  314. else if ( result->Status == cmCTestMemCheckHandler::NOT_RUN )
  315. {
  316. os << "notrun";
  317. }
  318. else
  319. {
  320. os << "failed";
  321. }
  322. std::string testPath = result->Path + "/" + result->Name;
  323. os << "\">\n"
  324. << "\t\t<Name>" << cmCTest::MakeXMLSafe(result->Name) << "</Name>\n"
  325. << "\t\t<Path>" << cmCTest::MakeXMLSafe(
  326. this->CTest->GetShortPathToFile(result->Path.c_str())) << "</Path>\n"
  327. << "\t\t<FullName>" << cmCTest::MakeXMLSafe(
  328. this->CTest->GetShortPathToFile(testPath.c_str())) << "</FullName>\n"
  329. << "\t\t<FullCommandLine>"
  330. << cmCTest::MakeXMLSafe(result->FullCommandLine)
  331. << "</FullCommandLine>\n"
  332. << "\t\t<Results>" << std::endl;
  333. for ( kk = 0; cmCTestMemCheckResultLongStrings[kk]; kk ++ )
  334. {
  335. if ( memcheckresults[kk] )
  336. {
  337. os << "\t\t\t<Defect type=\"" << cmCTestMemCheckResultLongStrings[kk]
  338. << "\">"
  339. << memcheckresults[kk]
  340. << "</Defect>" << std::endl;
  341. }
  342. this->MemoryTesterGlobalResults[kk] += memcheckresults[kk];
  343. }
  344. os
  345. << "\t\t</Results>\n"
  346. << "\t<Log>\n" << memcheckstr << std::endl
  347. << "\t</Log>\n"
  348. << "\t</Test>" << std::endl;
  349. if ( current < cc )
  350. {
  351. cmCTestLog(this->CTest, HANDLER_OUTPUT, "#" << std::flush);
  352. current += step;
  353. }
  354. }
  355. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  356. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory checking results:"
  357. << std::endl);
  358. os << "\t<DefectList>" << std::endl;
  359. for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  360. {
  361. if ( this->MemoryTesterGlobalResults[cc] )
  362. {
  363. #ifdef cerr
  364. # undef cerr
  365. #endif
  366. std::cerr.width(35);
  367. #define cerr no_cerr
  368. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  369. cmCTestMemCheckResultLongStrings[cc] << " - "
  370. << this->MemoryTesterGlobalResults[cc] << std::endl);
  371. os << "\t\t<Defect Type=\"" << cmCTestMemCheckResultLongStrings[cc]
  372. << "\"/>" << std::endl;
  373. }
  374. }
  375. os << "\t</DefectList>" << std::endl;
  376. os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>" << std::endl;
  377. os << "<ElapsedMinutes>"
  378. << static_cast<int>(this->ElapsedTestingTime/6)/10.0
  379. << "</ElapsedMinutes>\n";
  380. os << "</DynamicAnalysis>" << std::endl;
  381. this->CTest->EndXML(os);
  382. }
  383. //----------------------------------------------------------------------
  384. bool cmCTestMemCheckHandler::InitializeMemoryChecking()
  385. {
  386. // Setup the command
  387. if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  388. "MemoryCheckCommand").c_str()) )
  389. {
  390. this->MemoryTester
  391. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  392. "MemoryCheckCommand").c_str());
  393. }
  394. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  395. "PurifyCommand").c_str()) )
  396. {
  397. this->MemoryTester
  398. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  399. "PurifyCommand").c_str());
  400. }
  401. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  402. "ValgrindCommand").c_str()) )
  403. {
  404. this->MemoryTester
  405. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  406. "ValgrindCommand").c_str());
  407. }
  408. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  409. "BoundsCheckerCommand").c_str()) )
  410. {
  411. this->MemoryTester
  412. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  413. "BoundsCheckerCommand").c_str());
  414. }
  415. else
  416. {
  417. cmCTestLog(this->CTest, WARNING,
  418. "Memory checker (MemoryCheckCommand) "
  419. "not set, or cannot find the specified program."
  420. << std::endl);
  421. return false;
  422. }
  423. if ( this->MemoryTester[0] == '\"' &&
  424. this->MemoryTester[this->MemoryTester.size()-1] == '\"' )
  425. {
  426. this->MemoryTester
  427. = this->MemoryTester.substr(1, this->MemoryTester.size()-2);
  428. }
  429. // Setup the options
  430. if ( this->CTest->GetCTestConfiguration(
  431. "MemoryCheckCommandOptions").size() )
  432. {
  433. this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
  434. "MemoryCheckCommandOptions");
  435. }
  436. else if ( this->CTest->GetCTestConfiguration(
  437. "ValgrindCommandOptions").size() )
  438. {
  439. this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
  440. "ValgrindCommandOptions");
  441. }
  442. this->MemoryTesterOutputFile
  443. = this->CTest->GetBinaryDir() + "/Testing/Temporary/MemoryChecker.log";
  444. if ( this->MemoryTester.find("valgrind") != std::string::npos )
  445. {
  446. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  447. if ( !this->MemoryTesterOptions.size() )
  448. {
  449. this->MemoryTesterOptions = "-q --tool=memcheck --leak-check=yes "
  450. "--show-reachable=yes --workaround-gcc296-bugs=yes --num-callers=100";
  451. }
  452. if ( this->CTest->GetCTestConfiguration(
  453. "MemoryCheckSuppressionFile").size() )
  454. {
  455. if ( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  456. "MemoryCheckSuppressionFile").c_str()) )
  457. {
  458. cmCTestLog(this->CTest, ERROR_MESSAGE,
  459. "Cannot find memory checker suppression file: "
  460. << this->CTest->GetCTestConfiguration(
  461. "MemoryCheckSuppressionFile").c_str() << std::endl);
  462. return false;
  463. }
  464. this->MemoryTesterOptions += " --suppressions=" +
  465. cmSystemTools::EscapeSpaces(this->CTest->GetCTestConfiguration(
  466. "MemoryCheckSuppressionFile").c_str()) + "";
  467. }
  468. }
  469. else if ( this->MemoryTester.find("purify") != std::string::npos )
  470. {
  471. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  472. std::string outputFile =
  473. cmSystemTools::EscapeSpaces(this->MemoryTesterOutputFile.c_str());
  474. #ifdef _WIN32
  475. this->MemoryTesterOptions += " /SAVETEXTDATA=" + outputFile;
  476. #else
  477. this->MemoryTesterOptions += " -log-file=" + outputFile;
  478. #endif
  479. }
  480. else if ( this->MemoryTester.find("BC") != std::string::npos )
  481. {
  482. this->BoundsCheckerXMLFile = this->MemoryTesterOutputFile;
  483. std::string outputFile =
  484. cmSystemTools::EscapeSpaces(this->MemoryTesterOutputFile.c_str());
  485. std::string dpbdFile = this->CTest->GetBinaryDir()
  486. + "/Testing/Temporary/MemoryChecker.DPbd";
  487. std::string errorFile = this->CTest->GetBinaryDir()
  488. + "/Testing/Temporary/MemoryChecker.error";
  489. errorFile = cmSystemTools::EscapeSpaces(errorFile.c_str());
  490. this->BoundsCheckerDPBDFile = dpbdFile;
  491. dpbdFile = cmSystemTools::EscapeSpaces(dpbdFile.c_str());
  492. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  493. this->MemoryTesterOptions += " /B " + dpbdFile;
  494. this->MemoryTesterOptions += " /X " + outputFile;
  495. this->MemoryTesterOptions += " /M ";
  496. }
  497. else
  498. {
  499. cmCTestLog(this->CTest, ERROR_MESSAGE,
  500. "Do not understand memory checker: " << this->MemoryTester.c_str()
  501. << std::endl);
  502. return false;
  503. }
  504. this->MemoryTesterOptionsParsed
  505. = cmSystemTools::ParseArguments(this->MemoryTesterOptions.c_str());
  506. std::vector<cmStdString>::size_type cc;
  507. for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  508. {
  509. this->MemoryTesterGlobalResults[cc] = 0;
  510. }
  511. return true;
  512. }
  513. //----------------------------------------------------------------------
  514. bool cmCTestMemCheckHandler::ProcessMemCheckOutput(const std::string& str,
  515. std::string& log, int* results)
  516. {
  517. std::string::size_type cc;
  518. for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
  519. {
  520. results[cc] = 0;
  521. }
  522. if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::VALGRIND )
  523. {
  524. return this->ProcessMemCheckValgrindOutput(str, log, results);
  525. }
  526. else if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
  527. {
  528. return this->ProcessMemCheckPurifyOutput(str, log, results);
  529. }
  530. else if ( this->MemoryTesterStyle ==
  531. cmCTestMemCheckHandler::BOUNDS_CHECKER )
  532. {
  533. return this->ProcessMemCheckBoundsCheckerOutput(str, log, results);
  534. }
  535. else
  536. {
  537. log.append("\nMemory checking style used was: ");
  538. log.append("None that I know");
  539. log = str;
  540. }
  541. return true;
  542. }
  543. //----------------------------------------------------------------------
  544. bool cmCTestMemCheckHandler::ProcessMemCheckPurifyOutput(
  545. const std::string& str, std::string& log,
  546. int* results)
  547. {
  548. std::vector<cmStdString> lines;
  549. cmSystemTools::Split(str.c_str(), lines);
  550. cmOStringStream ostr;
  551. log = "";
  552. cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
  553. int defects = 0;
  554. for( std::vector<cmStdString>::iterator i = lines.begin();
  555. i != lines.end(); ++i)
  556. {
  557. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  558. if ( pfW.find(*i) )
  559. {
  560. int cc;
  561. for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
  562. {
  563. if ( pfW.match(1) == cmCTestMemCheckResultStrings[cc] )
  564. {
  565. failure = cc;
  566. break;
  567. }
  568. }
  569. if ( cc == cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  570. {
  571. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
  572. << pfW.match(1) << std::endl);
  573. ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
  574. << std::endl;
  575. }
  576. }
  577. if ( failure != NO_MEMORY_FAULT )
  578. {
  579. ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
  580. results[failure] ++;
  581. defects ++;
  582. }
  583. ostr << cmCTest::MakeXMLSafe(*i) << std::endl;
  584. }
  585. log = ostr.str();
  586. if ( defects )
  587. {
  588. return false;
  589. }
  590. return true;
  591. }
  592. //----------------------------------------------------------------------
  593. bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
  594. const std::string& str, std::string& log,
  595. int* results)
  596. {
  597. std::vector<cmStdString> lines;
  598. cmSystemTools::Split(str.c_str(), lines);
  599. bool unlimitedOutput = false;
  600. if(str.find("CTEST_FULL_OUTPUT") != str.npos ||
  601. this->CustomMaximumFailedTestOutputSize == 0)
  602. {
  603. unlimitedOutput = true;
  604. }
  605. std::string::size_type cc;
  606. cmOStringStream ostr;
  607. log = "";
  608. int defects = 0;
  609. cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
  610. cmsys::RegularExpression vgFIM(
  611. "== .*Invalid free\\(\\) / delete / delete\\[\\]");
  612. cmsys::RegularExpression vgFMM(
  613. "== .*Mismatched free\\(\\) / delete / delete \\[\\]");
  614. cmsys::RegularExpression vgMLK(
  615. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are definitely lost"
  616. " in loss record [0-9][0-9]* of [0-9]");
  617. cmsys::RegularExpression vgPAR(
  618. "== .*Syscall param .* contains unaddressable byte\\(s\\)");
  619. cmsys::RegularExpression vgMPK1(
  620. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are possibly lost in"
  621. " loss record [0-9][0-9]* of [0-9]");
  622. cmsys::RegularExpression vgMPK2(
  623. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are still reachable"
  624. " in loss record [0-9][0-9]* of [0-9]");
  625. cmsys::RegularExpression vgUMC(
  626. "== .*Conditional jump or move depends on uninitialised value\\(s\\)");
  627. cmsys::RegularExpression vgUMR1(
  628. "== .*Use of uninitialised value of size [0-9][0-9]*");
  629. cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9][0-9]*");
  630. cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
  631. cmsys::RegularExpression vgUMR4("== .*Syscall param .* contains "
  632. "uninitialised or unaddressable byte\\(s\\)");
  633. cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9]");
  634. cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
  635. "locked by a different thread");
  636. std::vector<std::string::size_type> nonValGrindOutput;
  637. double sttime = cmSystemTools::GetTime();
  638. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  639. std::string::size_type totalOutputSize = 0;
  640. bool outputFull = false;
  641. for ( cc = 0; cc < lines.size(); cc ++ )
  642. {
  643. if ( valgrindLine.find(lines[cc]) )
  644. {
  645. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  646. if ( vgFIM.find(lines[cc]) )
  647. {
  648. failure = cmCTestMemCheckHandler::FIM;
  649. }
  650. else if ( vgFMM.find(lines[cc]) )
  651. {
  652. failure = cmCTestMemCheckHandler::FMM;
  653. }
  654. else if ( vgMLK.find(lines[cc]) )
  655. {
  656. failure = cmCTestMemCheckHandler::MLK;
  657. }
  658. else if ( vgPAR.find(lines[cc]) )
  659. {
  660. failure = cmCTestMemCheckHandler::PAR;
  661. }
  662. else if ( vgMPK1.find(lines[cc]) )
  663. {
  664. failure = cmCTestMemCheckHandler::MPK;
  665. }
  666. else if ( vgMPK2.find(lines[cc]) )
  667. {
  668. failure = cmCTestMemCheckHandler::MPK;
  669. }
  670. else if ( vgUMC.find(lines[cc]) )
  671. {
  672. failure = cmCTestMemCheckHandler::UMC;
  673. }
  674. else if ( vgUMR1.find(lines[cc]) )
  675. {
  676. failure = cmCTestMemCheckHandler::UMR;
  677. }
  678. else if ( vgUMR2.find(lines[cc]) )
  679. {
  680. failure = cmCTestMemCheckHandler::UMR;
  681. }
  682. else if ( vgUMR3.find(lines[cc]) )
  683. {
  684. failure = cmCTestMemCheckHandler::UMR;
  685. }
  686. else if ( vgUMR4.find(lines[cc]) )
  687. {
  688. failure = cmCTestMemCheckHandler::UMR;
  689. }
  690. else if ( vgIPW.find(lines[cc]) )
  691. {
  692. failure = cmCTestMemCheckHandler::IPW;
  693. }
  694. else if ( vgABR.find(lines[cc]) )
  695. {
  696. failure = cmCTestMemCheckHandler::ABR;
  697. }
  698. if ( failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  699. {
  700. ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
  701. results[failure] ++;
  702. defects ++;
  703. }
  704. if(!outputFull)
  705. {
  706. totalOutputSize += lines[cc].size();
  707. ostr << cmCTest::MakeXMLSafe(lines[cc]) << std::endl;
  708. if(totalOutputSize >
  709. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize))
  710. {
  711. outputFull = true;
  712. }
  713. }
  714. }
  715. else
  716. {
  717. nonValGrindOutput.push_back(cc);
  718. }
  719. }
  720. // Now put all all the non valgrind output into the test output
  721. if(!outputFull)
  722. {
  723. for(std::vector<std::string::size_type>::iterator i =
  724. nonValGrindOutput.begin(); i != nonValGrindOutput.end(); ++i)
  725. {
  726. totalOutputSize += lines[*i].size();
  727. ostr << cmCTest::MakeXMLSafe(lines[*i]) << std::endl;
  728. if(!unlimitedOutput && totalOutputSize >
  729. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize))
  730. {
  731. outputFull = true;
  732. ostr << "....\n";
  733. ostr << "Output for this test has been truncated see testing machine logs for full output,\n";
  734. ostr << "or put CTEST_FULL_OUTPUT in the output of this test program.\n";
  735. }
  736. }
  737. }
  738. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  739. << (cmSystemTools::GetTime() - sttime) << std::endl);
  740. log = ostr.str();
  741. if ( defects )
  742. {
  743. return false;
  744. }
  745. return true;
  746. }
  747. //----------------------------------------------------------------------
  748. bool cmCTestMemCheckHandler::ProcessMemCheckBoundsCheckerOutput(
  749. const std::string& str, std::string& log,
  750. int* results)
  751. {
  752. log = "";
  753. double sttime = cmSystemTools::GetTime();
  754. std::vector<cmStdString> lines;
  755. cmSystemTools::Split(str.c_str(), lines);
  756. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  757. std::vector<cmStdString>::size_type cc;
  758. for ( cc = 0; cc < lines.size(); cc ++ )
  759. {
  760. if(lines[cc] == BOUNDS_CHECKER_MARKER)
  761. {
  762. break;
  763. }
  764. }
  765. cmBoundsCheckerParser parser(this->CTest);
  766. parser.InitializeParser();
  767. if(cc < lines.size())
  768. {
  769. for(cc++; cc < lines.size(); ++cc)
  770. {
  771. std::string& theLine = lines[cc];
  772. // check for command line arguments that are not escaped
  773. // correctly by BC
  774. if(theLine.find("TargetArgs=") != theLine.npos)
  775. {
  776. // skip this because BC gets it wrong and we can't parse it
  777. }
  778. else if(!parser.ParseChunk(theLine.c_str(), theLine.size()))
  779. {
  780. cmCTestLog(this->CTest, ERROR_MESSAGE,
  781. "Error in ParseChunk: " << theLine.c_str()
  782. << std::endl);
  783. }
  784. }
  785. }
  786. int defects = 0;
  787. for(cc =0; cc < parser.Errors.size(); ++cc)
  788. {
  789. results[parser.Errors[cc]]++;
  790. defects++;
  791. }
  792. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  793. << (cmSystemTools::GetTime() - sttime) << std::endl);
  794. if(defects)
  795. {
  796. // only put the output of Bounds Checker if there were
  797. // errors or leaks detected
  798. log = parser.Log;
  799. return false;
  800. }
  801. return true;
  802. }
  803. void
  804. cmCTestMemCheckHandler::ProcessOneTest(cmCTestTestProperties *props,
  805. std::vector<cmStdString> &passed,
  806. std::vector<cmStdString> &failed,
  807. int count, int tmsize)
  808. {
  809. // run parent test
  810. cmCTestTestHandler::ProcessOneTest(props, passed, failed, count, tmsize);
  811. cmCTestTestResult& res = this->TestResults[this->TestResults.size()-1];
  812. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "process test output now: "
  813. << props->Name.c_str() << " " << res.Name.c_str() << std::endl);
  814. if( this->MemoryTesterStyle == cmCTestMemCheckHandler::BOUNDS_CHECKER)
  815. {
  816. this->PostProcessBoundsCheckerTest(res);
  817. }
  818. else if(this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
  819. {
  820. this->PostProcessPurifyTest(res);
  821. }
  822. }
  823. // This method puts the bounds checker output file into the output
  824. // for the test
  825. void
  826. cmCTestMemCheckHandler::PostProcessBoundsCheckerTest(cmCTestTestResult& res)
  827. {
  828. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  829. "PostProcessBoundsCheckerTest for : "
  830. << res.Name.c_str() << std::endl);
  831. if ( !cmSystemTools::FileExists(this->MemoryTesterOutputFile.c_str()) )
  832. {
  833. std::string log = "Cannot find memory tester output file: "
  834. + this->MemoryTesterOutputFile;
  835. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  836. return;
  837. }
  838. // put a scope around this to close ifs so the file can be removed
  839. {
  840. std::ifstream ifs(this->MemoryTesterOutputFile.c_str());
  841. if ( !ifs )
  842. {
  843. std::string log = "Cannot read memory tester output file: " + this->MemoryTesterOutputFile;
  844. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  845. return;
  846. }
  847. res.Output += BOUNDS_CHECKER_MARKER;
  848. res.Output += "\n";
  849. std::string line;
  850. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  851. {
  852. res.Output += line;
  853. res.Output += "\n";
  854. }
  855. }
  856. cmSystemTools::Delay(1000);
  857. cmSystemTools::RemoveFile(this->BoundsCheckerDPBDFile.c_str());
  858. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  859. << this->BoundsCheckerDPBDFile.c_str() << std::endl);
  860. cmSystemTools::RemoveFile(this->BoundsCheckerXMLFile.c_str());
  861. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  862. << this->BoundsCheckerXMLFile.c_str() << std::endl);
  863. }
  864. void
  865. cmCTestMemCheckHandler::PostProcessPurifyTest(cmCTestTestResult& res)
  866. {
  867. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  868. "PostProcessPurifyTest for : "
  869. << res.Name.c_str() << std::endl);
  870. if ( !cmSystemTools::FileExists(this->MemoryTesterOutputFile.c_str()) )
  871. {
  872. std::string log = "Cannot find memory tester output file: "
  873. + this->MemoryTesterOutputFile;
  874. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  875. return;
  876. }
  877. std::ifstream ifs(this->MemoryTesterOutputFile.c_str());
  878. if ( !ifs )
  879. {
  880. std::string log = "Cannot read memory tester output file: " + this->MemoryTesterOutputFile;
  881. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  882. return;
  883. }
  884. std::string line;
  885. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  886. {
  887. res.Output += line;
  888. }
  889. }