Files
TheRock/compiler/amd-llvm/lldb/test/Shell/Subprocess/Inputs/fork.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

116 lines
2.6 KiB
C++

#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#if defined(TEST_CLONE)
#include <sched.h>
#endif
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int g_val = 0;
void parent_func() {
g_val = 1;
printf("function run in parent\n");
}
int parent_done[2];
char parent_done_str[16];
void wait_for_parent() {
char buf[2];
// wait for the parent to finish its part
int ret = read(parent_done[0], buf, sizeof(buf));
assert(ret == 2);
ret = close(parent_done[0]);
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
g_val = 2;
g_val = 0;
execl(argv0, argv0, parent_done_str, NULL);
assert(0 && "this should not be reached");
return 1;
}
int child_top_func(void *argv0_ptr) {
const char *argv0 = static_cast<char*>(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;
if (argv[1]) {
parent_done[0] = atoi(argv[1]);
assert(parent_done[0] != 0);
#if defined(TEST_FORK)
// for vfork(), we need to synchronize after exec
if (TEST_FORK == vfork)
wait_for_parent();
#endif
fprintf(stderr, "function run in exec'd child\n");
return 0;
}
ret = pipe(parent_done);
assert(ret == 0);
ret = snprintf(parent_done_str, sizeof(parent_done_str),
"%d", parent_done[0]);
assert(ret != -1);
#if defined(TEST_CLONE)
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_top_func(argv[0]));
#endif
assert(pid != -1);
ret = close(parent_done[0]);
assert(ret == 0);
parent_func();
// resume the child
ret = write(parent_done[1], "go", 2);
assert(ret == 2);
ret = close(parent_done[1]);
assert(ret == 0);
int status, wait_flags = 0;
#if defined(TEST_CLONE)
wait_flags = __WALL;
#endif
pid_t waited = waitpid(pid, &status, wait_flags);
assert(waited == pid);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);
return 0;
}