Commit 17d6b5bb authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[builtins] Use uintptr in TypedArray's size computation functions

The size/length limits are still at kSmiMaxValue.

Bug: v8:4153
Change-Id: I6ffda50a3b9f235b97a3718e86df7deadce9f6f8
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1874346
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64514}
parent c195def1
...@@ -1855,9 +1855,6 @@ extern transitioning macro ToInteger_Inline( ...@@ -1855,9 +1855,6 @@ extern transitioning macro ToInteger_Inline(
Context, JSAny, constexpr ToIntegerTruncationMode): Number; Context, JSAny, constexpr ToIntegerTruncationMode): Number;
extern transitioning macro ToLength_Inline(Context, JSAny): Number; extern transitioning macro ToLength_Inline(Context, JSAny): Number;
extern transitioning macro ToNumber_Inline(Context, JSAny): Number; extern transitioning macro ToNumber_Inline(Context, JSAny): Number;
// TODO(v8:4153): Use ToIndex() instead.
extern transitioning macro ToSmiIndex(implicit context: Context)(JSAny):
PositiveSmi labels IfRangeError;
extern transitioning macro ToSmiLength(implicit context: Context)(JSAny): extern transitioning macro ToSmiLength(implicit context: Context)(JSAny):
PositiveSmi labels IfRangeError; PositiveSmi labels IfRangeError;
extern transitioning macro ToString_Inline(Context, JSAny): String; extern transitioning macro ToString_Inline(Context, JSAny): String;
......
...@@ -70,7 +70,7 @@ namespace typed_array { ...@@ -70,7 +70,7 @@ namespace typed_array {
} }
transitioning macro TypedArrayInitialize(implicit context: Context)( transitioning macro TypedArrayInitialize(implicit context: Context)(
initialize: constexpr bool, map: Map, length: PositiveSmi, initialize: constexpr bool, map: Map, length: uintptr,
elementsInfo: typed_array::TypedArrayElementsInfo, elementsInfo: typed_array::TypedArrayElementsInfo,
bufferConstructor: JSReceiver): JSTypedArray { bufferConstructor: JSReceiver): JSTypedArray {
const byteLength = elementsInfo.CalculateByteLength(length) const byteLength = elementsInfo.CalculateByteLength(length)
...@@ -91,8 +91,7 @@ namespace typed_array { ...@@ -91,8 +91,7 @@ namespace typed_array {
const isOnHeap: constexpr bool = true; const isOnHeap: constexpr bool = true;
const typedArray = AllocateTypedArray( const typedArray = AllocateTypedArray(
isOnHeap, map, buffer, byteOffset, byteLength, isOnHeap, map, buffer, byteOffset, byteLength, length);
Convert<uintptr>(length));
if constexpr (initialize) { if constexpr (initialize) {
const backingStore = typedArray.data_ptr; const backingStore = typedArray.data_ptr;
...@@ -113,8 +112,7 @@ namespace typed_array { ...@@ -113,8 +112,7 @@ namespace typed_array {
const buffer = Cast<JSArrayBuffer>(bufferObj) otherwise unreachable; const buffer = Cast<JSArrayBuffer>(bufferObj) otherwise unreachable;
const isOnHeap: constexpr bool = false; const isOnHeap: constexpr bool = false;
return AllocateTypedArray( return AllocateTypedArray(
isOnHeap, map, buffer, byteOffset, byteLength, isOnHeap, map, buffer, byteOffset, byteLength, length);
Convert<uintptr>(length));
} }
} }
...@@ -134,7 +132,8 @@ namespace typed_array { ...@@ -134,7 +132,8 @@ namespace typed_array {
const defaultConstructor: Constructor = GetArrayBufferFunction(); const defaultConstructor: Constructor = GetArrayBufferFunction();
const initialize: constexpr bool = true; const initialize: constexpr bool = true;
return TypedArrayInitialize( return TypedArrayInitialize(
initialize, map, positiveLength, elementsInfo, defaultConstructor); initialize, map, Convert<uintptr>(positiveLength), elementsInfo,
defaultConstructor);
} }
// 22.2.4.4 TypedArray ( object ) // 22.2.4.4 TypedArray ( object )
...@@ -148,7 +147,8 @@ namespace typed_array { ...@@ -148,7 +147,8 @@ namespace typed_array {
otherwise ThrowRangeError(kInvalidTypedArrayLength, initialLength); otherwise ThrowRangeError(kInvalidTypedArrayLength, initialLength);
const initialize: constexpr bool = false; const initialize: constexpr bool = false;
const typedArray = TypedArrayInitialize( const typedArray = TypedArrayInitialize(
initialize, map, length, elementsInfo, bufferConstructor); initialize, map, Convert<uintptr>(length), elementsInfo,
bufferConstructor);
try { try {
const src: JSTypedArray = Cast<JSTypedArray>(arrayLike) otherwise IfSlow; const src: JSTypedArray = Cast<JSTypedArray>(arrayLike) otherwise IfSlow;
...@@ -220,13 +220,10 @@ namespace typed_array { ...@@ -220,13 +220,10 @@ namespace typed_array {
goto IfInvalidAlignment('start offset'); goto IfInvalidAlignment('start offset');
} }
let newLength: PositiveSmi = 0;
let newByteLength: uintptr;
// 8. If length is present and length is not undefined, then // 8. If length is present and length is not undefined, then
if (length != Undefined) { // a. Let newLength be ? ToIndex(length).
// a. Let newLength be ? ToIndex(length). let newLength: uintptr = ToIndex(length) otherwise IfInvalidLength;
newLength = ToSmiIndex(length) otherwise IfInvalidLength; let newByteLength: uintptr;
}
// 9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. // 9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
if (IsDetachedBuffer(buffer)) { if (IsDetachedBuffer(buffer)) {
...@@ -269,8 +266,7 @@ namespace typed_array { ...@@ -269,8 +266,7 @@ namespace typed_array {
const isOnHeap: constexpr bool = false; const isOnHeap: constexpr bool = false;
return AllocateTypedArray( return AllocateTypedArray(
isOnHeap, map, buffer, offset, newByteLength, isOnHeap, map, buffer, offset, newByteLength, newLength);
Convert<uintptr>(newLength));
} }
label IfInvalidAlignment(problemString: String) deferred { label IfInvalidAlignment(problemString: String) deferred {
ThrowInvalidTypedArrayAlignment(map, problemString); ThrowInvalidTypedArrayAlignment(map, problemString);
...@@ -407,4 +403,17 @@ namespace typed_array { ...@@ -407,4 +403,17 @@ namespace typed_array {
} }
return typedArray; return typedArray;
} }
transitioning macro TypedArraySpeciesCreateByBuffer(implicit context:
Context)(
methodName: constexpr string, exemplar: JSTypedArray,
buffer: JSArrayBuffer, beginByteOffset: uintptr,
newLength: uintptr): JSTypedArray {
const numArgs: constexpr int31 = 3;
// TODO(v8:4153): pass length further as uintptr.
const typedArray: JSTypedArray = TypedArraySpeciesCreate(
methodName, numArgs, exemplar, buffer, Convert<Number>(beginByteOffset),
Convert<Number>(newLength));
return typedArray;
}
} }
...@@ -12,7 +12,7 @@ namespace typed_array { ...@@ -12,7 +12,7 @@ namespace typed_array {
macro FastCopy( macro FastCopy(
src: typed_array::AttachedJSTypedArray, dest: JSTypedArray, k: uintptr, src: typed_array::AttachedJSTypedArray, dest: JSTypedArray, k: uintptr,
count: PositiveSmi) labels IfSlow { count: uintptr) labels IfSlow {
if (IsForceSlowPath()) goto IfSlow; if (IsForceSlowPath()) goto IfSlow;
const srcKind: ElementsKind = src.elements_kind; const srcKind: ElementsKind = src.elements_kind;
...@@ -27,10 +27,9 @@ namespace typed_array { ...@@ -27,10 +27,9 @@ namespace typed_array {
goto IfSlow; goto IfSlow;
} }
const countBytes: uintptr = const countBytes: uintptr = destInfo.CalculateByteLength(count)
destInfo.CalculateByteLength(count) otherwise unreachable; otherwise unreachable;
const startOffset: uintptr = const startOffset: uintptr = destInfo.CalculateByteLength(k)
destInfo.CalculateByteLength(Convert<PositiveSmi>(Signed(k)))
otherwise unreachable; otherwise unreachable;
const srcPtr: RawPtr = src.data_ptr + Convert<intptr>(startOffset); const srcPtr: RawPtr = src.data_ptr + Convert<intptr>(startOffset);
...@@ -81,11 +80,11 @@ namespace typed_array { ...@@ -81,11 +80,11 @@ namespace typed_array {
end != Undefined ? ConvertToRelativeIndex(end, len) : len; end != Undefined ? ConvertToRelativeIndex(end, len) : len;
// 8. Let count be max(final - k, 0). // 8. Let count be max(final - k, 0).
const count = Convert<PositiveSmi>(IntPtrMax(Signed(final - k), 0)); const count: uintptr = Unsigned(IntPtrMax(Signed(final - k), 0));
// 9. Let A be ? TypedArraySpeciesCreate(O, « count »). // 9. Let A be ? TypedArraySpeciesCreate(O, « count »).
const dest: JSTypedArray = TypedArraySpeciesCreateByLength( const dest: JSTypedArray =
kBuiltinNameSlice, src, Convert<uintptr>(count)); TypedArraySpeciesCreateByLength(kBuiltinNameSlice, src, count);
if (count > 0) { if (count > 0) {
try { try {
......
...@@ -38,7 +38,7 @@ namespace typed_array { ...@@ -38,7 +38,7 @@ namespace typed_array {
arg1 != Undefined ? ConvertToRelativeIndex(arg1, srcLength) : srcLength; arg1 != Undefined ? ConvertToRelativeIndex(arg1, srcLength) : srcLength;
// 11. Let newLength be max(endIndex - beginIndex, 0). // 11. Let newLength be max(endIndex - beginIndex, 0).
const newLength = Convert<PositiveSmi>(IntPtrMax(Signed(end - begin), 0)); const newLength: uintptr = Unsigned(IntPtrMax(Signed(end - begin), 0));
// 12. Let constructorName be the String value of O.[[TypedArrayName]]. // 12. Let constructorName be the String value of O.[[TypedArrayName]].
// 13. Let elementSize be the Number value of the Element Size value // 13. Let elementSize be the Number value of the Element Size value
...@@ -49,16 +49,13 @@ namespace typed_array { ...@@ -49,16 +49,13 @@ namespace typed_array {
const srcByteOffset: uintptr = source.byte_offset; const srcByteOffset: uintptr = source.byte_offset;
// 15. Let beginByteOffset be srcByteOffset + beginIndex × elementSize. // 15. Let beginByteOffset be srcByteOffset + beginIndex × elementSize.
const beginByteOffset = srcByteOffset + const beginByteOffset =
elementsInfo.CalculateByteLength(Convert<PositiveSmi>(Signed(begin))) srcByteOffset + elementsInfo.CalculateByteLength(begin)
otherwise ThrowRangeError(kInvalidArrayBufferLength); otherwise ThrowRangeError(kInvalidArrayBufferLength);
// 16. Let argumentsList be « buffer, beginByteOffset, newLength ». // 16. Let argumentsList be « buffer, beginByteOffset, newLength ».
const beginByteOffsetNum = Convert<Number>(beginByteOffset);
// 17. Return ? TypedArraySpeciesCreate(O, argumentsList). // 17. Return ? TypedArraySpeciesCreate(O, argumentsList).
const numArgs: constexpr int31 = 3; return TypedArraySpeciesCreateByBuffer(
return TypedArraySpeciesCreate( methodName, source, buffer, beginByteOffset, newLength);
methodName, numArgs, source, buffer, beginByteOffsetNum, newLength);
} }
} }
...@@ -23,20 +23,23 @@ namespace typed_array { ...@@ -23,20 +23,23 @@ namespace typed_array {
@export @export
struct TypedArrayElementsInfo { struct TypedArrayElementsInfo {
// Calculates the number of bytes required for specified number of elements. // Calculates the number of bytes required for specified number of elements.
CalculateByteLength(lengthSmi: PositiveSmi): uintptr labels IfInvalid { CalculateByteLength(length: uintptr): uintptr labels IfInvalid {
const length = Convert<uintptr>(lengthSmi); // TODO(v8:4153): use kArrayBufferMaxByteLength instead of kSmiMaxValue
// to allow creation of huge TypedArrays.
const maxArrayLength = kSmiMaxValue >>> this.sizeLog2;
if (length > maxArrayLength) goto IfInvalid;
const byteLength = length << this.sizeLog2; const byteLength = length << this.sizeLog2;
// If an overflow ocurred, the byte length exceeds
// JSArrayBuffer::kMaxByteLength and is invalid.
if (byteLength >>> this.sizeLog2 != length) goto IfInvalid;
return byteLength; return byteLength;
} }
// Calculates the maximum number of elements supported by a specified number // Calculates the maximum number of elements supported by a specified number
// of bytes. // of bytes.
CalculateLength(byteLength: uintptr): PositiveSmi labels IfInvalid { CalculateLength(byteLength: uintptr): uintptr labels IfInvalid {
return Convert<PositiveSmi>(byteLength >>> this.sizeLog2) const length = byteLength >>> this.sizeLog2;
otherwise IfInvalid; // TODO(v8:4153): use kArrayBufferMaxByteLength instead of kSmiMaxValue
// to allow creation of huge TypedArrays.
if (length > kSmiMaxValue) goto IfInvalid;
return length;
} }
// Determines if `bytes` (byte offset or length) cannot be evenly divided by // Determines if `bytes` (byte offset or length) cannot be evenly divided by
......
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