From caf508d7124353522e7604dbfea36b429469bd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Fri, 10 Sep 2021 12:51:13 +0200 Subject: [PATCH] [lldb] [test] Synchronize before the breakpoint in fork tests We set breakpoint on child_func, so synchronization inside it is too late to guarantee ordering between the parent output and child breakpoint. Split the function in two, and perform synchronization before the breakpoint. Differential Revision: https://reviews.llvm.org/D109591 --- lldb/test/Shell/Subprocess/Inputs/fork.cpp | 35 ++++++++++++---------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lldb/test/Shell/Subprocess/Inputs/fork.cpp b/lldb/test/Shell/Subprocess/Inputs/fork.cpp index f4cf1c2c42dc..81b1d116c943 100644 --- a/lldb/test/Shell/Subprocess/Inputs/fork.cpp +++ b/lldb/test/Shell/Subprocess/Inputs/fork.cpp @@ -28,19 +28,8 @@ void wait_for_parent() { assert(ret == 0); } -int child_func(void *argv0_ptr) { - const char *argv0 = static_cast(argv0_ptr); - - // NB: when using vfork(), the parent may be suspended while running - // this function, so do not rely on any synchronization until we exec -#if defined(TEST_FORK) - if (TEST_FORK != vfork) -#endif - wait_for_parent(); - - int ret = close(parent_done[1]); - assert(ret == 0); - +// This is the function we set breakpoint on. +int child_func(const char *argv0) { // we need to avoid memory modifications for vfork(), yet we want // to be able to test watchpoints, so do the next best thing // and restore the original value @@ -51,6 +40,22 @@ int child_func(void *argv0_ptr) { return 1; } +int child_top_func(void *argv0_ptr) { + const char *argv0 = static_cast(argv0_ptr); + + int ret = close(parent_done[1]); + assert(ret == 0); + + // NB: when using vfork(), the parent may be suspended while running + // this function, so do not rely on any synchronization until we exec +#if defined(TEST_FORK) + if (TEST_FORK != vfork) +#endif + wait_for_parent(); + + return child_func(argv0); +} + int main(int argc, char* argv[]) { alignas(uintmax_t) char stack[4096]; int ret; @@ -77,12 +82,12 @@ int main(int argc, char* argv[]) { assert(ret != -1); #if defined(TEST_CLONE) - pid_t pid = clone(child_func, &stack[sizeof(stack)], 0, argv[0]); + pid_t pid = clone(child_top_func, &stack[sizeof(stack)], 0, argv[0]); #elif defined(TEST_FORK) pid_t pid = TEST_FORK(); // NB: this must be equivalent to the clone() call above if (pid == 0) - _exit(child_func(argv[0])); + _exit(child_top_func(argv[0])); #endif assert(pid != -1);