10
10
11
11
#include " X86/TestBase.h"
12
12
#include " gtest/gtest.h"
13
+ #include < string>
13
14
#include < unordered_map>
14
15
15
16
#ifdef __linux__
16
17
#include < endian.h>
17
18
#include < fcntl.h>
18
19
#include < sys/mman.h>
20
+ #include < unistd.h>
19
21
#endif // __linux__
20
22
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
+
21
27
namespace llvm {
22
28
namespace exegesis {
23
29
24
30
#if defined(__linux__) && !defined(__ANDROID__)
25
31
26
32
class SubprocessMemoryTest : public X86TestBase {
27
33
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
+
28
41
void
29
42
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);
33
54
}
34
55
35
56
void checkSharedMemoryDefinition (const std::string &DefinitionName,
@@ -59,7 +80,7 @@ TEST_F(SubprocessMemoryTest, DISABLED_OneDefinition) {
59
80
TEST_F (SubprocessMemoryTest, OneDefinition) {
60
81
#endif
61
82
testCommon ({{" test1" , {APInt (8 , 0xff ), 4096 , 0 }}}, 0 );
62
- checkSharedMemoryDefinition (" /0memdef0 " , 4096 , {0xff });
83
+ checkSharedMemoryDefinition (getSharedMemoryName ( 0 , 0 ) , 4096 , {0xff });
63
84
}
64
85
65
86
#if defined(__powerpc__) || defined(__s390x__)
@@ -71,9 +92,9 @@ TEST_F(SubprocessMemoryTest, MultipleDefinitions) {
71
92
{" test2" , {APInt (8 , 0xbb ), 4096 , 1 }},
72
93
{" test3" , {APInt (8 , 0xcc ), 4096 , 2 }}},
73
94
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 });
77
98
}
78
99
79
100
#if defined(__powerpc__) || defined(__s390x__)
@@ -88,9 +109,9 @@ TEST_F(SubprocessMemoryTest, DefinitionFillsCompletely) {
88
109
std::vector<uint8_t > Test1Expected (512 , 0xaa );
89
110
std::vector<uint8_t > Test2Expected (512 , 0xbb );
90
111
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);
94
115
}
95
116
96
117
// The following test is only supported on little endian systems.
@@ -123,7 +144,7 @@ TEST_F(SubprocessMemoryTest, DefinitionEndTruncation) {
123
144
Test1Expected[I] = 0xaa ;
124
145
}
125
146
}
126
- checkSharedMemoryDefinition (" /3memdef0 " , 4096 , Test1Expected);
147
+ checkSharedMemoryDefinition (getSharedMemoryName ( 3 , 0 ) , 4096 , Test1Expected);
127
148
}
128
149
129
150
#endif // defined(__linux__) && !defined(__ANDROID__)
0 commit comments