From 879f6599eee6e1dfcbe9a24bf688b261c03e9558 Mon Sep 17 00:00:00 2001 From: jbroman Date: Tue, 15 Nov 2016 14:42:40 -0800 Subject: [PATCH] Initialize internal fields in Factory::NewJSTypedArray and NewJSDataView. This was causing array buffer views created by ValueDeserializer to have uninitialized internal fields, which lead to crashes in layout tests when Blink tried to read those fields. For array buffers, JSArrayBuffer::Setup is responsible for this logic (as well as initializing the V8 fields); this is similar to that. The runtime already seems to correctly initialize these for script-created array buffer views as well, which is why this issue was not detected sooner. Review-Url: https://codereview.chromium.org/2498413002 Cr-Commit-Position: refs/heads/master@{#41014} --- src/factory.cc | 11 +++++++++++ test/cctest/test-api.cc | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/factory.cc b/src/factory.cc index 61aada853c26..ec30d5a0dbd4 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -2010,6 +2010,12 @@ void SetupArrayBufferView(i::Isolate* isolate, DCHECK(byte_offset + byte_length <= static_cast(buffer->byte_length()->Number())); + DCHECK_EQ(obj->GetInternalFieldCount(), + v8::ArrayBufferView::kInternalFieldCount); + for (int i = 0; i < v8::ArrayBufferView::kInternalFieldCount; i++) { + obj->SetInternalField(i, Smi::kZero); + } + obj->set_buffer(*buffer); i::Handle byte_offset_object = @@ -2079,6 +2085,11 @@ Handle Factory::NewJSTypedArray(ElementsKind elements_kind, size_t number_of_elements, PretenureFlag pretenure) { Handle obj = NewJSTypedArray(elements_kind, pretenure); + DCHECK_EQ(obj->GetInternalFieldCount(), + v8::ArrayBufferView::kInternalFieldCount); + for (int i = 0; i < v8::ArrayBufferView::kInternalFieldCount; i++) { + obj->SetInternalField(i, Smi::kZero); + } size_t element_size = GetFixedTypedArraysElementSize(elements_kind); ExternalArrayType array_type = GetArrayTypeFromElementsKind(elements_kind); diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 437848b8c05f..c5781158885b 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -26156,3 +26156,31 @@ THREADED_TEST(MutableProtoGlobal) { CHECK(result->Equals(context, v8::Integer::New(CcTest::isolate(), 0)) .FromJust()); } + +TEST(InternalFieldsOnTypedArray) { + LocalContext env; + v8::Isolate* isolate = env->GetIsolate(); + v8::HandleScope scope(isolate); + v8::Local context = env.local(); + Context::Scope context_scope(context); + v8::Local buffer = v8::ArrayBuffer::New(isolate, 1); + v8::Local array = v8::Uint8Array::New(buffer, 0, 1); + for (int i = 0; i < v8::ArrayBufferView::kInternalFieldCount; i++) { + CHECK_EQ(static_cast(nullptr), + array->GetAlignedPointerFromInternalField(i)); + } +} + +TEST(InternalFieldsOnDataView) { + LocalContext env; + v8::Isolate* isolate = env->GetIsolate(); + v8::HandleScope scope(isolate); + v8::Local context = env.local(); + Context::Scope context_scope(context); + v8::Local buffer = v8::ArrayBuffer::New(isolate, 1); + v8::Local array = v8::DataView::New(buffer, 0, 1); + for (int i = 0; i < v8::ArrayBufferView::kInternalFieldCount; i++) { + CHECK_EQ(static_cast(nullptr), + array->GetAlignedPointerFromInternalField(i)); + } +}