[PATCH] Verify fork() support

11 views
Skip to first unread message

Earl Chew

unread,
Jul 21, 2023, 10:16:24 AM7/21/23
to efibootg...@googlegroups.com, earl...@yahoo.com
Some tests modify global variables which are then reused in
subsequent tests. With fork() support, modifications are
contained within the child process, allowing the parent to
run the next test without having to reinitialise global state.

Signed-off-by: Earl Chew <earl...@yahoo.com>
---
tools/tests/test_main.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/tools/tests/test_main.c b/tools/tests/test_main.c
index 73b42d1..11a4c2f 100644
--- a/tools/tests/test_main.c
+++ b/tools/tests/test_main.c
@@ -13,6 +13,7 @@
*/

#include <stdlib.h>
+#include <stdio.h>
#include <check.h>
#include <fff.h>

@@ -20,7 +21,7 @@ extern Suite *ebg_test_suite(void);

int main(void)
{
- int number_failed;
+ int number_failed = 1;

Suite *s;
SRunner *sr;
@@ -28,9 +29,13 @@ int main(void)
s = ebg_test_suite();
sr = srunner_create(s);

- srunner_run_all(sr, CK_NORMAL);
- number_failed = srunner_ntests_failed(sr);
- srunner_free(sr);
+ if (srunner_fork_status(sr) != CK_FORK) {
+ fprintf(stderr, "Tests assume fork() support");
+ } else {
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ }

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
--
2.39.1

Jan Kiszka

unread,
Jul 21, 2023, 1:42:56 PM7/21/23
to Earl Chew, efibootg...@googlegroups.com
Indention is off.

> + srunner_run_all(sr, CK_NORMAL);
> + number_failed = srunner_ntests_failed(sr);
> + srunner_free(sr);
> + }
>
> return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
> }

Will that prevent calling the test suite with CK_FORK=no
(https://libcheck.github.io/check/doc/check_html/check_4.html#No-Fork-Mode)?

But I'm also not yet getting what this brings us. It only checks if
forking mode is on, right? But the commit message says that this allows
to simplify the tests - where are these simplifications?

Jan

--
Siemens AG, Technology
Linux Expert Center

Earl Chew

unread,
Jul 21, 2023, 10:44:16 PM7/21/23
to Jan Kiszka, efibootg...@googlegroups.com
Jan,

I think I might have to revise the commit message, but let's sort out
the details here first.

> Will that prevent calling the test suite with CK_FORK=no
> (https://libcheck.github.io/check/doc/check_html/check_4.html#No-Fork-Mode)?

I have libcheck compiled with CF_FORK=no, and discovered that the tests
would not pass.

I figure the tests have been written assuming CF_FORK=yes, and each test
inherits a pristine copy of global state in the forked child process.
Mutations of global state by the child process are not propagated
forward to the next test.

The point of this commit is to expose this implicit assumption as an
explicit test.

What do you think?

Earl

Jan Kiszka

unread,
Jul 22, 2023, 4:25:23 AM7/22/23
to Earl Chew, efibootg...@googlegroups.com
On 22.07.23 04:44, 'Earl Chew' via EFI Boot Guard wrote:
> Jan,
>
> I think I might have to revise the commit message, but let's sort out
> the details here first.
>
>> Will that prevent calling the test suite with CK_FORK=no
>> (https://libcheck.github.io/check/doc/check_html/check_4.html#No-Fork-Mode)?
>
> I have libcheck compiled with CF_FORK=no, and discovered that the tests
> would not pass.
>
> I figure the tests have been written assuming CF_FORK=yes, and each test
> inherits a pristine copy of global state in the forked child process.
> Mutations of global state by the child process are not propagated
> forward to the next test.
>
> The point of this commit is to expose this implicit assumption as an
> explicit test.
>
> What do you think?

OK, now it makes sense to me. Please update the patch accordingly.

Earl Chew

unread,
Jul 22, 2023, 11:35:02 AM7/22/23
to efibootg...@googlegroups.com, earl...@yahoo.com
The tests will not pass if libcheck is compiled with CF_FORK=no
because they have been written assuming CF_FORK=yes. Each test
inherits a pristine copy of global state in the forked child process.
Mutations of global state by the child process are not propagated
forward to the next test.

This commit exposes the implicit assumption of CF_FORK=yes as
an explicit test.

Signed-off-by: Earl Chew <earl...@yahoo.com>
---
tools/tests/test_main.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/tools/tests/test_main.c b/tools/tests/test_main.c
index 73b42d1..6111f91 100644
--- a/tools/tests/test_main.c
+++ b/tools/tests/test_main.c
@@ -13,6 +13,7 @@
*/

#include <stdlib.h>
+#include <stdio.h>
#include <check.h>
#include <fff.h>

@@ -20,7 +21,7 @@ extern Suite *ebg_test_suite(void);

int main(void)
{
- int number_failed;
+ int number_failed = 1;

Suite *s;
SRunner *sr;
@@ -28,9 +29,13 @@ int main(void)
s = ebg_test_suite();
sr = srunner_create(s);

- srunner_run_all(sr, CK_NORMAL);
- number_failed = srunner_ntests_failed(sr);
- srunner_free(sr);
+ if (srunner_fork_status(sr) != CK_FORK) {
+ fprintf(stderr, "Tests assume fork() support");
+ } else {
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ }

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
--
2.39.1

Jan Kiszka

unread,
Jul 24, 2023, 10:19:45 AM7/24/23
to Earl Chew, efibootg...@googlegroups.com
Thanks, applied.
Reply all
Reply to author
Forward
0 new messages