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+
2127namespace llvm {
2228namespace exegesis {
2329
2430#if defined(__linux__) && !defined(__ANDROID__)
2531
2632class SubprocessMemoryTest : public X86TestBase {
2733protected:
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) {
5980TEST_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