summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testArrayBuffer.cpp
blob: fd74993893f3325c34153fe24b6e45264a7ceac3 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */

#include "builtin/TestingFunctions.h"
#include "js/Array.h"        // JS::NewArrayObject
#include "js/ArrayBuffer.h"  // JS::{GetArrayBuffer{ByteLength,Data},IsArrayBufferObject,NewArrayBuffer{,WithContents},StealArrayBufferContents}
#include "js/ArrayBufferMaybeShared.h"
#include "js/CallAndConstruct.h"
#include "js/Exception.h"
#include "js/experimental/TypedData.h"  // JS_New{Int32,Uint8}ArrayWithBuffer
#include "js/friend/ErrorMessages.h"    // JSMSG_*
#include "js/MemoryFunctions.h"
#include "js/PropertyAndElement.h"  // JS_GetElement, JS_GetProperty, JS_SetElement
#include "js/Realm.h"
#include "jsapi-tests/tests.h"

#include "vm/Realm-inl.h"

BEGIN_TEST(testArrayBuffer_bug720949_steal) {
  static const unsigned NUM_TEST_BUFFERS = 2;
  static const unsigned MAGIC_VALUE_1 = 3;
  static const unsigned MAGIC_VALUE_2 = 17;

  JS::RootedObject buf_len1(cx), buf_len200(cx);
  JS::RootedObject tarray_len1(cx), tarray_len200(cx);

  uint32_t sizes[NUM_TEST_BUFFERS] = {sizeof(uint32_t), 200 * sizeof(uint32_t)};
  JS::HandleObject testBuf[NUM_TEST_BUFFERS] = {buf_len1, buf_len200};
  JS::HandleObject testArray[NUM_TEST_BUFFERS] = {tarray_len1, tarray_len200};

  // Single-element ArrayBuffer (uses fixed slots for storage)
  CHECK(buf_len1 = JS::NewArrayBuffer(cx, sizes[0]));
  CHECK(tarray_len1 = JS_NewInt32ArrayWithBuffer(cx, testBuf[0], 0, -1));

  CHECK(JS_SetElement(cx, testArray[0], 0, MAGIC_VALUE_1));

  // Many-element ArrayBuffer (uses dynamic storage)
  CHECK(buf_len200 = JS::NewArrayBuffer(cx, 200 * sizeof(uint32_t)));
  CHECK(tarray_len200 = JS_NewInt32ArrayWithBuffer(cx, testBuf[1], 0, -1));

  for (unsigned i = 0; i < NUM_TEST_BUFFERS; i++) {
    JS::HandleObject obj = testBuf[i];
    JS::HandleObject view = testArray[i];
    uint32_t size = sizes[i];
    JS::RootedValue v(cx);

    // Byte lengths should all agree
    CHECK(JS::IsArrayBufferObject(obj));
    CHECK_EQUAL(JS::GetArrayBufferByteLength(obj), size);
    CHECK(JS_GetProperty(cx, obj, "byteLength", &v));
    CHECK(v.isInt32(size));
    CHECK(JS_GetProperty(cx, view, "byteLength", &v));
    CHECK(v.isInt32(size));

    // Modifying the underlying data should update the value returned through
    // the view
    {
      JS::AutoCheckCannotGC nogc;
      bool sharedDummy;
      uint8_t* data = JS::GetArrayBufferData(obj, &sharedDummy, nogc);
      CHECK(data != nullptr);
      *reinterpret_cast<uint32_t*>(data) = MAGIC_VALUE_2;
    }
    CHECK(JS_GetElement(cx, view, 0, &v));
    CHECK(v.isInt32(MAGIC_VALUE_2));

    // Steal the contents
    void* contents = JS::StealArrayBufferContents(cx, obj);
    CHECK(contents != nullptr);

    CHECK(JS::IsDetachedArrayBufferObject(obj));

    // Transfer to a new ArrayBuffer
    JS::RootedObject dst(cx,
                         JS::NewArrayBufferWithContents(cx, size, contents));
    CHECK(JS::IsArrayBufferObject(dst));
    {
      JS::AutoCheckCannotGC nogc;
      bool sharedDummy;
      (void)JS::GetArrayBufferData(obj, &sharedDummy, nogc);
    }

    JS::RootedObject dstview(cx, JS_NewInt32ArrayWithBuffer(cx, dst, 0, -1));
    CHECK(dstview != nullptr);

    CHECK_EQUAL(JS::GetArrayBufferByteLength(dst), size);
    {
      JS::AutoCheckCannotGC nogc;
      bool sharedDummy;
      uint8_t* data = JS::GetArrayBufferData(dst, &sharedDummy, nogc);
      CHECK(data != nullptr);
      CHECK_EQUAL(*reinterpret_cast<uint32_t*>(data), MAGIC_VALUE_2);
    }
    CHECK(JS_GetElement(cx, dstview, 0, &v));
    CHECK(v.isInt32(MAGIC_VALUE_2));
  }

  return true;
}
END_TEST(testArrayBuffer_bug720949_steal)

