Skip to content
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

[flang] fix VLA using malloc and avoid using std::vector, arg input type change to char * from std::byte #77911

Closed
wants to merge 3 commits into from

Conversation

yi-wu-arm
Copy link
Contributor

The extension getlog #74628 introduce a issue where in Solarias system, LOGIN_NAME_MAX can be undefined. this problem was fixed in #77775.
But due to the use of variable-length-array (VLA), two further patch went in with the use of std::vector. fb09447 and 18734f6.

This fix use malloc (AllocatedOrCrash and FreeMemory) instead, and change the input type from std::byte * to char *

@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jan 12, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 12, 2024

@llvm/pr-subscribers-flang-runtime

Author: Yi Wu (yi-wu-arm)

Changes

The extension getlog #74628 introduce a issue where in Solarias system, LOGIN_NAME_MAX can be undefined. this problem was fixed in #77775.
But due to the use of variable-length-array (VLA), two further patch went in with the use of std::vector. fb09447 and 18734f6.

This fix use malloc (AllocatedOrCrash and FreeMemory) instead, and change the input type from std::byte * to char *


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

3 Files Affected:

  • (modified) flang/include/flang/Runtime/extensions.h (+1-1)
  • (modified) flang/runtime/extensions.cpp (+9-6)
  • (modified) flang/unittests/Runtime/CommandTest.cpp (+10-11)
diff --git a/flang/include/flang/Runtime/extensions.h b/flang/include/flang/Runtime/extensions.h
index b34edb94ada43a..d8ddd8069e202d 100644
--- a/flang/include/flang/Runtime/extensions.h
+++ b/flang/include/flang/Runtime/extensions.h
@@ -33,7 +33,7 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
     std::int32_t &n, std::int8_t *arg, std::int64_t length);
 
 // GNU extension subroutine GETLOG(C).
-void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *name, std::int64_t length);
+void FORTRAN_PROCEDURE_NAME(getlog)(char *name, std::int64_t length);
 
 } // extern "C"
 #endif // FORTRAN_RUNTIME_EXTENSIONS_H_
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 352da8f11d9d56..7340895744dbe0 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -15,6 +15,7 @@
 #include "flang/Runtime/command.h"
 #include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/io-api.h"
+#include "flang/Runtime/memory.h"
 #include <ctime>
 
 #ifdef _WIN32
@@ -48,7 +49,7 @@ extern "C" {
 namespace Fortran::runtime {
 
 void GetUsernameEnvVar(
-    const char *envName, std::byte *arg, std::int64_t length) {
+    const char *envName, char *arg, std::int64_t length) {
   Descriptor name{*Descriptor::Create(
       1, std::strlen(envName) + 1, const_cast<char *>(envName), 0)};
   Descriptor value{*Descriptor::Create(1, length, arg, 0)};
@@ -98,7 +99,7 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
 }
 
 // CALL GETLOG(USRNAME)
-void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
+void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) {
 #if _REENTRANT || _POSIX_C_SOURCE >= 199506L
   int nameMaxLen;
 #ifdef LOGIN_NAME_MAX
@@ -108,17 +109,19 @@ void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
   if (nameMaxLen == -1)
     nameMaxLen = _POSIX_LOGIN_NAME_MAX + 1;
 #endif
-  std::vector<char> str(nameMaxLen);
+  Terminator terminator{__FILE__, __LINE__};
+  char *str{(char *)AllocateMemoryOrCrash(terminator, nameMaxLen)};
+  str[nameMaxLen] = '\0';
 
-  int error{getlogin_r(str.data(), nameMaxLen)};
+  int error{getlogin_r(str, nameMaxLen)};
   if (error == 0) {
     // no error: find first \0 in string then pad from there
-    CopyAndPad(reinterpret_cast<char *>(arg), str.data(), length,
-        std::strlen(str.data()));
+    CopyAndPad(arg, str, length, std::strlen(str));
   } else {
     // error occur: get username from environment variable
     GetUsernameEnvVar("LOGNAME", arg, length);
   }
+  FreeMemory((void*)str);
 #elif _WIN32
   // Get username from environment to avoid link to Advapi32.lib
   GetUsernameEnvVar("USERNAME", arg, length);
diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang/unittests/Runtime/CommandTest.cpp
index a0f14c519412a4..fda5f00c57d2ca 100644
--- a/flang/unittests/Runtime/CommandTest.cpp
+++ b/flang/unittests/Runtime/CommandTest.cpp
@@ -13,6 +13,8 @@
 #include "flang/Runtime/execute.h"
 #include "flang/Runtime/extensions.h"
 #include "flang/Runtime/main.h"
+#include "flang/Runtime/memory.h"
+#include "../../runtime/terminator.h"
 #include <cstddef>
 #include <cstdlib>
 
@@ -682,8 +684,7 @@ TEST_F(EnvironmentVariables, GetlogGetName) {
   const int charLen{3};
   char input[charLen]{"\0\0"};
 
-  FORTRAN_PROCEDURE_NAME(getlog)
-  (reinterpret_cast<std::byte *>(input), charLen);
+  FORTRAN_PROCEDURE_NAME(getlog)(input, charLen);
 
   EXPECT_NE(input[0], '\0');
 }
@@ -699,12 +700,13 @@ TEST_F(EnvironmentVariables, GetlogPadSpace) {
   if (charLen == -1)
     charLen = _POSIX_LOGIN_NAME_MAX + 2;
 #endif
-  std::vector<char> input(charLen);
+  Terminator terminator{__FILE__, __LINE__};
+  char *input{(char *)AllocateMemoryOrCrash(terminator, charLen)};
 
-  FORTRAN_PROCEDURE_NAME(getlog)
-  (reinterpret_cast<std::byte *>(input.data()), charLen);
+  FORTRAN_PROCEDURE_NAME(getlog)(input, charLen);
 
   EXPECT_EQ(input[charLen - 1], ' ');
+  FreeMemory((void *)input);
 }
 #endif
 
@@ -715,8 +717,7 @@ TEST_F(EnvironmentVariables, GetlogEnvGetName) {
         << "Environment variable USERNAME does not exist";
 
     char input[]{"XXXXXXXXX"};
-    FORTRAN_PROCEDURE_NAME(getlog)
-    (reinterpret_cast<std::byte *>(input), sizeof(input));
+    FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
 
     CheckCharEqStr(input, "loginName");
   }
@@ -728,8 +729,7 @@ TEST_F(EnvironmentVariables, GetlogEnvBufferShort) {
         << "Environment variable USERNAME does not exist";
 
     char input[]{"XXXXXX"};
-    FORTRAN_PROCEDURE_NAME(getlog)
-    (reinterpret_cast<std::byte *>(input), sizeof(input));
+    FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
 
     CheckCharEqStr(input, "loginN");
   }
@@ -741,8 +741,7 @@ TEST_F(EnvironmentVariables, GetlogEnvPadSpace) {
         << "Environment variable USERNAME does not exist";
 
     char input[]{"XXXXXXXXXX"};
-    FORTRAN_PROCEDURE_NAME(getlog)
-    (reinterpret_cast<std::byte *>(input), sizeof(input));
+    FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
 
     CheckCharEqStr(input, "loginName ");
   }

Copy link

github-actions bot commented Jan 12, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@yi-wu-arm
Copy link
Contributor Author

Fixed in: #78063

@yi-wu-arm yi-wu-arm closed this Jan 16, 2024
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.

None yet

3 participants