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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "windowsrunner.h"
#if ZEN_WITH_COMPUTE_SERVICES && ZEN_PLATFORM_WINDOWS
# include <zencore/compactbinary.h>
# include <zencore/compactbinarypackage.h>
# include <zencore/except.h>
# include <zencore/except_fmt.h>
# include <zencore/filesystem.h>
# include <zencore/fmtutils.h>
# include <zencore/scopeguard.h>
# include <zencore/trace.h>
# include <zencore/system.h>
# include <zencore/timer.h>
ZEN_THIRD_PARTY_INCLUDES_START
# include <userenv.h>
# include <aclapi.h>
# include <sddl.h>
ZEN_THIRD_PARTY_INCLUDES_END
namespace zen::compute {
using namespace std::literals;
WindowsProcessRunner::WindowsProcessRunner(ChunkResolver& Resolver,
const std::filesystem::path& BaseDir,
DeferredDirectoryDeleter& Deleter,
WorkerThreadPool& WorkerPool,
bool Sandboxed,
int32_t MaxConcurrentActions)
: LocalProcessRunner(Resolver, BaseDir, Deleter, WorkerPool, MaxConcurrentActions)
, m_Sandboxed(Sandboxed)
{
if (!m_Sandboxed)
{
return;
}
// Build a unique profile name per process to avoid collisions
m_AppContainerName = L"zenserver-sandbox-" + std::to_wstring(GetCurrentProcessId());
// Clean up any stale profile from a previous crash
DeleteAppContainerProfile(m_AppContainerName.c_str());
PSID Sid = nullptr;
HRESULT Hr = CreateAppContainerProfile(m_AppContainerName.c_str(),
m_AppContainerName.c_str(), // display name
m_AppContainerName.c_str(), // description
nullptr, // no capabilities
0, // capability count
&Sid);
if (FAILED(Hr))
{
throw zen::runtime_error("CreateAppContainerProfile failed: HRESULT 0x{:08X}", static_cast<uint32_t>(Hr));
}
m_AppContainerSid = Sid;
ZEN_INFO("AppContainer sandboxing enabled for child processes (profile={})", WideToUtf8(m_AppContainerName));
}
WindowsProcessRunner::~WindowsProcessRunner()
{
if (m_AppContainerSid)
{
FreeSid(m_AppContainerSid);
m_AppContainerSid = nullptr;
}
if (!m_AppContainerName.empty())
{
DeleteAppContainerProfile(m_AppContainerName.c_str());
}
}
void
WindowsProcessRunner::GrantAppContainerAccess(const std::filesystem::path& Path, DWORD AccessMask)
{
PACL ExistingDacl = nullptr;
PSECURITY_DESCRIPTOR SecurityDescriptor = nullptr;
DWORD Result = GetNamedSecurityInfoW(Path.c_str(),
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION,
nullptr,
nullptr,
&ExistingDacl,
nullptr,
&SecurityDescriptor);
if (Result != ERROR_SUCCESS)
{
throw zen::runtime_error("GetNamedSecurityInfoW failed for '{}': {}", Path.string(), GetSystemErrorAsString(Result));
}
auto $0 = MakeGuard([&] { LocalFree(SecurityDescriptor); });
EXPLICIT_ACCESSW Access{};
Access.grfAccessPermissions = AccessMask;
Access.grfAccessMode = SET_ACCESS;
Access.grfInheritance = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE;
Access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
Access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
Access.Trustee.ptstrName = static_cast<LPWSTR>(m_AppContainerSid);
PACL NewDacl = nullptr;
Result = SetEntriesInAclW(1, &Access, ExistingDacl, &NewDacl);
if (Result != ERROR_SUCCESS)
{
throw zen::runtime_error("SetEntriesInAclW failed for '{}': {}", Path.string(), GetSystemErrorAsString(Result));
}
auto $1 = MakeGuard([&] { LocalFree(NewDacl); });
Result = SetNamedSecurityInfoW(const_cast<LPWSTR>(Path.c_str()),
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION,
nullptr,
nullptr,
NewDacl,
nullptr);
if (Result != ERROR_SUCCESS)
{
throw zen::runtime_error("SetNamedSecurityInfoW failed for '{}': {}", Path.string(), GetSystemErrorAsString(Result));
}
}
SubmitResult
WindowsProcessRunner::SubmitAction(Ref<RunnerAction> Action)
{
ZEN_TRACE_CPU("WindowsProcessRunner::SubmitAction");
std::optional<PreparedAction> Prepared = PrepareActionSubmission(Action);
if (!Prepared)
{
return SubmitResult{.IsAccepted = false};
}
// Set up environment variables
CbObject WorkerDescription = Prepared->WorkerPackage.GetObject();
StringBuilder<1024> EnvironmentBlock;
for (auto& It : WorkerDescription["environment"sv])
{
EnvironmentBlock.Append(It.AsString());
EnvironmentBlock.Append('\0');
}
EnvironmentBlock.Append('\0');
EnvironmentBlock.Append('\0');
// Execute process - this spawns the child process immediately without waiting
// for completion
std::string_view ExecPath = WorkerDescription["path"sv].AsString();
std::filesystem::path ExePath = Prepared->WorkerPath / std::filesystem::path(ExecPath).make_preferred();
ExtendableWideStringBuilder<512> CommandLine;
CommandLine.Append(L'"');
CommandLine.Append(ExePath.c_str());
CommandLine.Append(L'"');
CommandLine.Append(L" -Build=build.action");
LPSECURITY_ATTRIBUTES lpProcessAttributes = nullptr;
LPSECURITY_ATTRIBUTES lpThreadAttributes = nullptr;
BOOL bInheritHandles = FALSE;
DWORD dwCreationFlags = 0;
ZEN_DEBUG("Executing: {} (sandboxed={})", WideToUtf8(CommandLine.c_str()), m_Sandboxed);
CommandLine.EnsureNulTerminated();
PROCESS_INFORMATION ProcessInformation{};
if (m_Sandboxed)
{
// Grant AppContainer access to sandbox and worker directories
GrantAppContainerAccess(Prepared->SandboxPath, FILE_ALL_ACCESS);
GrantAppContainerAccess(Prepared->WorkerPath, FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
// Set up extended startup info with AppContainer security capabilities
SECURITY_CAPABILITIES SecurityCapabilities{};
SecurityCapabilities.AppContainerSid = m_AppContainerSid;
SecurityCapabilities.Capabilities = nullptr;
SecurityCapabilities.CapabilityCount = 0;
SIZE_T AttrListSize = 0;
InitializeProcThreadAttributeList(nullptr, 1, 0, &AttrListSize);
auto AttrList = static_cast<PPROC_THREAD_ATTRIBUTE_LIST>(malloc(AttrListSize));
auto $0 = MakeGuard([&] { free(AttrList); });
if (!InitializeProcThreadAttributeList(AttrList, 1, 0, &AttrListSize))
{
zen::ThrowLastError("InitializeProcThreadAttributeList failed");
}
auto $1 = MakeGuard([&] { DeleteProcThreadAttributeList(AttrList); });
if (!UpdateProcThreadAttribute(AttrList,
0,
PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES,
&SecurityCapabilities,
sizeof(SecurityCapabilities),
nullptr,
nullptr))
{
zen::ThrowLastError("UpdateProcThreadAttribute (SECURITY_CAPABILITIES) failed");
}
STARTUPINFOEXW StartupInfoEx{};
StartupInfoEx.StartupInfo.cb = sizeof(STARTUPINFOEXW);
StartupInfoEx.lpAttributeList = AttrList;
dwCreationFlags |= EXTENDED_STARTUPINFO_PRESENT;
BOOL Success = CreateProcessW(nullptr,
CommandLine.Data(),
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
(LPVOID)EnvironmentBlock.Data(),
Prepared->SandboxPath.c_str(),
&StartupInfoEx.StartupInfo,
/* out */ &ProcessInformation);
if (!Success)
{
zen::ThrowLastError("Unable to launch sandboxed process");
}
}
else
{
STARTUPINFO StartupInfo{};
StartupInfo.cb = sizeof StartupInfo;
BOOL Success = CreateProcessW(nullptr,
CommandLine.Data(),
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
(LPVOID)EnvironmentBlock.Data(),
Prepared->SandboxPath.c_str(),
&StartupInfo,
/* out */ &ProcessInformation);
if (!Success)
{
zen::ThrowLastError("Unable to launch process");
}
}
CloseHandle(ProcessInformation.hThread);
Ref<RunningAction> NewAction{new RunningAction()};
NewAction->Action = Action;
NewAction->ProcessHandle = ProcessInformation.hProcess;
NewAction->SandboxPath = std::move(Prepared->SandboxPath);
{
RwLock::ExclusiveLockScope _(m_RunningLock);
m_RunningMap[Prepared->ActionLsn] = std::move(NewAction);
}
Action->SetActionState(RunnerAction::State::Running);
return SubmitResult{.IsAccepted = true};
}
void
WindowsProcessRunner::SweepRunningActions()
{
ZEN_TRACE_CPU("WindowsProcessRunner::SweepRunningActions");
std::vector<Ref<RunningAction>> CompletedActions;
m_RunningLock.WithExclusiveLock([&] {
for (auto It = begin(m_RunningMap), ItEnd = end(m_RunningMap); It != ItEnd;)
{
Ref<RunningAction> Running = It->second;
DWORD ExitCode = 0;
BOOL IsSuccess = GetExitCodeProcess(Running->ProcessHandle, &ExitCode);
if (IsSuccess && ExitCode != STILL_ACTIVE)
{
CloseHandle(Running->ProcessHandle);
Running->ProcessHandle = INVALID_HANDLE_VALUE;
Running->ExitCode = ExitCode;
CompletedActions.push_back(std::move(Running));
It = m_RunningMap.erase(It);
}
else
{
++It;
}
}
});
ProcessCompletedActions(CompletedActions);
}
void
WindowsProcessRunner::CancelRunningActions()
{
ZEN_TRACE_CPU("WindowsProcessRunner::CancelRunningActions");
Stopwatch Timer;
std::unordered_map<int, Ref<RunningAction>> RunningMap;
m_RunningLock.WithExclusiveLock([&] { std::swap(RunningMap, m_RunningMap); });
if (RunningMap.empty())
{
return;
}
ZEN_INFO("cancelling all running actions");
// For expedience we initiate the process termination for all known
// processes before attempting to wait for them to exit.
// Initiate termination for all known processes before waiting for them to exit.
for (const auto& Kv : RunningMap)
{
Ref<RunningAction> Running = Kv.second;
BOOL TermSuccess = TerminateProcess(Running->ProcessHandle, 222);
if (!TermSuccess)
{
DWORD LastError = GetLastError();
if (LastError != ERROR_ACCESS_DENIED)
{
ZEN_WARN("TerminateProcess for LSN {} not successful: {}", Running->Action->ActionLsn, GetSystemErrorAsString(LastError));
}
}
}
// Wait for all processes and clean up, regardless of whether TerminateProcess succeeded.
for (auto& [Lsn, Running] : RunningMap)
{
if (Running->ProcessHandle != INVALID_HANDLE_VALUE)
{
DWORD WaitResult = WaitForSingleObject(Running->ProcessHandle, 2000);
if (WaitResult != WAIT_OBJECT_0)
{
ZEN_WARN("wait for LSN {}: process exit did not succeed, result = {}", Running->Action->ActionLsn, WaitResult);
}
else
{
ZEN_DEBUG("LSN {}: process exit OK", Running->Action->ActionLsn);
}
CloseHandle(Running->ProcessHandle);
Running->ProcessHandle = INVALID_HANDLE_VALUE;
}
m_DeferredDeleter.Enqueue(Running->Action->ActionLsn, std::move(Running->SandboxPath));
Running->Action->SetActionState(RunnerAction::State::Failed);
}
ZEN_INFO("DONE - cancelled {} running processes (took {})", RunningMap.size(), NiceTimeSpanMs(Timer.GetElapsedTimeMs()));
}
bool
WindowsProcessRunner::CancelAction(int ActionLsn)
{
ZEN_TRACE_CPU("WindowsProcessRunner::CancelAction");
// Hold the shared lock while terminating to prevent the sweep thread from
// closing the handle between our lookup and TerminateProcess call.
bool Sent = false;
m_RunningLock.WithSharedLock([&] {
auto It = m_RunningMap.find(ActionLsn);
if (It == m_RunningMap.end())
{
return;
}
Ref<RunningAction> Target = It->second;
if (Target->ProcessHandle == INVALID_HANDLE_VALUE)
{
return;
}
BOOL TermSuccess = TerminateProcess(Target->ProcessHandle, 222);
if (!TermSuccess)
{
DWORD LastError = GetLastError();
if (LastError != ERROR_ACCESS_DENIED)
{
ZEN_WARN("CancelAction: TerminateProcess for LSN {} not successful: {}", ActionLsn, GetSystemErrorAsString(LastError));
}
return;
}
ZEN_DEBUG("CancelAction: initiated cancellation of LSN {}", ActionLsn);
Sent = true;
});
// The monitor thread will pick up the process exit and mark the action as Failed.
return Sent;
}
void
WindowsProcessRunner::SampleProcessCpu(RunningAction& Running)
{
FILETIME CreationTime, ExitTime, KernelTime, UserTime;
if (!GetProcessTimes(Running.ProcessHandle, &CreationTime, &ExitTime, &KernelTime, &UserTime))
{
return;
}
auto FtToU64 = [](FILETIME Ft) -> uint64_t { return (static_cast<uint64_t>(Ft.dwHighDateTime) << 32) | Ft.dwLowDateTime; };
// FILETIME values are in 100-nanosecond intervals
const uint64_t CurrentOsTicks = FtToU64(KernelTime) + FtToU64(UserTime);
const uint64_t NowTicks = GetHifreqTimerValue();
// Cumulative CPU seconds (absolute, available from first sample): 100ns → seconds
Running.Action->CpuSeconds.store(static_cast<float>(static_cast<double>(CurrentOsTicks) / 10'000'000.0), std::memory_order_relaxed);
if (Running.LastCpuSampleTicks != 0 && Running.LastCpuOsTicks != 0)
{
const uint64_t ElapsedMs = Stopwatch::GetElapsedTimeMs(NowTicks - Running.LastCpuSampleTicks);
if (ElapsedMs > 0)
{
const uint64_t DeltaOsTicks = CurrentOsTicks - Running.LastCpuOsTicks;
// 100ns → ms: divide by 10000; then as percent of elapsed ms
const float CpuPct = static_cast<float>(static_cast<double>(DeltaOsTicks) / 10000.0 / ElapsedMs * 100.0);
Running.Action->CpuUsagePercent.store(CpuPct, std::memory_order_relaxed);
}
}
Running.LastCpuSampleTicks = NowTicks;
Running.LastCpuOsTicks = CurrentOsTicks;
}
} // namespace zen::compute
#endif
|