llvm-project

Форк
0
/
SubprocessMemoryTest.cpp 
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

24
namespace llvm {
25
namespace 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.
31
static constexpr const size_t TestCount = 4;
32

33
class SubprocessMemoryTest : public X86TestBase {
34
protected:
35
  int getSharedMemoryNumber(const unsigned TestNumber) {
36
    // Do a process similar to 2D array indexing so that each process gets it's
37
    // own shared memory space to avoid collisions. This will not overflow as
38
    // the maximum value a PID can take on is 10^22.
39
    return getpid() * TestCount + TestNumber;
40
  }
41

42
  void
43
  testCommon(std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
44
             const unsigned TestNumber) {
45
    EXPECT_FALSE(
46
        SM.initializeSubprocessMemory(getSharedMemoryNumber(TestNumber)));
47
    EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions,
48
                                        getSharedMemoryNumber(TestNumber)));
49
  }
50

51
  std::string getSharedMemoryName(const unsigned TestNumber,
52
                                  const unsigned DefinitionNumber) {
53
    long CurrentTID = syscall(SYS_gettid);
54
    return "/" + std::to_string(getSharedMemoryNumber(TestNumber)) + "t" +
55
           std::to_string(CurrentTID) + "memdef" +
56
           std::to_string(DefinitionNumber);
57
  }
58

59
  void checkSharedMemoryDefinition(const std::string &DefinitionName,
60
                                   size_t DefinitionSize,
61
                                   std::vector<uint8_t> ExpectedValue) {
62
    int SharedMemoryFD =
63
        shm_open(DefinitionName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
64
    uint8_t *SharedMemoryMapping = (uint8_t *)mmap(
65
        NULL, DefinitionSize, PROT_READ, MAP_SHARED, SharedMemoryFD, 0);
66
    EXPECT_NE((intptr_t)SharedMemoryMapping, -1);
67
    for (size_t I = 0; I < ExpectedValue.size(); ++I) {
68
      EXPECT_EQ(SharedMemoryMapping[I], ExpectedValue[I]);
69
    }
70
    munmap(SharedMemoryMapping, DefinitionSize);
71
  }
72

73
  SubprocessMemory 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__)
81
TEST_F(SubprocessMemoryTest, DISABLED_OneDefinition) {
82
#else
83
TEST_F(SubprocessMemoryTest, OneDefinition) {
84
#endif
85
  testCommon({{"test1", {APInt(8, 0xff), 4096, 0}}}, 0);
86
  checkSharedMemoryDefinition(getSharedMemoryName(0, 0), 4096, {0xff});
87
}
88

89
#if defined(__powerpc__) || defined(__s390x__)
90
TEST_F(SubprocessMemoryTest, DISABLED_MultipleDefinitions) {
91
#else
92
TEST_F(SubprocessMemoryTest, MultipleDefinitions) {
93
#endif
94
  testCommon({{"test1", {APInt(8, 0xaa), 4096, 0}},
95
              {"test2", {APInt(8, 0xbb), 4096, 1}},
96
              {"test3", {APInt(8, 0xcc), 4096, 2}}},
97
             1);
98
  checkSharedMemoryDefinition(getSharedMemoryName(1, 0), 4096, {0xaa});
99
  checkSharedMemoryDefinition(getSharedMemoryName(1, 1), 4096, {0xbb});
100
  checkSharedMemoryDefinition(getSharedMemoryName(1, 2), 4096, {0xcc});
101
}
102

103
#if defined(__powerpc__) || defined(__s390x__)
104
TEST_F(SubprocessMemoryTest, DISABLED_DefinitionFillsCompletely) {
105
#else
106
TEST_F(SubprocessMemoryTest, DefinitionFillsCompletely) {
107
#endif
108
  testCommon({{"test1", {APInt(8, 0xaa), 4096, 0}},
109
              {"test2", {APInt(16, 0xbbbb), 4096, 1}},
110
              {"test3", {APInt(24, 0xcccccc), 4096, 2}}},
111
             2);
112
  std::vector<uint8_t> Test1Expected(512, 0xaa);
113
  std::vector<uint8_t> Test2Expected(512, 0xbb);
114
  std::vector<uint8_t> Test3Expected(512, 0xcc);
115
  checkSharedMemoryDefinition(getSharedMemoryName(2, 0), 4096, Test1Expected);
116
  checkSharedMemoryDefinition(getSharedMemoryName(2, 1), 4096, Test2Expected);
117
  checkSharedMemoryDefinition(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__
122
TEST_F(SubprocessMemoryTest, DISABLED_DefinitionEndTruncation) {
123
#else
124
TEST_F(SubprocessMemoryTest, DefinitionEndTruncation) {
125
#endif
126
  testCommon({{"test1", {APInt(48, 0xaabbccddeeff), 4096, 0}}}, 3);
127
  std::vector<uint8_t> Test1Expected(512, 0);
128
  // order is reversed since we're assuming a little endian system.
129
  for (size_t I = 0; I < Test1Expected.size(); ++I) {
130
    switch (I % 6) {
131
    case 0:
132
      Test1Expected[I] = 0xff;
133
      break;
134
    case 1:
135
      Test1Expected[I] = 0xee;
136
      break;
137
    case 2:
138
      Test1Expected[I] = 0xdd;
139
      break;
140
    case 3:
141
      Test1Expected[I] = 0xcc;
142
      break;
143
    case 4:
144
      Test1Expected[I] = 0xbb;
145
      break;
146
    case 5:
147
      Test1Expected[I] = 0xaa;
148
    }
149
  }
150
  checkSharedMemoryDefinition(getSharedMemoryName(3, 0), 4096, Test1Expected);
151
}
152

153
#endif // defined(__linux__) && !defined(__ANDROID__)
154

155
} // namespace exegesis
156
} // namespace llvm
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.