1 //===-- SubprocessMemoryTest.cpp --------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "SubprocessMemory.h"
11 #include "X86/TestBase.h"
12 #include "gtest/gtest.h"
14 #include <unordered_map>
20 #include <sys/syscall.h>
27 #if defined(__linux__) && !defined(__ANDROID__) && \
28 !(defined(__powerpc__) || defined(__s390x__) || defined(__sparc__))
30 // This needs to be updated anytime a test is added or removed from the test
32 static constexpr const size_t TestCount
= 4;
34 class SubprocessMemoryTest
: public X86TestBase
{
36 int getSharedMemoryNumber(const unsigned TestNumber
) {
37 // Do a process similar to 2D array indexing so that each process gets it's
38 // own shared memory space to avoid collisions. This will not overflow as
39 // the maximum value a PID can take on is 10^22.
40 return getpid() * TestCount
+ TestNumber
;
44 testCommon(std::unordered_map
<std::string
, MemoryValue
> MemoryDefinitions
,
45 const unsigned TestNumber
) {
47 SM
.initializeSubprocessMemory(getSharedMemoryNumber(TestNumber
)));
48 EXPECT_FALSE(SM
.addMemoryDefinition(MemoryDefinitions
,
49 getSharedMemoryNumber(TestNumber
)));
52 std::string
getSharedMemoryName(const unsigned TestNumber
,
53 const unsigned DefinitionNumber
) {
54 long CurrentTID
= syscall(SYS_gettid
);
55 return "/" + std::to_string(getSharedMemoryNumber(TestNumber
)) + "t" +
56 std::to_string(CurrentTID
) + "memdef" +
57 std::to_string(DefinitionNumber
);
60 void checkSharedMemoryDefinition(const std::string
&DefinitionName
,
61 size_t DefinitionSize
,
62 std::vector
<uint8_t> ExpectedValue
) {
64 shm_open(DefinitionName
.c_str(), O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
);
65 uint8_t *SharedMemoryMapping
= (uint8_t *)mmap(
66 NULL
, DefinitionSize
, PROT_READ
, MAP_SHARED
, SharedMemoryFD
, 0);
67 EXPECT_NE(reinterpret_cast<intptr_t>(SharedMemoryMapping
), -1);
68 for (size_t I
= 0; I
< ExpectedValue
.size(); ++I
) {
69 EXPECT_EQ(SharedMemoryMapping
[I
], ExpectedValue
[I
]);
71 munmap(SharedMemoryMapping
, DefinitionSize
);
77 // Some of the tests below are failing on s390x and PPC due to the shared
78 // memory calls not working in some cases, so they have been disabled.
79 // TODO(boomanaiden154): Investigate and fix this issue on PPC.
81 TEST_F(SubprocessMemoryTest
, OneDefinition
) {
82 testCommon({{"test1", {APInt(8, 0xff), 4096, 0}}}, 0);
83 checkSharedMemoryDefinition(getSharedMemoryName(0, 0), 4096, {0xff});
86 TEST_F(SubprocessMemoryTest
, MultipleDefinitions
) {
87 testCommon({{"test1", {APInt(8, 0xaa), 4096, 0}},
88 {"test2", {APInt(8, 0xbb), 4096, 1}},
89 {"test3", {APInt(8, 0xcc), 4096, 2}}},
91 checkSharedMemoryDefinition(getSharedMemoryName(1, 0), 4096, {0xaa});
92 checkSharedMemoryDefinition(getSharedMemoryName(1, 1), 4096, {0xbb});
93 checkSharedMemoryDefinition(getSharedMemoryName(1, 2), 4096, {0xcc});
96 TEST_F(SubprocessMemoryTest
, DefinitionFillsCompletely
) {
97 testCommon({{"test1", {APInt(8, 0xaa), 4096, 0}},
98 {"test2", {APInt(16, 0xbbbb), 4096, 1}},
99 {"test3", {APInt(24, 0xcccccc), 4096, 2}}},
101 std::vector
<uint8_t> Test1Expected(512, 0xaa);
102 std::vector
<uint8_t> Test2Expected(512, 0xbb);
103 std::vector
<uint8_t> Test3Expected(512, 0xcc);
104 checkSharedMemoryDefinition(getSharedMemoryName(2, 0), 4096, Test1Expected
);
105 checkSharedMemoryDefinition(getSharedMemoryName(2, 1), 4096, Test2Expected
);
106 checkSharedMemoryDefinition(getSharedMemoryName(2, 2), 4096, Test3Expected
);
109 // The following test is only supported on little endian systems.
110 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
111 TEST_F(SubprocessMemoryTest
, DISABLED_DefinitionEndTruncation
) {
113 TEST_F(SubprocessMemoryTest
, DefinitionEndTruncation
) {
115 testCommon({{"test1", {APInt(48, 0xaabbccddeeff), 4096, 0}}}, 3);
116 std::vector
<uint8_t> Test1Expected(512, 0);
117 // order is reversed since we're assuming a little endian system.
118 for (size_t I
= 0; I
< Test1Expected
.size(); ++I
) {
121 Test1Expected
[I
] = 0xff;
124 Test1Expected
[I
] = 0xee;
127 Test1Expected
[I
] = 0xdd;
130 Test1Expected
[I
] = 0xcc;
133 Test1Expected
[I
] = 0xbb;
136 Test1Expected
[I
] = 0xaa;
139 checkSharedMemoryDefinition(getSharedMemoryName(3, 0), 4096, Test1Expected
);
142 #endif // __linux__ && !__ANDROID__ && !(__powerpc__ || __s390x__ || __sparc__)
144 } // namespace exegesis