cmCTestMemCheckHandler.cxx 39 KB

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