summaryrefslogtreecommitdiffstats
path: root/third_party/mojo/src/mojo/edk/system/memory_unittest.cc
blob: 90133e194260226079649f9c1f7809fa83911ce5 (plain)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/mojo/src/mojo/edk/system/memory.h"

#include <stddef.h>
#include <stdint.h>

#include <limits>

#include "mojo/public/c/system/macros.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace system {
namespace {

TEST(MemoryTest, Valid) {
  char my_char;
  int32_t my_int32;
  int64_t my_int64_array[5] = {};  // Zero initialize.

  UserPointer<char> my_char_ptr(&my_char);
  UserPointer<int32_t> my_int32_ptr(&my_int32);
  UserPointer<int64_t> my_int64_array_ptr(my_int64_array);

  // |UserPointer<>::IsNull()|:
  EXPECT_FALSE(my_char_ptr.IsNull());
  EXPECT_FALSE(my_int32_ptr.IsNull());
  EXPECT_FALSE(my_int64_array_ptr.IsNull());

  // |UserPointer<>::Put()| and |UserPointer<>::Get()|:
  my_char_ptr.Put('x');
  EXPECT_EQ('x', my_char);
  EXPECT_EQ('x', my_char_ptr.Get());
  my_int32_ptr.Put(123);
  EXPECT_EQ(123, my_int32);
  EXPECT_EQ(123, my_int32_ptr.Get());
  my_int64_array_ptr.Put(456);
  EXPECT_EQ(456, my_int64_array[0]);
  EXPECT_EQ(456, my_int64_array_ptr.Get());

  // |UserPointer<>::At()|, etc.:
  my_int64_array_ptr.At(3).Put(789);
  EXPECT_EQ(789, my_int64_array[3]);
  {
    // Copy construction:
    UserPointer<int64_t> other(my_int64_array_ptr.At(3));
    EXPECT_FALSE(other.IsNull());
    EXPECT_EQ(789, other.Get());

    // Assignment:
    other = my_int64_array_ptr;
    EXPECT_FALSE(other.IsNull());
    EXPECT_EQ(456, other.Get());

    // Assignment to |NullUserPointer()|:
    other = NullUserPointer();
    EXPECT_TRUE(other.IsNull());

    // |MakeUserPointer()|:
    other = MakeUserPointer(&my_int64_array[1]);
    other.Put(-123);
    EXPECT_EQ(-123, my_int64_array_ptr.At(1).Get());
  }

  // "const" |UserPointer<>|:
  {
    // Explicit constructor from |NullUserPointer()|:
    UserPointer<const char> other((NullUserPointer()));
    EXPECT_TRUE(other.IsNull());

    // Conversion to "const":
    other = my_char_ptr;
    EXPECT_EQ('x', other.Get());
  }

  // Default constructor:
  {
    UserPointer<int32_t> other;
    EXPECT_TRUE(other.IsNull());

    other = my_int32_ptr;
    other.Put(-456);
    EXPECT_EQ(-456, my_int32_ptr.Get());
  }

  // |UserPointer<>::CheckArray()|:
  my_int64_array_ptr.CheckArray(5);

  // |UserPointer<>::GetArray()|:
  {
    // From a "const" |UserPointer<>| (why not?):
    UserPointer<const int64_t> other(my_int64_array_ptr);
    int64_t array[3] = {1, 2, 3};
    other.At(1).GetArray(array, 3);
    EXPECT_EQ(-123, array[0]);
    EXPECT_EQ(0, array[1]);
    EXPECT_EQ(789, array[2]);
  }

  // |UserPointer<>::PutArray()|:
  {
    const int64_t array[2] = {654, 321};
    my_int64_array_ptr.At(3).PutArray(array, 2);
    EXPECT_EQ(0, my_int64_array[2]);
    EXPECT_EQ(654, my_int64_array[3]);
    EXPECT_EQ(321, my_int64_array[4]);
  }

  // |UserPointer<>::Reader|:
  {
    UserPointer<int64_t>::Reader reader(my_int64_array_ptr, 5);
    EXPECT_EQ(456, reader.GetPointer()[0]);
    EXPECT_EQ(321, reader.GetPointer()[4]);
  }

  // Non-const to const:
  {
    UserPointer<const int64_t>::Reader reader(my_int64_array_ptr.At(3), 1);
    const int64_t* ptr = reader.GetPointer();
    EXPECT_EQ(654, *ptr);
  }

  // |UserPointer<>::Writer|:
  {
    UserPointer<int64_t>::Writer writer(my_int64_array_ptr.At(2), 1);
    int64_t* ptr = writer.GetPointer();
    *ptr = 1234567890123LL;
    writer.Commit();
    EXPECT_EQ(1234567890123LL, my_int64_array[2]);
  }

  // |UserPointer<>::ReaderWriter|:
  {
    UserPointer<int32_t>::ReaderWriter reader_writer(my_int32_ptr, 1);
    int32_t* ptr = reader_writer.GetPointer();
    EXPECT_EQ(-456, *ptr);
    *ptr = 42;
    reader_writer.Commit();
    EXPECT_EQ(42, my_int32);
  }

  // |UserPointer<>::ReinterpretCast<>|:
  // (This assumes little-endian, etc.)
  {
    UserPointer<const char> other(my_int32_ptr.ReinterpretCast<char>());
    EXPECT_EQ(42, other.Get());
    EXPECT_EQ(0, other.At(1).Get());
    EXPECT_EQ(0, other.At(2).Get());
    EXPECT_EQ(0, other.At(3).Get());
  }

  // |UserPointer<>::GetPointerValue()|:
  {
    UserPointer<int32_t> other;
    EXPECT_EQ(0u, other.GetPointerValue());
    other = my_int32_ptr;
    EXPECT_EQ(reinterpret_cast<uintptr_t>(&my_int32), other.GetPointerValue());
  }
}

TEST(MemoryTest, InvalidDeath) {
  const char kMemoryCheckFailedRegex[] = "Check failed";

  // Note: |Check...()| are defined to be "best effort" checks (and may always
  // return true). Thus these tests of invalid cases only reflect the current
  // implementation.

  // These tests depend on |int32_t| and |int64_t| having nontrivial alignment.
  static_assert(MOJO_ALIGNOF(int32_t) != 1,
                "int32_t does not require nontrivial alignment");
  static_assert(MOJO_ALIGNOF(int64_t) != 1,
                "int64_t does not require nontrivial alignment");

  // Null:
  {
    UserPointer<char> ptr(nullptr);
    char array[5] = {};
    EXPECT_DEATH_IF_SUPPORTED(ptr.Check(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Get(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Put('x'), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(array, 5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.PutArray(array, 5), kMemoryCheckFailedRegex);
  }
  {
    UserPointer<int32_t> ptr(nullptr);
    int32_t array[5] = {};
    EXPECT_DEATH_IF_SUPPORTED(ptr.Check(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Get(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Put(123), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(array, 5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.PutArray(array, 5), kMemoryCheckFailedRegex);
  }
  {
    UserPointer<int64_t> ptr(nullptr);
    int64_t array[5] = {};
    EXPECT_DEATH_IF_SUPPORTED(ptr.Check(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Get(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Put(123), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(array, 5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.PutArray(array, 5), kMemoryCheckFailedRegex);
  }
  // Also check a const pointer:
  {
    UserPointer<const int32_t> ptr(nullptr);
    int32_t array[5] = {};
    EXPECT_DEATH_IF_SUPPORTED(ptr.Check(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Get(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(array, 5), kMemoryCheckFailedRegex);
  }

  // Unaligned:
  {
    int32_t x[10];
    UserPointer<int32_t> ptr(
        reinterpret_cast<int32_t*>(reinterpret_cast<uintptr_t>(x) + 1));
    int32_t array[5] = {};
    EXPECT_DEATH_IF_SUPPORTED(ptr.Check(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Get(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Put(123), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(array, 5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.PutArray(array, 5), kMemoryCheckFailedRegex);
  }
  {
    int64_t x[10];
    UserPointer<int64_t> ptr(
        reinterpret_cast<int64_t*>(reinterpret_cast<uintptr_t>(x) + 1));
    int64_t array[5] = {};
    EXPECT_DEATH_IF_SUPPORTED(ptr.Check(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Get(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Put(123), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(array, 5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.PutArray(array, 5), kMemoryCheckFailedRegex);
  }
  // Also check a const pointer:
  {
    int32_t x[10];
    UserPointer<const int32_t> ptr(
        reinterpret_cast<const int32_t*>(reinterpret_cast<uintptr_t>(x) + 1));
    int32_t array[5] = {};
    EXPECT_DEATH_IF_SUPPORTED(ptr.Check(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.Get(), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(5), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(array, 5), kMemoryCheckFailedRegex);
  }

  // Count too big:
  {
    const size_t kTooBig =
        std::numeric_limits<size_t>::max() / sizeof(int32_t) + 1;
    int32_t x = 0;
    UserPointer<int32_t> ptr(&x);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(kTooBig), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(&x, kTooBig),
                              kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.PutArray(&x, kTooBig),
                              kMemoryCheckFailedRegex);
  }
  {
    const size_t kTooBig =
        std::numeric_limits<size_t>::max() / sizeof(int64_t) + 1;
    int64_t x = 0;
    UserPointer<int64_t> ptr(&x);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(kTooBig), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(&x, kTooBig),
                              kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.PutArray(&x, kTooBig),
                              kMemoryCheckFailedRegex);
  }
  // Also check a const pointer:
  {
    const size_t kTooBig =
        std::numeric_limits<size_t>::max() / sizeof(int32_t) + 1;
    int32_t x = 0;
    UserPointer<const int32_t> ptr(&x);
    EXPECT_DEATH_IF_SUPPORTED(ptr.CheckArray(kTooBig), kMemoryCheckFailedRegex);
    EXPECT_DEATH_IF_SUPPORTED(ptr.GetArray(&x, kTooBig),
                              kMemoryCheckFailedRegex);
  }

  // TODO(vtl): Tests for |UserPointer{Reader,Writer,ReaderWriter}|.
}

}  // namespace
}  // namespace system
}  // namespace mojo