Skip to content

Commit 33ae8f3

Browse files
committed
test(subprocess): prove descendant memory is measured on every platform
subprocess_tree_rss_measures_contained_descendants asserted only that the tree total was above zero, which the root alone satisfies, and it was skipped on Windows although its skip text said it ran there. The test runner gains two roles: __cbm_rss_tree_root starts one __cbm_rss_tree_hold descendant (fork on POSIX, CreateProcessW into the same Job Object on Windows) that touches a controlled allocation and publishes its pid by an atomic rename. The test measures two otherwise identical trees, one whose descendant holds nothing and one whose descendant holds 128 MiB, and requires the second total to be at least 96 MiB higher. Only a probe that measures past the root can pass. The test now runs natively on Windows. The waits have no deadline: they end when the descendant is ready or the tree stops running, and the root exits when its descendant dies. Binding check: with the macOS probe reduced to the root only, the test fails (the loaded tree is not 96 MiB above the idle one). GREEN on the real probe (idle 43 MiB, loaded 187 MiB under ASan). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 767ac37 commit 33ae8f3

2 files changed

Lines changed: 210 additions & 33 deletions

File tree

‎tests/test_main.c‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ int tf_deselected_count = 0;
4444
#include <windows.h>
4545
#include "foundation/win_utf8.h" /* cbm_module_path_utf8 — the runner's own image */
4646
#else
47+
#include <sys/wait.h> /* waitpid: the process-tree probe root */
4748
#include <unistd.h>
4849
#ifdef __APPLE__
4950
#include <fcntl.h>
@@ -1968,6 +1969,100 @@ static int tf_role_env_isolation(int argc, char **argv) {
19681969
return 0;
19691970
}
19701971