// Varying number of views of a buffer, to test the detachment weak pointers
BEGIN_TEST(testArrayBuffer_bug720949_viewList) {
  JS::RootedObject buffer(cx);

  // No views
  buffer = JS::NewArrayBuffer(cx, 2000);
  buffer = nullptr;
  GC(cx);

  // One view.
  {
    buffer = JS::NewArrayBuffer(cx, 2000);
    JS::RootedObject view(cx, JS_NewUint8ArrayWithBuffer(cx, buffer, 0, -1));
    void* contents = JS::StealArrayBufferContents(cx, buffer);
    CHECK(contents != nullptr);
    JS_free(nullptr, contents);
    GC(cx);
    CHECK(hasDetachedBuffer(view));
    CHECK(JS::IsDetachedArrayBufferObject(buffer));
    view = nullptr;
    GC(cx);
    buffer = nullptr;
    GC(cx);
  }

  // Two views
  {
    buffer = JS::NewArrayBuffer(cx, 2000);

    JS::RootedObject view1(cx, JS_NewUint8ArrayWithBuffer(cx, buffer, 0, -1));
    JS::RootedObject view2(cx, JS_NewUint8ArrayWithBuffer(cx, buffer, 1, 200));

    // Remove, re-add a view
    view2 = nullptr;
    GC(cx);
    view2 = JS_NewUint8ArrayWithBuffer(cx, buffer, 1, 200);

    // Detach
    void* contents = JS::StealArrayBufferContents(cx, buffer);
    CHECK(contents != nullptr);
    JS_free(nullptr, contents);

    CHECK(hasDetachedBuffer(view1));
    CHECK(hasDetachedBuffer(view2));
    CHECK(JS::IsDetachedArrayBufferObject(buffer));

    view1 = nullptr;
    GC(cx);
    view2 = nullptr;
    GC(cx);
    buffer = nullptr;
    GC(cx);
  }

  return true;
}

static void GC(JSContext* cx) {
  JS_GC(cx);
  JS_GC(cx);  // Trigger another to wait for background finalization to end
}

bool hasDetachedBuffer(JS::HandleObject obj) {
  JS::RootedValue v(cx);
  return JS_GetProperty(cx, obj, "byteLength", &v) && v.toInt32() == 0;
}

END_TEST(testArrayBuffer_bug720949_viewList)

BEGIN_TEST(testArrayBuffer_customFreeFunc) {
  ExternalData data("One two three four");

  // The buffer takes ownership of the data.
  JS::RootedObject buffer(
      cx, JS::NewExternalArrayBuffer(cx, data.len(), data.contents(),
                                     &ExternalData::freeCallback, &data));
  CHECK(buffer);
  CHECK(!data.wasFreed());

  size_t len;
  bool isShared;
  uint8_t* bufferData;
  JS::GetArrayBufferLengthAndData(buffer, &len, &isShared, &bufferData);
  CHECK_EQUAL(len, data.len());
  CHECK(bufferData == data.contents());
  CHECK(strcmp(reinterpret_cast<char*>(bufferData), data.asString()) == 0);

  buffer = nullptr;
  JS_GC(cx);
  JS_GC(cx);
  CHECK(data.wasFreed());

  return true;
}
END_TEST(testArrayBuffer_customFreeFunc)

BEGIN_TEST(testArrayBuffer_staticContents) {
  ExternalData data("One two three four");

  // When not passing a free function, the buffer doesn't own the data.
  JS::RootedObject buffer(
      cx, JS::NewExternalArrayBuffer(cx, data.len(), data.contents(), nullptr));
  CHECK(buffer);
  CHECK(!data.wasFreed());

  size_t len;
  bool isShared;
  uint8_t* bufferData;
  JS::GetArrayBufferLengthAndData(buffer, &len, &isShared, &bufferData);
  CHECK_EQUAL(len, data.len());
  CHECK(bufferData == data.contents());
  CHECK(strcmp(reinterpret_cast<char*>(bufferData), data.asString()) == 0);

  buffer = nullptr;
  JS_GC(cx);
  JS_GC(cx);
  CHECK(!data.wasFreed());

  data.free();
  return true;
}
END_TEST(testArrayBuffer_staticContents)

