cmCTestMemCheckHandler.cxx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 <iostream>
  12. #include <sstream>
  13. #include <string.h>
  14. struct CatToErrorType
  15. {
  16. const char* ErrorCategory;
  17. int ErrorCode;
  18. };
  19. static CatToErrorType cmCTestMemCheckBoundsChecker[] = {
  20. // Error tags
  21. { "Write Overrun", cmCTestMemCheckHandler::ABW },
  22. { "Read Overrun", cmCTestMemCheckHandler::ABR },
  23. { "Memory Overrun", cmCTestMemCheckHandler::ABW },
  24. { "Allocation Conflict", cmCTestMemCheckHandler::FMM },
  25. { "Bad Pointer Use", cmCTestMemCheckHandler::FMW },
  26. { "Dangling Pointer", cmCTestMemCheckHandler::FMR },
  27. { nullptr, 0 }
  28. };
  29. static void xmlReportError(int line, const char* msg, void* data)
  30. {
  31. cmCTest* ctest = reinterpret_cast<cmCTest*>(data);
  32. cmCTestLog(ctest, ERROR_MESSAGE, "Error parsing XML in stream at line "
  33. << line << ": " << msg << std::endl);
  34. }
  35. // parse the xml file containing the results of last BoundsChecker run
  36. class cmBoundsCheckerParser : public cmXMLParser
  37. {
  38. public:
  39. cmBoundsCheckerParser(cmCTest* c)
  40. {
  41. this->CTest = c;
  42. this->SetErrorCallback(xmlReportError, c);
  43. }
  44. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  45. {
  46. if (name == "MemoryLeak" || name == "ResourceLeak") {
  47. this->Errors.push_back(cmCTestMemCheckHandler::MLK);
  48. } else if (name == "Error" || name == "Dangling Pointer") {
  49. this->ParseError(atts);
  50. }
  51. // Create the log
  52. std::ostringstream ostr;
  53. ostr << name << ":\n";
  54. int i = 0;
  55. for (; atts[i] != nullptr; i += 2) {
  56. ostr << " " << atts[i] << " - " << atts[i + 1] << "\n";
  57. }
  58. ostr << "\n";
  59. this->Log += ostr.str();
  60. }
  61. void EndElement(const std::string& /*name*/) CM_OVERRIDE {}
  62. const char* GetAttribute(const char* name, const char** atts)
  63. {
  64. int i = 0;
  65. for (; atts[i] != nullptr; ++i) {
  66. if (strcmp(name, atts[i]) == 0) {
  67. return atts[i + 1];
  68. }
  69. }
  70. return nullptr;
  71. }
  72. void ParseError(const char** atts)
  73. {
  74. CatToErrorType* ptr = cmCTestMemCheckBoundsChecker;
  75. const char* cat = this->GetAttribute("ErrorCategory", atts);
  76. if (!cat) {
  77. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  78. cmCTestLog(this->CTest, ERROR_MESSAGE,
  79. "No Category found in Bounds checker XML\n");
  80. return;
  81. }
  82. while (ptr->ErrorCategory && cat) {
  83. if (strcmp(ptr->ErrorCategory, cat) == 0) {
  84. this->Errors.push_back(ptr->ErrorCode);
  85. return; // found it we are done
  86. }
  87. ptr++;
  88. }
  89. if (ptr->ErrorCategory) {
  90. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  91. cmCTestLog(this->CTest, ERROR_MESSAGE,
  92. "Found unknown Bounds Checker error " << ptr->ErrorCategory
  93. << std::endl);
  94. }
  95. }
  96. cmCTest* CTest;
  97. std::vector<int> Errors;
  98. std::string Log;
  99. };
  100. #define BOUNDS_CHECKER_MARKER \
  101. "******######*****Begin BOUNDS CHECKER XML******######******"
  102. cmCTestMemCheckHandler::cmCTestMemCheckHandler()
  103. {
  104. this->MemCheck = true;
  105. this->CustomMaximumPassedTestOutputSize = 0;
  106. this->CustomMaximumFailedTestOutputSize = 0;
  107. this->LogWithPID = false;
  108. }
  109. void cmCTestMemCheckHandler::Initialize()
  110. {
  111. this->Superclass::Initialize();
  112. this->LogWithPID = false;
  113. this->CustomMaximumPassedTestOutputSize = 0;
  114. this->CustomMaximumFailedTestOutputSize = 0;
  115. this->MemoryTester = "";
  116. this->MemoryTesterDynamicOptions.clear();
  117. this->MemoryTesterOptions.clear();
  118. this->MemoryTesterStyle = UNKNOWN;
  119. this->MemoryTesterOutputFile = "";
  120. this->DefectCount = 0;
  121. }
  122. int cmCTestMemCheckHandler::PreProcessHandler()
  123. {
  124. if (!this->InitializeMemoryChecking()) {
  125. return 0;
  126. }
  127. if (!this->ExecuteCommands(this->CustomPreMemCheck)) {
  128. cmCTestLog(this->CTest, ERROR_MESSAGE,
  129. "Problem executing pre-memcheck command(s)." << std::endl);
  130. return 0;
  131. }
  132. return 1;
  133. }
  134. int cmCTestMemCheckHandler::PostProcessHandler()
  135. {
  136. if (!this->ExecuteCommands(this->CustomPostMemCheck)) {
  137. cmCTestLog(this->CTest, ERROR_MESSAGE,
  138. "Problem executing post-memcheck command(s)." << std::endl);
  139. return 0;
  140. }
  141. return 1;
  142. }
  143. void cmCTestMemCheckHandler::GenerateTestCommand(
  144. std::vector<std::string>& args, int test)
  145. {
  146. std::vector<std::string>::size_type pp;
  147. std::string index;
  148. std::ostringstream stream;
  149. std::string memcheckcommand =
  150. cmSystemTools::ConvertToOutputPath(this->MemoryTester.c_str());
  151. stream << test;
  152. index = stream.str();
  153. for (pp = 0; pp < this->MemoryTesterDynamicOptions.size(); pp++) {
  154. std::string arg = this->MemoryTesterDynamicOptions[pp];
  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 (pp = 0; pp < this->MemoryTesterOptions.size(); pp++) {
  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 += " " + this->MemoryTesterOptions[pp];
  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(this->MemoryTesterOptions[pp]);
  179. memcheckcommand += " \"";
  180. memcheckcommand += this->MemoryTesterOptions[pp];
  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 (cc = 0; cc < this->TestResults.size(); cc++) {
  309. cmCTestTestResult* result = &this->TestResults[cc];
  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* result = &this->TestResults[cc];
  319. std::string memcheckstr;
  320. std::vector<int> memcheckresults(this->ResultStrings.size(), 0);
  321. bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
  322. 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("ElapsedMinutes",
  392. static_cast<int>(this->ElapsedTestingTime / 6) / 10.0);
  393. xml.EndElement(); // DynamicAnalysis
  394. this->CTest->EndXML(xml);
  395. }
  396. bool cmCTestMemCheckHandler::InitializeMemoryChecking()
  397. {
  398. this->MemoryTesterEnvironmentVariable = "";
  399. this->MemoryTester = "";
  400. // Setup the command
  401. if (cmSystemTools::FileExists(
  402. this->CTest->GetCTestConfiguration("MemoryCheckCommand").c_str())) {
  403. this->MemoryTester =
  404. this->CTest->GetCTestConfiguration("MemoryCheckCommand");
  405. std::string testerName =
  406. cmSystemTools::GetFilenameName(this->MemoryTester);
  407. // determine the checker type
  408. if (testerName.find("valgrind") != std::string::npos ||
  409. this->CTest->GetCTestConfiguration("MemoryCheckType") == "Valgrind") {
  410. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  411. } else if (testerName.find("purify") != std::string::npos) {
  412. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  413. } else if (testerName.find("BC") != std::string::npos) {
  414. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  415. } else {
  416. this->MemoryTesterStyle = cmCTestMemCheckHandler::UNKNOWN;
  417. }
  418. } else if (cmSystemTools::FileExists(
  419. this->CTest->GetCTestConfiguration("PurifyCommand").c_str())) {
  420. this->MemoryTester = this->CTest->GetCTestConfiguration("PurifyCommand");
  421. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  422. } else if (cmSystemTools::FileExists(
  423. this->CTest->GetCTestConfiguration("ValgrindCommand")
  424. .c_str())) {
  425. this->MemoryTester = this->CTest->GetCTestConfiguration("ValgrindCommand");
  426. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  427. } else if (cmSystemTools::FileExists(
  428. this->CTest->GetCTestConfiguration("BoundsCheckerCommand")
  429. .c_str())) {
  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(
  511. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile")
  512. .c_str())) {
  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. std::string suppressions = "--suppressions=" +
  521. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  522. this->MemoryTesterOptions.push_back(suppressions);
  523. }
  524. std::string outputFile = "--log-file=" + this->MemoryTesterOutputFile;
  525. this->MemoryTesterDynamicOptions.push_back(outputFile);
  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(
  534. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile")
  535. .c_str())) {
  536. cmCTestLog(
  537. this->CTest, ERROR_MESSAGE,
  538. "Cannot find memory checker suppression file: "
  539. << this->CTest
  540. ->GetCTestConfiguration("MemoryCheckSuppressionFile")
  541. .c_str()
  542. << std::endl);
  543. return false;
  544. }
  545. std::string filterFiles = "/FilterFiles=" +
  546. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  547. this->MemoryTesterOptions.push_back(filterFiles);
  548. }
  549. outputFile = "/SAVETEXTDATA=";
  550. #else
  551. outputFile = "-log-file=";
  552. #endif
  553. outputFile += this->MemoryTesterOutputFile;
  554. this->MemoryTesterDynamicOptions.push_back(outputFile);
  555. break;
  556. }
  557. case cmCTestMemCheckHandler::BOUNDS_CHECKER: {
  558. this->BoundsCheckerXMLFile = this->MemoryTesterOutputFile;
  559. std::string dpbdFile = this->CTest->GetBinaryDir() +
  560. "/Testing/Temporary/MemoryChecker.??.DPbd";
  561. this->BoundsCheckerDPBDFile = dpbdFile;
  562. this->MemoryTesterDynamicOptions.push_back("/B");
  563. this->MemoryTesterDynamicOptions.push_back(dpbdFile);
  564. this->MemoryTesterDynamicOptions.push_back("/X");
  565. this->MemoryTesterDynamicOptions.push_back(this->MemoryTesterOutputFile);
  566. this->MemoryTesterOptions.push_back("/M");
  567. break;
  568. }
  569. // these are almost the same but the env var used is different
  570. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  571. case cmCTestMemCheckHandler::LEAK_SANITIZER:
  572. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  573. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  574. case cmCTestMemCheckHandler::UB_SANITIZER: {
  575. // To pass arguments to ThreadSanitizer the environment variable
  576. // TSAN_OPTIONS is used. This is done with the cmake -E env command.
  577. // The MemoryTesterDynamicOptions is setup with the -E env
  578. // Then the MemoryTesterEnvironmentVariable gets the
  579. // TSAN_OPTIONS string with the log_path in it.
  580. this->MemoryTesterDynamicOptions.push_back("-E");
  581. this->MemoryTesterDynamicOptions.push_back("env");
  582. std::string envVar;
  583. std::string extraOptions;
  584. std::string suppressionsOption;
  585. if (!this->CTest->GetCTestConfiguration("MemoryCheckSanitizerOptions")
  586. .empty()) {
  587. extraOptions = ":" +
  588. this->CTest->GetCTestConfiguration("MemoryCheckSanitizerOptions");
  589. }
  590. if (!this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile")
  591. .empty()) {
  592. suppressionsOption = ":suppressions=" +
  593. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  594. }
  595. if (this->MemoryTesterStyle ==
  596. cmCTestMemCheckHandler::ADDRESS_SANITIZER) {
  597. envVar = "ASAN_OPTIONS";
  598. } else if (this->MemoryTesterStyle ==
  599. cmCTestMemCheckHandler::LEAK_SANITIZER) {
  600. envVar = "LSAN_OPTIONS";
  601. } else if (this->MemoryTesterStyle ==
  602. cmCTestMemCheckHandler::THREAD_SANITIZER) {
  603. envVar = "TSAN_OPTIONS";
  604. } else if (this->MemoryTesterStyle ==
  605. cmCTestMemCheckHandler::MEMORY_SANITIZER) {
  606. envVar = "MSAN_OPTIONS";
  607. } else if (this->MemoryTesterStyle ==
  608. cmCTestMemCheckHandler::UB_SANITIZER) {
  609. envVar = "UBSAN_OPTIONS";
  610. }
  611. // Quote log_path with single quotes; see
  612. // https://bugs.chromium.org/p/chromium/issues/detail?id=467936
  613. std::string outputFile =
  614. envVar + "=log_path='" + this->MemoryTesterOutputFile + "'";
  615. this->MemoryTesterEnvironmentVariable =
  616. outputFile + suppressionsOption + extraOptions;
  617. break;
  618. }
  619. default:
  620. cmCTestLog(this->CTest, ERROR_MESSAGE,
  621. "Do not understand memory checker: " << this->MemoryTester
  622. << std::endl);
  623. return false;
  624. }
  625. this->InitializeResultsVectors();
  626. // std::vector<std::string>::size_type cc;
  627. // for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  628. // {
  629. // this->MemoryTesterGlobalResults[cc] = 0;
  630. // }
  631. return true;
  632. }
  633. bool cmCTestMemCheckHandler::ProcessMemCheckOutput(const std::string& str,
  634. std::string& log,
  635. std::vector<int>& results)
  636. {
  637. switch (this->MemoryTesterStyle) {
  638. case cmCTestMemCheckHandler::VALGRIND:
  639. return this->ProcessMemCheckValgrindOutput(str, log, results);
  640. case cmCTestMemCheckHandler::PURIFY:
  641. return this->ProcessMemCheckPurifyOutput(str, log, results);
  642. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  643. case cmCTestMemCheckHandler::LEAK_SANITIZER:
  644. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  645. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  646. case cmCTestMemCheckHandler::UB_SANITIZER:
  647. return this->ProcessMemCheckSanitizerOutput(str, log, results);
  648. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  649. return this->ProcessMemCheckBoundsCheckerOutput(str, log, results);
  650. default:
  651. log.append("\nMemory checking style used was: ");
  652. log.append("None that I know");
  653. log = str;
  654. return true;
  655. }
  656. }
  657. std::vector<int>::size_type cmCTestMemCheckHandler::FindOrAddWarning(
  658. const std::string& warning)
  659. {
  660. for (std::vector<std::string>::size_type i = 0;
  661. i < this->ResultStrings.size(); ++i) {
  662. if (this->ResultStrings[i] == warning) {
  663. return i;
  664. }
  665. }
  666. this->GlobalResults.push_back(0); // this must stay the same size
  667. this->ResultStrings.push_back(warning);
  668. this->ResultStringsLong.push_back(warning);
  669. return this->ResultStrings.size() - 1;
  670. }
  671. bool cmCTestMemCheckHandler::ProcessMemCheckSanitizerOutput(
  672. const std::string& str, std::string& log, std::vector<int>& result)
  673. {
  674. std::string regex;
  675. switch (this->MemoryTesterStyle) {
  676. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  677. regex = "ERROR: AddressSanitizer: (.*) on.*";
  678. break;
  679. case cmCTestMemCheckHandler::LEAK_SANITIZER:
  680. // use leakWarning regex
  681. break;
  682. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  683. regex = "WARNING: ThreadSanitizer: (.*) \\(pid=.*\\)";
  684. break;
  685. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  686. regex = "WARNING: MemorySanitizer: (.*)";
  687. break;
  688. case cmCTestMemCheckHandler::UB_SANITIZER:
  689. regex = "runtime error: (.*)";
  690. break;
  691. default:
  692. break;
  693. }
  694. cmsys::RegularExpression sanitizerWarning(regex);
  695. cmsys::RegularExpression leakWarning("(Direct|Indirect) leak of .*");
  696. int defects = 0;
  697. std::vector<std::string> lines;
  698. cmSystemTools::Split(str.c_str(), lines);
  699. std::ostringstream ostr;
  700. log = "";
  701. for (std::vector<std::string>::iterator i = lines.begin(); i != lines.end();
  702. ++i) {
  703. std::string resultFound;
  704. if (leakWarning.find(*i)) {
  705. resultFound = leakWarning.match(1) + " leak";
  706. } else if (sanitizerWarning.find(*i)) {
  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 << *i << 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 = "";
  732. cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
  733. int defects = 0;
  734. for (std::vector<std::string>::iterator i = lines.begin(); i != lines.end();
  735. ++i) {
  736. std::vector<int>::size_type failure = this->ResultStrings.size();
  737. if (pfW.find(*i)) {
  738. std::vector<int>::size_type cc;
  739. for (cc = 0; cc < this->ResultStrings.size(); cc++) {
  740. if (pfW.match(1) == this->ResultStrings[cc]) {
  741. failure = cc;
  742. break;
  743. }
  744. }
  745. if (cc == this->ResultStrings.size()) {
  746. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
  747. << pfW.match(1) << std::endl);
  748. ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
  749. << std::endl;
  750. }
  751. }
  752. if (failure != this->ResultStrings.size()) {
  753. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  754. results[failure]++;
  755. defects++;
  756. }
  757. ostr << *i << std::endl;
  758. }
  759. log = ostr.str();
  760. this->DefectCount += defects;
  761. return defects == 0;
  762. }
  763. bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
  764. const std::string& str, std::string& log, std::vector<int>& results)
  765. {
  766. std::vector<std::string> lines;
  767. cmSystemTools::Split(str.c_str(), lines);
  768. bool unlimitedOutput = false;
  769. if (str.find("CTEST_FULL_OUTPUT") != std::string::npos ||
  770. this->CustomMaximumFailedTestOutputSize == 0) {
  771. unlimitedOutput = true;
  772. }
  773. std::string::size_type cc;
  774. std::ostringstream ostr;
  775. log = "";
  776. int defects = 0;
  777. cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
  778. cmsys::RegularExpression vgFIM(
  779. "== .*Invalid free\\(\\) / delete / delete\\[\\]");
  780. cmsys::RegularExpression vgFMM(
  781. "== .*Mismatched free\\(\\) / delete / delete \\[\\]");
  782. cmsys::RegularExpression vgMLK1(
  783. "== .*[0-9,]+ bytes in [0-9,]+ blocks are definitely lost"
  784. " in loss record [0-9,]+ of [0-9,]+");
  785. cmsys::RegularExpression vgMLK2(
  786. "== .*[0-9,]+ \\([0-9,]+ direct, [0-9,]+ indirect\\)"
  787. " bytes in [0-9,]+ blocks are definitely lost"
  788. " in loss record [0-9,]+ of [0-9,]+");
  789. cmsys::RegularExpression vgPAR(
  790. "== .*Syscall param .* (contains|points to) unaddressable byte\\(s\\)");
  791. cmsys::RegularExpression vgMPK1(
  792. "== .*[0-9,]+ bytes in [0-9,]+ blocks are possibly lost in"
  793. " loss record [0-9,]+ of [0-9,]+");
  794. cmsys::RegularExpression vgMPK2(
  795. "== .*[0-9,]+ bytes in [0-9,]+ blocks are still reachable"
  796. " in loss record [0-9,]+ of [0-9,]+");
  797. cmsys::RegularExpression vgUMC(
  798. "== .*Conditional jump or move depends on uninitialised value\\(s\\)");
  799. cmsys::RegularExpression vgUMR1(
  800. "== .*Use of uninitialised value of size [0-9,]+");
  801. cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9,]+");
  802. cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
  803. cmsys::RegularExpression vgUMR4(
  804. "== .*Syscall param .* contains "
  805. "uninitialised or unaddressable byte\\(s\\)");
  806. cmsys::RegularExpression vgUMR5("== .*Syscall param .* uninitialised");
  807. cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9,]+");
  808. cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
  809. "locked by a different thread");
  810. std::vector<std::string::size_type> nonValGrindOutput;
  811. double sttime = cmSystemTools::GetTime();
  812. cmCTestOptionalLog(this->CTest, DEBUG,
  813. "Start test: " << lines.size() << std::endl, this->Quiet);
  814. std::string::size_type totalOutputSize = 0;
  815. for (cc = 0; cc < lines.size(); cc++) {
  816. cmCTestOptionalLog(this->CTest, DEBUG,
  817. "test line " << lines[cc] << std::endl, this->Quiet);
  818. if (valgrindLine.find(lines[cc])) {
  819. cmCTestOptionalLog(this->CTest, DEBUG,
  820. "valgrind line " << lines[cc] << std::endl,
  821. this->Quiet);
  822. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  823. if (vgFIM.find(lines[cc])) {
  824. failure = cmCTestMemCheckHandler::FIM;
  825. } else if (vgFMM.find(lines[cc])) {
  826. failure = cmCTestMemCheckHandler::FMM;
  827. } else if (vgMLK1.find(lines[cc])) {
  828. failure = cmCTestMemCheckHandler::MLK;
  829. } else if (vgMLK2.find(lines[cc])) {
  830. failure = cmCTestMemCheckHandler::MLK;
  831. } else if (vgPAR.find(lines[cc])) {
  832. failure = cmCTestMemCheckHandler::PAR;
  833. } else if (vgMPK1.find(lines[cc])) {
  834. failure = cmCTestMemCheckHandler::MPK;
  835. } else if (vgMPK2.find(lines[cc])) {
  836. failure = cmCTestMemCheckHandler::MPK;
  837. } else if (vgUMC.find(lines[cc])) {
  838. failure = cmCTestMemCheckHandler::UMC;
  839. } else if (vgUMR1.find(lines[cc])) {
  840. failure = cmCTestMemCheckHandler::UMR;
  841. } else if (vgUMR2.find(lines[cc])) {
  842. failure = cmCTestMemCheckHandler::UMR;
  843. } else if (vgUMR3.find(lines[cc])) {
  844. failure = cmCTestMemCheckHandler::UMR;
  845. } else if (vgUMR4.find(lines[cc])) {
  846. failure = cmCTestMemCheckHandler::UMR;
  847. } else if (vgUMR5.find(lines[cc])) {
  848. failure = cmCTestMemCheckHandler::UMR;
  849. } else if (vgIPW.find(lines[cc])) {
  850. failure = cmCTestMemCheckHandler::IPW;
  851. } else if (vgABR.find(lines[cc])) {
  852. failure = cmCTestMemCheckHandler::ABR;
  853. }
  854. if (failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT) {
  855. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  856. results[failure]++;
  857. defects++;
  858. }
  859. totalOutputSize += lines[cc].size();
  860. ostr << lines[cc] << std::endl;
  861. } else {
  862. nonValGrindOutput.push_back(cc);
  863. }
  864. }
  865. // Now put all all the non valgrind output into the test output
  866. // This should be last in case it gets truncated by the output
  867. // limiting code
  868. for (std::vector<std::string::size_type>::iterator i =
  869. nonValGrindOutput.begin();
  870. i != nonValGrindOutput.end(); ++i) {
  871. totalOutputSize += lines[*i].size();
  872. ostr << lines[*i] << std::endl;
  873. if (!unlimitedOutput &&
  874. totalOutputSize >
  875. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize)) {
  876. ostr << "....\n";
  877. ostr << "Test Output for this test has been truncated see testing"
  878. " machine logs for full output,\n";
  879. ostr << "or put CTEST_FULL_OUTPUT in the output of "
  880. "this test program.\n";
  881. break; // stop the copy of output if we are full
  882. }
  883. }
  884. cmCTestOptionalLog(this->CTest, DEBUG, "End test (elapsed: "
  885. << (cmSystemTools::GetTime() - sttime) << 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 = "";
  895. double sttime = cmSystemTools::GetTime();
  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 (cc = 0; cc < parser.Errors.size(); ++cc) {
  923. results[parser.Errors[cc]]++;
  924. defects++;
  925. }
  926. cmCTestOptionalLog(this->CTest, DEBUG, "End test (elapsed: "
  927. << (cmSystemTools::GetTime() - sttime) << std::endl,
  928. this->Quiet);
  929. if (defects) {
  930. // only put the output of Bounds Checker if there were
  931. // errors or leaks detected
  932. log = parser.Log;
  933. }
  934. this->DefectCount += defects;
  935. return defects == 0;
  936. }
  937. // PostProcessTest memcheck results
  938. void cmCTestMemCheckHandler::PostProcessTest(cmCTestTestResult& res, int test)
  939. {
  940. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  941. "PostProcessTest memcheck results for : " << res.Name
  942. << std::endl,
  943. this->Quiet);
  944. if (this->MemoryTesterStyle == cmCTestMemCheckHandler::BOUNDS_CHECKER) {
  945. this->PostProcessBoundsCheckerTest(res, test);
  946. } else {
  947. std::vector<std::string> files;
  948. this->TestOutputFileNames(test, files);
  949. for (std::vector<std::string>::iterator i = files.begin();
  950. i != files.end(); ++i) {
  951. this->AppendMemTesterOutput(res, *i);
  952. }
  953. }
  954. }
  955. // This method puts the bounds checker output file into the output
  956. // for the test
  957. void cmCTestMemCheckHandler::PostProcessBoundsCheckerTest(
  958. cmCTestTestResult& res, int test)
  959. {
  960. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  961. "PostProcessBoundsCheckerTest for : " << res.Name
  962. << std::endl,
  963. this->Quiet);
  964. std::vector<std::string> files;
  965. this->TestOutputFileNames(test, files);
  966. if (files.empty()) {
  967. return;
  968. }
  969. std::string ofile = files[0];
  970. if (ofile.empty()) {
  971. return;
  972. }
  973. // put a scope around this to close ifs so the file can be removed
  974. {
  975. cmsys::ifstream ifs(ofile.c_str());
  976. if (!ifs) {
  977. std::string log = "Cannot read memory tester output file: " + ofile;
  978. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  979. return;
  980. }
  981. res.Output += BOUNDS_CHECKER_MARKER;
  982. res.Output += "\n";
  983. std::string line;
  984. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  985. res.Output += line;
  986. res.Output += "\n";
  987. }
  988. }
  989. cmSystemTools::Delay(1000);
  990. cmSystemTools::RemoveFile(this->BoundsCheckerDPBDFile);
  991. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  992. "Remove: " << this->BoundsCheckerDPBDFile << std::endl,
  993. this->Quiet);
  994. cmSystemTools::RemoveFile(this->BoundsCheckerXMLFile);
  995. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  996. "Remove: " << this->BoundsCheckerXMLFile << std::endl,
  997. this->Quiet);
  998. }
  999. void cmCTestMemCheckHandler::AppendMemTesterOutput(cmCTestTestResult& res,
  1000. std::string const& ofile)
  1001. {
  1002. if (ofile.empty()) {
  1003. return;
  1004. }
  1005. // put ifs in scope so file can be deleted if needed
  1006. {
  1007. cmsys::ifstream ifs(ofile.c_str());
  1008. if (!ifs) {
  1009. std::string log = "Cannot read memory tester output file: " + ofile;
  1010. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  1011. return;
  1012. }
  1013. std::string line;
  1014. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  1015. res.Output += line;
  1016. res.Output += "\n";
  1017. }
  1018. }
  1019. if (this->LogWithPID) {
  1020. cmSystemTools::RemoveFile(ofile);
  1021. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1022. "Remove: " << ofile << "\n", this->Quiet);
  1023. }
  1024. }
  1025. void cmCTestMemCheckHandler::TestOutputFileNames(
  1026. int test, std::vector<std::string>& files)
  1027. {
  1028. std::string index;
  1029. std::ostringstream stream;
  1030. stream << test;
  1031. index = stream.str();
  1032. std::string ofile = this->MemoryTesterOutputFile;
  1033. std::string::size_type pos = ofile.find("??");
  1034. ofile.replace(pos, 2, index);
  1035. if (this->LogWithPID) {
  1036. ofile += ".*";
  1037. cmsys::Glob g;
  1038. g.FindFiles(ofile);
  1039. if (g.GetFiles().empty()) {
  1040. std::string log = "Cannot find memory tester output file: " + ofile;
  1041. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  1042. ofile = "";
  1043. } else {
  1044. files = g.GetFiles();
  1045. return;
  1046. }
  1047. } else if (!cmSystemTools::FileExists(ofile.c_str())) {
  1048. std::string log = "Cannot find memory tester output file: " + ofile;
  1049. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  1050. ofile = "";
  1051. }
  1052. files.push_back(ofile);
  1053. }