File: wrappers_cpp_test.cpp

package info (click to toggle)
llvm-toolchain-18 1%3A18.1.8-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,908,340 kB
  • sloc: cpp: 6,667,937; ansic: 1,440,452; asm: 883,619; python: 230,549; objc: 76,880; f90: 74,238; lisp: 35,989; pascal: 16,571; sh: 10,229; perl: 7,459; ml: 5,047; awk: 3,523; makefile: 2,987; javascript: 2,149; xml: 892; fortran: 649; cs: 573
file content (273 lines) | stat: -rw-r--r-- 7,395 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//===-- wrappers_cpp_test.cpp -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "memtag.h"
#include "tests/scudo_unit_test.h"

#include <atomic>
#include <condition_variable>
#include <fstream>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

// Android does not support checking for new/delete mismatches.
#if SCUDO_ANDROID
#define SKIP_MISMATCH_TESTS 1
#else
#define SKIP_MISMATCH_TESTS 0
#endif

void operator delete(void *, size_t) noexcept;
void operator delete[](void *, size_t) noexcept;

extern "C" {
#ifndef SCUDO_ENABLE_HOOKS_TESTS
#define SCUDO_ENABLE_HOOKS_TESTS 0
#endif

#if (SCUDO_ENABLE_HOOKS_TESTS == 1) && (SCUDO_ENABLE_HOOKS == 0)
#error "Hooks tests should have hooks enabled as well!"
#endif

struct AllocContext {
  void *Ptr;
  size_t Size;
};
struct DeallocContext {
  void *Ptr;
};
static AllocContext AC;
static DeallocContext DC;

#if (SCUDO_ENABLE_HOOKS_TESTS == 1)
__attribute__((visibility("default"))) void __scudo_allocate_hook(void *Ptr,
                                                                  size_t Size) {
  AC.Ptr = Ptr;
  AC.Size = Size;
}
__attribute__((visibility("default"))) void __scudo_deallocate_hook(void *Ptr) {
  DC.Ptr = Ptr;
}
#endif // (SCUDO_ENABLE_HOOKS_TESTS == 1)
}

class ScudoWrappersCppTest : public Test {
protected:
  void SetUp() override {
    if (SCUDO_ENABLE_HOOKS && !SCUDO_ENABLE_HOOKS_TESTS)
      printf("Hooks are enabled but hooks tests are disabled.\n");
  }

  void verifyAllocHookPtr(UNUSED void *Ptr) {
    if (SCUDO_ENABLE_HOOKS_TESTS)
      EXPECT_EQ(Ptr, AC.Ptr);
  }
  void verifyAllocHookSize(UNUSED size_t Size) {
    if (SCUDO_ENABLE_HOOKS_TESTS)
      EXPECT_EQ(Size, AC.Size);
  }
  void verifyDeallocHookPtr(UNUSED void *Ptr) {
    if (SCUDO_ENABLE_HOOKS_TESTS)
      EXPECT_EQ(Ptr, DC.Ptr);
  }

  template <typename T> void testCxxNew() {
    T *P = new T;
    EXPECT_NE(P, nullptr);
    verifyAllocHookPtr(P);
    verifyAllocHookSize(sizeof(T));
    memset(P, 0x42, sizeof(T));
    EXPECT_DEATH(delete[] P, "");
    delete P;
    verifyDeallocHookPtr(P);
    EXPECT_DEATH(delete P, "");

    P = new T;
    EXPECT_NE(P, nullptr);
    memset(P, 0x42, sizeof(T));
    operator delete(P, sizeof(T));
    verifyDeallocHookPtr(P);

    P = new (std::nothrow) T;
    verifyAllocHookPtr(P);
    verifyAllocHookSize(sizeof(T));
    EXPECT_NE(P, nullptr);
    memset(P, 0x42, sizeof(T));
    delete P;
    verifyDeallocHookPtr(P);

    const size_t N = 16U;
    T *A = new T[N];
    EXPECT_NE(A, nullptr);
    verifyAllocHookPtr(A);
    verifyAllocHookSize(sizeof(T) * N);
    memset(A, 0x42, sizeof(T) * N);
    EXPECT_DEATH(delete A, "");
    delete[] A;
    verifyDeallocHookPtr(A);
    EXPECT_DEATH(delete[] A, "");

    A = new T[N];
    EXPECT_NE(A, nullptr);
    memset(A, 0x42, sizeof(T) * N);
    operator delete[](A, sizeof(T) * N);
    verifyDeallocHookPtr(A);

    A = new (std::nothrow) T[N];
    verifyAllocHookPtr(A);
    verifyAllocHookSize(sizeof(T) * N);
    EXPECT_NE(A, nullptr);
    memset(A, 0x42, sizeof(T) * N);
    delete[] A;
    verifyDeallocHookPtr(A);
  }
};
using ScudoWrappersCppDeathTest = ScudoWrappersCppTest;

