Commit ed04f49f authored by Marja Hölttä's avatar Marja Hölttä Committed by V8 LUCI CQ

[rab/gsab] RAB / GSAB support for constructing TAs from TAs

Bug: v8:11111
Change-Id: Id4273832d6d48d5a516a04982afcdf92b2cf045d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3447366Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79024}
parent 2694b75e
......@@ -155,15 +155,17 @@ transitioning macro ConstructByArrayLike(implicit context: Context)(
try {
const src: JSTypedArray = Cast<JSTypedArray>(arrayLike) otherwise IfSlow;
if (IsDetachedBuffer(src.buffer)) {
let byteLength: uintptr;
try {
byteLength = LoadJSArrayBufferViewByteLength(src, src.buffer)
otherwise DetachedOrOutOfBounds;
} label DetachedOrOutOfBounds deferred {
ThrowTypeError(MessageTemplate::kDetachedOperation, 'Construct');
} else if (src.elements_kind != elementsInfo.kind) {
}
if (src.elements_kind != elementsInfo.kind) {
goto IfElementsKindMismatch(src.elements_kind);
} else if (length > 0) {
const byteLength = typedArray.byte_length;
dcheck(byteLength <= kArrayBufferMaxByteLength);
if (IsSharedArrayBuffer(src.buffer)) {
typed_array::CallCRelaxedMemcpy(
......@@ -211,16 +213,27 @@ transitioning macro ConstructByTypedArray(implicit context: Context)(
labels IfConstructByArrayLike(JSTypedArray, uintptr, JSReceiver) {
let bufferConstructor: JSReceiver = GetArrayBufferFunction();
const srcBuffer: JSArrayBuffer = srcTypedArray.buffer;
// TODO(petermarshall): Throw on detached typedArray.
let length: uintptr = IsDetachedBuffer(srcBuffer) ? 0 : srcTypedArray.length;
let length: uintptr;
try {
// TODO(petermarshall): Throw on detached typedArray.
length = LoadJSTypedArrayLengthAndCheckDetached(srcTypedArray)
otherwise DetachedOrOutOfBounds;
} label DetachedOrOutOfBounds {
length = 0;
}
// The spec requires that constructing a typed array using a SAB-backed
// typed array use the ArrayBuffer constructor, not the species constructor.
// See https://tc39.github.io/ecma262/#sec-typedarray-typedarray.
if (!IsSharedArrayBuffer(srcBuffer)) {
bufferConstructor = SpeciesConstructor(srcBuffer, bufferConstructor);
// TODO(petermarshall): Throw on detached typedArray.
if (IsDetachedBuffer(srcBuffer)) length = 0;
try {
// TODO(petermarshall): Throw on detached typedArray.
length = LoadJSTypedArrayLengthAndCheckDetached(srcTypedArray)
otherwise DetachedOrOutOfBounds;
} label DetachedOrOutOfBounds {
length = 0;
}
}
goto IfConstructByArrayLike(srcTypedArray, length, bufferConstructor);
}
......@@ -397,7 +410,6 @@ transitioning builtin CreateTypedArray(
return ConstructByArrayBuffer(target, newTarget, buffer, arg2, arg3);
}
case (typedArray: JSTypedArray): {
// TODO(v8:11111): Support RAB / GSAB.
ConstructByTypedArray(typedArray) otherwise IfConstructByArrayLike;
}
case (obj: JSReceiver): {
......
......@@ -3879,12 +3879,6 @@ class TypedElementsAccessor
// All conversions from TypedArrays can be done without allocation.
if (source->IsJSTypedArray()) {
// TODO(v8:11111): Add RAB/GSAB support.
DCHECK(!destination_ta->is_length_tracking());
DCHECK(!destination_ta->is_backed_by_rab());
DCHECK(!Handle<JSTypedArray>::cast(source)->is_length_tracking());
DCHECK(!Handle<JSTypedArray>::cast(source)->is_backed_by_rab());
CHECK(!destination_ta->WasDetached());
bool out_of_bounds = false;
CHECK_LE(offset + length,
......@@ -3899,7 +3893,7 @@ class TypedElementsAccessor
// If we have to copy more elements than we have in the source, we need to
// do special handling and conversion; that happens in the slow case.
if (source_is_bigint == target_is_bigint && !source_ta->WasDetached() &&
length + offset <= source_ta->length()) {
length + offset <= source_ta->GetLength()) {
CopyElementsFromTypedArray(*source_ta, *destination_ta, length, offset);
return *isolate->factory()->undefined_value();
}
......
......@@ -76,14 +76,14 @@ macro IsLengthTrackingJSArrayBufferView(array: JSArrayBufferView): bool {
extern macro LoadVariableLengthJSArrayBufferViewByteLength(
JSArrayBufferView, JSArrayBuffer): uintptr labels DetachedOrOutOfBounds;
@export
macro LoadJSArrayBufferViewByteLength(
view: JSArrayBufferView, buffer: JSArrayBuffer):
uintptr labels DetachedOrOutOfBounds {
view: JSArrayBufferView,
buffer: JSArrayBuffer): uintptr labels DetachedOrOutOfBounds {
if (IsVariableLengthJSArrayBufferView(view)) {
return LoadVariableLengthJSArrayBufferViewByteLength(view, buffer)
otherwise DetachedOrOutOfBounds;
}
if (IsDetachedBuffer(buffer)) goto DetachedOrOutOfBounds;
return view.byte_length;
}
......
......@@ -93,6 +93,93 @@ d8.file.execute('test/mjsunit/typedarray-helpers.js');
/Invalid typed array length: 2/);
})();
(function ConstructFromTypedArray() {
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
const gsab = CreateGrowableSharedArrayBuffer(
4 * sourceCtor.BYTES_PER_ELEMENT,
8 * sourceCtor.BYTES_PER_ELEMENT);
const fixedLength = new sourceCtor(gsab, 0, 4);
const fixedLengthWithOffset = new sourceCtor(
gsab, 2 * sourceCtor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new sourceCtor(gsab, 0);
const lengthTrackingWithOffset = new sourceCtor(
gsab, 2 * sourceCtor.BYTES_PER_ELEMENT);
// Write some data into the array.
const taFull = new sourceCtor(gsab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taFull, i, i + 1);
}
// Orig. array: [1, 2, 3, 4]
// [1, 2, 3, 4] << fixedLength
// [3, 4] << fixedLengthWithOffset
// [1, 2, 3, 4, ...] << lengthTracking
// [3, 4, ...] << lengthTrackingWithOffset
assertEquals([1, 2, 3, 4], ToNumbers(new targetCtor(fixedLength)));
assertEquals([3, 4], ToNumbers(new targetCtor(fixedLengthWithOffset)));
assertEquals([1, 2, 3, 4], ToNumbers(new targetCtor(lengthTracking)));
assertEquals([3, 4], ToNumbers(new targetCtor(lengthTrackingWithOffset)));
// Grow.
gsab.grow(6 * sourceCtor.BYTES_PER_ELEMENT);
for (let i = 0; i < 6; ++i) {
WriteToTypedArray(taFull, i, i + 1);
}
// Orig. array: [1, 2, 3, 4, 5, 6]
// [1, 2, 3, 4] << fixedLength
// [3, 4] << fixedLengthWithOffset
// [1, 2, 3, 4, 5, 6, ...] << lengthTracking
// [3, 4, 5, 6, ...] << lengthTrackingWithOffset
assertEquals([1, 2, 3, 4], ToNumbers(new targetCtor(fixedLength)));
assertEquals([3, 4], ToNumbers(new targetCtor(fixedLengthWithOffset)));
assertEquals([1, 2, 3, 4, 5, 6],
ToNumbers(new targetCtor(lengthTracking)));
assertEquals([3, 4, 5, 6],
ToNumbers(new targetCtor(lengthTrackingWithOffset)));
});
})();
(function ConstructFromTypedArraySpeciesConstructorNotCalled() {
class MySharedArrayBuffer extends SharedArrayBuffer {
constructor(...params) {
super(...params);
}
static get [Symbol.species]() {
throw new Error('This should not be called!');
}
};
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
const gsab = new MySharedArrayBuffer(
4 * sourceCtor.BYTES_PER_ELEMENT,
{maxByteLength: 8 * sourceCtor.BYTES_PER_ELEMENT});
// Write some data into the array.
const taWrite = new sourceCtor(gsab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
const fixedLength = new sourceCtor(gsab, 0, 4);
assertEquals([0, 2, 4, 6], ToNumbers(new targetCtor(fixedLength)));
const fixedLengthWithOffset = new sourceCtor(
gsab, 2 * sourceCtor.BYTES_PER_ELEMENT, 2);
assertEquals([4, 6], ToNumbers(new targetCtor(fixedLengthWithOffset)));
const lengthTracking = new sourceCtor(gsab, 0);
assertEquals([0, 2, 4, 6], ToNumbers(new targetCtor(lengthTracking)));
const lengthTrackingWithOffset = new sourceCtor(
gsab, 2 * sourceCtor.BYTES_PER_ELEMENT);
assertEquals([4, 6], ToNumbers(new targetCtor(lengthTrackingWithOffset)));
});
})();
(function TypedArrayLengthWhenGrown1() {
const gsab = CreateGrowableSharedArrayBuffer(16, 40);
......
......@@ -60,6 +60,19 @@ function IsBigIntTypedArray(ta) {
return (ta instanceof BigInt64Array) || (ta instanceof BigUint64Array);
}
function AllBigIntMatchedCtorCombinations(test) {
for (let targetCtor of ctors) {
for (let sourceCtor of ctors) {
if (IsBigIntTypedArray(new targetCtor()) !=
IsBigIntTypedArray(new sourceCtor())) {
// Can't mix BigInt and non-BigInt types.
continue;
}
test(targetCtor, sourceCtor);
}
}
}
function ReadDataFromBuffer(ab, ctor) {
let result = [];
const ta = new ctor(ab, 0, ab.byteLength / ctor.BYTES_PER_ELEMENT);
......
......@@ -38,6 +38,57 @@ d8.file.execute('test/mjsunit/typedarray-helpers.js');
}
})();
(function ConstructFromTypedArraySpeciesConstructorDetaches() {
let rab;
class MyArrayBuffer extends ArrayBuffer {
constructor(...params) {
super(...params);
}
static get [Symbol.species]() {
%ArrayBufferDetach(rab);
}
};
function CreateRabForTest(ctor) {
const rab = new MyArrayBuffer(
4 * ctor.BYTES_PER_ELEMENT,
{maxByteLength: 8 * ctor.BYTES_PER_ELEMENT});
// Write some data into the array.
const taWrite = new ctor(rab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
return rab;
}
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const fixedLength = new sourceCtor(rab, 0, 4);
assertThrows(() => { new targetCtor(fixedLength); }, TypeError);
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const fixedLengthWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT, 2);
assertThrows(() => { new targetCtor(fixedLengthWithOffset); }, TypeError);
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const lengthTracking = new sourceCtor(rab, 0);
assertThrows(() => { new targetCtor(lengthTracking); }, TypeError);
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const lengthTrackingWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT);
assertThrows(() => { new targetCtor(lengthTrackingWithOffset); },
TypeError);
});
})();
(function AccessDetachedTypedArray() {
const rab = CreateResizableArrayBuffer(16, 40);
......
......@@ -93,6 +93,209 @@ d8.file.execute('test/mjsunit/typedarray-helpers.js');
/Invalid typed array length: 2/);
})();
(function ConstructFromTypedArray() {
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
const rab = CreateResizableArrayBuffer(
4 * sourceCtor.BYTES_PER_ELEMENT,
8 * sourceCtor.BYTES_PER_ELEMENT);
const fixedLength = new sourceCtor(rab, 0, 4);
const fixedLengthWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new sourceCtor(rab, 0);
const lengthTrackingWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT);
// Write some data into the array.
const taFull = new sourceCtor(rab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taFull, i, i + 1);
}
// Orig. array: [1, 2, 3, 4]
// [1, 2, 3, 4] << fixedLength
// [3, 4] << fixedLengthWithOffset
// [1, 2, 3, 4, ...] << lengthTracking
// [3, 4, ...] << lengthTrackingWithOffset
assertEquals([1, 2, 3, 4], ToNumbers(new targetCtor(fixedLength)));
assertEquals([3, 4], ToNumbers(new targetCtor(fixedLengthWithOffset)));
assertEquals([1, 2, 3, 4], ToNumbers(new targetCtor(lengthTracking)));
assertEquals([3, 4], ToNumbers(new targetCtor(lengthTrackingWithOffset)));
// Shrink so that fixed length TAs go out of bounds.
rab.resize(3 * sourceCtor.BYTES_PER_ELEMENT);
// Orig. array: [1, 2, 3]
// [1, 2, 3, ...] << lengthTracking
// [3, ...] << lengthTrackingWithOffset
assertThrows(() => { new targetCtor(fixedLength); }, TypeError);
assertThrows(() => { new targetCtor(fixedLengthWithOffset); }, TypeError);
assertEquals([1, 2, 3], ToNumbers(new targetCtor(lengthTracking)));
assertEquals([3], ToNumbers(new targetCtor(lengthTrackingWithOffset)));
// Shrink so that the TAs with offset go out of bounds.
rab.resize(1 * sourceCtor.BYTES_PER_ELEMENT);
assertThrows(() => { new targetCtor(fixedLength); }, TypeError);
assertThrows(() => { new targetCtor(fixedLengthWithOffset); }, TypeError);
assertEquals([1], ToNumbers(new targetCtor(lengthTracking)));
assertThrows(() => { new targetCtor(lengthTrackingWithOffset); },
TypeError);
// Shrink to zero.
rab.resize(0);
assertThrows(() => { new targetCtor(fixedLength); }, TypeError);
assertThrows(() => { new targetCtor(fixedLengthWithOffset); }, TypeError);
assertEquals([], ToNumbers(new targetCtor(lengthTracking)));
assertThrows(() => { new targetCtor(lengthTrackingWithOffset); },
TypeError);
// Grow so that all TAs are back in-bounds.
rab.resize(6 * sourceCtor.BYTES_PER_ELEMENT);
for (let i = 0; i < 6; ++i) {
WriteToTypedArray(taFull, i, i + 1);
}
// Orig. array: [1, 2, 3, 4, 5, 6]
// [1, 2, 3, 4] << fixedLength
// [3, 4] << fixedLengthWithOffset
// [1, 2, 3, 4, 5, 6, ...] << lengthTracking
// [3, 4, 5, 6, ...] << lengthTrackingWithOffset
assertEquals([1, 2, 3, 4], ToNumbers(new targetCtor(fixedLength)));
assertEquals([3, 4], ToNumbers(new targetCtor(fixedLengthWithOffset)));
assertEquals([1, 2, 3, 4, 5, 6],
ToNumbers(new targetCtor(lengthTracking)));
assertEquals([3, 4, 5, 6],
ToNumbers(new targetCtor(lengthTrackingWithOffset)));
});
})();
(function ConstructFromTypedArraySpeciesConstructorShrinks() {
let rab;
let resizeTo;
class MyArrayBuffer extends ArrayBuffer {
constructor(...params) {
super(...params);
}
static get [Symbol.species]() {
rab.resize(resizeTo);
}
};
function CreateRabForTest(ctor) {
const rab = new MyArrayBuffer(
4 * ctor.BYTES_PER_ELEMENT,
{maxByteLength: 8 * ctor.BYTES_PER_ELEMENT});
// Write some data into the array.
const taWrite = new ctor(rab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
return rab;
}
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const fixedLength = new sourceCtor(rab, 0, 4);
resizeTo = 2 * sourceCtor.BYTES_PER_ELEMENT;
assertThrows(() => { new targetCtor(fixedLength); }, TypeError);
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const fixedLengthWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT, 2);
resizeTo = 2 * sourceCtor.BYTES_PER_ELEMENT;
assertThrows(() => { new targetCtor(fixedLengthWithOffset); }, TypeError);
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const lengthTracking = new sourceCtor(rab, 0);
resizeTo = 2 * sourceCtor.BYTES_PER_ELEMENT;
assertEquals([0, 2], ToNumbers(new targetCtor(lengthTracking)));
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const lengthTrackingWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT);
resizeTo = 3 * sourceCtor.BYTES_PER_ELEMENT;
assertEquals([4], ToNumbers(new targetCtor(lengthTrackingWithOffset)));
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const lengthTrackingWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT);
resizeTo = 1 * sourceCtor.BYTES_PER_ELEMENT;
assertThrows(() => { new targetCtor(lengthTrackingWithOffset); },
TypeError);
});
})();
(function ConstructFromTypedArraySpeciesConstructorGrows() {
let rab;
let resizeTo;
class MyArrayBuffer extends ArrayBuffer {
constructor(...params) {
super(...params);
}
static get [Symbol.species]() {
rab.resize(resizeTo);
}
};
function CreateRabForTest(ctor) {
const rab = new MyArrayBuffer(
4 * ctor.BYTES_PER_ELEMENT,
{maxByteLength: 8 * ctor.BYTES_PER_ELEMENT});
// Write some data into the array.
const taWrite = new ctor(rab);
for (let i = 0; i < 4; ++i) {
WriteToTypedArray(taWrite, i, 2 * i);
}
return rab;
}
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const fixedLength = new sourceCtor(rab, 0, 4);
resizeTo = 6 * sourceCtor.BYTES_PER_ELEMENT;
// Fixed-length TA unaffected by growing.
assertEquals([0, 2, 4, 6], ToNumbers(new targetCtor(fixedLength)));
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const fixedLengthWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT, 2);
resizeTo = 6 * sourceCtor.BYTES_PER_ELEMENT;
// Fixed-length TA unaffected by growing.
assertEquals([4, 6], ToNumbers(new targetCtor(fixedLengthWithOffset)));
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const lengthTracking = new sourceCtor(rab, 0);
resizeTo = 6 * sourceCtor.BYTES_PER_ELEMENT;
assertEquals([0, 2, 4, 6, 0, 0],
ToNumbers(new targetCtor(lengthTracking)));
});
AllBigIntMatchedCtorCombinations((targetCtor, sourceCtor) => {
rab = CreateRabForTest(sourceCtor);
const lengthTrackingWithOffset = new sourceCtor(
rab, 2 * sourceCtor.BYTES_PER_ELEMENT);
resizeTo = 6 * sourceCtor.BYTES_PER_ELEMENT;
assertEquals([4, 6, 0, 0],
ToNumbers(new targetCtor(lengthTrackingWithOffset)));
});
})();
(function TypedArrayLengthWhenResizedOutOfBounds1() {
const rab = CreateResizableArrayBuffer(16, 40);
......
......@@ -285,8 +285,6 @@
'built-ins/ArrayBuffer/prototype/transfer/this-is-sharedarraybuffer': [PASS],
'built-ins/TypedArray/prototype/sort/BigInt/return-abrupt-from-this-out-of-bounds': [FAIL],
'built-ins/TypedArray/prototype/sort/return-abrupt-from-this-out-of-bounds': [FAIL],
'built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-different-type': [FAIL],
'built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-same-type': [FAIL],
# See also https://github.com/tc39/test262/issues/3380
'built-ins/TypedArray/prototype/map/callbackfn-resize': [FAIL],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment