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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmProcess.h"
#include <csignal>
#include <iostream>
#include <string>
#include <utility>
#include <cmext/algorithm>
#include "cmsys/Process.h"
#include "cmCTest.h"
#include "cmCTestRunTest.h"
#include "cmCTestTestHandler.h"
#include "cmGetPipes.h"
#include "cmStringAlgorithms.h"
#if defined(_WIN32)
# include "cm_kwiml.h"
#endif
#define CM_PROCESS_BUF_SIZE 65536
cmProcess::cmProcess(std::unique_ptr<cmCTestRunTest> runner)
: Runner(std::move(runner))
, Conv(cmProcessOutput::UTF8, CM_PROCESS_BUF_SIZE)
{
this->Timeout = cmDuration::zero();
this->TotalTime = cmDuration::zero();
this->ExitValue = 0;
this->Id = 0;
this->StartTime = std::chrono::steady_clock::time_point();
}
cmProcess::~cmProcess() = default;
void cmProcess::SetCommand(std::string const& command)
{
this->Command = command;
}
void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
{
this->Arguments = args;
}
void cmProcess::SetWorkingDirectory(std::string const& dir)
{
this->WorkingDirectory = dir;
}
bool cmProcess::StartProcess(uv_loop_t& loop, std::vector<size_t>* affinity)
{
this->ProcessState = cmProcess::State::Error;
if (this->Command.empty()) {
return false;
}
this->StartTime = std::chrono::steady_clock::now();
this->ProcessArgs.clear();
// put the command as arg0
this->ProcessArgs.push_back(this->Command.c_str());
// now put the command arguments in
for (std::string const& arg : this->Arguments) {
this->ProcessArgs.push_back(arg.c_str());
}
this->ProcessArgs.push_back(nullptr); // null terminate the list
cm::uv_timer_ptr timer;
int status = timer.init(loop, this);
if (status != 0) {
cmCTestLog(this->Runner->GetCTest(), ERROR_MESSAGE,
"Error initializing timer: " << uv_strerror(status)
<< std::endl);
return false;
}
cm::uv_pipe_ptr pipe_writer;
cm::uv_pipe_ptr pipe_reader;
pipe_writer.init(loop, 0);
pipe_reader.init(loop, 0, this);
int fds[2] = { -1, -1 };
status = cmGetPipes(fds);
if (status != 0) {
cmCTestLog(this->Runner->GetCTest(), ERROR_MESSAGE,
"Error initializing pipe: " << uv_strerror(status)
<< std::endl);
return false;
}
uv_pipe_open(pipe_reader, fds[0]);
uv_pipe_open(pipe_writer, fds[1]);
uv_stdio_container_t stdio[3];
stdio[0].flags = UV_INHERIT_FD;
stdio[0].data.fd = 0;
stdio[1].flags = UV_INHERIT_STREAM;
stdio[1].data.stream = pipe_writer;
stdio[2] = stdio[1];
uv_process_options_t options = uv_process_options_t();
options.file = this->Command.data();
options.args = const_cast<char**>(this->ProcessArgs.data());
options.stdio_count = 3; // in, out and err
options.exit_cb = &cmProcess::OnExitCB;
options.stdio = stdio;
#if !defined(CMAKE_USE_SYSTEM_LIBUV)
std::vector<char> cpumask;
if (affinity && !affinity->empty()) {
cpumask.resize(static_cast<size_t>(uv_cpumask_size()), 0);
for (auto p : *affinity) {
cpumask[p] = 1;
}
options.cpumask = cpumask.data();
options.cpumask_size = cpumask.size();
} else {
options.cpumask = nullptr;
options.cpumask_size = 0;
}
#else
static_cast<void>(affinity);
#endif
status =
uv_read_start(pipe_reader, &cmProcess::OnAllocateCB, &cmProcess::OnReadCB);
if (status != 0) {
cmCTestLog(this->Runner->GetCTest(), ERROR_MESSAGE,
"Error starting read events: " << uv_strerror(status)
<< std::endl);
return false;
}
status = this->Process.spawn(loop, options, this);
if (status != 0) {
cmCTestLog(this->Runner->GetCTest(), ERROR_MESSAGE,
"Process not started\n " << this->Command << "\n["
<< uv_strerror(status) << "]\n");
return false;
}
this->PipeReader = std::move(pipe_reader);
this->Timer = std::move(timer);
this->StartTimer();
this->ProcessState = cmProcess::State::Executing;
return true;
}
void cmProcess::StartTimer()
{
auto properties = this->Runner->GetTestProperties();
auto msec =
std::chrono::duration_cast<std::chrono::milliseconds>(this->Timeout);
if (msec != std::chrono::milliseconds(0) || !properties->ExplicitTimeout) {
this->Timer.start(&cmProcess::OnTimeoutCB,
static_cast<uint64_t>(msec.count()), 0);
}
}
bool cmProcess::Buffer::GetLine(std::string& line)
{
// Scan for the next newline.
for (size_type sz = this->size(); this->Last != sz; ++this->Last) {
if ((*this)[this->Last] == '\n' || (*this)[this->Last] == '\0') {
// Extract the range first..last as a line.
const char* text = this->data() + this->First;
size_type length = this->Last - this->First;
while (length && text[length - 1] == '\r') {
length--;
}
line.assign(text, length);
// Start a new range for the next line.
++this->Last;
this->First = Last;
// Return the line extracted.
return true;
}
}
// Available data have been exhausted without a newline.
if (this->First != 0) {
// Move the partial line to the beginning of the buffer.
this->erase(this->begin(), this->begin() + this->First);
this->First = 0;
this->Last = this->size();
}
return false;
}
bool cmProcess::Buffer::GetLast(std::string& line)
{
// Return the partial last line, if any.
if (!this->empty()) {
line.assign(this->data(), this->size());
this->First = this->Last = 0;
this->clear();
return true;
}
return false;
}
void cmProcess::OnReadCB(uv_stream_t* stream, ssize_t nread,
const uv_buf_t* buf)
{
auto self = static_cast<cmProcess*>(stream->data);
self->OnRead(nread, buf);
}
void cmProcess::OnRead(ssize_t nread, const uv_buf_t* buf)
{
std::string line;
if (nread > 0) {
std::string strdata;
this->Conv.DecodeText(buf->base, static_cast<size_t>(nread), strdata);
cm::append(this->Output, strdata);
while (this->Output.GetLine(line)) {
this->Runner->CheckOutput(line);
line.clear();
}
return;
}
if (nread == 0) {
return;
}
// The process will provide no more data.
if (nread != UV_EOF) {
auto error = static_cast<int>(nread);
cmCTestLog(this->Runner->GetCTest(), ERROR_MESSAGE,
"Error reading stream: " << uv_strerror(error) << std::endl);
}
// Look for partial last lines.
if (this->Output.GetLast(line)) {
this->Runner->CheckOutput(line);
}
this->ReadHandleClosed = true;
this->PipeReader.reset();
if (this->ProcessHandleClosed) {
uv_timer_stop(this->Timer);
this->Finish();
}
}
void cmProcess::OnAllocateCB(uv_handle_t* handle, size_t suggested_size,
uv_buf_t* buf)
{
auto self = static_cast<cmProcess*>(handle->data);
self->OnAllocate(suggested_size, buf);
}
void cmProcess::OnAllocate(size_t /*suggested_size*/, uv_buf_t* buf)
{
if (this->Buf.size() != CM_PROCESS_BUF_SIZE) {
this->Buf.resize(CM_PROCESS_BUF_SIZE);
}
*buf =
uv_buf_init(this->Buf.data(), static_cast<unsigned int>(this->Buf.size()));
}
void cmProcess::OnTimeoutCB(uv_timer_t* timer)
{
auto self = static_cast<cmProcess*>(timer->data);
self->OnTimeout();
}
void cmProcess::OnTimeout()
{
this->ProcessState = cmProcess::State::Expired;
bool const was_still_reading = !this->ReadHandleClosed;
if (!this->ReadHandleClosed) {
this->ReadHandleClosed = true;
this->PipeReader.reset();
}
if (!this->ProcessHandleClosed) {
// Kill the child and let our on-exit handler finish the test.
cmsysProcess_KillPID(static_cast<unsigned long>(this->Process->pid));
} else if (was_still_reading) {
// Our on-exit handler already ran but did not finish the test
// because we were still reading output. We've just dropped
// our read handler, so we need to finish the test now.
this->Finish();
}
}
void cmProcess::OnExitCB(uv_process_t* process, int64_t exit_status,
int term_signal)
{
auto self = static_cast<cmProcess*>(process->data);
self->OnExit(exit_status, term_signal);
}
void cmProcess::OnExit(int64_t exit_status, int term_signal)
{
if (this->ProcessState != cmProcess::State::Expired) {
if (
#if defined(_WIN32)
((DWORD)exit_status & 0xF0000000) == 0xC0000000
#else
term_signal != 0
#endif
) {
this->ProcessState = cmProcess::State::Exception;
} else {
this->ProcessState = cmProcess::State::Exited;
}
}
// Record exit information.
this->ExitValue = exit_status;
this->Signal = term_signal;
this->ProcessHandleClosed = true;
if (this->ReadHandleClosed) {
uv_timer_stop(this->Timer);
this->Finish();
}
}
void cmProcess::Finish()
{
this->TotalTime = std::chrono::steady_clock::now() - this->StartTime;
// Because of a processor clock scew the runtime may become slightly
// negative. If someone changed the system clock while the process was
// running this may be even more. Make sure not to report a negative
// duration here.
if (this->TotalTime <= cmDuration::zero()) {
this->TotalTime = cmDuration::zero();
}
this->Runner->FinalizeTest();
}
cmProcess::State cmProcess::GetProcessStatus()
{
return this->ProcessState;
}
void cmProcess::ChangeTimeout(cmDuration t)
{
this->Timeout = t;
this->StartTimer();
}
void cmProcess::ResetStartTime()
{
this->StartTime = std::chrono::steady_clock::now();
}
cmProcess::Exception cmProcess::GetExitException()
{
auto exception = Exception::None;
#if defined(_WIN32) && !defined(__CYGWIN__)
auto exit_code = (DWORD)this->ExitValue;
if ((exit_code & 0xF0000000) != 0xC0000000) {
return exception;
}
if (exit_code) {
switch (exit_code) {
case STATUS_DATATYPE_MISALIGNMENT:
case STATUS_ACCESS_VIOLATION:
case STATUS_IN_PAGE_ERROR:
case STATUS_INVALID_HANDLE:
case STATUS_NONCONTINUABLE_EXCEPTION:
case STATUS_INVALID_DISPOSITION:
case STATUS_ARRAY_BOUNDS_EXCEEDED:
case STATUS_STACK_OVERFLOW:
exception = Exception::Fault;
break;
case STATUS_FLOAT_DENORMAL_OPERAND:
case STATUS_FLOAT_DIVIDE_BY_ZERO:
case STATUS_FLOAT_INEXACT_RESULT:
case STATUS_FLOAT_INVALID_OPERATION:
case STATUS_FLOAT_OVERFLOW:
case STATUS_FLOAT_STACK_CHECK:
case STATUS_FLOAT_UNDERFLOW:
# ifdef STATUS_FLOAT_MULTIPLE_FAULTS
case STATUS_FLOAT_MULTIPLE_FAULTS:
# endif
# ifdef STATUS_FLOAT_MULTIPLE_TRAPS
case STATUS_FLOAT_MULTIPLE_TRAPS:
# endif
case STATUS_INTEGER_DIVIDE_BY_ZERO:
case STATUS_INTEGER_OVERFLOW:
exception = Exception::Numerical;
break;
case STATUS_CONTROL_C_EXIT:
exception = Exception::Interrupt;
break;
case STATUS_ILLEGAL_INSTRUCTION:
case STATUS_PRIVILEGED_INSTRUCTION:
exception = Exception::Illegal;
break;
default:
exception = Exception::Other;
}
}
#else
if (this->Signal) {
switch (this->Signal) {
case SIGSEGV:
exception = Exception::Fault;
break;
case SIGFPE:
exception = Exception::Numerical;
break;
case SIGINT:
exception = Exception::Interrupt;
break;
case SIGILL:
exception = Exception::Illegal;
break;
default:
exception = Exception::Other;
}
}
#endif
return exception;
}
std::string cmProcess::GetExitExceptionString()
{
std::string exception_str;
#if defined(_WIN32)
switch (this->ExitValue) {
case STATUS_CONTROL_C_EXIT:
exception_str = "User interrupt";
break;
case STATUS_FLOAT_DENORMAL_OPERAND:
exception_str = "Floating-point exception (denormal operand)";
break;
case STATUS_FLOAT_DIVIDE_BY_ZERO:
exception_str = "Divide-by-zero";
break;
case STATUS_FLOAT_INEXACT_RESULT:
exception_str = "Floating-point exception (inexact result)";
break;
case STATUS_FLOAT_INVALID_OPERATION:
exception_str = "Invalid floating-point operation";
break;
case STATUS_FLOAT_OVERFLOW:
exception_str = "Floating-point overflow";
break;
case STATUS_FLOAT_STACK_CHECK:
exception_str = "Floating-point stack check failed";
break;
case STATUS_FLOAT_UNDERFLOW:
exception_str = "Floating-point underflow";
break;
# ifdef STATUS_FLOAT_MULTIPLE_FAULTS
case STATUS_FLOAT_MULTIPLE_FAULTS:
exception_str = "Floating-point exception (multiple faults)";
break;
# endif
# ifdef STATUS_FLOAT_MULTIPLE_TRAPS
case STATUS_FLOAT_MULTIPLE_TRAPS:
exception_str = "Floating-point exception (multiple traps)";
break;
# endif
case STATUS_INTEGER_DIVIDE_BY_ZERO:
exception_str = "Integer divide-by-zero";
break;
case STATUS_INTEGER_OVERFLOW:
exception_str = "Integer overflow";
break;
case STATUS_DATATYPE_MISALIGNMENT:
exception_str = "Datatype misalignment";
break;
case STATUS_ACCESS_VIOLATION:
exception_str = "Access violation";
break;
case STATUS_IN_PAGE_ERROR:
exception_str = "In-page error";
break;
case STATUS_INVALID_HANDLE:
exception_str = "Invalid handle";
break;
case STATUS_NONCONTINUABLE_EXCEPTION:
exception_str = "Noncontinuable exception";
break;
case STATUS_INVALID_DISPOSITION:
exception_str = "Invalid disposition";
break;
case STATUS_ARRAY_BOUNDS_EXCEEDED:
exception_str = "Array bounds exceeded";
break;
case STATUS_STACK_OVERFLOW:
exception_str = "Stack overflow";
break;
case STATUS_ILLEGAL_INSTRUCTION:
exception_str = "Illegal instruction";
break;
case STATUS_PRIVILEGED_INSTRUCTION:
exception_str = "Privileged instruction";
break;
case STATUS_NO_MEMORY:
default:
char buf[1024];
const char* fmt = "Exit code 0x%" KWIML_INT_PRIx64 "\n";
_snprintf(buf, 1024, fmt, this->ExitValue);
exception_str.assign(buf);
}
#else
switch (this->Signal) {
# ifdef SIGSEGV
case SIGSEGV:
exception_str = "Segmentation fault";
break;
# endif
# ifdef SIGBUS
# if !defined(SIGSEGV) || SIGBUS != SIGSEGV
case SIGBUS:
exception_str = "Bus error";
break;
# endif
# endif
# ifdef SIGFPE
case SIGFPE:
exception_str = "Floating-point exception";
break;
# endif
# ifdef SIGILL
case SIGILL:
exception_str = "Illegal instruction";
break;
# endif
# ifdef SIGINT
case SIGINT:
exception_str = "User interrupt";
break;
# endif
# ifdef SIGABRT
case SIGABRT:
exception_str = "Child aborted";
break;
# endif
# ifdef SIGKILL
case SIGKILL:
exception_str = "Child killed";
break;
# endif
# ifdef SIGTERM
case SIGTERM:
exception_str = "Child terminated";
break;
# endif
# ifdef SIGHUP
case SIGHUP:
exception_str = "SIGHUP";
break;
# endif
# ifdef SIGQUIT
case SIGQUIT:
exception_str = "SIGQUIT";
break;
# endif
# ifdef SIGTRAP
case SIGTRAP:
exception_str = "SIGTRAP";
break;
# endif
# ifdef SIGIOT
# if !defined(SIGABRT) || SIGIOT != SIGABRT
case SIGIOT:
exception_str = "SIGIOT";
break;
# endif
# endif
# ifdef SIGUSR1
case SIGUSR1:
exception_str = "SIGUSR1";
break;
# endif
# ifdef SIGUSR2
case SIGUSR2:
exception_str = "SIGUSR2";
break;
# endif
# ifdef SIGPIPE
case SIGPIPE:
exception_str = "SIGPIPE";
break;
# endif
# ifdef SIGALRM
case SIGALRM:
exception_str = "SIGALRM";
break;
# endif
# ifdef SIGSTKFLT
case SIGSTKFLT:
exception_str = "SIGSTKFLT";
break;
# endif
# ifdef SIGCHLD
case SIGCHLD:
exception_str = "SIGCHLD";
break;
# elif defined(SIGCLD)
case SIGCLD:
exception_str = "SIGCLD";
break;
# endif
# ifdef SIGCONT
case SIGCONT:
exception_str = "SIGCONT";
break;
# endif
# ifdef SIGSTOP
case SIGSTOP:
exception_str = "SIGSTOP";
break;
# endif
# ifdef SIGTSTP
case SIGTSTP:
exception_str = "SIGTSTP";
break;
# endif
# ifdef SIGTTIN
case SIGTTIN:
exception_str = "SIGTTIN";
break;
# endif
# ifdef SIGTTOU
case SIGTTOU:
exception_str = "SIGTTOU";
break;
# endif
# ifdef SIGURG
case SIGURG:
exception_str = "SIGURG";
break;
# endif
# ifdef SIGXCPU
case SIGXCPU:
exception_str = "SIGXCPU";
break;
# endif
# ifdef SIGXFSZ
case SIGXFSZ:
exception_str = "SIGXFSZ";
break;
# endif
# ifdef SIGVTALRM
case SIGVTALRM:
exception_str = "SIGVTALRM";
break;
# endif
# ifdef SIGPROF
case SIGPROF:
exception_str = "SIGPROF";
break;
# endif
# ifdef SIGWINCH
case SIGWINCH:
exception_str = "SIGWINCH";
break;
# endif
# ifdef SIGPOLL
case SIGPOLL:
exception_str = "SIGPOLL";
break;
# endif
# ifdef SIGIO
# if !defined(SIGPOLL) || SIGIO != SIGPOLL
case SIGIO:
exception_str = "SIGIO";
break;
# endif
# endif
# ifdef SIGPWR
case SIGPWR:
exception_str = "SIGPWR";
break;
# endif
# ifdef SIGSYS
case SIGSYS:
exception_str = "SIGSYS";
break;
# endif
# ifdef SIGUNUSED
# if !defined(SIGSYS) || SIGUNUSED != SIGSYS
case SIGUNUSED:
exception_str = "SIGUNUSED";
break;
# endif
# endif
default:
exception_str = cmStrCat("Signal ", this->Signal);
}
#endif
return exception_str;
}
|