cmCTestMemCheckHandler.cxx 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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 <cmsys/Glob.hxx>
  19. #include <cmsys/FStream.hxx>
  20. #include "cmMakefile.h"
  21. #include "cmXMLWriter.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. static void xmlReportError(int line, const char* msg, void* data)
  41. {
  42. cmCTest* ctest = (cmCTest*)data;
  43. cmCTestLog(ctest, ERROR_MESSAGE,
  44. "Error parsing XML in stream at line "
  45. << line << ": " << msg << std::endl);
  46. }
  47. // parse the xml file containing the results of last BoundsChecker run
  48. class cmBoundsCheckerParser : public cmXMLParser
  49. {
  50. public:
  51. cmBoundsCheckerParser(cmCTest* c)
  52. {
  53. this->CTest = c;
  54. this->SetErrorCallback(xmlReportError, (void*)c);
  55. }
  56. void StartElement(const std::string& name, const char** atts)
  57. {
  58. if(name == "MemoryLeak" ||
  59. name == "ResourceLeak")
  60. {
  61. this->Errors.push_back(cmCTestMemCheckHandler::MLK);
  62. }
  63. else if(name == "Error" ||
  64. name == "Dangling Pointer")
  65. {
  66. this->ParseError(atts);
  67. }
  68. // Create the log
  69. std::ostringstream ostr;
  70. ostr << name << ":\n";
  71. int i = 0;
  72. for(; atts[i] != 0; i+=2)
  73. {
  74. ostr << " " << atts[i]
  75. << " - " << atts[i+1] << "\n";
  76. }
  77. ostr << "\n";
  78. this->Log += ostr.str();
  79. }
  80. void EndElement(const std::string& )
  81. {
  82. }
  83. const char* GetAttribute(const char* name, const char** atts)
  84. {
  85. int i = 0;
  86. for(; atts[i] != 0; ++i)
  87. {
  88. if(strcmp(name, atts[i]) == 0)
  89. {
  90. return atts[i+1];
  91. }
  92. }
  93. return 0;
  94. }
  95. void ParseError(const char** atts)
  96. {
  97. CatToErrorType* ptr = cmCTestMemCheckBoundsChecker;
  98. const char* cat = this->GetAttribute("ErrorCategory", atts);
  99. if(!cat)
  100. {
  101. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  102. cmCTestLog(this->CTest, ERROR_MESSAGE,
  103. "No Category found in Bounds checker XML\n" );
  104. return;
  105. }
  106. while(ptr->ErrorCategory && cat)
  107. {
  108. if(strcmp(ptr->ErrorCategory, cat) == 0)
  109. {
  110. this->Errors.push_back(ptr->ErrorCode);
  111. return; // found it we are done
  112. }
  113. ptr++;
  114. }
  115. if(ptr->ErrorCategory)
  116. {
  117. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  118. cmCTestLog(this->CTest, ERROR_MESSAGE,
  119. "Found unknown Bounds Checker error "
  120. << ptr->ErrorCategory << std::endl);
  121. }
  122. }
  123. cmCTest* CTest;
  124. std::vector<int> Errors;
  125. std::string Log;
  126. };
  127. #define BOUNDS_CHECKER_MARKER \
  128. "******######*****Begin BOUNDS CHECKER XML******######******"
  129. //----------------------------------------------------------------------
  130. cmCTestMemCheckHandler::cmCTestMemCheckHandler()
  131. {
  132. this->MemCheck = true;
  133. this->CustomMaximumPassedTestOutputSize = 0;
  134. this->CustomMaximumFailedTestOutputSize = 0;
  135. this->LogWithPID = false;
  136. }
  137. //----------------------------------------------------------------------
  138. void cmCTestMemCheckHandler::Initialize()
  139. {
  140. this->Superclass::Initialize();
  141. this->LogWithPID = false;
  142. this->CustomMaximumPassedTestOutputSize = 0;
  143. this->CustomMaximumFailedTestOutputSize = 0;
  144. this->MemoryTester = "";
  145. this->MemoryTesterDynamicOptions.clear();
  146. this->MemoryTesterOptions.clear();
  147. this->MemoryTesterStyle = UNKNOWN;
  148. this->MemoryTesterOutputFile = "";
  149. }
  150. //----------------------------------------------------------------------
  151. int cmCTestMemCheckHandler::PreProcessHandler()
  152. {
  153. if ( !this->InitializeMemoryChecking() )
  154. {
  155. return 0;
  156. }
  157. if ( !this->ExecuteCommands(this->CustomPreMemCheck) )
  158. {
  159. cmCTestLog(this->CTest, ERROR_MESSAGE,
  160. "Problem executing pre-memcheck command(s)." << std::endl);
  161. return 0;
  162. }
  163. return 1;
  164. }
  165. //----------------------------------------------------------------------
  166. int cmCTestMemCheckHandler::PostProcessHandler()
  167. {
  168. if ( !this->ExecuteCommands(this->CustomPostMemCheck) )
  169. {
  170. cmCTestLog(this->CTest, ERROR_MESSAGE,
  171. "Problem executing post-memcheck command(s)." << std::endl);
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. //----------------------------------------------------------------------
  177. void cmCTestMemCheckHandler::GenerateTestCommand(
  178. std::vector<std::string>& args, int test)
  179. {
  180. std::vector<std::string>::size_type pp;
  181. std::string index;
  182. std::ostringstream stream;
  183. std::string memcheckcommand
  184. = cmSystemTools::ConvertToOutputPath(this->MemoryTester.c_str());
  185. stream << test;
  186. index = stream.str();
  187. for ( pp = 0; pp < this->MemoryTesterDynamicOptions.size(); pp ++ )
  188. {
  189. std::string arg = this->MemoryTesterDynamicOptions[pp];
  190. std::string::size_type pos = arg.find("??");
  191. if (pos != std::string::npos)
  192. {
  193. arg.replace(pos, 2, index);
  194. }
  195. args.push_back(arg);
  196. memcheckcommand += " \"";
  197. memcheckcommand += arg;
  198. memcheckcommand += "\"";
  199. }
  200. // Create a copy of the memory tester environment variable.
  201. // This is used for memory testing programs that pass options
  202. // via environment varaibles.
  203. std::string memTesterEnvironmentVariable =
  204. this->MemoryTesterEnvironmentVariable;
  205. for ( pp = 0; pp < this->MemoryTesterOptions.size(); pp ++ )
  206. {
  207. if(!memTesterEnvironmentVariable.empty())
  208. {
  209. // If we are using env to pass options, append all the options to
  210. // this string with space separation.
  211. memTesterEnvironmentVariable += " " + this->MemoryTesterOptions[pp];
  212. }
  213. // for regular options just add them to args and memcheckcommand
  214. // which is just used for display
  215. else
  216. {
  217. args.push_back(this->MemoryTesterOptions[pp]);
  218. memcheckcommand += " \"";
  219. memcheckcommand += this->MemoryTesterOptions[pp];
  220. memcheckcommand += "\"";
  221. }
  222. }
  223. // if this is an env option type, then add the env string as a single
  224. // argument.
  225. if(!memTesterEnvironmentVariable.empty())
  226. {
  227. std::string::size_type pos = memTesterEnvironmentVariable.find("??");
  228. if (pos != std::string::npos)
  229. {
  230. memTesterEnvironmentVariable.replace(pos, 2, index);
  231. }
  232. memcheckcommand += " " + memTesterEnvironmentVariable;
  233. args.push_back(memTesterEnvironmentVariable);
  234. }
  235. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  236. "Memory check command: " << memcheckcommand << std::endl, this->Quiet);
  237. }
  238. //----------------------------------------------------------------------
  239. void cmCTestMemCheckHandler::InitializeResultsVectors()
  240. {
  241. // fill these members
  242. // cmsys::vector<std::string> ResultStrings;
  243. // cmsys::vector<std::string> ResultStringsLong;
  244. // cmsys::vector<int> GlobalResults;
  245. this->ResultStringsLong.clear();
  246. this->ResultStrings.clear();
  247. this->GlobalResults.clear();
  248. // If we are working with style checkers that dynamically fill
  249. // the results strings then return.
  250. if(this->MemoryTesterStyle > cmCTestMemCheckHandler::BOUNDS_CHECKER)
  251. {
  252. return;
  253. }
  254. // define the standard set of errors
  255. //----------------------------------------------------------------------
  256. static const char* cmCTestMemCheckResultStrings[] = {
  257. "ABR",
  258. "ABW",
  259. "ABWL",
  260. "COR",
  261. "EXU",
  262. "FFM",
  263. "FIM",
  264. "FMM",
  265. "FMR",
  266. "FMW",
  267. "FUM",
  268. "IPR",
  269. "IPW",
  270. "MAF",
  271. "MLK",
  272. "MPK",
  273. "NPR",
  274. "ODS",
  275. "PAR",
  276. "PLK",
  277. "UMC",
  278. "UMR",
  279. 0
  280. };
  281. //----------------------------------------------------------------------
  282. static const char* cmCTestMemCheckResultLongStrings[] = {
  283. "Threading Problem",
  284. "ABW",
  285. "ABWL",
  286. "COR",
  287. "EXU",
  288. "FFM",
  289. "FIM",
  290. "Mismatched deallocation",
  291. "FMR",
  292. "FMW",
  293. "FUM",
  294. "IPR",
  295. "IPW",
  296. "MAF",
  297. "Memory Leak",
  298. "Potential Memory Leak",
  299. "NPR",
  300. "ODS",
  301. "Invalid syscall param",
  302. "PLK",
  303. "Uninitialized Memory Conditional",
  304. "Uninitialized Memory Read",
  305. 0
  306. };
  307. this->GlobalResults.clear();
  308. for(int i =0; cmCTestMemCheckResultStrings[i] != 0; ++i)
  309. {
  310. this->ResultStrings.push_back(cmCTestMemCheckResultStrings[i]);
  311. this->ResultStringsLong.push_back(cmCTestMemCheckResultLongStrings[i]);
  312. this->GlobalResults.push_back(0);
  313. }
  314. }
  315. //----------------------------------------------------------------------
  316. void cmCTestMemCheckHandler::PopulateCustomVectors(cmMakefile *mf)
  317. {
  318. this->cmCTestTestHandler::PopulateCustomVectors(mf);
  319. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_MEMCHECK",
  320. this->CustomPreMemCheck);
  321. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_MEMCHECK",
  322. this->CustomPostMemCheck);
  323. this->CTest->PopulateCustomVector(mf,
  324. "CTEST_CUSTOM_MEMCHECK_IGNORE",
  325. this->CustomTestsIgnore);
  326. std::string cmake = cmSystemTools::GetCMakeCommand();
  327. this->CTest->SetCTestConfiguration("CMakeCommand", cmake.c_str(),
  328. this->Quiet);
  329. }
  330. //----------------------------------------------------------------------
  331. void cmCTestMemCheckHandler::GenerateDartOutput(cmXMLWriter& xml)
  332. {
  333. if ( !this->CTest->GetProduceXML() )
  334. {
  335. return;
  336. }
  337. this->CTest->StartXML(xml, this->AppendXML);
  338. xml.StartElement("DynamicAnalysis");
  339. switch ( this->MemoryTesterStyle )
  340. {
  341. case cmCTestMemCheckHandler::VALGRIND:
  342. xml.Attribute("Checker", "Valgrind");
  343. break;
  344. case cmCTestMemCheckHandler::PURIFY:
  345. xml.Attribute("Checker", "Purify");
  346. break;
  347. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  348. xml.Attribute("Checker", "BoundsChecker");
  349. break;
  350. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  351. xml.Attribute("Checker", "AddressSanitizer");
  352. break;
  353. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  354. xml.Attribute("Checker", "ThreadSanitizer");
  355. break;
  356. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  357. xml.Attribute("Checker", "MemorySanitizer");
  358. break;
  359. case cmCTestMemCheckHandler::UB_SANITIZER:
  360. xml.Attribute("Checker", "UndefinedBehaviorSanitizer");
  361. break;
  362. default:
  363. xml.Attribute("Checker", "Unknown");
  364. }
  365. xml.Element("StartDateTime", this->StartTest);
  366. xml.Element("StartTestTime", this->StartTestTime);
  367. xml.StartElement("TestList");
  368. cmCTestMemCheckHandler::TestResultsVector::size_type cc;
  369. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  370. {
  371. cmCTestTestResult *result = &this->TestResults[cc];
  372. std::string testPath = result->Path + "/" + result->Name;
  373. xml.Element("Test", this->CTest->GetShortPathToFile(testPath.c_str()));
  374. }
  375. xml.EndElement(); // TestList
  376. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  377. "-- Processing memory checking output: ", this->Quiet);
  378. size_t total = this->TestResults.size();
  379. size_t step = total / 10;
  380. size_t current = 0;
  381. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  382. {
  383. cmCTestTestResult *result = &this->TestResults[cc];
  384. std::string memcheckstr;
  385. std::vector<int> memcheckresults(this->ResultStrings.size(), 0);
  386. bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
  387. memcheckresults);
  388. if ( res && result->Status == cmCTestMemCheckHandler::COMPLETED )
  389. {
  390. continue;
  391. }
  392. this->CleanTestOutput(memcheckstr,
  393. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize));
  394. this->WriteTestResultHeader(xml, result);
  395. xml.StartElement("Results");
  396. for(std::vector<int>::size_type kk = 0;
  397. kk < memcheckresults.size(); ++kk)
  398. {
  399. if ( memcheckresults[kk] )
  400. {
  401. xml.StartElement("Defect");
  402. xml.Attribute("type", this->ResultStringsLong[kk]);
  403. xml.Content(memcheckresults[kk]);
  404. xml.EndElement(); // Defect
  405. }
  406. this->GlobalResults[kk] += memcheckresults[kk];
  407. }
  408. xml.EndElement(); // Results
  409. xml.StartElement("Log");
  410. if(this->CTest->ShouldCompressMemCheckOutput())
  411. {
  412. this->CTest->CompressString(memcheckstr);
  413. xml.Attribute("compression", "gzip");
  414. xml.Attribute("encoding", "base64");
  415. }
  416. xml.Content(memcheckstr);
  417. xml.EndElement(); // Log
  418. this->WriteTestResultFooter(xml, result);
  419. if ( current < cc )
  420. {
  421. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "#" << std::flush,
  422. this->Quiet);
  423. current += step;
  424. }
  425. }
  426. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl, this->Quiet);
  427. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "Memory checking results:"
  428. << std::endl, this->Quiet);
  429. xml.StartElement("DefectList");
  430. for ( cc = 0; cc < this->GlobalResults.size(); cc ++ )
  431. {
  432. if ( this->GlobalResults[cc] )
  433. {
  434. #ifdef cerr
  435. # undef cerr
  436. #endif
  437. std::cerr.width(35);
  438. #define cerr no_cerr
  439. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  440. this->ResultStringsLong[cc] << " - "
  441. << this->GlobalResults[cc] << std::endl, this->Quiet);
  442. xml.StartElement("Defect");
  443. xml.Attribute("Type", this->ResultStringsLong[cc]);
  444. xml.EndElement();
  445. }
  446. }
  447. xml.EndElement(); // DefectList
  448. xml.Element("EndDateTime", this->EndTest);
  449. xml.Element("EndTestTime", this->EndTestTime);
  450. xml.Element("ElapsedMinutes",
  451. static_cast<int>(this->ElapsedTestingTime/6)/10.0);
  452. xml.EndElement(); // DynamicAnalysis
  453. this->CTest->EndXML(xml);
  454. }
  455. //----------------------------------------------------------------------
  456. bool cmCTestMemCheckHandler::InitializeMemoryChecking()
  457. {
  458. this->MemoryTesterEnvironmentVariable = "";
  459. this->MemoryTester = "";
  460. // Setup the command
  461. if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  462. "MemoryCheckCommand").c_str()) )
  463. {
  464. this->MemoryTester
  465. = this->CTest->GetCTestConfiguration("MemoryCheckCommand").c_str();
  466. std::string testerName =
  467. cmSystemTools::GetFilenameName(this->MemoryTester);
  468. // determine the checker type
  469. if ( testerName.find("valgrind") != std::string::npos ||
  470. this->CTest->GetCTestConfiguration("MemoryCheckType")
  471. == "Valgrind")
  472. {
  473. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  474. }
  475. else if ( testerName.find("purify") != std::string::npos )
  476. {
  477. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  478. }
  479. else if ( testerName.find("BC") != std::string::npos )
  480. {
  481. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  482. }
  483. else
  484. {
  485. this->MemoryTesterStyle = cmCTestMemCheckHandler::UNKNOWN;
  486. }
  487. }
  488. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  489. "PurifyCommand").c_str()) )
  490. {
  491. this->MemoryTester
  492. = this->CTest->GetCTestConfiguration("PurifyCommand").c_str();
  493. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  494. }
  495. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  496. "ValgrindCommand").c_str()) )
  497. {
  498. this->MemoryTester
  499. = this->CTest->GetCTestConfiguration("ValgrindCommand").c_str();
  500. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  501. }
  502. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  503. "BoundsCheckerCommand").c_str()) )
  504. {
  505. this->MemoryTester
  506. = this->CTest->GetCTestConfiguration("BoundsCheckerCommand").c_str();
  507. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  508. }
  509. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  510. == "AddressSanitizer")
  511. {
  512. this->MemoryTester
  513. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  514. this->MemoryTesterStyle = cmCTestMemCheckHandler::ADDRESS_SANITIZER;
  515. this->LogWithPID = true; // even if we give the log file the pid is added
  516. }
  517. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  518. == "ThreadSanitizer")
  519. {
  520. this->MemoryTester
  521. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  522. this->MemoryTesterStyle = cmCTestMemCheckHandler::THREAD_SANITIZER;
  523. this->LogWithPID = true; // even if we give the log file the pid is added
  524. }
  525. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  526. == "MemorySanitizer")
  527. {
  528. this->MemoryTester
  529. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  530. this->MemoryTesterStyle = cmCTestMemCheckHandler::MEMORY_SANITIZER;
  531. this->LogWithPID = true; // even if we give the log file the pid is added
  532. }
  533. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  534. == "UndefinedBehaviorSanitizer")
  535. {
  536. this->MemoryTester
  537. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  538. this->MemoryTesterStyle = cmCTestMemCheckHandler::UB_SANITIZER;
  539. this->LogWithPID = true; // even if we give the log file the pid is added
  540. }
  541. // Check the MemoryCheckType
  542. if(this->MemoryTesterStyle == cmCTestMemCheckHandler::UNKNOWN)
  543. {
  544. std::string checkType =
  545. this->CTest->GetCTestConfiguration("MemoryCheckType");
  546. if(checkType == "Purify")
  547. {
  548. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  549. }
  550. else if(checkType == "BoundsChecker")
  551. {
  552. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  553. }
  554. else if(checkType == "Valgrind")
  555. {
  556. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  557. }
  558. }
  559. if(this->MemoryTester.empty())
  560. {
  561. cmCTestOptionalLog(this->CTest, WARNING,
  562. "Memory checker (MemoryCheckCommand) "
  563. "not set, or cannot find the specified program."
  564. << std::endl, this->Quiet);
  565. return false;
  566. }
  567. // Setup the options
  568. std::string memoryTesterOptions;
  569. if ( this->CTest->GetCTestConfiguration(
  570. "MemoryCheckCommandOptions").size() )
  571. {
  572. memoryTesterOptions = this->CTest->GetCTestConfiguration(
  573. "MemoryCheckCommandOptions");
  574. }
  575. else if ( this->CTest->GetCTestConfiguration(
  576. "ValgrindCommandOptions").size() )
  577. {
  578. memoryTesterOptions = this->CTest->GetCTestConfiguration(
  579. "ValgrindCommandOptions");
  580. }
  581. this->MemoryTesterOptions
  582. = cmSystemTools::ParseArguments(memoryTesterOptions.c_str());
  583. this->MemoryTesterOutputFile
  584. = this->CTest->GetBinaryDir()
  585. + "/Testing/Temporary/MemoryChecker.??.log";
  586. switch ( this->MemoryTesterStyle )
  587. {
  588. case cmCTestMemCheckHandler::VALGRIND:
  589. {
  590. if ( this->MemoryTesterOptions.empty() )
  591. {
  592. this->MemoryTesterOptions.push_back("-q");
  593. this->MemoryTesterOptions.push_back("--tool=memcheck");
  594. this->MemoryTesterOptions.push_back("--leak-check=yes");
  595. this->MemoryTesterOptions.push_back("--show-reachable=yes");
  596. this->MemoryTesterOptions.push_back("--num-callers=50");
  597. }
  598. if ( this->CTest->GetCTestConfiguration(
  599. "MemoryCheckSuppressionFile").size() )
  600. {
  601. if ( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  602. "MemoryCheckSuppressionFile").c_str()) )
  603. {
  604. cmCTestLog(this->CTest, ERROR_MESSAGE,
  605. "Cannot find memory checker suppression file: "
  606. << this->CTest->GetCTestConfiguration(
  607. "MemoryCheckSuppressionFile") << std::endl);
  608. return false;
  609. }
  610. std::string suppressions = "--suppressions="
  611. + this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  612. this->MemoryTesterOptions.push_back(suppressions);
  613. }
  614. std::string outputFile = "--log-file="
  615. + this->MemoryTesterOutputFile;
  616. this->MemoryTesterDynamicOptions.push_back(outputFile);
  617. break;
  618. }
  619. case cmCTestMemCheckHandler::PURIFY:
  620. {
  621. std::string outputFile;
  622. #ifdef _WIN32
  623. if( this->CTest->GetCTestConfiguration(
  624. "MemoryCheckSuppressionFile").size() )
  625. {
  626. if( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  627. "MemoryCheckSuppressionFile").c_str()) )
  628. {
  629. cmCTestLog(this->CTest, ERROR_MESSAGE,
  630. "Cannot find memory checker suppression file: "
  631. << this->CTest->GetCTestConfiguration(
  632. "MemoryCheckSuppressionFile").c_str() << std::endl);
  633. return false;
  634. }
  635. std::string filterFiles = "/FilterFiles="
  636. + this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  637. this->MemoryTesterOptions.push_back(filterFiles);
  638. }
  639. outputFile = "/SAVETEXTDATA=";
  640. #else
  641. outputFile = "-log-file=";
  642. #endif
  643. outputFile += this->MemoryTesterOutputFile;
  644. this->MemoryTesterDynamicOptions.push_back(outputFile);
  645. break;
  646. }
  647. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  648. {
  649. this->BoundsCheckerXMLFile = this->MemoryTesterOutputFile;
  650. std::string dpbdFile = this->CTest->GetBinaryDir()
  651. + "/Testing/Temporary/MemoryChecker.??.DPbd";
  652. this->BoundsCheckerDPBDFile = dpbdFile;
  653. this->MemoryTesterDynamicOptions.push_back("/B");
  654. this->MemoryTesterDynamicOptions.push_back(dpbdFile);
  655. this->MemoryTesterDynamicOptions.push_back("/X");
  656. this->MemoryTesterDynamicOptions.push_back(this->MemoryTesterOutputFile);
  657. this->MemoryTesterOptions.push_back("/M");
  658. break;
  659. }
  660. // these are almost the same but the env var used is different
  661. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  662. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  663. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  664. case cmCTestMemCheckHandler::UB_SANITIZER:
  665. {
  666. // To pass arguments to ThreadSanitizer the environment variable
  667. // TSAN_OPTIONS is used. This is done with the cmake -E env command.
  668. // The MemoryTesterDynamicOptions is setup with the -E env
  669. // Then the MemoryTesterEnvironmentVariable gets the
  670. // TSAN_OPTIONS string with the log_path in it.
  671. this->MemoryTesterDynamicOptions.push_back("-E");
  672. this->MemoryTesterDynamicOptions.push_back("env");
  673. std::string envVar;
  674. std::string extraOptions =
  675. this->CTest->GetCTestConfiguration("MemoryCheckSanitizerOptions");
  676. if(this->MemoryTesterStyle == cmCTestMemCheckHandler::ADDRESS_SANITIZER)
  677. {
  678. envVar = "ASAN_OPTIONS";
  679. extraOptions += " detect_leaks=1";
  680. }
  681. else if(this->MemoryTesterStyle ==
  682. cmCTestMemCheckHandler::THREAD_SANITIZER)
  683. {
  684. envVar = "TSAN_OPTIONS";
  685. }
  686. else if(this->MemoryTesterStyle ==
  687. cmCTestMemCheckHandler::MEMORY_SANITIZER)
  688. {
  689. envVar = "MSAN_OPTIONS";
  690. }
  691. else if(this->MemoryTesterStyle == cmCTestMemCheckHandler::UB_SANITIZER)
  692. {
  693. envVar = "UBSAN_OPTIONS";
  694. }
  695. std::string outputFile = envVar + "=log_path=\""
  696. + this->MemoryTesterOutputFile + "\" ";
  697. this->MemoryTesterEnvironmentVariable = outputFile + extraOptions;
  698. break;
  699. }
  700. default:
  701. cmCTestLog(this->CTest, ERROR_MESSAGE,
  702. "Do not understand memory checker: " << this->MemoryTester
  703. << std::endl);
  704. return false;
  705. }
  706. this->InitializeResultsVectors();
  707. // std::vector<std::string>::size_type cc;
  708. // for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  709. // {
  710. // this->MemoryTesterGlobalResults[cc] = 0;
  711. // }
  712. return true;
  713. }
  714. //----------------------------------------------------------------------
  715. bool cmCTestMemCheckHandler::
  716. ProcessMemCheckOutput(const std::string& str,
  717. std::string& log, std::vector<int>& results)
  718. {
  719. if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::VALGRIND )
  720. {
  721. return this->ProcessMemCheckValgrindOutput(str, log, results);
  722. }
  723. else if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
  724. {
  725. return this->ProcessMemCheckPurifyOutput(str, log, results);
  726. }
  727. else if ( this->MemoryTesterStyle ==
  728. cmCTestMemCheckHandler::ADDRESS_SANITIZER ||
  729. this->MemoryTesterStyle ==
  730. cmCTestMemCheckHandler::THREAD_SANITIZER ||
  731. this->MemoryTesterStyle ==
  732. cmCTestMemCheckHandler::MEMORY_SANITIZER ||
  733. this->MemoryTesterStyle ==
  734. cmCTestMemCheckHandler::UB_SANITIZER)
  735. {
  736. return this->ProcessMemCheckSanitizerOutput(str, log, results);
  737. }
  738. else if ( this->MemoryTesterStyle ==
  739. cmCTestMemCheckHandler::BOUNDS_CHECKER )
  740. {
  741. return this->ProcessMemCheckBoundsCheckerOutput(str, log, results);
  742. }
  743. else
  744. {
  745. log.append("\nMemory checking style used was: ");
  746. log.append("None that I know");
  747. log = str;
  748. }
  749. return true;
  750. }
  751. std::vector<int>::size_type cmCTestMemCheckHandler::FindOrAddWarning(
  752. const std::string& warning)
  753. {
  754. for(std::vector<std::string>::size_type i =0;
  755. i < this->ResultStrings.size(); ++i)
  756. {
  757. if(this->ResultStrings[i] == warning)
  758. {
  759. return i;
  760. }
  761. }
  762. this->GlobalResults.push_back(0); // this must stay the same size
  763. this->ResultStrings.push_back(warning);
  764. this->ResultStringsLong.push_back(warning);
  765. return this->ResultStrings.size()-1;
  766. }
  767. //----------------------------------------------------------------------
  768. bool cmCTestMemCheckHandler::ProcessMemCheckSanitizerOutput(
  769. const std::string& str, std::string& log,
  770. std::vector<int>& result)
  771. {
  772. std::string regex;
  773. switch ( this->MemoryTesterStyle )
  774. {
  775. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  776. regex = "ERROR: AddressSanitizer: (.*) on.*";
  777. break;
  778. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  779. regex = "WARNING: ThreadSanitizer: (.*) \\(pid=.*\\)";
  780. break;
  781. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  782. regex = "WARNING: MemorySanitizer: (.*)";
  783. break;
  784. case cmCTestMemCheckHandler::UB_SANITIZER:
  785. regex = "runtime error: (.*)";
  786. break;
  787. default:
  788. break;
  789. }
  790. cmsys::RegularExpression sanitizerWarning(regex);
  791. cmsys::RegularExpression leakWarning("(Direct|Indirect) leak of .*");
  792. int defects = 0;
  793. std::vector<std::string> lines;
  794. cmSystemTools::Split(str.c_str(), lines);
  795. std::ostringstream ostr;
  796. log = "";
  797. for( std::vector<std::string>::iterator i = lines.begin();
  798. i != lines.end(); ++i)
  799. {
  800. std::string resultFound;
  801. if(leakWarning.find(*i))
  802. {
  803. resultFound = leakWarning.match(1)+" leak";
  804. }
  805. else if (sanitizerWarning.find(*i))
  806. {
  807. resultFound = sanitizerWarning.match(1);
  808. }
  809. if(!resultFound.empty())
  810. {
  811. std::vector<int>::size_type idx = this->FindOrAddWarning(resultFound);
  812. if(result.empty() || idx > result.size()-1)
  813. {
  814. result.push_back(1);
  815. }
  816. else
  817. {
  818. result[idx]++;
  819. }
  820. defects++;
  821. ostr << "<b>" << this->ResultStrings[idx] << "</b> ";
  822. }
  823. ostr << *i << std::endl;
  824. }
  825. log = ostr.str();
  826. if(defects)
  827. {
  828. return false;
  829. }
  830. return true;
  831. }
  832. //----------------------------------------------------------------------
  833. bool cmCTestMemCheckHandler::ProcessMemCheckPurifyOutput(
  834. const std::string& str, std::string& log,
  835. std::vector<int>& results)
  836. {
  837. std::vector<std::string> lines;
  838. cmSystemTools::Split(str.c_str(), lines);
  839. std::ostringstream ostr;
  840. log = "";
  841. cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
  842. int defects = 0;
  843. for( std::vector<std::string>::iterator i = lines.begin();
  844. i != lines.end(); ++i)
  845. {
  846. std::vector<int>::size_type failure = this->ResultStrings.size();
  847. if ( pfW.find(*i) )
  848. {
  849. std::vector<int>::size_type cc;
  850. for ( cc = 0; cc < this->ResultStrings.size(); cc ++ )
  851. {
  852. if ( pfW.match(1) == this->ResultStrings[cc] )
  853. {
  854. failure = cc;
  855. break;
  856. }
  857. }
  858. if ( cc == this->ResultStrings.size() )
  859. {
  860. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
  861. << pfW.match(1) << std::endl);
  862. ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
  863. << std::endl;
  864. }
  865. }
  866. if ( failure != this->ResultStrings.size() )
  867. {
  868. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  869. results[failure] ++;
  870. defects ++;
  871. }
  872. ostr << *i << std::endl;
  873. }
  874. log = ostr.str();
  875. if ( defects )
  876. {
  877. return false;
  878. }
  879. return true;
  880. }
  881. //----------------------------------------------------------------------
  882. bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
  883. const std::string& str, std::string& log,
  884. std::vector<int>& results)
  885. {
  886. std::vector<std::string> lines;
  887. cmSystemTools::Split(str.c_str(), lines);
  888. bool unlimitedOutput = false;
  889. if(str.find("CTEST_FULL_OUTPUT") != str.npos ||
  890. this->CustomMaximumFailedTestOutputSize == 0)
  891. {
  892. unlimitedOutput = true;
  893. }
  894. std::string::size_type cc;
  895. std::ostringstream ostr;
  896. log = "";
  897. int defects = 0;
  898. cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
  899. cmsys::RegularExpression vgFIM(
  900. "== .*Invalid free\\(\\) / delete / delete\\[\\]");
  901. cmsys::RegularExpression vgFMM(
  902. "== .*Mismatched free\\(\\) / delete / delete \\[\\]");
  903. cmsys::RegularExpression vgMLK1(
  904. "== .*[0-9,]+ bytes in [0-9,]+ blocks are definitely lost"
  905. " in loss record [0-9,]+ of [0-9,]+");
  906. cmsys::RegularExpression vgMLK2(
  907. "== .*[0-9,]+ \\([0-9,]+ direct, [0-9,]+ indirect\\)"
  908. " bytes in [0-9,]+ blocks are definitely lost"
  909. " in loss record [0-9,]+ of [0-9,]+");
  910. cmsys::RegularExpression vgPAR(
  911. "== .*Syscall param .* (contains|points to) unaddressable byte\\(s\\)");
  912. cmsys::RegularExpression vgMPK1(
  913. "== .*[0-9,]+ bytes in [0-9,]+ blocks are possibly lost in"
  914. " loss record [0-9,]+ of [0-9,]+");
  915. cmsys::RegularExpression vgMPK2(
  916. "== .*[0-9,]+ bytes in [0-9,]+ blocks are still reachable"
  917. " in loss record [0-9,]+ of [0-9,]+");
  918. cmsys::RegularExpression vgUMC(
  919. "== .*Conditional jump or move depends on uninitialised value\\(s\\)");
  920. cmsys::RegularExpression vgUMR1(
  921. "== .*Use of uninitialised value of size [0-9,]+");
  922. cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9,]+");
  923. cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
  924. cmsys::RegularExpression vgUMR4("== .*Syscall param .* contains "
  925. "uninitialised or unaddressable byte\\(s\\)");
  926. cmsys::RegularExpression vgUMR5("== .*Syscall param .* uninitialised");
  927. cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9,]+");
  928. cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
  929. "locked by a different thread");
  930. std::vector<std::string::size_type> nonValGrindOutput;
  931. double sttime = cmSystemTools::GetTime();
  932. cmCTestOptionalLog(this->CTest, DEBUG,
  933. "Start test: " << lines.size() << std::endl, this->Quiet);
  934. std::string::size_type totalOutputSize = 0;
  935. for ( cc = 0; cc < lines.size(); cc ++ )
  936. {
  937. cmCTestOptionalLog(this->CTest, DEBUG, "test line "
  938. << lines[cc] << std::endl, this->Quiet);
  939. if ( valgrindLine.find(lines[cc]) )
  940. {
  941. cmCTestOptionalLog(this->CTest, DEBUG, "valgrind line "
  942. << lines[cc] << std::endl, this->Quiet);
  943. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  944. if ( vgFIM.find(lines[cc]) )
  945. {
  946. failure = cmCTestMemCheckHandler::FIM;
  947. }
  948. else if ( vgFMM.find(lines[cc]) )
  949. {
  950. failure = cmCTestMemCheckHandler::FMM;
  951. }
  952. else if ( vgMLK1.find(lines[cc]) )
  953. {
  954. failure = cmCTestMemCheckHandler::MLK;
  955. }
  956. else if ( vgMLK2.find(lines[cc]) )
  957. {
  958. failure = cmCTestMemCheckHandler::MLK;
  959. }
  960. else if ( vgPAR.find(lines[cc]) )
  961. {
  962. failure = cmCTestMemCheckHandler::PAR;
  963. }
  964. else if ( vgMPK1.find(lines[cc]) )
  965. {
  966. failure = cmCTestMemCheckHandler::MPK;
  967. }
  968. else if ( vgMPK2.find(lines[cc]) )
  969. {
  970. failure = cmCTestMemCheckHandler::MPK;
  971. }
  972. else if ( vgUMC.find(lines[cc]) )
  973. {
  974. failure = cmCTestMemCheckHandler::UMC;
  975. }
  976. else if ( vgUMR1.find(lines[cc]) )
  977. {
  978. failure = cmCTestMemCheckHandler::UMR;
  979. }
  980. else if ( vgUMR2.find(lines[cc]) )
  981. {
  982. failure = cmCTestMemCheckHandler::UMR;
  983. }
  984. else if ( vgUMR3.find(lines[cc]) )
  985. {
  986. failure = cmCTestMemCheckHandler::UMR;
  987. }
  988. else if ( vgUMR4.find(lines[cc]) )
  989. {
  990. failure = cmCTestMemCheckHandler::UMR;
  991. }
  992. else if ( vgUMR5.find(lines[cc]) )
  993. {
  994. failure = cmCTestMemCheckHandler::UMR;
  995. }
  996. else if ( vgIPW.find(lines[cc]) )
  997. {
  998. failure = cmCTestMemCheckHandler::IPW;
  999. }
  1000. else if ( vgABR.find(lines[cc]) )
  1001. {
  1002. failure = cmCTestMemCheckHandler::ABR;
  1003. }
  1004. if ( failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  1005. {
  1006. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  1007. results[failure] ++;
  1008. defects ++;
  1009. }
  1010. totalOutputSize += lines[cc].size();
  1011. ostr << lines[cc] << std::endl;
  1012. }
  1013. else
  1014. {
  1015. nonValGrindOutput.push_back(cc);
  1016. }
  1017. }
  1018. // Now put all all the non valgrind output into the test output
  1019. // This should be last in case it gets truncated by the output
  1020. // limiting code
  1021. for(std::vector<std::string::size_type>::iterator i =
  1022. nonValGrindOutput.begin(); i != nonValGrindOutput.end(); ++i)
  1023. {
  1024. totalOutputSize += lines[*i].size();
  1025. ostr << lines[*i] << std::endl;
  1026. if(!unlimitedOutput && totalOutputSize >
  1027. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize))
  1028. {
  1029. ostr << "....\n";
  1030. ostr << "Test Output for this test has been truncated see testing"
  1031. " machine logs for full output,\n";
  1032. ostr << "or put CTEST_FULL_OUTPUT in the output of "
  1033. "this test program.\n";
  1034. break; // stop the copy of output if we are full
  1035. }
  1036. }
  1037. cmCTestOptionalLog(this->CTest, DEBUG, "End test (elapsed: "
  1038. << (cmSystemTools::GetTime() - sttime) << std::endl, this->Quiet);
  1039. log = ostr.str();
  1040. if ( defects )
  1041. {
  1042. return false;
  1043. }
  1044. return true;
  1045. }
  1046. //----------------------------------------------------------------------
  1047. bool cmCTestMemCheckHandler::ProcessMemCheckBoundsCheckerOutput(
  1048. const std::string& str, std::string& log,
  1049. std::vector<int>& results)
  1050. {
  1051. log = "";
  1052. double sttime = cmSystemTools::GetTime();
  1053. std::vector<std::string> lines;
  1054. cmSystemTools::Split(str.c_str(), lines);
  1055. cmCTestOptionalLog(this->CTest, DEBUG,
  1056. "Start test: " << lines.size() << std::endl, this->Quiet);
  1057. std::vector<std::string>::size_type cc;
  1058. for ( cc = 0; cc < lines.size(); cc ++ )
  1059. {
  1060. if(lines[cc] == BOUNDS_CHECKER_MARKER)
  1061. {
  1062. break;
  1063. }
  1064. }
  1065. cmBoundsCheckerParser parser(this->CTest);
  1066. parser.InitializeParser();
  1067. if(cc < lines.size())
  1068. {
  1069. for(cc++; cc < lines.size(); ++cc)
  1070. {
  1071. std::string& theLine = lines[cc];
  1072. // check for command line arguments that are not escaped
  1073. // correctly by BC
  1074. if(theLine.find("TargetArgs=") != theLine.npos)
  1075. {
  1076. // skip this because BC gets it wrong and we can't parse it
  1077. }
  1078. else if(!parser.ParseChunk(theLine.c_str(), theLine.size()))
  1079. {
  1080. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1081. "Error in ParseChunk: " << theLine
  1082. << std::endl);
  1083. }
  1084. }
  1085. }
  1086. int defects = 0;
  1087. for(cc =0; cc < parser.Errors.size(); ++cc)
  1088. {
  1089. results[parser.Errors[cc]]++;
  1090. defects++;
  1091. }
  1092. cmCTestOptionalLog(this->CTest, DEBUG, "End test (elapsed: "
  1093. << (cmSystemTools::GetTime() - sttime) << std::endl, this->Quiet);
  1094. if(defects)
  1095. {
  1096. // only put the output of Bounds Checker if there were
  1097. // errors or leaks detected
  1098. log = parser.Log;
  1099. return false;
  1100. }
  1101. return true;
  1102. }
  1103. // PostProcessTest memcheck results
  1104. void
  1105. cmCTestMemCheckHandler::PostProcessTest(cmCTestTestResult& res,
  1106. int test)
  1107. {
  1108. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1109. "PostProcessTest memcheck results for : "
  1110. << res.Name << std::endl, this->Quiet);
  1111. if(this->MemoryTesterStyle
  1112. == cmCTestMemCheckHandler::BOUNDS_CHECKER)
  1113. {
  1114. this->PostProcessBoundsCheckerTest(res, test);
  1115. }
  1116. else
  1117. {
  1118. std::vector<std::string> files;
  1119. this->TestOutputFileNames(test, files);
  1120. for(std::vector<std::string>::iterator i = files.begin();
  1121. i != files.end(); ++i)
  1122. {
  1123. this->AppendMemTesterOutput(res, *i);
  1124. }
  1125. }
  1126. }
  1127. // This method puts the bounds checker output file into the output
  1128. // for the test
  1129. void
  1130. cmCTestMemCheckHandler::PostProcessBoundsCheckerTest(cmCTestTestResult& res,
  1131. int test)
  1132. {
  1133. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1134. "PostProcessBoundsCheckerTest for : "
  1135. << res.Name << std::endl, this->Quiet);
  1136. std::vector<std::string> files;
  1137. this->TestOutputFileNames(test, files);
  1138. if (files.empty())
  1139. {
  1140. return;
  1141. }
  1142. std::string ofile = files[0];
  1143. if ( ofile.empty() )
  1144. {
  1145. return;
  1146. }
  1147. // put a scope around this to close ifs so the file can be removed
  1148. {
  1149. cmsys::ifstream ifs(ofile.c_str());
  1150. if ( !ifs )
  1151. {
  1152. std::string log = "Cannot read memory tester output file: " + ofile;
  1153. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1154. return;
  1155. }
  1156. res.Output += BOUNDS_CHECKER_MARKER;
  1157. res.Output += "\n";
  1158. std::string line;
  1159. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1160. {
  1161. res.Output += line;
  1162. res.Output += "\n";
  1163. }
  1164. }
  1165. cmSystemTools::Delay(1000);
  1166. cmSystemTools::RemoveFile(this->BoundsCheckerDPBDFile);
  1167. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  1168. << this->BoundsCheckerDPBDFile << std::endl, this->Quiet);
  1169. cmSystemTools::RemoveFile(this->BoundsCheckerXMLFile);
  1170. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  1171. << this->BoundsCheckerXMLFile << std::endl, this->Quiet);
  1172. }
  1173. void
  1174. cmCTestMemCheckHandler::AppendMemTesterOutput(cmCTestTestResult& res,
  1175. std::string const& ofile)
  1176. {
  1177. if ( ofile.empty() )
  1178. {
  1179. return;
  1180. }
  1181. // put ifs in scope so file can be deleted if needed
  1182. {
  1183. cmsys::ifstream ifs(ofile.c_str());
  1184. if ( !ifs )
  1185. {
  1186. std::string log = "Cannot read memory tester output file: " + ofile;
  1187. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1188. return;
  1189. }
  1190. std::string line;
  1191. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1192. {
  1193. res.Output += line;
  1194. res.Output += "\n";
  1195. }
  1196. }
  1197. if(this->LogWithPID)
  1198. {
  1199. cmSystemTools::RemoveFile(ofile);
  1200. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1201. "Remove: "<< ofile <<"\n", this->Quiet);
  1202. }
  1203. }
  1204. void cmCTestMemCheckHandler::TestOutputFileNames(int test,
  1205. std::vector<std::string>&
  1206. files)
  1207. {
  1208. std::string index;
  1209. std::ostringstream stream;
  1210. stream << test;
  1211. index = stream.str();
  1212. std::string ofile = this->MemoryTesterOutputFile;
  1213. std::string::size_type pos = ofile.find("??");
  1214. ofile.replace(pos, 2, index);
  1215. if(this->LogWithPID)
  1216. {
  1217. ofile += ".*";
  1218. cmsys::Glob g;
  1219. g.FindFiles(ofile);
  1220. if(g.GetFiles().empty())
  1221. {
  1222. std::string log = "Cannot find memory tester output file: "
  1223. + ofile;
  1224. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1225. ofile = "";
  1226. }
  1227. else
  1228. {
  1229. files = g.GetFiles();
  1230. return;
  1231. }
  1232. }
  1233. else if ( !cmSystemTools::FileExists(ofile.c_str()) )
  1234. {
  1235. std::string log = "Cannot find memory tester output file: "
  1236. + ofile;
  1237. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1238. ofile = "";
  1239. }
  1240. files.push_back(ofile);
  1241. }