-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Add a minimal implementation of the POSIX fork function.
A very simple and minimal implementation of fork is added. Future changes will add more functionality to satisfy POSIX and Linux requirements. An implementation of wait and a few support macros in sys/wait.h have also been added to help with testing the fork function. Reviewed By: lntue, michaelrj Differential Revision: https://reviews.llvm.org/D135131
- Loading branch information
Siva Chandra Reddy
committed
Oct 4, 2022
1 parent
b39b805
commit e3638e8
Showing
21 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//===-- Definition of macros from sys/wait.h ------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H | ||
#define __LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H | ||
|
||
// Wait status info macros | ||
#define WTERMSIG(status) (((status)&0x7F)) | ||
#define WIFEXITED(status) (WTERMSIG(status) == 0) | ||
#define WEXITSTATUS(status) (((status)&0xFF00) >> 8) | ||
|
||
#endif // __LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//===-- Macros defined in sys/wait.h header file --------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __LLVM_LIBC_MACROS_SYS_WAIT_MACROS_H | ||
#define __LLVM_LIBC_MACROS_SYS_WAIT_MACROS_H | ||
|
||
#ifdef __linux__ | ||
#include "linux/sys-wait-macros.h" | ||
#endif | ||
|
||
#endif // __LLVM_LIBC_MACROS_SYS_WAIT_MACROS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===-- POSIX header wait.h -----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SYS_WAIT_H | ||
#define LLVM_LIBC_SYS_WAIT_H | ||
|
||
#include <__llvm-libc-common.h> | ||
|
||
#include <llvm-libc-macros/sys-wait-macros.h> | ||
|
||
%%public_api() | ||
|
||
#endif // LLVM_LIBC_SYS_WAIT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) | ||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) | ||
endif() | ||
|
||
add_entrypoint_object( | ||
wait | ||
ALIAS | ||
DEPENDS | ||
.${LIBC_TARGET_OS}.wait | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_entrypoint_object( | ||
wait | ||
SRCS | ||
wait.cpp | ||
HDRS | ||
../wait.h | ||
DEPENDS | ||
libc.include.errno | ||
libc.include.sys_wait | ||
libc.include.sys_syscall | ||
libc.src.__support.OSUtil.osutil | ||
libc.src.errno.errno | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===-- Linux implementation of wait --------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/sys/wait/wait.h" | ||
|
||
#include "src/__support/OSUtil/syscall.h" // For internal syscall function. | ||
#include "src/__support/common.h" | ||
|
||
#include <errno.h> | ||
#include <sys/syscall.h> // For syscall numbers. | ||
#include <sys/wait.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
// The implementation of wait here is very minimal. We will add more | ||
// functionality and standard compliance in future. | ||
|
||
LLVM_LIBC_FUNCTION(pid_t, wait, (int *wait_status)) { | ||
pid_t pid = __llvm_libc::syscall_impl(SYS_wait4, -1, wait_status, 0, 0); | ||
if (pid < 0) { | ||
// Error case, a child process was not created. | ||
errno = -pid; | ||
return -1; | ||
} | ||
|
||
return pid; | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for wait --------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_SYS_WAIT_WAIT_H | ||
#define LLVM_LIBC_SRC_SYS_WAIT_WAIT_H | ||
|
||
#include <sys/wait.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
pid_t wait(int *waitstatus); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_SYS_WAIT_WAIT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for fork --------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_UNISTD_FORK_H | ||
#define LLVM_LIBC_SRC_UNISTD_FORK_H | ||
|
||
#include <unistd.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
pid_t fork(); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_UNISTD_FORK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//===-- Linux implementation of fork --------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/unistd/fork.h" | ||
|
||
#include "src/__support/OSUtil/syscall.h" // For internal syscall function. | ||
#include "src/__support/common.h" | ||
#include "src/__support/threads/thread.h" // For thread self object | ||
|
||
#include <errno.h> | ||
#include <sys/syscall.h> // For syscall numbers. | ||
|
||
namespace __llvm_libc { | ||
|
||
// The implementation of fork here is very minimal. We will add more | ||
// functionality and standard compliance in future. | ||
|
||
LLVM_LIBC_FUNCTION(pid_t, fork, (void)) { | ||
#ifdef SYS_fork | ||
pid_t ret = __llvm_libc::syscall_impl(SYS_fork); | ||
#elif defined(SYS_clone) | ||
pid_t ret = __llvm_libc::syscall_impl(SYS_clone, SIGCHLD, 0); | ||
#else | ||
#error "SYS_fork or SYS_clone not available." | ||
#endif | ||
if (ret == 0) { | ||
// Return value is 0 in the child process. | ||
// The child is created with a single thread whose self object will be a | ||
// copy of parent process' thread which called fork. So, we have to fix up | ||
// the child process' self object with the new process' tid. | ||
self.attrib->tid = __llvm_libc::syscall_impl(SYS_gettid); | ||
return 0; | ||
} | ||
|
||
if (ret < 0) { | ||
// Error case, a child process was not created. | ||
errno = -ret; | ||
return -1; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
add_custom_target(unistd-integration-tests) | ||
add_dependencies(libc-integration-tests unistd-integration-tests) | ||
|
||
add_integration_test( | ||
fork_test | ||
SUITE | ||
unistd-integration-tests | ||
SRCS | ||
fork_test.cpp | ||
LOADER | ||
libc.loader.linux.crt1 | ||
DEPENDS | ||
libc.include.errno | ||
libc.include.signal | ||
libc.include.sys_wait | ||
libc.include.unistd | ||
libc.src.signal.raise | ||
libc.src.sys.wait.wait | ||
libc.src.unistd.fork | ||
) |
Oops, something went wrong.