Hoa Nguyen has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/35195 )

Change subject: arch-arm: Replace call to `tmpnam()` by a deterministic one
......................................................................

arch-arm: Replace call to `tmpnam()` by a deterministic one

According to the documentation, the use of tmpnam() should be
avoided.

This commit generates a temporary filename by concat-ing the
object name with an index that is internally tracked, the index
is increased until a filename that is not being used is found.

JIRA: https://gem5.atlassian.net/browse/GEM5-206

Change-Id: Ibfe604d741b6b7d7b02fc051add217f95f81d05e
Signed-off-by: Hoa Nguyen <hoangu...@ucdavis.edu>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/35195
Reviewed-by: Andreas Sandberg <andreas.sandb...@arm.com>
Maintainer: Andreas Sandberg <andreas.sandb...@arm.com>
Tested-by: kokoro <noreply+kok...@google.com>
---
M src/arch/arm/semihosting.cc
M src/arch/arm/semihosting.hh
2 files changed, 19 insertions(+), 6 deletions(-)

Approvals:
  Andreas Sandberg: Looks good to me, approved; Looks good to me, approved
  kokoro: Regressions pass



diff --git a/src/arch/arm/semihosting.cc b/src/arch/arm/semihosting.cc
index bd7b617..901fdd1 100644
--- a/src/arch/arm/semihosting.cc
+++ b/src/arch/arm/semihosting.cc
@@ -37,10 +37,14 @@

 #include "arch/arm/semihosting.hh"

+#include <unistd.h>
+
+#include <cerrno>
 #include <cstdio>

 #include "arch/arm/utility.hh"
 #include "base/logging.hh"
+#include "base/output.hh"
 #include "base/time.hh"
 #include "debug/Semihosting.hh"
 #include "dev/serial/serial.hh"
@@ -449,16 +453,21 @@
 ArmSemihosting::callTmpNam(ThreadContext *tc, Addr addr, uint64_t id,
                            size_t size)
 {
-    std::vector<char> buf(L_tmpnam);
-    char *path = tmpnam(buf.data());
-    if (!path)
-        return retError(EINVAL);
+    std::string path = "";
+    int64_t unlink_call_ret = 0;

-    const size_t path_len = strlen(path);
+    do {
+ path = simout.resolve(csprintf("%s.tmp%05i", name(), tmpNameIndex++));
+        // remove the (potentially existing) file of the given path
+        unlink_call_ret = unlink(path.c_str());
+    // if the file is busy, find another name
+    } while ((unlink_call_ret < 0) && (errno == EBUSY));
+
+    const size_t path_len = path.length();
     if (path_len >= size)
         return retError(ENOSPC);

-    portProxy(tc).writeBlob(addr, path, path_len + 1);
+    portProxy(tc).writeBlob(addr, path.c_str(), path_len + 1);
     return retOK(0);
 }

diff --git a/src/arch/arm/semihosting.hh b/src/arch/arm/semihosting.hh
index e9dc984..da0644f 100644
--- a/src/arch/arm/semihosting.hh
+++ b/src/arch/arm/semihosting.hh
@@ -581,6 +581,10 @@
     static const std::map<uint64_t, const char *> exitCodes;
     static const std::vector<uint8_t> features;
     static const std::map<const std::string, FILE *> stdioMap;
+
+    // used in callTmpNam() to deterministically generate a temp filename
+    uint16_t tmpNameIndex = 0;
+
 };

 std::ostream &operator << (

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/35195
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ibfe604d741b6b7d7b02fc051add217f95f81d05e
Gerrit-Change-Number: 35195
Gerrit-PatchSet: 14
Gerrit-Owner: Hoa Nguyen <hoangu...@ucdavis.edu>
Gerrit-Reviewer: Andreas Sandberg <andreas.sandb...@arm.com>
Gerrit-Reviewer: Bobby R. Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Gabe Black <gabebl...@google.com>
Gerrit-Reviewer: Giacomo Travaglini <giacomo.travagl...@arm.com>
Gerrit-Reviewer: Hoa Nguyen <hoangu...@ucdavis.edu>
Gerrit-Reviewer: Jason Lowe-Power <power...@gmail.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to