forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrl.cpp
More file actions
5122 lines (4308 loc) · 135 KB
/
rl.cpp
File metadata and controls
5122 lines (4308 loc) · 135 KB
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
// rl.cpp
// To build:
// nmake RELEASE=1 rl.exe
//
// Julian Burger
// Microsoft Corporation
// 11/06/98
// Configuration files are XML
//
// rl{asm,exe}dirs.xml have directory lists
// rl{asm,exe}.xml have test lists
//
// General format:
//
// <?xml version="1.0"?>
//
// <regress-asm> // head node, tag unimportant
//
// <test> // test node, tag unimportant
//
// <default> // default test information
//
// test info // see below
//
// </default>
//
// <condition order="1" type="exclude"> // condition node
//
// <target>...</target> // list of targets this condition applies to
//
// </condition>
//
// </test>
//
//
// Test info (valid for all configuration files):
//
// <files>...</files> // list of comma-delimited files
//
// For asm regressions, files may be grouped in a single test node for
// convenience. For exe regressions, all the files comprising a single test
// should be group.
//
// <tags>...</tags> // list of comma-delimited (case-insensitive) tags
//
// Tags are intended as a proper of the test case itself. That is, they
// should be used to indicate features of a test (like C++ EH or x86 asm)
// rather than arbitrary nonsense.
//
// One tag is recognized specifically by RL: "Pogo". This indicates the test
// is a Pogo test and should be run specially. Any other tag is for explicit
// inclusion/exclusion of tests. E.g.
//
// <tags>SEH</tags>
//
// This marks a test with the SEH tag. If -tags:SEH is specified on the command
// line, only tests marked as such would be included. Similarly, -nottags:SEH
// would exclude tests so tagged.
//
// Test info (valid for asm and exe test configuration files):
//
// <compile-flags>...</compile-flags> // compilation flags for this test
// <rl>...</rl> // RL directives (case-insensitive)
//
// Test info (valid for exe test configuration files)
//
// <baseline>...</baseline> // baseline, expected test output
// <link-flags>...</link-flags> // link flags for this test
// <env> // extra env vars for this test
// <envvar1>value1</envvar1> // any number of child nodes with arbitrary data
// ... // <build>release</build>
// // will result in "set build=release" for the test task env
// </env>
//
// <condition> nodes have an explicit order of evaluation and type (action to
// take if the condition applies) which can be either "include" or "exclude".
//
// asm and exe test conditions may contain <override> nodes that allow default
// info to be overridden. E.g.
//
// <condition order="1" type="include">
//
// <target>ia64</target>
//
// <override>
//
// <baseline>ia64-result.out</baseline>
//
// </override>
//
// </condition>
//
// The above would override the default baseline with "ia64-result.out" when
// the target is ia64.
//
//
// Currently, condition nodes may have only a single condition (target or
// compile-flags) and non-target conditions must appear after target
// conditions.
// Tags processing
//
// Tags specified in the same command line argument are treated as OR clauses.
// Multiple -tags (and -nottags) switches are permitted and are processed in
// command line order as AND clauses. E.g.
//
// -tags:Pogo -nottags:SEH,CPPEH -tags:asm
//
// The -tags declarations says: match all tests with "Pogo" tags. From that
// set, exclude those that have "SEH" or "CPPEH". The next -tags arg further
// restricts the set to those with "asm". (This is a contrived example.)
//
// Note that -tags/-nottags are only applied to files. Directories may not
// have tags because they are too error prone.
// Directives
//
// The following directives are recognized in the <rl>...</rl> section of a
// test's default info:
//
// PassFo
// NoAsm
// NoGPF
// Log file format
//
// There are three output files generated by default
// 1. rl.log contains the summary of tests passed and failed in each dir
// 2. rl.results.log contains the passed/failed result of each test variation
// 3. rl.full.log contains all the output from all the tests,
// delimited by +++ <test variation> +++
// Notes:
// 1. The test output in these files is not synchronized on MP,
// without the -sync option.
// 2. rl.results.log can be used as a baseline database to track regressions,
// if -time is not used.
// -genlst mode and lst file format
//
// In -genlst -all mode, rl won't run tests but generate test.lst and env.lst
// containing the same info as in the rlexedirs.xml and rlexe.xml files
// These files are then read by the runall test harness to run the tests.
// Please refer to the runall documentation for the lst file format.
// By default, rl spawns as many threads to execute tests as there are
// processors on the system. Only one thread works per directory because
// running multiple tests in a single directory may cause conflicts in the
// presence of startdir.cmd/enddir.cmd and precompiled header flags (e.g.,
// /YX). The primary thread generates a work queue of directories and files to
// test in each directory. As soon as the tests in a directory are determined,
// the directory becomes available for worker threads.
//
// One complication is that in Windows there is only a single "current
// directory" per process. Thus, we need to synchronize as follows:
//
// 1. Worker threads grab the "directory" critical section immediately before
// spawning a process (e.g., compile, link, etc.),
//
// 2. The process is spawned with the correct current directory, which it
// inherits.
//
// 3. The lock is released and the next worker is free to change the directory.
//
// 4. The primary thread never changes the current directory, to avoid
// synchronization issues.
//
// 5. Any file access (fopen_unsafe, etc.) must use full pathnames. The only place
// relative paths may be used is in commands that are executed via
// ExecuteCommand(), which does a _chdir before creating a new process to
// execute the command.
//
// 6. Global variables used by worker threads must be thread-local or
// properly synchronized. Exceptions are: (a) variables only read, not
// written, by the worker threads, (b) bThreadStop, which is only written
// to TRUE by worker threads, so synchronization is not necessary.
//
// 7. All printf/fprintf output from the worker threads must go through
// the COutputBuffer class, to be properly synchronized. This class buffers
// up output per thread and flushes it at reasonable intervals, normally
// after a test is finished running. This includes any output generated
// by rl as well as any output captured by the pipe output filter on
// spawned processes.
//
// 8. Do not use _putenv() in the worker threads, as it sets process-wide
// state.
#include "rl.h"
#include "strsafe.h"
#pragma warning(disable: 4474) // 'fprintf' : too many arguments passed for format string
// Win64 headers (process.h) has this:
#ifndef _INTPTR_T_DEFINED
#ifdef _WIN64
typedef __int64 intptr_t;
#else
typedef int intptr_t;
#endif
#define _INTPTR_T_DEFINED
#endif
// Target machine information
// Any changes made here should have corresponding name added
// to TARGET_MACHINES in rl.h. NOTE: The TARGET_MACHINES enum is in exactly
// the same order as the TargetInfo array is initialized!
TARGETINFO TargetInfo[] = {
"unknown", TRUE, FALSE, FALSE, NULL, NULL, NULL,
"x86", FALSE, FALSE, TRUE, NULL, NULL, NULL,
"ppcwce", FALSE, FALSE, FALSE, "comcli", NULL, "x86asm",
"wvm", FALSE, FALSE, FALSE, NULL, "vccoree.lib", "x86asm",
"wvmcee", FALSE, FALSE, FALSE, NULL, NULL, "x86asm",
"wvmx86", FALSE, FALSE, FALSE, NULL, NULL, "x86asm",
"mips", FALSE, FALSE, FALSE, NULL, NULL, "x86asm",
"arm", FALSE, FALSE, FALSE, "armsd", "libcnoce.lib", "x86asm",
"thumb", FALSE, FALSE, FALSE, "armsd", "libcnocet.lib", "x86asm",
"arm64", FALSE, FALSE, FALSE, "armsd", "libcnoce.lib", "x86asm",
"sh3", FALSE, FALSE, FALSE, "sh3sim", "libcsim.lib", "x86asm",
"sh4", FALSE, FALSE, FALSE, "sh3sim", "libcsim.lib", "x86asm",
"sh5c", FALSE, FALSE, FALSE, "sh5sim", "libcsim.lib", "x86asm",
"sh5m", FALSE, FALSE, FALSE, "sh5sim", "libcsim.lib", "x86asm",
"ia64", FALSE, TRUE, TRUE, NULL, NULL, "x86asm",
"amd64", FALSE, TRUE, TRUE, NULL, NULL, "x86asm",
"amd64sys", FALSE, FALSE, TRUE, "issuerun", NULL, "x86asm",
"wvm64", FALSE, FALSE, FALSE, NULL, NULL, "x86asm",
"am33", FALSE, FALSE, FALSE, NULL, NULL, "x86asm",
"m32r", FALSE, FALSE, FALSE, NULL, NULL, "x86asm",
"msil", FALSE, FALSE, FALSE, NULL, NULL, "x86asm",
NULL
};
// Target OS information
// Any changes made here should have corresponding name added
// to TARGET_OS in rl.h. NOTE: The TARGET_OS enum is in exactly
// the same order as the TargetOSNames array is initialized
const char* const TargetOSNames[] = {
"unknown",
"win7",
"win8",
"winBlue",
"win10",
"wp8",
NULL
};
#define IS_PATH_CHAR(c) ((c == '/') || (c == '\\'))
#define LOG_FULL_SUFFIX ".full"
const char * const ModeNames[] = {
"assembly", "executable", "shouldn't be printed (RM_DIR)"
};
const char * const InternalModeCmd[] = {
"<asm regress>", "<exe regress>"
};
const char * const TestInfoKindName[] =
{
"files",
"baseline",
"compile-flags",
"link-flags",
"tags",
"rl",
"env",
"command",
"timeout",
"sourcepath",
NULL
};
const char * const DirectiveNames[] =
{
"PassFo",
"NoAsm",
"NoGPF",
NULL
};
//
// Global variables set before worker threads start, and only accessed
// (not set) by the worker threads.
//
TARGET_MACHINES TargetMachine = DEFAULT_TM;
TARGET_MACHINES RLMachine = DEFAULT_TM;
TARGET_OS TargetOS = DEFAULT_OS;
Tags * TagsList = NULL;
Tags * TagsLast = NULL;
Tags* DirectoryTagsList = NULL;
Tags* DirectoryTagsLast = NULL;
char SavedConsoleTitle[BUFFER_SIZE];
const char *DIFF_DIR;
char *REGRESS = NULL, *MASTER_DIR, *TARGET_MACHINE, *RL_MACHINE, *TARGET_OS_NAME = NULL;
const char *REGR_CL, *REGR_DIFF;
char *REGR_ASM, *REGR_SHOWD;
const char *TARGET_VM;
char *EXTRA_CC_FLAGS, *EXEC_TESTS_FLAGS;
const char *LINKER, *LINKFLAGS;
char *CL, *_CL_;
const char *JCBinary = "jshost.exe";
BOOL FStatus = TRUE;
char *StatusPrefix;
const char *StatusFormat;
BOOL FVerbose;
BOOL FQuiet;
BOOL FNoWarn;
BOOL FTest;
BOOL FLow;
BOOL FNoDelete;
BOOL FCopyOnFail;
BOOL FSummary = TRUE;
BOOL FMoveDiffs;
BOOL FNoMoveDiffsSwitch;
BOOL FRelativeLogPath;
BOOL FNoDirName = TRUE;
BOOL FBaseline;
BOOL FRebase = FALSE;
BOOL FDiff;
BOOL FBaseDiff;
BOOL FSyncEnumDirs;
BOOL FNogpfnt = TRUE;
BOOL FAppend;
BOOL FAppendTestNameToExtraCCFlags = FALSE;
#ifndef NODEBUG
BOOL FDebug;
#endif
// Output synchronization options
BOOL FSyncImmediate = FALSE; // flush output immediately---no buffering
BOOL FSyncVariation = FALSE; // flush after every test variation (default)
BOOL FSyncTest = FALSE; // flush after all variations of a test
BOOL FSyncDir = FALSE; // flush after an entire directory
BOOL FSingleThreadPerDir = FALSE; // Perform all tests within each directory on a single thread.
BOOL FSingleDir = FALSE;
BOOL FNoThreadId = FALSE;
char* ExeCompiler = NULL;
char* BaseCompiler = NULL;
char* DiffCompiler = NULL;
RLMODE Mode = DEFAULT_RLMODE;
char *DCFGfile = NULL;
char const *CFGfile = NULL;
char const *CMDfile = NULL;
int CFGline;
#define MAX_ALLOWED_THREADS 10 // must be <= MAXIMUM_WAIT_OBJECTS (64)
unsigned NumberOfThreads = 0;
TestList DirList, ExcludeDirList;
BOOL FUserSpecifiedFiles = FALSE;
BOOL FUserSpecifiedDirs = TRUE;
BOOL FExcludeDirs = FALSE;
BOOL FGenLst = FALSE;
char *ResumeDir, *MatchDir;
TIME_OPTION Timing = TIME_DIR | TIME_TEST; // Default to report times at test and directory level
static const char *ProgramName;
static const char *LogName;
static const char *FullLogName;
static const char *ResultsLogName;
// NOTE: this might be unused now
static char TempPath[MAX_PATH] = ""; // Path for temporary files
//
// Global variables read and written by the worker threads: these need to
// either be protected by synchronization or use thread-local storage.
//
// Ctrl-C or done? This is written by worker threads or the main thread, but
// only to TRUE. Once written to TRUE, it never changes again. Threads notice
// its new state by polling it periodically, and shutting down gracefully if
// it is TRUE.
BOOL bThreadStop = FALSE;
char TitleStatus[BUFFER_SIZE]; // protected by csTitleBar
CRITICAL_SECTION csTitleBar; // used to serialize title bar
CRITICAL_SECTION csStdio; // printf serialization
CRITICAL_SECTION csCurrentDirectory; // used when changing current directory
CProtectedLong NumVariationsRun[RLS_COUNT];
CProtectedLong NumVariationsTotal[RLS_COUNT]; // run / total is displayed
CProtectedLong NumFailuresTotal[RLS_COUNT];
CProtectedLong NumDiffsTotal[RLS_COUNT];
CProtectedLong MaxThreads;
CProtectedLong CntDeleteFileFailures;
CProtectedLong CntDeleteFileFatals;
// Under -syncdirs, enumerate all directories/files before allowing
// any work to be done. Do this by setting this event when done enumerating.
// This releases the worker threads.
CHandle heventDirsEnumerated;
CDirectoryQueue DirectoryQueue;
CDirectoryQueue DirectoryTestQueue;
CDirectoryAndTestCaseQueue DirectoryAndTestCaseQueue;
CThreadInfo* ThreadInfo = NULL;
// A per-thread id. ThreadId 0 is the primary thread, the directory/file
// enumeration thread. Other threads are numbered 1 to NumberOfThreads
__declspec(thread) int ThreadId = 0;
__declspec(thread) char *TargetVM;
__declspec(thread) COutputBuffer* ThreadOut; // stdout
__declspec(thread) COutputBuffer* ThreadLog; // log file
__declspec(thread) COutputBuffer* ThreadFull; // full log file
__declspec(thread) COutputBuffer* ThreadRes; // results log file
// Per-thread compare buffers, allocated once, deleted on thread destroy
#define CMPBUF_SIZE 65536
__declspec(thread) char *cmpbuf1 = NULL;
__declspec(thread) char *cmpbuf2 = NULL;
// Allow usage of select deprecated CRT APIs without disabling all deprecated CRT API warnings
#pragma warning (push)
#pragma warning (disable:4996)
char * getenv_unsafe(const char * varName)
{
// Use getenv instead of getenv_s or _dupenv_s to simplify calls to the API.
return getenv(varName);
}
FILE * fopen_unsafe(const char * filename, const char * mode)
{
// Using fopen_s leads to EACCES error being returned occasionally, even when contentious
// fopen/fclose pairs are wrapped in a critical section. Unclear why this is happening.
// Use deprecated fopen instead.
_set_errno(0);
return fopen(filename, mode);
}
char* strerror_unsafe(int errnum)
{
return strerror(errnum);
}
#pragma warning (pop)
//////////////////////////////////////////////////////////////////////////////
void
CleanUp(BOOL fKilled)
{
if (FStatus)
SetConsoleTitle(SavedConsoleTitle);
// Try to remove temporary files. The temp files may
// be in use, so we might not be able to.
if (ThreadInfo != NULL) { // if we've gotten through parsing commands
for (unsigned int i = 0; i <= NumberOfThreads; i++) {
ThreadInfo[i].DeleteTmpFileList();
}
}
if (FRLFE)
RLFEDisconnect(fKilled);
}
void __cdecl
NormalCleanUp(void)
{
CleanUp(FALSE);
}
int __stdcall
NT_handling_function(unsigned long /* dummy -- unused */)
{
fprintf(stderr, "Exiting...\n");
fflush(stdout);
fflush(stderr);
CleanUp(TRUE);
// For now, just exit ungracefully. This will probably cause bad
// things to happen with output filtering, since we've
// just killed the pipe.
ExitProcess(1);
#if _MSC_VER<1300
return 1; // avoid C4716 warning caused by no return on ExitProcess
#endif
}
void
assert(
const char *file,
int line
)
{
fprintf(stderr, "Assertion failed on line %d of %s\n",
line, file);
}
//////////////////////////////////////////////////////////////////////////////
//
// Output functions. All worker thread output must be generated using these
// functions, then flushed at appropriate times based on the -sync option.
//
void
__cdecl Fatal(const char *fmt, ...)
{
va_list arg_ptr;
va_start(arg_ptr, fmt);
fprintf(stderr, "Error: ");
vfprintf(stderr, fmt, arg_ptr);
fputc('\n', stderr);
exit(1);
}
void
__cdecl Warning(const char *fmt, ...)
{
va_list arg_ptr;
char buf[50], tempBuf[BUFFER_SIZE];
ASSERT(ThreadOut != NULL);
buf[0] = '\0';
if (!FNoThreadId && ThreadId != 0 && NumberOfThreads > 1) {
sprintf_s(buf, "%d>", ThreadId);
}
if (!FNoWarn) {
va_start(arg_ptr, fmt);
vsprintf_s(tempBuf, fmt, arg_ptr);
ASSERT(strlen(tempBuf) < BUFFER_SIZE);
ThreadOut->Add("%sWarning: %s\n", buf, tempBuf);
ThreadFull->Add("%sWarning: %s\n", buf, tempBuf);
}
}
void
__cdecl Message(const char *fmt, ...)
{
va_list arg_ptr;
char buf[50], tempBuf[BUFFER_SIZE];
ASSERT(ThreadOut != NULL);
buf[0] = '\0';
if (!FNoThreadId && ThreadId != 0 && NumberOfThreads > 1) {
sprintf_s(buf, "%d>", ThreadId);
}
va_start(arg_ptr, fmt);
vsprintf_s(tempBuf, fmt, arg_ptr);
ASSERT(strlen(tempBuf) < BUFFER_SIZE);
ThreadOut->Add("%s%s\n", buf, tempBuf);
ThreadFull->Add("%s%s\n", buf, tempBuf);
}
void
__cdecl WriteLog(const char *fmt, ...)
{
va_list arg_ptr;
char buf[50], tempBuf[BUFFER_SIZE];
ASSERT(ThreadLog != NULL);
buf[0] = '\0';
if (!FNoThreadId && ThreadId != 0 && NumberOfThreads > 1) {
sprintf_s(buf, "%d>", ThreadId);
}
va_start(arg_ptr, fmt);
vsprintf_s(tempBuf, fmt, arg_ptr);
ASSERT(strlen(tempBuf) < BUFFER_SIZE);
ThreadLog->Add("%s%s\n", buf, tempBuf);
}
void
__cdecl LogOut(const char *fmt, ...)
{
va_list arg_ptr;
char buf[50], tempBuf[BUFFER_SIZE];
ASSERT(ThreadOut != NULL);
ASSERT(ThreadLog != NULL);
buf[0] = '\0';
if (!FNoThreadId && ThreadId != 0 && NumberOfThreads > 1) {
sprintf_s(buf, "%d>", ThreadId);
}
va_start(arg_ptr, fmt);
vsprintf_s(tempBuf, fmt, arg_ptr);
ASSERT(strlen(tempBuf) < BUFFER_SIZE);
ThreadOut->Add("%s%s\n", buf, tempBuf);
ThreadLog->Add("%s%s\n", buf, tempBuf);
ThreadFull->Add("%s%s\n", buf, tempBuf);
}
void
__cdecl LogError(const char *fmt, ...)
{
va_list arg_ptr;
char buf[50], tempBuf[BUFFER_SIZE];
ASSERT(ThreadOut != NULL);
ASSERT(ThreadLog != NULL);
buf[0] = '\0';
if (!FNoThreadId && ThreadId != 0 && NumberOfThreads > 1) {
sprintf_s(buf, "%d>", ThreadId);
}
va_start(arg_ptr, fmt);
vsprintf_s(tempBuf, fmt, arg_ptr);
ASSERT(strlen(tempBuf) < BUFFER_SIZE);
ThreadOut->Add("%sError: %s\n", buf, tempBuf);
ThreadLog->Add("%sError: %s\n", buf, tempBuf);
ThreadFull->Add("%sError: %s\n", buf, tempBuf);
}
// Helper function to flush all thread output.
__inline void FlushOutput(
void
)
{
ThreadOut->Flush();
ThreadLog->Flush();
ThreadFull->Flush();
ThreadRes->Flush();
}
//////////////////////////////////////////////////////////////////////////////
//
// Various DeleteFile() wrappers. We have had lots of problems where
// DeleteFile() fails with error 5 -- access denied. It's not clear why this
// is: it seems like an OS bug where the handle to a process is not released
// when the process handle becomes signaled. Perhaps there is a process that
// sits around grabbing handles. It seems to be worse with CLR testing and
// issuerun/readrun cross-compiler testing. To deal with this, loop and sleep
// between delete tries for some calls. Keep track of how many failures there
// were. Don't clog the log files with "error 5" unless the
// sleep doesn't fix the problem, or the user specifies verbose.
BOOL
DeleteFileIfFoundInternal(
const char* filename
)
{
BOOL ok;
ASSERT(filename != NULL);
if (FTest)
{
return TRUE;
}
// We could see if it's there and then try to delete it. It's easier to
// just try deleting it and see what happens.
ok = DeleteFile(filename);
if (!ok && (GetLastError() != ERROR_FILE_NOT_FOUND)) {
CntDeleteFileFailures++;
return FALSE;
}
return TRUE;
}
BOOL
DeleteFileIfFound(
const char* filename
)
{
BOOL ok;
ok = DeleteFileIfFoundInternal(filename);
if (!ok) {
LogError("DeleteFileIfFound: Unable to delete file %s - error %d", filename, GetLastError());
return FALSE;
}
return TRUE;
}
// Call Win32's DeleteFile(), and print an error if it fails
void
DeleteFileMsg(
char *filename
)
{
if (FTest)
{
return;
}
BOOL ok;
ok = DeleteFile(filename);
if (!ok) {
CntDeleteFileFailures++;
LogError("DeleteFileMsg: Unable to delete file %s - error %d", filename, GetLastError());
}
}
// Call Win32's DeleteFile(), and print an error if it fails. Retry a few
// times, just to improve robustness. NOTE: This function will fail (after
// retries) if the file does not exist!
#define MAX_DELETE_FILE_TRIES 10
#define DELETE_FILE_INITIAL_INTERVAL_IN_MS 100
#define DELETE_FILE_DELTA_IN_MS 100
void
DeleteFileRetryMsg(
char *filename
)
{
unsigned tries = 1;
BOOL ok;
DWORD dwWait = DELETE_FILE_INITIAL_INTERVAL_IN_MS;
for (;;) {
ok = DeleteFileIfFoundInternal(filename);
if (ok)
break;
if (FVerbose) {
LogError("DeleteFileRetryMsg: Unable to delete file %s - error %d", filename, GetLastError());
}
if (++tries > MAX_DELETE_FILE_TRIES) {
LogError("Giving up trying to delete file %s. Further related errors are likely", filename);
CntDeleteFileFatals++;
break;
}
Sleep(dwWait);
dwWait += DELETE_FILE_DELTA_IN_MS;
}
}
void
DeleteMultipleFiles(
CDirectory* pDir,
const char* pattern
)
{
WIN32_FIND_DATA findData;
HANDLE h;
char full[MAX_PATH];
sprintf_s(full, "%s\\%s", pDir->GetDirectoryPath(), pattern);
// Read filenames...
if ((h = FindFirstFile(full, &findData)) == INVALID_HANDLE_VALUE)
return;
do {
// FindFirstFile/FindNextFile set cFileName to a file name without a
// path. We need to prepend the directory and use a full path to do
// the delete. This is because multithreaded rl might have changed the
// current directory out from underneath us.
sprintf_s(full, "%s\\%s", pDir->GetDirectoryPath(), findData.cFileName);
if (FVerbose)
Message("Deleting %s\n", full);
DeleteFileRetryMsg(full);
} while (FindNextFile(h, &findData));
FindClose(h);
}
char *
GetFilenamePtr(
char *path
)
{
char *s;
s = strrchr(path, '\\');
if (s)
return s + 1;
return path;
}
// Get extension of a filename, or "" if no extension found.
const char* GetFilenameExt(const char *path)
{
const char* ext = strrchr(path, '.');
return ext ? ext : "";
}
char *
mytmpnam(
const char *directory,
const char *prefix,
char *filename
)
{
UINT r;
char threadPrefix[MAX_PATH]; // make the prefix thread-specific
// Note that GetTempFileName only uses the first 3 characters of the
// prefix. This should be okay, because it will still create a unique
// filename.
sprintf_s(threadPrefix, "%s%X", prefix, ThreadId);
// NOTE: GetTempFileName actually creates a file when it succeeds.
r = GetTempFileNameA(directory, threadPrefix, 0, filename);
if (r == 0) {
return NULL;
}
return filename;
}
//
// It is possible antivirus program on dev box locked testout when we want to compare testout with baseline.
// If this happens, delay and try a few more times.
//
HANDLE OpenFileToCompare(char *file)
{
const int MAX_TRY = 1200;
const DWORD NEXT_TRY_SLEEP_MS = 250;
HANDLE h;
int i = 0;
for (;;)
{
// Open the files using exclusive access
h = CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (h != INVALID_HANDLE_VALUE)
{
break;
}
DWORD lastError = GetLastError();
if (++i < MAX_TRY && lastError == ERROR_SHARING_VIOLATION)
{
Sleep(NEXT_TRY_SLEEP_MS);
continue;
}
LogError("Unable to open file %s - error %d", file, lastError);
break;
}
return h;
}
// Do a quick file equality comparison using pure Win32 functions. (Avoid
// using CRT functions; the MT CRT seems to have locking/flushing problems on
// MP boxes.)
int
DoCompare(
char *file1,
char *file2
)
{
CHandle h1, h2; // automatically closes open handles
DWORD count1, count2;
BOOL b1, b2;
DWORD size1, size2;
// Allocate large buffers for doing quick file comparisons.
if (cmpbuf1 == NULL) {
// initialize the buffers
cmpbuf1 = new char[CMPBUF_SIZE];
cmpbuf2 = new char[CMPBUF_SIZE];
if ((cmpbuf1 == NULL) || (cmpbuf2 == NULL))
Fatal("new failed");
}
h1 = OpenFileToCompare(file1);
if (h1 == INVALID_HANDLE_VALUE) {
return -1;
}
h2 = OpenFileToCompare(file2);
if (h2 == INVALID_HANDLE_VALUE) {
return -1;
}
// Short circuit by first checking for different file lengths.
size1 = GetFileSize(h1, NULL); // assume < 4GB files
if (size1 == 0xFFFFFFFF) {
LogError("Unable to get file size for %s - error %d", file1, GetLastError());
return -1;
}
size2 = GetFileSize(h2, NULL);
if (size2 == 0xFFFFFFFF) {
LogError("Unable to get file size for %s - error %d", file2, GetLastError());
return -1;
}
if (size1 != size2) { // not equal; don't bother reading the files
#ifndef NODEBUG
if (FDebug)
printf("DoCompare shows %s and %s are NOT equal (different size)\n", file1, file2);
#endif
return 1;
}
do {
b1 = ReadFile(h1, cmpbuf1, CMPBUF_SIZE, &count1, NULL);
b2 = ReadFile(h2, cmpbuf2, CMPBUF_SIZE, &count2, NULL);
if (!b1 || !b2) {
LogError("ReadFile failed doing compare of %s and %s", file1, file2);
return -1;
}
if (count1 != count2 || memcmp(cmpbuf1, cmpbuf2, count1) != 0) {
#ifndef NODEBUG
if (FDebug)
printf("DoCompare shows %s and %s are NOT equal (contents differ)\n", file1, file2);
#endif
return 1;
}
} while (count1 == CMPBUF_SIZE);
#ifndef NODEBUG
if (FDebug)
printf("DoCompare shows %s and %s are equal\n", file1, file2);
#endif
return 0;
}
char *
FormatString(
const char *format
)
{
static char buf[BUFFER_SIZE + 32]; // extra in case a sprintf_s goes over
int i;
i = 0;
while (*format) {
if (*format != '%') {
buf[i++] = *format;
}
else {
switch (*++format) {
case 'd':
i += sprintf_s(&buf[i], BUFFER_SIZE + 32 - i, "%d", (int32)NumDiffsTotal[RLS_TOTAL]);
break;
case 'f':
i += sprintf_s(&buf[i], BUFFER_SIZE + 32 - i, "%d", (int32)NumFailuresTotal[RLS_TOTAL]);
break;
case 't':
i += sprintf_s(&buf[i], BUFFER_SIZE + 32 - i, "%d", (int32)NumVariationsRun[RLS_TOTAL]);
break;
case 'T':
i += sprintf_s(&buf[i], BUFFER_SIZE + 32 - i, "%d", (int32)NumVariationsTotal[RLS_TOTAL]);
break;
case 'p':
if ((int32)NumVariationsTotal[RLS_TOTAL]) {
i += sprintf_s(&buf[i], BUFFER_SIZE + 32 - i, "%d",
100 * (int32)NumVariationsRun[RLS_TOTAL] /
(int32)NumVariationsTotal[RLS_TOTAL]);
}
else {
i += sprintf_s(&buf[i], BUFFER_SIZE + 32 - i, "--");
}
break;
default:
buf[i++] = *format;
}
}
format++;
if (i > BUFFER_SIZE)
break;