Skip to content

[flang] Handle missing LOGIN_NAME_MAX definition in runtime #77775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2024

Conversation

rorth
Copy link
Collaborator

@rorth rorth commented Jan 11, 2024

18af032 broke the Solaris build:

/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:24: error: use of undeclared identifier 'LOGIN_NAME_MAX'
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |                        ^
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   61 |   char str[nameMaxLen];
      |            ^~~~~~~~~~
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: note: initializer of 'nameMaxLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:13: note: declared here
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |             ^

flang/unittests/Runtime/CommandTest.cpp has the same issue.

As documented in Solaris 11.4 limits.h(3HEAD), LOGIN_NAME_MAX can be undefined. To determine the value, sysconf(3C) needs to be used instead.

Beside that portable method, Solaris also provides a non-standard LOGNAME_MAX which could be used, but I've preferred the standard route instead which would support other targets with the same issue.

Tested on amd64-pc-solaris2.11 and x86_64-pc-linux-gnu.

18af032 broke the Solaris build:
```
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:24: error: use of undeclared identifier 'LOGIN_NAME_MAX'
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |                        ^
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   61 |   char str[nameMaxLen];
      |            ^~~~~~~~~~
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: note: initializer of 'nameMaxLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:13: note: declared here
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |             ^
```
`flang/unittests/Runtime/CommandTest.cpp` has the same issue.

As documented in Solaris 11.4 `limits.h(3HEAD)`, `LOGIN_NAME_MAX` can be
undefined.  To determine the value, `sysconf(3C)` needs to be used instead.

Beside that portable method, Solaris also provides a non-standard
`LOGNAME_MAX` which could be used, but I've preferred the standard route
instead which would support other targets with the same issue.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jan 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 11, 2024

@llvm/pr-subscribers-flang-runtime

Author: Rainer Orth (rorth)

Changes

18af032 broke the Solaris build:

/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:24: error: use of undeclared identifier 'LOGIN_NAME_MAX'
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |                        ^
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   61 |   char str[nameMaxLen];
      |            ^~~~~~~~~~
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: note: initializer of 'nameMaxLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:13: note: declared here
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |             ^

flang/unittests/Runtime/CommandTest.cpp has the same issue.

As documented in Solaris 11.4 limits.h(3HEAD), LOGIN_NAME_MAX can be undefined. To determine the value, sysconf(3C) needs to be used instead.

Beside that portable method, Solaris also provides a non-standard LOGNAME_MAX which could be used, but I've preferred the standard route instead which would support other targets with the same issue.

Tested on amd64-pc-solaris2.11 and x86_64-pc-linux-gnu.


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

2 Files Affected:

  • (modified) flang/runtime/extensions.cpp (+8-1)
  • (modified) flang/unittests/Runtime/CommandTest.cpp (+8-1)
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 1c025d40b39524..d8fd2dff27742f 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -57,7 +57,14 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
 // CALL GETLOG(USRNAME)
 void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
 #if _REENTRANT || _POSIX_C_SOURCE >= 199506L
-  const int nameMaxLen{LOGIN_NAME_MAX + 1};
+  int nameMaxLen;
+#ifdef LOGIN_NAME_MAX
+  nameMaxLen = LOGIN_NAME_MAX + 1;
+#else
+  nameMaxLen = sysconf(_SC_LOGIN_NAME_MAX) + 1;
+  if (nameMaxLen == -1)
+    nameMaxLen = _POSIX_LOGIN_NAME_MAX + 1;
+#endif
   char str[nameMaxLen];
 
   int error{getlogin_r(str, nameMaxLen)};
diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang/unittests/Runtime/CommandTest.cpp
index 50b11d7fe8a0d5..0085d42edd7076 100644
--- a/flang/unittests/Runtime/CommandTest.cpp
+++ b/flang/unittests/Runtime/CommandTest.cpp
@@ -635,7 +635,14 @@ TEST_F(EnvironmentVariables, GetlogGetName) {
 #if _REENTRANT || _POSIX_C_SOURCE >= 199506L
 TEST_F(EnvironmentVariables, GetlogPadSpace) {
   // guarantee 1 char longer than max, last char should be pad space
-  const int charLen{LOGIN_NAME_MAX + 2};
+  int charLen;
+#ifdef LOGIN_NAME_MAX
+  charLen = LOGIN_NAME_MAX + 2;
+#else
+  charLen = sysconf(_SC_LOGIN_NAME_MAX) + 2;
+  if (charLen == -1)
+    charLen = _POSIX_LOGIN_NAME_MAX + 2;
+#endif
   char input[charLen];
 
   FORTRAN_PROCEDURE_NAME(getlog)

Copy link
Contributor

@yiwu0b11 yiwu0b11 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix!

@rorth rorth merged commit 731b295 into llvm:main Jan 11, 2024
@clementval
Copy link
Contributor

This might have broke a buildbot https://lab.llvm.org/buildbot/#/builders/175

@rorth
Copy link
Collaborator Author

rorth commented Jan 11, 2024

It seems so, yes. AFAICS that bot's use of -Werror is a non-standard configuration, so I missed this.

Unless I'm mistaken, this patch fixes the warnings.
flang-vla.txt

At least it made the warning vanish on amd64-pc-solaris2.11 and x86_64-pc-linux-gnu and passed testing on the latter.

@rorth
Copy link
Collaborator Author

rorth commented Jan 12, 2024

I happened to notice that @kazutakahirata silently fixed the issue slightly differently in fb09447 and 18734f6.

@yiwu0b11
Copy link
Contributor

I happened to notice that @kazutakahirata silently fixed the issue slightly differently in fb09447 and 18734f6.

@klausler notice a problem in this two fixes, https://flang-compiler.slack.com/archives/C5C58TT32/p1705019662369869
"also, i think that somebody must have introduced a dependency between the Fortran runtime library and the C++ runtime via use of operator new rather than calling runtime::AllocateMemory(). … probably via use of std::vector<> in the runtime (getlog in extensions.cpp)"

justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
18af032 broke the Solaris build:
```
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:24: error: use of undeclared identifier 'LOGIN_NAME_MAX'
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |                        ^
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   61 |   char str[nameMaxLen];
      |            ^~~~~~~~~~
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: note: initializer of 'nameMaxLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:13: note: declared here
   60 |   const int nameMaxLen{LOGIN_NAME_MAX + 1};
      |             ^
```
`flang/unittests/Runtime/CommandTest.cpp` has the same issue.

As documented in Solaris 11.4 `limits.h(3HEAD)`, `LOGIN_NAME_MAX` can be
undefined. To determine the value, `sysconf(3C)` needs to be used
instead.

Beside that portable method, Solaris also provides a non-standard
`LOGNAME_MAX` which could be used, but I've preferred the standard route
instead which would support other targets with the same issue.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants