Commit 9f727741 authored by Shu-yu Guo's avatar Shu-yu Guo Committed by V8 LUCI CQ

[builtins] Fix Array#groupBy fast path assumptions

The FastArray path for Array#groupBy and Array#groupByToMap does not
recheck the input array's length each iteration. This is incorrect since
the grouping callback can truncate the length, and we should deopt to the
generic path when this happens.

Bug: chromium:1312838, v8:12499
Change-Id: Id3a4973e9960500a2f29ed63281ea721777d4dd3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3570342Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Auto-Submit: Shu-yu Guo <syg@chromium.org>
Commit-Queue: Shu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79787}
parent c39e47aa
...@@ -1564,22 +1564,6 @@ inline Handle<OrderedHashMap> AddValueToKeyedGroup( ...@@ -1564,22 +1564,6 @@ inline Handle<OrderedHashMap> AddValueToKeyedGroup(
return groups; return groups;
} }
inline ElementsKind DeduceKeyedGroupElementsKind(ElementsKind kind) {
// The keyed groups are array lists with fast elements.
// Double elements are stored as HeapNumbers in the keyed group elements
// so that we don't need to cast all the keyed groups when switching from
// fast path to the generic path.
// TODO(v8:12499) add unboxed double elements support
switch (kind) {
case ElementsKind::PACKED_SMI_ELEMENTS: {
return ElementsKind::PACKED_SMI_ELEMENTS;
}
default: {
return ElementsKind::PACKED_ELEMENTS;
}
}
}
inline bool IsFastArray(Handle<JSReceiver> object) { inline bool IsFastArray(Handle<JSReceiver> object) {
Isolate* isolate = object->GetIsolate(); Isolate* isolate = object->GetIsolate();
if (isolate->force_slow_path()) return false; if (isolate->force_slow_path()) return false;
...@@ -1659,7 +1643,10 @@ inline MaybeHandle<OrderedHashMap> GenericArrayGroupBy( ...@@ -1659,7 +1643,10 @@ inline MaybeHandle<OrderedHashMap> GenericArrayGroupBy(
template <GroupByMode mode> template <GroupByMode mode>
inline MaybeHandle<OrderedHashMap> FastArrayGroupBy( inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
Isolate* isolate, Handle<JSArray> array, Handle<Object> callbackfn, Isolate* isolate, Handle<JSArray> array, Handle<Object> callbackfn,
Handle<OrderedHashMap> groups, double len) { Handle<OrderedHashMap> groups, double len,
ElementsKind* result_elements_kind) {
DCHECK_NOT_NULL(result_elements_kind);
Handle<Map> original_map = Handle<Map>(array->map(), isolate); Handle<Map> original_map = Handle<Map>(array->map(), isolate);
uint32_t uint_len = static_cast<uint32_t>(len); uint32_t uint_len = static_cast<uint32_t>(len);
ElementsAccessor* accessor = array->GetElementsAccessor(); ElementsAccessor* accessor = array->GetElementsAccessor();
...@@ -1667,7 +1654,8 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy( ...@@ -1667,7 +1654,8 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
// 4. Let k be 0. // 4. Let k be 0.
// 6. Repeat, while k < len // 6. Repeat, while k < len
for (InternalIndex k : InternalIndex::Range(uint_len)) { for (InternalIndex k : InternalIndex::Range(uint_len)) {
if (!CheckArrayMapNotModified(array, original_map)) { if (!CheckArrayMapNotModified(array, original_map) ||
k.as_uint32() >= static_cast<uint32_t>(array->length().Number())) {
return GenericArrayGroupBy<mode>(isolate, array, callbackfn, groups, return GenericArrayGroupBy<mode>(isolate, array, callbackfn, groups,
k.as_uint32(), len); k.as_uint32(), len);
} }
...@@ -1709,6 +1697,17 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy( ...@@ -1709,6 +1697,17 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
// done by the loop. // done by the loop.
} }
// When staying on the fast path, we can deduce a more specific results
// ElementsKind for the keyed groups based on the input ElementsKind.
//
// Double elements are stored as HeapNumbers in the keyed group elements
// so that we don't need to cast all the keyed groups when switching from
// fast path to the generic path.
// TODO(v8:12499) add unboxed double elements support
if (array->GetElementsKind() == ElementsKind::PACKED_SMI_ELEMENTS) {
*result_elements_kind = ElementsKind::PACKED_SMI_ELEMENTS;
}
return groups; return groups;
} }
...@@ -1738,16 +1737,13 @@ BUILTIN(ArrayPrototypeGroupBy) { ...@@ -1738,16 +1737,13 @@ BUILTIN(ArrayPrototypeGroupBy) {
// 5. Let groups be a new empty List. // 5. Let groups be a new empty List.
Handle<OrderedHashMap> groups = isolate->factory()->NewOrderedHashMap(); Handle<OrderedHashMap> groups = isolate->factory()->NewOrderedHashMap();
// Elements kind of the array for grouped elements kind deduction. ElementsKind result_elements_kind = ElementsKind::PACKED_ELEMENTS;
ElementsKind elements_kind = ElementsKind::NO_ELEMENTS;
if (IsFastArray(O)) { if (IsFastArray(O)) {
Handle<JSArray> array = Handle<JSArray>::cast(O); Handle<JSArray> array = Handle<JSArray>::cast(O);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups, isolate, groups,
FastArrayGroupBy<GroupByMode::kToObject>(isolate, array, callbackfn, FastArrayGroupBy<GroupByMode::kToObject>(
groups, len)); isolate, array, callbackfn, groups, len, &result_elements_kind));
// Get array's elements kind after called into javascript.
elements_kind = array->GetElementsKind();
} else { } else {
// 4. Let k be 0. // 4. Let k be 0.
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
...@@ -1758,8 +1754,7 @@ BUILTIN(ArrayPrototypeGroupBy) { ...@@ -1758,8 +1754,7 @@ BUILTIN(ArrayPrototypeGroupBy) {
// 7. Let obj be ! OrdinaryObjectCreate(null). // 7. Let obj be ! OrdinaryObjectCreate(null).
Handle<JSObject> obj = isolate->factory()->NewJSObjectWithNullProto(); Handle<JSObject> obj = isolate->factory()->NewJSObjectWithNullProto();
ElementsKind result_elements_kind =
DeduceKeyedGroupElementsKind(elements_kind);
// 8. For each Record { [[Key]], [[Elements]] } g of groups, do // 8. For each Record { [[Key]], [[Elements]] } g of groups, do
for (InternalIndex entry : groups->IterateEntries()) { for (InternalIndex entry : groups->IterateEntries()) {
Handle<Name> key = Handle<Name>(Name::cast(groups->KeyAt(entry)), isolate); Handle<Name> key = Handle<Name>(Name::cast(groups->KeyAt(entry)), isolate);
...@@ -1804,16 +1799,13 @@ BUILTIN(ArrayPrototypeGroupByToMap) { ...@@ -1804,16 +1799,13 @@ BUILTIN(ArrayPrototypeGroupByToMap) {
// 5. Let groups be a new empty List. // 5. Let groups be a new empty List.
Handle<OrderedHashMap> groups = isolate->factory()->NewOrderedHashMap(); Handle<OrderedHashMap> groups = isolate->factory()->NewOrderedHashMap();
// Elements kind of the array for grouped elements kind deduction. ElementsKind result_elements_kind = ElementsKind::PACKED_ELEMENTS;
ElementsKind elements_kind = ElementsKind::NO_ELEMENTS;
if (IsFastArray(O)) { if (IsFastArray(O)) {
Handle<JSArray> array = Handle<JSArray>::cast(O); Handle<JSArray> array = Handle<JSArray>::cast(O);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups, isolate, groups,
FastArrayGroupBy<GroupByMode::kToMap>(isolate, array, callbackfn, FastArrayGroupBy<GroupByMode::kToMap>(
groups, len)); isolate, array, callbackfn, groups, len, &result_elements_kind));
// Get array's elements kind after called into javascript.
elements_kind = array->GetElementsKind();
} else { } else {
// 4. Let k be 0. // 4. Let k be 0.
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
...@@ -1825,8 +1817,6 @@ BUILTIN(ArrayPrototypeGroupByToMap) { ...@@ -1825,8 +1817,6 @@ BUILTIN(ArrayPrototypeGroupByToMap) {
// 7. Let map be ! Construct(%Map%). // 7. Let map be ! Construct(%Map%).
Handle<JSMap> map = isolate->factory()->NewJSMap(); Handle<JSMap> map = isolate->factory()->NewJSMap();
Handle<OrderedHashMap> map_table = isolate->factory()->NewOrderedHashMap(); Handle<OrderedHashMap> map_table = isolate->factory()->NewOrderedHashMap();
ElementsKind result_elements_kind =
DeduceKeyedGroupElementsKind(elements_kind);
// 8. For each Record { [[Key]], [[Elements]] } g of groups, do // 8. For each Record { [[Key]], [[Elements]] } g of groups, do
for (InternalIndex entry : groups->IterateEntries()) { for (InternalIndex entry : groups->IterateEntries()) {
Handle<Object> key = Handle<Object>(groups->KeyAt(entry), isolate); Handle<Object> key = Handle<Object>(groups->KeyAt(entry), isolate);
......
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Flags: --harmony-array-grouping
// Test OOB indexing on fast path.
let arr1 = [];
for (let i = 0; i < 32; i++) arr1.push(i);
let popped = false;
let grouped1 = arr1.groupBy(() => {
// Pop all of the elements to trigger right-trimming of the elements
// FixedArray.
for (let i = 0, len = arr1.length; i < len; i++) {
arr1.pop();
}
});
// 'undefined' is the only group.
assertArrayEquals(['undefined'], Object.getOwnPropertyNames(grouped1));
// 0 the only value in the group because the grouping function pops the entire
// array.
let expectedGrouped1 = [0];
for (let i = 1; i < 32; i++) expectedGrouped1.push(undefined);
assertArrayEquals(expectedGrouped1, grouped1['undefined']);
// Test result ElementsKind deduction on fast path.
//
// Initial Smi array, but due to length truncation result is not a Smi array.
let arr2 = [0,1,2,3,4,5,6,7,8,9];
let grouped2 = arr2.groupBy(() => { arr2.length = 2; });
// 'undefined' is the only group.
assertArrayEquals(['undefined'], Object.getOwnPropertyNames(grouped2));
// 0,1 are the only values in the group because the source array gets truncated
// to length 2.
let expectedGrouped2 = [0,1];
for (let i = 2; i < 10; i++) expectedGrouped2.push(undefined);
assertArrayEquals(expectedGrouped2, grouped2['undefined']);
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