Skip to content

[libc] Add stub for confstr - #218789

Merged
michaelrj-google merged 3 commits into
llvm:mainfrom
michaelrj-google:libcConfStrStub
Aug 25, 2026
Merged

[libc] Add stub for confstr#218789
michaelrj-google merged 3 commits into
llvm:mainfrom
michaelrj-google:libcConfStrStub

Conversation

@michaelrj-google

@michaelrj-google michaelrj-google commented Aug 25, 2026

Copy link
Copy Markdown
Member

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.

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.
@michaelrj-google
michaelrj-google requested a review from a team as a code owner August 25, 2026 21:32
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/218789.diff

10 Files Affected:

  • (modified) libc/config/linux/aarch64/entrypoints.txt (+5)
  • (modified) libc/config/linux/arm/entrypoints.txt (+5)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+5)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+5)
  • (modified) libc/include/unistd.yaml (+8)
  • (modified) libc/src/unistd/CMakeLists.txt (+13)
  • (added) libc/src/unistd/confstr.cpp (+24)
  • (added) libc/src/unistd/confstr.h (+27)
  • (modified) libc/test/src/unistd/CMakeLists.txt (+11)
  • (added) libc/test/src/unistd/confstr_test.cpp (+34)
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread libc/src/unistd/confstr.cpp Outdated
Comment thread libc/test/src/unistd/confstr_test.cpp Outdated
HDRS
confstr.h
DEPENDS
libc.hdr.errno_macros

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need libc.src.errno.errno dependency here to actually set it.

Comment thread libc/src/unistd/confstr.h Outdated
#define LLVM_LIBC_SRC_UNISTD_CONFSTR_H

#include "hdr/types/size_t.h"
#include "hdr/unistd_macros.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be included in .cpp file instead?

Comment thread libc/test/src/unistd/confstr_test.cpp Outdated

using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;

TEST(LlvmLibcConfStrTest, InvalidName) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: also use ErrnoCheckingTest to clear out errno before the test / ensure it's validated after the test.

@michaelrj-google
michaelrj-google merged commit 357d84a into llvm:main Aug 25, 2026
45 checks passed
@michaelrj-google
michaelrj-google deleted the libcConfStrStub branch August 25, 2026 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants