llvm-project
156 строк · 5.4 Кб
1//===-- SubprocessMemoryTest.cpp --------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "SubprocessMemory.h"10
11#include "X86/TestBase.h"12#include "gtest/gtest.h"13#include <string>14#include <unordered_map>15
16#ifdef __linux__17#include <endian.h>18#include <fcntl.h>19#include <sys/mman.h>20#include <sys/syscall.h>21#include <unistd.h>22#endif // __linux__23
24namespace llvm {25namespace exegesis {26
27#if defined(__linux__) && !defined(__ANDROID__)28
29// This needs to be updated anytime a test is added or removed from the test
30// suite.
31static constexpr const size_t TestCount = 4;32
33class SubprocessMemoryTest : public X86TestBase {34protected:35int getSharedMemoryNumber(const unsigned TestNumber) {36// Do a process similar to 2D array indexing so that each process gets it's37// own shared memory space to avoid collisions. This will not overflow as38// the maximum value a PID can take on is 10^22.39return getpid() * TestCount + TestNumber;40}41
42void43testCommon(std::unordered_map<std::string, MemoryValue> MemoryDefinitions,44const unsigned TestNumber) {45EXPECT_FALSE(46SM.initializeSubprocessMemory(getSharedMemoryNumber(TestNumber)));47EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions,48getSharedMemoryNumber(TestNumber)));49}50
51std::string getSharedMemoryName(const unsigned TestNumber,52const unsigned DefinitionNumber) {53long CurrentTID = syscall(SYS_gettid);54return "/" + std::to_string(getSharedMemoryNumber(TestNumber)) + "t" +55std::to_string(CurrentTID) + "memdef" +56std::to_string(DefinitionNumber);57}58
59void checkSharedMemoryDefinition(const std::string &DefinitionName,60size_t DefinitionSize,61std::vector<uint8_t> ExpectedValue) {62int SharedMemoryFD =63shm_open(DefinitionName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);64uint8_t *SharedMemoryMapping = (uint8_t *)mmap(65NULL, DefinitionSize, PROT_READ, MAP_SHARED, SharedMemoryFD, 0);66EXPECT_NE((intptr_t)SharedMemoryMapping, -1);67for (size_t I = 0; I < ExpectedValue.size(); ++I) {68EXPECT_EQ(SharedMemoryMapping[I], ExpectedValue[I]);69}70munmap(SharedMemoryMapping, DefinitionSize);71}72
73SubprocessMemory SM;74};75
76// Some of the tests below are failing on s390x and PPC due to the shared
77// memory calls not working in some cases, so they have been disabled.
78// TODO(boomanaiden154): Investigate and fix this issue on PPC.
79
80#if defined(__powerpc__) || defined(__s390x__)81TEST_F(SubprocessMemoryTest, DISABLED_OneDefinition) {82#else83TEST_F(SubprocessMemoryTest, OneDefinition) {84#endif85testCommon({{"test1", {APInt(8, 0xff), 4096, 0}}}, 0);86checkSharedMemoryDefinition(getSharedMemoryName(0, 0), 4096, {0xff});87}
88
89#if defined(__powerpc__) || defined(__s390x__)90TEST_F(SubprocessMemoryTest, DISABLED_MultipleDefinitions) {91#else92TEST_F(SubprocessMemoryTest, MultipleDefinitions) {93#endif94testCommon({{"test1", {APInt(8, 0xaa), 4096, 0}},95{"test2", {APInt(8, 0xbb), 4096, 1}},96{"test3", {APInt(8, 0xcc), 4096, 2}}},971);98checkSharedMemoryDefinition(getSharedMemoryName(1, 0), 4096, {0xaa});99checkSharedMemoryDefinition(getSharedMemoryName(1, 1), 4096, {0xbb});100checkSharedMemoryDefinition(getSharedMemoryName(1, 2), 4096, {0xcc});101}
102
103#if defined(__powerpc__) || defined(__s390x__)104TEST_F(SubprocessMemoryTest, DISABLED_DefinitionFillsCompletely) {105#else106TEST_F(SubprocessMemoryTest, DefinitionFillsCompletely) {107#endif108testCommon({{"test1", {APInt(8, 0xaa), 4096, 0}},109{"test2", {APInt(16, 0xbbbb), 4096, 1}},110{"test3", {APInt(24, 0xcccccc), 4096, 2}}},1112);112std::vector<uint8_t> Test1Expected(512, 0xaa);113std::vector<uint8_t> Test2Expected(512, 0xbb);114std::vector<uint8_t> Test3Expected(512, 0xcc);115checkSharedMemoryDefinition(getSharedMemoryName(2, 0), 4096, Test1Expected);116checkSharedMemoryDefinition(getSharedMemoryName(2, 1), 4096, Test2Expected);117checkSharedMemoryDefinition(getSharedMemoryName(2, 2), 4096, Test3Expected);118}
119
120// The following test is only supported on little endian systems.
121#if defined(__powerpc__) || defined(__s390x__) || __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__122TEST_F(SubprocessMemoryTest, DISABLED_DefinitionEndTruncation) {123#else124TEST_F(SubprocessMemoryTest, DefinitionEndTruncation) {125#endif126testCommon({{"test1", {APInt(48, 0xaabbccddeeff), 4096, 0}}}, 3);127std::vector<uint8_t> Test1Expected(512, 0);128// order is reversed since we're assuming a little endian system.129for (size_t I = 0; I < Test1Expected.size(); ++I) {130switch (I % 6) {131case 0:132Test1Expected[I] = 0xff;133break;134case 1:135Test1Expected[I] = 0xee;136break;137case 2:138Test1Expected[I] = 0xdd;139break;140case 3:141Test1Expected[I] = 0xcc;142break;143case 4:144Test1Expected[I] = 0xbb;145break;146case 5:147Test1Expected[I] = 0xaa;148}149}150checkSharedMemoryDefinition(getSharedMemoryName(3, 0), 4096, Test1Expected);151}
152
153#endif // defined(__linux__) && !defined(__ANDROID__)154
155} // namespace exegesis156} // namespace llvm157