Skip to content

Commit 044fef7

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/vitest-4-1-11
2 parents 8c2692c + 9c6f45b commit 044fef7

7 files changed

Lines changed: 198 additions & 2 deletions

File tree

‎src/foundation/subprocess.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ struct cbm_subprocess {
411411
void *log_ud;
412412
int quiet_timeout_ms;
413413
int cancel_grace_ms;
414+
size_t memory_limit_bytes;
414415
bool delete_log_on_exit;
415416

416417
long tail_pos;
@@ -444,6 +445,9 @@ static void cbm_subprocess_result_init(cbm_proc_result_t *result) {
444445
result->forced = false;
445446
result->tree_quiesced = false;
446447
result->supervision_failed = false;
448+
result->job_memory_limit_bytes = 0;
449+
result->peak_job_memory_bytes = 0;
450+
result->job_memory_available = false;
447451
}
448452

449453
static void cbm_subprocess_free_config(cbm_subprocess_t *process) {
@@ -525,6 +529,7 @@ static cbm_subprocess_t *cbm_subprocess_copy_opts(const cbm_proc_opts_t *opts) {
525529
if (process->cancel_grace_ms > CBM_SUBPROCESS_MAX_CANCEL_GRACE_MS) {
526530
process->cancel_grace_ms = CBM_SUBPROCESS_MAX_CANCEL_GRACE_MS;
527531
}
532+
process->memory_limit_bytes = opts->memory_limit_bytes;
528533
process->delete_log_on_exit = opts->delete_log_on_exit;
529534
atomic_init(&process->lifecycle, CBM_SUBPROCESS_ACTIVE);
530535
cbm_subprocess_result_init(&process->result);
@@ -658,6 +663,10 @@ static int cbm_subprocess_spawn_win(cbm_subprocess_t *process) {
658663
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits;
659664
ZeroMemory(&limits, sizeof(limits));
660665
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
666+
if (process->memory_limit_bytes > 0) {
667+
limits.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_JOB_MEMORY;
668+
limits.JobMemoryLimit = (SIZE_T)process->memory_limit_bytes;
669+
}
661670
HANDLE job = CreateJobObjectW(NULL, NULL);
662671
if (!job ||
663672
!SetInformationJobObject(job, JobObjectExtendedLimitInformation, &limits, sizeof(limits))) {
@@ -785,6 +794,17 @@ static bool cbm_win_job_active(cbm_subprocess_t *process, bool *known) {
785794
return accounting.ActiveProcesses != 0;
786795
}
787796

797+
static void cbm_win_capture_job_memory(cbm_subprocess_t *process) {
798+
process->result.job_memory_limit_bytes = process->memory_limit_bytes;
799+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits;
800+
ZeroMemory(&limits, sizeof(limits));
801+
if (QueryInformationJobObject(process->job, JobObjectExtendedLimitInformation, &limits,
802+
sizeof(limits), NULL)) {
803+
process->result.peak_job_memory_bytes = (size_t)limits.PeakJobMemoryUsed;
804+
process->result.job_memory_available = true;
805+
}
806+
}
807+
788808
static void cbm_win_begin_termination(cbm_subprocess_t *process, uint64_t now) {
789809
if (process->termination_started) {
790810
return;
@@ -873,9 +893,11 @@ static cbm_proc_poll_t cbm_subprocess_poll_win(cbm_subprocess_t *process, cbm_pr
873893
if (process->force_started_ms != 0 &&
874894
now - process->force_started_ms >= CBM_SUBPROCESS_FORCE_SETTLE_MS &&
875895
(!job_known || job_active || !process->root_reaped)) {
896+
cbm_win_capture_job_memory(process);
876897
return cbm_subprocess_finish_failed(process, out);
877898
}
878899
if (process->root_reaped && !job_active) {
900+
cbm_win_capture_job_memory(process);
879901
return cbm_subprocess_finish(process, out);
880902
}
881903
return CBM_PROC_POLL_RUNNING;

‎src/foundation/subprocess.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ typedef struct {
4242
bool forced; /* force was needed after grace expiry or to reap descendants after root exit */
4343
bool tree_quiesced; /* the owned process tree has no surviving processes */
4444
bool supervision_failed; /* the bounded containment deadline expired; tree_quiesced is false */
45+
size_t job_memory_limit_bytes; /* Windows Job Object commit cap; zero on POSIX or uncapped */
46+
size_t peak_job_memory_bytes; /* Windows peak commit, not RSS or proof of a limit hit */
47+
bool job_memory_available; /* terminal Job Object memory query succeeded */
4548
} cbm_proc_result_t;
4649

4750
/* Called synchronously for each newly-completed log chunk while the child runs.
@@ -68,6 +71,8 @@ typedef struct {
6871
* ms with no new completed log line */
6972
int cancel_grace_ms; /* graceful tree-termination window; <= 0 uses the finite
7073
* CBM_SUBPROCESS_DEFAULT_CANCEL_GRACE_MS */
74+
size_t memory_limit_bytes; /* Windows-only hard commit limit for the entire Job Object;
75+
* 0 => no OS-enforced memory limit */
7176
bool delete_log_on_exit; /* unlink log_file after reaping */
7277
} cbm_proc_opts_t;
7378

‎src/mcp/index_supervisor.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,21 @@ static void worker_terminal_log(cbm_index_worker_handle_t *handle) {
609609
(void)snprintf(exit_text, sizeof(exit_text), "%d", handle->result.exit_code);
610610
cbm_log_info("index.supervisor.reap", "outcome", cbm_proc_outcome_str(handle->result.outcome),
611611
"exit_code", exit_text, "signal", signal_text);
612+
if (!worker_result_succeeded(&handle->result) &&
613+
handle->process_result.job_memory_limit_bytes > 0) {
614+
char limit_text[CBM_SZ_32];
615+
char peak_text[CBM_SZ_32];
616+
(void)snprintf(limit_text, sizeof(limit_text), "%zu",
617+
handle->process_result.job_memory_limit_bytes);
618+
(void)snprintf(peak_text, sizeof(peak_text), "%zu",
619+
handle->process_result.peak_job_memory_bytes);
620+
/* Peak accounting may include a denied allocation and exceed the cap,
621+
* or remain below it. Report evidence, not an inferred OOM classification
622+
* or a file to quarantine. */
623+
cbm_log_warn("index.supervisor.worker_memory", "job_limit_bytes", limit_text,
624+
"peak_job_memory_bytes",
625+
handle->process_result.job_memory_available ? peak_text : "unavailable");
626+
}
612627
if (handle->result.response_rejected) {
613628
cbm_log_error("index.supervisor.response_rejected", "reason", "payload_too_large", "log",
614629
handle->log_path);
@@ -629,6 +644,26 @@ static void worker_terminal_log(cbm_index_worker_handle_t *handle) {
629644
}
630645
}
631646

647+
size_t cbm_index_worker_job_memory_limit(size_t memory_budget_bytes) {
648+
/* Tiny token budgets must still reach main (image/CRT/stack startup already
649+
* needs memory). Keep their argv value but leave the OS cap disabled below
650+
* 512 MiB. KILL_ON_JOB_CLOSE remains in force regardless of the cap.
651+
*
652+
* Windows charges job-wide COMMIT, not RSS. This 1.5x limit leaves room
653+
* for the
654+
* cooperative budget's recovery/abort gate, but an allocation can
655+
* still be denied before
656+
* the next cooperative check. It is a backstop for
657+
* descendants too, not exact
658+
* enforcement of the accounting budget. */
659+
const size_t minimum_budget = (size_t)512U * 1024U * 1024U;
660+
if (memory_budget_bytes < minimum_budget) {
661+
return 0;
662+
}
663+
size_t headroom = memory_budget_bytes / 2;
664+
return memory_budget_bytes > SIZE_MAX - headroom ? SIZE_MAX : memory_budget_bytes + headroom;
665+
}
666+
632667
int cbm_index_worker_start_with_log(const char *args_json, size_t memory_budget_bytes,
633668
bool single_thread, const char *marker_file,
634669
const char *quarantine_file, cbm_proc_log_cb log_callback,
@@ -716,6 +751,7 @@ int cbm_index_worker_start_with_log(const char *args_json, size_t memory_budget_
716751
options.on_log_line = NULL;
717752
options.log_ud = NULL;
718753
options.quiet_timeout_ms = worker_quiet_timeout_ms();
754+
options.memory_limit_bytes = cbm_index_worker_job_memory_limit(memory_budget_bytes);
719755
options.delete_log_on_exit = false;
720756
if (cbm_subprocess_spawn(&options, &handle->process) != 0) {
721757
(void)cbm_unlink(handle->response_path);

‎src/mcp/index_supervisor.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ typedef enum {
161161
CBM_INDEX_WORKER_POLL_TERMINAL = 1,
162162
} cbm_index_worker_poll_t;
163163

164+
/* Pure Windows backstop policy, available on all platforms for regression tests.
165+
* Small/zero budgets disable the OS cap, not the cooperative budget or containment. */
166+
size_t cbm_index_worker_job_memory_limit(size_t memory_budget_bytes);
167+
164168
/* Start returns after process creation. All string arguments are copied by the
165169
* contained subprocess layer. Recovery values are encoded only as hidden argv
166170
* and applied after exec by cbm_index_set_worker_role_options(). */

‎tests/test_index_supervisor.c‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "daemon/bootstrap.h"
77
#include "foundation/compat.h"
88
#include "foundation/compat_fs.h"
9+
#include "foundation/log.h"
910
#include "foundation/platform.h"
1011
#include "foundation/profile.h"
1112
#include "mcp/index_supervisor.h"
@@ -560,7 +561,8 @@ static bool index_supervisor_test_run_probe(const char *mode, bool profiling,
560561
(void)snprintf(args, sizeof(args), "{\"__cbm_test_worker\":\"%s\"}", mode);
561562
cbm_profile_active = profiling;
562563
cbm_index_worker_handle_t *handle = NULL;
563-
if (cbm_index_worker_start(args, 0, false, NULL, NULL, &handle) != 0 || !handle) {
564+
const size_t budget = (size_t)1024U * 1024U * 1024U;
565+
if (cbm_index_worker_start(args, budget, false, NULL, NULL, &handle) != 0 || !handle) {
564566
return false;
565567
}
566568
char log_path[INDEX_SUPERVISOR_TEST_PATH_CAP];
@@ -588,6 +590,17 @@ static bool index_supervisor_test_run_probe(const char *mode, bool profiling,
588590
return terminal;
589591
}
590592

593+
static char g_index_supervisor_memory_log[1024];
594+
static int g_index_supervisor_memory_log_count;
595+
596+
static void index_supervisor_test_memory_log_sink(const char *line) {
597+
if (strstr(line, "index.supervisor.worker_memory")) {
598+
g_index_supervisor_memory_log_count++;
599+
(void)snprintf(g_index_supervisor_memory_log, sizeof(g_index_supervisor_memory_log), "%s",
600+
line);
601+
}
602+
}
603+
591604
TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {
592605
char cache[INDEX_SUPERVISOR_TEST_PATH_CAP];
593606
(void)snprintf(cache, sizeof(cache), "%s/cbm-index-logs-XXXXXX", cbm_tmpdir());
@@ -597,6 +610,14 @@ TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {
597610
bool saved_profile = cbm_profile_active;
598611
(void)cbm_setenv("CBM_CACHE_DIR", cache, 1);
599612

613+
CBMLogLevel saved_level = cbm_log_get_level();
614+
CBMLogFormat saved_format = cbm_log_get_format();
615+
cbm_log_set_level(CBM_LOG_INFO);
616+
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
617+
g_index_supervisor_memory_log[0] = '\0';
618+
g_index_supervisor_memory_log_count = 0;
619+
cbm_log_set_sink_ex(index_supervisor_test_memory_log_sink, CBM_LOG_SINK_TEE);
620+
600621
cbm_proc_outcome_t clean_outcome = CBM_PROC_SPAWN_FAILED;
601622
cbm_proc_outcome_t profile_outcome = CBM_PROC_SPAWN_FAILED;
602623
cbm_proc_outcome_t crash_outcome = CBM_PROC_SPAWN_FAILED;
@@ -621,6 +642,9 @@ TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {
621642
bool crash_classified_failure = crash_outcome == CBM_PROC_CRASH;
622643
#endif
623644

645+
cbm_log_set_sink(NULL);
646+
cbm_log_set_level(saved_level);
647+
cbm_log_set_format(saved_format);
624648
cbm_profile_active = saved_profile;
625649
index_supervisor_test_restore_env("CBM_CACHE_DIR", saved_cache);
626650
(void)th_rmtree(cache);
@@ -640,6 +664,14 @@ TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {
640664
ASSERT_FALSE(crash_response);
641665
ASSERT_TRUE(crash_log);
642666
ASSERT_FALSE(crash_response_file);
667+
#ifdef _WIN32
668+
ASSERT_EQ(g_index_supervisor_memory_log_count, 1); /* crash only, never clean */
669+
ASSERT_TRUE(strstr(g_index_supervisor_memory_log, "job_limit_bytes=1610612736") != NULL);
670+
ASSERT_TRUE(strstr(g_index_supervisor_memory_log, "peak_job_memory_bytes=") != NULL);
671+
ASSERT_TRUE(strstr(g_index_supervisor_memory_log, "unavailable") == NULL);
672+
#else
673+
ASSERT_EQ(g_index_supervisor_memory_log_count, 0);
674+
#endif
643675
PASS();
644676
}
645677

@@ -893,7 +925,24 @@ TEST(index_supervisor_killed_worker_log_is_never_empty_and_names_the_run) {
893925
PASS();
894926
}
895927

928+
TEST(index_supervisor_job_memory_limit_has_floor_headroom_and_no_overflow) {
929+
const size_t mib = (size_t)1024U * 1024U;
930+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(0) == 0);
931+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(4096) == 0);
932+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(8192) == 0);
933+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(512U * mib - 1) == 0);
934+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(512U * mib) == 768U * mib);
935+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(512U * mib + 1) == 768U * mib + 1);
936+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(1024U * mib) == 1536U * mib);
937+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(SIZE_MAX / 2) ==
938+
SIZE_MAX / 2 + (SIZE_MAX / 2) / 2);
939+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(SIZE_MAX - 1) == SIZE_MAX);
940+
ASSERT_TRUE(cbm_index_worker_job_memory_limit(SIZE_MAX) == SIZE_MAX);
941+
PASS();
942+
}
943+
896944
SUITE(index_supervisor) {
945+
RUN_TEST(index_supervisor_job_memory_limit_has_floor_headroom_and_no_overflow);
897946
RUN_TEST(index_supervisor_worker_argv_requires_exact_build_bound_grammar);
898947
RUN_TEST(index_supervisor_async_jobs_are_isolated_cancellable_and_terminal_cached);
899948
RUN_TEST(index_supervisor_sync_wrapper_forwards_cancel_and_drains_tree);

‎tests/test_main.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ int tf_skip_count = 0;
3434
#include <signal.h>
3535
#ifdef _WIN32
3636
#include <winsock2.h> /* #798 follow-up: socket-isolation re-exec probe */
37+
#include <windows.h>
3738
#else
3839
#include <unistd.h>
3940
#ifdef __APPLE__
@@ -51,6 +52,29 @@ int tf_skip_count = 0;
5152
* unwind after either production containment or the test's verified backstop. */
5253
#define TF_BLOCKING_GIT_MARKER_ENV "CBM_TEST_RUNTIME_BLOCKING_GIT_PID_FILE"
5354

55+
/* Native child for subprocess_windows_job_object_enforces_memory_limit. The
56+
* fixed-size commit keeps the RED path bounded: without a Job memory limit it
57+
* succeeds and exits 0; with the limit it is denied and exits with the sentinel
58+
* code expected by the parent test. */
59+
static int tf_maybe_run_windows_memory_limit_probe(int argc, char **argv) {
60+
#ifdef _WIN32
61+
if (argc == 2 && argv && strcmp(argv[1], "__cbm_windows_memory_limit_probe") == 0) {
62+
const SIZE_T allocation_size = (SIZE_T)2U * 1024U * 1024U * 1024U;
63+
void *allocation =
64+
VirtualAlloc(NULL, allocation_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
65+
if (!allocation) {
66+
return 73;
67+
}
68+
(void)VirtualFree(allocation, 0, MEM_RELEASE);
69+
return 0;
70+
}
71+
#else
72+
(void)argc;
73+
(void)argv;
74+
#endif
75+
return -1;
76+
}
77+
5478
#ifdef _WIN32
5579
static bool tf_invoked_as_windows_git_module(void) {
5680
/* CreateProcessW authenticates the executable with lpApplicationName, but
@@ -905,6 +929,10 @@ int main(int argc, char **argv) {
905929
(void)cbm_setenv("CBM_TEST_BUILD_FINGERPRINT",
906930
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 1);
907931
}
932+
int memory_limit_probe_rc = tf_maybe_run_windows_memory_limit_probe(argc, argv);
933+
if (memory_limit_probe_rc >= 0) {
934+
return memory_limit_probe_rc;
935+
}
908936
int blocking_git_rc = tf_maybe_run_blocking_git_probe(argc, argv);
909937
if (blocking_git_rc >= 0) {
910938
return blocking_git_rc;

‎tests/test_subprocess.c‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <unistd.h>
2626
#else
2727
#include <windows.h>
28+
#include "../src/foundation/win_utf8.h"
2829
#endif
2930

3031
/* ── Layer 1: pure classifier (all platforms) ─────────────────────────────── */
@@ -279,10 +280,20 @@ TEST(subprocess_run_spawn_failure) {
279280
TEST(subprocess_run_null_bin_rejected) {
280281
cbm_proc_opts_t opts = {0};
281282
opts.bin = NULL;
282-
cbm_proc_result_t r;
283+
/* Reusing a previous result must not expose stale Job Object diagnostics
284+
* when validation rejects the next spawn before a process exists. */
285+
cbm_proc_result_t r = {
286+
.job_memory_limit_bytes = 123,
287+
.peak_job_memory_bytes = 456,
288+
.job_memory_available = true,
289+
};
283290
int rc = cbm_subprocess_run(&opts, &r);
284291
ASSERT_EQ(rc, -1);
285292
ASSERT_EQ(r.outcome, CBM_PROC_SPAWN_FAILED);
293+
ASSERT_EQ(r.exit_code, -1);
294+
ASSERT_TRUE(r.job_memory_limit_bytes == 0);
295+
ASSERT_TRUE(r.peak_job_memory_bytes == 0);
296+
ASSERT_FALSE(r.job_memory_available);
286297
PASS();
287298
}
288299

@@ -744,6 +755,46 @@ TEST(subprocess_windows_job_object_cancellation_quiesces_descendant_tree) {
744755
#endif
745756
}
746757

758+
TEST(subprocess_windows_job_object_enforces_memory_limit) {
759+
#ifndef _WIN32
760+
SKIP_PLATFORM("native Windows Job Object memory-limit probe");
761+
#else
762+
char *self_path = cbm_module_path_utf8();
763+
ASSERT_TRUE(self_path != NULL);
764+
const char *argv[] = {self_path, "__cbm_windows_memory_limit_probe", NULL};
765+
cbm_proc_opts_t opts = {0};
766+
opts.bin = self_path;
767+
opts.argv = argv;
768+
opts.quiet_timeout_ms = 5000;
769+
770+
/* Without the cap the SAME allocation must succeed. A machine-wide commit
771+
* shortage must fail this test, not masquerade as Job Object enforcement. */
772+
cbm_proc_result_t uncapped = {0};
773+
int uncapped_rc = cbm_subprocess_run(&opts, &uncapped);
774+
opts.memory_limit_bytes = (size_t)1024U * 1024U * 1024U;
775+
cbm_proc_result_t result = {0};
776+
int run_rc = cbm_subprocess_run(&opts, &result);
777+
free(self_path);
778+
ASSERT_EQ(uncapped_rc, 0);
779+
ASSERT_EQ(uncapped.outcome, CBM_PROC_CLEAN);
780+
ASSERT_EQ(uncapped.exit_code, 0);
781+
ASSERT_TRUE(uncapped.tree_quiesced);
782+
ASSERT_FALSE(uncapped.supervision_failed);
783+
ASSERT_TRUE(uncapped.job_memory_limit_bytes == 0);
784+
ASSERT_EQ(run_rc, 0);
785+
ASSERT_EQ(result.outcome, CBM_PROC_EXIT_NONZERO);
786+
ASSERT_EQ(result.exit_code, 73);
787+
ASSERT_TRUE(result.tree_quiesced);
788+
ASSERT_FALSE(result.supervision_failed);
789+
ASSERT_TRUE(result.job_memory_available);
790+
ASSERT_TRUE(result.job_memory_limit_bytes == opts.memory_limit_bytes);
791+
ASSERT_TRUE(result.peak_job_memory_bytes > 0);
792+
/* Windows may include the denied reservation in its peak counter, so peak
793+
* can exceed the cap. The uncapped/capped exit codes prove enforcement. */
794+
PASS();
795+
#endif
796+
}
797+
747798
TEST(subprocess_cancel_grace_is_hard_capped) {
748799
#ifdef _WIN32
749800
SKIP_PLATFORM("POSIX process-group grace cap probe; native Windows coverage pending");
@@ -1228,6 +1279,7 @@ SUITE(subprocess) {
12281279
RUN_TEST(subprocess_cancel_is_idempotent_and_kills_ignoring_tree);
12291280
RUN_TEST(subprocess_quiet_timeout_kills_ignoring_tree);
12301281
RUN_TEST(subprocess_windows_job_object_cancellation_quiesces_descendant_tree);
1282+
RUN_TEST(subprocess_windows_job_object_enforces_memory_limit);
12311283
RUN_TEST(subprocess_cancel_grace_is_hard_capped);
12321284
RUN_TEST(subprocess_poll_log_delivery_is_bounded_and_terminal_is_lossless);
12331285
RUN_TEST(subprocess_final_log_drain_error_is_terminal_and_preserves_classification);

0 commit comments

Comments
 (0)