diff options
Diffstat (limited to 'deps/v8/test/cctest/wasm/wasm-run-utils.cc')
-rw-r--r-- | deps/v8/test/cctest/wasm/wasm-run-utils.cc | 133 |
1 files changed, 71 insertions, 62 deletions
diff --git a/deps/v8/test/cctest/wasm/wasm-run-utils.cc b/deps/v8/test/cctest/wasm/wasm-run-utils.cc index 33090cfb2a..4fa1fb1c7a 100644 --- a/deps/v8/test/cctest/wasm/wasm-run-utils.cc +++ b/deps/v8/test/cctest/wasm/wasm-run-utils.cc @@ -15,7 +15,7 @@ namespace wasm { TestingModuleBuilder::TestingModuleBuilder( Zone* zone, WasmExecutionMode mode, - compiler::RuntimeExceptionSupport exception_support) + compiler::RuntimeExceptionSupport exception_support, LowerSimd lower_simd) : test_module_ptr_(&test_module_), isolate_(CcTest::InitIsolateOnce()), global_offset(0), @@ -24,7 +24,7 @@ TestingModuleBuilder::TestingModuleBuilder( interpreter_(nullptr), execution_mode_(mode), runtime_exception_support_(exception_support), - lower_simd_(mode == kExecuteSimdLowered) { + lower_simd_(lower_simd) { WasmJs::Install(isolate_, true); test_module_.globals_size = kMaxGlobalsSize; memset(globals_data_, 0, sizeof(globals_data_)); @@ -41,9 +41,9 @@ byte* TestingModuleBuilder::AddMemory(uint32_t size) { DCHECK(!instance_object_->has_memory_object()); test_module_.has_memory = true; const bool enable_guard_regions = - trap_handler::UseTrapHandler() && test_module_.is_wasm(); + trap_handler::IsTrapHandlerEnabled() && test_module_.is_wasm(); uint32_t alloc_size = - enable_guard_regions ? RoundUp(size, base::OS::CommitPageSize()) : size; + enable_guard_regions ? RoundUp(size, CommitPageSize()) : size; Handle<JSArrayBuffer> new_buffer = wasm::NewArrayBuffer(isolate_, alloc_size, enable_guard_regions); CHECK(!new_buffer.is_null()); @@ -96,17 +96,15 @@ uint32_t TestingModuleBuilder::AddJsFunction( *v8::Local<v8::Function>::Cast(CompileRun(source)))); uint32_t index = AddFunction(sig, nullptr); js_imports_table->set(0, *isolate_->native_context()); + // TODO(6792): No longer needed once WebAssembly code is off heap. + CodeSpaceMemoryModificationScope modification_scope(isolate_->heap()); + Handle<Code> code = compiler::CompileWasmToJSWrapper( + isolate_, jsfunc, sig, index, test_module_.origin(), + trap_handler::IsTrapHandlerEnabled(), js_imports_table); if (FLAG_wasm_jit_to_native) { native_module_->ResizeCodeTableForTest(index); - Handle<Code> wrapper = compiler::CompileWasmToJSWrapper( - isolate_, jsfunc, sig, index, test_module_.origin(), js_imports_table); - native_module_->AddCodeCopy(wrapper, wasm::WasmCode::WasmToJsWrapper, - index); + native_module_->AddCodeCopy(code, wasm::WasmCode::kWasmToJsWrapper, index); } else { - // TODO(6792): No longer needed once WebAssembly code is off heap. - CodeSpaceMemoryModificationScope modification_scope(isolate_->heap()); - Handle<Code> code = compiler::CompileWasmToJSWrapper( - isolate_, jsfunc, sig, index, test_module_.origin(), js_imports_table); function_code_[index] = code; } return index; @@ -120,10 +118,11 @@ Handle<JSFunction> TestingModuleBuilder::WrapCode(uint32_t index) { : WasmCodeWrapper(function_code_[index]); byte* context_address = test_module_.has_memory - ? reinterpret_cast<byte*>(instance_object_->wasm_context()) + ? reinterpret_cast<byte*>(instance_object_->wasm_context()->get()) : nullptr; Handle<Code> ret_code = compiler::CompileJSToWasmWrapper( - isolate_, &test_module_, code, index, context_address); + isolate_, &test_module_, code, index, context_address, + trap_handler::IsTrapHandlerEnabled()); Handle<JSFunction> ret = WasmExportedFunction::New( isolate_, instance_object(), MaybeHandle<String>(), static_cast<int>(index), @@ -133,19 +132,20 @@ Handle<JSFunction> TestingModuleBuilder::WrapCode(uint32_t index) { // Add weak reference to exported functions. Handle<WasmCompiledModule> compiled_module( instance_object()->compiled_module(), isolate_); - Handle<FixedArray> old_arr = compiled_module->weak_exported_functions(); + Handle<FixedArray> old_arr(compiled_module->weak_exported_functions(), + isolate_); Handle<FixedArray> new_arr = isolate_->factory()->NewFixedArray(old_arr->length() + 1); old_arr->CopyTo(0, *new_arr, 0, old_arr->length()); Handle<WeakCell> weak_fn = isolate_->factory()->NewWeakCell(ret); new_arr->set(old_arr->length(), *weak_fn); - compiled_module->set_weak_exported_functions(new_arr); + compiled_module->set_weak_exported_functions(*new_arr); return ret; } -void TestingModuleBuilder::AddIndirectFunctionTable(uint16_t* function_indexes, - uint32_t table_size) { +void TestingModuleBuilder::AddIndirectFunctionTable( + const uint16_t* function_indexes, uint32_t table_size) { test_module_.function_tables.emplace_back(); WasmIndirectFunctionTable& table = test_module_.function_tables.back(); table.initial_size = table_size; @@ -155,14 +155,10 @@ void TestingModuleBuilder::AddIndirectFunctionTable(uint16_t* function_indexes, table.values.push_back(function_indexes[i]); } + FixedArray* func_table = *isolate_->factory()->NewFixedArray( + table_size * compiler::kFunctionTableEntrySize); function_tables_.push_back( - isolate_->global_handles() - ->Create(*isolate_->factory()->NewFixedArray(table_size)) - .address()); - signature_tables_.push_back( - isolate_->global_handles() - ->Create(*isolate_->factory()->NewFixedArray(table_size)) - .address()); + isolate_->global_handles()->Create(func_table).address()); } void TestingModuleBuilder::PopulateIndirectFunctionTable() { @@ -172,30 +168,32 @@ void TestingModuleBuilder::PopulateIndirectFunctionTable() { WasmIndirectFunctionTable& table = test_module_.function_tables[i]; Handle<FixedArray> function_table( reinterpret_cast<FixedArray**>(function_tables_[i])); - Handle<FixedArray> signature_table( - reinterpret_cast<FixedArray**>(signature_tables_[i])); int table_size = static_cast<int>(table.values.size()); for (int j = 0; j < table_size; j++) { WasmFunction& function = test_module_.functions[table.values[j]]; - signature_table->set( - j, Smi::FromInt(test_module_.signature_map.Find(function.sig))); + function_table->set( + compiler::FunctionTableSigOffset(j), + Smi::FromInt(test_module_.signature_map.Find(function.sig))); if (FLAG_wasm_jit_to_native) { Handle<Foreign> foreign_holder = isolate_->factory()->NewForeign( native_module_->GetCode(function.func_index) ->instructions() .start(), TENURED); - function_table->set(j, *foreign_holder); + function_table->set(compiler::FunctionTableCodeOffset(j), + *foreign_holder); } else { - function_table->set(j, *function_code_[function.func_index]); + function_table->set(compiler::FunctionTableCodeOffset(j), + *function_code_[function.func_index]); } } } } uint32_t TestingModuleBuilder::AddBytes(Vector<const byte> bytes) { - Handle<SeqOneByteString> old_bytes( - instance_object_->compiled_module()->module_bytes(), isolate_); + Handle<WasmSharedModuleData> shared( + instance_object_->compiled_module()->shared(), isolate_); + Handle<SeqOneByteString> old_bytes(shared->module_bytes(), isolate_); uint32_t old_size = static_cast<uint32_t>(old_bytes->length()); // Avoid placing strings at offset 0, this might be interpreted as "not // set", e.g. for function names. @@ -205,14 +203,13 @@ uint32_t TestingModuleBuilder::AddBytes(Vector<const byte> bytes) { memcpy(new_bytes.start() + bytes_offset, bytes.start(), bytes.length()); Handle<SeqOneByteString> new_bytes_str = Handle<SeqOneByteString>::cast( isolate_->factory()->NewStringFromOneByte(new_bytes).ToHandleChecked()); - instance_object_->compiled_module()->shared()->set_module_bytes( - *new_bytes_str); + shared->set_module_bytes(*new_bytes_str); return bytes_offset; } compiler::ModuleEnv TestingModuleBuilder::CreateModuleEnv() { - return {&test_module_, function_tables_, signature_tables_, function_code_, - Handle<Code>::null()}; + return {&test_module_, function_tables_, function_code_, Handle<Code>::null(), + trap_handler::IsTrapHandlerEnabled()}; } const WasmGlobal* TestingModuleBuilder::AddGlobal(ValueType type) { @@ -243,7 +240,7 @@ Handle<WasmInstanceObject> TestingModuleBuilder::InitInstanceObject() { Handle<FixedArray> export_wrappers = isolate_->factory()->NewFixedArray(0); Handle<WasmCompiledModule> compiled_module = WasmCompiledModule::New( isolate_, test_module_ptr_, code_table, export_wrappers, function_tables_, - signature_tables_); + trap_handler::IsTrapHandlerEnabled()); compiled_module->OnWasmModuleDecodingComplete(shared_module_data); // This method is called when we initialize TestEnvironment. We don't // have a memory yet, so we won't create it here. We'll update the @@ -251,43 +248,55 @@ Handle<WasmInstanceObject> TestingModuleBuilder::InitInstanceObject() { native_module_ = compiled_module->GetNativeModule(); Handle<FixedArray> weak_exported = isolate_->factory()->NewFixedArray(0); - compiled_module->set_weak_exported_functions(weak_exported); + compiled_module->set_weak_exported_functions(*weak_exported); DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); script->set_wasm_compiled_module(*compiled_module); auto instance = WasmInstanceObject::New(isolate_, compiled_module); instance->wasm_context()->get()->globals_start = globals_data_; Handle<WeakCell> weak_instance = isolate()->factory()->NewWeakCell(instance); - compiled_module->set_weak_owning_instance(weak_instance); + compiled_module->set_weak_owning_instance(*weak_instance); return instance; } -void TestBuildingGraph( - Zone* zone, compiler::JSGraph* jsgraph, compiler::ModuleEnv* module, - FunctionSig* sig, compiler::SourcePositionTable* source_position_table, - const byte* start, const byte* end, - compiler::RuntimeExceptionSupport runtime_exception_support) { - compiler::WasmGraphBuilder builder( - module, zone, jsgraph, CEntryStub(jsgraph->isolate(), 1).GetCode(), sig, - source_position_table, runtime_exception_support); - +void TestBuildingGraphWithBuilder(compiler::WasmGraphBuilder* builder, + Zone* zone, FunctionSig* sig, + const byte* start, const byte* end) { DecodeResult result = - BuildTFGraph(zone->allocator(), &builder, sig, start, end); + BuildTFGraph(zone->allocator(), builder, sig, start, end); if (result.failed()) { +#ifdef DEBUG if (!FLAG_trace_wasm_decoder) { // Retry the compilation with the tracing flag on, to help in debugging. FLAG_trace_wasm_decoder = true; - result = BuildTFGraph(zone->allocator(), &builder, sig, start, end); + result = BuildTFGraph(zone->allocator(), builder, sig, start, end); } +#endif uint32_t pc = result.error_offset(); - std::ostringstream str; - str << "Verification failed; pc = +" << pc - << ", msg = " << result.error_msg().c_str(); - FATAL(str.str().c_str()); + FATAL("Verification failed; pc = +%x, msg = %s", pc, + result.error_msg().c_str()); } - builder.LowerInt64(); + builder->LowerInt64(); if (!CpuFeatures::SupportsWasmSimd128()) { - builder.SimdScalarLoweringForTesting(); + builder->SimdScalarLoweringForTesting(); + } +} + +void TestBuildingGraph( + Zone* zone, compiler::JSGraph* jsgraph, compiler::ModuleEnv* module, + FunctionSig* sig, compiler::SourcePositionTable* source_position_table, + const byte* start, const byte* end, + compiler::RuntimeExceptionSupport runtime_exception_support) { + if (module) { + compiler::WasmGraphBuilder builder( + module, zone, jsgraph, CEntryStub(jsgraph->isolate(), 1).GetCode(), sig, + source_position_table, runtime_exception_support); + TestBuildingGraphWithBuilder(&builder, zone, sig, start, end); + } else { + compiler::WasmGraphBuilder builder( + nullptr, zone, jsgraph, CEntryStub(jsgraph->isolate(), 1).GetCode(), + sig, source_position_table, runtime_exception_support); + TestBuildingGraphWithBuilder(&builder, zone, sig, start, end); } } @@ -428,7 +437,7 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) { if (FLAG_wasm_jit_to_native) { native_module->ResizeCodeTableForTest(function_->func_index); } - Handle<SeqOneByteString> wire_bytes(compiled_module->module_bytes(), + Handle<SeqOneByteString> wire_bytes(compiled_module->shared()->module_bytes(), isolate()); compiler::ModuleEnv module_env = builder_->CreateModuleEnv(); @@ -476,7 +485,7 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) { builder_->SetFunctionCode(function_index(), code); // Add to code table. - Handle<FixedArray> code_table = compiled_module->code_table(); + Handle<FixedArray> code_table(compiled_module->code_table(), isolate()); if (static_cast<int>(function_index()) >= code_table->length()) { Handle<FixedArray> new_arr = isolate()->factory()->NewFixedArray( static_cast<int>(function_index()) + 1); @@ -487,11 +496,11 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) { DCHECK(code_table->get(static_cast<int>(function_index())) ->IsUndefined(isolate())); code_table->set(static_cast<int>(function_index()), *code); - if (trap_handler::UseTrapHandler()) { + if (trap_handler::IsTrapHandlerEnabled()) { UnpackAndRegisterProtectedInstructionsGC(isolate(), code_table); } } else { - if (trap_handler::UseTrapHandler()) { + if (trap_handler::IsTrapHandlerEnabled()) { UnpackAndRegisterProtectedInstructions(isolate(), native_module); } } @@ -516,7 +525,7 @@ WasmFunctionCompiler::WasmFunctionCompiler(Zone* zone, FunctionSig* sig, WasmFunctionCompiler::~WasmFunctionCompiler() { if (!FLAG_wasm_jit_to_native) { - if (trap_handler::UseTrapHandler() && + if (trap_handler::IsTrapHandlerEnabled() && !builder_->GetFunctionCode(function_index()).is_null()) { const int handler_index = builder_->GetFunctionCode(function_index()) .GetCode() |