[llvm] [llvm-exegesis] Make SubprocessMemoryTest use PIDs (PR #65245)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 3 22:47:12 PDT 2023


https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/65245:

This patch makes SubprocessMemoryTest use process PIDs during creation of the SubprocessMemory objects within the tests so that there isn't interference between multiple instances of the test running at the same time which could potentially occur in multi-user environments.

This is a continuation the review in https://reviews.llvm.org/D154680.

>From 6357a8e2f59d5239d001ca3f66defbedc35603d1 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <agrossman154 at yahoo.com>
Date: Thu, 6 Jul 2023 21:05:38 -0700
Subject: [PATCH] [llvm-exegesis] Make SubprocessMemoryTest use PIDs

This patch makes SubprocessMemoryTest use process PIDs during creation
of the SubprocessMemory objects within the tests so that there isn't
interference between multiple instances of the test running at the same
time which could potentially occur in multi-user environments.
---
 .../X86/SubprocessMemoryTest.cpp              | 43 ++++++++++++++-----
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp b/llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp
index 4a6f13642ddedf..27162cd335487f 100644
--- a/llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp
@@ -10,14 +10,20 @@
 
 #include "X86/TestBase.h"
 #include "gtest/gtest.h"
+#include <string>
 #include <unordered_map>
 
 #ifdef __linux__
 #include <endian.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <unistd.h>
 #endif // __linux__
 
+// This needs to be updated anytime a test is added or removed from the test
+// suite.
+static constexpr const size_t TestCount = 4;
+
 namespace llvm {
 namespace exegesis {
 
@@ -25,11 +31,26 @@ namespace exegesis {
 
 class SubprocessMemoryTest : public X86TestBase {
 protected:
+  int getSharedMemoryNumber(const unsigned TestNumber) {
+    // Do a process similar to 2D array indexing so that each process gets it's
+    // own shared memory space to avoid collisions. This will not overflow as
+    // the maximum value a PID can take on is 10^22.
+    return getpid() * TestCount + TestNumber;
+  }
+
   void
   testCommon(std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
-             const int MainProcessPID) {
-    EXPECT_FALSE(SM.initializeSubprocessMemory(MainProcessPID));
-    EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions, MainProcessPID));
+             const unsigned TestNumber) {
+    EXPECT_FALSE(
+        SM.initializeSubprocessMemory(getSharedMemoryNumber(TestNumber)));
+    EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions,
+                                        getSharedMemoryNumber(TestNumber)));
+  }
+
+  std::string getSharedMemoryName(const unsigned TestNumber,
+                                  const unsigned DefinitionNumber) {
+    return "/" + std::to_string(getSharedMemoryNumber(TestNumber)) + "memdef" +
+           std::to_string(DefinitionNumber);
   }
 
   void checkSharedMemoryDefinition(const std::string &DefinitionName,
@@ -59,7 +80,7 @@ TEST_F(SubprocessMemoryTest, DISABLED_OneDefinition) {
 TEST_F(SubprocessMemoryTest, OneDefinition) {
 #endif
   testCommon({{"test1", {APInt(8, 0xff), 4096, 0}}}, 0);
-  checkSharedMemoryDefinition("/0memdef0", 4096, {0xff});
+  checkSharedMemoryDefinition(getSharedMemoryName(0, 0), 4096, {0xff});
 }
 
 #if defined(__powerpc__) || defined(__s390x__)
@@ -71,9 +92,9 @@ TEST_F(SubprocessMemoryTest, MultipleDefinitions) {
               {"test2", {APInt(8, 0xbb), 4096, 1}},
               {"test3", {APInt(8, 0xcc), 4096, 2}}},
              1);
-  checkSharedMemoryDefinition("/1memdef0", 4096, {0xaa});
-  checkSharedMemoryDefinition("/1memdef1", 4096, {0xbb});
-  checkSharedMemoryDefinition("/1memdef2", 4096, {0xcc});
+  checkSharedMemoryDefinition(getSharedMemoryName(1, 0), 4096, {0xaa});
+  checkSharedMemoryDefinition(getSharedMemoryName(1, 1), 4096, {0xbb});
+  checkSharedMemoryDefinition(getSharedMemoryName(1, 2), 4096, {0xcc});
 }
 
 #if defined(__powerpc__) || defined(__s390x__)
@@ -88,9 +109,9 @@ TEST_F(SubprocessMemoryTest, DefinitionFillsCompletely) {
   std::vector<uint8_t> Test1Expected(512, 0xaa);
   std::vector<uint8_t> Test2Expected(512, 0xbb);
   std::vector<uint8_t> Test3Expected(512, 0xcc);
-  checkSharedMemoryDefinition("/2memdef0", 4096, Test1Expected);
-  checkSharedMemoryDefinition("/2memdef1", 4096, Test2Expected);
-  checkSharedMemoryDefinition("/2memdef2", 4096, Test3Expected);
+  checkSharedMemoryDefinition(getSharedMemoryName(2, 0), 4096, Test1Expected);
+  checkSharedMemoryDefinition(getSharedMemoryName(2, 1), 4096, Test2Expected);
+  checkSharedMemoryDefinition(getSharedMemoryName(2, 2), 4096, Test3Expected);
 }
 
 // The following test is only supported on little endian systems.
@@ -123,7 +144,7 @@ TEST_F(SubprocessMemoryTest, DefinitionEndTruncation) {
       Test1Expected[I] = 0xaa;
     }
   }
-  checkSharedMemoryDefinition("/3memdef0", 4096, Test1Expected);
+  checkSharedMemoryDefinition(getSharedMemoryName(3, 0), 4096, Test1Expected);
 }
 
 #endif // defined(__linux__) && !defined(__ANDROID__)



More information about the llvm-commits mailing list