cmCTestMemCheckHandler.cxx 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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 "cmXMLSafe.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. cmOStringStream ostr;
  70. ostr << name << ":\n";
  71. int i = 0;
  72. for(; atts[i] != 0; i+=2)
  73. {
  74. ostr << " " << cmXMLSafe(atts[i])
  75. << " - " << cmXMLSafe(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. cmOStringStream 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.size())
  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.size())
  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. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Memory check command: "
  236. << memcheckcommand << std::endl);
  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. }
  329. //----------------------------------------------------------------------
  330. void cmCTestMemCheckHandler::GenerateDartOutput(std::ostream& os)
  331. {
  332. if ( !this->CTest->GetProduceXML() )
  333. {
  334. return;
  335. }
  336. this->CTest->StartXML(os, this->AppendXML);
  337. os << "<DynamicAnalysis Checker=\"";
  338. switch ( this->MemoryTesterStyle )
  339. {
  340. case cmCTestMemCheckHandler::VALGRIND:
  341. os << "Valgrind";
  342. break;
  343. case cmCTestMemCheckHandler::PURIFY:
  344. os << "Purify";
  345. break;
  346. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  347. os << "BoundsChecker";
  348. break;
  349. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  350. os << "AddressSanitizer";
  351. break;
  352. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  353. os << "ThreadSanitizer";
  354. break;
  355. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  356. os << "MemorySanitizer";
  357. break;
  358. case cmCTestMemCheckHandler::UB_SANITIZER:
  359. os << "UndefinedBehaviorSanitizer";
  360. break;
  361. default:
  362. os << "Unknown";
  363. }
  364. os << "\">" << std::endl;
  365. os << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
  366. << "\t<StartTestTime>" << this->StartTestTime << "</StartTestTime>\n"
  367. << "\t<TestList>\n";
  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. os << "\t\t<Test>" << cmXMLSafe(
  374. this->CTest->GetShortPathToFile(testPath.c_str()))
  375. << "</Test>" << std::endl;
  376. }
  377. os << "\t</TestList>\n";
  378. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  379. "-- Processing memory checking output: ");
  380. size_t total = this->TestResults.size();
  381. size_t step = total / 10;
  382. size_t current = 0;
  383. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  384. {
  385. cmCTestTestResult *result = &this->TestResults[cc];
  386. std::string memcheckstr;
  387. std::vector<int> memcheckresults(this->ResultStrings.size(), 0);
  388. bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
  389. memcheckresults);
  390. if ( res && result->Status == cmCTestMemCheckHandler::COMPLETED )
  391. {
  392. continue;
  393. }
  394. this->CleanTestOutput(memcheckstr,
  395. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize));
  396. this->WriteTestResultHeader(os, result);
  397. os << "\t\t<Results>" << std::endl;
  398. for(std::vector<int>::size_type kk = 0;
  399. kk < memcheckresults.size(); ++kk)
  400. {
  401. if ( memcheckresults[kk] )
  402. {
  403. os << "\t\t\t<Defect type=\"" << this->ResultStringsLong[kk]
  404. << "\">"
  405. << memcheckresults[kk]
  406. << "</Defect>" << std::endl;
  407. }
  408. this->GlobalResults[kk] += memcheckresults[kk];
  409. }
  410. std::string logTag;
  411. if(this->CTest->ShouldCompressMemCheckOutput())
  412. {
  413. this->CTest->CompressString(memcheckstr);
  414. logTag = "\t<Log compression=\"gzip\" encoding=\"base64\">\n";
  415. }
  416. else
  417. {
  418. logTag = "\t<Log>\n";
  419. }
  420. os
  421. << "\t\t</Results>\n"
  422. << logTag << cmXMLSafe(memcheckstr) << std::endl
  423. << "\t</Log>\n";
  424. this->WriteTestResultFooter(os, result);
  425. if ( current < cc )
  426. {
  427. cmCTestLog(this->CTest, HANDLER_OUTPUT, "#" << std::flush);
  428. current += step;
  429. }
  430. }
  431. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  432. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory checking results:"
  433. << std::endl);
  434. os << "\t<DefectList>" << std::endl;
  435. for ( cc = 0; cc < this->GlobalResults.size(); cc ++ )
  436. {
  437. if ( this->GlobalResults[cc] )
  438. {
  439. #ifdef cerr
  440. # undef cerr
  441. #endif
  442. std::cerr.width(35);
  443. #define cerr no_cerr
  444. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  445. this->ResultStringsLong[cc] << " - "
  446. << this->GlobalResults[cc] << std::endl);
  447. os << "\t\t<Defect Type=\"" << this->ResultStringsLong[cc]
  448. << "\"/>" << std::endl;
  449. }
  450. }
  451. os << "\t</DefectList>" << std::endl;
  452. os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>" << std::endl;
  453. os << "\t<EndTestTime>" << this->EndTestTime
  454. << "</EndTestTime>" << std::endl;
  455. os << "<ElapsedMinutes>"
  456. << static_cast<int>(this->ElapsedTestingTime/6)/10.0
  457. << "</ElapsedMinutes>\n";
  458. os << "</DynamicAnalysis>" << std::endl;
  459. this->CTest->EndXML(os);
  460. }
  461. //----------------------------------------------------------------------
  462. bool cmCTestMemCheckHandler::InitializeMemoryChecking()
  463. {
  464. this->MemoryTesterEnvironmentVariable = "";
  465. this->MemoryTester = "";
  466. // Setup the command
  467. if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  468. "MemoryCheckCommand").c_str()) )
  469. {
  470. this->MemoryTester
  471. = this->CTest->GetCTestConfiguration("MemoryCheckCommand").c_str();
  472. std::string testerName =
  473. cmSystemTools::GetFilenameName(this->MemoryTester);
  474. // determine the checker type
  475. if ( testerName.find("valgrind") != std::string::npos ||
  476. this->CTest->GetCTestConfiguration("MemoryCheckType")
  477. == "Valgrind")
  478. {
  479. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  480. }
  481. else if ( testerName.find("purify") != std::string::npos )
  482. {
  483. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  484. }
  485. else if ( testerName.find("BC") != std::string::npos )
  486. {
  487. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  488. }
  489. else
  490. {
  491. this->MemoryTesterStyle = cmCTestMemCheckHandler::UNKNOWN;
  492. }
  493. }
  494. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  495. "PurifyCommand").c_str()) )
  496. {
  497. this->MemoryTester
  498. = this->CTest->GetCTestConfiguration("PurifyCommand").c_str();
  499. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  500. }
  501. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  502. "ValgrindCommand").c_str()) )
  503. {
  504. this->MemoryTester
  505. = this->CTest->GetCTestConfiguration("ValgrindCommand").c_str();
  506. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  507. }
  508. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  509. "BoundsCheckerCommand").c_str()) )
  510. {
  511. this->MemoryTester
  512. = this->CTest->GetCTestConfiguration("BoundsCheckerCommand").c_str();
  513. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  514. }
  515. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  516. == "AddressSanitizer")
  517. {
  518. this->MemoryTester
  519. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  520. this->MemoryTesterStyle = cmCTestMemCheckHandler::ADDRESS_SANITIZER;
  521. this->LogWithPID = true; // even if we give the log file the pid is added
  522. }
  523. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  524. == "ThreadSanitizer")
  525. {
  526. this->MemoryTester
  527. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  528. this->MemoryTesterStyle = cmCTestMemCheckHandler::THREAD_SANITIZER;
  529. this->LogWithPID = true; // even if we give the log file the pid is added
  530. }
  531. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  532. == "MemorySanitizer")
  533. {
  534. this->MemoryTester
  535. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  536. this->MemoryTesterStyle = cmCTestMemCheckHandler::MEMORY_SANITIZER;
  537. this->LogWithPID = true; // even if we give the log file the pid is added
  538. }
  539. if ( this->CTest->GetCTestConfiguration("MemoryCheckType")
  540. == "UndefinedBehaviorSanitizer")
  541. {
  542. this->MemoryTester
  543. = this->CTest->GetCTestConfiguration("CMakeCommand").c_str();
  544. this->MemoryTesterStyle = cmCTestMemCheckHandler::UB_SANITIZER;
  545. this->LogWithPID = true; // even if we give the log file the pid is added
  546. }
  547. // Check the MemoryCheckType
  548. if(this->MemoryTesterStyle == cmCTestMemCheckHandler::UNKNOWN)
  549. {
  550. std::string checkType =
  551. this->CTest->GetCTestConfiguration("MemoryCheckType");
  552. if(checkType == "Purify")
  553. {
  554. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  555. }
  556. else if(checkType == "BoundsChecker")
  557. {
  558. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  559. }
  560. else if(checkType == "Valgrind")
  561. {
  562. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  563. }
  564. }
  565. if(this->MemoryTester.size() == 0 )
  566. {
  567. cmCTestLog(this->CTest, WARNING,
  568. "Memory checker (MemoryCheckCommand) "
  569. "not set, or cannot find the specified program."
  570. << std::endl);
  571. return false;
  572. }
  573. // Setup the options
  574. std::string memoryTesterOptions;
  575. if ( this->CTest->GetCTestConfiguration(
  576. "MemoryCheckCommandOptions").size() )
  577. {
  578. memoryTesterOptions = this->CTest->GetCTestConfiguration(
  579. "MemoryCheckCommandOptions");
  580. }
  581. else if ( this->CTest->GetCTestConfiguration(
  582. "ValgrindCommandOptions").size() )
  583. {
  584. memoryTesterOptions = this->CTest->GetCTestConfiguration(
  585. "ValgrindCommandOptions");
  586. }
  587. this->MemoryTesterOptions
  588. = cmSystemTools::ParseArguments(memoryTesterOptions.c_str());
  589. this->MemoryTesterOutputFile
  590. = this->CTest->GetBinaryDir()
  591. + "/Testing/Temporary/MemoryChecker.??.log";
  592. switch ( this->MemoryTesterStyle )
  593. {
  594. case cmCTestMemCheckHandler::VALGRIND:
  595. {
  596. if ( this->MemoryTesterOptions.empty() )
  597. {
  598. this->MemoryTesterOptions.push_back("-q");
  599. this->MemoryTesterOptions.push_back("--tool=memcheck");
  600. this->MemoryTesterOptions.push_back("--leak-check=yes");
  601. this->MemoryTesterOptions.push_back("--show-reachable=yes");
  602. this->MemoryTesterOptions.push_back("--num-callers=50");
  603. }
  604. if ( this->CTest->GetCTestConfiguration(
  605. "MemoryCheckSuppressionFile").size() )
  606. {
  607. if ( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  608. "MemoryCheckSuppressionFile").c_str()) )
  609. {
  610. cmCTestLog(this->CTest, ERROR_MESSAGE,
  611. "Cannot find memory checker suppression file: "
  612. << this->CTest->GetCTestConfiguration(
  613. "MemoryCheckSuppressionFile") << std::endl);
  614. return false;
  615. }
  616. std::string suppressions = "--suppressions="
  617. + this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  618. this->MemoryTesterOptions.push_back(suppressions);
  619. }
  620. std::string outputFile = "--log-file="
  621. + this->MemoryTesterOutputFile;
  622. this->MemoryTesterDynamicOptions.push_back(outputFile);
  623. break;
  624. }
  625. case cmCTestMemCheckHandler::PURIFY:
  626. {
  627. std::string outputFile;
  628. #ifdef _WIN32
  629. if( this->CTest->GetCTestConfiguration(
  630. "MemoryCheckSuppressionFile").size() )
  631. {
  632. if( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  633. "MemoryCheckSuppressionFile").c_str()) )
  634. {
  635. cmCTestLog(this->CTest, ERROR_MESSAGE,
  636. "Cannot find memory checker suppression file: "
  637. << this->CTest->GetCTestConfiguration(
  638. "MemoryCheckSuppressionFile").c_str() << std::endl);
  639. return false;
  640. }
  641. std::string filterFiles = "/FilterFiles="
  642. + this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  643. this->MemoryTesterOptions.push_back(filterFiles);
  644. }
  645. outputFile = "/SAVETEXTDATA=";
  646. #else
  647. outputFile = "-log-file=";
  648. #endif
  649. outputFile += this->MemoryTesterOutputFile;
  650. this->MemoryTesterDynamicOptions.push_back(outputFile);
  651. break;
  652. }
  653. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  654. {
  655. this->BoundsCheckerXMLFile = this->MemoryTesterOutputFile;
  656. std::string dpbdFile = this->CTest->GetBinaryDir()
  657. + "/Testing/Temporary/MemoryChecker.??.DPbd";
  658. this->BoundsCheckerDPBDFile = dpbdFile;
  659. this->MemoryTesterDynamicOptions.push_back("/B");
  660. this->MemoryTesterDynamicOptions.push_back(dpbdFile);
  661. this->MemoryTesterDynamicOptions.push_back("/X");
  662. this->MemoryTesterDynamicOptions.push_back(this->MemoryTesterOutputFile);
  663. this->MemoryTesterOptions.push_back("/M");
  664. break;
  665. }
  666. // these are almost the same but the env var used is different
  667. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  668. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  669. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  670. case cmCTestMemCheckHandler::UB_SANITIZER:
  671. {
  672. // To pass arguments to ThreadSanitizer the environment variable
  673. // TSAN_OPTIONS is used. This is done with the cmake -E env command.
  674. // The MemoryTesterDynamicOptions is setup with the -E env
  675. // Then the MemoryTesterEnvironmentVariable gets the
  676. // TSAN_OPTIONS string with the log_path in it.
  677. this->MemoryTesterDynamicOptions.push_back("-E");
  678. this->MemoryTesterDynamicOptions.push_back("env");
  679. std::string envVar;
  680. std::string extraOptions =
  681. this->CTest->GetCTestConfiguration("MemoryCheckSanitizerOptions");
  682. if(this->MemoryTesterStyle == cmCTestMemCheckHandler::ADDRESS_SANITIZER)
  683. {
  684. envVar = "ASAN_OPTIONS";
  685. extraOptions += " detect_leaks=1";
  686. }
  687. else if(this->MemoryTesterStyle ==
  688. cmCTestMemCheckHandler::THREAD_SANITIZER)
  689. {
  690. envVar = "TSAN_OPTIONS";
  691. }
  692. else if(this->MemoryTesterStyle ==
  693. cmCTestMemCheckHandler::MEMORY_SANITIZER)
  694. {
  695. envVar = "MSAN_OPTIONS";
  696. }
  697. else if(this->MemoryTesterStyle == cmCTestMemCheckHandler::UB_SANITIZER)
  698. {
  699. envVar = "UBSAN_OPTIONS";
  700. }
  701. std::string outputFile = envVar + "=log_path=\""
  702. + this->MemoryTesterOutputFile + "\" ";
  703. this->MemoryTesterEnvironmentVariable = outputFile + extraOptions;
  704. break;
  705. }
  706. default:
  707. cmCTestLog(this->CTest, ERROR_MESSAGE,
  708. "Do not understand memory checker: " << this->MemoryTester
  709. << std::endl);
  710. return false;
  711. }
  712. this->InitializeResultsVectors();
  713. // std::vector<std::string>::size_type cc;
  714. // for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  715. // {
  716. // this->MemoryTesterGlobalResults[cc] = 0;
  717. // }
  718. return true;
  719. }
  720. //----------------------------------------------------------------------
  721. bool cmCTestMemCheckHandler::
  722. ProcessMemCheckOutput(const std::string& str,
  723. std::string& log, std::vector<int>& results)
  724. {
  725. if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::VALGRIND )
  726. {
  727. return this->ProcessMemCheckValgrindOutput(str, log, results);
  728. }
  729. else if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
  730. {
  731. return this->ProcessMemCheckPurifyOutput(str, log, results);
  732. }
  733. else if ( this->MemoryTesterStyle ==
  734. cmCTestMemCheckHandler::ADDRESS_SANITIZER ||
  735. this->MemoryTesterStyle ==
  736. cmCTestMemCheckHandler::THREAD_SANITIZER ||
  737. this->MemoryTesterStyle ==
  738. cmCTestMemCheckHandler::MEMORY_SANITIZER ||
  739. this->MemoryTesterStyle ==
  740. cmCTestMemCheckHandler::UB_SANITIZER)
  741. {
  742. return this->ProcessMemCheckSanitizerOutput(str, log, results);
  743. }
  744. else if ( this->MemoryTesterStyle ==
  745. cmCTestMemCheckHandler::BOUNDS_CHECKER )
  746. {
  747. return this->ProcessMemCheckBoundsCheckerOutput(str, log, results);
  748. }
  749. else
  750. {
  751. log.append("\nMemory checking style used was: ");
  752. log.append("None that I know");
  753. log = str;
  754. }
  755. return true;
  756. }
  757. std::vector<int>::size_type cmCTestMemCheckHandler::FindOrAddWarning(
  758. const std::string& warning)
  759. {
  760. for(std::vector<std::string>::size_type i =0;
  761. i < this->ResultStrings.size(); ++i)
  762. {
  763. if(this->ResultStrings[i] == warning)
  764. {
  765. return i;
  766. }
  767. }
  768. this->GlobalResults.push_back(0); // this must stay the same size
  769. this->ResultStrings.push_back(warning);
  770. this->ResultStringsLong.push_back(warning);
  771. return this->ResultStrings.size()-1;
  772. }
  773. //----------------------------------------------------------------------
  774. bool cmCTestMemCheckHandler::ProcessMemCheckSanitizerOutput(
  775. const std::string& str, std::string& log,
  776. std::vector<int>& result)
  777. {
  778. std::string regex;
  779. switch ( this->MemoryTesterStyle )
  780. {
  781. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  782. regex = "ERROR: AddressSanitizer: (.*) on.*";
  783. break;
  784. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  785. regex = "WARNING: ThreadSanitizer: (.*) \\(pid=.*\\)";
  786. break;
  787. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  788. regex = "WARNING: MemorySanitizer: (.*)";
  789. break;
  790. case cmCTestMemCheckHandler::UB_SANITIZER:
  791. regex = "runtime error: (.*)";
  792. break;
  793. default:
  794. break;
  795. }
  796. cmsys::RegularExpression sanitizerWarning(regex);
  797. cmsys::RegularExpression leakWarning("(Direct|Indirect) leak of .*");
  798. int defects = 0;
  799. std::vector<std::string> lines;
  800. cmSystemTools::Split(str.c_str(), lines);
  801. cmOStringStream ostr;
  802. log = "";
  803. for( std::vector<std::string>::iterator i = lines.begin();
  804. i != lines.end(); ++i)
  805. {
  806. std::string resultFound;
  807. if(leakWarning.find(*i))
  808. {
  809. resultFound = leakWarning.match(1)+" leak";
  810. }
  811. else if (sanitizerWarning.find(*i))
  812. {
  813. resultFound = sanitizerWarning.match(1);
  814. }
  815. if(resultFound.size())
  816. {
  817. std::vector<int>::size_type idx = this->FindOrAddWarning(resultFound);
  818. if(result.size() == 0 || idx > result.size()-1)
  819. {
  820. result.push_back(1);
  821. }
  822. else
  823. {
  824. result[idx]++;
  825. }
  826. defects++;
  827. ostr << "<b>" << this->ResultStrings[idx] << "</b> ";
  828. }
  829. ostr << cmXMLSafe(*i) << std::endl;
  830. }
  831. log = ostr.str();
  832. if(defects)
  833. {
  834. return false;
  835. }
  836. return true;
  837. }
  838. //----------------------------------------------------------------------
  839. bool cmCTestMemCheckHandler::ProcessMemCheckPurifyOutput(
  840. const std::string& str, std::string& log,
  841. std::vector<int>& results)
  842. {
  843. std::vector<std::string> lines;
  844. cmSystemTools::Split(str.c_str(), lines);
  845. cmOStringStream ostr;
  846. log = "";
  847. cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
  848. int defects = 0;
  849. for( std::vector<std::string>::iterator i = lines.begin();
  850. i != lines.end(); ++i)
  851. {
  852. std::vector<int>::size_type failure = this->ResultStrings.size();
  853. if ( pfW.find(*i) )
  854. {
  855. std::vector<int>::size_type cc;
  856. for ( cc = 0; cc < this->ResultStrings.size(); cc ++ )
  857. {
  858. if ( pfW.match(1) == this->ResultStrings[cc] )
  859. {
  860. failure = cc;
  861. break;
  862. }
  863. }
  864. if ( cc == this->ResultStrings.size() )
  865. {
  866. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
  867. << pfW.match(1) << std::endl);
  868. ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
  869. << std::endl;
  870. }
  871. }
  872. if ( failure != this->ResultStrings.size() )
  873. {
  874. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  875. results[failure] ++;
  876. defects ++;
  877. }
  878. ostr << cmXMLSafe(*i) << std::endl;
  879. }
  880. log = ostr.str();
  881. if ( defects )
  882. {
  883. return false;
  884. }
  885. return true;
  886. }
  887. //----------------------------------------------------------------------
  888. bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
  889. const std::string& str, std::string& log,
  890. std::vector<int>& results)
  891. {
  892. std::vector<std::string> lines;
  893. cmSystemTools::Split(str.c_str(), lines);
  894. bool unlimitedOutput = false;
  895. if(str.find("CTEST_FULL_OUTPUT") != str.npos ||
  896. this->CustomMaximumFailedTestOutputSize == 0)
  897. {
  898. unlimitedOutput = true;
  899. }
  900. std::string::size_type cc;
  901. cmOStringStream ostr;
  902. log = "";
  903. int defects = 0;
  904. cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
  905. cmsys::RegularExpression vgFIM(
  906. "== .*Invalid free\\(\\) / delete / delete\\[\\]");
  907. cmsys::RegularExpression vgFMM(
  908. "== .*Mismatched free\\(\\) / delete / delete \\[\\]");
  909. cmsys::RegularExpression vgMLK1(
  910. "== .*[0-9,]+ bytes in [0-9,]+ blocks are definitely lost"
  911. " in loss record [0-9,]+ of [0-9,]+");
  912. cmsys::RegularExpression vgMLK2(
  913. "== .*[0-9,]+ \\([0-9,]+ direct, [0-9,]+ indirect\\)"
  914. " bytes in [0-9,]+ blocks are definitely lost"
  915. " in loss record [0-9,]+ of [0-9,]+");
  916. cmsys::RegularExpression vgPAR(
  917. "== .*Syscall param .* (contains|points to) unaddressable byte\\(s\\)");
  918. cmsys::RegularExpression vgMPK1(
  919. "== .*[0-9,]+ bytes in [0-9,]+ blocks are possibly lost in"
  920. " loss record [0-9,]+ of [0-9,]+");
  921. cmsys::RegularExpression vgMPK2(
  922. "== .*[0-9,]+ bytes in [0-9,]+ blocks are still reachable"
  923. " in loss record [0-9,]+ of [0-9,]+");
  924. cmsys::RegularExpression vgUMC(
  925. "== .*Conditional jump or move depends on uninitialised value\\(s\\)");
  926. cmsys::RegularExpression vgUMR1(
  927. "== .*Use of uninitialised value of size [0-9,]+");
  928. cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9,]+");
  929. cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
  930. cmsys::RegularExpression vgUMR4("== .*Syscall param .* contains "
  931. "uninitialised or unaddressable byte\\(s\\)");
  932. cmsys::RegularExpression vgUMR5("== .*Syscall param .* uninitialised");
  933. cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9,]+");
  934. cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
  935. "locked by a different thread");
  936. std::vector<std::string::size_type> nonValGrindOutput;
  937. double sttime = cmSystemTools::GetTime();
  938. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  939. std::string::size_type totalOutputSize = 0;
  940. for ( cc = 0; cc < lines.size(); cc ++ )
  941. {
  942. cmCTestLog(this->CTest, DEBUG, "test line "
  943. << lines[cc] << std::endl);
  944. if ( valgrindLine.find(lines[cc]) )
  945. {
  946. cmCTestLog(this->CTest, DEBUG, "valgrind line "
  947. << lines[cc] << std::endl);
  948. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  949. if ( vgFIM.find(lines[cc]) )
  950. {
  951. failure = cmCTestMemCheckHandler::FIM;
  952. }
  953. else if ( vgFMM.find(lines[cc]) )
  954. {
  955. failure = cmCTestMemCheckHandler::FMM;
  956. }
  957. else if ( vgMLK1.find(lines[cc]) )
  958. {
  959. failure = cmCTestMemCheckHandler::MLK;
  960. }
  961. else if ( vgMLK2.find(lines[cc]) )
  962. {
  963. failure = cmCTestMemCheckHandler::MLK;
  964. }
  965. else if ( vgPAR.find(lines[cc]) )
  966. {
  967. failure = cmCTestMemCheckHandler::PAR;
  968. }
  969. else if ( vgMPK1.find(lines[cc]) )
  970. {
  971. failure = cmCTestMemCheckHandler::MPK;
  972. }
  973. else if ( vgMPK2.find(lines[cc]) )
  974. {
  975. failure = cmCTestMemCheckHandler::MPK;
  976. }
  977. else if ( vgUMC.find(lines[cc]) )
  978. {
  979. failure = cmCTestMemCheckHandler::UMC;
  980. }
  981. else if ( vgUMR1.find(lines[cc]) )
  982. {
  983. failure = cmCTestMemCheckHandler::UMR;
  984. }
  985. else if ( vgUMR2.find(lines[cc]) )
  986. {
  987. failure = cmCTestMemCheckHandler::UMR;
  988. }
  989. else if ( vgUMR3.find(lines[cc]) )
  990. {
  991. failure = cmCTestMemCheckHandler::UMR;
  992. }
  993. else if ( vgUMR4.find(lines[cc]) )
  994. {
  995. failure = cmCTestMemCheckHandler::UMR;
  996. }
  997. else if ( vgUMR5.find(lines[cc]) )
  998. {
  999. failure = cmCTestMemCheckHandler::UMR;
  1000. }
  1001. else if ( vgIPW.find(lines[cc]) )
  1002. {
  1003. failure = cmCTestMemCheckHandler::IPW;
  1004. }
  1005. else if ( vgABR.find(lines[cc]) )
  1006. {
  1007. failure = cmCTestMemCheckHandler::ABR;
  1008. }
  1009. if ( failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  1010. {
  1011. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  1012. results[failure] ++;
  1013. defects ++;
  1014. }
  1015. totalOutputSize += lines[cc].size();
  1016. ostr << cmXMLSafe(lines[cc]) << std::endl;
  1017. }
  1018. else
  1019. {
  1020. nonValGrindOutput.push_back(cc);
  1021. }
  1022. }
  1023. // Now put all all the non valgrind output into the test output
  1024. // This should be last in case it gets truncated by the output
  1025. // limiting code
  1026. for(std::vector<std::string::size_type>::iterator i =
  1027. nonValGrindOutput.begin(); i != nonValGrindOutput.end(); ++i)
  1028. {
  1029. totalOutputSize += lines[*i].size();
  1030. cmCTestLog(this->CTest, DEBUG, "before xml safe "
  1031. << lines[*i] << std::endl);
  1032. cmCTestLog(this->CTest, DEBUG, "after xml safe "
  1033. << cmXMLSafe(lines[*i]) << std::endl);
  1034. ostr << cmXMLSafe(lines[*i]) << std::endl;
  1035. if(!unlimitedOutput && totalOutputSize >
  1036. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize))
  1037. {
  1038. ostr << "....\n";
  1039. ostr << "Test Output for this test has been truncated see testing"
  1040. " machine logs for full output,\n";
  1041. ostr << "or put CTEST_FULL_OUTPUT in the output of "
  1042. "this test program.\n";
  1043. break; // stop the copy of output if we are full
  1044. }
  1045. }
  1046. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  1047. << (cmSystemTools::GetTime() - sttime) << std::endl);
  1048. log = ostr.str();
  1049. if ( defects )
  1050. {
  1051. return false;
  1052. }
  1053. return true;
  1054. }
  1055. //----------------------------------------------------------------------
  1056. bool cmCTestMemCheckHandler::ProcessMemCheckBoundsCheckerOutput(
  1057. const std::string& str, std::string& log,
  1058. std::vector<int>& results)
  1059. {
  1060. log = "";
  1061. double sttime = cmSystemTools::GetTime();
  1062. std::vector<std::string> lines;
  1063. cmSystemTools::Split(str.c_str(), lines);
  1064. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  1065. std::vector<std::string>::size_type cc;
  1066. for ( cc = 0; cc < lines.size(); cc ++ )
  1067. {
  1068. if(lines[cc] == BOUNDS_CHECKER_MARKER)
  1069. {
  1070. break;
  1071. }
  1072. }
  1073. cmBoundsCheckerParser parser(this->CTest);
  1074. parser.InitializeParser();
  1075. if(cc < lines.size())
  1076. {
  1077. for(cc++; cc < lines.size(); ++cc)
  1078. {
  1079. std::string& theLine = lines[cc];
  1080. // check for command line arguments that are not escaped
  1081. // correctly by BC
  1082. if(theLine.find("TargetArgs=") != theLine.npos)
  1083. {
  1084. // skip this because BC gets it wrong and we can't parse it
  1085. }
  1086. else if(!parser.ParseChunk(theLine.c_str(), theLine.size()))
  1087. {
  1088. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1089. "Error in ParseChunk: " << theLine
  1090. << std::endl);
  1091. }
  1092. }
  1093. }
  1094. int defects = 0;
  1095. for(cc =0; cc < parser.Errors.size(); ++cc)
  1096. {
  1097. results[parser.Errors[cc]]++;
  1098. defects++;
  1099. }
  1100. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  1101. << (cmSystemTools::GetTime() - sttime) << std::endl);
  1102. if(defects)
  1103. {
  1104. // only put the output of Bounds Checker if there were
  1105. // errors or leaks detected
  1106. log = parser.Log;
  1107. return false;
  1108. }
  1109. return true;
  1110. }
  1111. // PostProcessTest memcheck results
  1112. void
  1113. cmCTestMemCheckHandler::PostProcessTest(cmCTestTestResult& res,
  1114. int test)
  1115. {
  1116. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1117. "PostProcessTest memcheck results for : "
  1118. << res.Name << std::endl);
  1119. if(this->MemoryTesterStyle
  1120. == cmCTestMemCheckHandler::BOUNDS_CHECKER)
  1121. {
  1122. this->PostProcessBoundsCheckerTest(res, test);
  1123. }
  1124. else
  1125. {
  1126. std::vector<std::string> files;
  1127. this->TestOutputFileNames(test, files);
  1128. for(std::vector<std::string>::iterator i = files.begin();
  1129. i != files.end(); ++i)
  1130. {
  1131. this->AppendMemTesterOutput(res, *i);
  1132. }
  1133. }
  1134. }
  1135. // This method puts the bounds checker output file into the output
  1136. // for the test
  1137. void
  1138. cmCTestMemCheckHandler::PostProcessBoundsCheckerTest(cmCTestTestResult& res,
  1139. int test)
  1140. {
  1141. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1142. "PostProcessBoundsCheckerTest for : "
  1143. << res.Name << std::endl);
  1144. std::vector<std::string> files;
  1145. this->TestOutputFileNames(test, files);
  1146. if ( files.size() == 0 )
  1147. {
  1148. return;
  1149. }
  1150. std::string ofile = files[0];
  1151. // put a scope around this to close ifs so the file can be removed
  1152. {
  1153. cmsys::ifstream ifs(ofile.c_str());
  1154. if ( !ifs )
  1155. {
  1156. std::string log = "Cannot read memory tester output file: " + ofile;
  1157. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1158. return;
  1159. }
  1160. res.Output += BOUNDS_CHECKER_MARKER;
  1161. res.Output += "\n";
  1162. std::string line;
  1163. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1164. {
  1165. res.Output += line;
  1166. res.Output += "\n";
  1167. }
  1168. }
  1169. cmSystemTools::Delay(1000);
  1170. cmSystemTools::RemoveFile(this->BoundsCheckerDPBDFile);
  1171. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  1172. << this->BoundsCheckerDPBDFile << std::endl);
  1173. cmSystemTools::RemoveFile(this->BoundsCheckerXMLFile);
  1174. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  1175. << this->BoundsCheckerXMLFile << std::endl);
  1176. }
  1177. void
  1178. cmCTestMemCheckHandler::AppendMemTesterOutput(cmCTestTestResult& res,
  1179. std::string const& ofile)
  1180. {
  1181. if ( ofile.empty() )
  1182. {
  1183. return;
  1184. }
  1185. // put ifs in scope so file can be deleted if needed
  1186. {
  1187. cmsys::ifstream ifs(ofile.c_str());
  1188. if ( !ifs )
  1189. {
  1190. std::string log = "Cannot read memory tester output file: " + ofile;
  1191. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1192. return;
  1193. }
  1194. std::string line;
  1195. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1196. {
  1197. res.Output += line;
  1198. res.Output += "\n";
  1199. }
  1200. }
  1201. if(this->LogWithPID)
  1202. {
  1203. cmSystemTools::RemoveFile(ofile);
  1204. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "<< ofile <<"\n");
  1205. }
  1206. }
  1207. void cmCTestMemCheckHandler::TestOutputFileNames(int test,
  1208. std::vector<std::string>&
  1209. files)
  1210. {
  1211. std::string index;
  1212. cmOStringStream stream;
  1213. stream << test;
  1214. index = stream.str();
  1215. std::string ofile = this->MemoryTesterOutputFile;
  1216. std::string::size_type pos = ofile.find("??");
  1217. ofile.replace(pos, 2, index);
  1218. if(this->LogWithPID)
  1219. {
  1220. ofile += ".*";
  1221. cmsys::Glob g;
  1222. g.FindFiles(ofile);
  1223. if(g.GetFiles().size() == 0)
  1224. {
  1225. std::string log = "Cannot find memory tester output file: "
  1226. + ofile;
  1227. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1228. ofile = "";
  1229. }
  1230. else
  1231. {
  1232. files = g.GetFiles();
  1233. return;
  1234. }
  1235. }
  1236. else if ( !cmSystemTools::FileExists(ofile.c_str()) )
  1237. {
  1238. std::string log = "Cannot find memory tester output file: "
  1239. + ofile;
  1240. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  1241. ofile = "";
  1242. }
  1243. files.push_back(ofile);
  1244. }