cmCTestMemCheckHandler.cxx 40 KB

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