[libc-commits] [libc] [libc][posix] implement _exit (PR #87185)
via libc-commits
libc-commits at lists.llvm.org
Sun Mar 31 00:55:35 PDT 2024
https://github.com/aniplcc created https://github.com/llvm/llvm-project/pull/87185
Fixes #87126.
>From b4ccc17833460103f1c99b6a6c58aa3c9c11da16 Mon Sep 17 00:00:00 2001
From: aniplcc <aniplccode at gmail.com>
Date: Sun, 31 Mar 2024 13:01:49 +0530
Subject: [PATCH 1/2] [libc][posix] implement _exit
---
libc/config/gpu/entrypoints.txt | 3 +++
libc/config/gpu/headers.txt | 1 +
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/spec/posix.td | 5 +++++
libc/src/unistd/CMakeLists.txt | 13 +++++++++++++
libc/src/unistd/_exit.cpp | 19 +++++++++++++++++++
libc/src/unistd/_exit.h | 18 ++++++++++++++++++
libc/test/src/unistd/CMakeLists.txt | 14 ++++++++++++++
libc/test/src/unistd/_exit_test.cpp | 15 +++++++++++++++
11 files changed, 91 insertions(+)
create mode 100644 libc/src/unistd/_exit.cpp
create mode 100644 libc/src/unistd/_exit.h
create mode 100644 libc/test/src/unistd/_exit_test.cpp
diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 4fb87cb9f5a33e..827b5937e63602 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -211,6 +211,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# gpu/rpc.h entrypoints
libc.src.gpu.rpc_host_call
+
+ # unistd.h entrypoints
+ libc.src.unistd._exit
)
set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/gpu/headers.txt b/libc/config/gpu/headers.txt
index dd16938da8a447..37c063a7ef6f7e 100644
--- a/libc/config/gpu/headers.txt
+++ b/libc/config/gpu/headers.txt
@@ -12,6 +12,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.errno
libc.include.stdlib
libc.include.stdio
+ libc.include.unistd
# Header for RPC extensions
libc.include.gpu_rpc
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 78da7f0b334b1f..128d7560b73daa 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -674,6 +674,7 @@ if(LLVM_LIBC_FULL_BUILD)
# unistd.h entrypoints
libc.src.unistd.__llvm_libc_syscall
+ libc.src.unistd._exit
libc.src.unistd.environ
libc.src.unistd.execv
libc.src.unistd.getopt
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5aae4e246cfb3c..b4e0f9ebd3e2dc 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -705,6 +705,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.time
# unistd.h entrypoints
+ libc.src.unistd._exit
libc.src.unistd.environ
libc.src.unistd.execv
libc.src.unistd.fork
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5b428e51aee620..0740acf3a026c1 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -771,6 +771,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.time
# unistd.h entrypoints
+ libc.src.unistd._exit
libc.src.unistd.environ
libc.src.unistd.execv
libc.src.unistd.fork
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index 8444a449ebe5be..7a48ecdd42de98 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -404,6 +404,11 @@ def POSIX : StandardSpec<"POSIX"> {
],
[], // Enumerations
[
+ FunctionSpec<
+ "_Exit",
+ RetValSpec<NoReturn>,
+ [ArgSpec<IntType>]
+ >,
FunctionSpec<
"access",
RetValSpec<IntType>,
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index e22b0e1872caa1..0a9372e07c75e4 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -2,6 +2,19 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()
+add_entrypoint_object(
+ _exit
+ SRCS
+ _exit.cpp
+ HDRS
+ _exit.h
+ DEPENDS
+ libc.include.stdlib
+ libc.include.unistd
+ libc.src.__support.OSUtil.osutil
+ libc.src.stdlib._Exit
+)
+
add_entrypoint_object(
access
ALIAS
diff --git a/libc/src/unistd/_exit.cpp b/libc/src/unistd/_exit.cpp
new file mode 100644
index 00000000000000..01b08c285e5563
--- /dev/null
+++ b/libc/src/unistd/_exit.cpp
@@ -0,0 +1,19 @@
+//===------------------- Implementation of _exit --------------------------===//
+//
+// 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/_exit.h"
+#include "src/__support/common.h"
+#include "src/stdlib/_Exit.h"
+
+namespace LIBC_NAMESPACE {
+
+[[noreturn]] LLVM_LIBC_FUNCTION(void, _exit, (int status)) {
+ LIBC_NAMESPACE::_Exit(status);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/_exit.h b/libc/src/unistd/_exit.h
new file mode 100644
index 00000000000000..141b8753218915
--- /dev/null
+++ b/libc/src/unistd/_exit.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for _exit -------------------------*- 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__EXIT_H
+#define LLVM_LIBC_SRC_UNISTD__EXIT_H
+
+namespace LIBC_NAMESPACE {
+
+[[noreturn]] void _exit(int status);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD__EXIT_H
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 3a7fe6f45c0911..9de5081f0094ea 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -444,3 +444,17 @@ add_libc_test(
libc.src.stdio.fopencookie
libc.src.stdio.fflush
)
+
+if(LLVM_LIBC_FULL_BUILD)
+ add_libc_test(
+ _exit_test
+ UNIT_TEST_ONLY
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ _exit_test.cpp
+ DEPENDS
+ libc.include.unistd
+ libc.src.unistd._exit
+ )
+endif()
diff --git a/libc/test/src/unistd/_exit_test.cpp b/libc/test/src/unistd/_exit_test.cpp
new file mode 100644
index 00000000000000..ff3fc39e112f8d
--- /dev/null
+++ b/libc/test/src/unistd/_exit_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for _exit -----------------------------------------------===//
+//
+// 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/_exit.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcUnistdExitTest, _exit) {
+ EXPECT_EXITS([] { LIBC_NAMESPACE::_exit(1); }, 1);
+ EXPECT_EXITS([] { LIBC_NAMESPACE::_exit(65); }, 65);
+}
>From aa5fd19e9ac99b78b889e0196b0c7a18f9c4a33d Mon Sep 17 00:00:00 2001
From: aniplcc <aniplccode at gmail.com>
Date: Sun, 31 Mar 2024 13:06:25 +0530
Subject: [PATCH 2/2] fix typo in spec
---
libc/spec/posix.td | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index 7a48ecdd42de98..cfa8d3afedde3f 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -405,7 +405,7 @@ def POSIX : StandardSpec<"POSIX"> {
[], // Enumerations
[
FunctionSpec<
- "_Exit",
+ "_exit",
RetValSpec<NoReturn>,
[ArgSpec<IntType>]
>,
More information about the libc-commits
mailing list