Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(801)

Side by Side Diff: src/runtime/runtime-wasm.cc

Issue 2396473003: [wasm] Refactor GrowMemory runtime call. (Closed)
Patch Set: Fix header Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/wasm/wasm-module.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 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/runtime/runtime-utils.h" 5 #include "src/runtime/runtime-utils.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/assembler.h" 8 #include "src/assembler.h"
9 #include "src/compiler/wasm-compiler.h" 9 #include "src/compiler/wasm-compiler.h"
10 #include "src/conversions.h" 10 #include "src/conversions.h"
11 #include "src/debug/debug.h" 11 #include "src/debug/debug.h"
12 #include "src/factory.h" 12 #include "src/factory.h"
13 #include "src/frames-inl.h" 13 #include "src/frames-inl.h"
14 #include "src/objects-inl.h" 14 #include "src/objects-inl.h"
15 #include "src/v8memory.h" 15 #include "src/v8memory.h"
16 #include "src/wasm/wasm-module.h" 16 #include "src/wasm/wasm-module.h"
17 17
18 namespace v8 { 18 namespace v8 {
19 namespace internal { 19 namespace internal {
20 20
21 RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { 21 RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
22 HandleScope scope(isolate); 22 HandleScope scope(isolate);
23 DCHECK_EQ(1, args.length()); 23 DCHECK_EQ(1, args.length());
24 CONVERT_UINT32_ARG_CHECKED(delta_pages, 0); 24 CONVERT_UINT32_ARG_CHECKED(delta_pages, 0);
25 Handle<JSObject> module_instance; 25 Handle<JSObject> module_instance;
26
27 { 26 {
28 // Get the module JSObject 27 // Get the module JSObject
29 DisallowHeapAllocation no_allocation; 28 DisallowHeapAllocation no_allocation;
30 const Address entry = Isolate::c_entry_fp(isolate->thread_local_top()); 29 const Address entry = Isolate::c_entry_fp(isolate->thread_local_top());
31 Address pc = 30 Address pc =
32 Memory::Address_at(entry + StandardFrameConstants::kCallerPCOffset); 31 Memory::Address_at(entry + StandardFrameConstants::kCallerPCOffset);
33 Code* code = 32 Code* code =
34 isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code; 33 isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code;
35 Object* owning_instance = wasm::GetOwningWasmInstance(code); 34 Object* owning_instance = wasm::GetOwningWasmInstance(code);
36 CHECK_NOT_NULL(owning_instance); 35 CHECK_NOT_NULL(owning_instance);
37 module_instance = handle(JSObject::cast(owning_instance), isolate); 36 module_instance = handle(JSObject::cast(owning_instance), isolate);
38 } 37 }
39 38 return *isolate->factory()->NewNumberFromInt(
40 Address old_mem_start, new_mem_start; 39 wasm::GrowInstanceMemory(isolate, module_instance, delta_pages));
41 uint32_t old_size, new_size;
42
43 // Get mem buffer associated with module object
44 MaybeHandle<JSArrayBuffer> maybe_mem_buffer =
45 wasm::GetInstanceMemory(isolate, module_instance);
46 Handle<JSArrayBuffer> old_buffer;
47 if (!maybe_mem_buffer.ToHandle(&old_buffer)) {
48 // If module object does not have linear memory associated with it,
49 // Allocate new array buffer of given size.
50 old_mem_start = nullptr;
51 old_size = 0;
52 // TODO(gdeepti): Fix bounds check to take into account size of memtype.
53 new_size = delta_pages * wasm::WasmModule::kPageSize;
54 // The code generated in the wasm compiler guarantees this precondition.
55 DCHECK(delta_pages <= wasm::WasmModule::kMaxMemPages);
56 new_mem_start =
57 static_cast<Address>(isolate->array_buffer_allocator()->Allocate(
58 static_cast<uint32_t>(new_size)));
59 if (new_mem_start == NULL) {
60 return *isolate->factory()->NewNumberFromInt(-1);
61 }
62 #if DEBUG
63 // Double check the API allocator actually zero-initialized the memory.
64 for (size_t i = old_size; i < new_size; i++) {
65 DCHECK_EQ(0, new_mem_start[i]);
66 }
67 #endif
68 } else {
69 old_mem_start = static_cast<Address>(old_buffer->backing_store());
70 old_size = old_buffer->byte_length()->Number();
71 // If the old memory was zero-sized, we should have been in the
72 // "undefined" case above.
73 DCHECK_NOT_NULL(old_mem_start);
74 DCHECK_NE(0, old_size);
75
76 new_size = old_size + delta_pages * wasm::WasmModule::kPageSize;
77 if (new_size >
78 wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) {
79 return *isolate->factory()->NewNumberFromInt(-1);
80 }
81 new_mem_start =
82 static_cast<Address>(isolate->array_buffer_allocator()->Allocate(
83 static_cast<uint32_t>(new_size)));
84 if (new_mem_start == NULL) {
85 return *isolate->factory()->NewNumberFromInt(-1);
86 }
87 #if DEBUG
88 // Double check the API allocator actually zero-initialized the memory.
89 for (size_t i = old_size; i < new_size; i++) {
90 DCHECK_EQ(0, new_mem_start[i]);
91 }
92 #endif
93 // Copy contents of the old buffer to the new buffer
94 memcpy(new_mem_start, old_mem_start, old_size);
95 }
96
97 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
98 JSArrayBuffer::Setup(buffer, isolate, false, new_mem_start, new_size);
99 buffer->set_is_neuterable(false);
100
101 // Set new buffer to be wasm memory
102
103 wasm::SetInstanceMemory(module_instance, *buffer);
104
105 CHECK(wasm::UpdateWasmModuleMemory(module_instance, old_mem_start,
106 new_mem_start, old_size, new_size));
107
108 return *isolate->factory()->NewNumberFromInt(old_size /
109 wasm::WasmModule::kPageSize);
110 } 40 }
111 41
112 RUNTIME_FUNCTION(Runtime_WasmThrowTypeError) { 42 RUNTIME_FUNCTION(Runtime_WasmThrowTypeError) {
113 HandleScope scope(isolate); 43 HandleScope scope(isolate);
114 DCHECK_EQ(0, args.length()); 44 DCHECK_EQ(0, args.length());
115 THROW_NEW_ERROR_RETURN_FAILURE( 45 THROW_NEW_ERROR_RETURN_FAILURE(
116 isolate, NewTypeError(MessageTemplate::kWasmTrapTypeError)); 46 isolate, NewTypeError(MessageTemplate::kWasmTrapTypeError));
117 } 47 }
118 48
119 RUNTIME_FUNCTION(Runtime_WasmThrow) { 49 RUNTIME_FUNCTION(Runtime_WasmThrow) {
(...skipping 13 matching lines...) Expand all
133 Object* exception = args[0]; 63 Object* exception = args[0];
134 // The unwinder will only deliver exceptions to wasm if the exception is a 64 // The unwinder will only deliver exceptions to wasm if the exception is a
135 // Number or a Smi (which we have just converted to a Number.) This logic 65 // Number or a Smi (which we have just converted to a Number.) This logic
136 // lives in Isolate::is_catchable_by_wasm(Object*). 66 // lives in Isolate::is_catchable_by_wasm(Object*).
137 CHECK(exception->IsNumber()); 67 CHECK(exception->IsNumber());
138 return exception; 68 return exception;
139 } 69 }
140 70
141 } // namespace internal 71 } // namespace internal
142 } // namespace v8 72 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/wasm/wasm-module.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698