| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/runtime/runtime.h" | 8 #include "src/runtime/runtime.h" |
| 9 #include "src/runtime/runtime-utils.h" | 9 #include "src/runtime/runtime-utils.h" |
| 10 | 10 |
| 11 | 11 |
| 12 namespace v8 { | 12 namespace v8 { |
| 13 namespace internal { | 13 namespace internal { |
| 14 | 14 |
| 15 namespace { |
| 16 |
| 17 size_t MaybeUnguardArrayBuffer(Isolate* isolate, JSArrayBuffer* array_buffer) { |
| 18 size_t allocated_length = NumberToSize(isolate, array_buffer->byte_length()); |
| 19 if (!array_buffer->is_guarded()) return allocated_length; |
| 20 array_buffer->set_is_guarded(false); |
| 21 size_t const page_size = base::OS::CommitPageSize(); |
| 22 CHECK(allocated_length % page_size == 0); |
| 23 CHECK((bit_cast<uintptr_t>(array_buffer->backing_store()) % page_size) == 0); |
| 24 isolate->RemoveGuardArea( |
| 25 bit_cast<uint8_t*>(array_buffer->backing_store()) + allocated_length, |
| 26 page_size); |
| 27 return allocated_length += page_size; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 |
| 15 void Runtime::FreeArrayBuffer(Isolate* isolate, | 33 void Runtime::FreeArrayBuffer(Isolate* isolate, |
| 16 JSArrayBuffer* phantom_array_buffer) { | 34 JSArrayBuffer* phantom_array_buffer) { |
| 35 size_t const allocated_length = |
| 36 MaybeUnguardArrayBuffer(isolate, phantom_array_buffer); |
| 17 if (phantom_array_buffer->should_be_freed()) { | 37 if (phantom_array_buffer->should_be_freed()) { |
| 18 DCHECK(phantom_array_buffer->is_external()); | 38 DCHECK(phantom_array_buffer->is_external()); |
| 19 free(phantom_array_buffer->backing_store()); | 39 free(phantom_array_buffer->backing_store()); |
| 20 } | 40 } |
| 21 if (phantom_array_buffer->is_external()) return; | 41 if (phantom_array_buffer->is_external()) return; |
| 22 | 42 |
| 23 size_t allocated_length = | |
| 24 NumberToSize(isolate, phantom_array_buffer->byte_length()); | |
| 25 | |
| 26 reinterpret_cast<v8::Isolate*>(isolate) | 43 reinterpret_cast<v8::Isolate*>(isolate) |
| 27 ->AdjustAmountOfExternalAllocatedMemory( | 44 ->AdjustAmountOfExternalAllocatedMemory( |
| 28 -static_cast<int64_t>(allocated_length)); | 45 -static_cast<int64_t>(allocated_length)); |
| 29 CHECK(V8::ArrayBufferAllocator() != NULL); | 46 CHECK(V8::ArrayBufferAllocator() != NULL); |
| 30 V8::ArrayBufferAllocator()->Free(phantom_array_buffer->backing_store(), | 47 V8::ArrayBufferAllocator()->Free(phantom_array_buffer->backing_store(), |
| 31 allocated_length); | 48 allocated_length); |
| 32 } | 49 } |
| 33 | 50 |
| 34 | 51 |
| 35 void Runtime::SetupArrayBuffer(Isolate* isolate, | 52 void Runtime::SetupArrayBuffer(Isolate* isolate, |
| 36 Handle<JSArrayBuffer> array_buffer, | 53 Handle<JSArrayBuffer> array_buffer, |
| 37 bool is_external, void* data, | 54 bool is_external, bool is_guarded, void* data, |
| 38 size_t allocated_length) { | 55 size_t allocated_length) { |
| 39 DCHECK(array_buffer->GetInternalFieldCount() == | 56 DCHECK(array_buffer->GetInternalFieldCount() == |
| 40 v8::ArrayBuffer::kInternalFieldCount); | 57 v8::ArrayBuffer::kInternalFieldCount); |
| 41 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { | 58 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { |
| 42 array_buffer->SetInternalField(i, Smi::FromInt(0)); | 59 array_buffer->SetInternalField(i, Smi::FromInt(0)); |
| 43 } | 60 } |
| 44 array_buffer->set_backing_store(data); | 61 array_buffer->set_backing_store(data); |
| 45 array_buffer->set_flag(Smi::FromInt(0)); | 62 array_buffer->set_flag(Smi::FromInt(0)); |
| 46 array_buffer->set_is_external(is_external); | 63 array_buffer->set_is_external(is_external); |
| 47 array_buffer->set_is_neuterable(true); | 64 array_buffer->set_is_neuterable(true); |
| 65 array_buffer->set_is_guarded(is_guarded); |
| 48 | 66 |
| 49 Handle<Object> byte_length = | 67 Handle<Object> byte_length = |
| 50 isolate->factory()->NewNumberFromSize(allocated_length); | 68 isolate->factory()->NewNumberFromSize(allocated_length); |
| 51 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); | 69 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 52 array_buffer->set_byte_length(*byte_length); | 70 array_buffer->set_byte_length(*byte_length); |
| 53 | 71 |
| 54 array_buffer->set_weak_next(isolate->heap()->array_buffers_list()); | 72 array_buffer->set_weak_next(isolate->heap()->array_buffers_list()); |
| 55 isolate->heap()->set_array_buffers_list(*array_buffer); | 73 isolate->heap()->set_array_buffers_list(*array_buffer); |
| 56 array_buffer->set_weak_first_view(isolate->heap()->undefined_value()); | 74 array_buffer->set_weak_first_view(isolate->heap()->undefined_value()); |
| 57 } | 75 } |
| 58 | 76 |
| 59 | 77 |
| 60 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate, | 78 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate, |
| 61 Handle<JSArrayBuffer> array_buffer, | 79 Handle<JSArrayBuffer> array_buffer, |
| 62 size_t allocated_length, | 80 size_t allocated_length, |
| 63 bool initialize) { | 81 bool initialize) { |
| 64 void* data; | 82 void* data; |
| 83 bool is_guarded = false; |
| 65 CHECK(V8::ArrayBufferAllocator() != NULL); | 84 CHECK(V8::ArrayBufferAllocator() != NULL); |
| 66 if (allocated_length != 0) { | 85 if (allocated_length != 0) { |
| 67 if (initialize) { | 86 const size_t page_size = base::OS::CommitPageSize(); |
| 87 if (allocated_length % page_size == 0) { |
| 88 data = V8::ArrayBufferAllocator()->Allocate(allocated_length + page_size, |
| 89 page_size); |
| 90 if (data == NULL) return false; |
| 91 isolate->AddGuardArea(bit_cast<uint8_t*>(data) + allocated_length, |
| 92 page_size); |
| 93 is_guarded = true; |
| 94 } else { |
| 68 data = V8::ArrayBufferAllocator()->Allocate(allocated_length); | 95 data = V8::ArrayBufferAllocator()->Allocate(allocated_length); |
| 69 } else { | |
| 70 data = | |
| 71 V8::ArrayBufferAllocator()->AllocateUninitialized(allocated_length); | |
| 72 } | 96 } |
| 73 if (data == NULL) return false; | 97 if (data == NULL) return false; |
| 74 } else { | 98 } else { |
| 75 data = NULL; | 99 data = NULL; |
| 76 } | 100 } |
| 77 | 101 |
| 78 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); | 102 SetupArrayBuffer(isolate, array_buffer, false, is_guarded, data, |
| 103 allocated_length); |
| 79 | 104 |
| 80 reinterpret_cast<v8::Isolate*>(isolate) | 105 reinterpret_cast<v8::Isolate*>(isolate) |
| 81 ->AdjustAmountOfExternalAllocatedMemory(allocated_length); | 106 ->AdjustAmountOfExternalAllocatedMemory(allocated_length); |
| 82 | 107 |
| 83 return true; | 108 return true; |
| 84 } | 109 } |
| 85 | 110 |
| 86 | 111 |
| 87 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { | 112 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { |
| 88 Isolate* isolate = array_buffer->GetIsolate(); | 113 Isolate* isolate = array_buffer->GetIsolate(); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) { | 194 RUNTIME_FUNCTION(Runtime_ArrayBufferNeuter) { |
| 170 HandleScope scope(isolate); | 195 HandleScope scope(isolate); |
| 171 DCHECK(args.length() == 1); | 196 DCHECK(args.length() == 1); |
| 172 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); | 197 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); |
| 173 if (array_buffer->backing_store() == NULL) { | 198 if (array_buffer->backing_store() == NULL) { |
| 174 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); | 199 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); |
| 175 return isolate->heap()->undefined_value(); | 200 return isolate->heap()->undefined_value(); |
| 176 } | 201 } |
| 177 DCHECK(!array_buffer->is_external()); | 202 DCHECK(!array_buffer->is_external()); |
| 178 void* backing_store = array_buffer->backing_store(); | 203 void* backing_store = array_buffer->backing_store(); |
| 179 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); | 204 size_t const allocated_length = |
| 205 MaybeUnguardArrayBuffer(isolate, *array_buffer); |
| 180 array_buffer->set_is_external(true); | 206 array_buffer->set_is_external(true); |
| 181 Runtime::NeuterArrayBuffer(array_buffer); | 207 Runtime::NeuterArrayBuffer(array_buffer); |
| 182 V8::ArrayBufferAllocator()->Free(backing_store, byte_length); | 208 V8::ArrayBufferAllocator()->Free(backing_store, allocated_length); |
| 183 return isolate->heap()->undefined_value(); | 209 return isolate->heap()->undefined_value(); |
| 184 } | 210 } |
| 185 | 211 |
| 186 | 212 |
| 187 void Runtime::ArrayIdToTypeAndSize(int arrayId, ExternalArrayType* array_type, | 213 void Runtime::ArrayIdToTypeAndSize(int arrayId, ExternalArrayType* array_type, |
| 188 ElementsKind* external_elements_kind, | 214 ElementsKind* external_elements_kind, |
| 189 ElementsKind* fixed_elements_kind, | 215 ElementsKind* fixed_elements_kind, |
| 190 size_t* element_size) { | 216 size_t* element_size) { |
| 191 switch (arrayId) { | 217 switch (arrayId) { |
| 192 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ | 218 #define ARRAY_ID_CASE(Type, type, TYPE, ctype, size) \ |
| (...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 DATA_VIEW_SETTER(Uint16, uint16_t) | 785 DATA_VIEW_SETTER(Uint16, uint16_t) |
| 760 DATA_VIEW_SETTER(Int16, int16_t) | 786 DATA_VIEW_SETTER(Int16, int16_t) |
| 761 DATA_VIEW_SETTER(Uint32, uint32_t) | 787 DATA_VIEW_SETTER(Uint32, uint32_t) |
| 762 DATA_VIEW_SETTER(Int32, int32_t) | 788 DATA_VIEW_SETTER(Int32, int32_t) |
| 763 DATA_VIEW_SETTER(Float32, float) | 789 DATA_VIEW_SETTER(Float32, float) |
| 764 DATA_VIEW_SETTER(Float64, double) | 790 DATA_VIEW_SETTER(Float64, double) |
| 765 | 791 |
| 766 #undef DATA_VIEW_SETTER | 792 #undef DATA_VIEW_SETTER |
| 767 } | 793 } |
| 768 } // namespace v8::internal | 794 } // namespace v8::internal |
| OLD | NEW |