BEGIN_TEST(testArrayBuffer_stealDetachExternal) {
  static const char dataBytes[] = "One two three four";
  ExternalData data(dataBytes);
  JS::RootedObject buffer(
      cx, JS::NewExternalArrayBuffer(cx, data.len(), data.contents(),
                                     &ExternalData::freeCallback, &data));
  CHECK(buffer);
  CHECK(!data.wasFreed());

  void* stolenContents = JS::StealArrayBufferContents(cx, buffer);

  // External buffers are stealable: the data is copied into freshly allocated
  // memory, and the buffer's data pointer is cleared (immediately freeing the
  // data) and the buffer is marked as detached.
  CHECK(stolenContents != data.contents());
  CHECK(strcmp(reinterpret_cast<char*>(stolenContents), dataBytes) == 0);
  CHECK(data.wasFreed());
  CHECK(JS::IsDetachedArrayBufferObject(buffer));

  JS_free(cx, stolenContents);
  return true;
}
END_TEST(testArrayBuffer_stealDetachExternal)

BEGIN_TEST(testArrayBuffer_serializeExternal) {
  JS::RootedValue serializeValue(cx);

  {
    JS::RootedFunction serialize(cx);
    serialize =
        JS_NewFunction(cx, js::testingFunc_serialize, 1, 0, "serialize");
    CHECK(serialize);

    serializeValue.setObject(*JS_GetFunctionObject(serialize));
  }

  ExternalData data("One two three four");
  JS::RootedObject externalBuffer(
      cx, JS::NewExternalArrayBuffer(cx, data.len(), data.contents(),
                                     &ExternalData::freeCallback, &data));
  CHECK(externalBuffer);
  CHECK(!data.wasFreed());

  JS::RootedValue v(cx, JS::ObjectValue(*externalBuffer));
  JS::RootedObject transferMap(cx,
                               JS::NewArrayObject(cx, JS::HandleValueArray(v)));
  CHECK(transferMap);

  JS::RootedValueArray<2> args(cx);
  args[0].setObject(*externalBuffer);
  args[1].setObject(*transferMap);

  // serialize(externalBuffer, [externalBuffer]) should throw for an unhandled
  // BufferContents kind.
  CHECK(!JS::Call(cx, JS::UndefinedHandleValue, serializeValue,
                  JS::HandleValueArray(args), &v));

  JS::ExceptionStack exnStack(cx);
  CHECK(JS::StealPendingExceptionStack(cx, &exnStack));

  JS::ErrorReportBuilder report(cx);
  CHECK(report.init(cx, exnStack, JS::ErrorReportBuilder::NoSideEffects));

  CHECK_EQUAL(report.report()->errorNumber,
              static_cast<unsigned int>(JSMSG_SC_NOT_TRANSFERABLE));

  // Data should have been left alone.
  CHECK(!data.wasFreed());

  v.setNull();
  transferMap = nullptr;
  args[0].setNull();
  args[1].setNull();
  externalBuffer = nullptr;

  JS_GC(cx);
  JS_GC(cx);
  CHECK(data.wasFreed());

  return true;
}
END_TEST(testArrayBuffer_serializeExternal)

