Commit c0ee8f90 authored by Benedikt Meurer's avatar Benedikt Meurer Committed by Commit Bot

[wasm][inspector] Don't use `Script::source_url` to store URL.

The `Script::source_url` field holds the value of the magic
`//# sourceURL` comment if found, and the `Script::name` field is
supposed to hold the actual name of the resource (as provided by
the embedder ideally), in case of Chromium that's supposed to be
the URL (in case of Node.js it's often the local path).

Using `source_url` worked by chance so far, but for loading DWARF
symbol files correctly we need the initiator (which we pick from
the embedderName of the Script as reported to DevTools). More
importantly, the partial handling of `//# sourceURL` in V8 is a
layering violation and causes trouble in DevTools, i.e. when users
put relative paths here. So as part of refactoring and correctifying
the handling of `//# sourceURL`, we need to make sure that the embedder
provided name (the URL in case of Chromium) is always stored in the
`Script::name` field.

Bug: chromium:1183990, chromium:974543, chromium:1174507
Change-Id: I32e11def2b9b52be11bd2e0e64a2ab6bdcf5e52d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2773584
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73536}
parent ce85e66a
......@@ -7400,13 +7400,12 @@ Local<ArrayBuffer> v8::WasmMemoryObject::Buffer() {
CompiledWasmModule WasmModuleObject::GetCompiledModule() {
#if V8_ENABLE_WEBASSEMBLY
i::Handle<i::WasmModuleObject> obj =
i::Handle<i::WasmModuleObject>::cast(Utils::OpenHandle(this));
auto source_url = i::String::cast(obj->script().source_url());
auto obj = i::Handle<i::WasmModuleObject>::cast(Utils::OpenHandle(this));
auto url =
i::handle(i::String::cast(obj->script().name()), obj->GetIsolate());
int length;
std::unique_ptr<char[]> cstring = source_url.ToCString(
i::DISALLOW_NULLS, i::FAST_STRING_TRAVERSAL, &length);
i::Handle<i::String> url(source_url, obj->GetIsolate());
std::unique_ptr<char[]> cstring =
url->ToCString(i::DISALLOW_NULLS, i::FAST_STRING_TRAVERSAL, &length);
return CompiledWasmModule(std::move(obj->shared_native_module()),
cstring.get(), length);
#else
......
......@@ -232,13 +232,9 @@ void ProfilerListener::CodeCreateEvent(LogEventsAndTags tag,
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
rec->instruction_start = code->instruction_start();
// Wasm modules always have a source URL. Asm.js modules never have one.
DCHECK_EQ(code->native_module()->module()->origin == wasm::kWasmOrigin,
source_url != nullptr);
rec->entry = new CodeEntry(
tag, GetName(name),
source_url ? GetName(source_url) : CodeEntry::kEmptyResourceName, 1,
code_offset + 1, nullptr, true, CodeEntry::CodeType::WASM);
rec->entry =
new CodeEntry(tag, GetName(name), GetName(source_url), 1, code_offset + 1,
nullptr, true, CodeEntry::CodeType::WASM);
rec->entry->set_script_id(script_id);
rec->entry->set_position(code_offset);
rec->instruction_size = code->instructions().length();
......
......@@ -1152,12 +1152,11 @@ bool CompileLazy(Isolate* isolate, Handle<WasmModuleObject> module_object,
if (WasmCode::ShouldBeLogged(isolate)) {
DisallowGarbageCollection no_gc;
Object source_url_obj = module_object->script().source_url();
DCHECK(source_url_obj.IsString() || source_url_obj.IsUndefined());
std::unique_ptr<char[]> source_url =
source_url_obj.IsString() ? String::cast(source_url_obj).ToCString()
: nullptr;
code->LogCode(isolate, source_url.get(), module_object->script().id());
Object url_obj = module_object->script().name();
DCHECK(url_obj.IsString() || url_obj.IsUndefined());
std::unique_ptr<char[]> url =
url_obj.IsString() ? String::cast(url_obj).ToCString() : nullptr;
code->LogCode(isolate, url.get(), module_object->script().id());
}
counters->wasm_lazily_compiled_functions()->Increment();
......
......@@ -872,11 +872,10 @@ void NativeModule::LogWasmCodes(Isolate* isolate, Script script) {
TRACE_EVENT1("v8.wasm", "wasm.LogWasmCodes", "functions",
module_->num_declared_functions);
Object source_url_obj = script.source_url();
DCHECK(source_url_obj.IsString() || source_url_obj.IsUndefined());
Object url_obj = script.name();
DCHECK(url_obj.IsString() || url_obj.IsUndefined());
std::unique_ptr<char[]> source_url =
source_url_obj.IsString() ? String::cast(source_url_obj).ToCString()
: nullptr;
url_obj.IsString() ? String::cast(url_obj).ToCString() : nullptr;
// Log all owned code, not just the current entries in the code table. This
// will also include import wrappers.
......
......@@ -130,11 +130,10 @@ class WasmGCForegroundTask : public CancelableTask {
class WeakScriptHandle {
public:
explicit WeakScriptHandle(Handle<Script> script) : script_id_(script->id()) {
DCHECK(script->source_url().IsString() ||
script->source_url().IsUndefined());
if (script->source_url().IsString()) {
DCHECK(script->name().IsString() || script->name().IsUndefined());
if (script->name().IsString()) {
std::unique_ptr<char[]> source_url =
String::cast(script->source_url()).ToCString();
String::cast(script->name()).ToCString();
// Convert from {unique_ptr} to {shared_ptr}.
source_url_ = {source_url.release(), source_url.get_deleter()};
}
......@@ -802,7 +801,7 @@ Handle<Script> CreateWasmScript(Isolate* isolate,
.ToHandleChecked();
}
}
script->set_source_url(*url_str);
script->set_name(*url_str);
const WasmDebugSymbols& debug_symbols = module->debug_symbols;
if (debug_symbols.type == WasmDebugSymbols::Type::SourceMap &&
......
......@@ -205,8 +205,8 @@ TEST(DeserializeWithSourceUrl) {
const std::string url = "http://example.com/example.wasm";
Handle<WasmModuleObject> module_object;
CHECK(test.Deserialize(VectorOf(url)).ToHandle(&module_object));
String source_url = String::cast(module_object->script().source_url());
CHECK_EQ(url, source_url.ToCString().get());
String url_str = String::cast(module_object->script().name());
CHECK_EQ(url, url_str.ToCString().get());
}
test.CollectGarbage();
}
......
......@@ -564,8 +564,7 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) {
DCHECK_NOT_NULL(code);
DisallowGarbageCollection no_gc;
Script script = builder_->instance_object()->module_object().script();
std::unique_ptr<char[]> source_url =
String::cast(script.source_url()).ToCString();
std::unique_ptr<char[]> source_url = String::cast(script.name()).ToCString();
if (WasmCode::ShouldBeLogged(isolate())) {
code->LogCode(isolate(), source_url.get(), script.id());
}
......
......@@ -88,11 +88,11 @@ Error.prepareStackTrace = function(error, frames) {
module.exports.main();
verifyStack(stack, [
// isWasm function line pos file offset funcIndex
[ false, "STACK", 38, 0, "stack.js"],
[ true, "main", 1, 0x86, null, '0x86', 1],
[ false, "testStackFrames", 88, 0, "stack.js"],
[ false, null, 97, 0, "stack.js"]
// isWasm function line pos file offset funcIndex
[ false, "STACK", 38, 0, "stack.js"],
[ true, "main", 1, 0x86, "wasm://wasm/7168ab72", '0x86', 1],
[ false, "testStackFrames", 88, 0, "stack.js"],
[ false, null, 97, 0, "stack.js"]
]);
})();
......@@ -103,10 +103,10 @@ Error.prepareStackTrace = function(error, frames) {
} catch (e) {
assertContains("unreachable", e.message);
verifyStack(e.stack, [
// isWasm function line pos file offset funcIndex
[ true, "exec_unreachable", 1, 0x8b, null, '0x8b', 2],
[ false, "testWasmUnreachable", 101, 0, "stack.js"],
[ false, null, 112, 0, "stack.js"]
// isWasm function line pos file offset funcIndex
[ true, "exec_unreachable", 1, 0x8b, "wasm://wasm/7168ab72", '0x8b', 2],
[ false, "testWasmUnreachable", 101, 0, "stack.js"],
[ false, null, 112, 0, "stack.js"]
]);
}
})();
......@@ -118,11 +118,11 @@ Error.prepareStackTrace = function(error, frames) {
} catch (e) {
assertContains("out of bounds", e.message);
verifyStack(e.stack, [
// isWasm function line pos file offset funcIndex
[ true, "mem_out_of_bounds", 1, 0x91, null, '0x91', 3],
[ true, "call_mem_out_of_bounds", 1, 0x97, null, '0x97', 4],
[ false, "testWasmMemOutOfBounds", 116, 0, "stack.js"],
[ false, null, 128, 0, "stack.js"]
// isWasm function line pos file offset funcIndex
[ true, "mem_out_of_bounds", 1, 0x91, "wasm://wasm/7168ab72", '0x91', 3],
[ true, "call_mem_out_of_bounds", 1, 0x97, "wasm://wasm/7168ab72", '0x97', 4],
[ false, "testWasmMemOutOfBounds", 116, 0, "stack.js"],
[ false, null, 128, 0, "stack.js"]
]);
}
})();
......@@ -147,11 +147,11 @@ Error.prepareStackTrace = function(error, frames) {
assertEquals("Maximum call stack size exceeded", e.message, "trap reason");
assertTrue(e.stack.length >= 4, "expected at least 4 stack entries");
verifyStack(e.stack.splice(0, 4), [
// isWasm function line pos file offset funcIndex
[ true, "recursion", 1, 0x34, null, '0x34', 0],
[ true, "recursion", 1, 0x37, null, '0x37', 0],
[ true, "recursion", 1, 0x37, null, '0x37', 0],
[ true, "recursion", 1, 0x37, null, '0x37', 0]
// isWasm function line pos file offset funcIndex
[ true, "recursion", 1, 0x34, "wasm://wasm/80a35e5a", '0x34', 0],
[ true, "recursion", 1, 0x37, "wasm://wasm/80a35e5a", '0x37', 0],
[ true, "recursion", 1, 0x37, "wasm://wasm/80a35e5a", '0x37', 0],
[ true, "recursion", 1, 0x37, "wasm://wasm/80a35e5a", '0x37', 0]
]);
}
})();
......@@ -175,10 +175,10 @@ Error.prepareStackTrace = function(error, frames) {
assertEquals('unreachable', e.message, 'trap reason');
let hexOffset = '0x' + (unreachable_pos + 0x25).toString(16);
verifyStack(e.stack, [
// isWasm function line pos file offset funcIndex
[ true, 'main', 1, unreachable_pos + 0x25, null, hexOffset, 0],
[ false, 'testBigOffset', 172, 0, 'stack.js'],
[ false, null, 184, 0, 'stack.js']
// isWasm function line pos file offset funcIndex
[ true, 'main', 1, unreachable_pos + 0x25, 'wasm://wasm/000600e6', hexOffset, 0],
[ false, 'testBigOffset', 172, 0, 'stack.js'],
[ false, null, 184, 0, 'stack.js']
]);
}
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment