Repository navigation
Expand file tree
/
Copy pathvalidation_driver.cpp
More file actions
147 lines (131 loc) · 4.99 KB
/
Copy pathvalidation_driver.cpp
File metadata and controls
147 lines (131 loc) · 4.99 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
/*
* Copyright 2026 CEA (www.cea.fr)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ============================================================================
// The main() of a grouped validation executable (see validation_harness.h).
// ============================================================================
//
// The harnesses of the executable are listed by VALIDATION_HARNESS_LIST, a
// header CMake writes per target, one VALIDATION_HARNESS(name) line each. The
// first argument names the one to run; the rest is its own command line.
// With a single harness (CHUNK_VALIDATION_UNITY=OFF: one executable per
// harness) the name may be left out.
// ============================================================================
#include <cstdio> // std::fprintf, std::perror, stderr
#include <cstdlib> // std::getenv, std::atexit, std::abort, mkdtemp (POSIX)
#include <cstring> // std::strcmp
#include <filesystem> // std::filesystem::temp_directory_path, remove_all (temp_path)
#include <iterator> // std::size
#include <string> // std::string (self_command, temp_path)
#include <system_error> // std::error_code (temp_path's cleanup)
#ifdef __linux__
#include <sys/prctl.h> // prctl(PR_SET_DUMPABLE)
#endif
#include "validation_harness.h" // validation::self_command (defined here)
#define VALIDATION_HARNESS(name) \
namespace validation::name \
{ \
int run(int argc, char** argv); \
}
#include VALIDATION_HARNESS_LIST // one VALIDATION_HARNESS(name) per harness: declares its run()
#undef VALIDATION_HARNESS
namespace
{
struct Harness
{
const char* name;
int (*run)(int, char**);
};
const Harness harness_table[] = {
#define VALIDATION_HARNESS(name) {#name, &validation::name::run},
#include VALIDATION_HARNESS_LIST // one VALIDATION_HARNESS(name) per harness: its table entry
#undef VALIDATION_HARNESS
};
constexpr int n_harnesses = static_cast<int>(std::size(harness_table));
const char* g_running = nullptr; // the harness name given on the command line, if any
} // namespace
std::string validation::self_command(const char* argv0)
{
// CHUNK_VALIDATION_CHILD=1: the child is one this harness runs to watch it
// fail -- most of them abort on purpose -- and main() makes it
// non-dumpable (see there).
std::string cmd = std::string("CHUNK_VALIDATION_CHILD=1 \"") + argv0 + "\"";
if (g_running != nullptr)
{
cmd += std::string(" ") + g_running;
}
return cmd;
}
namespace
{
std::string g_temp_dir; // temp_path()'s directory, removed at exit
} // namespace
std::string validation::temp_path(const std::string& name)
{
if (g_temp_dir.empty())
{
std::string dir = (std::filesystem::temp_directory_path() / "shrink_validation.XXXXXX").string();
if (::mkdtemp(dir.data()) == nullptr)
{
std::perror("validation: mkdtemp");
std::abort();
}
g_temp_dir = dir;
std::atexit(
[]
{
std::error_code ec; // best effort: a leftover directory is not a failure
std::filesystem::remove_all(g_temp_dir, ec);
});
}
return g_temp_dir + "/" + name;
}
int main(int argc, char** argv)
{
#ifdef __linux__
// A child run through self_command() aborts on purpose. Dumpable, every
// such abort is a core dump: under WSL each one is captured and written to
// Windows' %TEMP%\wsl-crashes, some 300 MB apiece for an executable whose
// arenas reserve that much address space. stale_proxy_validation's 1- and
// 8-thread runs fire dozens back to back -- 261 captures, 7.7 GB, and WSL
// went down mid-capture. A non-dumpable process writes no core at all.
if (std::getenv("CHUNK_VALIDATION_CHILD") != nullptr)
{
prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
}
#endif
if (argc > 1)
{
for (const Harness& harness : harness_table)
{
if (std::strcmp(argv[1], harness.name) == 0)
{
g_running = harness.name;
argv[1] = argv[0]; // the harness sees argv[0] = the executable, then its own arguments
return harness.run(argc - 1, argv + 1);
}
}
}
if (n_harnesses == 1)
{
return harness_table[0].run(argc, argv);
}
std::fprintf(stderr, "usage: %s <harness> [args...]\nharnesses:\n", argv[0]);
for (const Harness& harness : harness_table)
{
std::fprintf(stderr, " %s\n", harness.name);
}
return 2;
}