Skip to content

Commit f0c65fb

Browse files
authored
Merge pull request DeusData#1346 from DeusData/fix/seam-callsites
fix(main): guard the seam call sites instead of stubbing the seams
2 parents 72bcc34 + 30b4def commit f0c65fb

1 file changed

Lines changed: 37 additions & 16 deletions

File tree

‎src/main.c‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,6 @@ static bool main_test_worker_project_lock_marker(const main_local_cli_mutation_t
249249
return close(marker) == 0 && written;
250250
#endif
251251
}
252-
#else
253-
static bool main_test_worker_project_lock_marker(const main_local_cli_mutation_t *mutation) {
254-
(void)mutation;
255-
return true;
256-
}
257252
#endif
258253

259254
static bool main_local_cli_mutation_begin(void *context, const char *project) {
@@ -280,13 +275,15 @@ static bool main_local_cli_mutation_begin(void *context, const char *project) {
280275
held->lease = lease;
281276
held->next = mutation->leases;
282277
mutation->leases = held;
278+
#ifdef CBM_ENABLE_TEST_SEAMS
283279
if (!main_test_worker_project_lock_marker(mutation)) {
284280
mutation->leases = held->next;
285281
main_project_lock_release_fully(&held->lease);
286282
free(held->project);
287283
free(held);
288284
return false;
289285
}
286+
#endif
290287
return true;
291288
}
292289
main_project_lock_release_fully(&lease);
@@ -405,6 +402,20 @@ static bool worker_prepare_process_group(void) {
405402
return (setpgid(0, 0) == 0 || getpgrp() == process_id) && getpgrp() == process_id;
406403
}
407404

405+
/* A worker that cannot contain its own process tree must not index: on failure
406+
* it would keep running as an orphan with no supervisor able to reap it. Shared
407+
* by the ordered containment steps in the worker path so all of them fail
408+
* identically — write() and _exit() rather than fprintf/exit, because this runs
409+
* after fork-sensitive setup and must not touch stdio locks or atexit handlers.
410+
*/
411+
static void worker_containment_unavailable(void) {
412+
static const char message[] =
413+
"CBM index worker could not start: process-tree containment unavailable\n";
414+
(void)write(STDERR_FILENO, message, sizeof(message) - 1);
415+
(void)kill(-getpid(), SIGKILL);
416+
_exit(EXIT_FAILURE);
417+
}
418+
408419
/* Test-only crash-orphan probe used by tests/test_worker_watchdog.sh. It is
409420
* created before the watchdog thread so fork never occurs in a multithreaded
410421
* worker, and inherits the worker's isolated process group.
@@ -457,10 +468,6 @@ static bool worker_start_watchdog_test_descendant(void) {
457468
}
458469
return written;
459470
}
460-
#else
461-
static bool worker_start_watchdog_test_descendant(void) {
462-
return true;
463-
}
464471
#endif
465472

466473
static bool worker_start_parent_watchdog(pid_t initial_ppid) {
@@ -2045,14 +2052,28 @@ int main(int argc, char **argv) {
20452052
invocation.marker_file, invocation.quarantine_file,
20462053
invocation.memory_budget_bytes);
20472054
#ifndef _WIN32
2055+
/* Split into three ordered steps rather than one condition, because the
2056+
* ORDER is load-bearing and the middle step only exists in test builds:
2057+
* 1. establish the isolated process group,
2058+
* 2. (test builds) start the crash-orphan probe, which must inherit
2059+
* that group and must fork BEFORE the watchdog thread exists —
2060+
* forking a multithreaded process is the bug this ordering avoids,
2061+
* 3. start the parent-death watchdog thread.
2062+
* Keeping the probe inside a single `||` chain made its call
2063+
* unconditional in the source, so with the seam compiled out cppcheck
2064+
* correctly reported `!probe()` as always false. Guarding the STEP, not
2065+
* stubbing the function, means release builds simply do not have it. */
20482066
if (!worker_prepare_process_group() || process_initial_ppid <= 1 ||
2049-
getppid() != process_initial_ppid || !worker_start_watchdog_test_descendant() ||
2050-
!worker_start_parent_watchdog(process_initial_ppid)) {
2051-
static const char message[] =
2052-
"CBM index worker could not start: process-tree containment unavailable\n";
2053-
(void)write(STDERR_FILENO, message, sizeof(message) - 1);
2054-
(void)kill(-getpid(), SIGKILL);
2055-
_exit(EXIT_FAILURE);
2067+
getppid() != process_initial_ppid) {
2068+
worker_containment_unavailable();
2069+
}
2070+
#ifdef CBM_ENABLE_TEST_SEAMS
2071+
if (!worker_start_watchdog_test_descendant()) {
2072+
worker_containment_unavailable();
2073+
}
2074+
#endif
2075+
if (!worker_start_parent_watchdog(process_initial_ppid)) {
2076+
worker_containment_unavailable();
20562077
}
20572078
#endif
20582079
cbm_index_supervisor_mark_host();

0 commit comments

Comments
 (0)