forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadexecutor.cpp
547 lines (452 loc) · 17.1 KB
/
threadexecutor.cpp
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
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "threadexecutor.h"
#include "cppcheck.h"
#include "cppcheckexecutor.h"
#ifdef THREADING_MODEL_FORK
#include <algorithm>
#include <iostream>
#include <sys/select.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <cstdio>
#include <errno.h>
#include <time.h>
#include <cstring>
#include <sstream>
#endif
#ifdef THREADING_MODEL_WIN
#include <process.h>
#include <windows.h>
#include <algorithm>
#include <cstring>
#include <errno.h>
#endif
// required for FD_ZERO
using std::memset;
ThreadExecutor::ThreadExecutor(const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger)
: _files(files), _settings(settings), _errorLogger(errorLogger), _fileCount(0)
{
#if defined(THREADING_MODEL_FORK)
_wpipe = 0;
#elif defined(THREADING_MODEL_WIN)
_processedFiles = 0;
_totalFiles = 0;
_processedSize = 0;
_totalFileSize = 0;
#endif
}
ThreadExecutor::~ThreadExecutor()
{
//dtor
}
///////////////////////////////////////////////////////////////////////////////
////// This code is for platforms that support fork() only ////////////////////
///////////////////////////////////////////////////////////////////////////////
#if defined(THREADING_MODEL_FORK)
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
{
_fileContents[ path ] = content;
}
int ThreadExecutor::handleRead(int rpipe, unsigned int &result)
{
char type = 0;
if (read(rpipe, &type, 1) <= 0) {
if (errno == EAGAIN)
return 0;
return -1;
}
if (type != REPORT_OUT && type != REPORT_ERROR && type != REPORT_INFO && type != CHILD_END) {
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
std::exit(0);
}
unsigned int len = 0;
if (read(rpipe, &len, sizeof(len)) <= 0) {
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
std::exit(0);
}
char *buf = new char[len];
if (read(rpipe, buf, len) <= 0) {
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
std::exit(0);
}
if (type == REPORT_OUT) {
_errorLogger.reportOut(buf);
} else if (type == REPORT_ERROR || type == REPORT_INFO) {
ErrorLogger::ErrorMessage msg;
msg.deserialize(buf);
std::string file;
unsigned int line(0);
if (!msg._callStack.empty()) {
file = msg._callStack.back().getfile(false);
line = msg._callStack.back().line;
}
if (!_settings.nomsg.isSuppressed(msg._id, file, line)) {
// Alert only about unique errors
std::string errmsg = msg.toString(_settings._verbose);
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) {
_errorList.push_back(errmsg);
if (type == REPORT_ERROR)
_errorLogger.reportErr(msg);
else
_errorLogger.reportInfo(msg);
}
}
} else if (type == CHILD_END) {
std::istringstream iss(buf);
unsigned int fileResult = 0;
iss >> fileResult;
result += fileResult;
delete [] buf;
return -1;
}
delete [] buf;
return 1;
}
bool ThreadExecutor::checkLoadAverage(size_t nchilds)
{
#ifdef __CYGWIN__ // getloadavg() is unsupported on Cygwin.
return true;
#else
if (!nchilds || !_settings._loadAverage) {
return true;
}
double sample(0);
if (getloadavg(&sample, 1) != 1) {
// disable load average checking on getloadavg error
return true;
} else if (sample < _settings._loadAverage) {
return true;
}
return false;
#endif
}
unsigned int ThreadExecutor::check()
{
_fileCount = 0;
unsigned int result = 0;
std::size_t totalfilesize = 0;
for (std::map<std::string, std::size_t>::const_iterator i = _files.begin(); i != _files.end(); ++i) {
totalfilesize += i->second;
}
std::list<int> rpipes;
std::map<pid_t, std::string> childFile;
std::map<int, std::string> pipeFile;
std::size_t processedsize = 0;
std::map<std::string, std::size_t>::const_iterator i = _files.begin();
for (;;) {
// Start a new child
size_t nchilds = rpipes.size();
if (i != _files.end() && nchilds < _settings._jobs && checkLoadAverage(nchilds)) {
int pipes[2];
if (pipe(pipes) == -1) {
std::cerr << "pipe() failed: "<< std::strerror(errno) << std::endl;
std::exit(EXIT_FAILURE);
}
int flags = 0;
if ((flags = fcntl(pipes[0], F_GETFL, 0)) < 0) {
std::cerr << "fcntl(F_GETFL) failed: "<< std::strerror(errno) << std::endl;
std::exit(EXIT_FAILURE);
}
if (fcntl(pipes[0], F_SETFL, flags | O_NONBLOCK) < 0) {
std::cerr << "fcntl(F_SETFL) failed: "<< std::strerror(errno) << std::endl;
std::exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid < 0) {
// Error
std::cerr << "Failed to create child process: "<< std::strerror(errno) << std::endl;
std::exit(EXIT_FAILURE);
} else if (pid == 0) {
close(pipes[0]);
_wpipe = pipes[1];
CppCheck fileChecker(*this, false);
fileChecker.settings() = _settings;
unsigned int resultOfCheck = 0;
if (!_fileContents.empty() && _fileContents.find(i->first) != _fileContents.end()) {
// File content was given as a string
resultOfCheck = fileChecker.check(i->first, _fileContents[ i->first ]);
} else {
// Read file from a file
resultOfCheck = fileChecker.check(i->first);
}
std::ostringstream oss;
oss << resultOfCheck;
writeToPipe(CHILD_END, oss.str());
std::exit(0);
}
close(pipes[1]);
rpipes.push_back(pipes[0]);
childFile[pid] = i->first;
pipeFile[pipes[0]] = i->first;
++i;
} else if (!rpipes.empty()) {
fd_set rfds;
FD_ZERO(&rfds);
for (std::list<int>::const_iterator rp = rpipes.begin(); rp != rpipes.end(); ++rp)
FD_SET(*rp, &rfds);
struct timeval tv; // for every second polling of load average condition
tv.tv_sec = 1;
tv.tv_usec = 0;
int r = select(*std::max_element(rpipes.begin(), rpipes.end()) + 1, &rfds, NULL, NULL, &tv);
if (r > 0) {
std::list<int>::iterator rp = rpipes.begin();
while (rp != rpipes.end()) {
if (FD_ISSET(*rp, &rfds)) {
int readRes = handleRead(*rp, result);
if (readRes == -1) {
std::size_t size = 0;
std::map<int, std::string>::iterator p = pipeFile.find(*rp);
if (p != pipeFile.end()) {
std::string name = p->second;
pipeFile.erase(p);
std::map<std::string, std::size_t>::const_iterator fs = _files.find(name);
if (fs != _files.end()) {
size = fs->second;
}
}
_fileCount++;
processedsize += size;
if (!_settings._errorsOnly)
CppCheckExecutor::reportStatus(_fileCount, _files.size(), processedsize, totalfilesize);
close(*rp);
rp = rpipes.erase(rp);
} else
++rp;
} else
++rp;
}
}
int stat = 0;
pid_t child = waitpid(0, &stat, WNOHANG);
if (child > 0) {
std::string childname;
std::map<pid_t, std::string>::iterator c = childFile.find(child);
if (c != childFile.end()) {
childname = c->second;
childFile.erase(c);
}
if (WIFSIGNALED(stat)) {
std::ostringstream oss;
oss << "Internal error: Child process crashed with signal " << WTERMSIG(stat);
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
locations.push_back(ErrorLogger::ErrorMessage::FileLocation(childname, 0));
const ErrorLogger::ErrorMessage errmsg(locations,
Severity::error,
oss.str(),
"cppcheckError",
false);
if (!_settings.nomsg.isSuppressed(errmsg._id, childname, 0))
_errorLogger.reportErr(errmsg);
}
}
} else {
// All done
break;
}
}
return result;
}
void ThreadExecutor::writeToPipe(PipeSignal type, const std::string &data)
{
unsigned int len = static_cast<unsigned int>(data.length() + 1);
char *out = new char[ len + 1 + sizeof(len)];
out[0] = static_cast<char>(type);
std::memcpy(&(out[1]), &len, sizeof(len));
std::memcpy(&(out[1+sizeof(len)]), data.c_str(), len);
if (write(_wpipe, out, len + 1 + sizeof(len)) <= 0) {
delete [] out;
out = 0;
std::cerr << "#### ThreadExecutor::writeToPipe, Failed to write to pipe" << std::endl;
std::exit(0);
}
delete [] out;
}
void ThreadExecutor::reportOut(const std::string &outmsg)
{
writeToPipe(REPORT_OUT, outmsg);
}
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
writeToPipe(REPORT_ERROR, msg.serialize());
}
void ThreadExecutor::reportInfo(const ErrorLogger::ErrorMessage &msg)
{
writeToPipe(REPORT_INFO, msg.serialize());
}
#elif defined(THREADING_MODEL_WIN)
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
{
_fileContents[path] = content;
}
unsigned int ThreadExecutor::check()
{
HANDLE *threadHandles = new HANDLE[_settings._jobs];
_itNextFile = _files.begin();
_processedFiles = 0;
_processedSize = 0;
_totalFiles = _files.size();
_totalFileSize = 0;
for (std::map<std::string, std::size_t>::const_iterator i = _files.begin(); i != _files.end(); ++i) {
_totalFileSize += i->second;
}
InitializeCriticalSection(&_fileSync);
InitializeCriticalSection(&_errorSync);
InitializeCriticalSection(&_reportSync);
for (unsigned int i = 0; i < _settings._jobs; ++i) {
threadHandles[i] = (HANDLE)_beginthreadex(NULL, 0, threadProc, this, 0, NULL);
if (!threadHandles[i]) {
std::cerr << "#### .\nThreadExecutor::check error, errno :" << errno << std::endl;
exit(EXIT_FAILURE);
}
}
DWORD waitResult = WaitForMultipleObjects(_settings._jobs, threadHandles, TRUE, INFINITE);
if (waitResult != WAIT_OBJECT_0) {
if (waitResult == WAIT_FAILED) {
std::cerr << "#### .\nThreadExecutor::check wait failed, result: " << waitResult << " error: " << GetLastError() << std::endl;
exit(EXIT_FAILURE);
} else {
std::cerr << "#### .\nThreadExecutor::check wait failed, result: " << waitResult << std::endl;
exit(EXIT_FAILURE);
}
}
unsigned int result = 0;
for (unsigned int i = 0; i < _settings._jobs; ++i) {
DWORD exitCode;
if (!GetExitCodeThread(threadHandles[i], &exitCode)) {
std::cerr << "#### .\nThreadExecutor::check get exit code failed, error:" << GetLastError() << std::endl;
exit(EXIT_FAILURE);
}
result += exitCode;
if (!CloseHandle(threadHandles[i])) {
std::cerr << "#### .\nThreadExecutor::check close handle failed, error:" << GetLastError() << std::endl;
exit(EXIT_FAILURE);
}
}
DeleteCriticalSection(&_fileSync);
DeleteCriticalSection(&_errorSync);
DeleteCriticalSection(&_reportSync);
delete[] threadHandles;
return result;
}
unsigned int __stdcall ThreadExecutor::threadProc(void *args)
{
unsigned int result = 0;
ThreadExecutor *threadExecutor = static_cast<ThreadExecutor*>(args);
std::map<std::string, std::size_t>::const_iterator &it = threadExecutor->_itNextFile;
// guard static members of CppCheck against concurrent access
EnterCriticalSection(&threadExecutor->_fileSync);
CppCheck fileChecker(*threadExecutor, false);
fileChecker.settings() = threadExecutor->_settings;
LeaveCriticalSection(&threadExecutor->_fileSync);
for (;;) {
EnterCriticalSection(&threadExecutor->_fileSync);
if (it == threadExecutor->_files.end()) {
LeaveCriticalSection(&threadExecutor->_fileSync);
return result;
}
const std::string &file = it->first;
const std::size_t fileSize = it->second;
++it;
LeaveCriticalSection(&threadExecutor->_fileSync);
std::map<std::string, std::string>::const_iterator fileContent = threadExecutor->_fileContents.find(file);
if (fileContent != threadExecutor->_fileContents.end()) {
// File content was given as a string
result += fileChecker.check(file, fileContent->second);
} else {
// Read file from a file
result += fileChecker.check(file);
}
EnterCriticalSection(&threadExecutor->_fileSync);
threadExecutor->_processedSize += fileSize;
threadExecutor->_processedFiles++;
if (!threadExecutor->_settings._errorsOnly) {
EnterCriticalSection(&threadExecutor->_reportSync);
CppCheckExecutor::reportStatus(threadExecutor->_processedFiles, threadExecutor->_totalFiles, threadExecutor->_processedSize, threadExecutor->_totalFileSize);
LeaveCriticalSection(&threadExecutor->_reportSync);
}
LeaveCriticalSection(&threadExecutor->_fileSync);
};
return result;
}
void ThreadExecutor::reportOut(const std::string &outmsg)
{
EnterCriticalSection(&_reportSync);
_errorLogger.reportOut(outmsg);
LeaveCriticalSection(&_reportSync);
}
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
report(msg, REPORT_ERROR);
}
void ThreadExecutor::reportInfo(const ErrorLogger::ErrorMessage &msg)
{
report(msg, REPORT_INFO);
}
void ThreadExecutor::report(const ErrorLogger::ErrorMessage &msg, MessageType msgType)
{
std::string file;
unsigned int line(0);
if (!msg._callStack.empty()) {
file = msg._callStack.back().getfile(false);
line = msg._callStack.back().line;
}
if (_settings.nomsg.isSuppressed(msg._id, file, line))
return;
// Alert only about unique errors
bool reportError = false;
std::string errmsg = msg.toString(_settings._verbose);
EnterCriticalSection(&_errorSync);
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) {
_errorList.push_back(errmsg);
reportError = true;
}
LeaveCriticalSection(&_errorSync);
if (reportError) {
EnterCriticalSection(&_reportSync);
switch (msgType) {
case REPORT_ERROR:
_errorLogger.reportErr(msg);
break;
case REPORT_INFO:
_errorLogger.reportInfo(msg);
break;
}
LeaveCriticalSection(&_reportSync);
}
}
#else
void ThreadExecutor::addFileContent(const std::string &/*path*/, const std::string &/*content*/)
{
}
unsigned int ThreadExecutor::check()
{
return 0;
}
void ThreadExecutor::reportOut(const std::string &/*outmsg*/)
{
}
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &/*msg*/)
{
}
void ThreadExecutor::reportInfo(const ErrorLogger::ErrorMessage &/*msg*/)
{
}
#endif