cmCTestMemCheckHandler.cxx 40 KB

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