BEGIN_TEST(testArrayBuffer_copyData) {
  ExternalData data1("One two three four");
  JS::RootedObject buffer1(cx, JS::NewExternalArrayBuffer(
                                   cx, data1.len(), data1.contents(), nullptr));

  CHECK(buffer1);

  ExternalData data2("Six");
  JS::RootedObject buffer2(cx, JS::NewExternalArrayBuffer(
                                   cx, data2.len(), data2.contents(), nullptr));

  CHECK(buffer2);

  // Check we can't copy from a larger to a smaller buffer.
  CHECK(!JS::ArrayBufferCopyData(cx, buffer2, 0, buffer1, 0, data1.len()));

  // Verify expected exception is thrown.
  {
    JS::ExceptionStack exnStack(cx);
    CHECK(JS::StealPendingExceptionStack(cx, &exnStack));

    JS::ErrorReportBuilder report(cx);
    CHECK(report.init(cx, exnStack, JS::ErrorReportBuilder::NoSideEffects));

    CHECK_EQUAL(report.report()->errorNumber,
                static_cast<unsigned int>(JSMSG_ARRAYBUFFER_COPY_RANGE));
  }

  CHECK(JS::ArrayBufferCopyData(
      cx, buffer1, 0, buffer2, 0,
      data2.len() - 1 /* don't copy null terminator */));

  {
    size_t len;
    bool isShared;
    uint8_t* bufferData;
    JS::GetArrayBufferLengthAndData(buffer1, &len, &isShared, &bufferData);

    ExternalData expected1("Six two three four");

    fprintf(stderr, "expected %s actual %s\n", expected1.asString(),
            bufferData);

    CHECK_EQUAL(len, expected1.len());
    CHECK_EQUAL(memcmp(expected1.contents(), bufferData, expected1.len()), 0);
  }

  return true;
}
END_TEST(testArrayBuffer_copyData)

BEGIN_TEST(testArrayBuffer_copyDataAcrossGlobals) {
  JS::RootedObject otherGlobal(cx, createGlobal(nullptr));
  if (!otherGlobal) {
    return false;
  }

  ExternalData data1("One two three four");
  JS::RootedObject buffer1(cx);
  {
    js::AutoRealm realm(cx, otherGlobal);
    buffer1 =
        JS::NewExternalArrayBuffer(cx, data1.len(), data1.contents(), nullptr);
  }
  CHECK(buffer1);
  CHECK(JS_WrapObject(cx, &buffer1));

  ExternalData data2("Six");
  JS::RootedObject buffer2(cx, JS::NewExternalArrayBuffer(
                                   cx, data2.len(), data2.contents(), nullptr));

  CHECK(buffer2);

  // Check we can't copy from a larger to a smaller buffer.
  CHECK(!JS::ArrayBufferCopyData(cx, buffer2, 0, buffer1, 0, data1.len()));

  // Verify expected exception is thrown.
  {
    JS::ExceptionStack exnStack(cx);
    CHECK(JS::StealPendingExceptionStack(cx, &exnStack));

    JS::ErrorReportBuilder report(cx);
    CHECK(report.init(cx, exnStack, JS::ErrorReportBuilder::NoSideEffects));

    CHECK_EQUAL(report.report()->errorNumber,
                static_cast<unsigned int>(JSMSG_ARRAYBUFFER_COPY_RANGE));
  }

  CHECK(JS::ArrayBufferCopyData(
      cx, buffer1, 0, buffer2, 0,
      data2.len() - 1 /* don't copy null terminator */));

  {
    JS::RootedObject unwrappedBuffer1(
        cx, JS::UnwrapArrayBufferMaybeShared(buffer1));
    CHECK(unwrappedBuffer1);

    size_t len;
    bool isShared;
    uint8_t* bufferData;
    JS::GetArrayBufferLengthAndData(unwrappedBuffer1, &len, &isShared,
                                    &bufferData);

    ExternalData expected1("Six two three four");

    fprintf(stderr, "expected %s actual %s\n", expected1.asString(),
            bufferData);

    CHECK_EQUAL(len, expected1.len());
    CHECK_EQUAL(memcmp(expected1.contents(), bufferData, expected1.len()), 0);
  }

  return true;
}
END_TEST(testArrayBuffer_copyDataAcrossGlobals)

BEGIN_TEST(testArrayBuffer_ArrayBufferClone) {
  ExternalData data("One two three four");
  JS::RootedObject externalBuffer(
      cx, JS::NewExternalArrayBuffer(cx, data.len(), data.contents(), nullptr));

  CHECK(externalBuffer);

  size_t lengthToCopy = 3;
  JS::RootedObject clonedBuffer(
      cx, JS::ArrayBufferClone(cx, externalBuffer, 4, lengthToCopy));
  CHECK(clonedBuffer);

  size_t len;
  bool isShared;
  uint8_t* bufferData;
  JS::GetArrayBufferLengthAndData(clonedBuffer, &len, &isShared, &bufferData);

  CHECK_EQUAL(len, lengthToCopy);

  ExternalData expectedData("two");
  CHECK_EQUAL(memcmp(expectedData.contents(), bufferData, len), 0);

  return true;
}
END_TEST(testArrayBuffer_ArrayBufferClone)