-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_process_run_exec.cpp
More file actions
276 lines (244 loc) · 11.7 KB
/
Copy pathtest_process_run_exec.cpp
File metadata and controls
276 lines (244 loc) · 11.7 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
#include <gtest/gtest.h>
#include <cerrno>
#include <cstdlib>
#include <fstream>
import std;
import mcpp.platform.process;
using namespace mcpp::platform;
// These exercise the POSIX launch path with POSIX program paths (/bin/true,
// /bin/sh, /bin/echo) — Linux and macOS share the direct posix_spawn path
// (#248 launcher-unify): extraEnv goes into the child's envp only, so the
// parent environment is never mutated. The Windows path is covered by the
// integration build (ninja launched via capture_exec).
#if !defined(_WIN32)
// The regression that matters: launching with an injected loader var must NOT
// mutate the parent (mcpp) environment — that mutation is exactly what leaked
// into /bin/sh and crashed it on newer-glibc hosts.
TEST(RunExec, DoesNotMutateParentEnvironment) {
::setenv("MCPP_TEST_LEAK", "sentinel", 1);
// Use /bin/sh (present on Linux AND macOS) — /bin/true lives at
// /usr/bin/true on macOS, so it is not a portable launch target.
int rc = process::run_exec({"/bin/sh", "-c", "exit 0"},
{{"MCPP_TEST_LEAK", "injected"}});
EXPECT_EQ(rc, 0);
const char* v = ::getenv("MCPP_TEST_LEAK");
ASSERT_NE(v, nullptr);
EXPECT_STREQ(v, "sentinel"); // parent unchanged → no leak
}
TEST(RunExec, ChildSeesInjectedEnv) {
// `sh -c '[ "$X" = injected ]'` exits 0 only if the child received X.
int rc = process::run_exec(
{"/bin/sh", "-c", "[ \"$MCPP_TEST_INJECT\" = injected ]"},
{{"MCPP_TEST_INJECT", "injected"}});
EXPECT_EQ(rc, 0);
}
TEST(RunExec, PropagatesChildExitCode) {
EXPECT_EQ(process::run_exec({"/bin/sh", "-c", "exit 7"}), 7);
}
TEST(RunExec, ReturnsErrorWhenProgramMissing) {
EXPECT_NE(process::run_exec({"/no/such/program/mcpp-xyz"}), 0);
}
TEST(CaptureExec, MissingProgramReportsSpawnFailure) {
constexpr std::string_view missing = "/no/such/program/mcpp-capture-xyz";
auto r = process::capture_exec({std::string(missing)});
EXPECT_EQ(r.exit_code, 127);
EXPECT_NE(r.output.find(missing), std::string::npos) << r.output;
EXPECT_NE(r.output.find("error 2"), std::string::npos) << r.output;
}
TEST(CaptureExec, CapturesStdoutWithoutShell) {
auto r = process::capture_exec({"/bin/echo", "hello world"});
EXPECT_EQ(r.exit_code, 0);
EXPECT_EQ(r.output, "hello world\n");
}
TEST(CaptureExec, CapturesStderrCombined) {
// stderr must be merged into output (replaces the old `2>&1`), otherwise
// ninja's error text would be lost from the fast-path stale detector.
auto r = process::capture_exec({"/bin/sh", "-c", "echo oops 1>&2"});
EXPECT_EQ(r.exit_code, 0);
EXPECT_EQ(r.output, "oops\n");
}
// #248 regression: extraEnv AND cwd BOTH non-empty — the previously untested
// combination (every prior call site passed at most one of the two; the only
// env+cwd caller is the build.mcpp child). The old macOS shell fallback built
// `KEY='v' cd <cwd> && prog 2>&1`, and POSIX binds the env assignments to the
// first command (`cd`) across `&&` — so the real program received NO extra env
// and build.mcpp lost the whole MCPP_* contract. Linux and macOS now share the
// posix_spawn path, so this locks the contract on both.
TEST(CaptureExec, EnvAndCwdCombinedBothReachChild) {
auto dir = std::filesystem::temp_directory_path() / "mcpp_248_env_cwd";
std::filesystem::create_directories(dir);
auto r = process::capture_exec(
{"/bin/sh", "-c", "printf '%s|%s' \"$MCPP_TEST_CONTRACT\" \"$PWD\""},
{{"MCPP_TEST_CONTRACT", "hello"}},
dir.string());
EXPECT_EQ(r.exit_code, 0);
// Env value must reach the child (the old prefix bound it to `cd` only).
EXPECT_NE(r.output.find("hello|"), std::string::npos) << r.output;
// Child must actually run in `cwd`. Compare the leaf name, not the full
// path: macOS resolves /var → /private/var in $PWD.
EXPECT_NE(r.output.find("mcpp_248_env_cwd"), std::string::npos) << r.output;
}
// Companion check for run_exec (no cwd parameter there, but extraEnv must
// still land in the child via the same merged_environ posix_spawn path).
TEST(RunExec, InjectedEnvSurvivesShellChildChain) {
int rc = process::run_exec(
{"/bin/sh", "-c", "[ \"$MCPP_TEST_CONTRACT\" = hello ]"},
{{"MCPP_TEST_CONTRACT", "hello"}});
EXPECT_EQ(rc, 0);
}
// ── spawn failures are typed, reported once, and never retried (#544) ──────
//
// The invariant the bounded launcher's DeadlineRun comment states ("could not
// spawn" and "ran and failed" must not share an exit code) was defeated one
// layer down: run_exec turned every posix_spawnp failure into a bare 127 with
// nothing printed, and both deadline wrappers fell back to it, spawning a
// second time and discarding the first errno. `mcpp run --target
// aarch64-linux-musl` on a host without an emulator printed "Running …", a
// blank line, and exited 1.
TEST(RunExec, MissingProgramReportsOnStderrWhenCallerDoesNotAsk) {
testing::internal::CaptureStderr();
int rc = process::run_exec({"/no/such/program/mcpp-run-xyz"});
auto err = testing::internal::GetCapturedStderr();
EXPECT_EQ(rc, 127);
EXPECT_NE(err.find("/no/such/program/mcpp-run-xyz"), std::string::npos) << err;
EXPECT_NE(err.find("error 2"), std::string::npos) << err;
}
TEST(RunExec, MissingProgramTypesErrnoWhenCallerAsks) {
int spawnErr = 0;
testing::internal::CaptureStderr();
int rc = process::run_exec({"/no/such/program/mcpp-run-xyz"}, {}, &spawnErr);
auto err = testing::internal::GetCapturedStderr();
EXPECT_EQ(rc, 127);
EXPECT_EQ(spawnErr, ENOENT);
EXPECT_TRUE(err.empty()) << err; // the caller owns the report
}
#if defined(__linux__)
// Linux only, and that is a MEASUREMENT (CI, macOS 14 ARM64, 2026-09-02): on
// macOS posix_spawnp handles ENOEXEC the way execvp does — it runs the file
// through /bin/sh — so the child IS spawned (spawn_error stays 0) and it is the
// shell that exits non-zero. The kernel's own refusal of a wrong-architecture
// Mach-O (EBADARCH) is a different reading and is not what this file
// synthesises. `mcpp run`'s "this host cannot execute" typing therefore fires
// on Linux for any unloadable file and on macOS only for a real foreign
// binary; see the design's open question 1.
TEST(RunExec, UnloadableArtifactIsENOEXEC) {
// An executable file with no loader magic: the kernel refuses it with
// ENOEXEC. glibc's posix_spawnp does not retry through /bin/sh the way
// execvp does, so the errno reaches the caller untouched. This is the
// reading `mcpp run` turns into "this host cannot execute the artifact".
auto dir = std::filesystem::temp_directory_path() / "mcpp-enoexec-test";
std::filesystem::create_directories(dir);
auto f = dir / "not-an-elf";
{ std::ofstream o(f, std::ios::binary); o << "\x7f" "NOT" "\x00\x00\x00\x00"; }
std::filesystem::permissions(f, std::filesystem::perms::owner_all);
int spawnErr = 0;
int rc = process::run_exec({f.string()}, {}, &spawnErr);
EXPECT_EQ(rc, 127);
EXPECT_EQ(spawnErr, ENOEXEC);
}
#endif
TEST(RunExec, SpawnedChildLeavesSpawnErrorZero) {
int spawnErr = -1;
EXPECT_EQ(process::run_exec({"/bin/sh", "-c", "exit 7"}, {}, &spawnErr), 7);
EXPECT_EQ(spawnErr, 0);
}
TEST(CaptureExec, MissingProgramTypesErrnoWhenCallerAsks) {
int spawnErr = 0;
auto r = process::capture_exec({"/no/such/program/mcpp-capture-typed"}, {}, {}, &spawnErr);
EXPECT_EQ(r.exit_code, 127);
EXPECT_EQ(spawnErr, ENOENT);
EXPECT_TRUE(r.output.empty()) << r.output;
}
TEST(RunExecDeadline, SpawnFailureIsTypedAndNotRetried) {
int spawnErr = 0;
bool timedOut = true;
testing::internal::CaptureStderr();
int rc = process::run_exec_deadline({"/no/such/program/mcpp-dl-xyz"}, {},
std::chrono::milliseconds(5000), &timedOut,
&spawnErr);
auto err = testing::internal::GetCapturedStderr();
EXPECT_EQ(rc, 127);
EXPECT_EQ(spawnErr, ENOENT);
EXPECT_FALSE(timedOut);
EXPECT_TRUE(err.empty()) << err;
}
TEST(RunExecDeadline, SpawnFailureIsReportedWhenCallerDoesNotAsk) {
bool timedOut = true;
testing::internal::CaptureStderr();
int rc = process::run_exec_deadline({"/no/such/program/mcpp-dl-untyped"}, {},
std::chrono::milliseconds(5000), &timedOut);
auto err = testing::internal::GetCapturedStderr();
EXPECT_EQ(rc, 127);
EXPECT_FALSE(timedOut);
EXPECT_NE(err.find("mcpp-dl-untyped"), std::string::npos) << err;
EXPECT_NE(err.find("error 2"), std::string::npos) << err;
}
TEST(CaptureExecDeadline, SpawnFailureIsTypedInOutcome) {
int spawnErr = 0;
bool timedOut = true;
auto r = process::capture_exec_deadline({"/no/such/program/mcpp-cdl-xyz"}, {},
std::chrono::milliseconds(5000), &timedOut,
{}, &spawnErr);
EXPECT_EQ(r.exit_code, 127);
EXPECT_EQ(spawnErr, ENOENT);
EXPECT_FALSE(timedOut);
EXPECT_TRUE(r.output.empty()) << r.output;
}
TEST(CaptureExecDeadline, SpawnFailureIsInOutputWhenCallerDoesNotAsk) {
bool timedOut = true;
auto r = process::capture_exec_deadline({"/no/such/program/mcpp-cdl-untyped"}, {},
std::chrono::milliseconds(5000), &timedOut);
EXPECT_EQ(r.exit_code, 127);
EXPECT_NE(r.output.find("mcpp-cdl-untyped"), std::string::npos) << r.output;
EXPECT_NE(r.output.find("error 2"), std::string::npos) << r.output;
}
// ── capture_stdout: the argument-vector form of `<program> 2>/dev/null` ──
TEST(CaptureStdout, CapturesStandardOutputOnly) {
auto r = process::capture_stdout(
{"/bin/sh", "-c", "echo out; echo 9.9.9-on-stderr 1>&2"});
EXPECT_EQ(r.exit_code, 0);
EXPECT_EQ(r.output, "out\n");
}
TEST(CaptureStdout, PropagatesExitCode) {
auto r = process::capture_stdout({"/bin/sh", "-c", "echo partial; exit 3"});
EXPECT_EQ(r.exit_code, 3);
EXPECT_EQ(r.output, "partial\n");
}
// A probe parses what it captured, so a launcher message must never be read
// as the program's answer: a missing program leaves the output empty.
TEST(CaptureStdout, MissingProgramLeavesOutputEmpty) {
int spawnErr = 0;
auto r = process::capture_stdout({"/no/such/program/mcpp-probe-2026.1.2.3"},
{}, &spawnErr);
EXPECT_EQ(r.exit_code, 127);
EXPECT_EQ(spawnErr, ENOENT);
EXPECT_TRUE(r.output.empty()) << r.output;
}
TEST(CaptureStdout, StandardInputIsEmpty) {
// `cat` returns at once with nothing when stdin is /dev/null; with an
// inherited terminal or pipe it would block or echo the parent's input.
auto r = process::capture_stdout({"/bin/sh", "-c", "cat; echo done"});
EXPECT_EQ(r.exit_code, 0);
EXPECT_EQ(r.output, "done\n");
}
TEST(CaptureStdout, ExtraEnvironmentReachesTheChild) {
auto r = process::capture_stdout({"/bin/sh", "-c", "printf %s \"$MCPP_PROBE_ENV\""},
{{"MCPP_PROBE_ENV", "reached"}});
EXPECT_EQ(r.exit_code, 0);
EXPECT_EQ(r.output, "reached");
}
#else // _WIN32
TEST(RunExec, WindowsCoveredByIntegration) {
// The Windows path uses _spawnvpe (no /bin/* programs to point at here);
// it is exercised by the integration build that launches ninja.
SUCCEED();
}
#endif
// Host-independent: the command line capture_stdout gives cmd.exe names cmd's
// own null device, never a POSIX path cmd would try to open as `\dev\null`.
TEST(CaptureStdout, WindowsCommandNamesCmdNullDevice) {
auto line = process::windows_stdout_probe_command(
{"C:\\Program Files\\xlings\\xlings.exe", "--version"});
EXPECT_EQ(line, "\"C:\\Program Files\\xlings\\xlings.exe\" \"--version\" 2>nul");
EXPECT_EQ(line.find("/dev/null"), std::string::npos) << line;
}