[libc] Add stub for confstr - #218789
Conversation
POSIX defines confstr as returning strings for various macros (see: https://pubs.opengroup.org/onlinepubs/9799919799/functions/confstr.html) This PR adds an implementation as experimental that always returns that there's no valid string.
|
@llvm/pr-subscribers-libc Author: Michael Jones (michaelrj-google) ChangesPOSIX defines confstr as returning strings for various macros (see: Assisted-by: Automated tooling, human reviewed. Full diff: https://github.com/llvm/llvm-project/pull/218789.diff 10 Files Affected:
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index dfba85299c4da..8008a7776005e 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1373,6 +1373,11 @@ if(LLVM_LIBC_FULL_BUILD)
endif()
if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
+ list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # unistd.h entrypoints
+ libc.src.unistd.confstr
+ )
+
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# net/if.h entrypoints
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 1f8c64bcae127..0c7d4085534f3 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -661,6 +661,11 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
)
if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
+ list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # unistd.h entrypoints
+ libc.src.unistd.confstr
+ )
+
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# regex.h entrypoints
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 507670684930c..c049c280b13ac 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1591,6 +1591,11 @@ if(LLVM_LIBC_FULL_BUILD)
endif()
if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
+ list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # unistd.h entrypoints
+ libc.src.unistd.confstr
+ )
+
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# net/if.h entrypoints
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7420a4db3330c..1b6c678a5dc1c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1604,6 +1604,11 @@ if(LLVM_LIBC_FULL_BUILD)
endif()
if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
+ list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # unistd.h entrypoints
+ libc.src.unistd.confstr
+ )
+
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# net/if.h entrypoints
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 0d5b5e71cf7f3..d6c0235dd830f 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -103,6 +103,14 @@ functions:
- type: const char *
- type: uid_t
- type: gid_t
+ - name: confstr
+ standards:
+ - posix
+ return_type: size_t
+ arguments:
+ - type: int
+ - type: char *
+ - type: size_t
- name: close
standards:
- posix
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index c10ea20681059..6109981b032d2 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -47,6 +47,19 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.chown
)
+add_entrypoint_object(
+ confstr
+ SRCS
+ confstr.cpp
+ HDRS
+ confstr.h
+ DEPENDS
+ libc.hdr.types.size_t
+ libc.hdr.unistd_macros
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
add_entrypoint_object(
close
ALIAS
diff --git a/libc/src/unistd/confstr.cpp b/libc/src/unistd/confstr.cpp
new file mode 100644
index 0000000000000..3ea3a275928fb
--- /dev/null
+++ b/libc/src/unistd/confstr.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 of confstr
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/confstr.h"
+
+#include "hdr/types/size_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, confstr, (int, char *, size_t)) { return 0; }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/confstr.h b/libc/src/unistd/confstr.h
new file mode 100644
index 0000000000000..1792942e6c101
--- /dev/null
+++ b/libc/src/unistd/confstr.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 confstr
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_CONFSTR_H
+#define LLVM_LIBC_SRC_UNISTD_CONFSTR_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/unistd_macros.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+size_t confstr(int name, char *buf, size_t len);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_CONFSTR_H
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index a220509308bd4..016752bb16289 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -641,6 +641,17 @@ add_libc_test(
)
+add_libc_test(
+ confstr_test
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ confstr_test.cpp
+ DEPENDS
+ libc.hdr.types.size_t
+ libc.src.unistd.confstr
+)
+
add_libc_test(
sysconf_test
SUITE
diff --git a/libc/test/src/unistd/confstr_test.cpp b/libc/test/src/unistd/confstr_test.cpp
new file mode 100644
index 0000000000000..28da2bc521460
--- /dev/null
+++ b/libc/test/src/unistd/confstr_test.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Unittests for confstr
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/confstr.h"
+
+#include "hdr/types/size_t.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcConfStrTest, Basic) {
+ char buf[64] = "initial";
+ size_t ret = LIBC_NAMESPACE::confstr(0, buf, sizeof(buf));
+ EXPECT_EQ(ret, size_t(0));
+}
+
+TEST(LlvmLibcConfStrTest, NullBufZeroLen) {
+ size_t ret = LIBC_NAMESPACE::confstr(0, nullptr, 0);
+ EXPECT_EQ(ret, size_t(0));
+}
+
+TEST(LlvmLibcConfStrTest, NonExistentConfig) {
+ char buf[64];
+ size_t ret = LIBC_NAMESPACE::confstr(-1, buf, sizeof(buf));
+ EXPECT_EQ(ret, size_t(0));
+}
|
| endif() | ||
|
|
||
| if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS) | ||
| list(APPEND TARGET_LIBC_ENTRYPOINTS |
There was a problem hiding this comment.
Eh, I don't see how it's fundamentally different from, say, sysconf() where we only support a handful of values for now instead of full slate of arguments. As such, I don't think listing it in "experimental" would be helpful. The function logic is behaving as expected, it's just that we don't provide configuration strings yet. Deferring to you though.
There was a problem hiding this comment.
I'm not feeling super picky. It's technically an invalid implementation since POSIX does specify a bunch of macros but I'm not worried about it.
| HDRS | ||
| confstr.h | ||
| DEPENDS | ||
| libc.hdr.errno_macros |
There was a problem hiding this comment.
I think you need libc.src.errno.errno dependency here to actually set it.
| #define LLVM_LIBC_SRC_UNISTD_CONFSTR_H | ||
|
|
||
| #include "hdr/types/size_t.h" | ||
| #include "hdr/unistd_macros.h" |
There was a problem hiding this comment.
Should this be included in .cpp file instead?
|
|
||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
|
|
||
| TEST(LlvmLibcConfStrTest, InvalidName) { |
There was a problem hiding this comment.
Nit: also use ErrnoCheckingTest to clear out errno before the test / ensure it's validated after the test.
POSIX defines confstr as returning strings for various macros (see:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/confstr.html)
This PR adds an implementation as experimental that always returns that
there's no valid string.
Assisted-by: Automated tooling, human reviewed.