Skip to content

Commit bfb8599

Browse files
DongSunchaoavagin
authored andcommitted
test/zdtm: Add ZDTM test for dynamic bfd expansion on massive group
Add a new ZDTM test case `massive_groups` to verify the dynamic buffer expansion logic in `bfd.c` Signed-off-by: dong sunchao <dongsunchao@gmail.com>
1 parent 145308d commit bfb8599

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

test/zdtm/static/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ TST_NOFILE := \
287287
sigtrap01 \
288288
change_mnt_context \
289289
fd_offset \
290+
massive_groups \
290291
# jobctl00 \
291292
292293
PKG_CONFIG ?= pkg-config
@@ -736,6 +737,8 @@ cgroupv2_01: LDLIBS += -pthread
736737
uprobes: CFLAGS += $(call pkg-cflags, libtracefs libtraceevent)
737738
uprobes: LDLIBS += $(call pkg-libs, libtracefs libelf)
738739

740+
massive_groups: LDLIBS += -lrt -pthread
741+
739742
$(LIB): force
740743
$(Q) $(MAKE) -C $(LIBDIR)
741744

test/zdtm/static/massive_groups.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <grp.h>
2+
#include <sys/types.h>
3+
#include <unistd.h>
4+
#include <stdlib.h>
5+
6+
#include "zdtmtst.h"
7+
8+
const char *test_doc = "Test dumping a process with many supplementary groups (Trigger bfd dynamic expansion)";
9+
const char *test_author = "DongSunchao";
10+
11+
/*
12+
* _SC_NGROUPS_MAX beyond parasite's maximum supported groups, so we use a hardcoded value.
13+
* Use 900 groups: enough to test the "Groups:" line in /proc/pid/status
14+
* and triggering dynamic expansion,
15+
* but still within the PARASITE_MAX_GROUPS limit (~900).
16+
* So we use the larger value to set GID and test the dynamic expansion of BFD buffer when dumping the process.
17+
*/
18+
#define TEST_NGROUPS 900
19+
20+
int main(int argc, char **argv)
21+
{
22+
gid_t *group;
23+
gid_t *restored_groups;
24+
int i, ngroups;
25+
int restored_ngroups;
26+
27+
test_init(argc, argv);
28+
29+
ngroups = TEST_NGROUPS;
30+
31+
group = malloc(ngroups * sizeof(gid_t));
32+
if (!group) {
33+
pr_perror("Failed to allocate memory for group IDs");
34+
return 1;
35+
}
36+
37+
/*
38+
* Fill the group array with large and unique group IDs to reach the maximum limit.
39+
* This will trigger the multiple dynamic expansion of the BFD buffer when dumping
40+
* the process.
41+
*/
42+
for (i = 0; i < ngroups; i++)
43+
group[i] = i + 1000000000;
44+
45+
if (setgroups(ngroups, group) < 0) {
46+
pr_perror("Failed to set supplementary groups");
47+
free(group);
48+
return 1;
49+
}
50+
51+
test_daemon();
52+
53+
test_waitsig();
54+
55+
restored_ngroups = getgroups(0, NULL);
56+
if (restored_ngroups < 0) {
57+
pr_perror("Failed to get number of supplementary groups");
58+
free(group);
59+
return 1;
60+
}
61+
62+
if (restored_ngroups != ngroups) {
63+
fail("Restored number of supplementary groups (%d) does not match expected (%d)", restored_ngroups, ngroups);
64+
free(group);
65+
return 1;
66+
}
67+
68+
restored_groups = malloc(restored_ngroups * sizeof(gid_t));
69+
70+
if (!restored_groups) {
71+
pr_perror("Failed to allocate memory for restored group IDs");
72+
free(group);
73+
return 1;
74+
}
75+
76+
if (getgroups(restored_ngroups, restored_groups) < 0) {
77+
pr_perror("Failed to get supplementary groups");
78+
free(restored_groups);
79+
free(group);
80+
return 1;
81+
}
82+
83+
for (i = 0; i < ngroups; i++) {
84+
if (restored_groups[i] != group[i]) {
85+
fail("Restored group ID at index %d (%u) does not match expected (%u)", i, (unsigned int)restored_groups[i], (unsigned int)group[i]);
86+
free(restored_groups);
87+
free(group);
88+
return 1;
89+
}
90+
}
91+
92+
free(restored_groups);
93+
free(group);
94+
pass();
95+
return 0;
96+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'flavor': 'h', 'flags': 'suid'}

0 commit comments

Comments
 (0)