[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Bill Budge | 07c6ea6 | 2017-12-22 19:16:11 | [diff] [blame] | 5 | #include "gin/array_buffer.h" |
| 6 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 10 | #include "base/allocator/partition_allocator/page_allocator.h" |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 11 | #include "base/logging.h" |
Alexei Filippov | 2eea119 | 2018-03-01 18:01:03 | [diff] [blame] | 12 | #include "base/partition_alloc_buildflags.h" |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 14 | #include "gin/per_isolate_data.h" |
| 15 | |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 16 | #if defined(OS_POSIX) |
| 17 | #include <sys/mman.h> |
| 18 | |
| 19 | #ifndef MAP_ANONYMOUS |
| 20 | #define MAP_ANONYMOUS MAP_ANON |
| 21 | #endif |
Bill Budge | 07c6ea6 | 2017-12-22 19:16:11 | [diff] [blame] | 22 | #endif // defined(OS_POSIX) |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 23 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 24 | namespace gin { |
| 25 | |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | gin::WrapperInfo g_array_buffer_wrapper_info = {gin::kEmbedderNativeGin}; |
| 29 | |
| 30 | } // namespace |
| 31 | |
anujk.sharma | c1b6156b | 2015-01-20 21:47:36 | [diff] [blame] | 32 | static_assert(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT == 2, |
| 33 | "array buffers must have two internal fields"); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 34 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 35 | // ArrayBufferAllocator ------------------------------------------------------- |
| 36 | |
| 37 | void* ArrayBufferAllocator::Allocate(size_t length) { |
Bill Budge | 07c6ea6 | 2017-12-22 19:16:11 | [diff] [blame] | 38 | // TODO(bbudge) Use partition allocator for malloc/calloc allocations. |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 39 | return calloc(1, length); |
| 40 | } |
| 41 | |
| 42 | void* ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
| 43 | return malloc(length); |
| 44 | } |
| 45 | |
| 46 | void ArrayBufferAllocator::Free(void* data, size_t length) { |
| 47 | free(data); |
| 48 | } |
| 49 | |
| 50 | ArrayBufferAllocator* ArrayBufferAllocator::SharedInstance() { |
| 51 | static ArrayBufferAllocator* instance = new ArrayBufferAllocator(); |
| 52 | return instance; |
| 53 | } |
| 54 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 55 | // ArrayBuffer::Private ------------------------------------------------------- |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 56 | |
| 57 | // This class exists to solve a tricky lifetime problem. The V8 API doesn't |
| 58 | // want to expose a direct view into the memory behind an array buffer because |
| 59 | // V8 might deallocate that memory during garbage collection. Instead, the V8 |
| 60 | // API forces us to externalize the buffer and take ownership of the memory. |
| 61 | // In order to know when to free the memory, we need to figure out both when |
| 62 | // we're done with it and when V8 is done with it. |
| 63 | // |
| 64 | // To determine whether we're done with the memory, every view we have into |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 65 | // the array buffer takes a reference to the ArrayBuffer::Private object that |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 66 | // actually owns the memory. To determine when V8 is done with the memory, we |
| 67 | // open a weak handle to the ArrayBuffer object. When we receive the weak |
| 68 | // callback, we know the object is about to be garbage collected and we can |
| 69 | // drop V8's implied reference to the memory. |
| 70 | // |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 71 | // The final subtlety is that we need every ArrayBuffer into the same array |
| 72 | // buffer to AddRef the same ArrayBuffer::Private. To make that work, we store |
| 73 | // a pointer to the ArrayBuffer::Private object in an internal field of the |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 74 | // ArrayBuffer object. |
| 75 | // |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 76 | class ArrayBuffer::Private : public base::RefCounted<ArrayBuffer::Private> { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 77 | public: |
| 78 | static scoped_refptr<Private> From(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 79 | v8::Local<v8::ArrayBuffer> array); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 80 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 81 | void* buffer() const { return buffer_; } |
| 82 | size_t length() const { return length_; } |
| 83 | |
| 84 | private: |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 85 | friend class base::RefCounted<Private>; |
| 86 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 87 | Private(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> array); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 88 | ~Private(); |
| 89 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 90 | static void FirstWeakCallback(const v8::WeakCallbackInfo<Private>& data); |
| 91 | static void SecondWeakCallback(const v8::WeakCallbackInfo<Private>& data); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 92 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 93 | v8::Global<v8::ArrayBuffer> array_buffer_; |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 94 | scoped_refptr<Private> self_reference_; |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 95 | v8::Isolate* isolate_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 96 | void* buffer_; |
| 97 | size_t length_; |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 98 | void* allocation_base_; |
| 99 | size_t allocation_length_; |
| 100 | v8::ArrayBuffer::Allocator::AllocationMode allocation_mode_; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 101 | }; |
| 102 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 103 | scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From( |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 104 | v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> array) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 105 | if (array->IsExternal()) { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 106 | CHECK_EQ(WrapperInfo::From(v8::Local<v8::Object>::Cast(array)), |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 107 | &g_array_buffer_wrapper_info) |
| 108 | << "Cannot mix blink and gin ArrayBuffers"; |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 109 | return base::WrapRefCounted(static_cast<Private*>( |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 110 | array->GetAlignedPointerFromInternalField(kEncodedValueIndex))); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 111 | } |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 112 | return base::WrapRefCounted(new Private(isolate, array)); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 115 | ArrayBuffer::Private::Private(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 116 | v8::Local<v8::ArrayBuffer> array) |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 117 | : array_buffer_(isolate, array), isolate_(isolate) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 118 | // Take ownership of the array buffer. |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 119 | CHECK(!array->IsExternal()); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 120 | v8::ArrayBuffer::Contents contents = array->Externalize(); |
Bill Budge | 2ca3514 | 2018-02-21 23:22:22 | [diff] [blame] | 121 | // We shouldn't receive large page-allocated array buffers. |
| 122 | CHECK_NE(v8::ArrayBuffer::Allocator::AllocationMode::kReservation, |
| 123 | contents.AllocationMode()); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 124 | buffer_ = contents.Data(); |
| 125 | length_ = contents.ByteLength(); |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 126 | allocation_base_ = contents.AllocationBase(); |
| 127 | allocation_length_ = contents.AllocationLength(); |
Eric Holk | b2fa95d | 2017-07-17 18:51:36 | [diff] [blame] | 128 | |
| 129 | DCHECK(reinterpret_cast<uintptr_t>(allocation_base_) <= |
| 130 | reinterpret_cast<uintptr_t>(buffer_)); |
| 131 | DCHECK(reinterpret_cast<uintptr_t>(buffer_) + length_ <= |
| 132 | reinterpret_cast<uintptr_t>(allocation_base_) + allocation_length_); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 133 | |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 134 | array->SetAlignedPointerInInternalField(kWrapperInfoIndex, |
| 135 | &g_array_buffer_wrapper_info); |
| 136 | array->SetAlignedPointerInInternalField(kEncodedValueIndex, this); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 137 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 138 | self_reference_ = this; // Cleared in SecondWeakCallback. |
| 139 | array_buffer_.SetWeak(this, FirstWeakCallback, |
| 140 | v8::WeakCallbackType::kParameter); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 143 | ArrayBuffer::Private::~Private() { |
Bill Budge | 2ca3514 | 2018-02-21 23:22:22 | [diff] [blame] | 144 | PerIsolateData::From(isolate_)->allocator()->Free(allocation_base_, |
| 145 | allocation_length_); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 146 | } |
| 147 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 148 | void ArrayBuffer::Private::FirstWeakCallback( |
| 149 | const v8::WeakCallbackInfo<Private>& data) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 150 | Private* parameter = data.GetParameter(); |
| 151 | parameter->array_buffer_.Reset(); |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 152 | data.SetSecondPassCallback(SecondWeakCallback); |
| 153 | } |
| 154 | |
| 155 | void ArrayBuffer::Private::SecondWeakCallback( |
| 156 | const v8::WeakCallbackInfo<Private>& data) { |
| 157 | Private* parameter = data.GetParameter(); |
[email protected] | 93f9f360 | 2013-11-21 18:38:51 | [diff] [blame] | 158 | parameter->self_reference_ = NULL; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 161 | // ArrayBuffer ---------------------------------------------------------------- |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 162 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 163 | ArrayBuffer::ArrayBuffer() |
| 164 | : bytes_(0), |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 165 | num_bytes_(0) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 168 | ArrayBuffer::ArrayBuffer(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 169 | v8::Local<v8::ArrayBuffer> array) { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 170 | private_ = ArrayBuffer::Private::From(isolate, array); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 171 | bytes_ = private_->buffer(); |
| 172 | num_bytes_ = private_->length(); |
| 173 | } |
| 174 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 175 | ArrayBuffer::~ArrayBuffer() = default; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 176 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 177 | ArrayBuffer& ArrayBuffer::operator=(const ArrayBuffer& other) = default; |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 178 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 179 | // Converter<ArrayBuffer> ----------------------------------------------------- |
| 180 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 181 | bool Converter<ArrayBuffer>::FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 182 | v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 183 | ArrayBuffer* out) { |
| 184 | if (!val->IsArrayBuffer()) |
| 185 | return false; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 186 | *out = ArrayBuffer(isolate, v8::Local<v8::ArrayBuffer>::Cast(val)); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
| 190 | // ArrayBufferView ------------------------------------------------------------ |
| 191 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 192 | ArrayBufferView::ArrayBufferView() |
| 193 | : offset_(0), |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 194 | num_bytes_(0) { |
| 195 | } |
| 196 | |
| 197 | ArrayBufferView::ArrayBufferView(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 198 | v8::Local<v8::ArrayBufferView> view) |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 199 | : array_buffer_(isolate, view->Buffer()), |
| 200 | offset_(view->ByteOffset()), |
| 201 | num_bytes_(view->ByteLength()) { |
| 202 | } |
| 203 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 204 | ArrayBufferView::~ArrayBufferView() = default; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 205 | |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 206 | ArrayBufferView& ArrayBufferView::operator=(const ArrayBufferView& other) = |
| 207 | default; |
[email protected] | dfc613d | 2014-05-16 13:16:52 | [diff] [blame] | 208 | |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 209 | // Converter<ArrayBufferView> ------------------------------------------------- |
| 210 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 211 | bool Converter<ArrayBufferView>::FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 212 | v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 213 | ArrayBufferView* out) { |
| 214 | if (!val->IsArrayBufferView()) |
| 215 | return false; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 216 | *out = ArrayBufferView(isolate, v8::Local<v8::ArrayBufferView>::Cast(val)); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 220 | } // namespace gin |