summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testAtomicOperations.cpp
blob: 06000f7676742dd6bd06513219bb5b4a48314a8a (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
295
296
297
298
299
300
301
302
303
304
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Alignment.h"
#include "mozilla/Assertions.h"

#include "jit/AtomicOperations.h"
#include "jsapi-tests/tests.h"
#include "vm/ArrayBufferObject.h"
#include "vm/SharedMem.h"
#include "vm/Uint8Clamped.h"
#include "wasm/WasmJS.h"

using namespace js;

// Machinery to disguise pointer addresses to the C++ compiler -- quite possibly
// not thread-safe.

extern void setHiddenPointer(void* p);
extern void* getHiddenPointer();

void* hidePointerValue(void* p) {
  setHiddenPointer(p);
  return getHiddenPointer();
}

//////////////////////////////////////////////////////////////////////
//
// Lock-freedom predicates

BEGIN_REUSABLE_TEST(testAtomicLockFree8) {
  // isLockfree8() must not return true if there are no 8-byte atomics

  CHECK(!jit::AtomicOperations::isLockfree8() ||
        jit::AtomicOperations::hasAtomic8());

  // We must have lock-free 8-byte atomics on every platform where we support
  // wasm, but we don't care otherwise.

  CHECK(!wasm::HasSupport(cx) || jit::AtomicOperations::isLockfree8());
  return true;
}
END_TEST(testAtomicLockFree8)

// The JS spec requires specific behavior for all but 1 and 2.

BEGIN_REUSABLE_TEST(testAtomicLockFreeJS) {
  static_assert(jit::AtomicOperations::isLockfreeJS(1) ==
                true);  // false is allowed by spec but not in SpiderMonkey
  static_assert(jit::AtomicOperations::isLockfreeJS(2) == true);   // ditto
  static_assert(jit::AtomicOperations::isLockfreeJS(8) == true);   // ditto
  static_assert(jit::AtomicOperations::isLockfreeJS(3) == false);  // required
  static_assert(jit::AtomicOperations::isLockfreeJS(4) == true);   // required
  static_assert(jit::AtomicOperations::isLockfreeJS(5) == false);  // required
  static_assert(jit::AtomicOperations::isLockfreeJS(6) == false);  // required
  static_assert(jit::AtomicOperations::isLockfreeJS(7) == false);  // required
  return true;
}
END_TEST(testAtomicLockFreeJS)

//////////////////////////////////////////////////////////////////////
//
// Fence

// This only tests that fenceSeqCst is defined and that it doesn't crash if we
// call it, but it has no return value and its effect is not observable here.

BEGIN_REUSABLE_TEST(testAtomicFence) {
  jit::AtomicOperations::fenceSeqCst();
  return true;
}
END_TEST(testAtomicFence)

//////////////////////////////////////////////////////////////////////
//
// Memory access primitives

// These tests for the atomic load and store primitives ascertain that the
// primitives are defined and that they load and store the values they should,
// but not that the primitives are actually atomic wrt to the memory subsystem.

// Memory for testing atomics.  This must be aligned to the natural alignment of
// the type we're testing; for now, use 8-byte alignment for all.

MOZ_ALIGNED_DECL(8, static uint8_t atomicMem[8]);
MOZ_ALIGNED_DECL(8, static uint8_t atomicMem2[8]);

// T is the primitive type we're testing, and A and B are references to constant
// bindings holding values of that type.
//
// No bytes of A and B should be 0 or FF.  A+B and A-B must not overflow.

#define ATOMIC_TESTS(T, A, B)                                           \
  T* q = (T*)hidePointerValue((void*)atomicMem);                        \
  *q = A;                                                               \
  SharedMem<T*> p =                                                     \
      SharedMem<T*>::shared((T*)hidePointerValue((T*)atomicMem));       \
  CHECK(*q == A);                                                       \
  CHECK(jit::AtomicOperations::loadSeqCst(p) == A);                     \
  CHECK(*q == A);                                                       \
  jit::AtomicOperations::storeSeqCst(p, B);                             \
  CHECK(*q == B);                                                       \
  CHECK(jit::AtomicOperations::exchangeSeqCst(p, A) == B);              \
  CHECK(*q == A);                                                       \
  CHECK(jit::AtomicOperations::compareExchangeSeqCst(p, (T)0, (T)1) ==  \
        A); /*failure*/                                                 \
  CHECK(*q == A);                                                       \
  CHECK(jit::AtomicOperations::compareExchangeSeqCst(p, A, B) ==        \
        A); /*success*/                                                 \
  CHECK(*q == B);                                                       \
  *q = A;                                                               \
  CHECK(jit::AtomicOperations::fetchAddSeqCst(p, B) == A);              \
  CHECK(*q == A + B);                                                   \
  *q = A;                                                               \
  CHECK(jit::AtomicOperations::fetchSubSeqCst(p, B) == A);              \
  CHECK(*q == A - B);                                                   \
  *q = A;                                                               \
  CHECK(jit::AtomicOperations::fetchAndSeqCst(p, B) == A);              \
  CHECK(*q == (A & B));                                                 \
  *q = A;                                                               \
  CHECK(jit::AtomicOperations::fetchOrSeqCst(p, B) == A);               \
  CHECK(*q == (A | B));                                                 \
  *q = A;                                                               \
  CHECK(jit::AtomicOperations::fetchXorSeqCst(p, B) == A);              \
  CHECK(*q == (A ^ B));                                                 \
  *q = A;                                                               \
  CHECK(jit::AtomicOperations::loadSafeWhenRacy(p) == A);               \
  jit::AtomicOperations::storeSafeWhenRacy(p, B);                       \
  CHECK(*q == B);                                                       \
  T* q2 = (T*)hidePointerValue((void*)atomicMem2);                      \
  SharedMem<T*> p2 =                                                    \
      SharedMem<T*>::shared((T*)hidePointerValue((void*)atomicMem2));   \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memcpySafeWhenRacy(p2, p, sizeof(T));          \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memcpySafeWhenRacy(p2, p.unwrap(), sizeof(T)); \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memcpySafeWhenRacy(p2.unwrap(), p, sizeof(T)); \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memmoveSafeWhenRacy(p2, p, sizeof(T));         \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::podCopySafeWhenRacy(p2, p, 1);                 \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::podMoveSafeWhenRacy(p2, p, 1);                 \
  CHECK(*q2 == A);                                                      \
  return true

BEGIN_REUSABLE_TEST(testAtomicOperationsU8) {
  const uint8_t A = 0xab;
  const uint8_t B = 0x37;
  ATOMIC_TESTS(uint8_t, A, B);
}
END_TEST(testAtomicOperationsU8)

BEGIN_REUSABLE_TEST(testAtomicOperationsI8) {
  const int8_t A = 0x3b;
  const int8_t B = 0x27;
  ATOMIC_TESTS(int8_t, A, B);
}
END_TEST(testAtomicOperationsI8)

BEGIN_REUSABLE_TEST(testAtomicOperationsU16) {
  const uint16_t A = 0xabdc;
  const uint16_t B = 0x3789;
  ATOMIC_TESTS(uint16_t, A, B);
}
END_TEST(testAtomicOperationsU16)

BEGIN_REUSABLE_TEST(testAtomicOperationsI16) {
  const int16_t A = 0x3bdc;
  const int16_t B = 0x2737;
  ATOMIC_TESTS(int16_t, A, B);
}
END_TEST(testAtomicOperationsI16)

BEGIN_REUSABLE_TEST(testAtomicOperationsU32) {
  const uint32_t A = 0xabdc0588;
  const uint32_t B = 0x37891942;
  ATOMIC_TESTS(uint32_t, A, B);
}
END_TEST(testAtomicOperationsU32)

BEGIN_REUSABLE_TEST(testAtomicOperationsI32) {
  const int32_t A = 0x3bdc0588;
  const int32_t B = 0x27371843;
  ATOMIC_TESTS(int32_t, A, B);
}
END_TEST(testAtomicOperationsI32)

BEGIN_REUSABLE_TEST(testAtomicOperationsU64) {
  if (!jit::AtomicOperations::hasAtomic8()) {
    return true;
  }

  const uint64_t A(0x9aadf00ddeadbeef);
  const uint64_t B(0x4eedbead1337f001);
  ATOMIC_TESTS(uint64_t, A, B);
}
END_TEST(testAtomicOperationsU64)

BEGIN_REUSABLE_TEST(testAtomicOperationsI64) {
  if (!jit::AtomicOperations::hasAtomic8()) {
    return true;
  }

  const int64_t A(0x2aadf00ddeadbeef);
  const int64_t B(0x4eedbead1337f001);
  ATOMIC_TESTS(int64_t, A, B);
}
END_TEST(testAtomicOperationsI64)

// T is the primitive float type we're testing, and A and B are references to
// constant bindings holding values of that type.
//
// Stay away from 0, NaN, infinities, and denormals.

#define ATOMIC_FLOAT_TESTS(T, A, B)                                     \
  T* q = (T*)hidePointerValue((void*)atomicMem);                        \
  *q = A;                                                               \
  SharedMem<T*> p =                                                     \
      SharedMem<T*>::shared((T*)hidePointerValue((T*)atomicMem));       \
  CHECK(*q == A);                                                       \
  CHECK(jit::AtomicOperations::loadSafeWhenRacy(p) == A);               \
  jit::AtomicOperations::storeSafeWhenRacy(p, B);                       \
  CHECK(*q == B);                                                       \
  T* q2 = (T*)hidePointerValue((void*)atomicMem2);                      \
  SharedMem<T*> p2 =                                                    \
      SharedMem<T*>::shared((T*)hidePointerValue((void*)atomicMem2));   \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memcpySafeWhenRacy(p2, p, sizeof(T));          \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memcpySafeWhenRacy(p2, p.unwrap(), sizeof(T)); \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memcpySafeWhenRacy(p2.unwrap(), p, sizeof(T)); \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::memmoveSafeWhenRacy(p2, p, sizeof(T));         \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::podCopySafeWhenRacy(p2, p, 1);                 \
  CHECK(*q2 == A);                                                      \
  *q = A;                                                               \
  *q2 = B;                                                              \
  jit::AtomicOperations::podMoveSafeWhenRacy(p2, p, 1);                 \
  CHECK(*q2 == A);                                                      \
  return true

BEGIN_REUSABLE_TEST(testAtomicOperationsF32) {
  const float A(123.25);
  const float B(-987.75);
  ATOMIC_FLOAT_TESTS(float, A, B);
}
END_TEST(testAtomicOperationsF32)

BEGIN_REUSABLE_TEST(testAtomicOperationsF64) {
  const double A(123.25);
  const double B(-987.75);
  ATOMIC_FLOAT_TESTS(double, A, B);
}
END_TEST(testAtomicOperationsF64)

#define ATOMIC_CLAMPED_TESTS(T, A, B)                             \
  T* q = (T*)hidePointerValue((void*)atomicMem);                  \
  *q = A;                                                         \
  SharedMem<T*> p =                                               \
      SharedMem<T*>::shared((T*)hidePointerValue((T*)atomicMem)); \
  CHECK(*q == A);                                                 \
  CHECK(jit::AtomicOperations::loadSafeWhenRacy(p) == A);         \
  jit::AtomicOperations::storeSafeWhenRacy(p, B);                 \
  CHECK(*q == B);                                                 \
  return true

BEGIN_REUSABLE_TEST(testAtomicOperationsU8Clamped) {
  const uint8_clamped A(0xab);
  const uint8_clamped B(0x37);
  ATOMIC_CLAMPED_TESTS(uint8_clamped, A, B);
}
END_TEST(testAtomicOperationsU8Clamped)

#undef ATOMIC_TESTS
#undef ATOMIC_FLOAT_TESTS
#undef ATOMIC_CLAMPED_TESTS