OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 <functional> | 5 #include <functional> |
6 #include <memory> | 6 #include <memory> |
7 | 7 |
8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
9 #include "src/debug/interface-types.h" | 9 #include "src/debug/interface-types.h" |
10 #include "src/frames-inl.h" | 10 #include "src/frames-inl.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 return memory; | 81 return memory; |
82 } else { | 82 } else { |
83 void* memory = | 83 void* memory = |
84 size == 0 ? nullptr : isolate->array_buffer_allocator()->Allocate(size); | 84 size == 0 ? nullptr : isolate->array_buffer_allocator()->Allocate(size); |
85 allocation_base = memory; | 85 allocation_base = memory; |
86 allocation_length = size; | 86 allocation_length = size; |
87 return memory; | 87 return memory; |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 static void MemoryInstanceFinalizer(Isolate* isolate, | |
92 WasmInstanceObject* instance) { | |
93 DisallowHeapAllocation no_gc; | |
94 // If the memory object is destroyed, nothing needs to be done here. | |
95 if (!instance->has_memory_object()) return; | |
96 Handle<WasmInstanceWrapper> instance_wrapper = | |
97 handle(instance->instance_wrapper()); | |
98 DCHECK(WasmInstanceWrapper::IsWasmInstanceWrapper(*instance_wrapper)); | |
99 DCHECK(instance_wrapper->has_instance()); | |
100 bool has_prev = instance_wrapper->has_previous(); | |
101 bool has_next = instance_wrapper->has_next(); | |
102 Handle<WasmMemoryObject> memory_object(instance->memory_object()); | |
103 | |
104 if (!has_prev && !has_next) { | |
105 memory_object->ResetInstancesLink(isolate); | |
106 return; | |
107 } else { | |
108 Handle<WasmInstanceWrapper> next_wrapper, prev_wrapper; | |
109 if (!has_prev) { | |
110 Handle<WasmInstanceWrapper> next_wrapper = | |
111 instance_wrapper->next_wrapper(); | |
112 next_wrapper->reset_previous_wrapper(); | |
113 // As this is the first link in the memory object, destroying | |
114 // without updating memory object would corrupt the instance chain in | |
115 // the memory object. | |
116 memory_object->set_instances_link(*next_wrapper); | |
117 } else if (!has_next) { | |
118 instance_wrapper->previous_wrapper()->reset_next_wrapper(); | |
119 } else { | |
120 DCHECK(has_next && has_prev); | |
121 Handle<WasmInstanceWrapper> prev_wrapper = | |
122 instance_wrapper->previous_wrapper(); | |
123 Handle<WasmInstanceWrapper> next_wrapper = | |
124 instance_wrapper->next_wrapper(); | |
125 prev_wrapper->set_next_wrapper(*next_wrapper); | |
126 next_wrapper->set_previous_wrapper(*prev_wrapper); | |
127 } | |
128 // Reset to avoid dangling pointers | |
129 instance_wrapper->reset(); | |
130 } | |
131 } | |
132 | |
133 static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) { | 91 static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) { |
134 DisallowHeapAllocation no_gc; | 92 DisallowHeapAllocation no_gc; |
135 JSObject** p = reinterpret_cast<JSObject**>(data.GetParameter()); | 93 JSObject** p = reinterpret_cast<JSObject**>(data.GetParameter()); |
136 WasmInstanceObject* owner = reinterpret_cast<WasmInstanceObject*>(*p); | 94 WasmInstanceObject* owner = reinterpret_cast<WasmInstanceObject*>(*p); |
137 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate()); | 95 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate()); |
138 // If a link to shared memory instances exists, update the list of memory | 96 // If a link to shared memory instances exists, update the list of memory |
139 // instances before the instance is destroyed. | 97 // instances before the instance is destroyed. |
140 if (owner->has_instance_wrapper()) MemoryInstanceFinalizer(isolate, owner); | |
141 WasmCompiledModule* compiled_module = owner->compiled_module(); | 98 WasmCompiledModule* compiled_module = owner->compiled_module(); |
142 TRACE("Finalizing %d {\n", compiled_module->instance_id()); | 99 TRACE("Finalizing %d {\n", compiled_module->instance_id()); |
143 DCHECK(compiled_module->has_weak_wasm_module()); | 100 DCHECK(compiled_module->has_weak_wasm_module()); |
144 WeakCell* weak_wasm_module = compiled_module->ptr_to_weak_wasm_module(); | 101 WeakCell* weak_wasm_module = compiled_module->ptr_to_weak_wasm_module(); |
145 | 102 |
146 if (trap_handler::UseTrapHandler()) { | 103 if (trap_handler::UseTrapHandler()) { |
147 Handle<FixedArray> code_table = compiled_module->code_table(); | 104 Handle<FixedArray> code_table = compiled_module->code_table(); |
148 for (int i = 0; i < code_table->length(); ++i) { | 105 for (int i = 0; i < code_table->length(); ++i) { |
149 Handle<Code> code = code_table->GetValueChecked<Code>(isolate, i); | 106 Handle<Code> code = code_table->GetValueChecked<Code>(isolate, i); |
150 int index = code->trap_handler_index()->value(); | 107 int index = code->trap_handler_index()->value(); |
151 if (index >= 0) { | 108 if (index >= 0) { |
152 trap_handler::ReleaseHandlerData(index); | 109 trap_handler::ReleaseHandlerData(index); |
153 code->set_trap_handler_index(Smi::FromInt(-1)); | 110 code->set_trap_handler_index(Smi::FromInt(-1)); |
154 } | 111 } |
155 } | 112 } |
156 } | 113 } |
157 | 114 |
| 115 // Since the order of finalizers is not guaranteed, it can be the case |
| 116 // that {instance->compiled_module()->module()}, which is a |
| 117 // {Managed<WasmModule>} has been collected earlier in this GC cycle. |
| 118 // Weak references to this instance won't be cleared until |
| 119 // the next GC cycle, so we need to manually break some links (such as |
| 120 // the weak references from {WasmMemoryObject::instances}. |
| 121 if (owner->has_memory_object()) { |
| 122 Handle<WasmMemoryObject> memory(owner->memory_object(), isolate); |
| 123 Handle<WasmInstanceObject> instance(owner, isolate); |
| 124 WasmMemoryObject::RemoveInstance(isolate, memory, instance); |
| 125 } |
| 126 |
158 // weak_wasm_module may have been cleared, meaning the module object | 127 // weak_wasm_module may have been cleared, meaning the module object |
159 // was GC-ed. In that case, there won't be any new instances created, | 128 // was GC-ed. In that case, there won't be any new instances created, |
160 // and we don't need to maintain the links between instances. | 129 // and we don't need to maintain the links between instances. |
161 if (!weak_wasm_module->cleared()) { | 130 if (!weak_wasm_module->cleared()) { |
162 WasmModuleObject* wasm_module = | 131 WasmModuleObject* wasm_module = |
163 WasmModuleObject::cast(weak_wasm_module->value()); | 132 WasmModuleObject::cast(weak_wasm_module->value()); |
164 WasmCompiledModule* current_template = wasm_module->compiled_module(); | 133 WasmCompiledModule* current_template = wasm_module->compiled_module(); |
165 | 134 |
166 TRACE("chain before {\n"); | 135 TRACE("chain before {\n"); |
167 TRACE_CHAIN(current_template); | 136 TRACE_CHAIN(current_template); |
(...skipping 983 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1151 callee_compiled->instruction_start()); | 1120 callee_compiled->instruction_start()); |
1152 } | 1121 } |
1153 DCHECK_EQ(non_compiled_functions.size(), idx); | 1122 DCHECK_EQ(non_compiled_functions.size(), idx); |
1154 } | 1123 } |
1155 | 1124 |
1156 Code* ret = | 1125 Code* ret = |
1157 Code::cast(compiled_module->code_table()->get(func_to_return_idx)); | 1126 Code::cast(compiled_module->code_table()->get(func_to_return_idx)); |
1158 DCHECK_EQ(Code::WASM_FUNCTION, ret->kind()); | 1127 DCHECK_EQ(Code::WASM_FUNCTION, ret->kind()); |
1159 return handle(ret, isolate); | 1128 return handle(ret, isolate); |
1160 } | 1129 } |
OLD | NEW |