1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmCTestMemCheckHandler.h"
#include "cmCTest.h"
#include "cmake.h"
#include "cmGeneratedFileStream.h"
#include <cmsys/Process.h>
#include <cmsys/RegularExpression.hxx>
#include <cmsys/Base64.h>
#include "cmMakefile.h"
#include <stdlib.h>
#include <math.h>
#include <float.h>
//----------------------------------------------------------------------
static const char* cmCTestMemCheckResultStrings[] = {
"ABR",
"ABW",
"ABWL",
"COR",
"EXU",
"FFM",
"FIM",
"FMM",
"FMR",
"FMW",
"FUM",
"IPR",
"IPW",
"MAF",
"MLK",
"MPK",
"NPR",
"ODS",
"PAR",
"PLK",
"UMC",
"UMR",
0
};
//----------------------------------------------------------------------
static const char* cmCTestMemCheckResultLongStrings[] = {
"Threading Problem",
"ABW",
"ABWL",
"COR",
"EXU",
"FFM",
"FIM",
"Mismatched deallocation",
"FMR",
"FMW",
"FUM",
"IPR",
"IPW",
"MAF",
"Memory Leak",
"Potential Memory Leak",
"NPR",
"ODS",
"Invalid syscall param",
"PLK",
"Uninitialized Memory Conditional",
"Uninitialized Memory Read",
0
};
//----------------------------------------------------------------------
cmCTestMemCheckHandler::cmCTestMemCheckHandler()
{
this->MemCheck = true;
this->CustomMaximumPassedTestOutputSize = 0;
this->CustomMaximumFailedTestOutputSize = 0;
}
//----------------------------------------------------------------------
void cmCTestMemCheckHandler::Initialize()
{
this->Superclass::Initialize();
this->MemoryTester = "";
this->MemoryTesterOptionsParsed.clear();
this->MemoryTesterOptions = "";
this->MemoryTesterStyle = UNKNOWN;
this->MemoryTesterOutputFile = "";
int cc;
for ( cc = 0; cc < NO_MEMORY_FAULT; cc ++ )
{
this->MemoryTesterGlobalResults[cc] = 0;
}
}
//----------------------------------------------------------------------
int cmCTestMemCheckHandler::PreProcessHandler()
{
if ( !this->InitializeMemoryChecking() )
{
return 0;
}
if ( !this->ExecuteCommands(this->CustomPreMemCheck) )
{
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Problem executing pre-memcheck command(s)." << std::endl);
return 0;
}
return 1;
}
//----------------------------------------------------------------------
int cmCTestMemCheckHandler::PostProcessHandler()
{
if ( !this->ExecuteCommands(this->CustomPostMemCheck) )
{
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Problem executing post-memcheck command(s)." << std::endl);
return 0;
}
return 1;
}
//----------------------------------------------------------------------
void cmCTestMemCheckHandler::GenerateTestCommand(
std::vector<const char*>& args)
{
std::vector<cmStdString>::size_type pp;
args.push_back(this->MemoryTester.c_str());
std::string memcheckcommand = "";
memcheckcommand = this->MemoryTester;
for ( pp = 0; pp < this->MemoryTesterOptionsParsed.size(); pp ++ )
{
args.push_back(this->MemoryTesterOptionsParsed[pp].c_str());
memcheckcommand += " ";
memcheckcommand += cmSystemTools::EscapeSpaces(
this->MemoryTesterOptionsParsed[pp].c_str());
}
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Memory check command: "
<< memcheckcommand << std::endl);
}
//----------------------------------------------------------------------
void cmCTestMemCheckHandler::PopulateCustomVectors(cmMakefile *mf)
{
this->cmCTestTestHandler::PopulateCustomVectors(mf);
this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_MEMCHECK",
this->CustomPreMemCheck);
this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_MEMCHECK",
this->CustomPostMemCheck);
this->CTest->PopulateCustomVector(mf,
"CTEST_CUSTOM_MEMCHECK_IGNORE",
this->CustomTestsIgnore);
}
//----------------------------------------------------------------------
void cmCTestMemCheckHandler::GenerateDartOutput(std::ostream& os)
{
if ( !this->CTest->GetProduceXML() )
{
return;
}
this->CTest->StartXML(os);
os << "<DynamicAnalysis Checker=\"";
switch ( this->MemoryTesterStyle )
{
case cmCTestMemCheckHandler::VALGRIND:
os << "Valgrind";
break;
case cmCTestMemCheckHandler::PURIFY:
os << "Purify";
break;
case cmCTestMemCheckHandler::BOUNDS_CHECKER:
os << "BoundsChecker";
break;
default:
os << "Unknown";
}
os << "\">" << std::endl;
os << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
<< "\t<TestList>\n";
cmCTestMemCheckHandler::TestResultsVector::size_type cc;
for ( cc = 0; cc < this->TestResults.size(); cc ++ )
{
cmCTestTestResult *result = &this->TestResults[cc];
std::string testPath = result->Path + "/" + result->Name;
os << "\t\t<Test>" << cmCTest::MakeXMLSafe(
this->CTest->GetShortPathToFile(testPath.c_str()))
<< "</Test>" << std::endl;
}
os << "\t</TestList>\n";
cmCTestLog(this->CTest, HANDLER_OUTPUT,
"-- Processing memory checking output: ");
unsigned int total = this->TestResults.size();
unsigned int step = total / 10;
unsigned int current = 0;
for ( cc = 0; cc < this->TestResults.size(); cc ++ )
{
cmCTestTestResult *result = &this->TestResults[cc];
std::string memcheckstr;
int memcheckresults[cmCTestMemCheckHandler::NO_MEMORY_FAULT];
int kk;
bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
memcheckresults);
if ( res && result->Status == cmCTestMemCheckHandler::COMPLETED )
{
continue;
}
this->CleanTestOutput(memcheckstr,
static_cast<size_t>(this->CustomMaximumFailedTestOutputSize));
os << "\t<Test Status=\"";
if ( result->Status == cmCTestMemCheckHandler::COMPLETED )
{
os << "passed";
}
else if ( result->Status == cmCTestMemCheckHandler::NOT_RUN )
{
os << "notrun";
}
else
{
os << "failed";
}
std::string testPath = result->Path + "/" + result->Name;
os << "\">\n"
<< "\t\t<Name>" << cmCTest::MakeXMLSafe(result->Name) << "</Name>\n"
<< "\t\t<Path>" << cmCTest::MakeXMLSafe(
this->CTest->GetShortPathToFile(result->Path.c_str())) << "</Path>\n"
<< "\t\t<FullName>" << cmCTest::MakeXMLSafe(
this->CTest->GetShortPathToFile(testPath.c_str())) << "</FullName>\n"
<< "\t\t<FullCommandLine>"
<< cmCTest::MakeXMLSafe(result->FullCommandLine)
<< "</FullCommandLine>\n"
<< "\t\t<Results>" << std::endl;
for ( kk = 0; cmCTestMemCheckResultLongStrings[kk]; kk ++ )
{
if ( memcheckresults[kk] )
{
os << "\t\t\t<Defect type=\"" << cmCTestMemCheckResultLongStrings[kk]
<< "\">"
<< memcheckresults[kk]
<< "</Defect>" << std::endl;
}
this->MemoryTesterGlobalResults[kk] += memcheckresults[kk];
}
os
<< "\t\t</Results>\n"
<< "\t<Log>\n" << memcheckstr << std::endl
<< "\t</Log>\n"
<< "\t</Test>" << std::endl;
if ( current < cc )
{
cmCTestLog(this->CTest, HANDLER_OUTPUT, "#" << std::flush);
current += step;
}
}
cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory checking results:"
<< std::endl);
os << "\t<DefectList>" << std::endl;
for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
{
if ( this->MemoryTesterGlobalResults[cc] )
{
#ifdef cerr
# undef cerr
#endif
std::cerr.width(35);
#define cerr no_cerr
cmCTestLog(this->CTest, HANDLER_OUTPUT,
cmCTestMemCheckResultLongStrings[cc] << " - "
<< this->MemoryTesterGlobalResults[cc] << std::endl);
os << "\t\t<Defect Type=\"" << cmCTestMemCheckResultLongStrings[cc]
<< "\"/>" << std::endl;
}
}
os << "\t</DefectList>" << std::endl;
os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>" << std::endl;
os << "<ElapsedMinutes>"
<< static_cast<int>(this->ElapsedTestingTime/6)/10.0
<< "</ElapsedMinutes>\n";
os << "</DynamicAnalysis>" << std::endl;
this->CTest->EndXML(os);
}
//----------------------------------------------------------------------
bool cmCTestMemCheckHandler::InitializeMemoryChecking()
{
// Setup the command
if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
"MemoryCheckCommand").c_str()) )
{
this->MemoryTester
= cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
"MemoryCheckCommand").c_str());
}
else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
"PurifyCommand").c_str()) )
{
this->MemoryTester
= cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
"PurifyCommand").c_str());
}
else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
"ValgrindCommand").c_str()) )
{
this->MemoryTester
= cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
"ValgrindCommand").c_str());
}
else
{
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Memory checker (MemoryCheckCommand) "
"not set, or cannot find the specified program."
<< std::endl);
return false;
}
if ( this->MemoryTester[0] == '\"' &&
this->MemoryTester[this->MemoryTester.size()-1] == '\"' )
{
this->MemoryTester
= this->MemoryTester.substr(1, this->MemoryTester.size()-2);
}
// Setup the options
if ( this->CTest->GetCTestConfiguration(
"MemoryCheckCommandOptions").size() )
{
this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
"MemoryCheckCommandOptions");
}
else if ( this->CTest->GetCTestConfiguration(
"ValgrindCommandOptions").size() )
{
this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
"ValgrindCommandOptions");
}
this->MemoryTesterOutputFile
= this->CTest->GetBinaryDir() + "/Testing/Temporary/MemoryChecker.log";
this->MemoryTesterOutputFile
= cmSystemTools::EscapeSpaces(this->MemoryTesterOutputFile.c_str());
if ( this->MemoryTester.find("valgrind") != std::string::npos )
{
this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
if ( !this->MemoryTesterOptions.size() )
{
this->MemoryTesterOptions = "-q --tool=memcheck --leak-check=yes "
"--show-reachable=yes --workaround-gcc296-bugs=yes --num-callers=100";
}
if ( this->CTest->GetCTestConfiguration(
"MemoryCheckSuppressionFile").size() )
{
if ( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
"MemoryCheckSuppressionFile").c_str()) )
{
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Cannot find memory checker suppression file: "
<< this->CTest->GetCTestConfiguration(
"MemoryCheckSuppressionFile").c_str() << std::endl);
return false;
}
this->MemoryTesterOptions += " --suppressions=" +
cmSystemTools::EscapeSpaces(this->CTest->GetCTestConfiguration(
"MemoryCheckSuppressionFile").c_str()) + "";
}
}
else if ( this->MemoryTester.find("purify") != std::string::npos )
{
this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
#ifdef _WIN32
this->MemoryTesterOptions += " /SAVETEXTDATA=" +
this->MemoryTesterOutputFile;
#else
this->MemoryTesterOptions += " -log-file=" + this->MemoryTesterOutputFile;
#endif
}
else if ( this->MemoryTester.find("boundschecker") != std::string::npos )
{
this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Bounds checker not yet implemented" << std::endl);
return false;
}
else
{
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Do not understand memory checker: " << this->MemoryTester.c_str()
<< std::endl);
return false;
}
this->MemoryTesterOptionsParsed
= cmSystemTools::ParseArguments(this->MemoryTesterOptions.c_str());
std::vector<cmStdString>::size_type cc;
for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
{
this->MemoryTesterGlobalResults[cc] = 0;
}
return true;
}
//----------------------------------------------------------------------
bool cmCTestMemCheckHandler::ProcessMemCheckOutput(const std::string& str,
std::string& log, int* results)
{
std::string::size_type cc;
for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
{
results[cc] = 0;
}
if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::VALGRIND )
{
return this->ProcessMemCheckValgrindOutput(str, log, results);
}
else if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
{
return this->ProcessMemCheckPurifyOutput(str, log, results);
}
else if ( this->MemoryTesterStyle ==
cmCTestMemCheckHandler::BOUNDS_CHECKER )
{
log.append("\nMemory checking style used was: ");
log.append("Bounds Checker");
}
else
{
log.append("\nMemory checking style used was: ");
log.append("None that I know");
log = str;
}
return true;
}
//----------------------------------------------------------------------
bool cmCTestMemCheckHandler::ProcessMemCheckPurifyOutput(
const std::string&, std::string& log,
int* results)
{
if ( !cmSystemTools::FileExists(this->MemoryTesterOutputFile.c_str()) )
{
log = "Cannot find Purify output file: " + this->MemoryTesterOutputFile;
cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
return false;
}
std::ifstream ifs(this->MemoryTesterOutputFile.c_str());
if ( !ifs )
{
log = "Cannot read Purify output file: " + this->MemoryTesterOutputFile;
cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
return false;
}
cmOStringStream ostr;
log = "";
cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
int defects = 0;
std::string line;
while ( cmSystemTools::GetLineFromStream(ifs, line) )
{
int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
if ( pfW.find(line) )
{
int cc;
for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
{
if ( pfW.match(1) == cmCTestMemCheckResultStrings[cc] )
{
failure = cc;
break;
}
}
if ( cc == cmCTestMemCheckHandler::NO_MEMORY_FAULT )
{
cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
<< pfW.match(1) << std::endl);
ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
<< std::endl;
}
}
if ( failure != NO_MEMORY_FAULT )
{
ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
results[failure] ++;
defects ++;
}
ostr << cmCTest::MakeXMLSafe(line) << std::endl;
}
log = ostr.str();
if ( defects )
{
return false;
}
return true;
}
//----------------------------------------------------------------------
bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
const std::string& str, std::string& log,
int* results)
{
std::vector<cmStdString> lines;
cmSystemTools::Split(str.c_str(), lines);
std::string::size_type cc;
cmOStringStream ostr;
log = "";
int defects = 0;
cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
cmsys::RegularExpression vgFIM(
"== .*Invalid free\\(\\) / delete / delete\\[\\]");
cmsys::RegularExpression vgFMM(
"== .*Mismatched free\\(\\) / delete / delete \\[\\]");
cmsys::RegularExpression vgMLK(
"== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are definitely lost"
" in loss record [0-9][0-9]* of [0-9]");
cmsys::RegularExpression vgPAR(
"== .*Syscall param .* contains unaddressable byte\\(s\\)");
cmsys::RegularExpression vgMPK1(
"== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are possibly lost in"
" loss record [0-9][0-9]* of [0-9]");
cmsys::RegularExpression vgMPK2(
"== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are still reachable"
" in loss record [0-9][0-9]* of [0-9]");
cmsys::RegularExpression vgUMC(
"== .*Conditional jump or move depends on uninitialised value\\(s\\)");
cmsys::RegularExpression vgUMR1(
"== .*Use of uninitialised value of size [0-9][0-9]*");
cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9][0-9]*");
cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
cmsys::RegularExpression vgUMR4("== .*Syscall param .* contains "
"uninitialised or unaddressable byte\\(s\\)");
cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9]");
cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
"locked by a different thread");
double sttime = cmSystemTools::GetTime();
cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
for ( cc = 0; cc < lines.size(); cc ++ )
{
if ( valgrindLine.find(lines[cc]) )
{
int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
if ( vgFIM.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::FIM;
}
else if ( vgFMM.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::FMM;
}
else if ( vgMLK.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::MLK;
}
else if ( vgPAR.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::PAR;
}
else if ( vgMPK1.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::MPK;
}
else if ( vgMPK2.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::MPK;
}
else if ( vgUMC.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::UMC;
}
else if ( vgUMR1.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::UMR;
}
else if ( vgUMR2.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::UMR;
}
else if ( vgUMR3.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::UMR;
}
else if ( vgUMR4.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::UMR;
}
else if ( vgIPW.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::IPW;
}
else if ( vgABR.find(lines[cc]) )
{
failure = cmCTestMemCheckHandler::ABR;
}
if ( failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT )
{
ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
results[failure] ++;
defects ++;
}
ostr << cmCTest::MakeXMLSafe(lines[cc]) << std::endl;
}
}
cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
<< (cmSystemTools::GetTime() - sttime) << std::endl);
log = ostr.str();
if ( defects )
{
return false;
}
return true;
}
|