[libc-commits] [libc] 9344320 - [libc] Implement `execle` (#217213)
via libc-commits
libc-commits at lists.llvm.org
Wed Aug 19 20:50:51 PDT 2026
Author: Bhavesh M
Date: 2026-08-19T20:50:46-07:00
New Revision: 9344320c4787aeedeb55e63b4c33b28f49ceff10
URL: https://github.com/llvm/llvm-project/commit/9344320c4787aeedeb55e63b4c33b28f49ceff10
DIFF: https://github.com/llvm/llvm-project/commit/9344320c4787aeedeb55e63b4c33b28f49ceff10.diff
LOG: [libc] Implement `execle` (#217213)
This implements `execle` and also creates a syscall wrapper for `execve`.
Added:
libc/src/__support/OSUtil/linux/syscall_wrappers/execve.h
libc/src/unistd/execle.h
libc/src/unistd/linux/execle.cpp
libc/test/integration/src/unistd/exec_test_signal_exit.cpp
libc/test/integration/src/unistd/execle_test.cpp
libc/test/integration/src/unistd/execle_test_normal_exit.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/unistd.yaml
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/unistd/CMakeLists.txt
libc/src/unistd/linux/CMakeLists.txt
libc/test/integration/src/unistd/CMakeLists.txt
libc/test/integration/src/unistd/execv_test.cpp
libc/test/integration/src/unistd/execve_test.cpp
Removed:
libc/test/integration/src/unistd/execv_test_signal_exit.cpp
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3af5943717e84..1676e69b5e02d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -393,6 +393,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.dup
libc.src.unistd.dup2
libc.src.unistd.dup3
+ libc.src.unistd.execle
libc.src.unistd.execve
libc.src.unistd.faccessat
libc.src.unistd.fchdir
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2a40f55076178..70bf1e4a38304 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -422,6 +422,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.dup
libc.src.unistd.dup2
libc.src.unistd.dup3
+ libc.src.unistd.execle
libc.src.unistd.execve
libc.src.unistd.faccessat
libc.src.unistd.fchdir
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index cef595fc67465..89886f0f04734 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -432,6 +432,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.dup
libc.src.unistd.dup2
libc.src.unistd.dup3
+ libc.src.unistd.execle
libc.src.unistd.execve
libc.src.unistd.faccessat
libc.src.unistd.fchdir
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 4b3703af7798e..bc76587866f8f 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -128,6 +128,14 @@ functions:
- type: int
- type: int
- type: int
+ - name: execle
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: const char *
+ - type: const char *
+ - type: '...'
- name: execv
standards:
- posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 997e199460ab7..0041db9a24cbd 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -993,3 +993,15 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ execve
+ HDRS
+ execve.h
+ DEPENDS
+ libc.hdr.fcntl_macros
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/execve.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/execve.h
new file mode 100644
index 0000000000000..9c88b7603e808
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/execve.h
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for execve.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_EXECVE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_EXECVE_H
+
+#include "hdr/fcntl_macros.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> execve(const char *path, char *const argv[],
+ char *const envp[]) {
+#if defined(SYS_execve)
+ int ret = syscall_impl<int>(SYS_execve, path, argv, envp);
+#elif defined(SYS_execveat)
+ int ret = syscall_impl<int>(SYS_execveat, AT_FDCWD, path, argv, envp, 0);
+#else
+#error "execve and execveat syscalls not available."
+#endif
+ if (ret < 0)
+ return Error(-ret);
+ return ret;
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_EXECVE_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 646c5881532de..c10ea20681059 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -110,6 +110,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fpathconf
)
+add_entrypoint_object(
+ execle
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.execle
+)
+
add_entrypoint_object(
execv
ALIAS
diff --git a/libc/src/unistd/execle.h b/libc/src/unistd/execle.h
new file mode 100644
index 0000000000000..a2ec59cca454d
--- /dev/null
+++ b/libc/src/unistd/execle.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for execle
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_EXECLE_H
+#define LLVM_LIBC_SRC_UNISTD_EXECLE_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int execle(const char *path, const char *arg0, ...);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_EXECLE_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 7a8b51e9514aa..d710f84742d59 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -162,6 +162,18 @@ add_entrypoint_object(
libc.src.unistd.linux.pathconf_utils
)
+add_entrypoint_object(
+ execle
+ SRCS
+ execle.cpp
+ HDRS
+ ../execle.h
+ DEPENDS
+ libc.hdr.types.size_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.execve
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
execv
SRCS
diff --git a/libc/src/unistd/linux/execle.cpp b/libc/src/unistd/linux/execle.cpp
new file mode 100644
index 0000000000000..fae705abd98a2
--- /dev/null
+++ b/libc/src/unistd/linux/execle.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of execle
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/execle.h"
+
+#include "hdr/types/size_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/execve.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, execle, (const char *path, const char *arg0, ...)) {
+ va_list varargs, varargs_copy;
+ va_start(varargs, arg0);
+ va_copy(varargs_copy, varargs);
+
+ size_t argc = 1;
+ while (va_arg(varargs, const char *) != nullptr)
+ ++argc;
+ va_end(varargs);
+
+ char *argv[argc + 1];
+ argv[0] = const_cast<char *>(arg0);
+
+ for (size_t i = 1; i <= argc; ++i)
+ argv[i] = va_arg(varargs_copy, char *);
+ char **envp = va_arg(varargs_copy, char **);
+ va_end(varargs_copy);
+
+ auto ret = linux_syscalls::execve(path, argv, envp);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+
+ // Control will not reach here on success but have a return statement will
+ // keep the compilers happy.
+ return *ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/integration/src/unistd/CMakeLists.txt b/libc/test/integration/src/unistd/CMakeLists.txt
index 3c09ae43b4dd7..4091082486b96 100644
--- a/libc/test/integration/src/unistd/CMakeLists.txt
+++ b/libc/test/integration/src/unistd/CMakeLists.txt
@@ -61,6 +61,18 @@ if((${LIBC_TARGET_OS} STREQUAL "linux") AND (${LIBC_TARGET_ARCHITECTURE_IS_X86})
)
endif()
+add_executable(
+ libc_exec_test_signal_exit
+ EXCLUDE_FROM_ALL
+ exec_test_signal_exit.cpp
+)
+set_target_properties(
+ libc_exec_test_signal_exit
+ PROPERTIES
+ OUTPUT_NAME libc_exec_test_signal_exit
+ RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+)
+
add_executable(
libc_execv_test_normal_exit
EXCLUDE_FROM_ALL
@@ -74,17 +86,34 @@ set_target_properties(
)
add_executable(
- libc_execv_test_signal_exit
+ libc_execle_test_normal_exit
EXCLUDE_FROM_ALL
- execv_test_signal_exit.cpp
+ execle_test_normal_exit.cpp
)
set_target_properties(
- libc_execv_test_signal_exit
+ libc_execle_test_normal_exit
PROPERTIES
- OUTPUT_NAME libc_execv_test_signal_exit
+ OUTPUT_NAME libc_execle_test_normal_exit
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
+add_integration_test(
+ execle_test
+ SUITE
+ unistd-integration-tests
+ SRCS
+ execle_test.cpp
+ DEPENDS
+ libc_execle_test_normal_exit
+ libc_exec_test_signal_exit
+ libc.src.errno.errno
+ libc.src.sys.wait.waitpid
+ libc.src.unistd.execle
+ libc.src.unistd.fork
+ ENV
+ EXECLE_TEST=PASS
+)
+
add_integration_test(
execv_test
SUITE
@@ -93,7 +122,7 @@ add_integration_test(
execv_test.cpp
DEPENDS
libc_execv_test_normal_exit
- libc_execv_test_signal_exit
+ libc_exec_test_signal_exit
libc.src.errno.errno
libc.src.sys.wait.waitpid
libc.src.unistd.execv
@@ -110,7 +139,7 @@ add_integration_test(
execve_test.cpp
DEPENDS
libc_execv_test_normal_exit
- libc_execv_test_signal_exit
+ libc_exec_test_signal_exit
libc.src.errno.errno
libc.src.sys.wait.waitpid
libc.src.unistd.execve
diff --git a/libc/test/integration/src/unistd/exec_test_signal_exit.cpp b/libc/test/integration/src/unistd/exec_test_signal_exit.cpp
new file mode 100644
index 0000000000000..bfcd25baa827b
--- /dev/null
+++ b/libc/test/integration/src/unistd/exec_test_signal_exit.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Helper that terminates via signal (SIGUSR1) to test exec*
+///
+//===----------------------------------------------------------------------===//
+
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main() {
+ char *env = getenv("__MISSING_ENV_VAR__");
+ if (env == nullptr)
+ raise(SIGUSR1);
+ return 0;
+}
diff --git a/libc/test/integration/src/unistd/execle_test.cpp b/libc/test/integration/src/unistd/execle_test.cpp
new file mode 100644
index 0000000000000..100c556dcb0f9
--- /dev/null
+++ b/libc/test/integration/src/unistd/execle_test.cpp
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// Unittests for execle
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/wait/waitpid.h"
+#include "src/unistd/execle.h"
+#include "src/unistd/fork.h"
+
+#include "test/IntegrationTest/test.h"
+
+#include <signal.h>
+#include <sys/wait.h>
+
+void fork_and_execle_normal_exit(char **envp) {
+ pid_t pid = LIBC_NAMESPACE::fork();
+ if (pid == 0) {
+ const char *path = "libc_execle_test_normal_exit";
+ LIBC_NAMESPACE::execle(path, const_cast<char *>("execle_test_normal_exit"),
+ const_cast<char *>("first"),
+ const_cast<char *>("second"),
+ static_cast<char *>(nullptr), envp);
+ }
+
+ ASSERT_TRUE(pid > 0);
+ int status;
+ pid_t cpid = LIBC_NAMESPACE::waitpid(pid, &status, 0);
+ ASSERT_TRUE(cpid > 0);
+ ASSERT_EQ(cpid, pid);
+ ASSERT_TRUE(WIFEXITED(status));
+}
+
+void fork_and_execle_signal_exit(char **envp) {
+ pid_t pid = LIBC_NAMESPACE::fork();
+ if (pid == 0) {
+ const char *path = "libc_exec_test_signal_exit";
+ LIBC_NAMESPACE::execle(path, const_cast<char *>("exec_test_signal_exit"),
+ static_cast<char *>(nullptr), envp);
+ }
+ ASSERT_TRUE(pid > 0);
+ int status;
+ pid_t cpid = LIBC_NAMESPACE::waitpid(pid, &status, 0);
+ ASSERT_TRUE(cpid > 0);
+ ASSERT_EQ(cpid, pid);
+ ASSERT_FALSE(WIFEXITED(status));
+ ASSERT_TRUE(WTERMSIG(status) == SIGUSR1);
+}
+
+TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
+ char **envp) {
+ fork_and_execle_normal_exit(envp);
+ fork_and_execle_signal_exit(envp);
+ return 0;
+}
diff --git a/libc/test/integration/src/unistd/execle_test_normal_exit.cpp b/libc/test/integration/src/unistd/execle_test_normal_exit.cpp
new file mode 100644
index 0000000000000..b2648704a7fae
--- /dev/null
+++ b/libc/test/integration/src/unistd/execle_test_normal_exit.cpp
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+ if (argc != 3)
+ raise(SIGUSR1);
+ if (strcmp(argv[0], "execle_test_normal_exit") != 0)
+ raise(SIGUSR1);
+ if (strcmp(argv[1], "first") != 0)
+ raise(SIGUSR1);
+ if (strcmp(argv[2], "second") != 0)
+ raise(SIGUSR1);
+
+ char *env = getenv("EXECLE_TEST");
+ if (env == nullptr || strcmp(env, "PASS") != 0)
+ raise(SIGUSR1);
+
+ return 0;
+}
diff --git a/libc/test/integration/src/unistd/execv_test.cpp b/libc/test/integration/src/unistd/execv_test.cpp
index bdd54d420760c..ffffd89af091f 100644
--- a/libc/test/integration/src/unistd/execv_test.cpp
+++ b/libc/test/integration/src/unistd/execv_test.cpp
@@ -36,9 +36,9 @@ void fork_and_execv_normal_exit() {
void fork_and_execv_signal_exit() {
pid_t pid = LIBC_NAMESPACE::fork();
if (pid == 0) {
- const char *path = "libc_execv_test_signal_exit";
+ const char *path = "libc_exec_test_signal_exit";
char *const argv[] = {
- const_cast<char *>("execv_test_normal_exit"),
+ const_cast<char *>("exec_test_signal_exit"),
nullptr,
};
LIBC_NAMESPACE::execv(path, argv);
diff --git a/libc/test/integration/src/unistd/execv_test_signal_exit.cpp b/libc/test/integration/src/unistd/execv_test_signal_exit.cpp
deleted file mode 100644
index e266b5c07490f..0000000000000
--- a/libc/test/integration/src/unistd/execv_test_signal_exit.cpp
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <signal.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-int main() {
- char *env = getenv("__MISSING_ENV_VAR__");
- if (env == nullptr)
- raise(SIGUSR1);
- return 0;
-}
diff --git a/libc/test/integration/src/unistd/execve_test.cpp b/libc/test/integration/src/unistd/execve_test.cpp
index 1204de3984c7e..e4ddf6f1879dd 100644
--- a/libc/test/integration/src/unistd/execve_test.cpp
+++ b/libc/test/integration/src/unistd/execve_test.cpp
@@ -36,9 +36,9 @@ void fork_and_execv_normal_exit(char **envp) {
void fork_and_execv_signal_exit(char **envp) {
pid_t pid = LIBC_NAMESPACE::fork();
if (pid == 0) {
- const char *path = "libc_execv_test_signal_exit";
+ const char *path = "libc_exec_test_signal_exit";
char *const argv[] = {
- const_cast<char *>("execv_test_normal_exit"),
+ const_cast<char *>("exec_test_signal_exit"),
nullptr,
};
LIBC_NAMESPACE::execve(path, argv, envp);
More information about the libc-commits
mailing list