1972+
/* Process-tree memory probe coverage (test_subprocess.c). The root starts ONE
1973+
* descendant that charges a controlled amount of memory, so a tree total can
1974+
* be checked against an allocation only a descendant holds. The descendant
1975+
* publishes its pid by an atomic rename once every page is touched, then
1976+
* blocks until it is killed. The root exits when its descendant dies, so a
1977+
* failed setup ends the tree instead of leaving the test waiting. */
1978+
static int tf_rss_tree_hold(const char *ready_path, const char *mib_text) {
1979+
char *end = NULL;
1980+
long mib = strtol(mib_text, &end, 10);
1981+
if (!end || *end != '\0' || mib < 0 || mib > 1024) {
1982+
return 81;
1983+
}
1984+
size_t bytes = (size_t)mib * 1024U * 1024U;
1985+
unsigned char *block = bytes > 0 ? malloc(bytes) : NULL;
1986+
if (bytes > 0 && !block) {
1987+
return 82;
1988+
}
1989+
if (block) {
1990+
memset(block, 0xA5, bytes);
1991+
}
1992+
char staged[1024];
1993+
int length = snprintf(staged, sizeof(staged), "%s.tmp", ready_path);
1994+
FILE *ready = length > 0 && (size_t)length < sizeof(staged) ? cbm_fopen(staged, "wb") : NULL;
1995+
if (!ready) {
1996+
return 83;
1997+
}
1998+
#ifdef _WIN32
1999+
(void)fprintf(ready, "%lu\n", (unsigned long)GetCurrentProcessId());
2000+
#else
2001+
(void)fprintf(ready, "%ld\n", (long)getpid());
2002+
#endif
2003+
if (fclose(ready) != 0 || rename(staged, ready_path) != 0) {
2004+
return 84;
2005+
}
2006+
volatile unsigned char keep = block ? block[bytes - 1] : 0;
2007+
(void)keep;
2008+
for (;;) {
2009+
#ifdef _WIN32
2010+
Sleep(INFINITE);
2011+
#else
2012+
(void)pause();
2013+
#endif
2014+
}
2015+
}
2016+
2017+
static int tf_role_rss_tree_hold(int argc, char **argv) {
2018+
return argc == 4 ? tf_rss_tree_hold(argv[2], argv[3]) : -1;
2019+
}
2020+
2021+
static int tf_role_rss_tree_root(int argc, char **argv) {
2022+
if (argc != 4) {
2023+
return -1;
2024+
}
2025+
#ifdef _WIN32
2026+
char *self = cbm_module_path_utf8();
2027+
const char *child_argv[] = {self, "__cbm_rss_tree_hold", argv[2], argv[3], NULL};
2028+
char command_line[8192];
2029+
bool built = self && cbm_build_win_cmdline(command_line, sizeof(command_line), child_argv);
2030+
wchar_t *wide_self = built ? cbm_utf8_to_wide(self) : NULL;
2031+
wchar_t *wide_command = built ? cbm_utf8_to_wide(command_line) : NULL;
2032+
STARTUPINFOW startup;
2033+
ZeroMemory(&startup, sizeof(startup));
2034+
startup.cb = sizeof(startup);
2035+
PROCESS_INFORMATION child;
2036+
ZeroMemory(&child, sizeof(child));
2037+
/* The child joins this process's Job Object by default, which is the tree
2038+
* the probe under test enumerates. */
2039+
BOOL started = wide_self && wide_command &&
2040+
CreateProcessW(wide_self, wide_command, NULL, NULL, FALSE, 0, NULL, NULL,
2041+
&startup, &child);
2042+
free(wide_self);
2043+
free(wide_command);
2044+
free(self);
2045+
if (!started) {
2046+
return 85;
2047+
}
2048+
CloseHandle(child.hThread);
2049+
(void)WaitForSingleObject(child.hProcess, INFINITE);
2050+
CloseHandle(child.hProcess);
2051+
#else
2052+
pid_t child = fork();
2053+
if (child < 0) {
2054+
return 85;
2055+
}
2056+
if (child == 0) {
2057+
_exit(tf_rss_tree_hold(argv[2], argv[3]));
2058+
}
2059+
int status = 0;
2060+
while (waitpid(child, &status, 0) < 0 && errno == EINTR) {
2061+
}
2062+
#endif
2063+
return 86; /* the descendant ended: end the tree */
2064+
}
2065+
19712066
/* Installation tests use this executable as a structurally real candidate.
19722067
* Mirror the production binary's minimal verification contract. */
19732068
static int tf_role_version(int argc, char **argv) {
@@ -2060,6 +2155,8 @@ typedef struct {
20602155
static const tf_role_t tf_roles[] = {
20612156
TF_ROLE(TF_PHASE_RAW, "__cbm_env_values", tf_role_env_values),
20622157
TF_ROLE(TF_PHASE_RAW, "__cbm_env_isolation", tf_role_env_isolation),
2158+
TF_ROLE(TF_PHASE_RAW, "__cbm_rss_tree_root", tf_role_rss_tree_root),
2159+
TF_ROLE(TF_PHASE_RAW, "__cbm_rss_tree_hold", tf_role_rss_tree_hold),
20632160
TF_ROLE(TF_PHASE_GIT_CLEAN, "__cbm_subprocess_stdin_probe", tf_role_subprocess_stdin_probe),
20642161
TF_ROLE(TF_PHASE_GIT_CLEAN, "__cbm_subprocess_stdout_probe", tf_role_subprocess_stdout_probe),
20652162
TF_ROLE(TF_PHASE_GIT_CLEAN, "__cbm_git_facts_diff_probe", tf_role_git_facts_diff_probe),

‎tests/test_subprocess.c‎

Lines changed: 113 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -807,43 +807,123 @@ TEST(subprocess_spawn_returns_while_child_is_running) {
807807
#endif
808808
}
809809

810-
TEST(subprocess_tree_rss_measures_contained_descendants) {
811-
#ifdef _WIN32
812-
SKIP_PLATFORM("native Job Object process-tree RSS runs on Windows CI");
813-
#else
814-
char pid_path[64];
815-
ASSERT_TRUE(make_tree_pid_path(pid_path));
816-
cbm_subprocess_t *process = NULL;
817-
ASSERT_EQ(spawn_ignoring_tree(pid_path, 0, 100, &process), 0);
818-
pid_t parent_pid = 0;
819-
pid_t grandchild_pid = 0;
820-
bool ready = wait_for_tree_pids(pid_path, process, &parent_pid, &grandchild_pid, 3000);
821-
uint64_t rss_bytes = 0;
822-
cbm_proc_tree_rss_status_t rss_status =
823-
ready ? cbm_subprocess_tree_rss_bytes(process, &rss_bytes) : CBM_PROC_TREE_RSS_ERROR;
824-
bool cancel_accepted = cbm_subprocess_request_cancel(process);
810+
/* ── Process-tree memory probe (every platform) ─────────────────────────────
811+
* The test runner's __cbm_rss_tree_root role starts one __cbm_rss_tree_hold
812+
* descendant that touches a controlled allocation and then publishes its pid.
813+
* Waits below have no deadline: they end when the descendant is ready or the
814+
* tree stops running, so the outcome, not a timer, decides the verdict. */
815+
816+
#define RSS_TREE_MIB UINT64_C(1048576)
817+
818+
typedef struct {
819+
bool spawned;
820+
bool ready;
821+
cbm_proc_tree_rss_status_t status;
822+
uint64_t rss;
823+
bool quiesced;
824+
cbm_proc_tree_rss_status_t final_status;
825+
uint64_t final_rss;
826+
} rss_tree_sample_t;
827+
828+
static void rss_tree_pause(void) {
829+
cbm_usleep(10000); /* poll interval only; nothing here is a deadline */
830+
}
831+
832+
static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *process) {
833+
for (;;) {
834+
FILE *ready = cbm_fopen(ready_path, "rb");
835+
if (ready) {
836+
long pid = 0;
837+
int fields = fscanf(ready, "%ld", &pid);
838+
(void)fclose(ready);
839+
if (fields == 1 && pid > 0) {
840+
return true;
841+
}
842+
}
843+
cbm_proc_result_t ignored;
844+
if (cbm_subprocess_poll(process, &ignored) != CBM_PROC_POLL_RUNNING) {
845+
return false;
846+
}
847+
rss_tree_pause();
848+
}
849+
}
850+
851+
static bool rss_tree_finish(cbm_subprocess_t *process, rss_tree_sample_t *sample) {
852+
(void)cbm_subprocess_request_cancel(process);
825853
cbm_proc_result_t result;
826-
bool terminal = poll_until_terminal(process, 5000, &result);
827-
uint64_t final_rss = UINT64_MAX;
828-
cbm_proc_tree_rss_status_t final_rss_status =
829-
terminal ? cbm_subprocess_tree_rss_bytes(process, &final_rss) : CBM_PROC_TREE_RSS_ERROR;
830-
if (terminal) {
831-
cbm_subprocess_destroy(process);
832-
} else {
833-
force_probe_cleanup(parent_pid, grandchild_pid);
854+
for (;;) {
855+
cbm_proc_poll_t state = cbm_subprocess_poll(process, &result);
856+
if (state == CBM_PROC_POLL_TERMINAL) {
857+
break;
858+
}
859+
if (state == CBM_PROC_POLL_ERROR) {
860+
return false;
861+
}
862+
rss_tree_pause();
834863
}
835-
(void)unlink(pid_path);
864+
sample->final_status = cbm_subprocess_tree_rss_bytes(process, &sample->final_rss);
865+
cbm_subprocess_destroy(process);
866+
return result.tree_quiesced;
867+
}
868+
869+
/* Measure one tree whose descendant holds `mib` MiB. */
870+
static rss_tree_sample_t rss_tree_sample(int mib) {
871+
rss_tree_sample_t sample = {.status = CBM_PROC_TREE_RSS_ERROR,
872+
.final_status = CBM_PROC_TREE_RSS_ERROR,
873+
.final_rss = UINT64_MAX};
874+
const char *made = th_mktempdir("cbm_rss_tree");
875+
char dir[256];
876+
char ready_path[300];
877+
char mib_text[16];
878+
if (!made || snprintf(dir, sizeof(dir), "%s", made) >= (int)sizeof(dir) ||
879+
snprintf(ready_path, sizeof(ready_path), "%s/ready", dir) >= (int)sizeof(ready_path)) {
880+
return sample;
881+
}
882+
(void)snprintf(mib_text, sizeof(mib_text), "%d", mib);
883+
const char *argv[] = {cbm_test_program_path, "__cbm_rss_tree_root", ready_path, mib_text,
884+
NULL};
885+
cbm_proc_opts_t opts = {0};
886+
opts.bin = cbm_test_program_path;
887+
opts.argv = argv;
888+
opts.cancel_grace_ms = 100;
889+
cbm_subprocess_t *process = NULL;
890+
sample.spawned = cbm_subprocess_spawn(&opts, &process) == 0;
891+
if (sample.spawned) {
892+
sample.ready = rss_tree_wait_ready(ready_path, process);
893+
if (sample.ready) {
894+
sample.status = cbm_subprocess_tree_rss_bytes(process, &sample.rss);
895+
}
896+
sample.quiesced = rss_tree_finish(process, &sample);
897+
}
898+
th_cleanup(dir);
899+
return sample;
900+
}
836901

837-
ASSERT_TRUE(ready);
838-
ASSERT_EQ(rss_status, CBM_PROC_TREE_RSS_OK);
839-
ASSERT_TRUE(rss_bytes > 0);
840-
ASSERT_TRUE(cancel_accepted);
841-
ASSERT_TRUE(terminal);
842-
ASSERT_TRUE(result.tree_quiesced);
843-
ASSERT_EQ(final_rss_status, CBM_PROC_TREE_RSS_EMPTY);
844-
ASSERT_EQ(final_rss, 0);
902+
/* Only the DESCENDANT allocates, so the difference between two otherwise
903+
* identical trees is memory the probe can only have counted by measuring past
904+
* the root. 128 MiB touched against a 96 MiB floor leaves room for allocator
905+
* and page-rounding noise and none for a root-only total. */
906+
TEST(subprocess_tree_rss_measures_contained_descendants) {
907+
rss_tree_sample_t idle = rss_tree_sample(0);
908+
rss_tree_sample_t loaded = rss_tree_sample(128);
909+
if (loaded.status == CBM_PROC_TREE_RSS_OK && idle.status == CBM_PROC_TREE_RSS_OK) {
910+
printf(" tree rss: idle=%llu MiB loaded=%llu MiB\n",
911+
(unsigned long long)(idle.rss / RSS_TREE_MIB),
912+
(unsigned long long)(loaded.rss / RSS_TREE_MIB));
913+
}
914+
ASSERT_TRUE(idle.spawned);
915+
ASSERT_TRUE(idle.ready);
916+
ASSERT_EQ(idle.status, CBM_PROC_TREE_RSS_OK);
917+
ASSERT_TRUE(idle.rss > 0);
918+
ASSERT_TRUE(loaded.spawned);
919+
ASSERT_TRUE(loaded.ready);
920+
ASSERT_EQ(loaded.status, CBM_PROC_TREE_RSS_OK);
921+
ASSERT_TRUE(loaded.rss >= idle.rss + 96 * RSS_TREE_MIB);
922+
ASSERT_TRUE(idle.quiesced);
923+
ASSERT_TRUE(loaded.quiesced);
924+
ASSERT_EQ(loaded.final_status, CBM_PROC_TREE_RSS_EMPTY);
925+
ASSERT_EQ(loaded.final_rss, 0);
845926
PASS();
846-
#endif
847927
}
848928

849929
TEST(subprocess_natural_completion_is_cached_across_polls) {

0 commit comments

Comments
 (0)