cmCTestMemCheckHandler.cxx 30 KB

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