class Pixel {
public:
  enum class Color { Red, Green, Blue };
  int X = 0;
  int Y = 0;
  Color C = Color::Red;
};

// Note that every Cxx allocation function in the test binary will be fulfilled
// by Scudo. See the comment in the C counterpart of this file.

TEST_F(ScudoWrappersCppDeathTest, New) {
  if (getenv("SKIP_TYPE_MISMATCH") || SKIP_MISMATCH_TESTS) {
    printf("Skipped type mismatch tests.\n");
    return;
  }
  testCxxNew<bool>();
  testCxxNew<uint8_t>();
  testCxxNew<uint16_t>();
  testCxxNew<uint32_t>();
  testCxxNew<uint64_t>();
  testCxxNew<float>();
  testCxxNew<double>();
  testCxxNew<long double>();
  testCxxNew<Pixel>();
}

static std::mutex Mutex;
static std::condition_variable Cv;
static bool Ready;

static void stressNew() {
  std::vector<uintptr_t *> V;
  {
    std::unique_lock<std::mutex> Lock(Mutex);
    while (!Ready)
      Cv.wait(Lock);
  }
  for (size_t I = 0; I < 256U; I++) {
    const size_t N = static_cast<size_t>(std::rand()) % 128U;
    uintptr_t *P = new uintptr_t[N];
    if (P) {
      memset(P, 0x42, sizeof(uintptr_t) * N);
      V.push_back(P);
    }
  }
  while (!V.empty()) {
    delete[] V.back();
    V.pop_back();
  }
}

TEST_F(ScudoWrappersCppTest, ThreadedNew) {
  // TODO: Investigate why libc sometimes crashes with tag missmatch in
  // __pthread_clockjoin_ex.
  std::unique_ptr<scudo::ScopedDisableMemoryTagChecks> NoTags;
  if (!SCUDO_ANDROID && scudo::archSupportsMemoryTagging() &&
      scudo::systemSupportsMemoryTagging())
    NoTags = std::make_unique<scudo::ScopedDisableMemoryTagChecks>();

  Ready = false;
  std::thread Threads[32];
  for (size_t I = 0U; I < sizeof(Threads) / sizeof(Threads[0]); I++)
    Threads[I] = std::thread(stressNew);
  {
    std::unique_lock<std::mutex> Lock(Mutex);
    Ready = true;
    Cv.notify_all();
  }
  for (auto &T : Threads)
    T.join();
}

#if !SCUDO_FUCHSIA
TEST_F(ScudoWrappersCppTest, AllocAfterFork) {
  // This test can fail flakily when ran as a part of large number of
  // other tests if the maxmimum number of mappings allowed is low.
  // We tried to reduce the number of iterations of the loops with
  // moderate success, so we will now skip this test under those
  // circumstances.
  if (SCUDO_LINUX) {
    long MaxMapCount = 0;
    // If the file can't be accessed, we proceed with the test.
    std::ifstream Stream("/proc/sys/vm/max_map_count");
    if (Stream.good()) {
      Stream >> MaxMapCount;
      if (MaxMapCount < 200000)
        return;
    }
  }

  std::atomic_bool Stop;

  // Create threads that simply allocate and free different sizes.
  std::vector<std::thread *> Threads;
  for (size_t N = 0; N < 5; N++) {
    std::thread *T = new std::thread([&Stop] {
      while (!Stop) {
        for (size_t SizeLog = 3; SizeLog <= 20; SizeLog++) {
          char *P = new char[1UL << SizeLog];
          EXPECT_NE(P, nullptr);
          // Make sure this value is not optimized away.
          asm volatile("" : : "r,m"(P) : "memory");
          delete[] P;
        }
      }
    });
    Threads.push_back(T);
  }

  // Create a thread to fork and allocate.
  for (size_t N = 0; N < 50; N++) {
    pid_t Pid;
    if ((Pid = fork()) == 0) {
      for (size_t SizeLog = 3; SizeLog <= 20; SizeLog++) {
        char *P = new char[1UL << SizeLog];
        EXPECT_NE(P, nullptr);
        // Make sure this value is not optimized away.
        asm volatile("" : : "r,m"(P) : "memory");
        // Make sure we can touch all of the allocation.
        memset(P, 0x32, 1U << SizeLog);
        // EXPECT_LE(1U << SizeLog, malloc_usable_size(ptr));
        delete[] P;
      }
      _exit(10);
    }
    EXPECT_NE(-1, Pid);
    int Status;
    EXPECT_EQ(Pid, waitpid(Pid, &Status, 0));
    EXPECT_FALSE(WIFSIGNALED(Status));
    EXPECT_EQ(10, WEXITSTATUS(Status));
  }

  printf("Waiting for threads to complete\n");
  Stop = true;
  for (auto Thread : Threads)
    Thread->join();
  Threads.clear();
}
#endif