Skip to content

Commit adb01de

Browse files
[llvm-exegesis] Make SubprocessMemoryTest use PIDs (#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.
1 parent 4b17c81 commit adb01de

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,47 @@
1010

1111
#include "X86/TestBase.h"
1212
#include "gtest/gtest.h"
13+
#include <string>
1314
#include <unordered_map>
1415

1516
#ifdef __linux__
1617
#include <endian.h>
1718
#include <fcntl.h>
1819
#include <sys/mman.h>
20+
#include <unistd.h>
1921
#endif // __linux__
2022

23+
// This needs to be updated anytime a test is added or removed from the test
24+
// suite.
25+
static constexpr const size_t TestCount = 4;
26+
2127
namespace llvm {
2228
namespace exegesis {
2329

2430
#if defined(__linux__) && !defined(__ANDROID__)
2531

2632
class SubprocessMemoryTest : public X86TestBase {
2733
protected:
34+
int getSharedMemoryNumber(const unsigned TestNumber) {
35+
// Do a process similar to 2D array indexing so that each process gets it's
36+
// own shared memory space to avoid collisions. This will not overflow as
37+
// the maximum value a PID can take on is 10^22.
38+
return getpid() * TestCount + TestNumber;
39+
}
40+
2841
void
2942
testCommon(std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
30-
const int MainProcessPID) {
31-
EXPECT_FALSE(SM.initializeSubprocessMemory(MainProcessPID));
32-
EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions, MainProcessPID));
43+
const unsigned TestNumber) {
44+
EXPECT_FALSE(
45+
SM.initializeSubprocessMemory(getSharedMemoryNumber(TestNumber)));
46+
EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions,
47+
getSharedMemoryNumber(TestNumber)));
48+
}
49+
50+
std::string getSharedMemoryName(const unsigned TestNumber,
51+
const unsigned DefinitionNumber) {
52+
return "/" + std::to_string(getSharedMemoryNumber(TestNumber)) + "memdef" +
53+
std::to_string(DefinitionNumber);
3354
}
3455

3556
void checkSharedMemoryDefinition(const std::string &DefinitionName,
@@ -59,7 +80,7 @@ TEST_F(SubprocessMemoryTest, DISABLED_OneDefinition) {
5980
TEST_F(SubprocessMemoryTest, OneDefinition) {
6081
#endif
6182
testCommon({{"test1", {APInt(8, 0xff), 4096, 0}}}, 0);
62-
checkSharedMemoryDefinition("/0memdef0", 4096, {0xff});
83+
checkSharedMemoryDefinition(getSharedMemoryName(0, 0), 4096, {0xff});
6384
}
6485

6586
#if defined(__powerpc__) || defined(__s390x__)
@@ -71,9 +92,9 @@ TEST_F(SubprocessMemoryTest, MultipleDefinitions) {
7192
{"test2", {APInt(8, 0xbb), 4096, 1}},
7293
{"test3", {APInt(8, 0xcc), 4096, 2}}},
7394
1);
74-
checkSharedMemoryDefinition("/1memdef0", 4096, {0xaa});
75-
checkSharedMemoryDefinition("/1memdef1", 4096, {0xbb});
76-
checkSharedMemoryDefinition("/1memdef2", 4096, {0xcc});
95+
checkSharedMemoryDefinition(getSharedMemoryName(1, 0), 4096, {0xaa});
96+
checkSharedMemoryDefinition(getSharedMemoryName(1, 1), 4096, {0xbb});
97+
checkSharedMemoryDefinition(getSharedMemoryName(1, 2), 4096, {0xcc});
7798
}
7899

79100
#if defined(__powerpc__) || defined(__s390x__)
@@ -88,9 +109,9 @@ TEST_F(SubprocessMemoryTest, DefinitionFillsCompletely) {
88109
std::vector<uint8_t> Test1Expected(512, 0xaa);
89110
std::vector<uint8_t> Test2Expected(512, 0xbb);
90111
std::vector<uint8_t> Test3Expected(512, 0xcc);
91-
checkSharedMemoryDefinition("/2memdef0", 4096, Test1Expected);
92-
checkSharedMemoryDefinition("/2memdef1", 4096, Test2Expected);
93-
checkSharedMemoryDefinition("/2memdef2", 4096, Test3Expected);
112+
checkSharedMemoryDefinition(getSharedMemoryName(2, 0), 4096, Test1Expected);
113+
checkSharedMemoryDefinition(getSharedMemoryName(2, 1), 4096, Test2Expected);
114+
checkSharedMemoryDefinition(getSharedMemoryName(2, 2), 4096, Test3Expected);
94115
}
95116

96117
// The following test is only supported on little endian systems.
@@ -123,7 +144,7 @@ TEST_F(SubprocessMemoryTest, DefinitionEndTruncation) {
123144
Test1Expected[I] = 0xaa;
124145
}
125146
}
126-
checkSharedMemoryDefinition("/3memdef0", 4096, Test1Expected);
147+
checkSharedMemoryDefinition(getSharedMemoryName(3, 0), 4096, Test1Expected);
127148
}
128149

129150
#endif // defined(__linux__) && !defined(__ANDROID__)

0 commit comments

Comments
 (0)